-
Notifications
You must be signed in to change notification settings - Fork 702
⚡️ Optimize gas consumption for mulDivUp and mulDivDown
#306
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
Changes from all commits
4c82119
1e2c974
460a035
d1d4430
6ea30df
8f64001
d6fb0a8
880ad0f
cc81c02
49fa626
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,8 @@ library FixedPointMathLib { | |
| SIMPLIFIED FIXED POINT OPERATIONS | ||
| //////////////////////////////////////////////////////////////*/ | ||
|
|
||
| uint256 internal constant MAX_UINT256 = 2**256 - 1; | ||
|
|
||
| uint256 internal constant WAD = 1e18; // The scalar of ETH and most ERC20s. | ||
|
|
||
| function mulWadDown(uint256 x, uint256 y) internal pure returns (uint256) { | ||
|
|
@@ -37,16 +39,13 @@ library FixedPointMathLib { | |
| uint256 denominator | ||
| ) internal pure returns (uint256 z) { | ||
| assembly { | ||
| // Store x * y in z for now. | ||
| z := mul(x, y) | ||
|
|
||
| // Equivalent to require(denominator != 0 && (x == 0 || (x * y) / x == y)) | ||
| if iszero(and(iszero(iszero(denominator)), or(iszero(x), eq(div(z, x), y)))) { | ||
| // Equivalent to require(denominator != 0 && (y == 0 || x <= type(uint256).max / y)) | ||
| if iszero(mul(denominator, iszero(mul(y, gt(x, div(MAX_UINT256, y)))))) { | ||
| revert(0, 0) | ||
| } | ||
|
|
||
| // Divide z by the denominator. | ||
| z := div(z, denominator) | ||
| // Divide x * y by the denominator. | ||
| z := div(mul(x, y), denominator) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you please add an english translation comment here like the one you removed? |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -56,18 +55,14 @@ library FixedPointMathLib { | |
| uint256 denominator | ||
| ) internal pure returns (uint256 z) { | ||
| assembly { | ||
| // Store x * y in z for now. | ||
| z := mul(x, y) | ||
|
|
||
| // Equivalent to require(denominator != 0 && (x == 0 || (x * y) / x == y)) | ||
| if iszero(and(iszero(iszero(denominator)), or(iszero(x), eq(div(z, x), y)))) { | ||
| // Equivalent to require(denominator != 0 && (y == 0 || x <= type(uint256).max / y)) | ||
| if iszero(mul(denominator, iszero(mul(y, gt(x, div(MAX_UINT256, y)))))) { | ||
| revert(0, 0) | ||
| } | ||
|
|
||
| // First, divide z - 1 by the denominator and add 1. | ||
| // We allow z - 1 to underflow if z is 0, because we multiply the | ||
| // end result by 0 if z is zero, ensuring we return 0 if z is zero. | ||
| z := mul(iszero(iszero(z)), add(div(sub(z, 1), denominator), 1)) | ||
| // If x * y modulo the denominator is strictly greater than 0, | ||
| // 1 is added to round up the division of x * y by the denominator. | ||
| z := add(gt(mod(mul(x, y), denominator), 0), div(mul(x, y), denominator)) | ||
MerlinEgalite marked this conversation as resolved.
Show resolved
Hide resolved
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you please add an english translation comment here like the one you removed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! |
||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use type(uint256).max here instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it is impossible in assembly
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes not possible, unfortunately. We can use
not(0)to replace it though. If the optimizer is high enough it should be the same gas cost.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EDIT: the gas seems to increase a bit (see below). I left the config at 1M optimizer runs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i just meant writing
haha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah lol sorry, not possible either 🥲
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh thats... stupid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
welp alright
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah clearly