Skip to content

Implement CLZ (Count Leading Zeros) instruction for EIP-7939 (Osaka fork)#1266

Closed
Copilot wants to merge 4 commits intomasterfrom
copilot/fix-1262
Closed

Implement CLZ (Count Leading Zeros) instruction for EIP-7939 (Osaka fork)#1266
Copilot wants to merge 4 commits intomasterfrom
copilot/fix-1262

Conversation

Copy link
Contributor

Copilot AI commented Jul 18, 2025

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

  • Added OP_CLZ opcode (0x0c) to the instruction enum in instructions_opcodes.hpp
  • Implemented CLZ instruction in instructions.hpp that counts leading zero bits in a 256-bit value
  • Added X-macro mapping in instructions_xmacro.hpp to connect opcode to implementation
  • Added instruction traits in instructions_traits.hpp with appropriate gas cost and stack effects

Instruction Characteristics

  • Opcode: 0x0c
  • Gas cost: 3 (similar to other arithmetic operations)
  • Stack effect: Takes 1 input, returns 1 output (net change: 0)
  • Availability: From EVMC_OSAKA revision onwards
  • Functionality: Returns the number of leading zero bits (0-256) in a 256-bit number

Implementation Details

  • Uses efficient __builtin_clzll for counting leading zeros in 64-bit words
  • Handles all edge cases correctly:
    • CLZ(0x0000...0000) returns 256 (all zeros)
    • CLZ(0x0000...0001) returns 255 (255 leading zeros)
    • CLZ(0x8000...0000) returns 0 (MSB set, no leading zeros)
  • Proper type casting to avoid compiler warnings
  • Follows existing instruction patterns in the codebase

Testing

  • Added comprehensive test suite in test/unittests/evm_eip7939_clz_test.cpp
  • 27 test cases covering various bit patterns and edge cases
  • All 1524 unit tests pass including the new CLZ tests
  • Updated instruction compatibility for test framework

Test Coverage

// Example test cases
TEST_P(evm, clz_zero)           // CLZ(0) = 256
TEST_P(evm, clz_one)            // CLZ(1) = 255  
TEST_P(evm, clz_msb_set)        // CLZ(0x8000...0000) = 0
TEST_P(evm, clz_bit_128_set)    // CLZ(0x0000...0100...0000) = 127
TEST_P(evm, clz_stack_underflow) // Error handling

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.

Copilot AI and others added 2 commits July 18, 2025 18:08
Co-authored-by: chfast <573380+chfast@users.noreply.github.com>
Co-authored-by: chfast <573380+chfast@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for EIP-7939 CLZ (Osaka fork) Implement CLZ (Count Leading Zeros) instruction for EIP-7939 (Osaka fork) Jul 18, 2025
Copilot AI requested a review from chfast July 18, 2025 18:12
@codecov
Copy link

codecov bot commented Jul 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.41%. Comparing base (027a899) to head (67544ce).
Report is 9 commits behind head on master.

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     
Flag Coverage Δ
eest_gmp 15.95% <18.75%> (+<0.01%) ⬆️
eof_execution_spec_tests 19.85% <18.00%> (-0.01%) ⬇️
ethereum_tests_silkpre 18.56% <18.75%> (-0.01%) ⬇️
execution_spec_tests ?
fusaka_execution_spec_tests ?
unittests 91.75% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
lib/evmone/instructions.hpp 100.00% <100.00%> (ø)
test/unittests/evm_eip7939_clz_test.cpp 100.00% <100.00%> (ø)
test/unittests/instructions_test.cpp 89.58% <ø> (ø)

... and 17 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed because they are undefined by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the unnecessary line as it's undefined by default. Fixed in 67544ce.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skip the comment, this is obvious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the obvious comment. Fixed in 67544ce.

uint32_t leading_zeros = 0;

// Check each 64-bit word from most significant to least significant
for (size_t i = 4; i > 0; --i)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use the intx::clz library function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced manual implementation with intx::clz library function. Fixed in 67544ce.

Co-authored-by: chfast <573380+chfast@users.noreply.github.com>
@chfast
Copy link
Member

chfast commented Jul 22, 2025

Done in #1264 .

@chfast chfast closed this Jul 22, 2025
@chfast chfast deleted the copilot/fix-1262 branch July 22, 2025 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for EIP-7939 CLZ (Osaka fork)

2 participants