Skip to content

ยง 2.1. Arithmetic Components

This page builds word arithmetic from one-bit cells. Two word adders compute the same sum with different carry networks: RippleCarryAdder<W> passes the carry through W full adders, and ParallelPrefixAdder<W> combines carries in logโ‚‚(W) levels. Ripple versus prefix carry compares them. AddSub<W> and ArithmeticUnit<W>, and every ALU in this project, use the faster prefix adder.

Bits are numbered from 0 at the least significant position. Let \(M=2^W\) and \(S=2^{W-1}\). Results wrap modulo M; signed values range from \(-S\) to \(S-1\). Carry is unsigned carry/no-borrow and is independent of signed overflow. These are combinational components with no clock or stored flags.

Component Description Structural Behavioral
HalfAdder One-bit sum and carry, without carry-in Yes Yes
FullAdder One-bit addition with carry-in Yes Yes
RippleCarryAdder<W> Word adder built from W chained full adders Yes Yes
ParallelPrefixAdder<W> Word adder with a logarithmic-depth carry network Yes Yes
AddSub<W> Addition or subtraction with a carry input and signed overflow Yes Yes
ArithmeticUnit<W> Seven arithmetic operations over one shared add/subtract path Yes Yes

W is 8, 16 or 32. The fidelity columns describe the recorded configuration of each section. The word units have a compact form at every width; HalfAdder and FullAdder have the gate-list compact form, chosen only explicitly. The C++ references take W as a template parameter. Construction and selection describes construction.

2.1.1. HalfAdder

HalfAdder adds two one-bit operands. It has no carry input, so it cannot by itself be a general cell in a multi-bit carry chain.

IO pins

Pin Direction Width Description
A Input 1 First operand bit
B Input 1 Second operand bit
Sum Output 1 Low bit of the sum
Carry Output 1 Carry into the next bit position

The output is named Carry, not Carry_out or Cout. Pin names are case-sensitive.

Behavior

For binary inputs,

\[ \begin{aligned} \mathrm{Sum} &= A \oplus B, \\ \mathrm{Carry} &= A \land B. \end{aligned} \]

Interpreting the bits as unsigned integers, A + B = Sum + 2*Carry. For example, 1 + 1 gives Sum = 0, Carry = 1. The component has no clock, stored state, or flags beyond this carry.

C++ example

struct HalfAdderResult {
    bool Sum;
    bool Carry;
};

HalfAdderResult half_adder_ref(bool a, bool b) {
    return {a != b, a && b};
}

This binary-only function describes the settled outputs. It is not a registered behavioral implementation.

Unknown inputs

The simulator's XOR produces X if either input is X or Z. The AND produces 0 if either input is definitely 0, 1 only if both are definitely 1, and X otherwise. Neither output drives Z.

A B Settled Sum Settled Carry
0 X or Z X 0
1 X or Z X X

Implementation

Structural implementation

Both inputs fan out to two gates, which operate in parallel:

Child Type Inputs โ†’ output
XOR1 XORGate A, B โ†’ Sum
AND1 ANDGate A, B โ†’ Carry

There is no internal carry chain or storage. HalfAdder has only this structural implementation; the two gates remain visible in the simulator.

Behavioral implementation

The compact form evaluates this part's own gate list, flattened once from its structural form, and publishes the settled values on the delays proven on those gates (ยง 1.1 Structural and compact forms). Its values are the gates' by construction, including unknown and floating inputs; GateListCompactEquivalenceTest checks them. It is chosen only explicitly, for example by collapsing the part in the simulator; profiles that prefer compact forms keep the gates.

Examples

All 16 recorded rows of HalfAdderTest, 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 ยท 1 bit BInput ยท 1 bit CarryExpected output ยท 1 bit SumExpected output ยท 1 bit
#0row-01 0 0 0 0
#1row-13 0 1 0 1
#2row-25 0 X 0 X
#3row-37 0 Z 0 X
#4row-49 1 0 0 1
#5row-511 1 1 1 0
#6row-613 1 X X X
#7row-715 1 Z X X
#8row-817 X 0 0 X
#9row-919 X 1 X X
#10row-1021 X X X X
#11row-1123 X Z X X
#12row-1225 Z 0 0 X
#13row-1327 Z 1 X X
#14row-1429 Z X X X
#15row-1531 Z Z X X

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.

The table contains all 16 combinations of A and B from {0, 1, X, Z}: four binary cases and twelve nonbinary cases. Each row checks both outputs. The checkpoint number and time identify the same settled observation in the simulation below.

Embedded simulation

The half-adder scenario runs HalfAdderTest. Inspect XOR1 and AND1 for the sum and carry paths. Compare (A, B) = (0, X) with (1, X) to see how a known zero suppresses uncertainty in the AND output without making the sum known.

Open HalfAdder in the simulator

Source and tests

Test Coverage
HalfAdderTest / halfAdderRows() All 16 four-state input combinations; verifies settled Sum and Carry
SettlingAnalysisTest Proves the gates' settling bound (one gate delay)
DocumentationExamplesTest Checks the reference pattern, implementation availability, and complete example-table export; compiles the actual C++ example and checks all four binary cases

The binary C++ check does not verify X/Z behavior. Those cases are verified by the structural scenario and retained in its table and simulation.

2.1.2. FullAdder

FullAdder adds two one-bit operands and a carry input. Its carry output can feed the next bit position; W such cells form RippleCarryAdder<W>.

IO pins

Pin Direction Width Description
A Input 1 First operand bit
B Input 1 Second operand bit
Carry_in Input 1 Carry from the previous bit position
Sum Output 1 Low bit of the three-input sum
Carry_out Output 1 Carry into the next bit position

