Blame mpz/2fac_ui.c

Packit 5c3484
/* mpz_2fac_ui(RESULT, N) -- Set RESULT to N!!.
Packit 5c3484
Packit 5c3484
Contributed to the GNU project by Marco Bodrato.
Packit 5c3484
Packit 5c3484
Copyright 2012 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
Packit 5c3484
#define FACTOR_LIST_STORE(P, PR, MAX_PR, VEC, I)		\
Packit 5c3484
  do {								\
Packit 5c3484
    if ((PR) > (MAX_PR)) {					\
Packit 5c3484
      (VEC)[(I)++] = (PR);					\
Packit 5c3484
      (PR) = (P);						\
Packit 5c3484
    } else							\
Packit 5c3484
      (PR) *= (P);						\
Packit 5c3484
  } while (0)
Packit 5c3484
Packit 5c3484
#define FAC_2DSC_THRESHOLD ((FAC_DSC_THRESHOLD << 1) | (FAC_DSC_THRESHOLD & 1))
Packit 5c3484
#define FACTORS_PER_LIMB   (GMP_NUMB_BITS / (LOG2C(FAC_2DSC_THRESHOLD-1)+1))
Packit 5c3484
Packit 5c3484
/* Computes n!!, the 2-multi-factorial of n. (aka double-factorial or semi-factorial)
Packit 5c3484
   WARNING: it assumes that n fits in a limb!
Packit 5c3484
 */
Packit 5c3484
void
Packit 5c3484
mpz_2fac_ui (mpz_ptr x, unsigned long n)
Packit 5c3484
{
Packit 5c3484
  ASSERT (n <= GMP_NUMB_MAX);
Packit 5c3484
Packit 5c3484
  if ((n & 1) == 0) { /* n is even, n = 2k, (2k)!! = k! 2^k */
Packit 5c3484
    mp_limb_t count;
Packit 5c3484
Packit 5c3484
    if ((n <= TABLE_LIMIT_2N_MINUS_POPC_2N) & (n != 0))
Packit 5c3484
      count = __gmp_fac2cnt_table[n / 2 - 1];
Packit 5c3484
    else
Packit 5c3484
      {
Packit 5c3484
	popc_limb (count, n);	/* popc(n) == popc(k) */
Packit 5c3484
	count = n - count;		/* n - popc(n) == k + k - popc(k) */
Packit 5c3484
      }
Packit 5c3484
    mpz_oddfac_1 (x, n >> 1, 0);
Packit 5c3484
    mpz_mul_2exp (x, x, count);
Packit 5c3484
  } else { /* n is odd */
Packit 5c3484
    if (n <= ODD_DOUBLEFACTORIAL_TABLE_LIMIT) {
Packit 5c3484
	PTR (x)[0] = __gmp_odd2fac_table[n >> 1];
Packit 5c3484
	SIZ (x) = 1;
Packit 5c3484
    } else if (BELOW_THRESHOLD (n, FAC_2DSC_THRESHOLD)) { /* odd basecase, */
Packit 5c3484
      mp_limb_t *factors, prod, max_prod, j;
Packit 5c3484
      TMP_SDECL;
Packit 5c3484
Packit 5c3484
      /* FIXME: we might alloc a fixed amount 1+FAC_2DSC_THRESHOLD/FACTORS_PER_LIMB */
Packit 5c3484
      TMP_SMARK;
Packit 5c3484
      factors = TMP_SALLOC_LIMBS (1 + n / (2 * FACTORS_PER_LIMB));
Packit 5c3484
Packit 5c3484
      factors[0] = ODD_DOUBLEFACTORIAL_TABLE_MAX;
Packit 5c3484
      j = 1;
Packit 5c3484
      prod = n;
Packit 5c3484
Packit 5c3484
      max_prod = GMP_NUMB_MAX / FAC_2DSC_THRESHOLD;
Packit 5c3484
      while ((n -= 2) > ODD_DOUBLEFACTORIAL_TABLE_LIMIT)
Packit 5c3484
	FACTOR_LIST_STORE (n, prod, max_prod, factors, j);
Packit 5c3484
Packit 5c3484
      factors[j++] = prod;
Packit 5c3484
      mpz_prodlimbs (x, factors, j);
Packit 5c3484
Packit 5c3484
      TMP_SFREE;
Packit 5c3484
    } else { /* for the asymptotically fast odd case, let oddfac do the job. */
Packit 5c3484
      mpz_oddfac_1 (x, n, 1);
Packit 5c3484
    }
Packit 5c3484
  }
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
#undef FACTORS_PER_LIMB
Packit 5c3484
#undef FACTOR_LIST_STORE
Packit 5c3484
#undef FAC_2DSC_THRESHOLD