Skip to content

Commit c5b7900

Browse files
niklasfvstinner
authored andcommitted
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().
1 parent 4691a2f commit c5b7900

File tree

4 files changed

+35
-54
lines changed

4 files changed

+35
-54
lines changed

Include/pymath.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,12 @@ PyAPI_FUNC(void) _Py_set_387controlword(unsigned short);
227227
* behavior. */
228228
#define _Py_InIntegralTypeRange(type, v) (_Py_IntegralTypeMin(type) <= v && v <= _Py_IntegralTypeMax(type))
229229

230+
/* Return the smallest integer k such that n < 2**k, or 0 if n == 0.
231+
* Equivalent to floor(log2(x))+1. Also equivalent to: bitwidth_of_type -
232+
* count_leading_zero_bits(x)
233+
*/
234+
#ifndef Py_LIMITED_API
235+
PyAPI_FUNC(unsigned int) _Py_bit_length(unsigned long d);
236+
#endif
237+
230238
#endif /* Py_PYMATH_H */

Modules/mathmodule.c

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,28 +1441,6 @@ math_fsum(PyObject *module, PyObject *seq)
14411441
#undef NUM_PARTIALS
14421442

14431443

1444-
/* Return the smallest integer k such that n < 2**k, or 0 if n == 0.
1445-
* Equivalent to floor(lg(x))+1. Also equivalent to: bitwidth_of_type -
1446-
* count_leading_zero_bits(x)
1447-
*/
1448-
1449-
/* XXX: This routine does more or less the same thing as
1450-
* bits_in_digit() in Objects/longobject.c. Someday it would be nice to
1451-
* consolidate them. On BSD, there's a library function called fls()
1452-
* that we could use, and GCC provides __builtin_clz().
1453-
*/
1454-
1455-
static unsigned long
1456-
bit_length(unsigned long n)
1457-
{
1458-
unsigned long len = 0;
1459-
while (n != 0) {
1460-
++len;
1461-
n >>= 1;
1462-
}
1463-
return len;
1464-
}
1465-
14661444
static unsigned long
14671445
count_set_bits(unsigned long n)
14681446
{
@@ -1877,7 +1855,7 @@ factorial_partial_product(unsigned long start, unsigned long stop,
18771855
/* find midpoint of range(start, stop), rounded up to next odd number. */
18781856
midpoint = (start + num_operands) | 1;
18791857
left = factorial_partial_product(start, midpoint,
1880-
bit_length(midpoint - 2));
1858+
_Py_bit_length(midpoint - 2));
18811859
if (left == NULL)
18821860
goto error;
18831861
right = factorial_partial_product(midpoint, stop, max_bits);
@@ -1907,7 +1885,7 @@ factorial_odd_part(unsigned long n)
19071885
Py_INCREF(outer);
19081886

19091887
upper = 3;
1910-
for (i = bit_length(n) - 2; i >= 0; i--) {
1888+
for (i = _Py_bit_length(n) - 2; i >= 0; i--) {
19111889
v = n >> i;
19121890
if (v <= 2)
19131891
continue;
@@ -1917,7 +1895,7 @@ factorial_odd_part(unsigned long n)
19171895
/* Here inner is the product of all odd integers j in the range (0,
19181896
n/2**(i+1)]. The factorial_partial_product call below gives the
19191897
product of all odd integers j in the range (n/2**(i+1), n/2**i]. */
1920-
partial = factorial_partial_product(lower, upper, bit_length(upper-2));
1898+
partial = factorial_partial_product(lower, upper, _Py_bit_length(upper-2));
19211899
/* inner *= partial */
19221900
if (partial == NULL)
19231901
goto error;

Objects/longobject.c

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -803,26 +803,6 @@ _PyLong_Sign(PyObject *vv)
803803
return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
804804
}
805805

806-
/* bits_in_digit(d) returns the unique integer k such that 2**(k-1) <= d <
807-
2**k if d is nonzero, else 0. */
808-
809-
static const unsigned char BitLengthTable[32] = {
810-
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
811-
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
812-
};
813-
814-
static int
815-
bits_in_digit(digit d)
816-
{
817-
int d_bits = 0;
818-
while (d >= 32) {
819-
d_bits += 6;
820-
d >>= 6;
821-
}
822-
d_bits += (int)BitLengthTable[d];
823-
return d_bits;
824-
}
825-
826806
size_t
827807
_PyLong_NumBits(PyObject *vv)
828808
{
@@ -840,7 +820,7 @@ _PyLong_NumBits(PyObject *vv)
840820
if ((size_t)(ndigits - 1) > SIZE_MAX / (size_t)PyLong_SHIFT)
841821
goto Overflow;
842822
result = (size_t)(ndigits - 1) * (size_t)PyLong_SHIFT;
843-
msd_bits = bits_in_digit(msd);
823+
msd_bits = _Py_bit_length(msd);
844824
if (SIZE_MAX - msd_bits < result)
845825
goto Overflow;
846826
result += msd_bits;
@@ -1950,7 +1930,7 @@ long_format_binary(PyObject *aa, int base, int alternate,
19501930
return -1;
19511931
}
19521932
size_a_in_bits = (size_a - 1) * PyLong_SHIFT +
1953-
bits_in_digit(a->ob_digit[size_a - 1]);
1933+
_Py_bit_length(a->ob_digit[size_a - 1]);
19541934
/* Allow 1 character for a '-' sign. */
19551935
sz = negative + (size_a_in_bits + (bits - 1)) / bits;
19561936
}
@@ -2770,7 +2750,7 @@ x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
27702750

27712751
/* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2.
27722752
shift v1 left by the same amount. Results go into w and v. */
2773-
d = PyLong_SHIFT - bits_in_digit(w1->ob_digit[size_w-1]);
2753+
d = PyLong_SHIFT - _Py_bit_length(w1->ob_digit[size_w-1]);
27742754
carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d);
27752755
assert(carry == 0);
27762756
carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d);
@@ -2891,7 +2871,7 @@ _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e)
28912871
*e = 0;
28922872
return 0.0;
28932873
}
2894-
a_bits = bits_in_digit(a->ob_digit[a_size-1]);
2874+
a_bits = _Py_bit_length(a->ob_digit[a_size-1]);
28952875
/* The following is an overflow-free version of the check
28962876
"if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */
28972877
if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 &&
@@ -3986,8 +3966,8 @@ long_true_divide(PyObject *v, PyObject *w)
39863966
/* Extreme underflow */
39873967
goto underflow_or_zero;
39883968
/* Next line is now safe from overflowing a Py_ssize_t */
3989-
diff = diff * PyLong_SHIFT + bits_in_digit(a->ob_digit[a_size - 1]) -
3990-
bits_in_digit(b->ob_digit[b_size - 1]);
3969+
diff = diff * PyLong_SHIFT + _Py_bit_length(a->ob_digit[a_size - 1]) -
3970+
_Py_bit_length(b->ob_digit[b_size - 1]);
39913971
/* Now diff = a_bits - b_bits. */
39923972
if (diff > DBL_MAX_EXP)
39933973
goto overflow;
@@ -4063,7 +4043,7 @@ long_true_divide(PyObject *v, PyObject *w)
40634043
}
40644044
x_size = Py_ABS(Py_SIZE(x));
40654045
assert(x_size > 0); /* result of division is never zero */
4066-
x_bits = (x_size-1)*PyLong_SHIFT+bits_in_digit(x->ob_digit[x_size-1]);
4046+
x_bits = (x_size-1)*PyLong_SHIFT+_Py_bit_length(x->ob_digit[x_size-1]);
40674047

