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

Small Bugfix: Remove possibility of bitshifting negative integers #1111

Merged
merged 6 commits into from
Jun 18, 2024

Conversation

lroberts36
Copy link
Collaborator

PR Summary

This PR fixes the bitshift related undefined behavior described in #1107 by multiplying by a power of two explicitly rather than performing the same operation (on positive numbers) via a bitshift.

PR Checklist

  • Code passes cpplint
  • New features are documented.
  • Adds a test for any bugs fixed. Adds tests for new features.
  • Code is formatted
  • Changes are summarized in CHANGELOG.md
  • Change is breaking (API, behavior, ...)
    • Change is additionally added to CHANGELOG.md in the breaking section
    • PR is marked as breaking
    • Short summary API changes at the top of the PR (plus optionally with an automated update/fix script)
  • CI has been triggered on Darwin for performance regression tests.
  • Docs build
  • (@lanl.gov employees) Update copyright on changed files

@lroberts36 lroberts36 changed the title Bugfix: Remove possibilty of bitshifting negative integers Bugfix: Remove possibility of bitshifting negative integers Jun 13, 2024
@lroberts36 lroberts36 changed the title Bugfix: Remove possibility of bitshifting negative integers Small Bugfix: Remove possibility of bitshifting negative integers Jun 13, 2024
@lroberts36 lroberts36 added the bug Something isn't working label Jun 13, 2024
Copy link
Collaborator

@pgrete pgrete left a comment

Choose a reason for hiding this comment

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

Why didn't case this bigger issues? Was this an edge case or working by chance?

Comment on lines -121 to 125
auto low = (l(dir) << level_shift_this) - 1;
auto low = l(dir) * block_size_this - 1;
auto hi = low + block_size_this + 1;

auto low_in = (in.l(dir) << level_shift_in);
auto low_in = in.l(dir) * block_size_in;
auto hi_in = low_in + block_size_in - 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Out of curiosity, where does the asymmetry come from (i.e., that low/hi is offset by-1/+1 and low_in, hi_in by 0,-1)?
Would be great to also add a short comment in the code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It just has to do with how the overlap is checked, i.e. the bounds of this are increased by one in the positive and negative direction to create a one layer thick ghost region.

@pgrete
Copy link
Collaborator

pgrete commented Jun 14, 2024

btw you got the Schnapszahl ;)

@lroberts36
Copy link
Collaborator Author

lroberts36 commented Jun 14, 2024

@pgrete: I am not sure why this worked previously, but I did not look carefully at what happened with the negative but shift. I think this should have been hit reasonably often.

Haha, I had to look up schnapszahl...

Copy link
Collaborator

@BenWibking BenWibking left a comment

Choose a reason for hiding this comment

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

The new code looks fine to me, but I am likewise confused as to how the cases where it was bitshifting -1 worked...

@pgrete
Copy link
Collaborator

pgrete commented Jun 17, 2024

The test now reliably failed twice. Does anyone see how this changeset could result in that error? I don't.

@BenWibking
Copy link
Collaborator

BenWibking commented Jun 17, 2024

The test now reliably failed twice. Does anyone see how this changeset could result in that error? I don't.

It's a NumPy 2.0 API breakage 😞...

AttributeError: module 'numpy' has no attribute 'product'

@BenWibking
Copy link
Collaborator

@lroberts36
Copy link
Collaborator Author

@pgrete and @BenWibking: Apparently this worked because (at least clang and gcc) bitshift negative numbers in the expected way, i.e neg_num << n == neg_num * std::pow(2, n).

@BenWibking
Copy link
Collaborator

CI fix here: #1116

@pgrete
Copy link
Collaborator

pgrete commented Jun 18, 2024

@pgrete and @BenWibking: Apparently this worked because (at least clang and gcc) bitshift negative numbers in the expected way, i.e neg_num << n == neg_num * std::pow(2, n).

great, thanks for confirming!

@pgrete pgrete disabled auto-merge June 18, 2024 10:35
@pgrete pgrete enabled auto-merge (squash) June 18, 2024 10:35
@pgrete pgrete merged commit da22529 into develop Jun 18, 2024
50 checks passed
@BenWibking BenWibking deleted the lroberts36/fix-bitshift-of-negative branch July 11, 2024 02:24
@BenWibking BenWibking restored the lroberts36/fix-bitshift-of-negative branch July 11, 2024 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants