-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[mypyc] feat: new primitive for int.bit_length
#19673
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
base: master
Are you sure you want to change the base?
Conversation
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
|
||
// int.bit_length() | ||
CPyTagged CPyTagged_BitLength(CPyTagged self) { | ||
PyObject *pyint = CPyTagged_StealAsObject(self); |
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 this PyLong_Check?
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.
No, it's always an instance of int
.
for more information, see https://pre-commit.ci
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.
Thanks for the PR! bit_length
can be useful in low-level code, where a generic method call can really slow things down.
|
||
// int.bit_length() | ||
CPyTagged CPyTagged_BitLength(CPyTagged self) { | ||
PyObject *pyint = CPyTagged_StealAsObject(self); |
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.
No, it's always an instance of int
.
#if defined(__GNUC__) || defined(__clang__) | ||
bits = (int)(CPY_BITS - CPY_CLZ(absval)); | ||
#else | ||
// Fallback to loop if no builtin |
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.
What about using _BitScanReverse[64]
on MSVC (https://learn.microsoft.com/en-us/cpp/intrinsics/bitscanreverse-bitscanreverse64?view=msvc-170)?
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.
Interesting. No, I did not consider this but I didn't realize it was an option. Will need to look thru the docs and figure out the best way to integrate.
mypyc/test-data/run-integers.test
Outdated
def bit_length(n: int) -> int: | ||
return n.bit_length() | ||
def test_bit_length() -> None: | ||
assert bit_length(0) == 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.
Minor: This would be a bit more robust if you'd have say bit_length_python
function that does return getattr(n, "bit_length")()
and you can assert that bit_length(n) == bit_length_python(n)
.
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.
Well that's odd, I implemented the tests as you asked here but now we get segfaults on Python3.14
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.
Also on the 32-bit test runner
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.
okay... well after implementing your bitscan idea the issue has resolved itself on python3.14... somehow
This PR adds a new primitive for
int.bit_length
and is already ready for review.