diff options
author | Niklas Fiekas <niklas.fiekas@backscattering.de> | 2020-01-16 14:09:19 (GMT) |
---|---|---|
committer | Victor Stinner <vstinner@python.org> | 2020-01-16 14:09:19 (GMT) |
commit | c5b79003f5fe6aa28a2a028680367839ba8677db (patch) | |
tree | ef8be6cb5538ff0bf96478f6feed362a71d95dc2 /Modules/mathmodule.c | |
parent | 4691a2f2a2b8174a6c958ce6976ed5f3354c9504 (diff) | |
download | cpython-c5b79003f5fe6aa28a2a028680367839ba8677db.zip cpython-c5b79003f5fe6aa28a2a028680367839ba8677db.tar.gz cpython-c5b79003f5fe6aa28a2a028680367839ba8677db.tar.bz2 |
bpo-31031: Unify duplicate bits_in_digit and bit_length (GH-2866)
Add _Py_bit_length() to unify duplicate bits_in_digit() and bit_length().
Diffstat (limited to 'Modules/mathmodule.c')
-rw-r--r-- | Modules/mathmodule.c | 28 |
1 files changed, 3 insertions, 25 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 5e8e485..81d8717 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1441,28 +1441,6 @@ math_fsum(PyObject *module, PyObject *seq) #undef NUM_PARTIALS -/* Return the smallest integer k such that n < 2**k, or 0 if n == 0. - * Equivalent to floor(lg(x))+1. Also equivalent to: bitwidth_of_type - - * count_leading_zero_bits(x) - */ - -/* XXX: This routine does more or less the same thing as - * bits_in_digit() in Objects/longobject.c. Someday it would be nice to - * consolidate them. On BSD, there's a library function called fls() - * that we could use, and GCC provides __builtin_clz(). - */ - -static unsigned long -bit_length(unsigned long n) -{ - unsigned long len = 0; - while (n != 0) { - ++len; - n >>= 1; - } - return len; -} - static unsigned long count_set_bits(unsigned long n) { @@ -1877,7 +1855,7 @@ factorial_partial_product(unsigned long start, unsigned long stop, /* find midpoint of range(start, stop), rounded up to next odd number. */ midpoint = (start + num_operands) | 1; left = factorial_partial_product(start, midpoint, - bit_length(midpoint - 2)); + _Py_bit_length(midpoint - 2)); if (left == NULL) goto error; right = factorial_partial_product(midpoint, stop, max_bits); @@ -1907,7 +1885,7 @@ factorial_odd_part(unsigned long n) Py_INCREF(outer); upper = 3; - for (i = bit_length(n) - 2; i >= 0; i--) { + for (i = _Py_bit_length(n) - 2; i >= 0; i--) { v = n >> i; if (v <= 2) continue; @@ -1917,7 +1895,7 @@ factorial_odd_part(unsigned long n) /* Here inner is the product of all odd integers j in the range (0, n/2**(i+1)]. The factorial_partial_product call below gives the product of all odd integers j in the range (n/2**(i+1), n/2**i]. */ - partial = factorial_partial_product(lower, upper, bit_length(upper-2)); + partial = factorial_partial_product(lower, upper, _Py_bit_length(upper-2)); /* inner *= partial */ if (partial == NULL) goto error; |