beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / 2fac_ui.c
blob37e6c468bc82f0c9f280c14a6250b2107e29416b
1 /* mpz_2fac_ui(RESULT, N) -- Set RESULT to N!!.
3 Contributed to the GNU project by Marco Bodrato.
5 Copyright 2012 Free Software Foundation, Inc.
7 This file is part of the GNU MP Library.
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of either:
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.
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.
22 or both in parallel, as here.
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.
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/. */
33 #include "gmp.h"
34 #include "gmp-impl.h"
36 #define FACTOR_LIST_STORE(P, PR, MAX_PR, VEC, I) \
37 do { \
38 if ((PR) > (MAX_PR)) { \
39 (VEC)[(I)++] = (PR); \
40 (PR) = (P); \
41 } else \
42 (PR) *= (P); \
43 } while (0)
45 #define FAC_2DSC_THRESHOLD ((FAC_DSC_THRESHOLD << 1) | (FAC_DSC_THRESHOLD & 1))
46 #define FACTORS_PER_LIMB (GMP_NUMB_BITS / (LOG2C(FAC_2DSC_THRESHOLD-1)+1))
48 /* Computes n!!, the 2-multi-factorial of n. (aka double-factorial or semi-factorial)
49 WARNING: it assumes that n fits in a limb!
51 void
52 mpz_2fac_ui (mpz_ptr x, unsigned long n)
54 ASSERT (n <= GMP_NUMB_MAX);
56 if ((n & 1) == 0) { /* n is even, n = 2k, (2k)!! = k! 2^k */
57 mp_limb_t count;
59 if ((n <= TABLE_LIMIT_2N_MINUS_POPC_2N) & (n != 0))
60 count = __gmp_fac2cnt_table[n / 2 - 1];
61 else
63 popc_limb (count, n); /* popc(n) == popc(k) */
64 count = n - count; /* n - popc(n) == k + k - popc(k) */
66 mpz_oddfac_1 (x, n >> 1, 0);
67 mpz_mul_2exp (x, x, count);
68 } else { /* n is odd */
69 if (n <= ODD_DOUBLEFACTORIAL_TABLE_LIMIT) {
70 PTR (x)[0] = __gmp_odd2fac_table[n >> 1];
71 SIZ (x) = 1;
72 } else if (BELOW_THRESHOLD (n, FAC_2DSC_THRESHOLD)) { /* odd basecase, */
73 mp_limb_t *factors, prod, max_prod, j;
74 TMP_SDECL;
76 /* FIXME: we might alloc a fixed amount 1+FAC_2DSC_THRESHOLD/FACTORS_PER_LIMB */
77 TMP_SMARK;
78 factors = TMP_SALLOC_LIMBS (1 + n / (2 * FACTORS_PER_LIMB));
80 factors[0] = ODD_DOUBLEFACTORIAL_TABLE_MAX;
81 j = 1;
82 prod = n;
84 max_prod = GMP_NUMB_MAX / FAC_2DSC_THRESHOLD;
85 while ((n -= 2) > ODD_DOUBLEFACTORIAL_TABLE_LIMIT)
86 FACTOR_LIST_STORE (n, prod, max_prod, factors, j);
88 factors[j++] = prod;
89 mpz_prodlimbs (x, factors, j);
91 TMP_SFREE;
92 } else { /* for the asymptotically fast odd case, let oddfac do the job. */
93 mpz_oddfac_1 (x, n, 1);
98 #undef FACTORS_PER_LIMB
99 #undef FACTOR_LIST_STORE
100 #undef FAC_2DSC_THRESHOLD