Skip to content

§ 2.6. Complete ALU

ALU<W> joins the units of § 2.1–2.5 into one combinational ALU: 29 operations selected by a five-bit OP, a carry input for multi-word arithmetic, and four result flags. W is the only parameter. Addition uses the parallel-prefix adder. Multiplication and division are separate iterative units.

The RV32I cores use a different, smaller ALU without flags or a carry input: RV32IALU, built from the same adder and zero detector.

Component Description Structural Behavioral
ALUControlDecoder Operation decoding; independent of the operand width Yes Yes
ALUResultSelector<W> OR-combination of six enabled result sources Yes Yes
ALU<W> 29-operation ALU with carry input and result flags Yes Yes

W is 8, 16 or 32. The fidelity columns describe the recorded configuration of each section: the decoder, the eight-bit result selector and the 16-bit ALU. ALU<8> also has a compact form with its own proven delays; ALU<16> and ALU<32> have a compiled gate-list form (their own gates compiled together), chosen only by an exact path rule, never by a profile's general behavioral choice.

Construction and selection

Each Chapter 2 component has its own header and takes only the width W, except ALUControlDecoder, which has no width.

#include "modules/composite/ALU.hpp"

auto alu = Component::create<ALU<16>>("ALU");      // build directly, always structural
builder.add(circuit::families::alu(16), "ALU");    // as a child: the build profile picks the fidelity
Component Header Family function Contract ID
RippleCarryAdder<W> RippleCarryAdder.hpp families::rippleCarryAdder(W) word.ripple-carry-adder.W
ParallelPrefixAdder<W> ParallelPrefixAdder.hpp families::parallelPrefixAdder(W) word.parallel-prefix-adder.W
AddSub<W> AddSub.hpp families::addSub(W) word.add-sub.W
ArithmeticUnit<W> ArithmeticUnit.hpp families::arithmeticUnit(W) word.arithmetic-unit.W
LogicUnit<W> LogicUnit.hpp families::logicUnit(W) word.logic-unit.W
BarrelShifter<W> BarrelShifter.hpp families::barrelShifter(W) word.barrel-shifter.W
ZeroDetect<W> ZeroDetect.hpp families::zeroDetect(W) word.zero-detect.W
CompareSelect<W> CompareSelect.hpp families::compareSelect(W) word.compare-select.W
ALUFlagUnit<W> ALUFlagUnit.hpp families::aluFlagUnit(W) word.alu-flag-unit.W
ALUControlDecoder ALUControlDecoder.hpp families::aluControlDecoder() word.alu-control-decoder
ALUResultSelector<W> ALUResultSelector.hpp families::aluResultSelector(W) word.alu-result-selector.W
ALU<W> ALU.hpp families::alu(W) word.alu.W

Headers are in include/modules/composite/. Compact forms exist for every unit at W = 8, for both adders at every width, and for ZeroDetect<32>; other configurations are structural. Python builds the same components with create_word_component(unit, name, width, fidelity), where unit is alu, add-sub, arithmetic, logic, shift, compare, flags, result, ripple-adder, prefix-adder or zero, and create_alu_control(name). An unsupported width or fidelity is rejected.

Pin names follow one rule: A and B are operands, OUT is the result word, C_IN and C_OUT are carry ports, and ZERO, NEGATIVE, CARRY and OVERFLOW are flags. The one-bit cells of § 2.1.1–2.1.2 keep their own names.

2.6.1. ALUControlDecoder

IO pins

Pin Direction Width Description
OP Input 5 Complete ALU opcode
LOCAL_OP Output 3 OP[2:0] for logic and shifts
ARITH_OP Output 3 Arithmetic selector; forced to SUB=2 for comparisons
COMPARE_OP Output 3 OP−21 during comparisons; otherwise zero
ARITHMETIC_ENABLE, LOGIC_ENABLE, SHIFT_ENABLE, COMPARE_ENABLE Output 1 each Result group enables
PASS_A_ENABLE, PASS_B_ENABLE Output 1 each Operand pass-through enables
INVALID_OP Output 1 Reserved opcode

Behavior

Arithmetic enables cover 0–6, logic 8–14, shifts 16–20, comparisons 21–27 and operand passes 28/29. At most one result source is enabled for a binary opcode. CLEAR=30 and reserved 7/15/31 disable all sources; only the reserved codes assert INVALID_OP. Comparisons request arithmetic SUB through ARITH_OP while leaving ARITHMETIC_ENABLE low.

The decoder has no width parameter; one decoder serves every ALU<W>.

C++ example

This binary control reference has no operand width. OP must fit five bits; it omits propagation delays and X/Z values.

#include <cstdint>
#include <stdexcept>

