diff --git a/.gas-snapshot b/.gas-snapshot index b88bd7d..7580cfb 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -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) diff --git a/src/utils/SignedWadMath.sol b/src/utils/SignedWadMath.sol index 75e8e19..53e61f7 100644 --- a/src/utils/SignedWadMath.sol +++ b/src/utils/SignedWadMath.sol @@ -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