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

MAINT: Prevent OverflowError in multiplication #230

Merged
merged 2 commits into from
Oct 31, 2024

Conversation

magruber
Copy link
Contributor

In newer numpy versions the multiplication of ndarrays containing uint8 results in uint8. Here this leads to an OverflowError. Therefore changing to an uint64 before the multiplication prevents this.

In newer numpy versions the multiplication of ndarrays containing uint8 results in uint8. Here this leads to an OverflowError. Therefore changing to an uint64 before the multiplication prevents this.
@cbespin cbespin self-requested a review October 10, 2024 15:05
Copy link
Contributor

@cbespin cbespin left a comment

Choose a reason for hiding this comment

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

Nice catch! Only a short comment, maybe personal style and happy to discuss if you think differently

@@ -41,7 +41,7 @@ def bitarray_to_byte_array(bitarr):
ba = bitarray(bitarr, endian=bitarr.endian())
ba.reverse() # this flip the byte order and the bit order of each byte
bs = np.frombuffer(ba.tobytes(), dtype=np.uint8) # byte padding happens here, bitarray.tobytes()
bs = (bs * 0x0202020202 & 0x010884422010) % 1023
bs = (bs.astype(np.uint64) * 0x0202020202 & 0x010884422010) % 1023
Copy link
Contributor

Choose a reason for hiding this comment

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

I would argue it is a bit easier to follow if you do bs * np.uint64(0x0202020202) to make the casting more explicit and keep bs an array of uint8 aka bytes. Performance seems to be about the same with a very simple test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I agree.

MarcoVogt added a commit that referenced this pull request Oct 27, 2024
@cbespin
Copy link
Contributor

cbespin commented Oct 28, 2024

@magruber Could you cherry-pick this commit (66fcf32) from #231 into your fork and push it to update this PR? Then I will merge it and it should not cause too much trouble with 231

@cbespin cbespin merged commit b64e5c9 into SiLab-Bonn:master Oct 31, 2024
5 checks passed
@cbespin cbespin mentioned this pull request Oct 31, 2024
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