struct ALUControl {
    std::uint8_t LOCAL_OP, ARITH_OP, COMPARE_OP;
    bool ARITHMETIC_ENABLE, LOGIC_ENABLE, SHIFT_ENABLE, COMPARE_ENABLE;
    bool PASS_A_ENABLE, PASS_B_ENABLE, INVALID_OP;
};

ALUControl alu_control_ref(std::uint8_t op) {
    if (op > 31) throw std::invalid_argument("OP must fit five bits");
    const bool compare = op >= 21 && op <= 27;
    return {
        static_cast<std::uint8_t>(op & 7),
        static_cast<std::uint8_t>(compare ? 2 : op & 7),
        static_cast<std::uint8_t>(compare ? op - 21 : 0),
        op <= 6, op >= 8 && op <= 14, op >= 16 && op <= 20, compare,
        op == 28, op == 29, op == 7 || op == 15 || op == 31
    };
}

Unknown inputs

Unknown OP bits follow the decoder's gate equations. INVALID_OP is an encoding predicate and does not certify that OP is binary. LOCAL_OP directly forwards its three bits, including Z.

Implementation

Structural implementation

A Decoder<5> with enable high feeds OR networks for group enables and invalid codes. Splitter/joiner wiring forwards the low opcode bits. Three muxes override arithmetic selection for comparisons; OR networks form the local comparison code.

Behavioral implementation

The compact form computes the decoder's settled outputs in one step with the same Boolean factoring, so X and Z inputs give the same outputs as the gates. It runs on delays proven on the gates and does not reproduce intermediate glitches.

Examples

All 32 recorded rows of ALUControlDecoderTest, 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 OPInput · 5 bit ARITHMETIC_ENABLEExpected output · 1 bit ARITH_OPExpected output · 3 bit COMPARE_ENABLEExpected output · 1 bit COMPARE_OPExpected output · 3 bit INVALID_OPExpected output · 1 bit LOCAL_OPExpected output · 3 bit LOGIC_ENABLEExpected output · 1 bit PASS_A_ENABLEExpected output · 1 bit PASS_B_ENABLEExpected output · 1 bit SHIFT_ENABLEExpected output · 1 bit
#0row-011 0x00 1 0x0 0 0x0 0 0x0 0 0 0 0
#1row-117 0x01 1 0x1 0 0x0 0 0x1 0 0 0 0
#2row-224 0x02 1 0x2 0 0x0 0 0x2 0 0 0 0
#3row-331 0x03 1 0x3 0 0x0 0 0x3 0 0 0 0
#4row-441 0x04 1 0x4 0 0x0 0 0x4 0 0 0 0
#5row-548 0x05 1 0x5 0 0x0 0 0x5 0 0 0 0
#6row-654 0x06 1 0x6 0 0x0 0 0x6 0 0 0 0
#7row-760 0x07 0 0x7 0 0x0 1 0x7 0 0 0 0
#8row-872 0x08 0 0x0 0 0x0 0 0x0 1 0 0 0
#9row-978 0x09 0 0x1 0 0x0 0 0x1 1 0 0 0
#10row-1085 0x0A 0 0x2 0 0x0 0 0x2 1 0 0 0
#11row-1192 0x0B 0 0x3 0 0x0 0 0x3 1 0 0 0
#12row-12102 0x0C 0 0x4 0 0x0 0 0x4 1 0 0 0
#13row-13109 0x0D 0 0x5 0 0x0 0 0x5 1 0 0 0
#14row-14115 0x0E 0 0x6 0 0x0 0 0x6 1 0 0 0
#15row-15121 0x0F 0 0x7 0 0x0 1 0x7 0 0 0 0
#16row-16131 0x10 0 0x0 0 0x0 0 0x0 0 0 0 1
#17row-17137 0x11 0 0x1 0 0x0 0 0x1 0 0 0 1
#18row-18144 0x12 0 0x2 0 0x0 0 0x2 0 0 0 1
#19row-19151 0x13 0 0x3 0 0x0 0 0x3 0 0 0 1
#20row-20161 0x14 0 0x4 0 0x0 0 0x4 0 0 0 1
#21row-21174 0x15 0 0x2 1 0x0 0 0x5 0 0 0 0
#22row-22188 0x16 0 0x2 1 0x1 0 0x6 0 0 0 0
#23row-23195 0x17 0 0x2 1 0x2 0 0x7 0 0 0 0
#24row-24207 0x18 0 0x2 1 0x3 0 0x0 0 0 0 0
#25row-25214 0x19 0 0x2 1 0x4 0 0x1 0 0 0 0
#26row-26221 0x1A 0 0x2 1 0x5 0 0x2 0 0 0 0
#27row-27228 0x1B 0 0x2 1 0x6 0 0x3 0 0 0 0
#28row-28241 0x1C 0 0x4 0 0x0 0 0x4 0 1 0 0
#29row-29246 0x1D 0 0x5 0 0x0 0 0x5 0 0 1 0
#30row-30251 0x1E 0 0x6 0 0x0 0 0x6 0 0 0 0
#31row-31256 0x1F 0 0x7 0 0x0 1 0x7 0 0 0 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 width-independent control recording checks all 32 binary opcode encodings.

