Implement CLZ (Count Leading Zeros) instruction for EIP-7939 (Osaka fork)#1266
Implement CLZ (Count Leading Zeros) instruction for EIP-7939 (Osaka fork)#1266
Conversation
Co-authored-by: chfast <573380+chfast@users.noreply.github.com>
Co-authored-by: chfast <573380+chfast@users.noreply.github.com>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1266 +/- ##
==========================================
- Coverage 94.30% 93.41% -0.89%
==========================================
Files 178 179 +1
Lines 20065 20115 +50
==========================================
- Hits 18922 18791 -131
- Misses 1143 1324 +181
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
lib/evmone/instructions_traits.hpp
Outdated
| table[EVMC_FRONTIER][OP_MULMOD] = 8; | ||
| table[EVMC_FRONTIER][OP_EXP] = 10; | ||
| table[EVMC_FRONTIER][OP_SIGNEXTEND] = 5; | ||
| table[EVMC_FRONTIER][OP_CLZ] = undefined; // CLZ not available before Osaka |
There was a problem hiding this comment.
This is not needed because they are undefined by default.
There was a problem hiding this comment.
Removed the unnecessary line as it's undefined by default. Fixed in 67544ce.
lib/evmone/instructions_traits.hpp
Outdated
| table[EVMC_PRAGUE] = table[EVMC_CANCUN]; | ||
|
|
||
| table[EVMC_OSAKA] = table[EVMC_PRAGUE]; | ||
| table[EVMC_OSAKA][OP_CLZ] = 3; // CLZ available from Osaka with 3 gas cost |
There was a problem hiding this comment.
Skip the comment, this is obvious.
There was a problem hiding this comment.
Removed the obvious comment. Fixed in 67544ce.
lib/evmone/instructions.hpp
Outdated
| uint32_t leading_zeros = 0; | ||
|
|
||
| // Check each 64-bit word from most significant to least significant | ||
| for (size_t i = 4; i > 0; --i) |
There was a problem hiding this comment.
Just use the intx::clz library function.
There was a problem hiding this comment.
Replaced manual implementation with intx::clz library function. Fixed in 67544ce.
Co-authored-by: chfast <573380+chfast@users.noreply.github.com>
|
Done in #1264 . |
This PR implements the CLZ (Count Leading Zeros) instruction as specified in EIP-7939, which will be available starting from the Osaka fork.
Changes Made
Core Implementation
OP_CLZopcode (0x0c) to the instruction enum ininstructions_opcodes.hppinstructions.hppthat counts leading zero bits in a 256-bit valueinstructions_xmacro.hppto connect opcode to implementationinstructions_traits.hppwith appropriate gas cost and stack effectsInstruction Characteristics
Implementation Details
__builtin_clzllfor counting leading zeros in 64-bit wordsCLZ(0x0000...0000)returns 256 (all zeros)CLZ(0x0000...0001)returns 255 (255 leading zeros)CLZ(0x8000...0000)returns 0 (MSB set, no leading zeros)Testing
test/unittests/evm_eip7939_clz_test.cppTest Coverage
The implementation is fully functional and ready for use when the Osaka fork activates.
Fixes #1262.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.