xref: /haiku/src/system/libroot/posix/glibc/arch/generic/mpa.c (revision 379f1467353183335fc90b76bd09141cef93d9e3)
1*379f1467SIngo Weinhold 
2*379f1467SIngo Weinhold /*
3*379f1467SIngo Weinhold  * IBM Accurate Mathematical Library
4*379f1467SIngo Weinhold  * written by International Business Machines Corp.
5*379f1467SIngo Weinhold  * Copyright (C) 2001 Free Software Foundation
6*379f1467SIngo Weinhold  *
7*379f1467SIngo Weinhold  * This program is free software; you can redistribute it and/or modify
8*379f1467SIngo Weinhold  * it under the terms of the GNU Lesser General Public License as published by
9*379f1467SIngo Weinhold  * the Free Software Foundation; either version 2.1 of the License, or
10*379f1467SIngo Weinhold  * (at your option) any later version.
11*379f1467SIngo Weinhold  *
12*379f1467SIngo Weinhold  * This program is distributed in the hope that it will be useful,
13*379f1467SIngo Weinhold  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14*379f1467SIngo Weinhold  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*379f1467SIngo Weinhold  * GNU Lesser General Public License for more details.
16*379f1467SIngo Weinhold  *
17*379f1467SIngo Weinhold  * You should have received a copy of the GNU Lesser General Public License
18*379f1467SIngo Weinhold  * along with this program; if not, write to the Free Software
19*379f1467SIngo Weinhold  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20*379f1467SIngo Weinhold  */
21*379f1467SIngo Weinhold /************************************************************************/
22*379f1467SIngo Weinhold /*  MODULE_NAME: mpa.c                                                  */
23*379f1467SIngo Weinhold /*                                                                      */
24*379f1467SIngo Weinhold /*  FUNCTIONS:                                                          */
25*379f1467SIngo Weinhold /*               mcr                                                    */
26*379f1467SIngo Weinhold /*               acr                                                    */
27*379f1467SIngo Weinhold /*               cr                                                     */
28*379f1467SIngo Weinhold /*               cpy                                                    */
29*379f1467SIngo Weinhold /*               cpymn                                                  */
30*379f1467SIngo Weinhold /*               norm                                                   */
31*379f1467SIngo Weinhold /*               denorm                                                 */
32*379f1467SIngo Weinhold /*               mp_dbl                                                 */
33*379f1467SIngo Weinhold /*               dbl_mp                                                 */
34*379f1467SIngo Weinhold /*               add_magnitudes                                         */
35*379f1467SIngo Weinhold /*               sub_magnitudes                                         */
36*379f1467SIngo Weinhold /*               add                                                    */
37*379f1467SIngo Weinhold /*               sub                                                    */
38*379f1467SIngo Weinhold /*               mul                                                    */
39*379f1467SIngo Weinhold /*               inv                                                    */
40*379f1467SIngo Weinhold /*               dvd                                                    */
41*379f1467SIngo Weinhold /*                                                                      */
42*379f1467SIngo Weinhold /* Arithmetic functions for multiple precision numbers.                 */
43*379f1467SIngo Weinhold /* Relative errors are bounded                                          */
44*379f1467SIngo Weinhold /************************************************************************/
45*379f1467SIngo Weinhold 
46*379f1467SIngo Weinhold 
47*379f1467SIngo Weinhold #include "endian.h"
48*379f1467SIngo Weinhold #include "mpa.h"
49*379f1467SIngo Weinhold #include "mpa2.h"
50*379f1467SIngo Weinhold /* mcr() compares the sizes of the mantissas of two multiple precision  */
51*379f1467SIngo Weinhold /* numbers. Mantissas are compared regardless of the signs of the       */
52*379f1467SIngo Weinhold /* numbers, even if x->d[0] or y->d[0] are zero. Exponents are also     */
53*379f1467SIngo Weinhold /* disregarded.                                                         */
mcr(const mp_no * x,const mp_no * y,int p)54*379f1467SIngo Weinhold static int mcr(const mp_no *x, const mp_no *y, int p) {
55*379f1467SIngo Weinhold   int i;
56*379f1467SIngo Weinhold   for (i=1; i<=p; i++) {
57*379f1467SIngo Weinhold     if      (X[i] == Y[i])  continue;
58*379f1467SIngo Weinhold     else if (X[i] >  Y[i])  return  1;
59*379f1467SIngo Weinhold     else                    return -1; }
60*379f1467SIngo Weinhold   return 0;
61*379f1467SIngo Weinhold }
62*379f1467SIngo Weinhold 
63*379f1467SIngo Weinhold 
64*379f1467SIngo Weinhold 
65*379f1467SIngo Weinhold /* acr() compares the absolute values of two multiple precision numbers */
__acr(const mp_no * x,const mp_no * y,int p)66*379f1467SIngo Weinhold int __acr(const mp_no *x, const mp_no *y, int p) {
67*379f1467SIngo Weinhold   int i;
68*379f1467SIngo Weinhold 
69*379f1467SIngo Weinhold   if      (X[0] == ZERO) {
70*379f1467SIngo Weinhold     if    (Y[0] == ZERO) i= 0;
71*379f1467SIngo Weinhold     else                 i=-1;
72*379f1467SIngo Weinhold   }
73*379f1467SIngo Weinhold   else if (Y[0] == ZERO) i= 1;
74*379f1467SIngo Weinhold   else {
75*379f1467SIngo Weinhold     if      (EX >  EY)   i= 1;
76*379f1467SIngo Weinhold     else if (EX <  EY)   i=-1;
77*379f1467SIngo Weinhold     else                 i= mcr(x,y,p);
78*379f1467SIngo Weinhold   }
79*379f1467SIngo Weinhold 
80*379f1467SIngo Weinhold   return i;
81*379f1467SIngo Weinhold }
82*379f1467SIngo Weinhold 
83*379f1467SIngo Weinhold 
84*379f1467SIngo Weinhold /* cr90 compares the values of two multiple precision numbers           */
__cr(const mp_no * x,const mp_no * y,int p)85*379f1467SIngo Weinhold int  __cr(const mp_no *x, const mp_no *y, int p) {
86*379f1467SIngo Weinhold   int i;
87*379f1467SIngo Weinhold 
88*379f1467SIngo Weinhold   if      (X[0] > Y[0])  i= 1;
89*379f1467SIngo Weinhold   else if (X[0] < Y[0])  i=-1;
90*379f1467SIngo Weinhold   else if (X[0] < ZERO ) i= __acr(y,x,p);
91*379f1467SIngo Weinhold   else                   i= __acr(x,y,p);
92*379f1467SIngo Weinhold 
93*379f1467SIngo Weinhold   return i;
94*379f1467SIngo Weinhold }
95*379f1467SIngo Weinhold 
96*379f1467SIngo Weinhold 
97*379f1467SIngo Weinhold /* Copy a multiple precision number. Set *y=*x. x=y is permissible.      */
__cpy(const mp_no * x,mp_no * y,int p)98*379f1467SIngo Weinhold void __cpy(const mp_no *x, mp_no *y, int p) {
99*379f1467SIngo Weinhold   int i;
100*379f1467SIngo Weinhold 
101*379f1467SIngo Weinhold   EY = EX;
102*379f1467SIngo Weinhold   for (i=0; i <= p; i++)    Y[i] = X[i];
103*379f1467SIngo Weinhold 
104*379f1467SIngo Weinhold   return;
105*379f1467SIngo Weinhold }
106*379f1467SIngo Weinhold 
107*379f1467SIngo Weinhold 
108*379f1467SIngo Weinhold /* Copy a multiple precision number x of precision m into a */
109*379f1467SIngo Weinhold /* multiple precision number y of precision n. In case n>m, */
110*379f1467SIngo Weinhold /* the digits of y beyond the m'th are set to zero. In case */
111*379f1467SIngo Weinhold /* n<m, the digits of x beyond the n'th are ignored.        */
112*379f1467SIngo Weinhold /* x=y is permissible.                                      */
113*379f1467SIngo Weinhold 
__cpymn(const mp_no * x,int m,mp_no * y,int n)114*379f1467SIngo Weinhold void __cpymn(const mp_no *x, int m, mp_no *y, int n) {
115*379f1467SIngo Weinhold 
116*379f1467SIngo Weinhold   int i,k;
117*379f1467SIngo Weinhold 
118*379f1467SIngo Weinhold   EY = EX;     k=MIN(m,n);
119*379f1467SIngo Weinhold   for (i=0; i <= k; i++)    Y[i] = X[i];
120*379f1467SIngo Weinhold   for (   ; i <= n; i++)    Y[i] = ZERO;
121*379f1467SIngo Weinhold 
122*379f1467SIngo Weinhold   return;
123*379f1467SIngo Weinhold }
124*379f1467SIngo Weinhold 
125*379f1467SIngo Weinhold /* Convert a multiple precision number *x into a double precision */
126*379f1467SIngo Weinhold /* number *y, normalized case  (|x| >= 2**(-1022))) */
norm(const mp_no * x,double * y,int p)127*379f1467SIngo Weinhold static void norm(const mp_no *x, double *y, int p)
128*379f1467SIngo Weinhold {
129*379f1467SIngo Weinhold   #define R  radixi.d
130*379f1467SIngo Weinhold   int i;
131*379f1467SIngo Weinhold #if 0
132*379f1467SIngo Weinhold   int k;
133*379f1467SIngo Weinhold #endif
134*379f1467SIngo Weinhold   double a,c,u,v,z[5];
135*379f1467SIngo Weinhold   if (p<5) {
136*379f1467SIngo Weinhold     if      (p==1) c = X[1];
137*379f1467SIngo Weinhold     else if (p==2) c = X[1] + R* X[2];
138*379f1467SIngo Weinhold     else if (p==3) c = X[1] + R*(X[2]  +   R* X[3]);
139*379f1467SIngo Weinhold     else if (p==4) c =(X[1] + R* X[2]) + R*R*(X[3] + R*X[4]);
140*379f1467SIngo Weinhold   }
141*379f1467SIngo Weinhold   else {
142*379f1467SIngo Weinhold     for (a=ONE, z[1]=X[1]; z[1] < TWO23; )
143*379f1467SIngo Weinhold         {a *= TWO;   z[1] *= TWO; }
144*379f1467SIngo Weinhold 
145*379f1467SIngo Weinhold     for (i=2; i<5; i++) {
146*379f1467SIngo Weinhold       z[i] = X[i]*a;
147*379f1467SIngo Weinhold       u = (z[i] + CUTTER)-CUTTER;
148*379f1467SIngo Weinhold       if  (u > z[i])  u -= RADIX;
149*379f1467SIngo Weinhold       z[i] -= u;
150*379f1467SIngo Weinhold       z[i-1] += u*RADIXI;
151*379f1467SIngo Weinhold     }
152*379f1467SIngo Weinhold 
153*379f1467SIngo Weinhold     u = (z[3] + TWO71) - TWO71;
154*379f1467SIngo Weinhold     if (u > z[3])   u -= TWO19;
155*379f1467SIngo Weinhold     v = z[3]-u;
156*379f1467SIngo Weinhold 
157*379f1467SIngo Weinhold     if (v == TWO18) {
158*379f1467SIngo Weinhold       if (z[4] == ZERO) {
159*379f1467SIngo Weinhold         for (i=5; i <= p; i++) {
160*379f1467SIngo Weinhold           if (X[i] == ZERO)   continue;
161*379f1467SIngo Weinhold           else                {z[3] += ONE;   break; }
162*379f1467SIngo Weinhold         }
163*379f1467SIngo Weinhold       }
164*379f1467SIngo Weinhold       else              z[3] += ONE;
165*379f1467SIngo Weinhold     }
166*379f1467SIngo Weinhold 
167*379f1467SIngo Weinhold     c = (z[1] + R *(z[2] + R * z[3]))/a;
168*379f1467SIngo Weinhold   }
169*379f1467SIngo Weinhold 
170*379f1467SIngo Weinhold   c *= X[0];
171*379f1467SIngo Weinhold 
172*379f1467SIngo Weinhold   for (i=1; i<EX; i++)   c *= RADIX;
173*379f1467SIngo Weinhold   for (i=1; i>EX; i--)   c *= RADIXI;
174*379f1467SIngo Weinhold 
175*379f1467SIngo Weinhold   *y = c;
176*379f1467SIngo Weinhold   return;
177*379f1467SIngo Weinhold #undef R
178*379f1467SIngo Weinhold }
179*379f1467SIngo Weinhold 
180*379f1467SIngo Weinhold /* Convert a multiple precision number *x into a double precision */
181*379f1467SIngo Weinhold /* number *y, denormalized case  (|x| < 2**(-1022))) */
denorm(const mp_no * x,double * y,int p)182*379f1467SIngo Weinhold static void denorm(const mp_no *x, double *y, int p)
183*379f1467SIngo Weinhold {
184*379f1467SIngo Weinhold   int i,k;
185*379f1467SIngo Weinhold   double c,u,z[5];
186*379f1467SIngo Weinhold #if 0
187*379f1467SIngo Weinhold   double a,v;
188*379f1467SIngo Weinhold #endif
189*379f1467SIngo Weinhold 
190*379f1467SIngo Weinhold #define R  radixi.d
191*379f1467SIngo Weinhold   if (EX<-44 || (EX==-44 && X[1]<TWO5))
192*379f1467SIngo Weinhold      { *y=ZERO; return; }
193*379f1467SIngo Weinhold 
194*379f1467SIngo Weinhold   if      (p==1) {
195*379f1467SIngo Weinhold     if      (EX==-42) {z[1]=X[1]+TWO10;  z[2]=ZERO;  z[3]=ZERO;  k=3;}
196*379f1467SIngo Weinhold     else if (EX==-43) {z[1]=     TWO10;  z[2]=X[1];  z[3]=ZERO;  k=2;}
197*379f1467SIngo Weinhold     else              {z[1]=     TWO10;  z[2]=ZERO;  z[3]=X[1];  k=1;}
198*379f1467SIngo Weinhold   }
199*379f1467SIngo Weinhold   else if (p==2) {
200*379f1467SIngo Weinhold     if      (EX==-42) {z[1]=X[1]+TWO10;  z[2]=X[2];  z[3]=ZERO;  k=3;}
201*379f1467SIngo Weinhold     else if (EX==-43) {z[1]=     TWO10;  z[2]=X[1];  z[3]=X[2];  k=2;}
202*379f1467SIngo Weinhold     else              {z[1]=     TWO10;  z[2]=ZERO;  z[3]=X[1];  k=1;}
203*379f1467SIngo Weinhold   }
204*379f1467SIngo Weinhold   else {
205*379f1467SIngo Weinhold     if      (EX==-42) {z[1]=X[1]+TWO10;  z[2]=X[2];  k=3;}
206*379f1467SIngo Weinhold     else if (EX==-43) {z[1]=     TWO10;  z[2]=X[1];  k=2;}
207*379f1467SIngo Weinhold     else              {z[1]=     TWO10;  z[2]=ZERO;  k=1;}
208*379f1467SIngo Weinhold     z[3] = X[k];
209*379f1467SIngo Weinhold   }
210*379f1467SIngo Weinhold 
211*379f1467SIngo Weinhold   u = (z[3] + TWO57) - TWO57;
212*379f1467SIngo Weinhold   if  (u > z[3])   u -= TWO5;
213*379f1467SIngo Weinhold 
214*379f1467SIngo Weinhold   if (u==z[3]) {
215*379f1467SIngo Weinhold     for (i=k+1; i <= p; i++) {
216*379f1467SIngo Weinhold       if (X[i] == ZERO)   continue;
217*379f1467SIngo Weinhold       else {z[3] += ONE;   break; }
218*379f1467SIngo Weinhold     }
219*379f1467SIngo Weinhold   }
220*379f1467SIngo Weinhold 
221*379f1467SIngo Weinhold   c = X[0]*((z[1] + R*(z[2] + R*z[3])) - TWO10);
222*379f1467SIngo Weinhold 
223*379f1467SIngo Weinhold   *y = c*TWOM1032;
224*379f1467SIngo Weinhold   return;
225*379f1467SIngo Weinhold 
226*379f1467SIngo Weinhold #undef R
227*379f1467SIngo Weinhold }
228*379f1467SIngo Weinhold 
229*379f1467SIngo Weinhold /* Convert a multiple precision number *x into a double precision number *y. */
230*379f1467SIngo Weinhold /* The result is correctly rounded to the nearest/even. *x is left unchanged */
231*379f1467SIngo Weinhold 
__mp_dbl(const mp_no * x,double * y,int p)232*379f1467SIngo Weinhold void __mp_dbl(const mp_no *x, double *y, int p) {
233*379f1467SIngo Weinhold #if 0
234*379f1467SIngo Weinhold   int i,k;
235*379f1467SIngo Weinhold   double a,c,u,v,z[5];
236*379f1467SIngo Weinhold #endif
237*379f1467SIngo Weinhold 
238*379f1467SIngo Weinhold   if (X[0] == ZERO)  {*y = ZERO;  return; }
239*379f1467SIngo Weinhold 
240*379f1467SIngo Weinhold   if      (EX> -42)                 norm(x,y,p);
241*379f1467SIngo Weinhold   else if (EX==-42 && X[1]>=TWO10)  norm(x,y,p);
242*379f1467SIngo Weinhold   else                              denorm(x,y,p);
243*379f1467SIngo Weinhold }
244*379f1467SIngo Weinhold 
245*379f1467SIngo Weinhold 
246*379f1467SIngo Weinhold /* dbl_mp() converts a double precision number x into a multiple precision  */
247*379f1467SIngo Weinhold /* number *y. If the precision p is too small the result is truncated. x is */
248*379f1467SIngo Weinhold /* left unchanged.                                                          */
249*379f1467SIngo Weinhold 
__dbl_mp(double x,mp_no * y,int p)250*379f1467SIngo Weinhold void __dbl_mp(double x, mp_no *y, int p) {
251*379f1467SIngo Weinhold 
252*379f1467SIngo Weinhold   int i,n;
253*379f1467SIngo Weinhold   double u;
254*379f1467SIngo Weinhold 
255*379f1467SIngo Weinhold   /* Sign */
256*379f1467SIngo Weinhold   if      (x == ZERO)  {Y[0] = ZERO;  return; }
257*379f1467SIngo Weinhold   else if (x >  ZERO)   Y[0] = ONE;
258*379f1467SIngo Weinhold   else                 {Y[0] = MONE;  x=-x;   }
259*379f1467SIngo Weinhold 
260*379f1467SIngo Weinhold   /* Exponent */
261*379f1467SIngo Weinhold   for (EY=ONE; x >= RADIX; EY += ONE)   x *= RADIXI;
262*379f1467SIngo Weinhold   for (      ; x <  ONE;   EY -= ONE)   x *= RADIX;
263*379f1467SIngo Weinhold 
264*379f1467SIngo Weinhold   /* Digits */
265*379f1467SIngo Weinhold   n=MIN(p,4);
266*379f1467SIngo Weinhold   for (i=1; i<=n; i++) {
267*379f1467SIngo Weinhold     u = (x + TWO52) - TWO52;
268*379f1467SIngo Weinhold     if (u>x)   u -= ONE;
269*379f1467SIngo Weinhold     Y[i] = u;     x -= u;    x *= RADIX; }
270*379f1467SIngo Weinhold   for (   ; i<=p; i++)     Y[i] = ZERO;
271*379f1467SIngo Weinhold   return;
272*379f1467SIngo Weinhold }
273*379f1467SIngo Weinhold 
274*379f1467SIngo Weinhold 
275*379f1467SIngo Weinhold /*  add_magnitudes() adds the magnitudes of *x & *y assuming that           */
276*379f1467SIngo Weinhold /*  abs(*x) >= abs(*y) > 0.                                                 */
277*379f1467SIngo Weinhold /* The sign of the sum *z is undefined. x&y may overlap but not x&z or y&z. */
278*379f1467SIngo Weinhold /* No guard digit is used. The result equals the exact sum, truncated.      */
279*379f1467SIngo Weinhold /* *x & *y are left unchanged.                                              */
280*379f1467SIngo Weinhold 
add_magnitudes(const mp_no * x,const mp_no * y,mp_no * z,int p)281*379f1467SIngo Weinhold static void add_magnitudes(const mp_no *x, const mp_no *y, mp_no *z, int p) {
282*379f1467SIngo Weinhold 
283*379f1467SIngo Weinhold   int i,j,k;
284*379f1467SIngo Weinhold 
285*379f1467SIngo Weinhold   EZ = EX;
286*379f1467SIngo Weinhold 
287*379f1467SIngo Weinhold   i=p;    j=p+ EY - EX;    k=p+1;
288*379f1467SIngo Weinhold 
289*379f1467SIngo Weinhold   if (j<1)
290*379f1467SIngo Weinhold      {__cpy(x,z,p);  return; }
291*379f1467SIngo Weinhold   else   Z[k] = ZERO;
292*379f1467SIngo Weinhold 
293*379f1467SIngo Weinhold   for (; j>0; i--,j--) {
294*379f1467SIngo Weinhold     Z[k] += X[i] + Y[j];
295*379f1467SIngo Weinhold     if (Z[k] >= RADIX) {
296*379f1467SIngo Weinhold       Z[k]  -= RADIX;
297*379f1467SIngo Weinhold       Z[--k] = ONE; }
298*379f1467SIngo Weinhold     else
299*379f1467SIngo Weinhold       Z[--k] = ZERO;
300*379f1467SIngo Weinhold   }
301*379f1467SIngo Weinhold 
302*379f1467SIngo Weinhold   for (; i>0; i--) {
303*379f1467SIngo Weinhold     Z[k] += X[i];
304*379f1467SIngo Weinhold     if (Z[k] >= RADIX) {
305*379f1467SIngo Weinhold       Z[k]  -= RADIX;
306*379f1467SIngo Weinhold       Z[--k] = ONE; }
307*379f1467SIngo Weinhold     else
308*379f1467SIngo Weinhold       Z[--k] = ZERO;
309*379f1467SIngo Weinhold   }
310*379f1467SIngo Weinhold 
311*379f1467SIngo Weinhold   if (Z[1] == ZERO) {
312*379f1467SIngo Weinhold     for (i=1; i<=p; i++)    Z[i] = Z[i+1]; }
313*379f1467SIngo Weinhold   else   EZ += ONE;
314*379f1467SIngo Weinhold }
315*379f1467SIngo Weinhold 
316*379f1467SIngo Weinhold 
317*379f1467SIngo Weinhold /*  sub_magnitudes() subtracts the magnitudes of *x & *y assuming that      */
318*379f1467SIngo Weinhold /*  abs(*x) > abs(*y) > 0.                                                  */
319*379f1467SIngo Weinhold /* The sign of the difference *z is undefined. x&y may overlap but not x&z  */
320*379f1467SIngo Weinhold /* or y&z. One guard digit is used. The error is less than one ulp.         */
321*379f1467SIngo Weinhold /* *x & *y are left unchanged.                                              */
322*379f1467SIngo Weinhold 
sub_magnitudes(const mp_no * x,const mp_no * y,mp_no * z,int p)323*379f1467SIngo Weinhold static void sub_magnitudes(const mp_no *x, const mp_no *y, mp_no *z, int p) {
324*379f1467SIngo Weinhold 
325*379f1467SIngo Weinhold   int i,j,k;
326*379f1467SIngo Weinhold 
327*379f1467SIngo Weinhold   EZ = EX;
328*379f1467SIngo Weinhold 
329*379f1467SIngo Weinhold   if (EX == EY) {
330*379f1467SIngo Weinhold     i=j=k=p;
331*379f1467SIngo Weinhold     Z[k] = Z[k+1] = ZERO; }
332*379f1467SIngo Weinhold   else {
333*379f1467SIngo Weinhold     j= EX - EY;
334*379f1467SIngo Weinhold     if (j > p)  {__cpy(x,z,p);  return; }
335*379f1467SIngo Weinhold     else {
336*379f1467SIngo Weinhold       i=p;   j=p+1-j;   k=p;
337*379f1467SIngo Weinhold       if (Y[j] > ZERO) {
338*379f1467SIngo Weinhold         Z[k+1] = RADIX - Y[j--];
339*379f1467SIngo Weinhold         Z[k]   = MONE; }
340*379f1467SIngo Weinhold       else {
341*379f1467SIngo Weinhold         Z[k+1] = ZERO;
342*379f1467SIngo Weinhold         Z[k]   = ZERO;   j--;}
343*379f1467SIngo Weinhold     }
344*379f1467SIngo Weinhold   }
345*379f1467SIngo Weinhold 
346*379f1467SIngo Weinhold   for (; j>0; i--,j--) {
347*379f1467SIngo Weinhold     Z[k] += (X[i] - Y[j]);
348*379f1467SIngo Weinhold     if (Z[k] < ZERO) {
349*379f1467SIngo Weinhold       Z[k]  += RADIX;
350*379f1467SIngo Weinhold       Z[--k] = MONE; }
351*379f1467SIngo Weinhold     else
352*379f1467SIngo Weinhold       Z[--k] = ZERO;
353*379f1467SIngo Weinhold   }
354*379f1467SIngo Weinhold 
355*379f1467SIngo Weinhold   for (; i>0; i--) {
356*379f1467SIngo Weinhold     Z[k] += X[i];
357*379f1467SIngo Weinhold     if (Z[k] < ZERO) {
358*379f1467SIngo Weinhold       Z[k]  += RADIX;
359*379f1467SIngo Weinhold       Z[--k] = MONE; }
360*379f1467SIngo Weinhold     else
361*379f1467SIngo Weinhold       Z[--k] = ZERO;
362*379f1467SIngo Weinhold   }
363*379f1467SIngo Weinhold 
364*379f1467SIngo Weinhold   for (i=1; Z[i] == ZERO; i++) ;
365*379f1467SIngo Weinhold   EZ = EZ - i + 1;
366*379f1467SIngo Weinhold   for (k=1; i <= p+1; )
367*379f1467SIngo Weinhold     Z[k++] = Z[i++];
368*379f1467SIngo Weinhold   for (; k <= p; )
369*379f1467SIngo Weinhold     Z[k++] = ZERO;
370*379f1467SIngo Weinhold 
371*379f1467SIngo Weinhold   return;
372*379f1467SIngo Weinhold }
373*379f1467SIngo Weinhold 
374*379f1467SIngo Weinhold 
375*379f1467SIngo Weinhold /* Add two multiple precision numbers. Set *z = *x + *y. x&y may overlap  */
376*379f1467SIngo Weinhold /* but not x&z or y&z. One guard digit is used. The error is less than    */
377*379f1467SIngo Weinhold /* one ulp. *x & *y are left unchanged.                                   */
378*379f1467SIngo Weinhold 
__add(const mp_no * x,const mp_no * y,mp_no * z,int p)379*379f1467SIngo Weinhold void __add(const mp_no *x, const mp_no *y, mp_no *z, int p) {
380*379f1467SIngo Weinhold 
381*379f1467SIngo Weinhold   int n;
382*379f1467SIngo Weinhold 
383*379f1467SIngo Weinhold   if      (X[0] == ZERO)     {__cpy(y,z,p);  return; }
384*379f1467SIngo Weinhold   else if (Y[0] == ZERO)     {__cpy(x,z,p);  return; }
385*379f1467SIngo Weinhold 
386*379f1467SIngo Weinhold   if (X[0] == Y[0])   {
387*379f1467SIngo Weinhold     if (__acr(x,y,p) > 0)      {add_magnitudes(x,y,z,p);  Z[0] = X[0]; }
388*379f1467SIngo Weinhold     else                     {add_magnitudes(y,x,z,p);  Z[0] = Y[0]; }
389*379f1467SIngo Weinhold   }
390*379f1467SIngo Weinhold   else                       {
391*379f1467SIngo Weinhold     if ((n=__acr(x,y,p)) == 1) {sub_magnitudes(x,y,z,p);  Z[0] = X[0]; }
392*379f1467SIngo Weinhold     else if (n == -1)        {sub_magnitudes(y,x,z,p);  Z[0] = Y[0]; }
393*379f1467SIngo Weinhold     else                      Z[0] = ZERO;
394*379f1467SIngo Weinhold   }
395*379f1467SIngo Weinhold   return;
396*379f1467SIngo Weinhold }
397*379f1467SIngo Weinhold 
398*379f1467SIngo Weinhold 
399*379f1467SIngo Weinhold /* Subtract two multiple precision numbers. *z is set to *x - *y. x&y may */
400*379f1467SIngo Weinhold /* overlap but not x&z or y&z. One guard digit is used. The error is      */
401*379f1467SIngo Weinhold /* less than one ulp. *x & *y are left unchanged.                         */
402*379f1467SIngo Weinhold 
__sub(const mp_no * x,const mp_no * y,mp_no * z,int p)403*379f1467SIngo Weinhold void __sub(const mp_no *x, const mp_no *y, mp_no *z, int p) {
404*379f1467SIngo Weinhold 
405*379f1467SIngo Weinhold   int n;
406*379f1467SIngo Weinhold 
407*379f1467SIngo Weinhold   if      (X[0] == ZERO)     {__cpy(y,z,p);  Z[0] = -Z[0];  return; }
408*379f1467SIngo Weinhold   else if (Y[0] == ZERO)     {__cpy(x,z,p);                 return; }
409*379f1467SIngo Weinhold 
410*379f1467SIngo Weinhold   if (X[0] != Y[0])    {
411*379f1467SIngo Weinhold     if (__acr(x,y,p) > 0)      {add_magnitudes(x,y,z,p);  Z[0] =  X[0]; }
412*379f1467SIngo Weinhold     else                     {add_magnitudes(y,x,z,p);  Z[0] = -Y[0]; }
413*379f1467SIngo Weinhold   }
414*379f1467SIngo Weinhold   else                       {
415*379f1467SIngo Weinhold     if ((n=__acr(x,y,p)) == 1) {sub_magnitudes(x,y,z,p);  Z[0] =  X[0]; }
416*379f1467SIngo Weinhold     else if (n == -1)        {sub_magnitudes(y,x,z,p);  Z[0] = -Y[0]; }
417*379f1467SIngo Weinhold     else                      Z[0] = ZERO;
418*379f1467SIngo Weinhold   }
419*379f1467SIngo Weinhold   return;
420*379f1467SIngo Weinhold }
421*379f1467SIngo Weinhold 
422*379f1467SIngo Weinhold 
423*379f1467SIngo Weinhold /* Multiply two multiple precision numbers. *z is set to *x * *y. x&y      */
424*379f1467SIngo Weinhold /* may overlap but not x&z or y&z. In case p=1,2,3 the exact result is     */
425*379f1467SIngo Weinhold /* truncated to p digits. In case p>3 the error is bounded by 1.001 ulp.   */
426*379f1467SIngo Weinhold /* *x & *y are left unchanged.                                             */
427*379f1467SIngo Weinhold 
__mul(const mp_no * x,const mp_no * y,mp_no * z,int p)428*379f1467SIngo Weinhold void __mul(const mp_no *x, const mp_no *y, mp_no *z, int p) {
429*379f1467SIngo Weinhold 
430*379f1467SIngo Weinhold   int i, i1, i2, j, k, k2;
431*379f1467SIngo Weinhold   double u;
432*379f1467SIngo Weinhold 
433*379f1467SIngo Weinhold                       /* Is z=0? */
434*379f1467SIngo Weinhold   if (X[0]*Y[0]==ZERO)
435*379f1467SIngo Weinhold      { Z[0]=ZERO;  return; }
436*379f1467SIngo Weinhold 
437*379f1467SIngo Weinhold                        /* Multiply, add and carry */
438*379f1467SIngo Weinhold   k2 = (p<3) ? p+p : p+3;
439*379f1467SIngo Weinhold   Z[k2]=ZERO;
440*379f1467SIngo Weinhold   for (k=k2; k>1; ) {
441*379f1467SIngo Weinhold     if (k > p)  {i1=k-p; i2=p+1; }
442*379f1467SIngo Weinhold     else        {i1=1;   i2=k;   }
443*379f1467SIngo Weinhold     for (i=i1,j=i2-1; i<i2; i++,j--)  Z[k] += X[i]*Y[j];
444*379f1467SIngo Weinhold 
445*379f1467SIngo Weinhold     u = (Z[k] + CUTTER)-CUTTER;
446*379f1467SIngo Weinhold     if  (u > Z[k])  u -= RADIX;
447*379f1467SIngo Weinhold     Z[k]  -= u;
448*379f1467SIngo Weinhold     Z[--k] = u*RADIXI;
449*379f1467SIngo Weinhold   }
450*379f1467SIngo Weinhold 
451*379f1467SIngo Weinhold                  /* Is there a carry beyond the most significant digit? */
452*379f1467SIngo Weinhold   if (Z[1] == ZERO) {
453*379f1467SIngo Weinhold     for (i=1; i<=p; i++)  Z[i]=Z[i+1];
454*379f1467SIngo Weinhold     EZ = EX + EY - 1; }
455*379f1467SIngo Weinhold   else
456*379f1467SIngo Weinhold     EZ = EX + EY;
457*379f1467SIngo Weinhold 
458*379f1467SIngo Weinhold   Z[0] = X[0] * Y[0];
459*379f1467SIngo Weinhold   return;
460*379f1467SIngo Weinhold }
461*379f1467SIngo Weinhold 
462*379f1467SIngo Weinhold 
463*379f1467SIngo Weinhold /* Invert a multiple precision number. Set *y = 1 / *x.                     */
464*379f1467SIngo Weinhold /* Relative error bound = 1.001*r**(1-p) for p=2, 1.063*r**(1-p) for p=3,   */
465*379f1467SIngo Weinhold /* 2.001*r**(1-p) for p>3.                                                  */
466*379f1467SIngo Weinhold /* *x=0 is not permissible. *x is left unchanged.                           */
467*379f1467SIngo Weinhold 
__inv(const mp_no * x,mp_no * y,int p)468*379f1467SIngo Weinhold void __inv(const mp_no *x, mp_no *y, int p) {
469*379f1467SIngo Weinhold   int i;
470*379f1467SIngo Weinhold #if 0
471*379f1467SIngo Weinhold   int l;
472*379f1467SIngo Weinhold #endif
473*379f1467SIngo Weinhold   double t;
474*379f1467SIngo Weinhold   mp_no z,w;
475*379f1467SIngo Weinhold   static const int np1[] = {0,0,0,0,1,2,2,2,2,3,3,3,3,3,3,3,3,3,
476*379f1467SIngo Weinhold                             4,4,4,4,4,4,4,4,4,4,4,4,4,4,4};
477*379f1467SIngo Weinhold   const mp_no mptwo = {1,{1.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,
478*379f1467SIngo Weinhold                          0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,
479*379f1467SIngo Weinhold                          0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,
480*379f1467SIngo Weinhold                          0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}};
481*379f1467SIngo Weinhold 
482*379f1467SIngo Weinhold   __cpy(x,&z,p);  z.e=0;  __mp_dbl(&z,&t,p);
483*379f1467SIngo Weinhold   t=ONE/t;   __dbl_mp(t,y,p);    EY -= EX;
484*379f1467SIngo Weinhold 
485*379f1467SIngo Weinhold   for (i=0; i<np1[p]; i++) {
486*379f1467SIngo Weinhold     __cpy(y,&w,p);
487*379f1467SIngo Weinhold     __mul(x,&w,y,p);
488*379f1467SIngo Weinhold     __sub(&mptwo,y,&z,p);
489*379f1467SIngo Weinhold     __mul(&w,&z,y,p);
490*379f1467SIngo Weinhold   }
491*379f1467SIngo Weinhold   return;
492*379f1467SIngo Weinhold }
493*379f1467SIngo Weinhold 
494*379f1467SIngo Weinhold 
495*379f1467SIngo Weinhold /* Divide one multiple precision number by another.Set *z = *x / *y. *x & *y */
496*379f1467SIngo Weinhold /* are left unchanged. x&y may overlap but not x&z or y&z.                   */
497*379f1467SIngo Weinhold /* Relative error bound = 2.001*r**(1-p) for p=2, 2.063*r**(1-p) for p=3     */
498*379f1467SIngo Weinhold /* and 3.001*r**(1-p) for p>3. *y=0 is not permissible.                      */
499*379f1467SIngo Weinhold 
__dvd(const mp_no * x,const mp_no * y,mp_no * z,int p)500*379f1467SIngo Weinhold void __dvd(const mp_no *x, const mp_no *y, mp_no *z, int p) {
501*379f1467SIngo Weinhold 
502*379f1467SIngo Weinhold   mp_no w;
503*379f1467SIngo Weinhold 
504*379f1467SIngo Weinhold   if (X[0] == ZERO)    Z[0] = ZERO;
505*379f1467SIngo Weinhold   else                {__inv(y,&w,p);   __mul(x,&w,z,p);}
506*379f1467SIngo Weinhold   return;
507*379f1467SIngo Weinhold }
508