Embedded simulation

All 32 binary opcodes are recorded, checking every output pin.

Open ALUControlDecoder in the simulator

Source and tests

2.6.2. ALUResultSelector<W>

IO pins

Pin Direction Width Description
ARITHMETIC_RESULT, LOGIC_RESULT, SHIFT_RESULT, COMPARE_RESULT Input W each Operation-unit results
A, B Input W each Operand pass-through sources
ARITHMETIC_ENABLE, LOGIC_ENABLE, SHIFT_ENABLE, COMPARE_ENABLE Input 1 each Operation-unit enables
PASS_A_ENABLE, PASS_B_ENABLE Input 1 each Pass-through enables
OUT Output W Bitwise OR of enabled sources

Behavior

Each source is AND-masked by its independent enable, then OR-combined. One enable selects that source; no enables produce zero; multiple enables produce the bitwise OR of those sources. There is no priority or invalid-selection output.

C++ example

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

#include <array>
#include <cstdint>

template<unsigned W>
std::uint32_t alu_result_selector_ref(
    const std::array<std::uint32_t, 6>& sources,
    const std::array<bool, 6>& enables) {
    std::uint32_t out = 0;
    for (unsigned i = 0; i < sources.size(); ++i)
        if (enables[i]) out |= sources[i];
    return out;
}

Unknown inputs

Low enables mask X/Z source bits. Active unknown values and unknown enables follow AND/OR logic. Z data is not forwarded as high impedance through a logic gate.

Implementation

Structural implementation

Six word gates RESULT_SELECT_0 … RESULT_SELECT_5 (ANDGate<W>) mask the sources with their enables, each enable spread across the word by wiring; five ORGate<W> word gates in a balanced tree (RESULT_OR_…) combine them into OUT. Per bit these are the same six AND and five OR gates as a bit-by-bit circuit. Operation units remain combinationally active regardless of selection.

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 (delay model).

Examples

