73 lines
1.3 KiB
C
73 lines
1.3 KiB
C
|
/* int _AddUnsArrToUnsArr(src1,src2,dst,n)
|
||
|
*
|
||
|
* ARGUMENT
|
||
|
* unsigned *src1, *src2, *dst;
|
||
|
* unsigned n;
|
||
|
*
|
||
|
* DESCRIPTION
|
||
|
* Adds src1 to src2 giving dst. src1 and src2 are unchanged.
|
||
|
* Src1, src2 and dst point to unsigned arrays.
|
||
|
*
|
||
|
* SIDE EFFECTS
|
||
|
*
|
||
|
*
|
||
|
* RETURNS
|
||
|
* GM_SUCCESS if no overflow, otherwise GM_FAILURE.
|
||
|
*
|
||
|
* AUTHOR
|
||
|
* Brugnoli Giugno 1992
|
||
|
*
|
||
|
* MODIFICATIONS
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <math.h>
|
||
|
#include "gm.h"
|
||
|
#include "gmsystem.h"
|
||
|
|
||
|
extern double pow(double, double);
|
||
|
|
||
|
int _AddUnsArrToUnsArr(src1, src2, dst, n)
|
||
|
unsigned SHORT src1[], src2[], dst[];
|
||
|
int n;
|
||
|
{
|
||
|
int i;
|
||
|
unsigned long pdst,maxunsint;
|
||
|
unsigned carry;
|
||
|
|
||
|
carry=0;
|
||
|
#ifdef SH_LIB
|
||
|
maxunsint=1;
|
||
|
for (i=1;i<=BITSPERUI;i++) maxunsint*=2;
|
||
|
maxunsint--;
|
||
|
#else
|
||
|
maxunsint=(unsigned long)pow(2.,(double)BITSPERUI)-1;
|
||
|
#endif
|
||
|
|
||
|
if (n>0)
|
||
|
{
|
||
|
for (i=0;i<n;i++)
|
||
|
{
|
||
|
pdst=carry+(unsigned long)src1[i];
|
||
|
pdst+=(unsigned long)src2[i];
|
||
|
if (pdst>maxunsint)
|
||
|
{
|
||
|
carry=(unsigned)(pdst >> BITSPERUI );
|
||
|
dst[i]=(unsigned SHORT )(pdst & maxunsint );
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
carry=0;
|
||
|
dst[i]=(unsigned SHORT)pdst;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (dst[n-1]>>(BITSPERUI-1))
|
||
|
return(GM_FAILURE);
|
||
|
}
|
||
|
|
||
|
return(GM_SUCCESS);
|
||
|
}
|
||
|
|