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

⚡️ LnWad micro optimization #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ LinearNFTTest:testMintNFT() (gas: 86166)
LinearVRGDATest:testAlwaysTargetPriceInRightConditions(uint256) (runs: 256, μ: 10109, ~: 10109)
LinearVRGDATest:testPricingBasic() (gas: 9972)
LinearVRGDATest:testTargetPrice() (gas: 10749)
LogisticNFTTest:testCannotMintMoreThanMax() (gas: 4437960)
LogisticNFTTest:testCannotUnderpayForNFTMint() (gas: 40767)
LogisticNFTTest:testMintAllNFT() (gas: 4426938)
LogisticNFTTest:testMintNFT() (gas: 86855)
LogisticVRGDATest:testAlwaysTargetPriceInRightConditions(uint256) (runs: 256, μ: 11692, ~: 11692)
LogisticNFTTest:testCannotMintMoreThanMax() (gas: 4435160)
LogisticNFTTest:testCannotUnderpayForNFTMint() (gas: 40739)
LogisticNFTTest:testMintAllNFT() (gas: 4424138)
LogisticNFTTest:testMintNFT() (gas: 86827)
LogisticVRGDATest:testAlwaysTargetPriceInRightConditions(uint256) (runs: 256, μ: 11636, ~: 11636)
LogisticVRGDATest:testFailOverflowForBeyondLimitTokens(uint256,uint256) (runs: 256, μ: 10278, ~: 10278)
LogisticVRGDATest:testGetTargetSaleTimeDoesNotRevertEarly() (gas: 6147)
LogisticVRGDATest:testGetTargetSaleTimeDoesNotRevertEarly() (gas: 6119)
LogisticVRGDATest:testGetTargetSaleTimeRevertsWhenExpected() (gas: 8533)
LogisticVRGDATest:testNoOverflowForAllTokens(uint256,uint256) (runs: 256, μ: 11229, ~: 11229)
LogisticVRGDATest:testNoOverflowForMostTokens(uint256,uint256) (runs: 256, μ: 11385, ~: 11528)
LogisticVRGDATest:testPricingBasic() (gas: 10762)
LogisticVRGDATest:testTargetPrice() (gas: 12246)
LogisticVRGDATest:testNoOverflowForAllTokens(uint256,uint256) (runs: 256, μ: 11201, ~: 11201)
LogisticVRGDATest:testNoOverflowForMostTokens(uint256,uint256) (runs: 256, μ: 11374, ~: 11500)
LogisticVRGDATest:testPricingBasic() (gas: 10734)
LogisticVRGDATest:testTargetPrice() (gas: 12190)
25 changes: 17 additions & 8 deletions src/utils/SignedWadMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,23 @@ function wadLn(int256 x) pure returns (int256 r) {
// and add ln(2**96 / 10**18) at the end.

assembly {
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(r, shl(3, lt(0xff, shr(r, x))))
r := or(r, shl(2, lt(0xf, shr(r, x))))
r := or(r, shl(1, lt(0x3, shr(r, x))))
r := or(r, lt(0x1, shr(r, x)))
let v := x
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, v))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, v))))
r := or(r, shl(5, lt(0xffffffff, shr(r, v))))

// For the remaining 32 bits, use a De Bruijn lookup.
// See: https://graphics.stanford.edu/~seander/bithacks.html
v := shr(r, v)
v := or(v, shr(1, v))
v := or(v, shr(2, v))
v := or(v, shr(4, v))
v := or(v, shr(8, v))
v := or(v, shr(16, v))

// prettier-ignore
r := or(r, byte(shr(251, mul(v, shl(224, 0x07c4acdd))),
0x0009010a0d15021d0b0e10121619031e080c141c0f111807131b17061a05041f))
}

// Reduce range of x to (1, 2) * 2**96
Expand Down