2008-06-25 03:33:36 -04:00
|
|
|
/* mpz_bin_ui - compute n over k.
|
|
|
|
|
2014-02-28 11:44:53 -05:00
|
|
|
Copyright 1998, 1999, 2000, 2001, 2002, 2012 Free Software Foundation, Inc.
|
2008-06-25 03:33:36 -04:00
|
|
|
|
|
|
|
This file is part of the GNU MP Library.
|
|
|
|
|
|
|
|
The GNU MP Library is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
2014-02-28 11:44:53 -05:00
|
|
|
the Free Software Foundation; either version 3 of the License, or (at your
|
2008-06-25 03:33:36 -04:00
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
The GNU MP Library is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2014-02-28 11:44:53 -05:00
|
|
|
along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
|
2008-06-25 03:33:36 -04:00
|
|
|
|
2009-02-12 05:24:24 -05:00
|
|
|
#include "mpir.h"
|
2008-06-25 03:33:36 -04:00
|
|
|
#include "gmp-impl.h"
|
|
|
|
#include "longlong.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* This is a poor implementation. Look at bin_uiui.c for improvement ideas.
|
|
|
|
In fact consider calling mpz_bin_uiui() when the arguments fit, leaving
|
|
|
|
the code here only for big n.
|
|
|
|
|
|
|
|
The identity bin(n,k) = (-1)^k * bin(-n+k-1,k) can be found in Knuth vol
|
|
|
|
1 section 1.2.6 part G. */
|
|
|
|
|
|
|
|
|
|
|
|
#define DIVIDE() \
|
|
|
|
do { \
|
|
|
|
ASSERT (SIZ(r) > 0); \
|
|
|
|
MPN_DIVREM_OR_DIVEXACT_1 (PTR(r), PTR(r), (mp_size_t) SIZ(r), kacc); \
|
|
|
|
SIZ(r) -= (PTR(r)[SIZ(r)-1] == 0); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
void
|
2014-02-28 14:33:27 -05:00
|
|
|
mpz_bin_ui (mpz_ptr r, mpz_srcptr n, mpir_ui k)
|
2008-06-25 03:33:36 -04:00
|
|
|
{
|
|
|
|
mpz_t ni;
|
|
|
|
mp_limb_t i;
|
|
|
|
mpz_t nacc;
|
|
|
|
mp_limb_t kacc;
|
|
|
|
mp_size_t negate;
|
|
|
|
|
2014-02-28 11:44:53 -05:00
|
|
|
if (SIZ (n) < 0)
|
2008-06-25 03:33:36 -04:00
|
|
|
{
|
|
|
|
/* bin(n,k) = (-1)^k * bin(-n+k-1,k), and set ni = -n+k-1 - k = -n-1 */
|
|
|
|
mpz_init (ni);
|
|
|
|
mpz_neg (ni, n);
|
|
|
|
mpz_sub_ui (ni, ni, 1L);
|
|
|
|
negate = (k & 1); /* (-1)^k */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* bin(n,k) == 0 if k>n
|
2014-02-28 11:44:53 -05:00
|
|
|
(no test for this under the n<0 case, since -n+k-1 >= k there) */
|
2008-06-25 03:33:36 -04:00
|
|
|
if (mpz_cmp_ui (n, k) < 0)
|
2014-02-28 11:44:53 -05:00
|
|
|
{
|
|
|
|
SIZ (r) = 0;
|
|
|
|
return;
|
|
|
|
}
|
2008-06-25 03:33:36 -04:00
|
|
|
|
|
|
|
/* set ni = n-k */
|
|
|
|
mpz_init (ni);
|
|
|
|
mpz_sub_ui (ni, n, k);
|
|
|
|
negate = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now wanting bin(ni+k,k), with ni positive, and "negate" is the sign (0
|
|
|
|
for positive, 1 for negative). */
|
2014-02-28 11:44:53 -05:00
|
|
|
SIZ (r) = 1; PTR (r)[0] = 1;
|
2008-06-25 03:33:36 -04:00
|
|
|
|
|
|
|
/* Rewrite bin(n,k) as bin(n,n-k) if that is smaller. In this case it's
|
|
|
|
whether ni+k-k < k meaning ni<k, and if so change to denominator ni+k-k
|
|
|
|
= ni, and new ni of ni+k-ni = k. */
|
|
|
|
if (mpz_cmp_ui (ni, k) < 0)
|
|
|
|
{
|
2014-02-28 11:44:53 -05:00
|
|
|
unsigned long tmp;
|
2008-06-25 03:33:36 -04:00
|
|
|
tmp = k;
|
|
|
|
k = mpz_get_ui (ni);
|
|
|
|
mpz_set_ui (ni, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
kacc = 1;
|
|
|
|
mpz_init_set_ui (nacc, 1L);
|
|
|
|
|
|
|
|
for (i = 1; i <= k; i++)
|
|
|
|
{
|
|
|
|
mp_limb_t k1, k0;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
mp_limb_t nacclow;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
nacclow = PTR(nacc)[0];
|
|
|
|
for (c = 0; (((kacc | nacclow) & 1) == 0); c++)
|
|
|
|
{
|
|
|
|
kacc >>= 1;
|
|
|
|
nacclow >>= 1;
|
|
|
|
}
|
|
|
|
mpz_div_2exp (nacc, nacc, c);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mpz_add_ui (ni, ni, 1L);
|
|
|
|
mpz_mul (nacc, nacc, ni);
|
|
|
|
umul_ppmm (k1, k0, kacc, i << GMP_NAIL_BITS);
|
|
|
|
if (k1 != 0)
|
|
|
|
{
|
|
|
|
/* Accumulator overflow. Perform bignum step. */
|
|
|
|
mpz_mul (r, r, nacc);
|
2014-02-28 11:44:53 -05:00
|
|
|
SIZ (nacc) = 1; PTR (nacc)[0] = 1;
|
|
|
|
DIVIDE ();
|
2008-06-25 03:33:36 -04:00
|
|
|
kacc = i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Save new products in accumulators to keep accumulating. */
|
2014-02-28 11:44:53 -05:00
|
|
|
kacc = k0 >> GMP_NAIL_BITS;
|
2008-06-25 03:33:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mpz_mul (r, r, nacc);
|
|
|
|
DIVIDE ();
|
|
|
|
SIZ(r) = (SIZ(r) ^ -negate) + negate;
|
|
|
|
|
|
|
|
mpz_clear (nacc);
|
|
|
|
mpz_clear (ni);
|
|
|
|
}
|