mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
common/math, core/vm: Move optimizations to math
Move exponentiation optimizations in opExp to math.Exp
This commit is contained in:
parent
53eb4e0b0f
commit
82087096c5
2 changed files with 14 additions and 15 deletions
|
|
@ -198,6 +198,18 @@ func S256(x *big.Int) *big.Int {
|
|||
// Courtesy @karalabe and @chfast
|
||||
func Exp(base, exponent *big.Int) *big.Int {
|
||||
result := big.NewInt(1)
|
||||
// some shortcuts
|
||||
cmpToOne := exponent.Cmp(result)
|
||||
if cmpToOne < 0 { // Exponent is zero
|
||||
// x ^ 0 == 1
|
||||
return result
|
||||
} else if base.Sign() == 0 {
|
||||
// 0 ^ y, if y != 0, == 0
|
||||
return result.SetUint64(0)
|
||||
} else if cmpToOne == 0 { // Exponent is one
|
||||
// x ^ 1 == x
|
||||
return result.Set(base)
|
||||
}
|
||||
|
||||
for _, word := range exponent.Bits() {
|
||||
for i := 0; i < wordBits; i++ {
|
||||
|
|
|
|||
|
|
@ -124,21 +124,8 @@ 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()
|
||||
// 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)
|
||||
}
|
||||
stack.push(math.Exp(base, exponent))
|
||||
interpreter.intPool.put(base)
|
||||
interpreter.intPool.put(exponent)
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue