summaryrefslogtreecommitdiffstats
path: root/gnu/lib/libgmp/mpn_mul.c
blob: 8455f1dbe476834b6b92707ff23c6568ba0517ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* mpn_mul -- Multiply two natural numbers.

Copyright (C) 1991, 1992 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

The GNU MP Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with the GNU MP Library; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "gmp.h"
#include "gmp-impl.h"
#include "longlong.h"

#ifdef DEBUG
#define MPN_MUL_VERIFY(res_ptr,res_size,op1_ptr,op1_size,op2_ptr,op2_size) \
  mpn_mul_verify (res_ptr, res_size, op1_ptr, op1_size, op2_ptr, op2_size)

#include <stdio.h>
static void
mpn_mul_verify (res_ptr, res_size, op1_ptr, op1_size, op2_ptr, op2_size)
     mp_ptr res_ptr, op1_ptr, op2_ptr;
     mp_size res_size, op1_size, op2_size;
{
  mp_ptr tmp_ptr;
  mp_size tmp_size;
  tmp_ptr = alloca ((op1_size + op2_size) * BYTES_PER_MP_LIMB);
  if (op1_size >= op2_size)
    tmp_size = mpn_mul_classic (tmp_ptr,
				 op1_ptr, op1_size, op2_ptr, op2_size);
  else
    tmp_size = mpn_mul_classic (tmp_ptr,
				 op2_ptr, op2_size, op1_ptr, op1_size);
  if (tmp_size != res_size
      || mpn_cmp (tmp_ptr, res_ptr, tmp_size) != 0)
    {
      fprintf (stderr, "GNU MP internal error: Wrong result in mpn_mul.\n");
      fprintf (stderr, "op1{%d} = ", op1_size); mpn_dump (op1_ptr, op1_size);
      fprintf (stderr, "op2{%d} = ", op2_size); mpn_dump (op2_ptr, op2_size);
      abort ();
    }
}
#else
#define MPN_MUL_VERIFY(a,b,c,d,e,f)
#endif

/* Multiply the natural numbers u (pointed to by UP, with USIZE limbs)
   and v (pointed to by VP, with VSIZE limbs), and store the result at
   PRODP.  USIZE + VSIZE limbs are always stored, but if the input
   operands are normalized, the return value will reflect the true
   result size (which is either USIZE + VSIZE, or USIZE + VSIZE -1).

   NOTE: The space pointed to by PRODP is overwritten before finished
   with U and V, so overlap is an error.

   Argument constraints:
   1. USIZE >= VSIZE.
   2. PRODP != UP and PRODP != VP, i.e. the destination
      must be distinct from the multiplier and the multiplicand.  */

/* If KARATSUBA_THRESHOLD is not already defined, define it to a
   value which is good on most machines.  */
#ifndef KARATSUBA_THRESHOLD
#define KARATSUBA_THRESHOLD 8
#endif

/* The code can't handle KARATSUBA_THRESHOLD smaller than 4.  */
#if KARATSUBA_THRESHOLD < 4
#undef KARATSUBA_THRESHOLD
#define KARATSUBA_THRESHOLD 4
#endif

mp_size
#ifdef __STDC__
mpn_mul (mp_ptr prodp,
	  mp_srcptr up, mp_size usize,
	  mp_srcptr vp, mp_size vsize)
#else
mpn_mul (prodp, up, usize, vp, vsize)
     mp_ptr prodp;
     mp_srcptr up;
     mp_size usize;
     mp_srcptr vp;
     mp_size vsize;
