ctz
Count trailing zero bits
- Extension
- Zbb
- XLEN
- RV32 and RV64
- Assembly (riscv-unified-db)
ctz xd, xs1- Length
- 32-bit
Encoding
| 3120 | 1915 | 1412 | 117 | 62 | 10 | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 011000000001 | rs1 | 001 | rd | 00100 | 11 | ||||||||||||||||||||||||||
Match 0x60101013, mask 0xfff0707f. Source: ctz rd rs1 31..20=0x601 14..12=1 6..2=0x04 1..0=3 in riscv-opcodes/extensions/rv_zbb.
| Field | Bits | riscv-opcodes name |
|---|---|---|
| rs1 | 19:15 | rs1 |
| rd | 11:7 | rd |
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 (i.e., XLEN-1). Accordingly, if the input is 0, the output is XLEN, and if the least-significant bit of the input is a 1, the output is 0.
Description from riscv-unified-db, reformatted.
Operation
if (xlen() == 32) {
X[xd] = lowest_set_bit(X[xs1][31:0]);
} else {
X[xd] = lowest_set_bit(X[xs1]);
}Pseudocode in riscv-unified-db’s IDL, quoted unchanged.