xref: /PHP-7.3/ext/bcmath/libbcmath/src/recmul.c (revision 9d9defa2)
1 /* recmul.c: bcmath library file. */
2 /*
3     Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
4     Copyright (C) 2000 Philip A. Nelson
5 
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2 of the License, or (at your option) any later version.
10 
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Lesser General Public License for more details.  (COPYING.LIB)
15 
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to:
18 
19       The Free Software Foundation, Inc.
20       59 Temple Place, Suite 330
21       Boston, MA 02111-1307 USA.
22 
23     You may contact the author by:
24        e-mail:  philnelson@acm.org
25       us-mail:  Philip A. Nelson
26                 Computer Science Department, 9062
27                 Western Washington University
28                 Bellingham, WA 98226-9062
29 
30 *************************************************************************/
31 
32 #include <config.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #include <stdlib.h>
36 #include <ctype.h>
37 #include <stdarg.h>
38 #include "bcmath.h"
39 #include "private.h"
40 
41 /* Recursive vs non-recursive multiply crossover ranges. */
42 #if defined(MULDIGITS)
43 #include "muldigits.h"
44 #else
45 #define MUL_BASE_DIGITS 80
46 #endif
47 
48 int mul_base_digits = MUL_BASE_DIGITS;
49 #define MUL_SMALL_DIGITS mul_base_digits/4
50 
51 /* Multiply utility routines */
52 
53 static bc_num
new_sub_num(length,scale,value)54 new_sub_num (length, scale, value)
55      int length, scale;
56      char *value;
57 {
58   bc_num temp;
59 
60   temp = (bc_num) emalloc (sizeof(bc_struct));
61 
62   temp->n_sign = PLUS;
63   temp->n_len = length;
64   temp->n_scale = scale;
65   temp->n_refs = 1;
66   temp->n_ptr = NULL;
67   temp->n_value = value;
68   return temp;
69 }
70 
71 static void
_bc_simp_mul(bc_num n1,int n1len,bc_num n2,int n2len,bc_num * prod,int full_scale)72 _bc_simp_mul (bc_num n1, int n1len, bc_num n2, int n2len, bc_num *prod,
73 	      int full_scale)
74 {
75   char *n1ptr, *n2ptr, *pvptr;
76   char *n1end, *n2end;		/* To the end of n1 and n2. */
77   int indx, sum, prodlen;
78 
79   prodlen = n1len+n2len+1;
80 
81   *prod = bc_new_num (prodlen, 0);
82 
83   n1end = (char *) (n1->n_value + n1len - 1);
84   n2end = (char *) (n2->n_value + n2len - 1);
85   pvptr = (char *) ((*prod)->n_value + prodlen - 1);
86   sum = 0;
87 
88   /* Here is the loop... */
89   for (indx = 0; indx < prodlen-1; indx++)
90     {
91       n1ptr = (char *) (n1end - MAX(0, indx-n2len+1));
92       n2ptr = (char *) (n2end - MIN(indx, n2len-1));
93       while ((n1ptr >= n1->n_value) && (n2ptr <= n2end))
94 	sum += *n1ptr-- * *n2ptr++;
95       *pvptr-- = sum % BASE;
96       sum = sum / BASE;
97     }
98   *pvptr = sum;
99 }
100 
101 
102 /* A special adder/subtractor for the recursive divide and conquer
103    multiply algorithm.  Note: if sub is called, accum must
104    be larger that what is being subtracted.  Also, accum and val
105    must have n_scale = 0.  (e.g. they must look like integers. *) */
106 static void
_bc_shift_addsub(bc_num accum,bc_num val,int shift,int sub)107 _bc_shift_addsub (bc_num accum, bc_num val, int shift, int sub)
108 {
109   signed char *accp, *valp;
110   int  count, carry;
111 
112   count = val->n_len;
113   if (val->n_value[0] == 0)
114     count--;
115   assert (accum->n_len+accum->n_scale >= shift+count);
116 
117   /* Set up pointers and others */
118   accp = (signed char *)(accum->n_value +
119 			 accum->n_len + accum->n_scale - shift - 1);
120   valp = (signed char *)(val->n_value + val->n_len - 1);
121   carry = 0;
122 
123   if (sub) {
124     /* Subtraction, carry is really borrow. */
125     while (count--) {
126       *accp -= *valp-- + carry;
127       if (*accp < 0) {
128 	carry = 1;
129         *accp-- += BASE;
130       } else {
131 	carry = 0;
132 	accp--;
133       }
134     }
135     while (carry) {
136       *accp -= carry;
137       if (*accp < 0)
138 	*accp-- += BASE;
139       else
140 	carry = 0;
141     }
142   } else {
143     /* Addition */
144     while (count--) {
145       *accp += *valp-- + carry;
146       if (*accp > (BASE-1)) {
147 	carry = 1;
148         *accp-- -= BASE;
149       } else {
150 	carry = 0;
151 	accp--;
152       }
153     }
154     while (carry) {
155       *accp += carry;
156       if (*accp > (BASE-1))
157 	*accp-- -= BASE;
158       else
159 	carry = 0;
160     }
161   }
162 }
163 
164 /* Recursive divide and conquer multiply algorithm.
165    Based on
166    Let u = u0 + u1*(b^n)
167    Let v = v0 + v1*(b^n)
168    Then uv = (B^2n+B^n)*u1*v1 + B^n*(u1-u0)*(v0-v1) + (B^n+1)*u0*v0
169 
170    B is the base of storage, number of digits in u1,u0 close to equal.
171 */
172 static void
_bc_rec_mul(bc_num u,int ulen,bc_num v,int vlen,bc_num * prod,int full_scale)173 _bc_rec_mul (bc_num u, int ulen, bc_num v, int vlen, bc_num *prod,
174 	     int full_scale)
175 {
176   bc_num u0, u1, v0, v1;
177   bc_num m1, m2, m3, d1, d2;
178   int n, prodlen, m1zero;
179   int d1len, d2len;
180 
181   /* Base case? */
182   if ((ulen+vlen) < mul_base_digits
183       || ulen < MUL_SMALL_DIGITS
184       || vlen < MUL_SMALL_DIGITS ) {
185     _bc_simp_mul (u, ulen, v, vlen, prod, full_scale);
186     return;
187   }
188 
189   /* Calculate n -- the u and v split point in digits. */
190   n = (MAX(ulen, vlen)+1) / 2;
191 
192   /* Split u and v. */
193   if (ulen < n) {
194     u1 = bc_copy_num (BCG(_zero_));
195     u0 = new_sub_num (ulen,0, u->n_value);
196   } else {
197     u1 = new_sub_num (ulen-n, 0, u->n_value);
198     u0 = new_sub_num (n, 0, u->n_value+ulen-n);
199   }
200   if (vlen < n) {
201     v1 = bc_copy_num (BCG(_zero_));
202     v0 = new_sub_num (vlen,0, v->n_value);
203   } else {
204     v1 = new_sub_num (vlen-n, 0, v->n_value);
205     v0 = new_sub_num (n, 0, v->n_value+vlen-n);
206     }
207   _bc_rm_leading_zeros (u1);
208   _bc_rm_leading_zeros (u0);
209   _bc_rm_leading_zeros (v1);
210   _bc_rm_leading_zeros (v0);
211 
212   m1zero = bc_is_zero(u1) || bc_is_zero(v1);
213 
214   /* Calculate sub results ... */
215 
216   bc_init_num(&d1);
217   bc_init_num(&d2);
218   bc_sub (u1, u0, &d1, 0);
219   d1len = d1->n_len;
220   bc_sub (v0, v1, &d2, 0);
221   d2len = d2->n_len;
222 
223 
224   /* Do recursive multiplies and shifted adds. */
225   if (m1zero)
226     m1 = bc_copy_num (BCG(_zero_));
227   else
228     _bc_rec_mul (u1, u1->n_len, v1, v1->n_len, &m1, 0);
229 
230   if (bc_is_zero(d1) || bc_is_zero(d2))
231     m2 = bc_copy_num (BCG(_zero_));
232   else
233     _bc_rec_mul (d1, d1len, d2, d2len, &m2, 0);
234 
235   if (bc_is_zero(u0) || bc_is_zero(v0))
236     m3 = bc_copy_num (BCG(_zero_));
237   else
238     _bc_rec_mul (u0, u0->n_len, v0, v0->n_len, &m3, 0);
239 
240   /* Initialize product */
241   prodlen = ulen+vlen+1;
242   *prod = bc_new_num(prodlen, 0);
243 
244   if (!m1zero) {
245     _bc_shift_addsub (*prod, m1, 2*n, 0);
246     _bc_shift_addsub (*prod, m1, n, 0);
247   }
248   _bc_shift_addsub (*prod, m3, n, 0);
249   _bc_shift_addsub (*prod, m3, 0, 0);
250   _bc_shift_addsub (*prod, m2, n, d1->n_sign != d2->n_sign);
251 
252   /* Now clean up! */
253   bc_free_num (&u1);
254   bc_free_num (&u0);
255   bc_free_num (&v1);
256   bc_free_num (&m1);
257   bc_free_num (&v0);
258   bc_free_num (&m2);
259   bc_free_num (&m3);
260   bc_free_num (&d1);
261   bc_free_num (&d2);
262 }
263 
264 /* The multiply routine.  N2 times N1 is put int PROD with the scale of
265    the result being MIN(N2 scale+N1 scale, MAX (SCALE, N2 scale, N1 scale)).
266    */
267 
268 void
bc_multiply(bc_num n1,bc_num n2,bc_num * prod,int scale)269 bc_multiply (bc_num n1, bc_num n2, bc_num *prod, int scale)
270 {
271   bc_num pval;
272   int len1, len2;
273   int full_scale, prod_scale;
274 
275   /* Initialize things. */
276   len1 = n1->n_len + n1->n_scale;
277   len2 = n2->n_len + n2->n_scale;
278   full_scale = n1->n_scale + n2->n_scale;
279   prod_scale = MIN(full_scale,MAX(scale,MAX(n1->n_scale,n2->n_scale)));
280 
281   /* Do the multiply */
282   _bc_rec_mul (n1, len1, n2, len2, &pval, full_scale);
283 
284   /* Assign to prod and clean up the number. */
285   pval->n_sign = ( n1->n_sign == n2->n_sign ? PLUS : MINUS );
286   pval->n_value = pval->n_ptr;
287   pval->n_len = len2 + len1 + 1 - full_scale;
288   pval->n_scale = prod_scale;
289   _bc_rm_leading_zeros (pval);
290   if (bc_is_zero (pval))
291     pval->n_sign = PLUS;
292   bc_free_num (prod);
293   *prod = pval;
294 }
295