The one-bit cells keep the names Carry_in and Carry_out; the word adders use C_IN and C_OUT.

Behavior

Define the first half adder's sum as P = A XOR B. For binary inputs,

\[ \begin{aligned} \mathrm{Sum} &= P \oplus \mathrm{Carry\_in}, \\ \mathrm{Carry\_out} &= (A \land B) \\ &\quad \lor (P \land \mathrm{Carry\_in}). \end{aligned} \]

Thus A + B + Carry_in = Sum + 2*Carry_out. With three input ones, the result is Sum = 1, Carry_out = 1; with exactly two ones, it is Sum = 0, Carry_out = 1. The carry is not a signed-overflow indication.

C++ example

struct FullAdderResult {
    bool Sum;
    bool Carry_out;
};

FullAdderResult full_adder_ref(bool a, bool b, bool carry_in) {
    const auto first = half_adder_ref(a, b);
    const auto second = half_adder_ref(first.Sum, carry_in);
    return {second.Sum, first.Carry || second.Carry};
}

This example reuses half_adder_ref above and follows the actual two-half-adder composition. It describes binary settled values, not simulator events or a selectable behavioral implementation.

Unknown inputs

X/Z inputs propagate through the two half adders and final OR gate. Use the stated XOR/AND/OR network, not an arbitrary binary-equivalent rewrite, to determine four-state results. For example:

A B Carry_in Settled Sum Settled Carry_out
0 0 Z X 0
1 1 X X 1
1 X 1 X X

In the last row, both the first sum and both intermediate carries are unknown. The binary-equivalent majority expression (A AND B) OR (A AND Carry_in) OR (B AND Carry_in) would instead yield carry 1 because A AND Carry_in = 1. That is not this structural circuit's four-state contract. Neither output drives Z.

Implementation

Structural implementation

Child Type Inputs โ†’ output
HA1 HalfAdder External A, B โ†’ intermediate sum and first carry
HA2 HalfAdder HA1.Sum, external Carry_in โ†’ external Sum and second carry
OR1 ORGate HA1.Carry, HA2.Carry โ†’ external Carry_out

Expanding the two half adders reveals five primitive gates in total: two XORs, two ANDs, and one OR. There is no stored carry or clock. FullAdder has only this structural implementation.

In RippleCarryAdder<8>, C_IN drives FA0.Carry_in, each cell's Carry_out drives the next cell's Carry_in, and FA7.Carry_out drives C_OUT. This is a ripple-carry connection, not eight independent additions.

Timing. The carry's longest path is HA1.XOR1 โ†’ HA2.AND1 โ†’ OR1 (an XOR, an AND and an OR); the sum's is two XOR gates. Which is longer depends on the profile: in unit every gate is one tick, so the carry path is longer; in the realistic profiles an XOR costs about four inverter delays, so the sum path is.

Behavioral implementation

The compact form evaluates this part's own gate list, flattened once from its structural form, and publishes the settled values on the delays proven on those gates (ยง 1.1 Structural and compact forms). Its values are the gates' by construction, including unknown and floating inputs; GateListCompactEquivalenceTest checks them. It is chosen only explicitly, for example by collapsing the part in the simulator; profiles that prefer compact forms keep the gates.

Examples

All 64 recorded rows of FullAdderTest, 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 ยท 1 bit BInput ยท 1 bit Carry_inInput ยท 1 bit Carry_outExpected output ยท 1 bit SumExpected output ยท 1 bit
#0row-02 0 0 0 0 0
#1row-14 0 0 1 0 1
#2row-26 0 0 X 0 X
#3row-38 0 0 Z 0 X
#4row-411 0 1 0 0 1
#5row-514 0 1 1 1 0
#6row-617 0 1 X X X
#7row-719 0 1 Z X X
#8row-822 0 X 0 0 X
#9row-925 0 X 1 X X
#10row-1027 0 X X X X
#11row-1129 0 X Z X X
#12row-1232 0 Z 0 0 X
#13row-1335 0 Z 1 X X
#14row-1437 0 Z X X X
#15row-1539 0 Z Z X X
#16row-1642 1 0 0 0 1
#17row-1745 1 0 1 1 0
#18row-1848 1 0 X X X
#19row-1950 1 0 Z X X
#20row-2053 1 1 0 1 0
#21row-2155 1 1 1 1 1
#22row-2257 1 1 X 1 X
#23row-2359 1 1 Z 1 X
#24row-2462 1 X 0 X X
#25row-2565 1 X 1 X X
#26row-2667 1 X X X X
#27row-2769 1 X Z X X
#28row-2872 1 Z 0 X X
#29row-2975 1 Z 1 X X
#30row-3077 1 Z X X X
#31row-3179 1 Z Z X X
#32row-3282 X 0 0 0 X
#33row-3385 X 0 1 X X
#34row-3487 X 0 X X X
#35row-3589 X 0 Z X X
#36row-3692 X 1 0 X X
#37row-3795 X 1 1 X X
#38row-3897 X 1 X X X
#39row-3999 X 1 Z X X
#40row-40102 X X 0 X X
#41row-41105 X X 1 X X
#42row-42107 X X X X X
#43row-43109 X X Z X X
#44row-44112 X Z 0 X X
#45row-45115 X Z 1 X X
#46row-46117 X Z X X X
#47row-47119 X Z Z X X
#48row-48122 Z 0 0 0 X
#49row-49125 Z 0 1 X X
#50row-50127 Z 0 X X X
#51row-51129 Z 0 Z X X
#52row-52132 Z 1 0 X X
#53row-53135 Z 1 1 X X
#54row-54137 Z 1 X X X
#55row-55139 Z 1 Z X X
#56row-56142 Z X 0 X X
#57row-57145 Z X 1 X X
#58row-58147 Z X X X X
#59row-59149 Z X Z X X
#60row-60152 Z Z 0 X X
#61row-61155 Z Z 1 X X
#62row-62157 Z Z X X X
#63row-63159 Z Z Z X X

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.

