Skip to content

Commit

Permalink
Branchless opt
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Sep 9, 2022
1 parent 01a5235 commit bae48b5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 56 deletions.
28 changes: 14 additions & 14 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ AuthTest:testSetOwnerWithPermissiveAuthority(address,address) (runs: 256, μ: 13
Bytes32AddressLibTest:testFillLast12Bytes() (gas: 223)
Bytes32AddressLibTest:testFromLast20Bytes() (gas: 191)
CREATE3Test:testDeployERC20() (gas: 853111)
CREATE3Test:testDeployERC20(bytes32,string,string,uint8) (runs: 256, μ: 923845, ~: 921961)
CREATE3Test:testDeployERC20(bytes32,string,string,uint8) (runs: 256, μ: 924106, ~: 921961)
CREATE3Test:testFailDoubleDeployDifferentBytecode() (gas: 9079256848778914174)
CREATE3Test:testFailDoubleDeployDifferentBytecode(bytes32,bytes,bytes) (runs: 256, μ: 5062195514745832485, ~: 8937393460516727435)
CREATE3Test:testFailDoubleDeployDifferentBytecode(bytes32,bytes,bytes) (runs: 256, μ: 5097107207950975695, ~: 8937393460516727446)
CREATE3Test:testFailDoubleDeploySameBytecode() (gas: 9079256848778906218)
CREATE3Test:testFailDoubleDeploySameBytecode(bytes32,bytes) (runs: 256, μ: 5027837975401088877, ~: 8937393460516728677)
CREATE3Test:testFailDoubleDeploySameBytecode(bytes32,bytes) (runs: 256, μ: 5062749668606232091, ~: 8937393460516728689)
DSTestPlusTest:testBound() (gas: 14214)
DSTestPlusTest:testBound(uint256,uint256,uint256) (runs: 256, μ: 2787, ~: 2793)
DSTestPlusTest:testBrutalizeMemory() (gas: 823)
Expand Down Expand Up @@ -115,8 +115,8 @@ ERC1155Test:testSafeTransferFromToEOA() (gas: 93167)
ERC1155Test:testSafeTransferFromToEOA(uint256,uint256,bytes,uint256,address,bytes) (runs: 256, μ: 93478, ~: 97450)
ERC1155Test:testSafeTransferFromToERC1155Recipient() (gas: 739583)
ERC1155Test:testSafeTransferFromToERC1155Recipient(uint256,uint256,bytes,uint256,bytes) (runs: 256, μ: 769591, ~: 765729)
ERC20Invariants:invariantBalanceSum() (runs: 256, calls: 3840, reverts: 2388)
ERC20Test:invariantMetadata() (runs: 256, calls: 3840, reverts: 2606)
ERC20Invariants:invariantBalanceSum (runs: 256, calls: 3840, reverts: 2388)
ERC20Test:invariantMetadata (runs: 256, calls: 3840, reverts: 2560)
ERC20Test:testApprove() (gas: 31058)
ERC20Test:testApprove(address,uint256) (runs: 256, μ: 30424, ~: 31280)
ERC20Test:testBurn() (gas: 56970)
Expand All @@ -137,16 +137,16 @@ ERC20Test:testFailTransferFromInsufficientBalance(address,uint256,uint256) (runs
ERC20Test:testFailTransferInsufficientBalance() (gas: 52806)
ERC20Test:testFailTransferInsufficientBalance(address,uint256,uint256) (runs: 256, μ: 51720, ~: 55310)
ERC20Test:testInfiniteApproveTransferFrom() (gas: 89793)
ERC20Test:testMetadata(string,string,uint8) (runs: 256, μ: 870618, ~: 863277)
ERC20Test:testMetadata(string,string,uint8) (runs: 256, μ: 868512, ~: 863173)
ERC20Test:testMint() (gas: 53746)
ERC20Test:testMint(address,uint256) (runs: 256, μ: 52214, ~: 53925)
ERC20Test:testPermit() (gas: 63193)
ERC20Test:testPermit(uint248,address,uint256,uint256) (runs: 256, μ: 62584, ~: 63517)
ERC20Test:testTransfer() (gas: 60272)
ERC20Test:testTransfer(address,uint256) (runs: 256, μ: 58773, ~: 60484)
ERC20Test:testTransferFrom() (gas: 83777)
ERC20Test:testTransferFrom(address,uint256,uint256) (runs: 256, μ: 86464, ~: 92841)
ERC4626Test:invariantMetadata() (runs: 256, calls: 3840, reverts: 2881)
ERC20Test:testTransferFrom(address,uint256,uint256) (runs: 256, μ: 86308, ~: 92841)
ERC4626Test:invariantMetadata (runs: 256, calls: 3840, reverts: 2881)
ERC4626Test:testFailDepositWithNoApproval() (gas: 13357)
ERC4626Test:testFailDepositWithNotEnoughApproval() (gas: 86993)
ERC4626Test:testFailDepositZero() (gas: 7780)
Expand All @@ -163,7 +163,7 @@ ERC4626Test:testSingleDepositWithdraw(uint128) (runs: 256, μ: 201569, ~: 201579
ERC4626Test:testSingleMintRedeem(uint128) (runs: 256, μ: 201484, ~: 201494)
ERC4626Test:testVaultInteractionsForSomeoneElse() (gas: 286247)
ERC4626Test:testWithdrawZero() (gas: 52462)
ERC721Test:invariantMetadata() (runs: 256, calls: 3840, reverts: 2170)
ERC721Test:invariantMetadata (runs: 256, calls: 3840, reverts: 2170)
ERC721Test:testApprove() (gas: 78427)
ERC721Test:testApprove(address,uint256) (runs: 256, μ: 78637, ~: 78637)
ERC721Test:testApproveAll() (gas: 31063)
Expand Down Expand Up @@ -273,9 +273,9 @@ FixedPointMathLibTest:testMulWadUp() (gas: 959)
FixedPointMathLibTest:testMulWadUp(uint256,uint256) (runs: 256, μ: 806, ~: 1051)
FixedPointMathLibTest:testMulWadUpEdgeCases() (gas: 1002)
FixedPointMathLibTest:testRPow() (gas: 2142)
FixedPointMathLibTest:testSqrt() (gas: 2537)
FixedPointMathLibTest:testSqrt(uint256) (runs: 256, μ: 1124, ~: 1131)
LibStringTest:testDifferentiallyFuzzToString(uint256,bytes) (runs: 256, μ: 20749, ~: 8925)
FixedPointMathLibTest:testSqrt() (gas: 521)
FixedPointMathLibTest:testSqrt(uint256) (runs: 256, μ: 1065, ~: 1113)
LibStringTest:testDifferentiallyFuzzToString(uint256,bytes) (runs: 256, μ: 21006, ~: 9531)
LibStringTest:testToString() (gas: 10047)
LibStringTest:testToStringDirty() (gas: 8123)
LibStringTest:testToStringOverwrite() (gas: 484)
Expand Down Expand Up @@ -306,7 +306,7 @@ OwnedTest:testCallFunctionAsNonOwner(address) (runs: 256, μ: 16238, ~: 16257)
OwnedTest:testCallFunctionAsOwner() (gas: 10479)
OwnedTest:testSetOwner() (gas: 13035)
OwnedTest:testSetOwner(address) (runs: 256, μ: 13151, ~: 13170)
ReentrancyGuardTest:invariantReentrancyStatusAlways1() (runs: 256, calls: 3840, reverts: 319)
ReentrancyGuardTest:invariantReentrancyStatusAlways1 (runs: 256, calls: 3840, reverts: 319)
ReentrancyGuardTest:testFailUnprotectedCall() (gas: 46147)
ReentrancyGuardTest:testNoReentrancy() (gas: 7515)
ReentrancyGuardTest:testProtectedCall() (gas: 33467)
Expand Down Expand Up @@ -442,7 +442,7 @@ SignedWadMathTest:testFailWadDivZeroDenominator(int256) (runs: 256, μ: 296, ~:
SignedWadMathTest:testFailWadMulOverflow(int256,int256) (runs: 256, μ: 323, ~: 296)
SignedWadMathTest:testWadDiv(uint256,uint256,bool,bool) (runs: 256, μ: 5696, ~: 5664)
SignedWadMathTest:testWadMul(uint256,uint256,bool,bool) (runs: 256, μ: 5720, ~: 5688)
WETHInvariants:invariantTotalSupplyEqualsBalance() (runs: 256, calls: 3840, reverts: 1908)
WETHInvariants:invariantTotalSupplyEqualsBalance (runs: 256, calls: 3840, reverts: 1908)
WETHTest:testDeposit() (gas: 63535)
WETHTest:testDeposit(uint256) (runs: 256, μ: 62792, ~: 65880)
WETHTest:testFallbackDeposit() (gas: 63249)
Expand Down
2 changes: 1 addition & 1 deletion lib/ds-test
Submodule ds-test updated 2 files
+22 −21 demo/demo.sol
+4 −4 src/test.sol
51 changes: 10 additions & 41 deletions src/utils/FixedPointMathLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -165,41 +165,15 @@ library FixedPointMathLib {

function sqrt(uint256 x) internal pure returns (uint256 z) {
assembly {
// Start off with z at 1.
z := 1

// Used below to help find a nearby power of 2.
let y := x

// Find the lowest power of 2 that is at least sqrt(x).
if iszero(lt(y, 0x100000000000000000000000000000000)) {
y := shr(128, y) // Like dividing by 2 ** 128.
z := shl(64, z) // Like multiplying by 2 ** 64.
}
if iszero(lt(y, 0x10000000000000000)) {
y := shr(64, y) // Like dividing by 2 ** 64.
z := shl(32, z) // Like multiplying by 2 ** 32.
}
if iszero(lt(y, 0x100000000)) {
y := shr(32, y) // Like dividing by 2 ** 32.
z := shl(16, z) // Like multiplying by 2 ** 16.
}
if iszero(lt(y, 0x10000)) {
y := shr(16, y) // Like dividing by 2 ** 16.
z := shl(8, z) // Like multiplying by 2 ** 8.
}
if iszero(lt(y, 0x100)) {
y := shr(8, y) // Like dividing by 2 ** 8.
z := shl(4, z) // Like multiplying by 2 ** 4.
}
if iszero(lt(y, 0x10)) {
y := shr(4, y) // Like dividing by 2 ** 4.
z := shl(2, z) // Like multiplying by 2 ** 2.
}
if iszero(lt(y, 0x8)) {
// Equivalent to 2 ** z.
z := shl(1, z)
}
let r := shl(7, gt(x, 0xffffffffffffffffffffffffffffffff))
r := or(r, shl(6, gt(shr(r, x), 0xffffffffffffffff)))
r := or(r, shl(5, gt(shr(r, x), 0xffffffff)))
r := or(r, shl(4, gt(shr(r, x), 0xffff)))
r := or(r, shl(3, gt(shr(r, x), 0xff)))
r := or(r, shl(2, gt(shr(r, x), 0xf)))
r := or(r, shl(1, gt(shr(r, x), 0x8)))
z := shl(shr(1, r), 1)

// Shifting right by 1 is like dividing by 2.
z := shr(1, add(z, div(x, z)))
Expand All @@ -210,13 +184,8 @@ library FixedPointMathLib {
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))

// Compute a rounded down version of z.
let zRoundDown := div(x, z)

// If zRoundDown is smaller, use it.
if lt(zRoundDown, z) {
z := zRoundDown
}
// Round down z.
z := sub(z, lt(div(x, z), z))
}
}

Expand Down

0 comments on commit bae48b5

Please sign in to comment.