aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorMartin Holst Swende <martin@swende.se>2018-10-16 06:51:39 +0800
committerFelix Lange <fjl@users.noreply.github.com>2018-10-16 06:51:39 +0800
commita352de6a08db2c68383c7b1fdcf8267184b7dcea (patch)
tree48aa9b6533849abe5ce5c96052e42626e7432244 /core
parent6a7695e3676ef8275afd5eafde6e045b7a7ab024 (diff)
downloadgo-tangerine-a352de6a08db2c68383c7b1fdcf8267184b7dcea.tar.gz
go-tangerine-a352de6a08db2c68383c7b1fdcf8267184b7dcea.tar.zst
go-tangerine-a352de6a08db2c68383c7b1fdcf8267184b7dcea.zip
core/vm: add shortcuts for trivial exp cases (#16851)
Diffstat (limited to 'core')
-rw-r--r--core/vm/instructions.go20
1 files changed, 16 insertions, 4 deletions
diff --git a/core/vm/instructions.go b/core/vm/instructions.go
index e94a2777b..b7c3ca532 100644
--- a/core/vm/instructions.go
+++ b/core/vm/instructions.go
@@ -124,10 +124,22 @@ func opSmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory
func opExp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
base, exponent := stack.pop(), stack.pop()
- stack.push(math.Exp(base, exponent))
-
- interpreter.intPool.put(base, exponent)
-
+ // some shortcuts
+ cmpToOne := exponent.Cmp(big1)
+ if cmpToOne < 0 { // Exponent is zero
+ // x ^ 0 == 1
+ stack.push(base.SetUint64(1))
+ } else if base.Sign() == 0 {
+ // 0 ^ y, if y != 0, == 0
+ stack.push(base.SetUint64(0))
+ } else if cmpToOne == 0 { // Exponent is one
+ // x ^ 1 == x
+ stack.push(base)
+ } else {
+ stack.push(math.Exp(base, exponent))
+ interpreter.intPool.put(base)
+ }
+ interpreter.intPool.put(exponent)
return nil, nil
}