Coverage Report

Created: 2024-11-21 07:03

/src/libgmp/mpz/fac_ui.c
Line
Count
Source (jump to first uncovered line)
1
/* mpz_fac_ui(RESULT, N) -- Set RESULT to N!.
2
3
Contributed to the GNU project by Marco Bodrato.
4
5
Copyright 1991, 1993-1995, 2000-2003, 2011, 2012, 2015, 2021 Free
6
Software Foundation, Inc.
7
8
This file is part of the GNU MP Library.
9
10
The GNU MP Library is free software; you can redistribute it and/or modify
11
it under the terms of either:
12
13
  * the GNU Lesser General Public License as published by the Free
14
    Software Foundation; either version 3 of the License, or (at your
15
    option) any later version.
16
17
or
18
19
  * the GNU General Public License as published by the Free Software
20
    Foundation; either version 2 of the License, or (at your option) any
21
    later version.
22
23
or both in parallel, as here.
24
25
The GNU MP Library is distributed in the hope that it will be useful, but
26
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
27
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
28
for more details.
29
30
You should have received copies of the GNU General Public License and the
31
GNU Lesser General Public License along with the GNU MP Library.  If not,
32
see https://www.gnu.org/licenses/.  */
33
34
#include "gmp-impl.h"
35
36
#define FACTOR_LIST_STORE(P, PR, MAX_PR, VEC, I)    \
37
0
  do {               \
38
0
    if ((PR) > (MAX_PR)) {         \
39
0
      (VEC)[(I)++] = (PR);          \
40
0
      (PR) = (P);           \
41
0
    } else             \
42
0
      (PR) *= (P);           \
43
0
  } while (0)
44
45
#if TUNE_PROGRAM_BUILD
46
#define FACTORS_PER_LIMB (GMP_NUMB_BITS / (LOG2C(FAC_DSC_THRESHOLD_LIMIT-1)+1))
47
#else
48
#define FACTORS_PER_LIMB (GMP_NUMB_BITS / (LOG2C(FAC_ODD_THRESHOLD)+1))
49
#endif
50
51
/* Computes n!, the factorial of n.
52
   WARNING: it assumes that n fits in a limb!
53
 */
54
void
55
mpz_fac_ui (mpz_ptr x, unsigned long n)
56
4
{
57
4
  static const mp_limb_t table[] = { ONE_LIMB_FACTORIAL_TABLE };
58
59
4
  ASSERT (n <= GMP_NUMB_MAX);
60
61
4
  if (n < numberof (table))
62
4
    {
63
4
      MPZ_NEWALLOC (x, 1)[0] = table[n];
64
4
      SIZ (x) = 1;
65
4
    }
66
0
  else if (BELOW_THRESHOLD (n, FAC_ODD_THRESHOLD))
67
0
    {
68
0
      mp_limb_t prod, max_prod;
69
0
      mp_size_t j;
70
0
      mp_ptr    factors;
71
0
      mp_limb_t fac, diff = n - numberof (table);
72
0
      TMP_SDECL;
73
74
0
      TMP_SMARK;
75
0
      factors = TMP_SALLOC_LIMBS (2 + diff / FACTORS_PER_LIMB);
76
77
0
      factors[0] = table[numberof (table)-1];
78
0
      j = 1;
79
0
      if ((diff & 1) == 0)
80
0
  {
81
0
    prod = n;
82
    /* if (diff != 0) */
83
0
      fac = --n * numberof (table);
84
0
  }
85
0
      else
86
0
  {
87
0
    prod = n * numberof (table);
88
0
    fac = prod + --diff;
89
0
  }
90
91
#if TUNE_PROGRAM_BUILD
92
      max_prod = GMP_NUMB_MAX / (FAC_DSC_THRESHOLD_LIMIT * FAC_DSC_THRESHOLD_LIMIT);
93
#else
94
0
      max_prod = GMP_NUMB_MAX /
95
0
  (((FAC_ODD_THRESHOLD + numberof (table) + 1) / 2) *
96
0
   ((FAC_ODD_THRESHOLD + numberof (table)) / 2));
97
0
#endif
98
0
      for (;diff != 0; fac += (diff -= 2))
99
0
  FACTOR_LIST_STORE (fac, prod, max_prod, factors, j);
100
101
0
      factors[j++] = prod;
102
0
      mpz_prodlimbs (x, factors, j);
103
104
0
      TMP_SFREE;
105
0
    }
106
0
  else
107
0
    {
108
0
      mp_limb_t count;
109
0
      mpz_oddfac_1 (x, n, 0);
110
0
      if (n <= TABLE_LIMIT_2N_MINUS_POPC_2N)
111
0
  count = __gmp_fac2cnt_table[n / 2 - 1];
112
0
      else
113
0
  {
114
0
    popc_limb (count, n);
115
0
    count = n - count;
116
0
  }
117
0
      mpz_mul_2exp (x, x, count);
118
0
    }
119
4
}
120
121
#undef FACTORS_PER_LIMB
122
#undef FACTOR_LIST_STORE