The table contains all 64 combinations of A, B, and Carry_in from {0, 1, X, Z}: eight binary cases and 56 nonbinary cases. Both outputs are checked at every checkpoint. The cases are exhaustive for settled four-state input combinations, not for every possible sequence of propagation glitches.

Embedded simulation

The full-adder scenario runs FullAdderTest. Expand HA1 and HA2 to inspect their gates, or keep them collapsed to trace the two intermediate carries into OR1. Compare (1, 1, X) with (1, X, 1) to inspect the four-state distinction described above.

Open FullAdder in the simulator

Source and tests

Test Coverage
FullAdderTest / fullAdderRows() All 64 four-state input combinations; verifies settled Sum and Carry_out against the two-half-adder network
SettlingAnalysisTest Proves the settling bound and reports its path
DocumentationExamplesTest Checks the reference pattern, implementation availability, and complete example-table export; compiles this C++ example with half_adder_ref and checks all eight binary cases

The embedded simulation and table use the structural test's complete sequence. The C++ function checks only binary values; it does not replace the four-state structural test.

2.1.3. RippleCarryAdder<W>

RippleCarryAdder<W> chains W full adders. It is the direct word-level extension of ยง 2.1.2 and the reference for the faster adder below.

IO pins

Pin Direction Width Description
A, B Input W each Unsigned operand patterns
C_IN Input 1 Carry into bit 0
OUT Output W Low W bits of the sum
C_OUT Output 1 Carry out of bit Wโˆ’1

Behavior

For \(T=A+B+\mathrm{C\_IN}\), OUT = T mod M and C_OUT = [T >= M]. There are no signed-overflow, zero or negative outputs. To add wider words, connect one adder's C_OUT to the next adder's C_IN: 0x00FF + 0x0001 gives a low byte of 0x00 with carry 1, then an upper byte of 0x01.

C++ example

This binary reference is parameterized by W = 8, 16 or 32. Inputs must fit their pin widths. It omits propagation delays and X/Z values.

#include <cstdint>

// Binary word domain used by the references in this chapter.
template<unsigned W>
struct WordReference {
    static_assert(W == 8 || W == 16 || W == 32);
    static constexpr std::uint64_t mask = (std::uint64_t{1} << W) - 1;
    static constexpr std::uint64_t sign = std::uint64_t{1} << (W - 1);
};

template<unsigned W>
struct AdderResult {
    std::uint32_t OUT;
    bool C_OUT;
};

template<unsigned W>
AdderResult<W> adder_ref(std::uint32_t a, std::uint32_t b, bool c_in) {
    const std::uint64_t total = std::uint64_t(a) + b + c_in;
    return {
        static_cast<std::uint32_t>(total & WordReference<W>::mask),
        total >= (WordReference<W>::mask + 1),
    };
}

Unknown inputs

Each bit follows the full adder's XOR/AND/OR equations. A known controlling input can mask an unknown one, so an unknown bit does not automatically make the whole word unknown. The recording below drives bit 0 of A to X and to Z with B = 0 and C_IN = 0: only OUT[0] becomes X, and C_OUT stays 0.

Implementation

Structural implementation

Two BitSplitter<W> parts separate the operand bits, W FullAdder cells FA0โ€ฆFA(Wโˆ’1) add them, and a BitJoiner<W> assembles OUT. C_IN feeds FA0; each Carry_out feeds the next cell; the last cell drives C_OUT. The carry path therefore grows linearly with W.

Behavioral implementation

The compact form computes the same ripple recurrence bit by bit with four-state values, so its outputs match the gates for every input, including X and Z. It exists at W = 8, 16 and 32 and runs on delays proven on this circuit.

Timing. These paths describe the eight-bit adder in each timing profile:

Latest time each path can still change after its input (for a setup row: how long before the edge the input must be stable), proven on the gate-level form.

Pathunitticksembeddedpsdesktopps
Slowest sum A โ†’ OUT16954665
Slowest carry A โ†’ C_OUT17936652
Carry chain alone C_IN โ†’ C_OUT16816568

Examples

All 14 recorded rows of RippleCarryAdderWidth8Test, 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 ยท 8 bit BInput ยท 8 bit C_INInput ยท 1 bit C_OUTExpected output ยท 1 bit OUTExpected output ยท 8 bit
#0row-04 0x00 0x00 0 0 0x00
#1row-17 0x01 0x02 0 0 0x03
#2row-217 0x0F 0x01 0 0 0x10
#3row-327 0xFF 0x01 0 1 0x00
#4row-445 0xFF 0x00 1 1 0x00
#5row-549 0x7F 0x00 1 0 0x80
#6row-654 0x80 0x80 0 1 0x00
#7row-772 0xAA 0x55 1 1 0x00
#8row-877 0x00 0x00 1 0 0x01
#9row-981 0xFF 0xFF 0 1 0xFE
#10row-1099 0x80 0x7F 1 1 0x00
#11row-11116 0x55 0xAA 0 0 0xFF
#12row-12119 0b0000000X 0x00 0 0 0b0000000X
#13row-13121 0b0000000Z 0x00 0 0 0b0000000X

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.

The 14 recorded cases include ordinary sums, carry propagation across the whole word, and the two unknown-input cases above. The recording uses W = 8.

