mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00: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
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue