Webcentive RISC-V Reference

clz

Count leading zero bits

Extension
Zbb
XLEN
RV32 and RV64
Assembly (riscv-unified-db)
clz xd, xs1
Length
32-bit

Encoding

3120191514121176210
011000000000rs1001rd0010011

Match 0x60001013, mask 0xfff0707f. Source: clz rd rs1 31..20=0x600 14..12=1 6..2=0x04 1..0=3 in riscv-opcodes/extensions/rv_zbb.

FieldBitsriscv-opcodes name
rs119:15rs1
rd11:7rd

Description

Counts the number of 0's before the first 1, starting at the most-significant bit (i.e., XLEN-1) and progressing to bit 0. Accordingly, if the input is 0, the output is XLEN, and if the most-significant bit of the input is a 1, the output is 0.

Description from riscv-unified-db, reformatted.

Operation

X[xd] = (xlen() - 1) - $signed(highest_set_bit(X[xs1]));

Pseudocode in riscv-unified-db’s IDL, quoted unchanged.

Specification and related pages