ctzw

Count trailing zero bits in word

This instruction is defined by:

Encoding

svg

Synopsis

This instruction 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.

Access

M

HS

U

VS

VU

Always

Always

Always

Always

Always

Decode Variables

Bits<5> rs1 = $encoding[19:15];
Bits<5> rd = $encoding[11:7];

Execution

  • Pruned, XLEN == 64

  • Original

X[rd] = (63) - $signed(lowest_set_bit(X[rs1][31:0]));
if (implemented?(ExtensionName::B) && (CSR[misa].B == 1'b0)) {
  raise(ExceptionCode::IllegalInstruction, mode(), $encoding);
}
X[rd] = (xlen() - 1) - $signed(lowest_set_bit(X[rs1][31:0]));