-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-29782: Use __builtin_clzl for bits_in_digit if available #594
Conversation
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately our records indicate you have not signed the CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow these steps to rectify the issue:
Thanks again to your contribution and we look forward to looking at it! |
@niklasf, thanks for your PR! By analyzing the history of the files in this pull request, we identified @mdickinson, @serhiy-storchaka, @tim-one, @brettcannon and @benjaminp to be potential reviewers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments left on the bpo issue. In brief:
- configuration decisions (deciding whether __builtin_clz is available and does the right thing) should be moved into the configuration machinery.
- the code currently assumes that
digit
is the same asunsigned long
. That's not always the case. - once the above issues are addressed, we still need to weigh any speed advantage against the costs in terms of maintainability, portability, readability.
Ah, no. I see. This should work whenever |
21c4a4b
to
bff329a
Compare
|
Python/pyintrinsics.c
Outdated
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 | ||
}; | ||
|
||
int bit_length(unsigned long d) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename the function to _Py_bit_length().
Why the result is signed? Why not unsigned int?
Include/pyintrinsics.h
Outdated
* count_leading_zero_bits(x) | ||
*/ | ||
|
||
#if defined(__GNUC__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the builtin available in old GCC versions? Any idea when the function was added?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gcc 3.4. I made the guard more precise. It also works for clang.
Btw. while looking how this is usually done in Python I found a typo that would affect gcc 3.x: https://github.com/python/cpython/blob/master/Include/pymacro.h#L56 should say __GNU*C*_MINOR__
. I'll make a seperate PR. #878.
Include/pyintrinsics.h
Outdated
#if defined(__GNUC__) | ||
#define HAVE_BIT_LENGTH | ||
static inline int bit_length(unsigned long d) { | ||
return d ? (8 * sizeof(unsigned long) - __builtin_clzl(d)) : 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be possible to avoid the conditional code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately I don't think so. __builtin_clzl(0)
is undefined or rather has different behavior on various processors.
Include/pyintrinsics.h
Outdated
} | ||
#elif defined(_MSC_VER) | ||
#define HAVE_BIT_LENGTH | ||
#include <intrin.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the following pragma?
#pragma intrinsic(_BitScanReverse)
@Haypo Thanks for the review. I updated the PR based on your comments. |
http://bugs.python.org/issue29782 has been closed as REJECTED. |
No description provided.