#endif
{
  mp_size n;
  mp_size prod_size;
  mp_limb cy;

  if (vsize < KARATSUBA_THRESHOLD)
    {
      /* Handle simple cases with traditional multiplication.

	 This is the most critical code of the entire function.  All
	 multiplies rely on this, both small and huge.  Small ones arrive
	 here immediately.  Huge ones arrive here as this is the base case
	 for the recursive algorithm below.  */
      mp_size i, j;
      mp_limb prod_low, prod_high;
      mp_limb cy_limb;
      mp_limb v_limb;

      if (vsize == 0)
	return 0;

      /* Offset UP and PRODP so that the inner loop can be faster.  */
      up += usize;
      prodp += usize;

      /* Multiply by the first limb in V separately, as the result can
	 be stored (not added) to PROD.  We also avoid a loop for zeroing.  */
      v_limb = vp[0];
      if (v_limb <= 1)
	{
	  if (v_limb == 1)
	    MPN_COPY (prodp - usize, up - usize, usize);
	  else
	    MPN_ZERO (prodp - usize, usize);
	  cy_limb = 0;
	}
      else
	{
	  cy_limb = 0;
	  j = -usize;
	  do
	    {
	      umul_ppmm (prod_high, prod_low, up[j], v_limb);
	      add_ssaaaa (cy_limb, prodp[j], prod_high, prod_low, 0, cy_limb);
	      j++;
	    }
	  while (j < 0);
	}

      prodp[0] = cy_limb;
      prodp++;

      /* For each iteration in the outer loop, multiply one limb from
	 U with one limb from V, and add it to PROD.  */
      for (i = 1; i < vsize; i++)
	{
	  v_limb = vp[i];
	  if (v_limb <= 1)
	    {
	      cy_limb = 0;
	      if (v_limb == 1)
		cy_limb = mpn_add (prodp - usize,
				    prodp - usize, usize, up - usize, usize);
	    }
	  else
	    {
	      cy_limb = 0;
	      j = -usize;

	      do
		{
		  umul_ppmm (prod_high, prod_low, up[j], v_limb);
		  add_ssaaaa (cy_limb, prod_low,
			      prod_high, prod_low, 0, cy_limb);
		  add_ssaaaa (cy_limb, prodp[j],
			      cy_limb, prod_low, 0, prodp[j]);
		  j++;
		}
	      while (j < 0);
	    }

	  prodp[0] = cy_limb;
	  prodp++;
	}

      return usize + vsize - (cy_limb == 0);
    }

  n = (usize + 1) / 2;

  /* Is USIZE larger than 1.5 times VSIZE?  Avoid Karatsuba's algorithm.  */
  if (2 * usize > 3 * vsize)
    {
      /* If U has at least twice as many limbs as V.  Split U in two
	 pieces, U1 and U0, such that U = U0 + U1*(2**BITS_PER_MP_LIMB)**N,
	 and recursively multiply the two pieces separately with V.  */

      mp_size u0_size;
      mp_ptr tmp;
      mp_size tmp_size;

      /* V1 (the high part of V) is zero.  */

      /* Calculate the length of U0.  It is normally equal to n, but
	 of course not for sure.  */
      for (u0_size = n; u0_size > 0 && up[u0_size - 1] == 0; u0_size--)
	;

      /* Perform (U0 * V).  */
      if (u0_size >= vsize)
	prod_size = mpn_mul (prodp, up, u0_size, vp, vsize);
      else
	prod_size = mpn_mul (prodp, vp, vsize, up, u0_size);
      MPN_MUL_VERIFY (prodp, prod_size, up, u0_size, vp, vsize);

      /* We have to zero-extend the lower partial product to n limbs,
	 since the mpn_add some lines below expect the first n limbs
	 to be well defined.  (This is normally a no-op.  It may
	 do something when U1 has many leading 0 limbs.) */
      while (prod_size < n)
	prodp[prod_size++] = 0;

      tmp = (mp_ptr) alloca ((usize + vsize - n) * BYTES_PER_MP_LIMB);

      /* Perform (U1 * V).  Make sure the first source argument to mpn_mul
	 is not less than the second source argument.  */
      if (vsize <= usize - n)
	tmp_size = mpn_mul (tmp, up + n, usize - n, vp, vsize);
      else
	tmp_size = mpn_mul (tmp, vp, vsize, up + n, usize - n);
      MPN_MUL_VERIFY (tmp, tmp_size, up + n, usize - n, vp, vsize);

      /* In this addition hides a potentially large copying of TMP.  */
      if (prod_size - n >= tmp_size)
	cy = mpn_add (prodp + n, prodp + n, prod_size - n, tmp, tmp_size);
      else
	cy = mpn_add (prodp + n, tmp, tmp_size, prodp + n, prod_size - n);
      if (cy)
	abort (); /* prodp[prod_size] = cy; */

      alloca (0);
      return tmp_size + n;
    }
  else
    {
      /* Karatsuba's divide-and-conquer algorithm.

	 Split U in two pieces, U1 and U0, such that
	 U = U0 + U1*(B**n),
	 and V in V1 and V0, such that
	 V = V0 + V1*(B**n).

	 UV is then computed recursively using the identity

		2n   n        n                   n
	 UV = (B  + B )U V + B (U -U )(V -V ) + (B + 1)U V
                        1 1      1  0   0  1            0 0

	 Where B = 2**BITS_PER_MP_LIMB.
       */

      /* It's possible to decrease the temporary allocation by using the
	 prodp area for temporary storage of the middle term, and doing
	 that recursive multiplication first.  (Do this later.)  */

      mp_size u0_size;
      mp_size v0_size;
      mp_size u0v0_size;
      mp_size u1v1_size;
      mp_ptr temp;
      mp_size temp_size;
      mp_size utem_size;
      mp_size vtem_size;
      mp_ptr ptem;
      mp_size ptem_size;
      int negflg;
      mp_ptr pp;

      pp = (mp_ptr) alloca (4 * n * BYTES_PER_MP_LIMB);

      /* Calculate the lengths of U0 and V0.  They are normally equal
	 to n, but of course not for sure.  */
      for (u0_size = n; u0_size > 0 && up[u0_size - 1] == 0; u0_size--)
	;
      for (v0_size = n; v0_size > 0 && vp[v0_size - 1] == 0; v0_size--)
	;

      /*** 1. PROD]2n..0] := U0 x V0
	    (Recursive call to mpn_mul may NOT overwrite input operands.)
	     ________________  ________________
	    |________________||____U0 x V0_____|  */

      if (u0_size >= v0_size)
	u0v0_size = mpn_mul (pp, up, u0_size, vp, v0_size);
      else
	u0v0_size = mpn_mul (pp, vp, v0_size, up, u0_size);
      MPN_MUL_VERIFY (pp, u0v0_size, up, u0_size, vp, v0_size);

      /* Zero-extend to 2n limbs. */
      while (u0v0_size < 2 * n)
	pp[u0v0_size++] = 0;


      /*** 2. PROD]4n..2n] := U1 x V1
	    (Recursive call to mpn_mul may NOT overwrite input operands.)
	     ________________  ________________
	    |_____U1 x V1____||____U0 x V0_____|  */

      u1v1_size = mpn_mul (pp + 2*n,
			     up + n, usize - n,
			     vp + n, vsize - n);
      MPN_MUL_VERIFY (pp + 2*n, u1v1_size,
		      up + n, usize - n, vp + n, vsize - n);
      prod_size = 2 * n + u1v1_size;


      /*** 3. PTEM]2n..0] := (U1-U0) x (V0-V1)
	    (Recursive call to mpn_mul may overwrite input operands.)
	     ________________
	    |_(U1-U0)(V0-V1)_|  */

      temp = (mp_ptr) alloca ((2 * n + 1) * BYTES_PER_MP_LIMB);
      if (usize - n > u0_size
	  || (usize - n == u0_size
	      && mpn_cmp (up + n, up, u0_size) >= 0))
	{
	  utem_size = usize - n
	    + mpn_sub (temp, up + n, usize - n, up, u0_size);
	  negflg = 0;
	}
      else
	{
	  utem_size = u0_size
	    + mpn_sub (temp, up, u0_size, up + n, usize - n);
	  negflg = 1;
	}
      if (vsize - n > v0_size
	  || (vsize - n == v0_size
	      && mpn_cmp (vp + n, vp, v0_size) >= 0))
	{
	  vtem_size = vsize - n
	    + mpn_sub (temp + n, vp + n, vsize - n, vp, v0_size);
	  negflg ^= 1;
	}
      else
	{
	  vtem_size = v0_size
	    + mpn_sub (temp + n, vp, v0_size, vp + n, vsize - n);
	  /* No change of NEGFLG.  */
	}
      ptem = (mp_ptr) alloca (2 * n * BYTES_PER_MP_LIMB);
      if (utem_size >= vtem_size)
	ptem_size = mpn_mul (ptem, temp, utem_size, temp + n, vtem_size);
      else
	ptem_size = mpn_mul (ptem, temp + n, vtem_size, temp, utem_size);
      MPN_MUL_VERIFY (ptem, ptem_size, temp, utem_size, temp + n, vtem_size);

      /*** 4. TEMP]2n..0] := PROD]2n..0] + PROD]4n..2n]
	      ________________
	     |_____U1 x V1____|
	      ________________
	     |_____U0_x_V0____|  */

      cy = mpn_add (temp, pp, 2*n, pp + 2*n, u1v1_size);
      if (cy != 0)
	{
	  temp[2*n] = cy;
	  temp_size = 2*n + 1;
	}
      else
	{
	  /* Normalize temp.  pp[2*n-1] might have been zero in the
	     mpn_add call above, and thus temp might be unnormalized.  */
	  for (temp_size = 2*n; temp_size > 0 && temp[temp_size - 1] == 0;
	       temp_size--)
	    ;
	}

      if (prod_size - n >= temp_size)
	cy = mpn_add (pp + n, pp + n, prod_size - n, temp, temp_size);
      else
	{
	  /* This is a weird special case that should not happen (often)!  */
	  cy = mpn_add (pp + n, temp, temp_size, pp + n, prod_size - n);
	  prod_size = temp_size + n;
	}
      if (cy != 0)
	{
	  pp[prod_size] = cy;
	  prod_size++;
	}
#ifdef DEBUG
      if (prod_size > 4 * n)
	abort();
#endif
      if (negflg)
	prod_size = prod_size
	  + mpn_sub (pp + n, pp + n, prod_size - n, ptem, ptem_size);
      else
	{
	  if (prod_size - n < ptem_size)
	    abort();
	  cy = mpn_add (pp + n, pp + n, prod_size - n, ptem, ptem_size);
	  if (cy != 0)
	    {
	      pp[prod_size] = cy;
	      prod_size++;
#ifdef DEBUG
	      if (prod_size > 4 * n)
		abort();
#endif
	    }
	}

      MPN_COPY (prodp, pp, prod_size);
      alloca (0);
      return prod_size;
    }
}
OpenPOWER on IntegriCloud