|
1 | 1 | # Nimbus
|
2 |
| -# Copyright (c) 2018-2024 Status Research & Development GmbH |
| 2 | +# Copyright (c) 2018-2025 Status Research & Development GmbH |
3 | 3 | # Licensed under either of
|
4 | 4 | # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
5 | 5 | # http://www.apache.org/licenses/LICENSE-2.0)
|
@@ -277,6 +277,26 @@ proc sarOp(cpt: VmCpt): EvmResultVoid =
|
277 | 277 |
|
278 | 278 | cpt.stack.binaryWithTop(sar256)
|
279 | 279 |
|
| 280 | +proc clzOp(cpt: VmCpt): EvmResultVoid = |
| 281 | + ## 0x1e, Count Leading Zeros |
| 282 | + template clz256(top, value, toStackElem) = |
| 283 | + if value.isZero: |
| 284 | + toStackElem(256.u256, top) |
| 285 | + else: |
| 286 | + var count = 0 |
| 287 | + for i in 0 ..< 32: |
| 288 | + let b = (value shr ((31 - i) * 8)).truncate(byte) |
| 289 | + if b != 0: |
| 290 | + var mask = 0x80'u8 |
| 291 | + while (b and mask) == 0: |
| 292 | + inc count |
| 293 | + mask = mask shr 1 |
| 294 | + break |
| 295 | + inc count, 8 |
| 296 | + toStackElem(count.u256, top) |
| 297 | + |
| 298 | + cpt.stack.unaryWithTop(clz256) |
| 299 | + |
280 | 300 | # ------------------------------------------------------------------------------
|
281 | 301 | # Public, op exec table entries
|
282 | 302 | # ------------------------------------------------------------------------------
|
@@ -460,7 +480,13 @@ const
|
460 | 480 | forks: VmOpConstantinopleAndLater,
|
461 | 481 | name: "sarOp",
|
462 | 482 | info: "Arithmetic shift right",
|
463 |
| - exec: sarOp)] |
| 483 | + exec: sarOp), |
| 484 | + |
| 485 | + (opCode: Clz, ## CLZ (Count Leading Zeros) |
| 486 | + forks: VmOpOsakaAndLater, ## Or a newer fork gate, if appropriate |
| 487 | + name: "clzOp", |
| 488 | + info: "Count leading zero bits in a 256-bit word", |
| 489 | + exec: clzOp)] |
464 | 490 |
|
465 | 491 | # ------------------------------------------------------------------------------
|
466 | 492 | # End
|
|
0 commit comments