mirror of
https://github.com/python/cpython
synced 2026-09-29 12:10:30 +03:00
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().
This commit is contained in:
parent
4691a2f2a2
commit
c5b79003f5
4 changed files with 35 additions and 54 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue