all: replace division with right shift if possible (#29911)
This commit is contained in:
parent
4939c25341
commit
06f1d077d3
@ -325,7 +325,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
|
|||||||
var (
|
var (
|
||||||
blockReward = big.NewInt(miningReward)
|
blockReward = big.NewInt(miningReward)
|
||||||
minerReward = new(big.Int).Set(blockReward)
|
minerReward = new(big.Int).Set(blockReward)
|
||||||
perOmmer = new(big.Int).Div(blockReward, big.NewInt(32))
|
perOmmer = new(big.Int).Rsh(blockReward, 5)
|
||||||
)
|
)
|
||||||
for _, ommer := range pre.Env.Ommers {
|
for _, ommer := range pre.Env.Ommers {
|
||||||
// Add 1/32th for each ommer included
|
// Add 1/32th for each ommer included
|
||||||
@ -334,7 +334,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
|
|||||||
reward := big.NewInt(8)
|
reward := big.NewInt(8)
|
||||||
reward.Sub(reward, new(big.Int).SetUint64(ommer.Delta))
|
reward.Sub(reward, new(big.Int).SetUint64(ommer.Delta))
|
||||||
reward.Mul(reward, blockReward)
|
reward.Mul(reward, blockReward)
|
||||||
reward.Div(reward, big.NewInt(8))
|
reward.Rsh(reward, 3)
|
||||||
statedb.AddBalance(ommer.Address, uint256.MustFromBig(reward), tracing.BalanceIncreaseRewardMineUncle)
|
statedb.AddBalance(ommer.Address, uint256.MustFromBig(reward), tracing.BalanceIncreaseRewardMineUncle)
|
||||||
}
|
}
|
||||||
statedb.AddBalance(pre.Env.Coinbase, uint256.MustFromBig(minerReward), tracing.BalanceIncreaseRewardMineBlock)
|
statedb.AddBalance(pre.Env.Coinbase, uint256.MustFromBig(minerReward), tracing.BalanceIncreaseRewardMineBlock)
|
||||||
|
@ -562,12 +562,6 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
|
|||||||
return hash
|
return hash
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some weird constants to avoid constant memory allocs for them.
|
|
||||||
var (
|
|
||||||
u256_8 = uint256.NewInt(8)
|
|
||||||
u256_32 = uint256.NewInt(32)
|
|
||||||
)
|
|
||||||
|
|
||||||
// accumulateRewards credits the coinbase of the given block with the mining
|
// accumulateRewards credits the coinbase of the given block with the mining
|
||||||
// reward. The total reward consists of the static block reward and rewards for
|
// reward. The total reward consists of the static block reward and rewards for
|
||||||
// included uncles. The coinbase of each uncle block is also rewarded.
|
// included uncles. The coinbase of each uncle block is also rewarded.
|
||||||
@ -589,10 +583,10 @@ func accumulateRewards(config *params.ChainConfig, stateDB *state.StateDB, heade
|
|||||||
r.AddUint64(uNum, 8)
|
r.AddUint64(uNum, 8)
|
||||||
r.Sub(r, hNum)
|
r.Sub(r, hNum)
|
||||||
r.Mul(r, blockReward)
|
r.Mul(r, blockReward)
|
||||||
r.Div(r, u256_8)
|
r.Rsh(r, 3)
|
||||||
stateDB.AddBalance(uncle.Coinbase, r, tracing.BalanceIncreaseRewardMineUncle)
|
stateDB.AddBalance(uncle.Coinbase, r, tracing.BalanceIncreaseRewardMineUncle)
|
||||||
|
|
||||||
r.Div(blockReward, u256_32)
|
r.Rsh(blockReward, 5)
|
||||||
reward.Add(reward, r)
|
reward.Add(reward, r)
|
||||||
}
|
}
|
||||||
stateDB.AddBalance(header.Coinbase, reward, tracing.BalanceIncreaseRewardMineBlock)
|
stateDB.AddBalance(header.Coinbase, reward, tracing.BalanceIncreaseRewardMineBlock)
|
||||||
|
@ -572,6 +572,6 @@ func deriveChainId(v *big.Int) *big.Int {
|
|||||||
}
|
}
|
||||||
return new(big.Int).SetUint64((v - 35) / 2)
|
return new(big.Int).SetUint64((v - 35) / 2)
|
||||||
}
|
}
|
||||||
v = new(big.Int).Sub(v, big.NewInt(35))
|
v.Sub(v, big.NewInt(35))
|
||||||
return v.Div(v, big.NewInt(2))
|
return v.Rsh(v, 1)
|
||||||
}
|
}
|
||||||
|
@ -296,10 +296,7 @@ type bigModExp struct {
|
|||||||
var (
|
var (
|
||||||
big1 = big.NewInt(1)
|
big1 = big.NewInt(1)
|
||||||
big3 = big.NewInt(3)
|
big3 = big.NewInt(3)
|
||||||
big4 = big.NewInt(4)
|
|
||||||
big7 = big.NewInt(7)
|
big7 = big.NewInt(7)
|
||||||
big8 = big.NewInt(8)
|
|
||||||
big16 = big.NewInt(16)
|
|
||||||
big20 = big.NewInt(20)
|
big20 = big.NewInt(20)
|
||||||
big32 = big.NewInt(32)
|
big32 = big.NewInt(32)
|
||||||
big64 = big.NewInt(64)
|
big64 = big.NewInt(64)
|
||||||
@ -325,13 +322,13 @@ func modexpMultComplexity(x *big.Int) *big.Int {
|
|||||||
case x.Cmp(big1024) <= 0:
|
case x.Cmp(big1024) <= 0:
|
||||||
// (x ** 2 // 4 ) + ( 96 * x - 3072)
|
// (x ** 2 // 4 ) + ( 96 * x - 3072)
|
||||||
x = new(big.Int).Add(
|
x = new(big.Int).Add(
|
||||||
new(big.Int).Div(new(big.Int).Mul(x, x), big4),
|
new(big.Int).Rsh(new(big.Int).Mul(x, x), 2),
|
||||||
new(big.Int).Sub(new(big.Int).Mul(big96, x), big3072),
|
new(big.Int).Sub(new(big.Int).Mul(big96, x), big3072),
|
||||||
)
|
)
|
||||||
default:
|
default:
|
||||||
// (x ** 2 // 16) + (480 * x - 199680)
|
// (x ** 2 // 16) + (480 * x - 199680)
|
||||||
x = new(big.Int).Add(
|
x = new(big.Int).Add(
|
||||||
new(big.Int).Div(new(big.Int).Mul(x, x), big16),
|
new(big.Int).Rsh(new(big.Int).Mul(x, x), 4),
|
||||||
new(big.Int).Sub(new(big.Int).Mul(big480, x), big199680),
|
new(big.Int).Sub(new(big.Int).Mul(big480, x), big199680),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -369,7 +366,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
|
|||||||
adjExpLen := new(big.Int)
|
adjExpLen := new(big.Int)
|
||||||
if expLen.Cmp(big32) > 0 {
|
if expLen.Cmp(big32) > 0 {
|
||||||
adjExpLen.Sub(expLen, big32)
|
adjExpLen.Sub(expLen, big32)
|
||||||
adjExpLen.Mul(big8, adjExpLen)
|
adjExpLen.Lsh(adjExpLen, 3)
|
||||||
}
|
}
|
||||||
adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))
|
adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))
|
||||||
// Calculate the gas cost of the operation
|
// Calculate the gas cost of the operation
|
||||||
@ -383,8 +380,8 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
|
|||||||
// ceiling(x/8)^2
|
// ceiling(x/8)^2
|
||||||
//
|
//
|
||||||
//where is x is max(length_of_MODULUS, length_of_BASE)
|
//where is x is max(length_of_MODULUS, length_of_BASE)
|
||||||
gas = gas.Add(gas, big7)
|
gas.Add(gas, big7)
|
||||||
gas = gas.Div(gas, big8)
|
gas.Rsh(gas, 3)
|
||||||
gas.Mul(gas, gas)
|
gas.Mul(gas, gas)
|
||||||
|
|
||||||
gas.Mul(gas, math.BigMax(adjExpLen, big1))
|
gas.Mul(gas, math.BigMax(adjExpLen, big1))
|
||||||
|
Loading…
Reference in New Issue
Block a user