Embedded simulation

Expand FA0โ€ฆFA7 to follow the carry from cell to cell.

Open RippleCarryAdder<8> in the simulator

Source and tests

Test Coverage
RippleCarryAdderWidth8Test The 14 recorded cases, on both the structural and the compact form
WidthComponentsTest Four-state carry propagation at W = 8, 16 and 32
AdderWidthSweepTest W = 8, 16 and 32 on both forms: carry chains of every length, a carry span of every length at every start position, bit patterns, 1,024 seeded random vectors and a single X or Z on every input bit, against A + B + C_IN
ALUFidelityContractTest Compact against structural outputs at W = 8, 16 and 32, with binary and X/Z inputs

2.1.4. ParallelPrefixAdder<W>

ParallelPrefixAdder<W> computes the same sum as RippleCarryAdder<W>, but finds every carry in logโ‚‚(W) combining levels instead of W chained cells. Every ALU in this project uses it.

IO pins

Pin Direction Width Description
A, B Input W each Operand patterns
C_IN Input 1 Carry into bit 0
OUT Output W Low W bits of the sum
C_OUT Output 1 Carry out of bit Wโˆ’1
C_SIGN Output 1 Carry into bit Wโˆ’1, the sign bit

Behavior

OUT and C_OUT follow the same rule as RippleCarryAdder<W>. C_SIGN is the carry into the sign bit; signed overflow of the addition is C_SIGN XOR C_OUT, which the RV32I ALU uses.

C++ example

The reference reuses adder_ref<W> and WordReference<W> from ยง 2.1.3. Inputs must fit W bits.

template<unsigned W>
struct PrefixAdderResult {
    std::uint32_t OUT;
    bool C_OUT;
    bool C_SIGN;
};

template<unsigned W>
PrefixAdderResult<W> prefix_adder_ref(std::uint32_t a, std::uint32_t b, bool c_in) {
    const auto sum = adder_ref<W>(a, b, c_in);
    const auto low_mask = WordReference<W>::sign - 1;
    const bool carry_into_sign = ((a & low_mask) + (b & low_mask) + c_in)
                                  >= WordReference<W>::sign;
    return {sum.OUT, sum.C_OUT, carry_into_sign};
}

Unknown inputs

Propagate/generate cells and combining levels use four-state XOR/AND/OR gates. The combination G = G_high OR (P_high AND G_low) is the ripple carry equation regrouped, so an unknown bit spreads the same way as in RippleCarryAdder<W>. The adder comparison test finds identical outputs for the two 32-bit adders on 3,136 binary and four-state rows.

Implementation

Structural implementation

The word gates PROPAGATE (XORGate<W>) and GENERATE (ANDGate<W>) give every bit's propagate pแตข = Aแตข XOR Bแตข and generate gแตข = Aแตข AND Bแตข at once (one-bit gates, grouped as words; see word gates). A carry group is the transformation c_out = G OR (P AND c_in); CarryCombine joins two neighbouring groups as G = G_high OR (P_high AND G_low), P = P_high AND P_low. Levels PREFIX_1, PREFIX_2, PREFIX_4, โ€ฆ combine groups at distances 1, 2, 4, โ€ฆ, so W = 8, 16 and 32 need 3, 4 and 5 levels. The word gates CIN (AND with C_IN on every bit) and CARRY (OR) give the carry out of every bit; the wiring part CARRY_BITS shifts it up one bit, with C_IN into bit 0, and also yields C_SIGN and C_OUT; the word gate SUM (XOR with the propagates) forms OUT.

Behavioral implementation

The compact form evaluates the same propagate/generate levels with four-state values and matches the gates for every input. It exists at W = 8, 16 and 32 and runs on delays proven on this circuit.

Examples

All 8 recorded rows of ParallelPrefixAdderWidth16Test, 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 ยท 16 bit BInput ยท 16 bit C_INInput ยท 1 bit C_OUTExpected output ยท 1 bit C_SIGNExpected output ยท 1 bit OUTExpected output ยท 16 bit
#0sum-08 0x0000 0x0000 0 0 not checked 0x0000
#1sum-110 0x0000 0x0000 1 0 not checked 0x0001
#2sum-221 0xFFFF 0x0001 0 1 not checked 0x0000
#3sum-323 0xFFFF 0x0001 1 1 not checked 0x0001
#4sum-431 0x7FFF 0x0001 0 0 not checked 0x8000
#5sum-533 0x7FFF 0x0001 1 0 not checked 0x8001
#6sum-642 0x1234 0x4321 0 0 not checked 0x5555
#7sum-746 0x1234 0x4321 1 0 not checked 0x5556

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.

The eight recorded cases use W = 16: zero, a carry across all bits, the signed boundary, and both carry inputs. The ripple-carry-adder-w16 recording below repeats exactly these cases on the ripple adder.

Embedded simulation

Select PROPAGATE or GENERATE to see their one-bit gates, and expand PREFIX_1, PREFIX_2, PREFIX_4, PREFIX_8 to follow the carry levels.

Open ParallelPrefixAdder<16> in the simulator

Source and tests

Test Coverage
ParallelPrefixAdderWidth16Test The eight recorded cases
ParallelPrefixAdderWidth32Test 32-bit boundary, carry-chain and four-state cases on both forms
AdderWidthSweepTest W = 8, 16 and 32 on both forms: carry chains of every length, a carry span of every length at every start position, bit patterns, 1,024 seeded random vectors and a single X or Z on every input bit, against A + B + C_IN
ArithmeticTimingOptimizationTest Equal outputs to the ripple adder; shorter settling bound at every width and timing profile
ALUFidelityContractTest Compact against structural outputs at W = 8, 16 and 32

