§ 6.1. Arithmetic¶
The unit needs one multiply-add per cell per clock edge. This section builds the parts that make that possible: a multiplier that finishes in one combinational pass, the same multiplier as an option inside the RV32IM core, the int8 multiply-add cell, and the requantizer that turns 32-bit sums back into bytes.
| Component | Description | Structural | Behavioral |
|---|---|---|---|
ArrayMultiplier<W> |
W × W product in one pass: partial products, a Dadda tree, a prefix adder | Yes | Yes (compiled gate list) |
ArrayMulDivUnit<W> |
MulDivUnit<W> with the array multiplier: multiply in 2 edges, divide still iterative |
Yes | Yes (compiled gate list) |
MacCell |
int8 × int8 added into int32, one weight register, registered outputs | Yes | Yes (compiled gate list) |
Requantizer |
int32 → int8: scale, round, shift, clamp, ReLU | Yes | Yes (host arithmetic on known values) |
Catalog: arithmetic.array-multiplier.W and arithmetic.array-mul-div.W
for W = 4, 8, 16, 32; accelerator.mac-cell; accelerator.requantize.
The 4-bit forms are small enough to inspect bit by bit; the core uses W = 32
and the matrix unit the 8-bit tree inside MacCell.
6.1.1. ArrayMultiplier<W>¶
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
A, B |
Input | W each | Operands |
SIGNED_A, SIGNED_B |
Input | 1 each | Read that operand as two's complement |
LO, HI |
Output | W each | Low and high halves of the 2W-bit product |
The sign inputs are those of the iterative Multiplier<W>
(§ 5.1.1), so the four RV32IM multiplies map
onto them in the same way: MUL and MULHU unsigned, MULH both signed,
MULHSU A signed.
Behavior¶
Combinational: {HI, LO} settles to A × B, each operand read as signed or
unsigned as its flag says. The result is exact; nothing is truncated.
C++ reference¶
#include <cstdint>
#include <utility>
// LO and HI of A × B at width W (4, 8, 16 or 32).
template<unsigned W>
std::pair<uint32_t, uint32_t> array_multiply_ref(uint32_t a, uint32_t b, bool signed_a, bool signed_b) {
static_assert(W == 4 || W == 8 || W == 16 || W == 32);
constexpr uint64_t mask = (uint64_t{1} << W) - 1, sign = uint64_t{1} << (W - 1);
auto value = [&](uint64_t v, bool is_signed) {
v &= mask;
return (is_signed && (v & sign)) ? int64_t(v) - int64_t(mask + 1) : int64_t(v);
};
const uint64_t p = uint64_t(value(a, signed_a) * value(b, signed_b));
return {uint32_t(p & mask), uint32_t((p >> W) & mask)}; // LO, HI
}
A binary settled-value reference. ArrayMultiplierTest checks the circuit
against an independent 128-bit form of the same rule.
Unknown inputs and timing¶
The gates follow the usual four-state rules: a known-zero bit of B masks
its unsigned partial-product row, and unknown bits make the columns they
reach unknown. SystolicArrayTest checks the case the unit relies on: an
unknown input times a zero weight gives 0 through the 8-bit signed tree
(6.2). There is no clock. The settling time is the
longest path through the tree and the final adder (see the comparison
below).
Implementation¶
Structural implementation¶
The multiplier is built the way a hardware multiplier usually is, in three layers:
- Partial products. For each bit
b_jof B, one word AND gate formsA AND b_j, the row of A shifted to column j. A W-bit product has W such rows. - Reduction tree. A Dadda tree of full and half adders squeezes the columns of bits down to two rows. Each stage reduces every column to the next height of the sequence 2, 3, 4, 6, 9, 13, 19, 28, … using as few adders as possible; a full adder takes three bits of a column and gives one back plus a carry into the next column.
- Final adder. The two remaining rows are added by the existing parallel-prefix adders (§ 2.1): 8, 16 or 32 bits at a time, carries chained between them.
Signed operands (Baugh-Wooley). Each operand gets one extra bit, its
sign s = SIGNED and top bit, worth −2^W. The products of a sign bit with an
ordinary bit are negative; they enter the tree inverted (NAND), and a
constant one at column W for each signed operand corrects the sum.
The sign-times-sign term and the corrections above bit 2W fall outside the
result. Unsigned operands give s = 0 and the same tree.
| Part | Gates |
|---|---|
| Partial products | W word AND gates of width W; two inverted rows for the signs |
HA_s_c_k, FA_s_c_k |
The Dadda stages: stage s, column c, cell k |
FINAL_ADD_k |
Prefix adders for the last two rows |
Behavioral implementation¶
The compact form runs the same gates as one compiled gate list, so it is exact for every input, X and Z included. Its delays are proven on those gates when it is first built. No host multiplication runs inside it.
Iterative versus array multiplier¶
The chapter-5 multiplier and this one compute the same products. They differ in when the answer is there, and in how many gates that costs:
| Multiplier | Gates and storage leaves | Clock edges per product | Settling time, embedded (65 nm) |
|---|---|---|---|
Multiplier<8> (iterative) |
1,334 | 9 | one step per edge |
ArrayMultiplier<8> |
605 | combinational | 1,672 ps |
Multiplier<16> |
2,585 | 17 | one step per edge |
ArrayMultiplier<16> |
2,049 | combinational | 2,242 ps |
Multiplier<32> |
5,168 | 33 | one step per edge |
ArrayMultiplier<32> |
7,160 | combinational | 2,704 ps |
MulDivUnit<32> |
11,572 | 33 (multiply and divide) | |
ArrayMulDivUnit<32> |
16,360 | 2 (multiply), 33 (divide) |
Counts are the primitive gates and storage cells of the all-gates build (the iterative units' registers are built from gates too); the settling time is the timing analyzer's latest input-to-output path. At 32 bits the array is about 40 % larger than the iterative multiplier and finishes in 2.7 ns, well inside the 4.5 ns clock of chapter 5, instead of 33 clock edges (about 150 ns at that clock). From 8 to 32 bits its delay grows by 1.6× while its width grows 4× (the tree's depth grows with log W).
The iterative one reuses one adder W times, one bit per clock edge; the
array spends a separate adder for nearly every partial-product bit. The
numbers come from accelerator_experiment --parts (2026-11-10).
Examples¶
Unsigned, signed and mixed products at W = 4, including the most negative
operand (−8 × −8 = 64) and the largest unsigned one (15 × 15 = 225).
Checkpoint names are AxB with each operand's value as its sign flag reads
it (the bits 1111 are −1 when signed), followed by u or s for each
operand.
All 8 recorded rows of ArrayMultiplierWidth4ScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | AInput · 4 bit |
BInput · 4 bit |
SIGNED_AInput · 1 bit |
SIGNED_BInput · 1 bit |
HIExpected output · 4 bit |
LOExpected output · 4 bit |
|---|---|---|---|---|---|---|---|
#07x5uu | 15 | 0x7 |
0x5 |
0 |
0 |
0x2 |
0x3 |
#115x15uu | 27 | 0xF |
0xF |
0 |
0 |
0xE |
0x1 |
#2-1x-1ss | 43 | 0xF |
0xF |
1 |
1 |
0x0 |
0x1 |
#3-8x7ss | 56 | 0x8 |
0x7 |
1 |
1 |
0xC |
0x8 |
#4-8x-8ss | 69 | 0x8 |
0x8 |
1 |
1 |
0x4 |
0x0 |
#5-7x3su | 82 | 0x9 |
0x3 |
1 |
0 |
0xE |
0xB |
#612x-3us | 96 | 0xC |
0xD |
0 |
1 |
0xD |
0xC |
#70x-5ss | 109 | 0x0 |
0xB |
1 |
1 |
0x0 |
0x0 |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
6.1.2. ArrayMulDivUnit<W>¶
MulDivUnit<W> (§ 5.1.1.5)
with its iterative multiplier replaced by an array multiply unit.
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
A |
Input | W | rs1 value |
B |
Input | W | rs2 value |
CANCEL |
Input | 1 | Synchronously discard the operation, even while paused |
CLK |
Input | 1 | Rising edges launch and step the operation |
ENABLE |
Input | 1 | Low pauses iteration and completion |
OP |
Input | 3 | Operation: instruction funct3 (0–3 multiplies, 4–7 divides) |
RST |
Input | 1 | Active-high reset: discards any operation |
START |
Input | 1 | Launch request |
TAKE |
Input | 1 | Consume a completed result |
BUSY |
Output | 1 | An operation is in progress |
DONE |
Output | 1 | A result is held |
READY |
Output | 1 | A launch would be accepted |
RESULT |
Output | W | Selected result |
The pins, OP codes and handshake are those of MulDivUnit<W>: START
with OP and the operands while READY, DONE when the result is on
RESULT, TAKE to hand it over; CANCEL and ENABLE as before.
Behavior¶
| Edge | Multiply (OP 0–3) | Divide (OP 4–7) |
|---|---|---|
| Launch | Operands and sign flags are registered | Operands are registered |
| Next edge | The array multiplier's product is registered; DONE |
First of W division steps |
| W-th edge after launch | Quotient or remainder ready; DONE |
So a multiply takes 2 edges instead of W + 1. Divisions keep the
iterative divider (W + 1 edges), as most processors do: a one-pass divider
is far larger than a multiplier. As in MulDivUnit<W>, READY is low while
DONE is high, so taking a result and launching the next operation need
separate edges.
The core's multiplier option. The RV32IM core
(§ 5.1.3) takes this unit with the parameter
multiplier=array (default iterative). The core's other parts and its
handshake with the M unit are unchanged; only the number of edges a
multiply holds the pipeline differs. The compact core compiles the core's
own gates, so the option holds in both forms.
Unknown inputs and timing¶
As for MulDivUnit<W>: the results are specified for known operands and
controls after reset. Four-state values still flow through the gates; no
numerical result is promised for unknown operands. The multiply's
combinational path (the whole array multiplier) lies between two registers,
so it must settle within one clock period: at 32 bits, 2.7 ns in the
embedded profile.
Implementation¶
Structural implementation¶
| Part | Type | Responsibility |
|---|---|---|
MULTIPLY |
ArrayMultiplyUnit<W> |
Below |
DIVIDE |
Divider<W> |
The iterative divider of § 5.1.1 |
OPERATION |
Register<3> |
OP, captured at the launch for the result selection |
RESULT_SELECTION |
MulDivResultSelection<W> |
LO, HI, quotient or remainder by the saved OP, as in MulDivUnit<W> |
| Loose gates | READY (both engines ready), the launch, the sign flags from OP, BUSY and DONE (either engine) |
Exactly one engine takes each launch: OP bit 2 picks the divider.
ArrayMultiplyUnit<W> (MULTIPLY)¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
CLK, RST, ENABLE, CANCEL, START, TAKE |
Input | 1 | The handshake of the iterative Multiplier<W> |
A, B |
Input | W | Operands |
SIGNED_A, SIGNED_B |
Input | 1 | Sign flags, from OP |
READY, BUSY, DONE |
Output | 1 | As in Multiplier<W> |
LO, HI |
Output | W | The registered product |
The launch edge registers the operands and sign flags (OPERAND_A,
OPERAND_B, SIGNED_A_STATE, SIGNED_B_STATE) and sets BUSY_STATE; the
next edge registers the ARRAY multiplier's halves in PRODUCT_LOW and
PRODUCT_HIGH and moves BUSY_STATE into DONE_STATE, which holds until
TAKE. It is the iterative control with a single step. READY is low
while RST or CANCEL is high or an operation is busy or done; CANCEL
clears both state bits at the next edge, even with ENABLE low.
Behavioral implementation¶
The compact form runs the unit's own gates as one compiled gate list, with its registers as characterized state and delays proven on the same gates. It keeps the 2-edge and W + 1-edge latencies and the handshake; it is not a host multiplication.
Examples¶
At W = 4: MUL 7 × 5, MULH −3 × 5, MULHSU −8 × 3, MULHU 15 × 15
(each launched, done one edge later, then taken), and DIV −7 ÷ 2 = −3,
whose result appears four edges after its launch edge (each step is a
row). Then the divide's edge cases, each launched, done and taken: DIVU
9 ÷ 0 = 15 (all ones, as RISC-V defines division by zero), REM −7 mod 3
= −1 (the sign of the dividend), and DIV −8 ÷ −1 = −8 (the one signed
overflow). Last, a divide is cancelled one step in: at the CANCEL edge
BUSY and DONE fall, and READY returns once CANCEL is released.
Checkpoint names give each operand as the operation reads it (neg3 is
−3).
All 34 recorded rows of ArrayMulDivUnitWidth4ScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | AInput · 4 bit |
BInput · 4 bit |
CANCELInput · 1 bit |
CLKInput · 1 bit |
ENABLEInput · 1 bit |
OPInput · 3 bit |
RSTInput · 1 bit |
STARTInput · 1 bit |
TAKEInput · 1 bit |
BUSYExpected output · 1 bit |
DONEExpected output · 1 bit |
READYExpected output · 1 bit |
RESULTExpected output · 4 bit |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
#0reset | 19 | 0x0 |
0x0 |
0 |
0 |
0 |
0x0 |
1 |
0 |
0 |
not checked | not checked | not checked | not checked |
#1ready | 34 | 0x0 |
0x0 |
0 |
0 |
0 |
0x0 |
0 |
0 |
0 |
0 |
0 |
not checked | not checked |
#2enable | 38 | 0x0 |
0x0 |
0 |
0 |
1 |
0x0 |
0 |
0 |
0 |
0 |
0 |
1 |
not checked |
#3mul-7-5-launch | 72 | 0x7 |
0x5 |
0 |
1 |
1 |
0x0 |
0 |
1 |
0 |
1 |
0 |
0 |
not checked |
#4mul-7-5-done | 93 | 0x7 |
0x5 |
0 |
1 |
1 |
0x0 |
0 |
0 |
0 |
0 |
1 |
0 |
0x3 |
#5mul-7-5-take | 122 | 0x7 |
0x5 |
0 |
1 |
1 |
0x0 |
0 |
0 |
1 |
0 |
0 |
1 |
not checked |
#6mulh-neg3-5-launch | 169 | 0xD |
0x5 |
0 |
1 |
1 |
0x1 |
0 |
1 |
0 |
1 |
0 |
0 |
not checked |
#7mulh-neg3-5-done | 190 | 0xD |
0x5 |
0 |
1 |
1 |
0x1 |
0 |
0 |
0 |
0 |
1 |
0 |
0xF |
#8mulh-neg3-5-take | 219 | 0xD |
0x5 |
0 |
1 |
1 |
0x1 |
0 |
0 |
1 |
0 |
0 |
1 |
not checked |
#9mulhsu-neg8-3-launch | 264 | 0x8 |
0x3 |
0 |
1 |
1 |
0x2 |
0 |
1 |
0 |
1 |
0 |
0 |
not checked |
#10mulhsu-neg8-3-done | 285 | 0x8 |
0x3 |
0 |
1 |
1 |
0x2 |
0 |
0 |
0 |
0 |
1 |
0 |
0xE |
#11mulhsu-neg8-3-take | 314 | 0x8 |
0x3 |
0 |
1 |
1 |
0x2 |
0 |
0 |
1 |
0 |
0 |
1 |
not checked |
#12mulhu-15-15-launch | 361 | 0xF |
0xF |
0 |
1 |
1 |
0x3 |
0 |
1 |
0 |
1 |
0 |
0 |
not checked |
#13mulhu-15-15-done | 382 | 0xF |
0xF |
0 |
1 |
1 |
0x3 |
0 |
0 |
0 |
0 |
1 |
0 |
0xE |
#14mulhu-15-15-take | 411 | 0xF |
0xF |
0 |
1 |
1 |
0x3 |
0 |
0 |
1 |
0 |
0 |
1 |
not checked |
#15div-neg7-2-launch | 455 | 0x9 |
0x2 |
0 |
1 |
1 |
0x4 |
0 |
1 |
0 |
1 |
0 |
0 |
not checked |
#16div-neg7-2-step-1 | 481 | 0x9 |
0x2 |
0 |
1 |
1 |
0x4 |
0 |
0 |
0 |
1 |
0 |
0 |
not checked |
#17div-neg7-2-step-2 | 511 | 0x9 |
0x2 |
0 |
1 |
1 |
0x4 |
0 |
0 |
0 |
1 |
0 |
0 |
not checked |
#18div-neg7-2-step-3 | 539 | 0x9 |
0x2 |
0 |
1 |
1 |
0x4 |
0 |
0 |
0 |
1 |
0 |
0 |
not checked |
#19div-neg7-2-done | 569 | 0x9 |
0x2 |
0 |
1 |
1 |
0x4 |
0 |
0 |
0 |
0 |
1 |
0 |
0xD |
#20div-neg7-2-take | 598 | 0x9 |
0x2 |
0 |
1 |
1 |
0x4 |
0 |
0 |
1 |
0 |
0 |
1 |
not checked |
#21divu-9-0-launch | 646 | 0x9 |
0x0 |
0 |
1 |
1 |
0x5 |
0 |
1 |
0 |
1 |
0 |
0 |
not checked |
#22divu-9-0-done | 748 | 0x9 |
0x0 |
0 |
1 |
1 |
0x5 |
0 |
0 |
0 |
0 |
1 |
0 |
0xF |
#23divu-9-0-take | 777 | 0x9 |
0x0 |
0 |
1 |
1 |
0x5 |
0 |
0 |
1 |
0 |
0 |
1 |
not checked |
#24rem-neg7-3-launch | 823 | 0x9 |
0x3 |
0 |
1 |
1 |
0x6 |
0 |
1 |
0 |
1 |
0 |
0 |
not checked |
#25rem-neg7-3-done | 939 | 0x9 |
0x3 |
0 |
1 |
1 |
0x6 |
0 |
0 |
0 |
0 |
1 |
0 |
0xF |
#26rem-neg7-3-take | 968 | 0x9 |
0x3 |
0 |
1 |
1 |
0x6 |
0 |
0 |
1 |
0 |
0 |
1 |
not checked |
#27div-neg8-neg1-launch | 1020 | 0x8 |
0xF |
0 |
1 |
1 |
0x4 |
0 |
1 |
0 |
1 |
0 |
0 |
not checked |
#28div-neg8-neg1-done | 1131 | 0x8 |
0xF |
0 |
1 |
1 |
0x4 |
0 |
0 |
0 |
0 |
1 |
0 |
0x8 |
#29div-neg8-neg1-take | 1160 | 0x8 |
0xF |
0 |
1 |
1 |
0x4 |
0 |
0 |
1 |
0 |
0 |
1 |
not checked |
#30cancel-launch | 1212 | 0x9 |
0x2 |
0 |
1 |
1 |
0x4 |
0 |
1 |
0 |
1 |
0 |
0 |
not checked |
#31cancel-step-1 | 1238 | 0x9 |
0x2 |
0 |
1 |
1 |
0x4 |
0 |
0 |
0 |
1 |
0 |
0 |
not checked |
#32cancel | 1276 | 0x9 |
0x2 |
1 |
1 |
1 |
0x4 |
0 |
0 |
0 |
0 |
0 |
not checked | not checked |
#33cancel-released | 1287 | 0x9 |
0x2 |
0 |
0 |
1 |
0x4 |
0 |
0 |
0 |
0 |
0 |
1 |
not checked |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
6.1.3. MacCell¶
One cell of the systolic array (6.2).
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
CLK, RST |
Input | 1 each | Clock; reset clears the three registers |
SHIFT |
Input | 1 | Load the weight register from W_IN at this edge |
W_IN |
Input | 8 | The weight from the cell above (or from outside for the top row) |
X_IN |
Input | 8 | The input from the cell to the left |
SUM_IN |
Input | 32 | The partial sum from the cell above (0 for the top row) |
W_OUT |
Output | 8 | The stored weight |
X_OUT |
Output | 8 | X_IN, one edge later |
SUM_OUT |
Output | 32 | SUM_IN + X_IN × weight, one edge later |
Behavior¶
Each rising edge: SUM_OUT ← SUM_IN + X_IN × WEIGHT (int8 × int8, sign
extended, added into int32 with wraparound), X_OUT ← X_IN, and
WEIGHT ← W_IN when SHIFT is 1. The sum uses the weight held before
the edge, so a cell loading a new weight still adds with the old one.
Reset clears the weight, X_OUT and SUM_OUT.
C++ reference¶
#include <cstdint>
struct MacCellRef {
int8_t weight = 0, x_out = 0;
int32_t sum_out = 0;
// One rising clock edge.
void edge(bool shift, int8_t w_in, int8_t x_in, int32_t sum_in) {
sum_out = int32_t(uint32_t(sum_in) + uint32_t(int32_t(x_in) * weight));
x_out = x_in;
if (shift) weight = w_in;
}
};
Unknown inputs and timing¶
Four-state values flow through the gates; a known-zero weight or input
gives a known-zero product, which the systolic array relies on (6.2).
The registers capture on the rising edge; the path from X_IN through the
8-bit multiplier and the 32-bit adder to the sum register must settle
within one clock period.
Implementation¶
Structural implementation¶
An 8-bit ArrayMultiplier tree (both operands signed) on X_IN and the
weight register, a 32-bit parallel-prefix adder adding the sign-extended
product to SUM_IN, and three registers: the weight (loaded when SHIFT),
X_OUT and SUM_OUT.
Behavioral implementation¶
The compact cell runs the same gates as one compiled gate list, with delays proven on them when first built.
Examples¶
Load the weight 3, then 5 × 3 + 10 = 25 and −2 × 3 + 25 = 19; load −128, then −128 × −128 = 16,384.
All 7 recorded rows of MacCellScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | CLKInput · 1 bit |
RSTInput · 1 bit |
SHIFTInput · 1 bit |
SUM_INInput · 32 bit |
W_INInput · 8 bit |
X_INInput · 8 bit |
SUM_OUTExpected output · 32 bit |
W_OUTExpected output · 8 bit |
X_OUTExpected output · 8 bit |
|---|---|---|---|---|---|---|---|---|---|---|
#0reset | 26 | 0 |
1 |
0 |
0x00000000 |
0x00 |
0x00 |
not checked | not checked | not checked |
#1ready | 39 | 0 |
0 |
0 |
0x00000000 |
0x00 |
0x00 |
0x00000000 |
0x00 |
0x00 |
#2load-weight-3 | 53 | 1 |
0 |
1 |
0x00000000 |
0x03 |
0x00 |
0x00000000 |
0x03 |
0x00 |
#35x3-plus-10 | 82 | 1 |
0 |
0 |
0x0000000A |
0x00 |
0x05 |
0x00000019 |
0x03 |
0x05 |
#4neg2x3-plus-25 | 132 | 1 |
0 |
0 |
0x00000019 |
0x00 |
0xFE |
0x00000013 |
0x03 |
0xFE |
#5load-weight-neg128 | 174 | 1 |
0 |
1 |
0x00000000 |
0x80 |
0x00 |
0x00000000 |
0x80 |
0x00 |
#6neg128-squared | 210 | 1 |
0 |
0 |
0x00000000 |
0x80 |
0x80 |
0x00004000 |
0x80 |
0x80 |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
6.1.4. Requantizer¶
Turns a 32-bit sum back into a signed byte at the end of a layer.
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
ACC |
Input | 32 | A signed sum |
SCALE |
Input | 16 | Unsigned multiplier |
SHIFT |
Input | 5 | Right shift, 0–31 |
RELU |
Input | 1 | Clamp negative results to 0 |
OUT |
Output | 8 | The signed byte |
Behavior¶
Combinational:
OUT = clamp((ACC × SCALE + 2^(SHIFT−1)) >> SHIFT, −128, 127) (2^(SHIFT−1) is 0 when SHIFT = 0)
OUT = 0 if RELU and OUT < 0
SCALE / 2^SHIFT is the layer's output scale as a fixed-point number
(6.5); the added half rounds to nearest (halves round up, toward
+∞). The shift is arithmetic, so negative sums round the same way.
C++ reference¶
#include <cstdint>
int8_t requantize_ref(int32_t acc, uint16_t scale, unsigned shift, bool relu) {
int64_t v = int64_t(acc) * scale;
if (shift) v = (v + (int64_t{1} << (shift - 1))) >> shift;
if (v > 127) v = 127;
if (v < -128) v = -128;
if (relu && v < 0) v = 0;
return int8_t(v);
}
Unknown inputs and timing¶
The gates follow four-state rules bit by bit. The compact form's contract
domain is known inputs: an unknown input gives an unknown byte, where the
gates may still know some bits. Inside the unit an unknown sum is never
written anywhere (6.4), so the unit behaves the same with either.
The part has 5,365 gates and settles in 4,082 ps in the embedded profile,
so at the 4.5 ns clock it just fits one cycle.
Implementation¶
Structural implementation¶
A 32 × 16 Baugh-Wooley multiplier (the A operand signed), a decoder that
turns SHIFT into the rounding bit, a 48-bit add, a five-stage arithmetic
shifter of word multiplexers, and the clamp: the value fits a byte when
bits 7–47 are all equal; otherwise the sign picks −128 or 127. Last, each
output bit is ANDed with NOT(RELU and negative), which gives 0 for a
negative result under ReLU.
Behavioral implementation¶
Host arithmetic on the same rule, for known inputs, with delays proven on
the gate-level requantizer when first built.
RequantizerTest compares both forms on every boundary and on random
values.
Examples¶
Halves rounded up (1000 >> 4: 62.5 → 63; −1000 >> 4: −62.5 → −62; 5 >> 1 → 3; −5 >> 1 → −2), an exact shift (1000 >> 3 = 125), ReLU on a negative result, and three clamps (300 × 3 >> 2 = 225 → 127, 40,000 → 127 and −40,000 → −128).
All 9 recorded rows of RequantizerScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | ACCInput · 32 bit |
RELUInput · 1 bit |
SCALEInput · 16 bit |
SHIFTInput · 5 bit |
OUTExpected output · 8 bit |
|---|---|---|---|---|---|---|
#01000*1>>4 | 47 | 0x000003E8 |
0 |
0x0001 |
0x04 |
0x3F |
#11000*1>>3 | 83 | 0x000003E8 |
0 |
0x0001 |
0x03 |
0x7D |
#2-1000*1>>4 | 128 | 0xFFFFFC18 |
0 |
0x0001 |
0x04 |
0xC2 |
#3-1000*1>>4-relu | 132 | 0xFFFFFC18 |
1 |
0x0001 |
0x04 |
0x00 |
#4300*3>>2 | 173 | 0x0000012C |
0 |
0x0003 |
0x02 |
0x7F |
#55*1>>1 | 209 | 0x00000005 |
0 |
0x0001 |
0x01 |
0x03 |
#6-5*1>>1 | 254 | 0xFFFFFFFB |
0 |
0x0001 |
0x01 |
0xFE |
#740000*1>>0 | 295 | 0x00009C40 |
0 |
0x0001 |
0x00 |
0x7F |
#8-40000*1>>0 | 336 | 0xFFFF63C0 |
0 |
0x0001 |
0x00 |
0x80 |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
Source and tests¶
- Array multiplier, array M unit and their catalog entries, the Dadda tree, interface
MacCellandRequantizer, interface- Tests and recorded scenarios
| Test | Coverage |
|---|---|
ArrayMultiplierTest |
W = 4 exhaustive (all 256 pairs × 4 sign modes); W = 8, 16, 32 boundary and seeded random pairs; gates and compact against a 128-bit reference |
ArrayMulDivUnitTest |
All eight M operations against an independent reference: W = 4 exhaustive on both forms, W = 8 and 32 boundary and random; a multiply is DONE exactly 2 edges after START, a divide W + 1 |
MacCellTest |
Edge weights and inputs (−128, 127, 0, ±1), int32 wraparound, weight loads, 200 random edges, both forms |
RequantizerTest |
Every boundary of ACC, SCALE and SHIFT, ReLU, then seeded random values, both forms against the reference |
ArrayMultiplierWidth4ScenarioTest, ArrayMulDivUnitWidth4ScenarioTest, MacCellScenarioTest, RequantizerScenarioTest |
The recorded examples above, both available fidelities |
The scenarios check settled values at their checkpoints, not exact edge
times. ArrayMulDivUnitTest does not test CANCEL, ENABLE low or reset
during an operation; those paths are the unchanged ones of MulDivUnit<W>.
ArrayMulDivUnitWidth4ScenarioTest checks one cancelled divide on both
forms.