Skip to content

[mypyc] Replace integer floor division by a power of two with a shift #12870

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

Merged
merged 2 commits into from
May 27, 2022

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented May 25, 2022

In a microbenchmark right shift was a bit faster.

Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A classic

if not isinstance(expr.right, IntExpr):
return expr
divisor = expr.right.value
n = 2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't super important, but I think it would be cleaner (and faster, though that doesn't really matter) to directly compute the shift instead of testing for it? I think you can do shift = divisor.bit_length() - 1 and then check that the shift is correct

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. We don't have a primitive for bit_length() so it may actually be slower, but I think that the code looks cleaner if we use it.

@JukkaL JukkaL merged commit d3ef642 into master May 27, 2022
@JukkaL JukkaL deleted the div-pow2 branch May 27, 2022 09:12
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