Coverage Report

Created: 2025-04-11 06:45

/src/gmp-6.2.1/mpz/mul_i.h
Line
Count
Source (jump to first uncovered line)
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
60
#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
71
#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
143
{
57
143
  mp_size_t size;
58
143
  mp_size_t sign_product;
59
143
  mp_limb_t sml;
60
143
  mp_limb_t cy;
61
143
  mp_ptr pp;
62
63
143
  sign_product = SIZ(mult);
64
143
  if (sign_product == 0 || small_mult == 0)
65
12
    {
66
12
      SIZ(prod) = 0;
67
12
      return;
68
12
    }
69
70
131
  size = ABS (sign_product);
71
72
131
  sml = MULTIPLICAND_ABS (small_mult);
73
74
131
  if (sml <= GMP_NUMB_MAX)
75
131
    {
76
131
      pp = MPZ_REALLOC (prod, size + 1);
77
131
      cy = mpn_mul_1 (pp, PTR(mult), size, sml);
78
131
      pp[size] = cy;
79
131
      size += cy != 0;
80
131
    }
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
131
  SIZ(prod) = ((sign_product < 0) ^ (small_mult < 0)) ? -size : size;
106
131
}
__gmpz_mul_ui
Line
Count
Source
56
77
{
57
77
  mp_size_t size;
58
77
  mp_size_t sign_product;
59
77
  mp_limb_t sml;
60
77
  mp_limb_t cy;
61
77
  mp_ptr pp;
62
63
77
  sign_product = SIZ(mult);
64
77
  if (sign_product == 0 || small_mult == 0)
65
6
    {
66
6
      SIZ(prod) = 0;
67
6
      return;
68
6
    }
69
70
71
  size = ABS (sign_product);
71
72
71
  sml = MULTIPLICAND_ABS (small_mult);
73
74
71
  if (sml <= GMP_NUMB_MAX)
75
71
    {
76
71
      pp = MPZ_REALLOC (prod, size + 1);
77
71
      cy = mpn_mul_1 (pp, PTR(mult), size, sml);
78
71
      pp[size] = cy;
79
71
      size += cy != 0;
80
71
    }
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
71
  SIZ(prod) = ((sign_product < 0) ^ (small_mult < 0)) ? -size : size;
106
71
}
__gmpz_mul_si
Line
Count
Source
56
66
{
57
66
  mp_size_t size;
58
66
  mp_size_t sign_product;
59
66
  mp_limb_t sml;
60
66
  mp_limb_t cy;
61
66
  mp_ptr pp;
62
63
66
  sign_product = SIZ(mult);
64
66
  if (sign_product == 0 || small_mult == 0)
65
6
    {
66
6
      SIZ(prod) = 0;
67
6
      return;
68
6
    }
69
70
60
  size = ABS (sign_product);
71
72
60
  sml = MULTIPLICAND_ABS (small_mult);
73
74
60
  if (sml <= GMP_NUMB_MAX)
75
60
    {
76
60
      pp = MPZ_REALLOC (prod, size + 1);
77
60
      cy = mpn_mul_1 (pp, PTR(mult), size, sml);
78
60
      pp[size] = cy;
79
60
      size += cy != 0;
80
60
    }
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
60
  SIZ(prod) = ((sign_product < 0) ^ (small_mult < 0)) ? -size : size;
106
60
}