Skip to content

Commit

Permalink
Make Math.log2 branchless
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeSandwich committed Feb 6, 2024
1 parent 192e873 commit 7f5b1b3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
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
---

Make Math.log2 branchless
67 changes: 41 additions & 26 deletions contracts/utils/math/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -391,42 +391,57 @@ library Math {
* @dev Return the log in base 2 of a positive value rounded towards zero.
* Returns 0 if given 0.
*/
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
function log2(uint256 value) internal pure returns (uint256 result) {
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
uint256 isGt;

assembly {
isGt := gt(value, 0xffffffffffffffffffffffffffffffff)
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
value >>= isGt * 128;
result += isGt * 128;

assembly {
isGt := gt(value, 0xffffffffffffffff)
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
value >>= isGt * 64;
result += isGt * 64;

assembly {
isGt := gt(value, 0xffffffff)
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
value >>= isGt * 32;
result += isGt * 32;

assembly {
isGt := gt(value, 0xffff)
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
value >>= isGt * 16;
result += isGt * 16;

assembly {
isGt := gt(value, 0xff)
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
value >>= isGt * 8;
result += isGt * 8;

assembly {
isGt := gt(value, 0xf)
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
value >>= isGt * 4;
result += isGt * 4;

assembly {
isGt := gt(value, 0x3)
}
if (value >> 1 > 0) {
result += 1;
value >>= isGt * 2;
result += isGt * 2;

assembly {
isGt := gt(value, 0x1)
}
result += isGt;
}
return result;
}

/**
Expand Down

0 comments on commit 7f5b1b3

Please sign in to comment.