beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / mul_i.h
blob486a8fd63e6f7ddcec33c61cfeb60f3396f5b163
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
5 Foundation, Inc.
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
20 later version.
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
27 for more details.
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/. */
33 #include "gmp.h"
34 #include "gmp-impl.h"
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))
41 #endif
43 #ifdef OPERATION_mul_ui
44 #define FUNCTION mpz_mul_ui
45 #define MULTIPLICAND_UNSIGNED unsigned
46 #define MULTIPLICAND_ABS(x) x
47 #endif
49 #ifndef FUNCTION
50 Error, error, unrecognised OPERATION
51 #endif
54 void
55 FUNCTION (mpz_ptr prod, mpz_srcptr mult,
56 MULTIPLICAND_UNSIGNED long int small_mult)
58 mp_size_t size;
59 mp_size_t sign_product;
60 mp_limb_t sml;
61 mp_limb_t cy;
62 mp_ptr pp;
64 sign_product = SIZ(mult);
65 if (sign_product == 0 || small_mult == 0)
67 SIZ(prod) = 0;
68 return;
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);
79 pp[size] = cy;
80 size += cy != 0;
82 #if GMP_NAIL_BITS != 0
83 else
85 /* Operand too large for the current nails size. Use temporary for
86 intermediate products, to allow prod and mult being identical. */
87 mp_ptr tp;
88 TMP_DECL;
89 TMP_MARK;
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);
95 tp[size] = cy;
96 cy = mpn_addmul_1 (tp + 1, PTR(mult), size, sml >> GMP_NUMB_BITS);
97 tp[size + 1] = cy;
98 size += 2;
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);
102 TMP_FREE;
104 #endif
106 SIZ(prod) = ((sign_product < 0) ^ (small_mult < 0)) ? -size : size;