Ripple versus prefix carry

Both adders compute the same function; they differ only in how long the carry takes. A ripple adder's worst path crosses all W full adders, so it grows linearly with W. A prefix adder's worst path crosses logโ‚‚(W) combining levels plus a few gates at each end, so it grows with the logarithm of W.

The rows below are the proven worst-case delays of each adder from an operand to the carry out, read from the same delay tables the compact forms use. unit counts gates (one tick each); embedded and desktop are the 65 nm and 32 nm profiles in picoseconds.

Latest time each path can still change after its input (for a setup row: how long before the edge the input must be stable), proven on the gate-level form.

Pathunitticksembeddedpsdesktopps
RippleCarryAdder<8> A โ†’ C_OUT17936652
ParallelPrefixAdder<8> A โ†’ C_OUT8478333
RippleCarryAdder<16> A โ†’ C_OUT331,7521,220
ParallelPrefixAdder<16> A โ†’ C_OUT10580404
RippleCarryAdder<32> A โ†’ C_OUT653,3842,356
ParallelPrefixAdder<32> A โ†’ C_OUT12682475

Each doubling of W doubles the ripple adder's carry delay (17, 33 and 65 gate delays in unit) but adds only one combining level to the prefix adder (8, 10 and 12). The prefix adder is already faster at 8 bits; at 32 bits it is about 5.4 times faster in unit and about 5 times faster in the realistic profiles. The price is area: the prefix network adds a combining cell for most bits at every level. Because carry delay dominates word arithmetic, AddSub<W>, both ALUs and the RV32I branch-target adders use the prefix adder.

The same comparison runs as a test: ArithmeticTimingOptimizationTest builds both adders at W = 8, 16 and 32 in all three timing profiles and requires the prefix adder to settle sooner and to contain carry levels. It also drives both 32-bit adders with 3,136 binary and four-state rows and requires identical outputs. The ripple-carry-adder-w16 recording repeats the prefix adder's eight 16-bit cases, so the two circuits can be compared side by side in the simulator.

2.1.5. AddSub<W>

IO pins

Pin Direction Width Description
A, B Input W each Operand patterns
SUB Input 1 Invert every B bit when high
C_IN Input 1 Carry into the adder
OUT Output W Wrapped result
C_OUT Output 1 Addition carry, or no-borrow for subtraction
OVERFLOW Output 1 Signed result outside โˆ’Sโ€ฆSโˆ’1

Behavior

