-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add SHL, SHR and SAR shift operations for EVM v2 #10154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
60fdfe0
Add SHL, SHR and SAR implementations and benchmarks for EVM v2
ahamlat 1dd435b
Use more accurate benchmarks on shift operations
ahamlat 3cac741
Merge branch 'main' into stack_artithmetic
ahamlat 45f61e5
Use existing implementation on SAR, SHR and SHL
ahamlat 531a87b
Add unit tests.
ahamlat 9b88b6d
Address review comments.
ahamlat 189a3c8
Add property based testing for v2 shift operations
ahamlat 9ccfea9
spotless
ahamlat fdcdb26
Merge branch 'main' into stack_artithmetic
ahamlat 0b688ff
Fix javadoc
ahamlat c4c7c3d
address comments.
ahamlat 5e1eb9f
Merge branch 'main' into stack_artithmetic
ahamlat 2c7fade
Merge branch 'main' into stack_artithmetic
ahamlat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
.../java/org/hyperledger/besu/ethereum/vm/operations/v2/AbstractSarOperationBenchmarkV2.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /* | ||
| * Copyright contributors to Besu. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.vm.operations.v2; | ||
|
|
||
| import static org.hyperledger.besu.ethereum.vm.operations.v2.BenchmarkHelperV2.bytesToUInt256; | ||
| import static org.hyperledger.besu.ethereum.vm.operations.v2.BenchmarkHelperV2.randomNegativeUInt256Value; | ||
| import static org.hyperledger.besu.ethereum.vm.operations.v2.BenchmarkHelperV2.randomPositiveUInt256Value; | ||
| import static org.hyperledger.besu.ethereum.vm.operations.v2.BenchmarkHelperV2.randomUInt256Value; | ||
|
|
||
| import org.hyperledger.besu.evm.UInt256; | ||
|
|
||
| import java.util.concurrent.ThreadLocalRandom; | ||
|
|
||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
|
|
||
| /** | ||
| * Abstract base class for SAR (Shift Arithmetic Right) operation benchmarks. | ||
| * | ||
| * <p>SAR has additional test cases for negative/positive values to test sign extension behavior. | ||
| */ | ||
| public abstract class AbstractSarOperationBenchmarkV2 extends BinaryOperationBenchmarkV2 { | ||
|
|
||
| /** Test cases covering different execution paths for SAR operations. */ | ||
| public enum Case { | ||
| /** Shift by 0 - early return path. */ | ||
| SHIFT_0, | ||
| /** Negative number (ALL_BITS) with shift=1 - tests sign extension OR path. */ | ||
| NEGATIVE_SHIFT_1, | ||
| /** value with all bits to 1 with shift=1 * */ | ||
| ALL_BITS_SHIFT_1, | ||
| /** Positive number with shift=1 - no sign extension needed. */ | ||
| POSITIVE_SHIFT_1, | ||
| /** Negative number with medium shift. */ | ||
| NEGATIVE_SHIFT_128, | ||
| /** Negative number with max shift. */ | ||
| NEGATIVE_SHIFT_255, | ||
| /** Positive number with medium shift. */ | ||
| POSITIVE_SHIFT_128, | ||
| /** positive number with max shift. */ | ||
| POSITIVE_SHIFT_255, | ||
| /** Overflow: shift >= 256. */ | ||
| OVERFLOW_SHIFT_256, | ||
| /** Overflow: shift amount > 4 bytes. */ | ||
| OVERFLOW_LARGE_SHIFT, | ||
| /** Random values (original behavior). */ | ||
| FULL_RANDOM | ||
| } | ||
|
|
||
| @Param({ | ||
| "SHIFT_0", | ||
| "NEGATIVE_SHIFT_1", | ||
| "POSITIVE_SHIFT_1", | ||
| "ALL_BITS_SHIFT_1", | ||
| "NEGATIVE_SHIFT_128", | ||
| "NEGATIVE_SHIFT_255", | ||
| "POSITIVE_SHIFT_128", | ||
| "POSITIVE_SHIFT_255", | ||
| "OVERFLOW_SHIFT_256", | ||
| "OVERFLOW_LARGE_SHIFT", | ||
| "FULL_RANDOM" | ||
| }) | ||
| protected String caseName; | ||
|
|
||
| @Setup(Level.Iteration) | ||
| @Override | ||
| public void setUp() { | ||
| frame = BenchmarkHelperV2.createMessageCallFrame(); | ||
|
|
||
| final Case scenario = Case.valueOf(caseName); | ||
| aPool = new UInt256[SAMPLE_SIZE]; // shift amount (pushed second, popped first) | ||
| bPool = new UInt256[SAMPLE_SIZE]; // value (pushed first, popped second) | ||
|
|
||
| final ThreadLocalRandom random = ThreadLocalRandom.current(); | ||
|
|
||
| for (int i = 0; i < SAMPLE_SIZE; i++) { | ||
| switch (scenario) { | ||
| case SHIFT_0: | ||
| aPool[i] = UInt256.fromInt(0); | ||
| bPool[i] = randomUInt256Value(random); | ||
| break; | ||
|
|
||
| case NEGATIVE_SHIFT_1: | ||
| aPool[i] = UInt256.fromInt(1); | ||
| bPool[i] = randomNegativeUInt256Value(random); | ||
| break; | ||
|
|
||
| case ALL_BITS_SHIFT_1: | ||
| aPool[i] = UInt256.fromInt(1); | ||
| bPool[i] = UInt256.MAX; | ||
| break; | ||
|
|
||
| case POSITIVE_SHIFT_1: | ||
| aPool[i] = UInt256.fromInt(1); | ||
| bPool[i] = randomPositiveUInt256Value(random); | ||
| break; | ||
|
|
||
| case NEGATIVE_SHIFT_128: | ||
| aPool[i] = UInt256.fromInt(128); | ||
| bPool[i] = randomNegativeUInt256Value(random); | ||
| break; | ||
|
|
||
| case NEGATIVE_SHIFT_255: | ||
| aPool[i] = UInt256.fromInt(255); | ||
| bPool[i] = randomNegativeUInt256Value(random); | ||
| break; | ||
|
|
||
| case POSITIVE_SHIFT_128: | ||
| aPool[i] = UInt256.fromInt(128); | ||
| bPool[i] = randomPositiveUInt256Value(random); | ||
| break; | ||
| case POSITIVE_SHIFT_255: | ||
| aPool[i] = UInt256.fromInt(255); | ||
| bPool[i] = randomPositiveUInt256Value(random); | ||
| break; | ||
|
|
||
| case OVERFLOW_SHIFT_256: | ||
| aPool[i] = UInt256.fromInt(256); | ||
| bPool[i] = randomUInt256Value(random); | ||
| break; | ||
|
|
||
| case OVERFLOW_LARGE_SHIFT: | ||
| aPool[i] = bytesToUInt256(new byte[] {0x01, 0x00, 0x00, 0x00, 0x00, 0x00}); | ||
| bPool[i] = randomUInt256Value(random); | ||
| break; | ||
|
|
||
| case FULL_RANDOM: | ||
| default: | ||
| final byte[] shift = new byte[1 + random.nextInt(2)]; | ||
| final byte[] value = new byte[1 + random.nextInt(32)]; | ||
| random.nextBytes(shift); | ||
| random.nextBytes(value); | ||
| aPool[i] = bytesToUInt256(shift); | ||
| bPool[i] = bytesToUInt256(value); | ||
| break; | ||
| } | ||
| } | ||
| index = 0; | ||
| } | ||
| } | ||
120 changes: 120 additions & 0 deletions
120
...ava/org/hyperledger/besu/ethereum/vm/operations/v2/AbstractShiftOperationBenchmarkV2.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Copyright contributors to Besu. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.vm.operations.v2; | ||
|
|
||
| import static org.hyperledger.besu.ethereum.vm.operations.v2.BenchmarkHelperV2.bytesToUInt256; | ||
| import static org.hyperledger.besu.ethereum.vm.operations.v2.BenchmarkHelperV2.randomUInt256Value; | ||
|
|
||
| import org.hyperledger.besu.evm.UInt256; | ||
|
|
||
| import java.util.concurrent.ThreadLocalRandom; | ||
|
|
||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
|
|
||
| /** | ||
| * Abstract base class for shift operation benchmarks (SHL, SHR, SAR). | ||
| * | ||
| * <p>Provides shared test case definitions and setup logic. | ||
| */ | ||
| public abstract class AbstractShiftOperationBenchmarkV2 extends BinaryOperationBenchmarkV2 { | ||
|
|
||
| /** Test cases covering different execution paths for shift operations. */ | ||
| public enum Case { | ||
| /** Shift by 0 - no shift needed. */ | ||
| SHIFT_0, | ||
| /** Small shift by 1 bit. */ | ||
| SHIFT_1, | ||
| /** Medium shift by 128 bits (half word). */ | ||
|
siladu marked this conversation as resolved.
Outdated
|
||
| SHIFT_128, | ||
| /** Large shift by 255 bits (max valid). */ | ||
| SHIFT_255, | ||
| /** Overflow: shift of exactly 256. */ | ||
| OVERFLOW_SHIFT_256, | ||
| /** Overflow: shift amount > 4 bytes. */ | ||
| OVERFLOW_LARGE_SHIFT, | ||
| /** Random values (original behavior). */ | ||
| FULL_RANDOM | ||
| } | ||
|
|
||
| @Param({ | ||
| "SHIFT_0", | ||
| "SHIFT_1", | ||
| "SHIFT_128", | ||
| "SHIFT_255", | ||
| "OVERFLOW_SHIFT_256", | ||
| "OVERFLOW_LARGE_SHIFT", | ||
| "FULL_RANDOM" | ||
| }) | ||
| protected String caseName; | ||
|
|
||
| @Setup(Level.Iteration) | ||
| @Override | ||
| public void setUp() { | ||
| frame = BenchmarkHelperV2.createMessageCallFrame(); | ||
|
|
||
| final Case scenario = Case.valueOf(caseName); | ||
| aPool = new UInt256[SAMPLE_SIZE]; // shift amount (pushed second, popped first) | ||
| bPool = new UInt256[SAMPLE_SIZE]; // value (pushed first, popped second) | ||
|
|
||
| final ThreadLocalRandom random = ThreadLocalRandom.current(); | ||
|
|
||
| for (int i = 0; i < SAMPLE_SIZE; i++) { | ||
| switch (scenario) { | ||
| case SHIFT_0: | ||
| aPool[i] = UInt256.ZERO; | ||
| bPool[i] = randomUInt256Value(random); | ||
| break; | ||
|
|
||
| case SHIFT_1: | ||
| aPool[i] = UInt256.ONE; | ||
| bPool[i] = randomUInt256Value(random); | ||
| break; | ||
|
|
||
| case SHIFT_128: | ||
| aPool[i] = UInt256.fromInt(128); | ||
| bPool[i] = randomUInt256Value(random); | ||
| break; | ||
|
|
||
| case SHIFT_255: | ||
| aPool[i] = UInt256.fromInt(255); | ||
| bPool[i] = randomUInt256Value(random); | ||
| break; | ||
|
|
||
| case OVERFLOW_SHIFT_256: | ||
| aPool[i] = UInt256.fromInt(256); // 256 | ||
| bPool[i] = randomUInt256Value(random); | ||
| break; | ||
|
|
||
| case OVERFLOW_LARGE_SHIFT: | ||
| aPool[i] = bytesToUInt256(new byte[] {0x01, 0x00, 0x00, 0x00, 0x00, 0x00}); | ||
| bPool[i] = randomUInt256Value(random); | ||
| break; | ||
|
|
||
| case FULL_RANDOM: | ||
| default: | ||
| final byte[] shift = new byte[1 + random.nextInt(4)]; | ||
| final byte[] value = new byte[1 + random.nextInt(32)]; | ||
| random.nextBytes(shift); | ||
| random.nextBytes(value); | ||
| aPool[i] = bytesToUInt256(shift); | ||
| bPool[i] = bytesToUInt256(value); | ||
| break; | ||
| } | ||
| } | ||
| index = 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
.../src/jmh/java/org/hyperledger/besu/ethereum/vm/operations/v2/SarOperationBenchmarkV2.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright contributors to Besu. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.vm.operations.v2; | ||
|
|
||
| import org.hyperledger.besu.evm.frame.MessageFrame; | ||
| import org.hyperledger.besu.evm.operation.Operation; | ||
| import org.hyperledger.besu.evm.v2.operation.SarOperationV2; | ||
|
|
||
| public class SarOperationBenchmarkV2 extends AbstractSarOperationBenchmarkV2 { | ||
|
|
||
| @Override | ||
| protected Operation.OperationResult invoke(final MessageFrame frame) { | ||
| return SarOperationV2.staticOperation(frame, frame.stackDataV2()); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
.../src/jmh/java/org/hyperledger/besu/ethereum/vm/operations/v2/ShlOperationBenchmarkV2.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright contributors to Besu. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.vm.operations.v2; | ||
|
|
||
| import org.hyperledger.besu.evm.frame.MessageFrame; | ||
| import org.hyperledger.besu.evm.operation.Operation; | ||
| import org.hyperledger.besu.evm.v2.operation.ShlOperationV2; | ||
|
|
||
| public class ShlOperationBenchmarkV2 extends AbstractShiftOperationBenchmarkV2 { | ||
|
|
||
| @Override | ||
| protected Operation.OperationResult invoke(final MessageFrame frame) { | ||
| return ShlOperationV2.staticOperation(frame, frame.stackDataV2()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.