All 66 recorded rows of ALUResultSelectorWidth8Test, 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 ARITHMETIC_ENABLEInput · 1 bit ARITHMETIC_RESULTInput · 8 bit BInput · 8 bit COMPARE_ENABLEInput · 1 bit COMPARE_RESULTInput · 8 bit LOGIC_ENABLEInput · 1 bit LOGIC_RESULTInput · 8 bit PASS_A_ENABLEInput · 1 bit PASS_B_ENABLEInput · 1 bit SHIFT_ENABLEInput · 1 bit SHIFT_RESULTInput · 8 bit OUTExpected output · 8 bit
#0row-04 0x55 0 0x81 0xAA 0 0x18 0 0x42 0 0 0 0x24 0x00
#1row-19 0x55 1 0x81 0xAA 0 0x18 0 0x42 0 0 0 0x24 0x81
#2row-214 0x55 0 0x81 0xAA 0 0x18 1 0x42 0 0 0 0x24 0x42
#3row-319 0x55 1 0x81 0xAA 0 0x18 1 0x42 0 0 0 0x24 0xC3
#4row-424 0x55 0 0x81 0xAA 0 0x18 0 0x42 0 0 1 0x24 0x24
#5row-529 0x55 1 0x81 0xAA 0 0x18 0 0x42 0 0 1 0x24 0xA5
#6row-634 0x55 0 0x81 0xAA 0 0x18 1 0x42 0 0 1 0x24 0x66
#7row-739 0x55 1 0x81 0xAA 0 0x18 1 0x42 0 0 1 0x24 0xE7
#8row-844 0x55 0 0x81 0xAA 1 0x18 0 0x42 0 0 0 0x24 0x18
#9row-949 0x55 1 0x81 0xAA 1 0x18 0 0x42 0 0 0 0x24 0x99
#10row-1054 0x55 0 0x81 0xAA 1 0x18 1 0x42 0 0 0 0x24 0x5A
#11row-1159 0x55 1 0x81 0xAA 1 0x18 1 0x42 0 0 0 0x24 0xDB
#12row-1264 0x55 0 0x81 0xAA 1 0x18 0 0x42 0 0 1 0x24 0x3C
#13row-1369 0x55 1 0x81 0xAA 1 0x18 0 0x42 0 0 1 0x24 0xBD
#14row-1474 0x55 0 0x81 0xAA 1 0x18 1 0x42 0 0 1 0x24 0x7E
#15row-1579 0x55 1 0x81 0xAA 1 0x18 1 0x42 0 0 1 0x24 0xFF
#16row-1684 0x55 0 0x81 0xAA 0 0x18 0 0x42 1 0 0 0x24 0x55
#17row-1789 0x55 1 0x81 0xAA 0 0x18 0 0x42 1 0 0 0x24 0xD5
#18row-1894 0x55 0 0x81 0xAA 0 0x18 1 0x42 1 0 0 0x24 0x57
#19row-1999 0x55 1 0x81 0xAA 0 0x18 1 0x42 1 0 0 0x24 0xD7
#20row-20104 0x55 0 0x81 0xAA 0 0x18 0 0x42 1 0 1 0x24 0x75
#21row-21109 0x55 1 0x81 0xAA 0 0x18 0 0x42 1 0 1 0x24 0xF5
#22row-22114 0x55 0 0x81 0xAA 0 0x18 1 0x42 1 0 1 0x24 0x77
#23row-23119 0x55 1 0x81 0xAA 0 0x18 1 0x42 1 0 1 0x24 0xF7
#24row-24124 0x55 0 0x81 0xAA 1 0x18 0 0x42 1 0 0 0x24 0x5D
#25row-25129 0x55 1 0x81 0xAA 1 0x18 0 0x42 1 0 0 0x24 0xDD
#26row-26134 0x55 0 0x81 0xAA 1 0x18 1 0x42 1 0 0 0x24 0x5F
#27row-27139 0x55 1 0x81 0xAA 1 0x18 1 0x42 1 0 0 0x24 0xDF
#28row-28144 0x55 0 0x81 0xAA 1 0x18 0 0x42 1 0 1 0x24 0x7D
#29row-29149 0x55 1 0x81 0xAA 1 0x18 0 0x42 1 0 1 0x24 0xFD
#30row-30154 0x55 0 0x81 0xAA 1 0x18 1 0x42 1 0 1 0x24 0x7F
#31row-31159 0x55 1 0x81 0xAA 1 0x18 1 0x42 1 0 1 0x24 0xFF
#32row-32164 0x55 0 0x81 0xAA 0 0x18 0 0x42 0 1 0 0x24 0xAA
#33row-33169 0x55 1 0x81 0xAA 0 0x18 0 0x42 0 1 0 0x24 0xAB
#34row-34174 0x55 0 0x81 0xAA 0 0x18 1 0x42 0 1 0 0x24 0xEA
#35row-35179 0x55 1 0x81 0xAA 0 0x18 1 0x42 0 1 0 0x24 0xEB
#36row-36184 0x55 0 0x81 0xAA 0 0x18 0 0x42 0 1 1 0x24 0xAE
#37row-37189 0x55 1 0x81 0xAA 0 0x18 0 0x42 0 1 1 0x24 0xAF
#38row-38194 0x55 0 0x81 0xAA 0 0x18 1 0x42 0 1 1 0x24 0xEE
#39row-39199 0x55 1 0x81 0xAA 0 0x18 1 0x42 0 1 1 0x24 0xEF
#40row-40204 0x55 0 0x81 0xAA 1 0x18 0 0x42 0 1 0 0x24 0xBA
#41row-41209 0x55 1 0x81 0xAA 1 0x18 0 0x42 0 1 0 0x24 0xBB
#42row-42214 0x55 0 0x81 0xAA 1 0x18 1 0x42 0 1 0 0x24 0xFA
#43row-43219 0x55 1 0x81 0xAA 1 0x18 1 0x42 0 1 0 0x24 0xFB
#44row-44224 0x55 0 0x81 0xAA 1 0x18 0 0x42 0 1 1 0x24 0xBE
#45row-45229 0x55 1 0x81 0xAA 1 0x18 0 0x42 0 1 1 0x24 0xBF
#46row-46234 0x55 0 0x81 0xAA 1 0x18 1 0x42 0 1 1 0x24 0xFE
#47row-47239 0x55 1 0x81 0xAA 1 0x18 1 0x42 0 1 1 0x24 0xFF
#48row-48244 0x55 0 0x81 0xAA 0 0x18 0 0x42 1 1 0 0x24 0xFF
#49row-49249 0x55 1 0x81 0xAA 0 0x18 0 0x42 1 1 0 0x24 0xFF
#50row-50254 0x55 0 0x81 0xAA 0 0x18 1 0x42 1 1 0 0x24 0xFF
#51row-51259 0x55 1 0x81 0xAA 0 0x18 1 0x42 1 1 0 0x24 0xFF
#52row-52264 0x55 0 0x81 0xAA 0 0x18 0 0x42 1 1 1 0x24 0xFF
#53row-53269 0x55 1 0x81 0xAA 0 0x18 0 0x42 1 1 1 0x24 0xFF
#54row-54274 0x55 0 0x81 0xAA 0 0x18 1 0x42 1 1 1 0x24 0xFF
#55row-55279 0x55 1 0x81 0xAA 0 0x18 1 0x42 1 1 1 0x24 0xFF
#56row-56284 0x55 0 0x81 0xAA 1 0x18 0 0x42 1 1 0 0x24 0xFF
#57row-57289 0x55 1 0x81 0xAA 1 0x18 0 0x42 1 1 0 0x24 0xFF
#58row-58294 0x55 0 0x81 0xAA 1 0x18 1 0x42 1 1 0 0x24 0xFF
#59row-59299 0x55 1 0x81 0xAA 1 0x18 1 0x42 1 1 0 0x24 0xFF
#60row-60304 0x55 0 0x81 0xAA 1 0x18 0 0x42 1 1 1 0x24 0xFF
#61row-61309 0x55 1 0x81 0xAA 1 0x18 0 0x42 1 1 1 0x24 0xFF
#62row-62314 0x55 0 0x81 0xAA 1 0x18 1 0x42 1 1 1 0x24 0xFF
#63row-63319 0x55 1 0x81 0xAA 1 0x18 1 0x42 1 1 1 0x24 0xFF
#64row-64324 0bXXXXXXXX 0 0bXXXXXXXX 0bXXXXXXXX 0 0bXXXXXXXX 0 0bXXXXXXXX 0 0 0 0bXXXXXXXX 0x00
#65row-65326 0bZZZZZZZZ 0 0bZZZZZZZZ 0bZZZZZZZZ 0 0bZZZZZZZZ 0 0bZZZZZZZZ 0 0 0 0bZZZZZZZZ 0x00

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 result-selector recording instantiates W = 8; its width-parameterized reference applies the same enable/OR rule at every supported W.

