Webcentive RISC-V Reference

ctzw

Count trailing zero bits in word

Extension
Zbb
XLEN
RV64 only
Assembly (riscv-unified-db)
ctzw xd, xs1
Length
32-bit

Encoding

3120191514121176210
011000000001rs1001rd0011011

Match 0x6010101b, mask 0xfff0707f. Source: ctzw rd rs1 31..20=0x601 14..12=1 6..2=0x06 1..0=3 in riscv-opcodes/extensions/rv64_zbb.

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

Description

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

Description from riscv-unified-db, reformatted.

Operation

# lowest_set_bit() reports xlen() (64 here) for a zero input, but ctzw must report 32
if (X[xs1][31:0] == 0) {
  X[xd] = 32;
} else {
  X[xd] = lowest_set_bit(X[xs1][31:0]);
}

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

Specification and related pages