home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume8
/
libhoward
/
part05
/
smp2ul.c
< prev
Wrap
C/C++ Source or Header
|
1989-10-01
|
3KB
|
118 lines
/*
* smp2ul - convert simple multiple precision number to unsigned long
*/
#ifndef lint
static char _cpyrgt[] = "Copyright 1989 Howard Lee Gayle";
#endif lint
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 1,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <howard/port.h>
#include <howard/version.h>
MODVER ("@(#)$Header: smp2ul.c,v 1.7 89/08/15 11:14:04 howard Exp $");
#include <errno.h>
#include <limits.h>
#include <howard/registers.i>
#include <howard/simultipre.i>
#include <howard/smp.h>
PUBLIC int smp2ul (sp, lp)
R1 smpT *sp; /* Points to SMP.*/
R5 ulongT *lp; /* Points to where to store result.*/
/* Function:
*
* Algorithm:
* Step downward through higher order SMP elements that ought to
* be zero, and make sure they are. Make sure high order bits in
* the highest order element are also zero. Or each element into
* the result. Check for a negative result. Store the result.
* Returns:
*
* Notes:
*
*/
{
R4 ulongT res = 0; /* Result.*/
R2 smpElT *p1; /* Steps through sp->smp_el[].*/
R3 smpElT *p2; /* Highest order smp_el used..*/
if (((smpT *) NULL) == sp) return (EINVAL);
p2 = &sp->smp_el[SMPNUL - 1];
for (p1 = &sp->smp_el[SMPNEL - 1]; p1 != p2;)
if (0 != *p1--) return (ERANGE);
if (0 != (*p1 >> SMPULHB)) return (ERANGE);
while (p1 >= sp->smp_el)
res = (res << SMPEB) | *p1--;
if (sp->smp_neg && (0 != res)) return (ERANGE);
if (((ulongT *) NULL) != lp) *lp = res;
return (SUCCESS);
}
#ifdef TEST
#include <howard/usage.h>
MAINVER ("@(#)$Header: smp2ul.c,v 1.7 89/08/15 11:14:04 howard Exp $");
USAGE ("");
#include <howard/malf.h>
PRIVATE void t (num, neg, hi, lo, xrc, xres)
int num; /* Test number.*/
boolT neg; /* smp_neg */
smpElT hi; /* smp_el[1] */
smpElT lo; /* smp_el[0] */
int xrc; /* Expected return code.*/
ulongT xres; /* Expected result.*/
{
int rc; /* Return code.*/
ulongT res; /* Result stored here.*/
smpT smp; /* SMP argument.*/
smp.smp_neg = neg;
smp.smp_el[0] = lo;
smp.smp_el[1] = hi;
rc = smp2ul (&smp, &res);
if (rc != xrc) PRINTF ("%d: rc %d expected %d\n", num, rc, xrc);
if ((0 == rc) && (0 == xrc) && (res != xres))
PRINTF ("%d: res %lX expected %lX\n", num, res, xres);
}
PUBLIC int main ()
{
/* num sgn hi lo rc res */
t(__LINE__, 0, 0, 0, 0, (ulongT) 0);
t(__LINE__, 1, 0, 0, 0, (ulongT) 0);
t(__LINE__, 0, 0, 1, 0, (ulongT) 1);
t(__LINE__, 0, 0x7FFF, 0xFFFF, 0, (ulongT) 0x7FFFFFFF);
t(__LINE__, 0, 0x8000, 0, 0, (ulongT) 0x80000000);
t(__LINE__, 0, 0xFFFF, 0xFFFF, 0, (ulongT) 0xFFFFFFFF);
t(__LINE__, 0,0x10000, 0x0000, ERANGE, (ulongT) 0);
t(__LINE__, 1, 0, 1, ERANGE, (ulongT) 0);
mfflush (stdout, "Standard Output");
exit (SUCCESS);
#ifdef lint
return (SUCCESS);
#endif
}
#endif