Let \(B'=B\) when SUB=0 and \(B'=M-1-B\) when SUB=1. The adder computes \(T=A+B'+\mathrm{C\_IN}\); OUT is T modulo M and C_OUT is [T >= M].

SUB C_IN Result before wrapping
0 0 A + B
0 1 A + B + 1
1 1 A โˆ’ B
1 0 A โˆ’ B โˆ’ 1

SUB only inverts B; C_IN stays a separate input, so ordinary subtraction needs C_IN = 1. For subtraction, C_OUT is [A >= B + (1 โˆ’ C_IN)], so 1 means no borrow. Signed overflow uses the operand and result signs: NOT(A_sign XOR B'_sign) AND (A_sign XOR OUT_sign).

To subtract wider words, start the lowest word with SUB = 1, C_IN = 1 and connect each C_OUT to the next word's C_IN. For 0x0100 โˆ’ 0x0001 in bytes, the low byte gives 0xFF with C_OUT = 0 (a borrow); the upper byte then gives 0x00 with C_OUT = 1.

C++ example

This binary reference reuses adder_ref<W> from ยง 2.1.3. It omits propagation delays and X/Z values.

template<unsigned W>
struct AddSubResult {
    std::uint32_t OUT;
    bool C_OUT;
    bool OVERFLOW;
};

template<unsigned W>
AddSubResult<W> addsub_ref(
    std::uint32_t a, std::uint32_t b, bool sub, bool c_in) {
    const auto effective_b = static_cast<std::uint32_t>(
        b ^ (sub ? WordReference<W>::mask : 0x00u));
    const auto sum = adder_ref<W>(a, effective_b, c_in);
    const bool same_sign = ((a ^ effective_b) & WordReference<W>::sign) == 0;
    const bool result_changed_sign = ((a ^ sum.OUT) & WordReference<W>::sign) != 0;
    return {sum.OUT, sum.C_OUT, same_sign && result_changed_sign};
}

Unknown inputs

Conditional inversion, carry propagation and the sign logic use four-state gates. An unknown SUB or C_IN follows those gates rather than an assumed Boolean value.

Implementation

Structural implementation

The word gate B_XOR_SUB (XORGate<W>, one XOR per bit) forms Bโ€ฒ from B and SUB, spread across the word by the wiring part SUB_WORD. One ParallelPrefixAdder<W>, named ADD, computes A + Bโ€ฒ + C_IN. Splitters expose the sign bits, and XOR/NOT/AND gates produce OVERFLOW from them.

Behavioral implementation

The compact form, available at W = 8, 16 and 32, computes the settled outputs of these gates in one step. It keeps the gates' Boolean factoring, so X and Z inputs give the same outputs as the structural circuit, and it runs on delays proven on that circuit.

Examples

All 80 recorded rows of AddSubWidth8Test, 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 ยท 8 bit BInput ยท 8 bit C_INInput ยท 1 bit SUBInput ยท 1 bit C_OUTExpected output ยท 1 bit OUTExpected output ยท 8 bit OVERFLOWExpected output ยท 1 bit
#0row-010 0x00 0x00 0 0 0 0x00 0
#1row-112 0x00 0x00 1 0 0 0x01 0
#2row-219 0x00 0x00 0 1 0 0xFF 0
#3row-325 0x00 0x00 1 1 1 0x00 0
#4row-432 0x00 0x01 0 0 0 0x01 0
#5row-536 0x00 0x01 1 0 0 0x02 0
#6row-642 0x00 0x01 0 1 0 0xFE 0
#7row-744 0x00 0x01 1 1 0 0xFF 0
#8row-850 0x00 0x80 0 0 0 0x80 0
#9row-952 0x00 0x80 1 0 0 0x81 0
#10row-1059 0x00 0x80 0 1 0 0x7F 0
#11row-1165 0x00 0x80 1 1 0 0x80 1
#12row-1272 0x00 0xFF 0 0 0 0xFF 0
#13row-1378 0x00 0xFF 1 0 1 0x00 0
#14row-1485 0x00 0xFF 0 1 0 0x00 0
#15row-1587 0x00 0xFF 1 1 0 0x01 0
#16row-1690 0x01 0x00 0 0 0 0x01 0
#17row-1794 0x01 0x00 1 0 0 0x02 0
#18row-18106 0x01 0x00 0 1 1 0x00 0
#19row-19108 0x01 0x00 1 1 1 0x01 0
#20row-20119 0x01 0x01 0 0 0 0x02 0
#21row-21121 0x01 0x01 1 0 0 0x03 0
#22row-22128 0x01 0x01 0 1 0 0xFF 0
#23row-23134 0x01 0x01 1 1 1 0x00 0
#24row-24141 0x01 0x80 0 0 0 0x81 0
#25row-25145 0x01 0x80 1 0 0 0x82 0
#26row-26157 0x01 0x80 0 1 0 0x80 1
#27row-27159 0x01 0x80 1 1 0 0x81 1
#28row-28167 0x01 0xFF 0 0 1 0x00 0
#29row-29169 0x01 0xFF 1 0 1 0x01 0
#30row-30180 0x01 0xFF 0 1 0 0x01 0
#31row-31184 0x01 0xFF 1 1 0 0x02 0
#32row-32190 0x7F 0x00 0 0 0 0x7F 0
#33row-33196 0x7F 0x00 1 0 0 0x80 1
#34row-34206 0x7F 0x00 0 1 1 0x7E 0
#35row-35208 0x7F 0x00 1 1 1 0x7F 0
#36row-36220 0x7F 0x01 0 0 0 0x80 1
#37row-37222 0x7F 0x01 1 0 0 0x81 1
#38row-38230 0x7F 0x01 0 1 1 0x7D 0
#39row-39234 0x7F 0x01 1 1 1 0x7E 0
#40row-40244 0x7F 0x80 0 0 0 0xFF 0
#41row-41250 0x7F 0x80 1 0 1 0x00 0
#42row-42260 0x7F 0x80 0 1 0 0xFE 1
#43row-43262 0x7F 0x80 1 1 0 0xFF 1
#44row-44270 0x7F 0xFF 0 0 1 0x7E 0
#45row-45272 0x7F 0xFF 1 0 1 0x7F 0
#46row-46282 0x7F 0xFF 0 1 0 0x7F 0
#47row-47288 0x7F 0xFF 1 1 0 0x80 1
#48row-48294 0x80 0x00 0 0 0 0x80 0
#49row-49296 0x80 0x00 1 0 0 0x81 0
#50row-50303 0x80 0x00 0 1 1 0x7F 1
#51row-51309 0x80 0x00 1 1 1 0x80 0
#52row-52316 0x80 0x01 0 0 0 0x81 0
#53row-53320 0x80 0x01 1 0 0 0x82 0
#54row-54327 0x80 0x01 0 1 1 0x7E 1
#55row-55329 0x80 0x01 1 1 1 0x7F 1
#56row-56335 0x80 0x80 0 0 1 0x00 1
#57row-57337 0x80 0x80 1 0 1 0x01 1
#58row-58344 0x80 0x80 0 1 0 0xFF 0
#59row-59350 0x80 0x80 1 1 1 0x00 0
#60row-60357 0x80 0xFF 0 0 1 0x7F 1
#61row-61363 0x80 0xFF 1 0 1 0x80 0
#62row-62370 0x80 0xFF 0 1 0 0x80 0
#63row-63372 0x80 0xFF 1 1 0 0x81 0
#64row-64378 0xFF 0x00 0 0 0 0xFF 0
#65row-65384 0xFF 0x00 1 0 1 0x00 0
#66row-66394 0xFF 0x00 0 1 1 0xFE 0
#67row-67396 0xFF 0x00 1 1 1 0xFF 0
#68row-68408 0xFF 0x01 0 0 1 0x00 0
#69row-69410 0xFF 0x01 1 0 1 0x01 0
#70row-70418 0xFF 0x01 0 1 1 0xFD 0
#71row-71422 0xFF 0x01 1 1 1 0xFE 0
#72row-72432 0xFF 0x80 0 0 1 0x7F 1
#73row-73438 0xFF 0x80 1 0 1 0x80 0
#74row-74448 0xFF 0x80 0 1 1 0x7E 0
#75row-75450 0xFF 0x80 1 1 1 0x7F 0
#76row-76456 0xFF 0xFF 0 0 1 0xFE 0
#77row-77458 0xFF 0xFF 1 0 1 0xFF 0
#78row-78468 0xFF 0xFF 0 1 0 0xFF 0
#79row-79474 0xFF 0xFF 1 1 1 0x00 0

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.

The 80 recorded cases cover five A values, four B values, both SUB values and both carry inputs at W = 8. For A = B = 0xFF, SUB = 1 and C_IN = 0, the result is 255 โˆ’ 255 โˆ’ 1 = โˆ’1: it borrows, so C_OUT = 0, and as a signed operation โˆ’1 โˆ’ (โˆ’1) โˆ’ 1 = โˆ’1 does not overflow.

Embedded simulation

Open AddSub<8> in the simulator

Source and tests

Test Coverage
AddSubWidth8Test The 80 recorded cases on both forms
ALUContractTest All 262,144 binary inputs of AddSub<8> on both forms

2.1.6. ArithmeticUnit<W>

IO pins

Pin Direction Width Description
A, B Input W each Primary and secondary operands
OP Input 3 Local arithmetic selector
C_IN Input 1 Carry/no-borrow input for ADC/SBC
OUT Output W Selected arithmetic result
CARRY Output 1 Carry or no-borrow
OVERFLOW Output 1 Signed overflow
INVALID_OP Output 1 Local opcode 7

Behavior

One shared AddSub<W> performs the selected operation. Results wrap modulo M.

Local OP Operation Result before wrapping Carry input used?
0 ADD A + B No
1 ADC A + B + C_IN Yes
2 SUB A โˆ’ B No
3 SBC A โˆ’ B โˆ’ (1 โˆ’ C_IN) Yes
4 INC A + 1 No
5 DEC A โˆ’ 1 No
6 NEG 0 โˆ’ A No
7 Reserved OUT=0, CARRY=0, OVERFLOW=0, INVALID_OP=1 No

For INC, carry means A=Mโˆ’1 and overflow means A=Sโˆ’1. For DEC, no-borrow means Aโ‰ 0 and overflow means A=S. For NEG, no-borrow means A=0 and overflow means A=S. The other overflow cases follow the range of the signed result. INVALID_OP reports an encoding, not uncertainty in the operands. The complete ALU's arithmetic codes 0โ€“6 equal these local codes.

C++ example

This binary reference reuses addsub_ref<W> from ยง 2.1.5. It omits propagation delays and X/Z values.

template<unsigned W>
struct ArithmeticResult {
    std::uint32_t OUT;
    bool CARRY;
    bool OVERFLOW;
    bool INVALID_OP;
};

template<unsigned W>
ArithmeticResult<W> arithmetic_ref(
    std::uint32_t a, std::uint32_t b, std::uint32_t op, bool c_in) {
    std::uint32_t x = a;
    std::uint32_t y = b;
    bool sub = false;
    bool carry_in = false;

    switch (op) {
    case 0x00: break;                                      // ADD
    case 0x01: carry_in = c_in; break;                     // ADC
    case 0x02: sub = true; carry_in = true; break;          // SUB
    case 0x03: sub = true; carry_in = c_in; break;          // SBC
    case 0x04: y = 0; carry_in = true; break;               // INC
    case 0x05: y = 1; sub = true; carry_in = true; break;   // DEC
    case 0x06: x = 0; y = a; sub = true;
               carry_in = true; break;                    // NEG
    default: return {0, false, false, true};
    }

    const auto result = addsub_ref<W>(x, y, sub, carry_in);
    return {result.OUT, result.C_OUT, result.OVERFLOW, false};
}

Unknown inputs

Operand multiplexers isolate ignored inputs: NEG ignores B and C_IN, and INC/DEC ignore B. The reserved code masks result, carry and overflow to zero. Unknown selector bits follow the gate-built multiplexers.

Implementation

Structural implementation

Opcode-controlled operand and carry multiplexers, constants, one AddSub<W> and reserved-operation masks form the circuit; the result mask is the word gate RESULT_MASK (ANDGate<W>, the valid bit spread across the word). SUB uses carry-in 1; SBC uses C_IN. INC selects B = 0 and carry 1; DEC selects B = 1, SUB = 1 and carry 1; NEG selects A = 0, B = the original A, SUB = 1 and carry 1.

Behavioral implementation

The compact form, available at W = 8, 16 and 32, computes the settled outputs of these gates in one step. It keeps the gates' Boolean factoring, so X and Z inputs give the same outputs as the structural circuit, and it runs on delays proven on that circuit.

Examples

All 80 recorded rows of ArithmeticUnitWidth8Test, 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 ยท 8 bit BInput ยท 8 bit C_INInput ยท 1 bit OPInput ยท 3 bit CARRYExpected output ยท 1 bit INVALID_OPExpected output ยท 1 bit OUTExpected output ยท 8 bit OVERFLOWExpected output ยท 1 bit
#0row-017 0x00 0x01 0 0x0 0 0 0x01 0
#1row-119 0x00 0x01 1 0x0 0 0 0x01 0
#2row-230 0x01 0x01 0 0x0 0 0 0x02 0
#3row-332 0x01 0x01 1 0x0 0 0 0x02 0
#4row-450 0x7F 0x01 0 0x0 0 0 0x80 1
#5row-552 0x7F 0x01 1 0x0 0 0 0x80 1
#6row-668 0x80 0x01 0 0x0 0 0 0x81 0
#7row-770 0x80 0x01 1 0x0 0 0 0x81 0
#8row-887 0xFF 0x01 0 0x0 1 0 0x00 0
#9row-989 0xFF 0x01 1 0x0 1 0 0x00 0
#10row-10106 0x00 0x01 0 0x1 0 0 0x01 0
#11row-11117 0x00 0x01 1 0x1 0 0 0x02 0
#12row-12127 0x01 0x01 0 0x1 0 0 0x02 0
#13row-13136 0x01 0x01 1 0x1 0 0 0x03 0
#14row-14154 0x7F 0x01 0 0x1 0 0 0x80 1
#15row-15163 0x7F 0x01 1 0x1 0 0 0x81 1
#16row-16179 0x80 0x01 0 0x1 0 0 0x81 0
#17row-17190 0x80 0x01 1 0x1 0 0 0x82 0
#18row-18207 0xFF 0x01 0 0x1 1 0 0x00 0
#19row-19216 0xFF 0x01 1 0x1 1 0 0x01 0
#20row-20231 0x00 0x01 0 0x2 0 0 0xFF 0
#21row-21233 0x00 0x01 1 0x2 0 0 0xFF 0
#22row-22248 0x01 0x01 0 0x2 1 0 0x00 0
#23row-23250 0x01 0x01 1 0x2 1 0 0x00 0
#24row-24263 0x7F 0x01 0 0x2 1 0 0x7E 0
#25row-25265 0x7F 0x01 1 0x2 1 0 0x7E 0
#26row-26281 0x80 0x01 0 0x2 1 0 0x7F 1
#27row-27283 0x80 0x01 1 0x2 1 0 0x7F 1
#28row-28299 0xFF 0x01 0 0x2 1 0 0xFE 0
#29row-29301 0xFF 0x01 1 0x2 1 0 0xFE 0
#30row-30316 0x00 0x01 0 0x3 0 0 0xFE 0
#31row-31325 0x00 0x01 1 0x3 0 0 0xFF 0
#32row-32337 0x01 0x01 0 0x3 0 0 0xFF 0
#33row-33349 0x01 0x01 1 0x3 1 0 0x00 0
#34row-34364 0x7F 0x01 0 0x3 1 0 0x7D 0
#35row-35375 0x7F 0x01 1 0x3 1 0 0x7E 0
#36row-36391 0x80 0x01 0 0x3 1 0 0x7E 1
#37row-37400 0x80 0x01 1 0x3 1 0 0x7F 1
#38row-38416 0xFF 0x01 0 0x3 1 0 0xFD 0
#39row-39427 0xFF 0x01 1 0x3 1 0 0xFE 0
#40row-40441 0x00 0x01 0 0x4 0 0 0x01 0
#41row-41443 0x00 0x01 1 0x4 0 0 0x01 0
#42row-42455 0x01 0x01 0 0x4 0 0 0x02 0
#43row-43457 0x01 0x01 1 0x4 0 0 0x02 0
#44row-44474 0x7F 0x01 0 0x4 0 0 0x80 1
#45row-45476 0x7F 0x01 1 0x4 0 0 0x80 1
#46row-46491 0x80 0x01 0 0x4 0 0 0x81 0
#47row-47493 0x80 0x01 1 0x4 0 0 0x81 0
#48row-48509 0xFF 0x01 0 0x4 1 0 0x00 0
#49row-49511 0xFF 0x01 1 0x4 1 0 0x00 0
#50row-50526 0x00 0x01 0 0x5 0 0 0xFF 0
#51row-51532 0x00 0x01 1 0x5 0 0 0xFF 0
#52row-52547 0x01 0x01 0 0x5 1 0 0x00 0
#53row-53553 0x01 0x01 1 0x5 1 0 0x00 0
#54row-54566 0x7F 0x01 0 0x5 1 0 0x7E 0
#55row-55572 0x7F 0x01 1 0x5 1 0 0x7E 0
#56row-56588 0x80 0x01 0 0x5 1 0 0x7F 1
#57row-57594 0x80 0x01 1 0x5 1 0 0x7F 1
#58row-58610 0xFF 0x01 0 0x5 1 0 0xFE 0
#59row-59616 0xFF 0x01 1 0x5 1 0 0xFE 0
#60row-60634 0x00 0x01 0 0x6 1 0 0x00 0
#61row-61636 0x00 0x01 1 0x6 1 0 0x00 0
#62row-62652 0x01 0x01 0 0x6 0 0 0xFF 0
#63row-63654 0x01 0x01 1 0x6 0 0 0xFF 0
#64row-64666 0x7F 0x01 0 0x6 0 0 0x81 0
#65row-65668 0x7F 0x01 1 0x6 0 0 0x81 0
#66row-66686 0x80 0x01 0 0x6 0 0 0x80 1
#67row-67688 0x80 0x01 1 0x6 0 0 0x80 1
#68row-68705 0xFF 0x01 0 0x6 0 0 0x01 0
#69row-69707 0xFF 0x01 1 0x6 0 0 0x01 0
#70row-70721 0x00 0x01 0 0x7 0 1 0x00 0
#71row-71727 0x00 0x01 1 0x7 0 1 0x00 0
#72row-72733 0x01 0x01 0 0x7 0 1 0x00 0
#73row-73739 0x01 0x01 1 0x7 0 1 0x00 0
#74row-74745 0x7F 0x01 0 0x7 0 1 0x00 0
#75row-75751 0x7F 0x01 1 0x7 0 1 0x00 0
#76row-76757 0x80 0x01 0 0x7 0 1 0x00 0
#77row-77763 0x80 0x01 1 0x7 0 1 0x00 0
#78row-78769 0xFF 0x01 0 0x7 0 1 0x00 0
#79row-79775 0xFF 0x01 1 0x7 0 1 0x00 0

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.

The 80 recorded cases cover all eight local opcodes, five A values and both carry inputs, with B = 1, at W = 8.

Embedded simulation

Open ArithmeticUnit<8> in the simulator

Source and tests

Test Coverage
ArithmeticUnitWidth8Test The 80 recorded cases on both forms
ALUContractTest Arithmetic through the complete ALU<8>: boundary, random and four-state cases