The binary source words are 81, 42, 24, 18, 55, AA (hex), in the source-array order above; the final two rows use X/Z source words.

Embedded simulation

The scenario records all 64 enable combinations with these source words, then two all-disabled cases with every source set to X or Z. It contains 66 rows, not an exhaustive sweep over the six data buses.

Open ALUResultSelector<8> in the simulator

Source and tests

2.6.3. ALU<W>

IO pins

Pin Direction Width Description
A, B Input W each Operands; B also supplies shift distance
OP Input 5 Operation code
C_IN Input 1 ADC/SBC input; zero-distance shift carry
OUT Output W Selected result
ZERO, NEGATIVE Output 1 each Result zero and binary sign
CARRY, OVERFLOW Output 1 each Qualified source flags
INVALID_OP Output 1 Reserved opcode

Behavior

Operands and OUT are W-bit patterns. Arithmetic wraps modulo \(2^W\); signed operations interpret a pattern as two's complement in \(-2^{W-1}\) through \(2^{W-1}-1\). Let \(s(x)\) denote that signed interpretation and \(n=B\mathbin{\&}(W-1)\) the effective shift distance.

Opcode map

OP Operation OUT
0x00 ADD A + B
0x01 ADC A + B + C_IN
0x02 SUB A - B
0x03 SBC A - B - (1 - C_IN)
0x04 INC A + 1
0x05 DEC A - 1
0x06 NEG 0 - A
0x07 Reserved 0x00; INVALID_OP = 1
0x08 AND A AND B, bitwise
0x09 OR A OR B, bitwise
0x0A XOR A XOR B, bitwise
0x0B NOT Bitwise complement of A
0x0C NAND Bitwise complement of A AND B
0x0D NOR Bitwise complement of A OR B
0x0E XNOR Bitwise complement of A XOR B
0x0F Reserved 0x00; INVALID_OP = 1
0x10 SLL Shift A left by n, zero fill
0x11 SRL Shift A right by n, zero fill
0x12 SRA Shift A right by n, sign fill
0x13 ROL Rotate A left by n
0x14 ROR Rotate A right by n
0x15 EQ 0x01 if A == B, otherwise 0x00
0x16 SLTU 0x01 if unsigned A < B, otherwise 0x00
0x17 SLT 0x01 if s(A) < s(B), otherwise 0x00
0x18 MINU Smaller unsigned operand
0x19 MAXU Larger unsigned operand
0x1A MINS Operand with smaller signed value
0x1B MAXS Operand with larger signed value
0x1C PASS_A A
0x1D PASS_B B
0x1E CLEAR 0x00; INVALID_OP = 0
0x1F Reserved 0x00; INVALID_OP = 1

Min/max operations return the original operand pattern, not a predicate or magnitude. Rotations operate on W bits, excluding carry. The full operation details are in arithmetic, logic, shifts and rotations, and comparison and selection.

Flags and ignored inputs

ZERO = [OUT == 0] and NEGATIVE = OUT[W−1] for every binary result, including predicates and reserved operations. A false comparison therefore sets ZERO = 1; a true comparison returns 0x01 and sets ZERO = 0.

