Home/Instructions/Count Leading Zeros in Word
CLZW

RISC-V CLZW Instruction Details

Instruction ManualI-type (OP-IMM-32, RV64)

Count leading zero bits in word (RV64)

Instruction Syntax

clzw rd, rs1
Operand Breakdown
rd: x0–x31; receives the count, with writes to x0 discarded.
rs1: only its low 32 bits participate; overlap with rd reads the source before writeback.
RV64-only, requiring Zbb. There is no immediate operand; imm[11:0]=011000000000 is a fixed instruction selector.
ZbbBit Manipulation

Instruction Behavior

clzw is an RV64 Zbb instruction that scans rs1 from bit 31 toward bit 0 and writes the number of zeros before the first 1 to rd.

CLZW Decode And Execute Animation

Starts with machine-code field decoding, then shows operand reads, instruction-specific execution, and ISA-visible state update.

rd
rs1
clzw
,
Execution Context

This example uses fixed initial register values and PC=0x2000; instruction fetch succeeds, with no asynchronous interrupt or extra extension checks. Exception branches stop ordinary commit; post-trap PC and handler state are EEI-dependent.

t1(x6)
0x0000000000000014
31..20
19..15
14..12
11..7
6..0
011000000000
fixed imm[11:0]
00110
rs1
001
funct3
00101
rd
0011011
opcode
Execution Data Path
instruction
0x6003129B
opcode
0011011 -> OP-IMM-32
funct3
001 -> CLZW
fixed imm[11:0]
011000000000 -> CLZW selector
rd / rs1
t0(x5) / t1(x6)=0x00000014
fixed imm[11:0]
011000000000 selects CLZW (not an arithmetic immediate)
zero-bit scan
Not computed
x5
t0(x5) = 0x0000000000003001; pc = 0x0000000000002000
Current Step

Concept Step: receive the 32-bit instruction encoding

The machine code is split by the current instruction format; the animation starts from encoding/decode.

encoding: 0x6003129B
syntax : clzw t0(x5), t1(x6)
Pending result: Not computed
Current architectural state
t0(x5) = 0x0000000000003001; pc = 0x0000000000002000

This animation shows ISA-visible decode and state changes, not any specific CPU pipeline, cache, prediction, or timing implementation.

Quick Understanding & Search Notes

clzw is an RV64 Zbb instruction that scans rs1 from bit 31 toward bit 0 and writes the number of zeros before the first 1 to rd.

CLZW is an RV64 Zbb OP-IMM-32 instruction and examines only the low 32 bits of rs1.
If the low 32 bits are zero, the result is 32, not XLEN.

Common Usage Scenarios

clzw leading-zero count

RV64 Zbb, x11=0xffffffff00000010: clzw x10, x11 examines only the low 32 bits and writes x10=27.

Pre-Use Checklist

Syntax Check
  • Confirm the current instruction format is I-type (OP-IMM-32, RV64).
  • Confirm the operand order matches the example.
Semantic Check
  • Ensure the destination register usage is compatible with the calling convention.
  • Confirm this is not the lower-level form of a pseudo-instruction expansion.

Pitfalls / Common Confusions

If the low 32 bits are zero, the result is 32, not XLEN.

FAQ

Does clzw access memory?

No. It reads integer register operands and writes rd only.

How does clzw handle a zero input?

The count is 32. Only the low 32 bits are examined; upper 32 bits do not participate.