78 lines
1.3 KiB
C
78 lines
1.3 KiB
C
|
/* int _SubUnsArrFromUnsArr(src1,src2,dst,n)
|
||
|
*
|
||
|
* ARGUMENT
|
||
|
* unsigned *src1, *src2, *dst;
|
||
|
* unsigned n;
|
||
|
*
|
||
|
* DESCRIPTION
|
||
|
* Subtracts src2 from 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 _SubUnsArrFromUnsArr(src1, src2, dst, n)
|
||
|
unsigned SHORT src1[], src2[], dst[];
|
||
|
int n;
|
||
|
{
|
||
|
int i;
|
||
|
unsigned long psrc,maxunsint;
|
||
|
unsigned SHORT carry;
|
||
|
|
||
|
carry=0;
|
||
|
#ifdef SH_LIB
|
||
|
maxunsint=1;
|
||
|
for (i=1;i<=BITSPERUI;i++) maxunsint*=2;
|
||
|
maxunsint--;
|
||
|
#else
|
||
|
maxunsint=(unsigned long)pow(2.,(float)BITSPERUI)-1;
|
||
|
#endif
|
||
|
|
||
|
if (n>0)
|
||
|
{
|
||
|
for (i=0;i<n;i++)
|
||
|
{
|
||
|
if (src2[i]>src1[i])
|
||
|
{
|
||
|
psrc=maxunsint+0x1L+(long)src1[i]-(long)carry;
|
||
|
carry=1;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
psrc=src1[i]-carry;
|
||
|
carry=0;
|
||
|
}
|
||
|
dst[i]=(unsigned SHORT)(psrc-(unsigned long)src2[i]);
|
||
|
}
|
||
|
if (carry)
|
||
|
{
|
||
|
return(GM_FAILURE);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return(GM_SUCCESS);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return(GM_SUCCESS);
|
||
|
}
|
||
|
}
|