Skip to content
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

improve undefined behavior case in bzhi emulation #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Validark
Copy link

Doing ~(popcnt >> 6) gives us -2 when popcnt is 64, otherwise it gives us -1. ANDing this value with 1 << popcnt is fine when 1 << 64 gives us 1, which it may on a lot of machines, but C does not guarantee that to be the case. Instead, we could do (popcnt >> 6) - 1 which gives us 0 when the popcnt is 64, and (0 & undefined) is (hopefully?) 0. When the popcnt is not 64, we get -1 from the revised expression, and ANDing with that works properly too.

Please test before merging!

Doing `~(popcnt >> 6)` gives us -2 when popcnt is 64, otherwise it gives us -1. ANDing this value with `1 << popcnt` is fine when `1 << 64` gives us `1`, which it may on a lot of machines, but C does not guarantee that to be the case. Instead, we could do `(popcnt >> 6) - 1` which gives us 0 when the popcnt is 64, and `(0 & undefined)` is 0. When the popcnt is not 64, we get `-1` from the revised expression, and ANDing with that works properly too.
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.

1 participant