cpopw
Count set bits in word
This instruction is defined by:
-
Zbb, version >= Zbb@1.0.0
This instruction is included in the following profiles:
-
RVA22S64 (Mandatory)
-
RVA22U64 (Mandatory)
-
RVA23M64 (Mandatory)
-
RVA23S64 (Mandatory)
-
RVA23U64 (Mandatory)
-
RVB23M64 (Mandatory)
-
RVB23S64 (Mandatory)
-
RVB23U64 (Mandatory)
Synopsis
This instructions counts the number of 1’s (i.e., set bits) in the least-significant word of the source register.
Software Hint
This operations is known as population count, popcount, sideways sum, bit summation, or Hamming weight. The GCC builtin function `__builtin_popcount (unsigned int x)` is implemented by cpop on RV32 and by cpopw on RV64. The GCC builtin function `__builtin_popcountl (unsigned long x)` for LP64 is implemented by cpop on RV64.
Execution
-
IDL
-
Sail
if (%%LINK%func;implemented?;implemented?%%(ExtensionName::B) && (%%LINK%csr_field;misa.B;CSR[misa].B%% == 1'b0)) {
%%LINK%func;raise;raise%%(ExceptionCode::IllegalInstruction, %%LINK%func;mode;mode%%(), $encoding);
}
XReg bitcount = 0;
XReg rs1_val = X[rs1];
for (U32 i = 0; i < 32; i++) {
if (rs1_val[i] == 1'b1) {
bitcount = bitcount + 1;
}
}
X[rd] = bitcount;
{
let rs1_val = X(rs1);
result : nat = 0;
foreach (i from 0 to 31)
if rs1_val[i] == bitone then result = result + 1;
X(rd) = to_bits(sizeof(xlen), result);
RETIRE_SUCCESS
}