1/* mpz_mul_ui/si (product, multiplier, small_multiplicand) -- Set PRODUCT to
2   MULTIPLICATOR times SMALL_MULTIPLICAND.
3
4Copyright 1991, 1993, 1994, 1996, 2000-2002, 2005, 2008, 2012 Free Software
5Foundation, Inc.
6
7This file is part of the GNU MP Library.
8
9The GNU MP Library is free software; you can redistribute it and/or modify
10it 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
16or
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
22or both in parallel, as here.
23
24The GNU MP Library is distributed in the hope that it will be useful, but
25WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27for more details.
28
29You should have received copies of the GNU General Public License and the
30GNU Lesser General Public License along with the GNU MP Library.  If not,
31see 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
49Error, error, unrecognised OPERATION
50#endif
51
52
53void
54FUNCTION (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