classification
Title: Unify duplicate bits_in_digit and bit_length
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: louielu, mark.dickinson, niklasf, vstinner
Priority: normal Keywords:

Created on 2017-07-25 12:48 by niklasf, last changed 2020-01-16 14:14 by vstinner. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 2866 merged niklasf, 2017-07-25 12:50
Messages (4)
msg299069 - (view) Author: Niklas Fiekas (niklasf) * Date: 2017-07-25 12:48
My previous patch to optimize bits_in_digit was
rejected: http://bugs.python.org/issue29782

This leaves this issue open (mathmodule.c):

/* 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().
 */

We could still deal with the code duplication without the
complexity of the optimizations in the previous patch.
msg360122 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-16 14:09
New changeset c5b79003f5fe6aa28a2a028680367839ba8677db by Victor Stinner (Niklas Fiekas) in branch 'master':
bpo-31031: Unify duplicate bits_in_digit and bit_length (GH-2866)
https://github.com/python/cpython/commit/c5b79003f5fe6aa28a2a028680367839ba8677db
msg360123 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-16 14:13
"On BSD, there's a library function called fls() that we could use, and GCC provides __builtin_clz()."

See also rejected bpo-29782 and GH-594.

If someone wants to experiment that, I suggest to add Include/internal/pycore_pymath.h, move _Py_bit_length() there and convert it to a static inline function.

Since the code is now unified, it makes a little bit more sense to add specialized code, *if* the code is not too complex.

I'm still not sure that GH-594 is worth it, since it was not possible to see the speedup on a microbenchmark :-(
msg360124 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-16 14:14
Thanks Niklas Fiekas for merging duplicated code ;-) It should make factorial_partial_product() a little bit more efficient!
History
Date User Action Args
2020-01-16 14:14:39vstinnersetstatus: open -> closed
resolution: fixed
stage: resolved
2020-01-16 14:14:30vstinnersetmessages: + msg360124
2020-01-16 14:13:46vstinnersetmessages: + msg360123
2020-01-16 14:09:24vstinnersetmessages: + msg360122
2017-07-25 12:54:59vstinnersetnosy: + vstinner
2017-07-25 12:50:43niklasfsetpull_requests: + pull_request2916
2017-07-25 12:48:04niklasfcreate