1 /* mpn_mul -- Multiply two natural numbers.
3 Copyright (C) 1991-2017 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library; see the file COPYING.LIB. If not, see
19 <http://www.gnu.org/licenses/>. */
24 /* Multiply the natural numbers u (pointed to by UP, with USIZE limbs)
25 and v (pointed to by VP, with VSIZE limbs), and store the result at
26 PRODP. USIZE + VSIZE limbs are always stored, but if the input
27 operands are normalized. Return the most significant limb of the
30 NOTE: The space pointed to by PRODP is overwritten before finished
31 with U and V, so overlap is an error.
35 2. PRODP != UP and PRODP != VP, i.e. the destination
36 must be distinct from the multiplier and the multiplicand. */
38 /* If KARATSUBA_THRESHOLD is not already defined, define it to a
39 value which is good on most machines. */
40 #ifndef KARATSUBA_THRESHOLD
41 #define KARATSUBA_THRESHOLD 32
45 mpn_mul (mp_ptr prodp
,
46 mp_srcptr up
, mp_size_t usize
,
47 mp_srcptr vp
, mp_size_t vsize
)
49 mp_ptr prod_endp
= prodp
+ usize
+ vsize
- 1;
54 if (vsize
< KARATSUBA_THRESHOLD
)
56 /* Handle simple cases with traditional multiplication.
58 This is the most critical code of the entire function. All
59 multiplies rely on this, both small and huge. Small ones arrive
60 here immediately. Huge ones arrive here as this is the base case
61 for Karatsuba's recursive algorithm below. */
69 /* Multiply by the first limb in V separately, as the result can be
70 stored (not added) to PROD. We also avoid a loop for zeroing. */
75 MPN_COPY (prodp
, up
, usize
);
77 MPN_ZERO (prodp
, usize
);
81 cy_limb
= mpn_mul_1 (prodp
, up
, usize
, v_limb
);
83 prodp
[usize
] = cy_limb
;
86 /* For each iteration in the outer loop, multiply one limb from
87 U with one limb from V, and add it to PROD. */
88 for (i
= 1; i
< vsize
; i
++)
95 cy_limb
= mpn_add_n (prodp
, prodp
, up
, usize
);
98 cy_limb
= mpn_addmul_1 (prodp
, up
, usize
, v_limb
);
100 prodp
[usize
] = cy_limb
;
108 tspace
= (mp_ptr
) TMP_ALLOC (2 * vsize
* BYTES_PER_MP_LIMB
);
109 MPN_MUL_N_RECURSE (prodp
, up
, vp
, vsize
, tspace
);
116 mp_ptr tp
= (mp_ptr
) TMP_ALLOC (2 * vsize
* BYTES_PER_MP_LIMB
);
119 MPN_MUL_N_RECURSE (tp
, up
, vp
, vsize
, tspace
);
120 cy
= mpn_add_n (prodp
, prodp
, tp
, vsize
);
121 mpn_add_1 (prodp
+ vsize
, tp
+ vsize
, vsize
, cy
);
126 while (usize
>= vsize
);
129 /* True: usize < vsize. */
131 /* Make life simple: Recurse. */
135 mpn_mul (tspace
, vp
, vsize
, up
, usize
);
136 cy
= mpn_add_n (prodp
, prodp
, tspace
, vsize
);
137 mpn_add_1 (prodp
+ vsize
, tspace
+ vsize
, usize
, cy
);