§ 2.4. Comparison and Selection¶
ZeroDetect<W> reduces a word to a predicate. CompareSelect<W> consumes a subtraction result and its flags to select predicates or min/max operands. The complete ALU shares its subtraction hardware between arithmetic and comparison.
| Component | Description | Structural | Behavioral |
|---|---|---|---|
ZeroDetect<W> |
One-bit all-zero predicate | Yes | Yes |
CompareSelect<W> |
Equality/less-than predicates and signed/unsigned min/max | Yes | Yes |
W is 8, 16 or 32. The fidelity columns hold at every width: each unit has a structural and a compact form at 8, 16 and 32 bits. The C++ references take W as a template parameter. Construction and selection describes construction.
2.4.1. ZeroDetect<W>¶
IO pins¶
| Pin | Direction | Width | Description |
|---|---|---|---|
A |
Input | W | Word to inspect |
ZERO |
Output | 1 | High when all W bits are zero |
Behavior¶
ZERO is NOT of the OR reduction of A[0] through A[W−1]. It is a one-bit predicate with no signed interpretation, clock or state.
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.
Unknown inputs¶
A known one anywhere makes ZERO=0. All known zeros make ZERO=1. With no known one, any X/Z makes ZERO=X. The output never drives Z.
Implementation¶
Structural implementation¶
A splitter feeds an OR reduction and a final NOT. The eight-bit tree has seven OR gates in three levels; larger widths use the width-aware reduction recipe. The same component serves equality detection and the complete ALU's result-zero flag.
Behavioral implementation¶
The compact form computes the same OR reduction with four-state values, so its output matches the gates for every input. It exists at W = 8 and 32 (the 16-bit detector is structural) and runs on delays proven on the gates.
Examples¶
All 11 recorded rows of ZeroDetectWidth8Test, 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 |
ZEROExpected output · 1 bit |
|---|---|---|---|
#0row-0 | 4 | 0x00 |
1 |
#1row-1 | 9 | 0x01 |
0 |
#2row-2 | 11 | 0x02 |
0 |
#3row-3 | 15 | 0x10 |
0 |
#4row-4 | 19 | 0x7F |
0 |
#5row-5 | 23 | 0x80 |
0 |
#6row-6 | 27 | 0xFF |
0 |
#7row-7 | 32 | 0b0000X000 |
X |
#8row-8 | 37 | 0b1000X000 |
0 |
#9row-9 | 42 | 0b0000Z000 |
X |
#10row-10 | 47 | 0b1000Z000 |
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.
This recording instantiates W = 8; the C++ reference accepts every supported W. Width/configuration coverage is listed in the tests below.
Embedded simulation¶
The zero-detect-w8 scenario runs ZeroDetectWidth8Test: seven binary cases and four four-state cases. The latter drive bit 3 to X or Z, first without a known 1 elsewhere and then with bit 7 high. Use checkpoints to compare the resulting unknown and known-zero predicates.
Open ZeroDetect<8> in the simulator
Source and tests¶
| Test | Coverage |
|---|---|
ZeroDetectWidth8Test / zeroDetect8Rows() |
11 recorded cases: seven binary words and four X/Z cases. Checks ZERO; this is the embedded scenario. |
CompareSelectWidth8Test and ALUContractTest |
Exercise zero detection through equality selection and the integrated ALU's result flags. These are integration checks, not a standalone exhaustive zero-detector sweep. |
ZeroDetectWidth8Test does not sweep all 256 binary words or all four-state patterns.
2.4.2. CompareSelect<W>¶
IO pins¶
| Pin | Direction | Width | Description |
|---|---|---|---|
A, B |
Input | W each | Original operands for min/max |
DIFF |
Input | W | Wrapped A−B |
CARRY_IN |
Input | 1 | Subtraction carry-out; high means no borrow |
OVERFLOW_IN |
Input | 1 | Subtraction signed overflow |
OP |
Input | 3 | Local comparison selector |
OUT |
Output | W | Zero/one predicate or selected original operand |
Behavior¶
The caller supplies consistent subtraction outputs. Decisions are E = [DIFF == 0], Lu = NOT CARRY_IN and Ls = DIFF[W−1] XOR OVERFLOW_IN.
| Local OP | Operation | OUT | ALU OP |
|---|---|---|---|
| 0 | EQ | E, zero-extended | 21 |
| 1 | SLTU | Lu, zero-extended | 22 |
| 2 | SLT | Ls, zero-extended | 23 |
| 3 | MINU | A if Lu, otherwise B | 24 |
| 4 | MAXU | B if Lu, otherwise A | 25 |
| 5 | MINS | A if Ls, otherwise B | 26 |
| 6 | MAXS | B if Ls, otherwise A | 27 |
| 7 | Reserved | Zero | No comparison operation |
The unit has no subtractor or flags. CARRY_IN is the subtraction's output, not an input carry operand. Min/max return original bit patterns. The extended complete ALU clears carry/overflow for these operations, although subtraction flags are used internally.
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>
std::uint32_t compare_select_ref(
std::uint32_t a, std::uint32_t b, std::uint32_t diff,
bool carry_in, bool overflow_in, std::uint32_t op) {
const bool equal = zero_detect_ref<W>(diff);
const bool unsigned_lt = !carry_in;
const bool signed_lt = ((diff & WordReference<W>::sign) != 0) != overflow_in;
switch (op) {
case 0: return static_cast<std::uint32_t>(equal);
case 1: return static_cast<std::uint32_t>(unsigned_lt);
case 2: return static_cast<std::uint32_t>(signed_lt);
case 3: return unsigned_lt ? a : b;
case 4: return unsigned_lt ? b : a;
case 5: return signed_lt ? a : b;
case 6: return signed_lt ? b : a;
default: return 0;
}
}
Unknown inputs¶
Each predicate uses only its declared subtraction signals. Known controlling bits can mask unknowns. Operand selections follow gate-built muxes rather than an optimistic merge; high impedance through logic becomes X. Known reserved OP masks OUT to zero.
Implementation¶
Structural implementation¶
One ZeroDetect<W>, no-borrow inversion and sign/overflow XOR produce E/Lu/Ls. Four word muxes MINU, MAXU, MINS and MAXS (Mux<2,W>, one 2:1 mux per bit) choose the unsigned and signed min/max words. The predicates become words with the flag in bit 0 (wiring only), and one Mux<8,W> (RESULT, an eight-way mux per bit) chooses predicates, operands or reserved zero. The shared subtraction remains in the parent arithmetic path.
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 40 recorded rows of CompareSelectWidth8Test, 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 |
CARRY_INInput · 1 bit |
DIFFInput · 8 bit |
OPInput · 3 bit |
OVERFLOW_INInput · 1 bit |
OUTExpected output · 8 bit |
|---|---|---|---|---|---|---|---|---|
#0row-0 | 10 | 0x00 |
0x01 |
0 |
0xFF |
0x0 |
0 |
0x00 |
#1row-1 | 21 | 0x01 |
0x01 |
1 |
0x00 |
0x0 |
0 |
0x01 |
#2row-2 | 32 | 0x7F |
0x01 |
1 |
0x7E |
0x0 |
0 |
0x00 |
#3row-3 | 40 | 0x80 |
0x01 |
1 |
0x7F |
0x0 |
1 |
0x00 |
#4row-4 | 48 | 0xFF |
0x01 |
1 |
0xFE |
0x0 |
0 |
0x00 |
#5row-5 | 56 | 0x00 |
0x01 |
0 |
0xFF |
0x1 |
0 |
0x01 |
#6row-6 | 66 | 0x01 |
0x01 |
1 |
0x00 |
0x1 |
0 |
0x00 |
#7row-7 | 72 | 0x7F |
0x01 |
1 |
0x7E |
0x1 |
0 |
0x00 |
#8row-8 | 82 | 0x80 |
0x01 |
1 |
0x7F |
0x1 |
1 |
0x00 |
#9row-9 | 90 | 0xFF |
0x01 |
1 |
0xFE |
0x1 |
0 |
0x00 |
#10row-10 | 98 | 0x00 |
0x01 |
0 |
0xFF |
0x2 |
0 |
0x01 |
#11row-11 | 108 | 0x01 |
0x01 |
1 |
0x00 |
0x2 |
0 |
0x00 |
#12row-12 | 116 | 0x7F |
0x01 |
1 |
0x7E |
0x2 |
0 |
0x00 |
#13row-13 | 126 | 0x80 |
0x01 |
1 |
0x7F |
0x2 |
1 |
0x01 |
#14row-14 | 132 | 0xFF |
0x01 |
1 |
0xFE |
0x2 |
0 |
0x01 |
#15row-15 | 143 | 0x00 |
0x01 |
0 |
0xFF |
0x3 |
0 |
0x00 |
#16row-16 | 154 | 0x01 |
0x01 |
1 |
0x00 |
0x3 |
0 |
0x01 |
#17row-17 | 160 | 0x7F |
0x01 |
1 |
0x7E |
0x3 |
0 |
0x01 |
#18row-18 | 168 | 0x80 |
0x01 |
1 |
0x7F |
0x3 |
1 |
0x01 |
#19row-19 | 174 | 0xFF |
0x01 |
1 |
0xFE |
0x3 |
0 |
0x01 |
#20row-20 | 184 | 0x00 |
0x01 |
0 |
0xFF |
0x4 |
0 |
0x01 |
#21row-21 | 195 | 0x01 |
0x01 |
1 |
0x00 |
0x4 |
0 |
0x01 |
#22row-22 | 205 | 0x7F |
0x01 |
1 |
0x7E |
0x4 |
0 |
0x7F |
#23row-23 | 214 | 0x80 |
0x01 |
1 |
0x7F |
0x4 |
1 |
0x80 |
#24row-24 | 223 | 0xFF |
0x01 |
1 |
0xFE |
0x4 |
0 |
0xFF |
#25row-25 | 232 | 0x00 |
0x01 |
0 |
0xFF |
0x5 |
0 |
0x00 |
#26row-26 | 243 | 0x01 |
0x01 |
1 |
0x00 |
0x5 |
0 |
0x01 |
#27row-27 | 249 | 0x7F |
0x01 |
1 |
0x7E |
0x5 |
0 |
0x01 |
#28row-28 | 260 | 0x80 |
0x01 |
1 |
0x7F |
0x5 |
1 |
0x80 |
#29row-29 | 269 | 0xFF |
0x01 |
1 |
0xFE |
0x5 |
0 |
0xFF |
#30row-30 | 277 | 0x00 |
0x01 |
0 |
0xFF |
0x6 |
0 |
0x01 |
#31row-31 | 288 | 0x01 |
0x01 |
1 |
0x00 |
0x6 |
0 |
0x01 |
#32row-32 | 297 | 0x7F |
0x01 |
1 |
0x7E |
0x6 |
0 |
0x7F |
#33row-33 | 308 | 0x80 |
0x01 |
1 |
0x7F |
0x6 |
1 |
0x01 |
#34row-34 | 314 | 0xFF |
0x01 |
1 |
0xFE |
0x6 |
0 |
0x01 |
#35row-35 | 324 | 0x00 |
0x01 |
0 |
0xFF |
0x7 |
0 |
0x00 |
#36row-36 | 334 | 0x01 |
0x01 |
1 |
0x00 |
0x7 |
0 |
0x00 |
#37row-37 | 340 | 0x7F |
0x01 |
1 |
0x7E |
0x7 |
0 |
0x00 |
#38row-38 | 348 | 0x80 |
0x01 |
1 |
0x7F |
0x7 |
1 |
0x00 |
#39row-39 | 354 | 0xFF |
0x01 |
1 |
0xFE |
0x7 |
0 |
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.
This recording instantiates W = 8; the C++ reference accepts every supported W. Width/configuration coverage is listed in the tests below.
The 0x80 versus 0x01 rows compare 128 with 1 as unsigned values, but −128 with 1 as signed values. The overflow input is necessary because the eight-bit difference wraps to the positive pattern 0x7F.
Embedded simulation¶
The compare-select-w8 scenario runs CompareSelectWidth8Test: 40 recorded cases using all eight local selections, five A values (0, 1, 127, 128, 255), and B = 1. The test driver supplies consistent DIFF, carry, and overflow for every case, including the reserved local opcode. All rows appear in the table above.
Open CompareSelect<8> in the simulator
Source and tests¶
- Interface:
CompareSelect.hpp - Structural recipe:
WordExtended.cpp - Shared subtraction wiring:
WordExtended.cpp
| Test | Coverage |
|---|---|
CompareSelectWidth8Test |
40 recorded binary cases: eight local selections × five A values, with B = 1 and consistent subtraction inputs. Checks OUT; this is the embedded scenario. |
ALUContractTest: integrated checks |
Includes 1,134 binary boundary cases for the seven comparison operations: seven opcodes × nine A values × nine B values × both top-level carry inputs. Seeded random ALU cases provide additional coverage. Subtraction signals come from the actual shared arithmetic unit. |
These checks are not exhaustive over operand pairs or independently driven DIFF/flag combinations. The recorded comparison scenario contains no X/Z cases. The separate ALUFidelityContractTest exercises independently driven DIFF/flags, sparse and dense X/Z data, and every four-state local selector against both fidelities.