xref: /netbsd-src/external/lgpl3/gmp/dist/mpz/mul_i.h (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* mpz_mul_ui/si (product, multiplier, small_multiplicand) -- Set PRODUCT to
2    MULTIPLICATOR times SMALL_MULTIPLICAND.
3 
4 Copyright 1991, 1993, 1994, 1996, 2000-2002, 2005, 2008, 2012 Free Software
5 Foundation, Inc.
6 
7 This file is part of the GNU MP Library.
8 
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of either:
11 
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.
15 
16 or
17 
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.
21 
22 or both in parallel, as here.
23 
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.
28 
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/.  */
32 
33 #include "gmp-impl.h"
34 
35 
36 #ifdef OPERATION_mul_si
37 #define FUNCTION               mpz_mul_si
38 #define MULTIPLICAND_UNSIGNED
39 #define MULTIPLICAND_ABS(x)    ABS_CAST(unsigned long, (x))
40 #endif
41 
42 #ifdef OPERATION_mul_ui
43 #define FUNCTION               mpz_mul_ui
44 #define MULTIPLICAND_UNSIGNED  unsigned
45 #define MULTIPLICAND_ABS(x)    x
46 #endif
47 
48 #ifndef FUNCTION
49 Error, error, unrecognised OPERATION
50 #endif
51 
52 
53 void
54 FUNCTION (mpz_ptr prod, mpz_srcptr mult,
55           MULTIPLICAND_UNSIGNED long int small_mult)
56 {
57   mp_size_t size;
58   mp_size_t sign_product;
59   mp_limb_t sml;
60   mp_limb_t cy;
61   mp_ptr pp;
62 
63   sign_product = SIZ(mult);
64   if (sign_product == 0 || small_mult == 0)
65     {
66       SIZ(prod) = 0;
67       return;
68     }
69 
70   size = ABS (sign_product);
71 
72   sml = MULTIPLICAND_ABS (small_mult);
73 
74   if (sml <= GMP_NUMB_MAX)
75     {
76       pp = MPZ_REALLOC (prod, size + 1);
77       cy = mpn_mul_1 (pp, PTR(mult), size, sml);
78       pp[size] = cy;
79       size += cy != 0;
80     }
81 #if GMP_NAIL_BITS != 0
82   else
83     {
84       /* Operand too large for the current nails size.  Use temporary for
85 	 intermediate products, to allow prod and mult being identical.  */
86       mp_ptr tp;
87       TMP_DECL;
88       TMP_MARK;
89 
90       tp = TMP_ALLOC_LIMBS (size + 2);
91 
92       /* Use, maybe, mpn_mul_2? */
93       cy = mpn_mul_1 (tp, PTR(mult), size, sml & GMP_NUMB_MASK);
94       tp[size] = cy;
95       cy = mpn_addmul_1 (tp + 1, PTR(mult), size, sml >> GMP_NUMB_BITS);
96       tp[size + 1] = cy;
97       size += 2;
98       MPN_NORMALIZE_NOT_ZERO (tp, size); /* too general, need to trim one or two limb */
99       pp = MPZ_NEWALLOC (prod, size);
100       MPN_COPY (pp, tp, size);
101       TMP_FREE;
102     }
103 #endif
104 
105   SIZ(prod) = ((sign_product < 0) ^ (small_mult < 0)) ? -size : size;
106 }
107