Home/Instructions/Count Population
CPOP

RISC-V CPOP Instruction Details

Instruction ManualR-type

Count set bits (population count)

Instruction Syntax

cpop rd, rs1
Operand Breakdown
Destination rd: register receiving the operation result.
Source rs1: register holding the first operand.
Source rs2: register holding the second operand.
BZbbBit ManipulationInteger Operation

Instruction Encoding

31..20
imm[11:0]
19..15
rs1
14..12
funct3
11..7
rd
6..0
opcode

CPOP uses opcode 0010011 (0x13), funct3 001, funct7 0110000. The rs1 field selects the source register, the 12-bit immediate provides the second operand, and rd selects the destination.

Format: I-type
opcode: 0010011 (0x13)
funct3: 001 (0x1)
funct7: 0110000 (0x30)

Instruction Behavior

cpop returns the number of one bits in rs1.

Quick Understanding & Search Notes

cpop returns the number of one bits in rs1.

These instructions are part of a B-extension subset and operate on XLEN-wide integer register values.
The result range is 0..XLEN.

Common Usage Scenarios

Arrays & Memory Access

Understand this scenario with real code like «cpop x10, x11 ; x10 = number of 1-bits in x11».

Comparison & Detection

Understand this scenario with real code like «cpop x10, x11 ; x10 = number of 1-bits in x11».

Debug & Monitoring

Understand this scenario with real code like «cpop x10, x11 ; x10 = number of 1-bits in x11».

Pre-Use Checklist

Syntax Check
  • Confirm the current instruction format is R-type.
  • Confirm the operand order matches the example.
Semantic Check
  • Ensure the destination register usage is compatible with the calling convention.
  • Confirm this is not the lower-level form of a pseudo-instruction expansion.

Pitfalls / Common Confusions

The result range is 0..XLEN.

FAQ

Does cpop access memory?

No. It reads integer register operands and writes rd only.

Does cpop interpret signed numbers?

These bit operations work on bit patterns directly; aside from the specified word-width selection, arithmetic signed magnitude is not used.