Operation group CARRY OVERFLOW
Arithmetic (0x00–0x06) Carry-out for addition; 1 means no borrow for subtraction Signed result before wrapping is outside −2^(W−1)…2^(W−1)−1
Shift/rotate (0x10–0x14), n > 0 Last bit shifted/rotated out 0
Shift/rotate (0x10–0x14), n = 0 C_IN 0
Logic, comparison, min/max, pass, clear, reserved 0 0

The arithmetic flag rules for INC, DEC, and NEG are specified in 2.1. In particular, NEG sets carry only for A = 0; this ALU uses the no-borrow convention consistently.

For known opcodes, ignored inputs are isolated from the settled outputs:

  • B is ignored by INC, DEC, NEG, NOT, and PASS_A.
  • A is ignored by PASS_B; both operands are ignored by CLEAR and reserved operations.
  • Shifts and rotations ignore B[W−1:log₂(W)], even when those bits are unknown.
  • C_IN is used only by ADC, SBC, and zero-distance shifts/rotates. In particular, ordinary subtraction and all comparisons ignore it.

Reserved opcodes produce OUT = 0, ZERO = 1, NEGATIVE = CARRY = OVERFLOW = 0, and INVALID_OP = 1. CLEAR produces the same result and flags except INVALID_OP = 0. For a supported, known opcode, INVALID_OP = 0 even when an operand is unknown or arithmetic overflows.

C++ example

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

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

template<unsigned W>
ALUResult<W> alu_ref(
    std::uint32_t a, std::uint32_t b, std::uint32_t op, bool c_in) {
    const bool arithmetic = op <= 0x06;
    const bool logic = op >= 0x08 && op <= 0x0E;
    const bool shift = op >= 0x10 && op <= 0x14;
    const bool comparison = op >= 0x15 && op <= 0x1B;
    const bool transfer = op >= 0x1C && op <= 0x1E;
    const bool invalid = !(arithmetic || logic || shift || comparison || transfer);

    std::uint32_t out = 0;
    bool arith_carry = false, arith_overflow = false, shift_carry = false;
    if (arithmetic || comparison) {
        const auto local_op = static_cast<std::uint32_t>(comparison ? 2 : op);
        const auto r = arithmetic_ref<W>(a, b, local_op, c_in);
        arith_carry = r.CARRY;
        arith_overflow = r.OVERFLOW;
        out = comparison
            ? compare_select_ref<W>(a, b, r.OUT, r.CARRY, r.OVERFLOW,
                                  static_cast<std::uint32_t>(op - 0x15))
            : r.OUT;
    } else if (logic) {
        out = logic_ref<W>(a, b, static_cast<std::uint32_t>(op - 0x08));
    } else if (shift) {
        const auto r = shift_ref<W>(a, b, static_cast<std::uint32_t>(op - 0x10), c_in);
        out = r.OUT;
        shift_carry = r.CARRY;
    } else if (op == 0x1C) {
        out = a;
    } else if (op == 0x1D) {
        out = b;
    }
    const auto f = flags_ref<W>(out, arithmetic, shift,
                              arith_carry, arith_overflow, shift_carry);
    return {out, f.ZERO, f.NEGATIVE, f.CARRY, f.OVERFLOW, invalid};
}

Unknown inputs

The structural gates determine four-state behavior; the C++ example does not. Known operation selection masks inactive result paths, so unknown data in an unused path does not automatically make OUT unknown. For example, CLEAR and reserved opcodes still produce their defined zero result with unknown data and carry inputs, and AND with an all-zero operand produces zero despite unknown bits in the other operand.

An X/Z bit that affects the selected computation follows its gates' propagation rules. ZERO can be unknown for a partially unknown result with no known 1. A high-impedance operand bit passing through logic is treated as unknown, not preserved as a high-impedance output.

An opcode containing X or Z is not automatically a reserved encoding. Decoder and selection gates determine the outputs; INVALID_OP is not an input-validity checker. The contract test specifically checks that an entirely unknown/high-impedance opcode produces INVALID_OP = X, without asserting a complete output word for that case.

The compact ALU<8> runs on delays proven on its gates, one set per operation code, so a quick operation does not wait for the slowest one. It does not reproduce intermediate gate glitches.

Implementation

Structural implementation

The structural groups are CONTROL (ALUControlDecoder), ARITHMETIC (ArithmeticUnit<W>), LOGIC, SHIFTER, COMPARE_SELECT, RESULT_SELECT and FLAGS. The arithmetic unit owns the one add/subtract circuit; its difference, carry and overflow also feed comparison. Result selection feeds the zero and sign flags; the operation-group enables qualify carry and overflow.

The diagram shows the seven groups for the recorded W = 16 configuration:

