Contributed to the GNU project by Torbjorn Granlund and Marco Bodrato.
Copyright 2010-2012, 2015-2018, 2020 Free Software Foundation, Inc.
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 either:
* the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
or
* the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
or both in parallel, as here.
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 General Public License for more details.
You should have received copies of the GNU General Public License and the GNU Lesser General Public License along with the GNU MP Library. If not,
see https://www.gnu.org/licenses/. */
Accumulate chunks of factors first limb-by-limb (using one of mul0-mul8) which are then accumulated into mpn numbers. The first inner loop accumulates divisor factors, the 2nd inner loop accumulates exactly the same number of dividend factors. We avoid accumulating more for the divisor, even with its smaller factors, since we else cannot guarantee divisibility.
Since we know each division will yield an integer, we compute the quotient using Hensel norm: If the quotient is limited by 2^t, we compute A / B mod 2^t.
Improvements:
(1) An obvious improvement to this code would be to compute mod 2^t everywhere. Unfortunately, we cannot determine t beforehand, unless we invoke some approximation, such as Stirling's formula. Of course, we don't need t to be tight. However, it is not clear that this would help much, our numbers are kept reasonably small already.
(2) Compute nmax/kmax semi-accurately, without scalar division or a loop. Extracting the 3 msb, then doing a table lookup using cnt*8+msb as index, would make it both reasonably accurate and fast. (We could use a table stored into a limb, perhaps.) The table should take the removed factors of 2 into account (those done on-the-fly in mulN).
(3) The first time in the loop we compute the odd part of a factorial in kp, we might use oddfac_1 for this task.
*/
/* This threshold determines how large divisor to accumulate before we call bdiv. Perhaps we should never call bdiv, and accumulate all we are told, since we are just basecase code anyway? Presumably, this depends on the
relative speed of the asymptotically fast code and this code. */ #define SOME_THRESHOLD 20
/* Multiply-into-limb functions. These remove factors of 2 on-the-fly. FIXME: All versions of MAXFACS don't take this 2 removal into account now, meaning that then, shifting just adds some overhead. (We remove factors from the
completed limb anyway.) */
static mp_limb_t
mul1 (mp_limb_t m)
{ return m;
}
static mp_limb_t
mul2 (mp_limb_t m)
{ /* We need to shift before multiplying, to avoid an overflow. */
mp_limb_t m01 = (m | 1) * ((m + 1) >> 1); return m01;
}
/* Number of factors-of-2 removed by the corresponding mulN function. */ staticconstunsignedchar tcnttab[] = {0, 1, 1, 2, 2, 4, 4, 6 /*,6,8*/};
#if 1 /* This variant is inaccurate but share the code with other functions. */ #define MAXFACS(max,l) \ do { \
(max) = log_n_max (l); \
} while (0) #else
/* This variant is exact(?) but uses a loop. It takes the 2 removal
of mulN into account. */ staticconstunsignedlong ftab[] = #if GMP_NUMB_BITS == 64 /* 1 to 8 factors per iteration */
{CNST_LIMB(0xffffffffffffffff),CNST_LIMB(0x16a09e667),0x32cbfc,0x16a08,0x24c0,0xa11,0x345,0x1ab /*,0xe9,0x8e */}; #endif #if GMP_NUMB_BITS == 32 /* 1 to 7 factors per iteration */
{0xffffffff,0x16a09,0x7ff,0x168,0x6f,0x3d,0x20 /* ,0x17 */}; #endif
#define MAXFACS(max,l) \ do { \ int __i; \ for (__i = numberof (ftab) - 1; l > ftab[__i]; __i--) \
; \
(max) = __i + 1; \
} while (0) #endif
/* Entry i contains (i!/2^t)^(-1) where t is chosen such that the parenthesis
is an odd integer. */ staticconst mp_limb_t facinv[] = { ONE_LIMB_ODD_FACTORIAL_INVERSES_TABLE };
staticvoid
mpz_bdiv_bin_uiui (mpz_ptr r, unsignedlongint n, unsignedlongint k)
{ unsigned nmax, kmax, nmaxnow, numfac;
mp_ptr np, kp;
mp_size_t nn, kn, alloc;
mp_limb_t i, j, t, iii, jjj, cy, dinv; int cnt;
mp_size_t maxn;
TMP_DECL;
ASSERT (k > ODD_FACTORIAL_TABLE_LIMIT);
TMP_MARK;
maxn = 1 + n / GMP_NUMB_BITS; /* absolutely largest result size (limbs) */
/* FIXME: This allocation might be insufficient, but is usually way too
large. */
alloc = SOME_THRESHOLD - 1 + MAX (3 * maxn / 2, SOME_THRESHOLD);
alloc = MIN (alloc, (mp_size_t) k) + 1;
TMP_ALLOC_LIMBS_2 (np, alloc, kp, SOME_THRESHOLD + 1);
/* Put back the right number of factors of 2. */
popc_limb (cnt, n - k);
popc_limb (j, k);
cnt += j;
popc_limb (j, n);
cnt -= j; if (cnt != 0)
{
ASSERT (cnt < GMP_NUMB_BITS); /* can happen, but not for intended use */
cy = mpn_lshift (np, np, nn, cnt);
np[nn] = cy;
nn += cy != 0;
}
Recursively exploit the relation bin(n,k) = bin(n,k>>1)*bin(n-k>>1,k-k>>1)/bin(k,k>>1) .
Values for binomial(k,k>>1) that fit in a limb are precomputed (with inverses).
*/
/* bin2kk[i - ODD_CENTRAL_BINOMIAL_OFFSET] =
binomial(i*2,i)/2^t (where t is chosen so that it is odd). */ staticconst mp_limb_t bin2kk[] = { ONE_LIMB_ODD_CENTRAL_BINOMIAL_TABLE };
/* bin2kkinv[i] = bin2kk[i]^-1 mod B */ staticconst mp_limb_t bin2kkinv[] = { ONE_LIMB_ODD_CENTRAL_BINOMIAL_INVERSE_TABLE };
/* bin2kk[i] = binomial((i+MIN_S)*2,i+MIN_S)/2^t. This table contains the t values. */ staticconstunsignedchar fac2bin[] = { CENTRAL_BINOMIAL_2FAC_TABLE };
/* mpz_goetgheluck_bin_uiui(RESULT, N, K) -- Set RESULT to binomial(N,K). * * Contributed to the GNU project by Marco Bodrato. * * Implementation of the algorithm by P. Goetgheluck, "Computing * Binomial Coefficients", The American Mathematical Monthly, Vol. 94, * No. 4 (April 1987), pp. 360-365. * * Acknowledgment: Peter Luschny did spot the slowness of the previous * code and suggested the reference.
*/
/* Returns an approximation of the sqare root of x. * It gives: * limb_apprsqrt (x) ^ 2 <= x < (limb_apprsqrt (x)+1) ^ 2 * or * x <= limb_apprsqrt (x) ^ 2 <= x * 9/8
*/ static mp_limb_t
limb_apprsqrt (mp_limb_t x)
{ int s;
/*********************************************************/ /* End of implementation of Goetgheluck's algorithm */ /*********************************************************/
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.