campo-sirio/gfm/mul10.c

88 lines
1.5 KiB
C
Raw Normal View History

/* int _MulUnsArrByPwrOf10(pa,n,w)
*
* ARGUMENT
* unsigned pa[];
* int n; power of 10 to multiply by
* int w; number of digits
*
* DESCRIPTION
* Multiplies the pa[] array by 10^n (0<n<47),
*
* SIDE EFFECTS
* The value of pa is unchanged on overflow.
*
* RETURNS
* GM_SUCCES if no overflow, otherwise GM_OVERFLOW
*
* AUTHOR
* Brugnoli Giugno 1992
*
* MODIFICATIONS
*
*/
#include <stdio.h>
#include <math.h>
#include "gm.h"
#include "gmsystem.h"
int Multiply(sr,num,m)
unsigned SHORT sr[],num;
int m;
{
int i;
unsigned long pdst[10];
unsigned SHORT carry = 0;
for (i=0;i<m;i++)
pdst[i]=(unsigned long)sr[i]*num;
for (i=0;i<m;i++)
{
pdst[i]+=carry;
if (pdst[i]>MAXUNSINT)
{
carry=(unsigned SHORT)(pdst[i]>>BITSPERUI);
sr[i]=(unsigned SHORT)(pdst[i]&MAXUNSINT);
}
else
{
carry=0;
sr[i]=(unsigned SHORT)pdst[i];
}
}
return( (carry)? GM_OVERFLOW : GM_SUCCESS );
}
int _MulUnsArrByPwrOf10(pa,n,w)
unsigned SHORT pa[];
int n,w;
{
int k=GM_SUCCESS;
if (n>47)
return GM_OVERFLOW;
while (n>4)
{
k = Multiply(pa,10000,w);
if (k != GM_SUCCESS)
return k;
n-=4;
}
switch (n)
{
case 0: k=Multiply(pa,1,w); break;
case 1: k=Multiply(pa,10,w); break;
case 2: k=Multiply(pa,100,w); break;
case 3: k=Multiply(pa,1000,w); break;
case 4: k=Multiply(pa,10000,w); break;
default: break;
}
return(k);
}