Blame mpz/fac_ui.c

Packit 5c3484
/* mpz_fac_ui(RESULT, N) -- Set RESULT to N!.
Packit 5c3484
Packit 5c3484
Contributed to the GNU project by Marco Bodrato.
Packit 5c3484
Packit 5c3484
Copyright 1991, 1993-1995, 2000-2003, 2011, 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
#if TUNE_PROGRAM_BUILD
Packit 5c3484
#define FACTORS_PER_LIMB (GMP_NUMB_BITS / (LOG2C(FAC_DSC_THRESHOLD_LIMIT-1)+1))
Packit 5c3484
#else
Packit 5c3484
#define FACTORS_PER_LIMB (GMP_NUMB_BITS / (LOG2C(FAC_ODD_THRESHOLD)+1))
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
/* Computes n!, the factorial of n.
Packit 5c3484
   WARNING: it assumes that n fits in a limb!
Packit 5c3484
 */
Packit 5c3484
void
Packit 5c3484
mpz_fac_ui (mpz_ptr x, unsigned long n)
Packit 5c3484
{
Packit 5c3484
  static const mp_limb_t table[] = { ONE_LIMB_FACTORIAL_TABLE };
Packit 5c3484
Packit 5c3484
  ASSERT (n <= GMP_NUMB_MAX);
Packit 5c3484
Packit 5c3484
  if (n < numberof (table))
Packit 5c3484
    {
Packit 5c3484
      PTR (x)[0] = table[n];
Packit 5c3484
      SIZ (x) = 1;
Packit 5c3484
    }
Packit 5c3484
  else if (BELOW_THRESHOLD (n, FAC_ODD_THRESHOLD))
Packit 5c3484
    {
Packit 5c3484
      mp_limb_t prod, max_prod;
Packit 5c3484
      mp_size_t j;
Packit 5c3484
      mp_ptr    factors;
Packit 5c3484
      TMP_SDECL;
Packit 5c3484
Packit 5c3484
      TMP_SMARK;
Packit 5c3484
      factors = TMP_SALLOC_LIMBS (2 + (n - numberof (table)) / FACTORS_PER_LIMB);
Packit 5c3484
Packit 5c3484
      factors[0] = table[numberof (table)-1];
Packit 5c3484
      j = 1;
Packit 5c3484
      prod = n;
Packit 5c3484
#if TUNE_PROGRAM_BUILD
Packit 5c3484
      max_prod = GMP_NUMB_MAX / FAC_DSC_THRESHOLD_LIMIT;
Packit 5c3484
#else
Packit 5c3484
      max_prod = GMP_NUMB_MAX / (FAC_ODD_THRESHOLD | 1);
Packit 5c3484
#endif
Packit 5c3484
      while (--n >= numberof (table))
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
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      mp_limb_t count;
Packit 5c3484
      mpz_oddfac_1 (x, n, 0);
Packit 5c3484
      if (n <= TABLE_LIMIT_2N_MINUS_POPC_2N)
Packit 5c3484
	count = __gmp_fac2cnt_table[n / 2 - 1];
Packit 5c3484
      else
Packit 5c3484
	{
Packit 5c3484
	  popc_limb (count, n);
Packit 5c3484
	  count = n - count;
Packit 5c3484
	}
Packit 5c3484
      mpz_mul_2exp (x, x, count);
Packit 5c3484
    }
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
#undef FACTORS_PER_LIMB
Packit 5c3484
#undef FACTOR_LIST_STORE