Skip to content

Commit

Permalink
Make log2 more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeSandwich committed Feb 6, 2024
1 parent c8e9e3c commit a1ae543
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions contracts/utils/math/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -389,38 +389,37 @@ library Math {
*/
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
uint256 isGt;
uint256 exp;
unchecked {
isGt = boolToUint(value > 0xffffffffffffffffffffffffffffffff);
value >>= isGt * 128;
result += isGt * 128;
exp = 128 * boolToUint(value > (1 << 128) - 1);
value >>= exp;
result += exp;

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

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

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

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

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

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

isGt = boolToUint(value > 0x1);
result += isGt;
result += boolToUint(value > 1);
}
return result;
}
Expand Down

0 comments on commit a1ae543

Please sign in to comment.