github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpz/2fac_ui.c (about)

     1  /* mpz_2fac_ui(RESULT, N) -- Set RESULT to N!!.
     2  
     3  Contributed to the GNU project by Marco Bodrato.
     4  
     5  Copyright 2012 Free Software 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.h"
    34  #include "gmp-impl.h"
    35  
    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)
    44  
    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))
    47  
    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!
    50   */
    51  void
    52  mpz_2fac_ui (mpz_ptr x, unsigned long n)
    53  {
    54    ASSERT (n <= GMP_NUMB_MAX);
    55  
    56    if ((n & 1) == 0) { /* n is even, n = 2k, (2k)!! = k! 2^k */
    57      mp_limb_t count;
    58  
    59      if ((n <= TABLE_LIMIT_2N_MINUS_POPC_2N) & (n != 0))
    60        count = __gmp_fac2cnt_table[n / 2 - 1];
    61      else
    62        {
    63  	popc_limb (count, n);	/* popc(n) == popc(k) */
    64  	count = n - count;		/* n - popc(n) == k + k - popc(k) */
    65        }
    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;
    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