Skip to content

Commit

Permalink
Optimize small bases.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Jul 28, 2020
1 parent 1450953 commit 1405ed6
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion libsolidity/codegen/YulUtilFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,28 @@ string YulUtilFunctions::overflowCheckedIntExpFunction(
eq(exponent, 1)
) { power := base leave }
power := 1
let max := <maxValue>
// small base optimizations
if eq(base, 2) {
if gt(exponent, 255) { revert(0, 0) }
power := exp(2, exponent)
if gt(power, max) { revert(0, 0) }
leave
}
// b**e < 2**256 <=> e * log(b) < 256 * log(2) <=> e < 256 * log(2) / log(b)
if or(
and(lt(base, 11), lt(exponent, 78)),
and(lt(base, 257), lt(exponent, 32))
) {
power := exp(base, exponent)
if gt(power, max) { revert(0, 0) }
leave
}
power := 1
for { } gt(exponent, 1) {} {
// overflow check for base * base
<?signed>
Expand Down

0 comments on commit 1405ed6

Please sign in to comment.