xref: /netbsd-src/external/lgpl3/gmp/dist/mpz/fac_ui.c (revision 479d8f7d843cc1b22d497efdf1f27a50ee8418d4)
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, 1994, 1995, 2000, 2001, 2002, 2003, 2011, 2012
6 Free 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 the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
14 
15 The GNU MP Library is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18 License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
22 
23 #include "gmp.h"
24 #include "gmp-impl.h"
25 
26 #define FACTOR_LIST_STORE(P, PR, MAX_PR, VEC, I)		\
27   do {								\
28     if ((PR) > (MAX_PR)) {					\
29       (VEC)[(I)++] = (PR);					\
30       (PR) = (P);						\
31     } else							\
32       (PR) *= (P);						\
33   } while (0)
34 
35 #if TUNE_PROGRAM_BUILD
36 #define FACTORS_PER_LIMB (GMP_NUMB_BITS / (LOG2C(FAC_DSC_THRESHOLD_LIMIT-1)+1))
37 #else
38 #define FACTORS_PER_LIMB (GMP_NUMB_BITS / (LOG2C(FAC_ODD_THRESHOLD)+1))
39 #endif
40 
41 /* Computes n!, the factorial of n.
42    WARNING: it assumes that n fits in a limb!
43  */
44 void
45 mpz_fac_ui (mpz_ptr x, unsigned long n)
46 {
47   static const mp_limb_t table[] = { ONE_LIMB_FACTORIAL_TABLE };
48 
49   ASSERT (n <= GMP_NUMB_MAX);
50 
51   if (n < numberof (table))
52     {
53       PTR (x)[0] = table[n];
54       SIZ (x) = 1;
55     }
56   else if (BELOW_THRESHOLD (n, FAC_ODD_THRESHOLD))
57     {
58       mp_limb_t prod, max_prod;
59       mp_size_t j;
60       mp_ptr    factors;
61       TMP_SDECL;
62 
63       TMP_SMARK;
64       factors = TMP_SALLOC_LIMBS (2 + (n - numberof (table)) / FACTORS_PER_LIMB);
65 
66       factors[0] = table[numberof (table)-1];
67       j = 1;
68       prod = n;
69 #if TUNE_PROGRAM_BUILD
70       max_prod = GMP_NUMB_MAX / FAC_DSC_THRESHOLD_LIMIT;
71 #else
72       max_prod = GMP_NUMB_MAX / (FAC_ODD_THRESHOLD | 1);
73 #endif
74       while (--n >= numberof (table))
75 	FACTOR_LIST_STORE (n, prod, max_prod, factors, j);
76 
77       factors[j++] = prod;
78       mpz_prodlimbs (x, factors, j);
79 
80       TMP_SFREE;
81     }
82   else
83     {
84       mp_limb_t count;
85       mpz_oddfac_1 (x, n, 0);
86       if (n <= TABLE_LIMIT_2N_MINUS_POPC_2N)
87 	count = __gmp_fac2cnt_table[n / 2 - 1];
88       else
89 	{
90 	  popc_limb (count, n);
91 	  count = n - count;
92 	}
93       mpz_mul_2exp (x, x, count);
94     }
95 }
96 
97 #undef FACTORS_PER_LIMB
98 #undef FACTOR_LIST_STORE
99