Skip to content

Instantly share code, notes, and snippets.

@k06a
Last active September 29, 2022 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k06a/12f7d1a4e94edaaf5fe74957d06d4680 to your computer and use it in GitHub Desktop.
Save k06a/12f7d1a4e94edaaf5fe74957d06d4680 to your computer and use it in GitHub Desktop.
isPowOfTen.yul
// 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