all: replace division with right shift if possible (#29911)

This commit is contained in:
gitglorythegreat 2024-06-29 00:08:31 +08:00 committed by GitHub
parent 4939c25341
commit 06f1d077d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 11 additions and 20 deletions

View File

@ -325,7 +325,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
var (
blockReward = big.NewInt(miningReward)
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 {
// 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.Sub(reward, new(big.Int).SetUint64(ommer.Delta))
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(pre.Env.Coinbase, uint256.MustFromBig(minerReward), tracing.BalanceIncreaseRewardMineBlock)

View File

@ -562,12 +562,6 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.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
// reward. The total reward consists of the static block reward and rewards for
// 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.Sub(r, hNum)
r.Mul(r, blockReward)
r.Div(r, u256_8)
r.Rsh(r, 3)
stateDB.AddBalance(uncle.Coinbase, r, tracing.BalanceIncreaseRewardMineUncle)
r.Div(blockReward, u256_32)
r.Rsh(blockReward, 5)
reward.Add(reward, r)
}
stateDB.AddBalance(header.Coinbase, reward, tracing.BalanceIncreaseRewardMineBlock)

View File

@ -572,6 +572,6 @@ func deriveChainId(v *big.Int) *big.Int {
}
return new(big.Int).SetUint64((v - 35) / 2)
}
v = new(big.Int).Sub(v, big.NewInt(35))
return v.Div(v, big.NewInt(2))
v.Sub(v, big.NewInt(35))
return v.Rsh(v, 1)
}

View File

@ -296,10 +296,7 @@ type bigModExp struct {
var (
big1 = big.NewInt(1)
big3 = big.NewInt(3)
big4 = big.NewInt(4)
big7 = big.NewInt(7)
big8 = big.NewInt(8)
big16 = big.NewInt(16)
big20 = big.NewInt(20)
big32 = big.NewInt(32)
big64 = big.NewInt(64)
@ -325,13 +322,13 @@ func modexpMultComplexity(x *big.Int) *big.Int {
case x.Cmp(big1024) <= 0:
// (x ** 2 // 4 ) + ( 96 * x - 3072)
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),
)
default:
// (x ** 2 // 16) + (480 * x - 199680)
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),
)
}
@ -369,7 +366,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
adjExpLen := new(big.Int)
if expLen.Cmp(big32) > 0 {
adjExpLen.Sub(expLen, big32)
adjExpLen.Mul(big8, adjExpLen)
adjExpLen.Lsh(adjExpLen, 3)
}
adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))
// Calculate the gas cost of the operation
@ -383,8 +380,8 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
// ceiling(x/8)^2
//
//where is x is max(length_of_MODULUS, length_of_BASE)
gas = gas.Add(gas, big7)
gas = gas.Div(gas, big8)
gas.Add(gas, big7)
gas.Rsh(gas, 3)
gas.Mul(gas, gas)
gas.Mul(gas, math.BigMax(adjExpLen, big1))