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

Optimize overflow check in mulDivRoundingUp #667

Merged
merged 4 commits into from
May 23, 2024

Conversation

shuhuiluo
Copy link
Contributor

Factored out of #601.

Related Issue

Which issue does this pull request resolve?

Description of changes

This commit optimizes the overflow check within the mulDivRoundingUp function of FullMath library. This is achieved by combining two steps: the increment of the result and the overflow check, into a single step using the ++ operator. This simplifies the code, improves its readability and brings down the gas usage slightly, as reflected in updated snapshot files.

This commit optimizes the overflow check within the `mulDivRoundingUp` function of FullMath library. This is achieved by combining two steps: the increment of the `result` and the overflow check, into a single step using the `++` operator. This simplifies the code, improves its readability and brings down the gas usage slightly, as reflected in updated snapshot files.
require(result < type(uint256).max);
result++;
if (mulmod(a, b, denominator) != 0) {
require(++result > 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

looks good. I see that the test test_fuzz_mulDivRoundingUp isn't very good because it checks

assertTrue(result == numerator / d || result == numerator / d + 1);

🥲
please could you update it to correctly identify whether it should have +1 or not with a mod?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

@hensha256 hensha256 merged commit 4ea21e0 into Uniswap:main May 23, 2024
5 of 6 checks passed
@shuhuiluo shuhuiluo deleted the mulDivRoundingUp branch May 23, 2024 01:11
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