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 /Python/pymath.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 'Python/pymath.c')
-rw-r--r-- | Python/pymath.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Python/pymath.c b/Python/pymath.c index 24b8042..a08a0e7 100644 --- a/Python/pymath.c +++ b/Python/pymath.c @@ -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; +} |