40684048
/* The number of extra bits that have to be rounded away. */
40694049
extra_bits = Py_MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG;
@@ -4877,7 +4857,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
48774857
alloc_b = Py_SIZE(b);
48784858
/* reduce until a fits into 2 digits */
48794859
while ((size_a = Py_SIZE(a)) > 2) {
4880-
nbits = bits_in_digit(a->ob_digit[size_a-1]);
4860+
nbits = _Py_bit_length(a->ob_digit[size_a-1]);
48814861
/* extract top 2*PyLong_SHIFT bits of a into x, along with
48824862
corresponding bits of b into y */
48834863
size_b = Py_SIZE(b);
@@ -5395,7 +5375,7 @@ int_bit_length_impl(PyObject *self)
53955375
return PyLong_FromLong(0);
53965376

53975377
msd = ((PyLongObject *)self)->ob_digit[ndigits-1];
5398-
msd_bits = bits_in_digit(msd);
5378+
msd_bits = _Py_bit_length(msd);
53995379

54005380
if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT)
54015381
return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits);

Python/pymath.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,18 @@ round(double x)
7979
return copysign(y, x);
8080
}
8181
#endif /* HAVE_ROUND */
82+
83+
static const unsigned int BitLengthTable[32] = {
84+
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
85+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
86+
};
87+
88+
unsigned int _Py_bit_length(unsigned long d) {
89+
unsigned int d_bits = 0;
90+
while (d >= 32) {
91+
d_bits += 6;
92+
d >>= 6;
93+
}
94+
d_bits += BitLengthTable[d];
95+
return d_bits;
96+
}

0 commit comments

Comments
 (0)