Coverage Report

Created: 2024-11-21 07:03

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