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:
Niklas Fiekas 2020-01-16 15:09:19 +01:00 committed by Victor Stinner
parent 4691a2f2a2
commit c5b79003f5
4 changed files with 35 additions and 54 deletions

View file

@ -79,3 +79,18 @@ round(double x)
return copysign(y, x);
}
#endif /* HAVE_ROUND */
static const unsigned int BitLengthTable[32] = {
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
};
unsigned int _Py_bit_length(unsigned long d) {
unsigned int d_bits = 0;
while (d >= 32) {
d_bits += 6;
d >>= 6;
}
d_bits += BitLengthTable[d];
return d_bits;
}