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

std: use math overflow helpers instead of builtins #18165

Closed
wants to merge 1 commit into from

Conversation

sno2
Copy link
Contributor

@sno2 sno2 commented Nov 30, 2023

This patch replaces all usages of the @___WithOverflow builtins where the overflow result is simply read for erroring with the nicer math helpers that return error.Overflow. This cleaned up a bit of code and hopefully impresses the useful math functions on more Zig code later.

Comment on lines +19 to +20
const mask = try math.shlExact(U, byte & 0x7f, group * 7);
value |= mask;
Copy link
Member

Choose a reason for hiding this comment

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

My understanding is that Andrew prefers to use builtins when available. These should instead be changed to use the destructuring syntax #498:

Suggested change
const mask = try math.shlExact(U, byte & 0x7f, group * 7);
value |= mask;
const mask, const overflow = @shlWithOverflow(@as(U, byte & 0x7f), group * 7);
if (overflow != 0) return error.Overflow;
value |= mask;

Copy link
Contributor Author

@sno2 sno2 Dec 2, 2023

Choose a reason for hiding this comment

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

Hmm, I am confused. I feel like that would make sense if you were using overflow for bit stuff. However, we seem to be just checking if there wasn't an overflow. Therefore, does it not make much more sense to use the "zig" approach with try?

As a side note, this also removes the need for prerequisite knowledge for how overflow bits behave (even if they seem simple to some people).

@Vexu Vexu requested a review from andrewrk December 2, 2023 07:31
@andrewrk andrewrk force-pushed the feat-remove-raw-overflows branch from d0c9fd0 to 3481c47 Compare March 14, 2024 10:21
@sno2 sno2 force-pushed the feat-remove-raw-overflows branch from 3481c47 to 7804828 Compare May 4, 2024 02:56
This patch replaces all usages of the `@___WithOverflow` builtins where
the overflow result is discarded with the nicer `math` helpers that
return `error.Overflow`. This cleaned up a bit of code and hopefully
impresses the useful `math` functions on more Zig code later.
@sno2 sno2 force-pushed the feat-remove-raw-overflows branch from 7804828 to ff8eb83 Compare May 4, 2024 04:22
@sno2 sno2 closed this Jun 8, 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.

3 participants