Inside ALU<W> ALU<W>: the control decoder turns the opcode into local selectors; the arithmetic, logic, shifter and compare-select units compute in parallel; the result selector picks one word and the flag unit derives the flags. CONTROL OP → selectors ARITHMETIC add · sub · neg LOGIC and · or · xor SHIFTER shift · rotate COMPARE_SELECT slt · min · max RESULT select FLAGS Z · N · C · V A, B and OP reach every unit; the decoder enables one result source.
Every unit computes; one result is selected. For comparisons the arithmetic unit's subtraction feeds COMPARE_SELECT, so there is no second subtractor. A and B can also pass straight to the result.

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 (delay model).

Inside a structural ALU each unit is requested as a family, so a build profile can make any unit compact while the rest stays gate level.

Timing. In ALU<8>, operation decoding, arithmetic and result selection precede zero detection, so OP-to-ZERO includes both the selected result path and the zero-reduction path:

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
Operation to flag ZERO OP → ZERO492,6641,856
Operation to result OP → OUT452,4781,727
Operand to result A → OUT311,8381,281
Control decoder alone OP → ARITH_OP14674469
Zero detection alone A → ZERO4186129

Examples

All 64 recorded rows of ALUScenarioTest, 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 OPInput · 5 bit CARRYExpected output · 1 bit INVALID_OPExpected output · 1 bit NEGATIVEExpected output · 1 bit OUTExpected output · 16 bit OVERFLOWExpected output · 1 bit ZEROExpected output · 1 bit
#0case-039 0xFFFF 0x0001 0 0x00 1 0 0 0x0000 0 1
#1case-162 0xFFFF 0x0001 0 0x01 1 0 0 0x0000 0 1
#2case-289 0xFFFF 0x0001 0 0x02 1 0 1 0xFFFE 0 0
#3case-3108 0xFFFF 0x0001 0 0x03 1 0 1 0xFFFD 0 0
#4case-4138 0xFFFF 0x0001 0 0x04 1 0 0 0x0000 0 1
#5case-5163 0xFFFF 0x0001 0 0x05 1 0 1 0xFFFE 0 0
#6case-6188 0xFFFF 0x0001 0 0x06 0 0 0 0x0001 0 0
#7case-7211 0xFFFF 0x0001 0 0x07 0 1 0 0x0000 0 1
#8case-8244 0xFFFF 0x0001 0 0x08 0 0 0 0x0001 0 0
#9case-9267 0xFFFF 0x0001 0 0x09 0 0 1 0xFFFF 0 0
#10case-10294 0xFFFF 0x0001 0 0x0A 0 0 1 0xFFFE 0 0
#11case-11311 0xFFFF 0x0001 0 0x0B 0 0 0 0x0000 0 1
#12case-12341 0xFFFF 0x0001 0 0x0C 0 0 1 0xFFFE 0 0
#13case-13366 0xFFFF 0x0001 0 0x0D 0 0 0 0x0000 0 1
#14case-14391 0xFFFF 0x0001 0 0x0E 0 0 0 0x0001 0 0
#15case-15414 0xFFFF 0x0001 0 0x0F 0 1 0 0x0000 0 1
#16case-16447 0xFFFF 0x0001 0 0x10 1 0 1 0xFFFE 0 0
#17case-17478 0xFFFF 0x0001 0 0x11 1 0 0 0x7FFF 0 0
#18case-18508 0xFFFF 0x0001 0 0x12 1 0 1 0xFFFF 0 0
#19case-19523 0xFFFF 0x0001 0 0x13 1 0 1 0xFFFF 0 0
#20case-20553 0xFFFF 0x0001 0 0x14 1 0 1 0xFFFF 0 0
#21case-21592 0xFFFF 0x0001 0 0x15 0 0 0 0x0000 0 1
#22case-22633 0xFFFF 0x0001 0 0x16 0 0 0 0x0000 0 1
#23case-23656 0xFFFF 0x0001 0 0x17 0 0 0 0x0001 0 0
#24case-24699 0xFFFF 0x0001 0 0x18 0 0 0 0x0001 0 0
#25case-25722 0xFFFF 0x0001 0 0x19 0 0 1 0xFFFF 0 0
#26case-26745 0xFFFF 0x0001 0 0x1A 0 0 1 0xFFFF 0 0
#27case-27767 0xFFFF 0x0001 0 0x1B 0 0 0 0x0001 0 0
#28case-28805 0xFFFF 0x0001 0 0x1C 0 0 1 0xFFFF 0 0
#29case-29830 0xFFFF 0x0001 0 0x1D 0 0 0 0x0001 0 0
#30case-30855 0xFFFF 0x0001 0 0x1E 0 0 0 0x0000 0 1
#31case-31878 0xFFFF 0x0001 0 0x1F 0 1 0 0x0000 0 1
#32case-32901 0x8000 0x000F 1 0x00 0 0 1 0x800F 0 0
#33case-33924 0x8000 0x000F 1 0x01 0 0 1 0x8010 0 0
#34case-34951 0x8000 0x000F 1 0x02 1 0 0 0x7FF1 1 0
#35case-35974 0x8000 0x000F 1 0x03 1 0 0 0x7FF1 1 0
#36case-36996 0x8000 0x000F 1 0x04 0 0 1 0x8001 0 0
#37case-371022 0x8000 0x000F 1 0x05 1 0 0 0x7FFF 1 0
#38case-381051 0x8000 0x000F 1 0x06 0 0 1 0x8000 1 0
#39case-391074 0x8000 0x000F 1 0x07 0 1 0 0x0000 0 1
#40case-401097 0x8000 0x000F 1 0x08 0 0 0 0x0000 0 1
#41case-411120 0x8000 0x000F 1 0x09 0 0 1 0x800F 0 0
#42case-421147 0x8000 0x000F 1 0x0A 0 0 1 0x800F 0 0
#43case-431170 0x8000 0x000F 1 0x0B 0 0 0 0x7FFF 0 0
#44case-441192 0x8000 0x000F 1 0x0C 0 0 1 0xFFFF 0 0
#45case-451218 0x8000 0x000F 1 0x0D 0 0 0 0x7FF0 0 0
#46case-461247 0x8000 0x000F 1 0x0E 0 0 0 0x7FF0 0 0
#47case-471270 0x8000 0x000F 1 0x0F 0 1 0 0x0000 0 1
#48case-481301 0x8000 0x000F 1 0x10 0 0 0 0x0000 0 1
#49case-491332 0x8000 0x000F 1 0x11 0 0 0 0x0001 0 0
#50case-501359 0x8000 0x000F 1 0x12 0 0 1 0xFFFF 0 0
#51case-511390 0x8000 0x000F 1 0x13 0 0 0 0x4000 0 0
#52case-521417 0x8000 0x000F 1 0x14 0 0 0 0x0001 0 0
#53case-531454 0x8000 0x000F 1 0x15 0 0 0 0x0000 0 1
#54case-541495 0x8000 0x000F 1 0x16 0 0 0 0x0000 0 1
#55case-551518 0x8000 0x000F 1 0x17 0 0 0 0x0001 0 0
#56case-561561 0x8000 0x000F 1 0x18 0 0 0 0x000F 0 0
#57case-571584 0x8000 0x000F 1 0x19 0 0 1 0x8000 0 0
#58case-581607 0x8000 0x000F 1 0x1A 0 0 1 0x8000 0 0
#59case-591630 0x8000 0x000F 1 0x1B 0 0 0 0x000F 0 0
#60case-601657 0x8000 0x000F 1 0x1C 0 0 1 0x8000 0 0
#61case-611683 0x8000 0x000F 1 0x1D 0 0 0 0x000F 0 0
#62case-621712 0x8000 0x000F 1 0x1E 0 0 0 0x0000 0 1
#63case-631735 0x8000 0x000F 1 0x1F 0 1 0 0x0000 0 1

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 primary recording uses ALU<16>. It checks every output for all 32 opcode encodings with two operand/carry sets (64 rows). The same generator records ALU<8> and ALU<32>.

