Altifigence Academy

2 / 20 · Concept

Signed products and accumulator width

Derive an accumulator width from the worst-case operand range.

Include the most negative input

A W-bit two's-complement input ranges from −2^(W−1) to 2^(W−1)−1. The largest positive product occurs when both inputs are the most negative value. For W=8, (−128)×(−128)=16384, which exceeds 127×127=16129. A full signed product of two W-bit inputs is safely represented in 2W bits.

For a reduction containing K terms, this course chooses the conservative rule below. K must be positive; the rule assumes equal operand widths and no additional bias.

Wacc=2W+⌈log⁡2K⌉W_{acc}=2W+\lceil\log_2 K\rceil

The rule is a simple safe bound, not a claim of minimum width for every K. With W=8 and K=4, choose eighteen bits. Four maximum products sum to 65536. A signed seventeen-bit accumulator stops at 65535, so it fails precisely at this boundary. Eighteen bits cover −131072 through 131071. When K=1, no extra accumulation bits are added.

Preserve the sign explicitly

Declare intermediate signals rather than guessing expression sizing. AW must be at least PW.

SystemVerilog
localparam int PW = 2*W;
localparam int AW = PW + $clog2(K);
logic signed [W-1:0] a, b;
logic signed [PW-1:0] product;
logic signed [AW-1:0] addend, acc;
assign product = $signed(a) * $signed(b);
assign addend = {{(AW-PW){product[PW-1]}}, product};

An eight-bit pattern 0xFE represents −2. Multiplying by three gives −6; widening that product requires sign extension. Zero extension changes a negative number into a positive number. Document the point where an unsigned interface bit pattern becomes a signed arithmetic value.

The width depends on the complete reduction retained in the accumulator. Splitting K into tiles does not justify narrowing a partial accumulator if it can overflow before combination. Adding a bias or combining multiple reductions requires a new range analysis.

Test minimum×minimum, minimum×maximum, maximum×maximum and alternating signs. Keep both the wide integer reference and the chosen-width result. Exact accumulation within the specified range and wraparound or saturation outside it are separate contracts; treating all mismatches as generic overflow hides real sign errors.

Try it yourself

For W=4 and K=3, find the conservative AW and maximum positive sum. Which bit extends the product −8×7?

Read the explanation

PW=8 and ceil(log2 3)=2, so AW=10. The maximum positive sum is 3×64=192. The product is −56 and must be extended with ones. Ten signed bits cover −512..511.

Your choice applies to this browser. Change it any time using the footer.