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

Optimize Math operations using branchless bool to uint translation. #4878

Merged
merged 10 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions .changeset/nervous-pans-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': patch
---

`Math`: Add `boolToUint` for branchless math.
ernestognw marked this conversation as resolved.
Show resolved Hide resolved
122 changes: 63 additions & 59 deletions contracts/utils/math/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,7 @@ library Math {
* @dev Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0) {
result += 1;
}
return result;
return mulDiv(x, y, denominator) + boolToUint(unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0);
}

/**
Expand Down Expand Up @@ -383,7 +379,7 @@ library Math {
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (unsignedRoundsUp(rounding) && result * result < a ? 1 : 0);
return result + boolToUint(unsignedRoundsUp(rounding) && result * result < a);
}
}

Expand All @@ -393,38 +389,37 @@ library Math {
*/
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
uint256 exp;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
}
if (value >> 1 > 0) {
result += 1;
}
exp = 128 * boolToUint(value > (1 << 128) - 1);
value >>= exp;
result += exp;

exp = 64 * boolToUint(value > (1 << 64) - 1);
value >>= exp;
result += exp;

exp = 32 * boolToUint(value > (1 << 32) - 1);
value >>= exp;
result += exp;

exp = 16 * boolToUint(value > (1 << 16) - 1);
value >>= exp;
result += exp;

exp = 8 * boolToUint(value > (1 << 8) - 1);
value >>= exp;
result += exp;

exp = 4 * boolToUint(value > (1 << 4) - 1);
value >>= exp;
result += exp;

exp = 2 * boolToUint(value > (1 << 2) - 1);
value >>= exp;
result += exp;

result += boolToUint(value > 1);
}
return result;
}
Expand All @@ -436,7 +431,7 @@ library Math {
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + (unsignedRoundsUp(rounding) && 1 << result < value ? 1 : 0);
return result + boolToUint(unsignedRoundsUp(rounding) && 1 << result < value);
}
}

Expand Down Expand Up @@ -485,7 +480,7 @@ library Math {
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (unsignedRoundsUp(rounding) && 10 ** result < value ? 1 : 0);
return result + boolToUint(unsignedRoundsUp(rounding) && 10 ** result < value);
}
}

Expand All @@ -497,26 +492,25 @@ library Math {
*/
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
uint256 isGt;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 16;
}
if (value >> 64 > 0) {
value >>= 64;
result += 8;
}
if (value >> 32 > 0) {
value >>= 32;
result += 4;
}
if (value >> 16 > 0) {
value >>= 16;
result += 2;
}
if (value >> 8 > 0) {
result += 1;
}
isGt = boolToUint(value > (1 << 128) - 1);
value >>= isGt * 128;
result += isGt * 16;

isGt = boolToUint(value > (1 << 64) - 1);
value >>= isGt * 64;
result += isGt * 8;

isGt = boolToUint(value > (1 << 32) - 1);
value >>= isGt * 32;
result += isGt * 4;

isGt = boolToUint(value > (1 << 16) - 1);
value >>= isGt * 16;
result += isGt * 2;

result += boolToUint(value > (1 << 8) - 1);
}
return result;
}
Expand All @@ -528,7 +522,7 @@ library Math {
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (unsignedRoundsUp(rounding) && 1 << (result << 3) < value ? 1 : 0);
return result + boolToUint(unsignedRoundsUp(rounding) && 1 << (result << 3) < value);
}
}

Expand All @@ -538,4 +532,14 @@ library Math {
function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
return uint8(rounding) % 2 == 1;
}

/**
* @dev Cast a boolean (false or true) to a uint256 (0 or 1) with no jump.
*/
function boolToUint(bool b) internal pure returns (uint256 u) {
/// @solidity memory-safe-assembly
assembly {
u := iszero(iszero(b))
}
}
}
10 changes: 10 additions & 0 deletions test/utils/math/Math.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,14 @@ describe('Math', function () {
});
});
});

describe('boolToUint', function () {
it('boolToUint(false) should be 0', async function () {
expect(await this.mock.$boolToUint(false)).to.equal(0n);
});

it('boolToUint(true) should be 1', async function () {
expect(await this.mock.$boolToUint(true)).to.equal(1n);
});
});
});
Loading