CLZ
Count Leading Zeros
CLZ rd, rs1
Counts the number of 0 bits at the MSB end of the register.
Encoding
Binary Layout
011000000000
31:20
rs1
19:15
001
14:12
rd
11:7
0010011
6:0
Operands
-
rd
Destination register (integer) -
rs1
Source
Example
CLZ x10, x11
// Find highest set bit index.
Related
Across architectures
Count Leading Zeros : how x86, ARM, RISC-V, and PowerISA each do this.
More in Zbb
Reference
View in RISC-V Unprivileged ISA Specification ↗
RISC-V ISA Manual
Description
CLZ counts the number of leading (most-significant) zero bits in rs1, writing the count to rd. Returns XLEN if rs1 is zero.
Operation
int count = 0; while(rs1[XLEN-1-count] == 0) count++; R[rd] = count;