Skip to content

Commit

Permalink
Optimize overflow check in mulDivRoundingUp (#667)
Browse files Browse the repository at this point in the history
* Optimize overflow check in `mulDivRoundingUp`

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.

* Update assertions in `test_fuzz_mulDivRoundingUp`
  • Loading branch information
shuhuiluo authored May 23, 2024
1 parent 5402ce1 commit 4ea21e0
Show file tree
Hide file tree
Showing 32 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactInCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1974
1961
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactOutCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1713
1700
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactOutPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2982
2956
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactInCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2091
2073
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactInPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3044
3021
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactOutCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1830
1812
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactOutPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2572
2554
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153006
152993
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
330666
330653
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
285662
285649
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143216
143203
Original file line number Diff line number Diff line change
@@ -1 +1 @@
301184
301171
Original file line number Diff line number Diff line change
@@ -1 +1 @@
665
652
Original file line number Diff line number Diff line change
@@ -1 +1 @@
776
763
Original file line number Diff line number Diff line change
@@ -1 +1 @@
856
843
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21173
21161
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap with native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115813
115780
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130974
130941
2 changes: 1 addition & 1 deletion .forge-snapshots/swap CA fee on unspecified.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
182094
182061
Original file line number Diff line number Diff line change
@@ -1 +1 @@
111942
111903
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123285
123246
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135150
135124
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn native 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124397
124371
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint output as 6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162828
162795
Original file line number Diff line number Diff line change
@@ -1 +1 @@
220585
220513
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
147029
146996
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142010
141971
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with lp fee and protocol fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179060
179040
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with return dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154882
154849
2 changes: 1 addition & 1 deletion .forge-snapshots/update dynamic fee in before swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
157485
157452
5 changes: 2 additions & 3 deletions src/libraries/FullMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ library FullMath {
function mulDivRoundingUp(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
result = mulDiv(a, b, denominator);
if (mulmod(a, b, denominator) > 0) {
require(result < type(uint256).max);
result++;
if (mulmod(a, b, denominator) != 0) {
require(++result > 0);
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions test/libraries/FullMath.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract FullMathTest is Test {
function test_fuzz_mulDiv(uint256 x, uint256 y, uint256 d) public pure {
vm.assume(d != 0);
vm.assume(y != 0);
vm.assume(x <= type(uint256).max / y);
x = bound(x, 0, type(uint256).max / y);
assertEq(FullMath.mulDiv(x, y, d), x * y / d);
}

Expand Down Expand Up @@ -97,10 +97,14 @@ contract FullMathTest is Test {
function test_fuzz_mulDivRoundingUp(uint256 x, uint256 y, uint256 d) public pure {
vm.assume(d != 0);
vm.assume(y != 0);
vm.assume(x <= type(uint256).max / y);
x = bound(x, 0, type(uint256).max / y);
uint256 numerator = x * y;
uint256 result = FullMath.mulDivRoundingUp(x, y, d);
assertTrue(result == numerator / d || result == numerator / d + 1);
if (mulmod(x, y, d) > 0) {
assertEq(result, numerator / d + 1);
} else {
assertEq(result, numerator / d);
}
}

function test_invariant_mulDivRounding(uint256 x, uint256 y, uint256 d) public pure {
Expand Down

0 comments on commit 4ea21e0

Please sign in to comment.