Skip to question
SystemVerilogDesignVerificationFirmwareArchitectureASIC Interview Questions→
/Interview questions/Count active lanes in a predicate mask

Q168·Free·SystemVerilog

Count active lanes in a predicate mask

Difficulty
Easy
Topic
RTL Design
Language
SV
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
endmodule
Reviewed example

Trace one case

Input
WIDTH=5; predicate mask=5'b10111
Expected 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

  1. Support any static WIDTH >= 1, including non-powers of two.
  2. 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.
  3. Do not use a simulation-only system task as the implementation.
  4. Assign the accumulator on every evaluation and avoid truncated intermediate results.
Exact question handoffPractice Q168

Solve it in the question bank, keep your progress, and reveal the reviewed solution when your access allows.

Open in question bank →
Solution accessEach time you open this Solution, one Practice Credit is used; it is not permanently unlocked. Premium Solution content also uses one credit per opening.
Continue learning

RTL Synthesis and Proof

Review widths, parameterization, memory inference, pipelining, implementation pressure, and proof evidence.

  • RTL Design
  • Population count
  • Reduction
  • Width arithmetic
RTL Synthesis and Proof →
Continue practicing

Related questions

Q1039 · RTL DesignPreserve width through dependent additionsEasyP→Q073 · RTL DatapathsDeduplicate a sorted register bankMedium→
ASIC.FYI · Learn silicon end to end.info@asic.fyi