-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(vm): implement TIP-7939 CLZ opcode #6656
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import static org.tron.common.crypto.Hash.sha3; | ||
| import static org.tron.common.utils.ByteUtil.EMPTY_BYTE_ARRAY; | ||
| import static org.tron.common.utils.ByteUtil.numberOfLeadingZeros; | ||
|
|
||
| import java.math.BigInteger; | ||
| import java.util.ArrayList; | ||
|
|
@@ -287,6 +288,17 @@ public static void sarAction(Program program) { | |
| program.step(); | ||
| } | ||
|
|
||
| public static void clzAction(Program program) { | ||
| DataWord word = program.stackPop(); | ||
| int clz = numberOfLeadingZeros(word.getData()); | ||
| if (clz == 256) { | ||
| program.stackPush(new DataWord(256)); | ||
| } else { | ||
| program.stackPush(DataWord.of((byte) clz)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice work overall! One minor suggestion: since DataWord.of(byte num) directly assigns bb[31] = num, casting clz to (byte) when it's in [128, 255] introduces a subtle signed/unsigned ambiguity. Would it be worth simplifying both branches into one using new DataWord(int), which handles the full range cleanly? |
||
| } | ||
| program.step(); | ||
| } | ||
|
|
||
| public static void sha3Action(Program program) { | ||
| DataWord memOffsetData = program.stackPop(); | ||
| DataWord lengthData = program.stackPop(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not define 256 as a static constant, so that whenever
wordis 0, it is pushed directly onto the stack?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if a
256DataWord constant is defined, it still needs to be cloned when pushed onto the stack. The overhead involved is nearly equivalent to creating a new instance from scratch, so the constant approach was not adopted.