Skip to content

[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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

BobTheBuidler
Copy link
Contributor

@BobTheBuidler BobTheBuidler commented Aug 16, 2025

This PR adds a new primitive for int.bit_length and is already ready for review.

Copy link
Collaborator

@JukkaL JukkaL left a 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.

def bit_length(n: int) -> int:
return n.bit_length()
def test_bit_length() -> None:
assert bit_length(0) == 0
Copy link
Collaborator

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).

Copy link
Contributor Author

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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

@BobTheBuidler BobTheBuidler requested a review from JukkaL August 20, 2025 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants