2002-02-26 12:19:02 +00:00
|
|
|
|
/* void _DivUnsArrByUnsArr(c,a,m,b,n,rf)
|
|
|
|
|
*
|
|
|
|
|
* ARGUMENT
|
|
|
|
|
* unsigned a[],b[],c[];
|
|
|
|
|
* int rf rounding flag
|
|
|
|
|
* int n,m; number of digits
|
|
|
|
|
*
|
|
|
|
|
* DESCRIPTION
|
|
|
|
|
* Divides an m-word integer a by a n-word integer b, storing the
|
|
|
|
|
* result in c. m must be >=n. The quotient is rounded when rf is set,
|
|
|
|
|
* otherwise it is truncated.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* SIDE EFFECTS
|
|
|
|
|
* None.
|
|
|
|
|
*
|
|
|
|
|
* RETURNS
|
|
|
|
|
* None.
|
|
|
|
|
*
|
|
|
|
|
* AUTHOR
|
|
|
|
|
* Brugnoli Giugno 1992
|
|
|
|
|
*
|
|
|
|
|
* MODIFICATIONS
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include "gm.h"
|
|
|
|
|
#include "gmsystem.h"
|
|
|
|
|
|
|
|
|
|
void _DivUnsArrByUnsArr(c,a,m,b,n,rf)
|
|
|
|
|
unsigned SHORT a[],c[],b[];
|
|
|
|
|
int n,m,rf;
|
|
|
|
|
{
|
2004-01-26 08:50:57 +00:00
|
|
|
|
int i,j,k,nc,ft = 0;
|
2004-03-09 09:49:17 +00:00
|
|
|
|
unsigned long dvd,pqt,pp;
|
2002-02-26 12:19:02 +00:00
|
|
|
|
float nqt;
|
|
|
|
|
unsigned SHORT copa[10],copb[10],af[10],ppt[10],ppd[10],pqa[2];
|
|
|
|
|
|
|
|
|
|
for(j=0;j<10;j++)
|
|
|
|
|
af[j]=copa[j]=copb[j]=0;
|
2004-03-09 09:49:17 +00:00
|
|
|
|
|
|
|
|
|
for(j=0;j<n;j++)
|
2002-02-26 12:19:02 +00:00
|
|
|
|
copb[j]=b[j];
|
2004-03-09 09:49:17 +00:00
|
|
|
|
|
|
|
|
|
for(j=0;j<m;j++)
|
2002-02-26 12:19:02 +00:00
|
|
|
|
copa[j]=a[j];
|
2004-03-09 09:49:17 +00:00
|
|
|
|
|
|
|
|
|
for(j=0;j<5;j++)
|
2002-02-26 12:19:02 +00:00
|
|
|
|
c[j]=0;
|
2004-03-09 09:49:17 +00:00
|
|
|
|
|
2002-02-26 12:19:02 +00:00
|
|
|
|
copa[m]=0;
|
|
|
|
|
nc=m;
|
|
|
|
|
af[0]=1;
|
|
|
|
|
|
|
|
|
|
for(i=m-n;i>=0;i--)
|
2004-03-09 09:49:17 +00:00
|
|
|
|
{
|
|
|
|
|
dvd=(long)copa[nc]*(MAXUNSINT+1)+(long)copa[nc-1];
|
2002-02-26 12:19:02 +00:00
|
|
|
|
pqt=dvd/copb[n-1];
|
|
|
|
|
pqa[1]=(unsigned SHORT)(pqt>>16);
|
2004-03-09 09:49:17 +00:00
|
|
|
|
pqa[0]=(unsigned SHORT)(pqt&MAXUNSINT);
|
2002-02-26 12:19:02 +00:00
|
|
|
|
for(j=0;j<10;j++)
|
|
|
|
|
ppd[j]=((j>n-1)?0:copb[j]);
|
2004-03-09 09:49:17 +00:00
|
|
|
|
|
|
|
|
|
for(j=1;j<=i;j++)
|
|
|
|
|
{
|
2002-02-26 12:19:02 +00:00
|
|
|
|
for(k=9;k>=1;k--)
|
|
|
|
|
ppd[k]=ppd[k-1];
|
2004-03-09 09:49:17 +00:00
|
|
|
|
|
|
|
|
|
ppd[0]=0;
|
|
|
|
|
}
|
2002-02-26 12:19:02 +00:00
|
|
|
|
_MulUnsArrByUnsArr(ppd,pqa,ppt,10,2,ft);
|
|
|
|
|
if (_CompareUnsArr(copa,ppt,nc+1)==-1)
|
2004-03-09 09:49:17 +00:00
|
|
|
|
{
|
|
|
|
|
pp=(long)ppt[nc]*(MAXUNSINT+1)+(long)ppt[nc-1];
|
2002-02-26 12:19:02 +00:00
|
|
|
|
nqt=(float)dvd;
|
|
|
|
|
nqt/=(float)pp;
|
|
|
|
|
nqt*=(float)pqt;
|
|
|
|
|
nqt+=(float)5;
|
2004-03-09 09:49:17 +00:00
|
|
|
|
// Brugnoli was HERE
|
|
|
|
|
pqa[1]=(unsigned SHORT)(((long int)nqt) >> BITSPERUI);
|
|
|
|
|
pqa[0]=(unsigned SHORT)(((long int)nqt) & MAXUNSINT);
|
|
|
|
|
|
2002-02-26 12:19:02 +00:00
|
|
|
|
_MulUnsArrByUnsArr(ppd,pqa,ppt,10,2,ft);
|
2004-03-09 09:49:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (_CompareUnsArr(copa,ppt,nc+1)==-1)
|
|
|
|
|
{
|
2002-02-26 12:19:02 +00:00
|
|
|
|
_SubUnsArr(pqa,af,2);
|
|
|
|
|
_MulUnsArrByUnsArr(ppd,pqa,ppt,10,2,ft);
|
2004-03-09 09:49:17 +00:00
|
|
|
|
}
|
2002-02-26 12:19:02 +00:00
|
|
|
|
c[i]=pqa[0];
|
|
|
|
|
_SubUnsArr(copa,ppt,m);
|
|
|
|
|
nc--;
|
2004-03-09 09:49:17 +00:00
|
|
|
|
}
|
2002-02-26 12:19:02 +00:00
|
|
|
|
|
|
|
|
|
/* qui copa <20> = al resto della divisione!! */
|
|
|
|
|
|
|
|
|
|
if (rf)
|
2004-03-09 09:49:17 +00:00
|
|
|
|
{
|
2002-02-26 12:19:02 +00:00
|
|
|
|
_SubUnsArr(copb,af,n);
|
|
|
|
|
_HalveUnsArr(copb,n);
|
|
|
|
|
if (_CompareUnsArr(copa,copb,m)==1)
|
|
|
|
|
_IncrementUnsArr(c);
|
2004-03-09 09:49:17 +00:00
|
|
|
|
}
|
2002-02-26 12:19:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|