Last active
September 29, 2022 14:49
-
-
Save k06a/12f7d1a4e94edaaf5fe74957d06d4680 to your computer and use it in GitHub Desktop.
isPowOfTen.yul
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup | |
function isPowerOfTen(uint256 x) public pure returns (bool result) { | |
assembly { | |
let dict := 0x00011c021d0e18031e16140f191104081f1b0d17151310071a0c12060b050a09 | |
let zeroes := 0 | |
for { let offset := 0 } lt(offset, 256) { offset := add(offset, 32) } { | |
let v := and(shr(offset, x), 0xffffffff) | |
switch v | |
case 0 { | |
zeroes := add(zeroes, 32) | |
} | |
default { | |
zeroes := add(zeroes, byte(shr(27, mul(and(v, sub(0, v)), 0x077CB531)), dict)) | |
} | |
} | |
result := eq(shr(zeroes, x), pow(5, zeroes)) // todo: use table of powers | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment