Q168FreeSystemVerilog
Count active lanes in a predicate mask
Interview prompt
Question
Implement a combinational population-count unit for a statically sized predicate mask. The result must represent every value from zero through WIDTH.
Candidate starting point
Implementation scaffold
module population_count #(
parameter int unsigned WIDTH = 64,
localparam int unsigned COUNT_W = $clog2(64'(WIDTH) + 64'd1)
) (
input logic [WIDTH-1:0] mask,
output logic [COUNT_W-1:0] ones
);
always_comb begin : count_bits
// TODO: Implement count_bits using the supplied state and interface.
end
endmoduleReviewed example
Trace one case
Input
WIDTH=5; predicate mask=5'b10111Expected output
count=3'b100 (4 active lanes)$clog2(5+1)=3 result bits represent the all-active value five without truncating the four counted ones.
What to cover
Requirements
- Support any static WIDTH >= 1, including non-powers of two.
- Size the result with the mathematical ceil(log2(WIDTH + 1)) so the all-ones answer is representable; widen WIDTH before the addition in the parameter expression.
- Do not use a simulation-only system task as the implementation.
- Assign the accumulator on every evaluation and avoid truncated intermediate results.
