1/* mpz_2fac_ui(RESULT, N) -- Set RESULT to N!!. 2 3Contributed to the GNU project by Marco Bodrato. 4 5Copyright 2012, 2015, 2018 Free Software Foundation, Inc. 6 7This file is part of the GNU MP Library. 8 9The GNU MP Library is free software; you can redistribute it and/or modify 10it 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 16or 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 22or both in parallel, as here. 23 24The GNU MP Library is distributed in the hope that it will be useful, but 25WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 26or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 27for more details. 28 29You should have received copies of the GNU General Public License and the 30GNU Lesser General Public License along with the GNU MP Library. If not, 31see https://www.gnu.org/licenses/. */ 32 33#include "gmp-impl.h" 34 35#define FACTOR_LIST_STORE(P, PR, MAX_PR, VEC, I) \ 36 do { \ 37 if ((PR) > (MAX_PR)) { \ 38 (VEC)[(I)++] = (PR); \ 39 (PR) = (P); \ 40 } else \ 41 (PR) *= (P); \ 42 } while (0) 43 44#define FAC_2DSC_THRESHOLD ((FAC_DSC_THRESHOLD << 1) | (FAC_DSC_THRESHOLD & 1)) 45#define FACTORS_PER_LIMB (GMP_NUMB_BITS / (LOG2C(FAC_2DSC_THRESHOLD-1)+1)) 46 47/* Computes n!!, the 2-multi-factorial of n. (aka double-factorial or semi-factorial) 48 WARNING: it assumes that n fits in a limb! 49 */ 50void 51mpz_2fac_ui (mpz_ptr x, unsigned long n) 52{ 53 ASSERT (n <= GMP_NUMB_MAX); 54 55 if ((n & 1) == 0) { /* n is even, n = 2k, (2k)!! = k! 2^k */ 56 mp_limb_t count; 57 58 if ((n <= TABLE_LIMIT_2N_MINUS_POPC_2N) & (n != 0)) 59 count = __gmp_fac2cnt_table[n / 2 - 1]; 60 else 61 { 62 popc_limb (count, n); /* popc(n) == popc(k) */ 63 count = n - count; /* n - popc(n) == k + k - popc(k) */ 64 } 65 mpz_oddfac_1 (x, n >> 1, 0); 66 mpz_mul_2exp (x, x, count); 67 } else { /* n is odd */ 68 if (n <= ODD_DOUBLEFACTORIAL_TABLE_LIMIT) { 69 MPZ_NEWALLOC (x, 1)[0] = __gmp_odd2fac_table[n >> 1]; 70 SIZ (x) = 1; 71 } else if (BELOW_THRESHOLD (n, FAC_2DSC_THRESHOLD)) { /* odd basecase, */ 72 mp_limb_t *factors, prod, max_prod; 73 mp_size_t j; 74 TMP_SDECL; 75 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)); 79 80 factors[0] = ODD_DOUBLEFACTORIAL_TABLE_MAX; 81 j = 1; 82 prod = n; 83 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); 87 88 factors[j++] = prod; 89 mpz_prodlimbs (x, factors, j); 90 91 TMP_SFREE; 92 } else { /* for the asymptotically fast odd case, let oddfac do the job. */ 93 mpz_oddfac_1 (x, n, 1); 94 } 95 } 96} 97 98#undef FACTORS_PER_LIMB 99#undef FACTOR_LIST_STORE 100#undef FAC_2DSC_THRESHOLD 101