← Home

cpopw

Count set bits in word

Counts the number of 1's (i.e., set bits) in the least-significant word of the source register.

.Software Hint Note: 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.

Assembly format

cpopw xd, xs1

Encoding

313029282726252423222120191817161514131211109876543210
011000000010xs1001xd0011011

Decode variables

VariableBitsWidth
xs1[19:15]5
xd[11:7]5

Execution

XReg bitcount = 0;
XReg xs1_val = X[xs1];

for (U32 i=0; i < 32; i++) {
  if (xs1_val[i] == 1'b1) {
    bitcount = bitcount + 1;
  }
}

X[xd] = bitcount;

Defined by

RV64 and Zbb

Access

SUVSVU
AlwaysAlwaysAlwaysAlways

Version af3eaf9+cheriot.f635218

Data from the RISC-V Unified Database, copyright RISC-V International and contributors.

CHERIoT extensions from the CHERIoT specification and Sail model, copyright Microsoft and the CHERIoT Platform contributors.

This site is provided as a community resource and is NOT affiliated with nor endorsed by RISC-V International.