The signed comparison and signed-minimum examples return different kinds of data: SLT returns a positive predicate word, while MINS returns the negative operand's original bits. Their NEGATIVE flags follow those output words, not the internal subtraction.

Embedded simulation

Expand ARITHMETIC → ADD_SUB → ADD to inspect the prefix adder's carry levels at distances 1, 2, 4 and 8.

Open ALU<16> in the simulator

The other widths are ALU<8> and ALU<32>.

Source and tests

Test Coverage
ALUScenarioTest, ALUWidth8ScenarioTest, ALUWidth32ScenarioTest The 64 recorded rows at each width
ALUWidthContractTest W = 8, 16 and 32: all 32 opcodes with boundary operands and both carry inputs, every shift distance, and targeted X/Z data and control; at 16 and 32 bits also a 6 × 6 boundary grid on the 14 arithmetic and compare opcodes and 512 seeded random vectors. All against an independent binary reference on the gate-level form
ALUContractTest ALU<8> on both forms: 5,184 boundary and 2,048 random binary cases, 28 targeted X/Z cases, and one shared AddSub<8> with one ParallelPrefixAdder<8>
ALUFidelityContractTest Every compact Chapter 2 form against its gates, including every four-state opcode; 13 mixed-fidelity ALU<8> profiles
ALUHierarchyTest The seven named groups of ALU<8> and their single shared adder

These checks are not exhaustive over the full ALU's input space.