Prev Problem
Next Problem

54. Population Count

module popcount8 (
    input  [7:0] in,
    output reg [3:0] count
);
    integer i;
    always @* begin
        count = 4'd0;
        for (i = 0; i < 8; i = i + 1) begin
            count = count + in[i];
        end
    end
endmodule

💡Remember

  • In RTL, for loops with constant bounds are compile-time unrolled into parallel hardware.
  • In this example, It creates 8 parallel adders, connected in such a way that the sum of all bits is produced.
  • Depending on the synthesis tool and optimization, this may map into a tree (more balanced, shorter delay) or a chain (longer delay).
  • Initialize outputs at the start of always @* to avoid latch inference.
  • Loop variable should be an integer (or int) and local to the block.