1 /* mpz_mul_ui/si (product, multiplier, small_multiplicand) -- Set PRODUCT to
2 MULTIPLICATOR times SMALL_MULTIPLICAND.
4 Copyright 1991, 1993, 1994, 1996, 2000-2002, 2005, 2008, 2012 Free Software
7 This file is part of the GNU MP Library.
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free Software
19 Foundation; either version 2 of the License, or (at your option) any
22 or both in parallel, as here.
24 The GNU MP Library is distributed in the hope that it will be useful, but
25 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 You should have received copies of the GNU General Public License and the
30 GNU Lesser General Public License along with the GNU MP Library. If not,
31 see https://www.gnu.org/licenses/. */
37 #ifdef OPERATION_mul_si
38 #define FUNCTION mpz_mul_si
39 #define MULTIPLICAND_UNSIGNED
40 #define MULTIPLICAND_ABS(x) ABS_CAST(unsigned long, (x))
43 #ifdef OPERATION_mul_ui
44 #define FUNCTION mpz_mul_ui
45 #define MULTIPLICAND_UNSIGNED unsigned
46 #define MULTIPLICAND_ABS(x) x
50 Error
, error
, unrecognised OPERATION
55 FUNCTION (mpz_ptr prod
, mpz_srcptr mult
,
56 MULTIPLICAND_UNSIGNED
long int small_mult
)
59 mp_size_t sign_product
;
64 sign_product
= SIZ(mult
);
65 if (sign_product
== 0 || small_mult
== 0)
71 size
= ABS (sign_product
);
73 sml
= MULTIPLICAND_ABS (small_mult
);
75 if (sml
<= GMP_NUMB_MAX
)
77 pp
= MPZ_REALLOC (prod
, size
+ 1);
78 cy
= mpn_mul_1 (pp
, PTR(mult
), size
, sml
);
82 #if GMP_NAIL_BITS != 0
85 /* Operand too large for the current nails size. Use temporary for
86 intermediate products, to allow prod and mult being identical. */
91 tp
= TMP_ALLOC_LIMBS (size
+ 2);
93 /* Use, maybe, mpn_mul_2? */
94 cy
= mpn_mul_1 (tp
, PTR(mult
), size
, sml
& GMP_NUMB_MASK
);
96 cy
= mpn_addmul_1 (tp
+ 1, PTR(mult
), size
, sml
>> GMP_NUMB_BITS
);
99 MPN_NORMALIZE_NOT_ZERO (tp
, size
); /* too general, need to trim one or two limb */
100 pp
= MPZ_REALLOC (prod
, size
);
101 MPN_COPY (pp
, tp
, size
);
106 SIZ(prod
) = ((sign_product
< 0) ^ (small_mult
< 0)) ? -size
: size
;