§ 2.3. Shifts and Rotations¶
BarrelShifter<W> produces shifts, rotations and the last displaced bit. It is combinational: a mux network selects the distance, with no repeated clocked shifting.
| Component | Description | Structural | Behavioral |
|---|---|---|---|
BarrelShifter<W> |
Variable-distance shifts/rotations with carry | 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.3.1. BarrelShifter<W>¶
IO pins¶
| Pin | Direction | Width | Description |
|---|---|---|---|
A |
Input | W | Word to shift or rotate |
B |
Input | W | Distance from the low log₂(W) bits; upper bits ignored |
OP |
Input | 3 | Local shift/rotation selector |
C_IN |
Input | 1 | Carry returned for valid distance zero |
OUT |
Output | W | Shifted or rotated word |
CARRY |
Output | 1 | Last displaced bit, or C_IN for distance zero |
Behavior¶
The effective distance is n = B & (W−1), ranging from 0 to W−1.
| Local OP | Operation | Movement/fill | ALU OP |
|---|---|---|---|
| 0 | SLL | Left, zero fill | 16 |
| 1 | SRL | Right, zero fill | 17 |
| 2 | SRA | Right, fill with A[W−1] | 18 |
| 3 | ROL | Left, wrap displaced bits | 19 |
| 4 | ROR | Right, wrap displaced bits | 20 |
| 5–7 | Reserved | OUT=0, CARRY=0 | Other ALU groups |
For valid n=0, OUT=A and CARRY=C_IN. For n>0, SLL/ROL carry is original A[W−n]; SRL/SRA/ROR carry is original A[n−1]. C_IN never fills OUT and is ignored at nonzero distance. Rotation covers exactly W bits. Arithmetic right shift fills the sign and rounds negative signed values toward negative infinity.
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 <cstdint>
template<unsigned W>
struct ShiftResult {
std::uint32_t OUT;
bool CARRY;
};
template<unsigned W>
ShiftResult<W> shift_ref(
std::uint32_t a, std::uint32_t b, std::uint32_t op, bool c_in) {
if (op > 4) return {0, false};
const unsigned n = unsigned(b) & (W - 1);
if (n == 0) return {a, c_in};
const std::uint64_t x = a;
std::uint64_t result = 0;
switch (op) {
case 0: result = x << n; break; // SLL
case 1: result = x >> n; break; // SRL
case 2: // SRA
result = (x >> n) | ((x & WordReference<W>::sign) ? (WordReference<W>::mask << (W - n)) : 0u);
break;
case 3: result = (x << n) | (x >> (W - n)); break; // ROL
case 4: result = (x >> n) | (x << (W - n)); break; // ROR
}
const unsigned carry_bit = (op == 0 || op == 3) ? W - n : n - 1;
return {
static_cast<std::uint32_t>(result & WordReference<W>::mask),
((x >> carry_bit) & 1u) != 0,
};
}
Unknown inputs¶
Unused upper B bits do not affect the circuit. Unknown C_IN is isolated at nonzero distance; at distance zero, X/Z carry produces X through the gates. Relevant unknown controls follow mux logic, while known zero-fill positions can remain determined. Reserved local codes mask both outputs.
Implementation¶
Structural implementation¶
The word muxes INPUT_REVERSE and OUTPUT_REVERSE (Mux<2,W>, one 2:1 mux per bit) reverse the bit order for left shifts, so one rightward network serves both directions. There are log₂(W) optional stages SHIFT_1, SHIFT_2, SHIFT_4, …, SHIFT_W/2; each is one Mux<2,W> choosing between the word and the word moved down by its distance. Moving and reversing a word is wiring only (splitters and joiners). The top bits of a moved word come from fill selection: zero, the sign or, for rotation, the bits moved out at the bottom; this is one 2:1 mux per bit, grouped as a word mux (SHIFT_d_FILL) from four bits up. Each stage also selects a displaced carry bit (SHIFT_d_CARRY); carry starts at C_IN and survives only when every distance stage is disabled. The word gate VALID (ANDGate<W>) and one AND mask the final word and carry for reserved operations. These are the same one-bit muxes and gates as a bit-by-bit circuit, grouped as words (word gates).
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 96 recorded rows of BarrelShifterWidth8Test, 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 |
OUTExpected output · 8 bit |
|---|---|---|---|---|---|---|---|
#0row-0 | 18 | 0x81 |
0x00 |
0 |
0x0 |
0 |
0x81 |
#1row-1 | 26 | 0x81 |
0x00 |
1 |
0x0 |
1 |
0x81 |
#2row-2 | 37 | 0x81 |
0x01 |
0 |
0x0 |
1 |
0x02 |
#3row-3 | 39 | 0x81 |
0x01 |
1 |
0x0 |
1 |
0x02 |
#4row-4 | 48 | 0x81 |
0x03 |
0 |
0x0 |
0 |
0x08 |
#5row-5 | 50 | 0x81 |
0x03 |
1 |
0x0 |
0 |
0x08 |
#6row-6 | 57 | 0x81 |
0x07 |
0 |
0x0 |
0 |
0x80 |
#7row-7 | 59 | 0x81 |
0x07 |
1 |
0x0 |
0 |
0x80 |
#8row-8 | 70 | 0x81 |
0x08 |
0 |
0x0 |
0 |
0x81 |
#9row-9 | 78 | 0x81 |
0x08 |
1 |
0x0 |
1 |
0x81 |
#10row-10 | 89 | 0x81 |
0xFF |
0 |
0x0 |
0 |
0x80 |
#11row-11 | 91 | 0x81 |
0xFF |
1 |
0x0 |
0 |
0x80 |
#12row-12 | 111 | 0x81 |
0x00 |
0 |
0x1 |
0 |
0x81 |
#13row-13 | 119 | 0x81 |
0x00 |
1 |
0x1 |
1 |
0x81 |
#14row-14 | 130 | 0x81 |
0x01 |
0 |
0x1 |
1 |
0x40 |
#15row-15 | 132 | 0x81 |
0x01 |
1 |
0x1 |
1 |
0x40 |
#16row-16 | 141 | 0x81 |
0x03 |
0 |
0x1 |
0 |
0x10 |
#17row-17 | 143 | 0x81 |
0x03 |
1 |
0x1 |
0 |
0x10 |
#18row-18 | 150 | 0x81 |
0x07 |
0 |
0x1 |
0 |
0x01 |
#19row-19 | 152 | 0x81 |
0x07 |
1 |
0x1 |
0 |
0x01 |
#20row-20 | 163 | 0x81 |
0x08 |
0 |
0x1 |
0 |
0x81 |
#21row-21 | 171 | 0x81 |
0x08 |
1 |
0x1 |
1 |
0x81 |
#22row-22 | 182 | 0x81 |
0xFF |
0 |
0x1 |
0 |
0x01 |
#23row-23 | 184 | 0x81 |
0xFF |
1 |
0x1 |
0 |
0x01 |
#24row-24 | 203 | 0x81 |
0x00 |
0 |
0x2 |
0 |
0x81 |
#25row-25 | 211 | 0x81 |
0x00 |
1 |
0x2 |
1 |
0x81 |
#26row-26 | 222 | 0x81 |
0x01 |
0 |
0x2 |
1 |
0xC0 |
#27row-27 | 224 | 0x81 |
0x01 |
1 |
0x2 |
1 |
0xC0 |
#28row-28 | 232 | 0x81 |
0x03 |
0 |
0x2 |
0 |
0xF0 |
#29row-29 | 234 | 0x81 |
0x03 |
1 |
0x2 |
0 |
0xF0 |
#30row-30 | 240 | 0x81 |
0x07 |
0 |
0x2 |
0 |
0xFF |
#31row-31 | 242 | 0x81 |
0x07 |
1 |
0x2 |
0 |
0xFF |
#32row-32 | 253 | 0x81 |
0x08 |
0 |
0x2 |
0 |
0x81 |
#33row-33 | 261 | 0x81 |
0x08 |
1 |
0x2 |
1 |
0x81 |
#34row-34 | 271 | 0x81 |
0xFF |
0 |
0x2 |
0 |
0xFF |
#35row-35 | 273 | 0x81 |
0xFF |
1 |
0x2 |
0 |
0xFF |
#36row-36 | 284 | 0x81 |
0x00 |
0 |
0x3 |
0 |
0x81 |
#37row-37 | 292 | 0x81 |
0x00 |
1 |
0x3 |
1 |
0x81 |
#38row-38 | 303 | 0x81 |
0x01 |
0 |
0x3 |
1 |
0x03 |
#39row-39 | 305 | 0x81 |
0x01 |
1 |
0x3 |
1 |
0x03 |
#40row-40 | 314 | 0x81 |
0x03 |
0 |
0x3 |
0 |
0x0C |
#41row-41 | 316 | 0x81 |
0x03 |
1 |
0x3 |
0 |
0x0C |
#42row-42 | 323 | 0x81 |
0x07 |
0 |
0x3 |
0 |
0xC0 |
#43row-43 | 325 | 0x81 |
0x07 |
1 |
0x3 |
0 |
0xC0 |
#44row-44 | 336 | 0x81 |
0x08 |
0 |
0x3 |
0 |
0x81 |
#45row-45 | 344 | 0x81 |
0x08 |
1 |
0x3 |
1 |
0x81 |
#46row-46 | 357 | 0x81 |
0xFF |
0 |
0x3 |
0 |
0xC0 |
#47row-47 | 359 | 0x81 |
0xFF |
1 |
0x3 |
0 |
0xC0 |
#48row-48 | 375 | 0x81 |
0x00 |
0 |
0x4 |
0 |
0x81 |
#49row-49 | 383 | 0x81 |
0x00 |
1 |
0x4 |
1 |
0x81 |
#50row-50 | 394 | 0x81 |
0x01 |
0 |
0x4 |
1 |
0xC0 |
#51row-51 | 396 | 0x81 |
0x01 |
1 |
0x4 |
1 |
0xC0 |
#52row-52 | 405 | 0x81 |
0x03 |
0 |
0x4 |
0 |
0x30 |
#53row-53 | 407 | 0x81 |
0x03 |
1 |
0x4 |
0 |
0x30 |
#54row-54 | 414 | 0x81 |
0x07 |
0 |
0x4 |
0 |
0x03 |
#55row-55 | 416 | 0x81 |
0x07 |
1 |
0x4 |
0 |
0x03 |
#56row-56 | 427 | 0x81 |
0x08 |
0 |
0x4 |
0 |
0x81 |
#57row-57 | 435 | 0x81 |
0x08 |
1 |
0x4 |
1 |
0x81 |
#58row-58 | 448 | 0x81 |
0xFF |
0 |
0x4 |
0 |
0x03 |
#59row-59 | 450 | 0x81 |
0xFF |
1 |
0x4 |
0 |
0x03 |
#60row-60 | 461 | 0x81 |
0x00 |
0 |
0x5 |
0 |
0x00 |
#61row-61 | 469 | 0x81 |
0x00 |
1 |
0x5 |
0 |
0x00 |
#62row-62 | 480 | 0x81 |
0x01 |
0 |
0x5 |
0 |
0x00 |
#63row-63 | 482 | 0x81 |
0x01 |
1 |
0x5 |
0 |
0x00 |
#64row-64 | 491 | 0x81 |
0x03 |
0 |
0x5 |
0 |
0x00 |
#65row-65 | 493 | 0x81 |
0x03 |
1 |
0x5 |
0 |
0x00 |
#66row-66 | 500 | 0x81 |
0x07 |
0 |
0x5 |
0 |
0x00 |
#67row-67 | 502 | 0x81 |
0x07 |
1 |
0x5 |
0 |
0x00 |
#68row-68 | 513 | 0x81 |
0x08 |
0 |
0x5 |
0 |
0x00 |
#69row-69 | 521 | 0x81 |
0x08 |
1 |
0x5 |
0 |
0x00 |
#70row-70 | 532 | 0x81 |
0xFF |
0 |
0x5 |
0 |
0x00 |
#71row-71 | 534 | 0x81 |
0xFF |
1 |
0x5 |
0 |
0x00 |
#72row-72 | 545 | 0x81 |
0x00 |
0 |
0x6 |
0 |
0x00 |
#73row-73 | 553 | 0x81 |
0x00 |
1 |
0x6 |
0 |
0x00 |
#74row-74 | 564 | 0x81 |
0x01 |
0 |
0x6 |
0 |
0x00 |
#75row-75 | 566 | 0x81 |
0x01 |
1 |
0x6 |
0 |
0x00 |
#76row-76 | 575 | 0x81 |
0x03 |
0 |
0x6 |
0 |
0x00 |
#77row-77 | 577 | 0x81 |
0x03 |
1 |
0x6 |
0 |
0x00 |
#78row-78 | 584 | 0x81 |
0x07 |
0 |
0x6 |
0 |
0x00 |
#79row-79 | 586 | 0x81 |
0x07 |
1 |
0x6 |
0 |
0x00 |
#80row-80 | 597 | 0x81 |
0x08 |
0 |
0x6 |
0 |
0x00 |
#81row-81 | 605 | 0x81 |
0x08 |
1 |
0x6 |
0 |
0x00 |
#82row-82 | 616 | 0x81 |
0xFF |
0 |
0x6 |
0 |
0x00 |
#83row-83 | 618 | 0x81 |
0xFF |
1 |
0x6 |
0 |
0x00 |
#84row-84 | 629 | 0x81 |
0x00 |
0 |
0x7 |
0 |
0x00 |
#85row-85 | 637 | 0x81 |
0x00 |
1 |
0x7 |
0 |
0x00 |
#86row-86 | 648 | 0x81 |
0x01 |
0 |
0x7 |
0 |
0x00 |
#87row-87 | 650 | 0x81 |
0x01 |
1 |
0x7 |
0 |
0x00 |
#88row-88 | 659 | 0x81 |
0x03 |
0 |
0x7 |
0 |
0x00 |
#89row-89 | 661 | 0x81 |
0x03 |
1 |
0x7 |
0 |
0x00 |
#90row-90 | 668 | 0x81 |
0x07 |
0 |
0x7 |
0 |
0x00 |
#91row-91 | 670 | 0x81 |
0x07 |
1 |
0x7 |
0 |
0x00 |
#92row-92 | 681 | 0x81 |
0x08 |
0 |
0x7 |
0 |
0x00 |
#93row-93 | 689 | 0x81 |
0x08 |
1 |
0x7 |
0 |
0x00 |
#94row-94 | 700 | 0x81 |
0xFF |
0 |
0x7 |
0 |
0x00 |
#95row-95 | 702 | 0x81 |
0xFF |
1 |
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.
Embedded simulation¶
The barrel-shifter-w8 scenario runs BarrelShifterWidth8Test: 96 recorded cases using A = 0x81, all eight local selections, six B values (0, 1, 3, 7, 8, 255), and both carry inputs. Checkpoints expose the zero-distance carry rule, masked distance, and reserved selections as well as shifts and rotations.
Open BarrelShifter<8> in the simulator
Source and tests¶
| Test | Coverage |
|---|---|
BarrelShifterWidth8Test |
96 recorded binary cases: eight local selections × six B values × both carry inputs, with A = 0x81. Checks OUT and CARRY; this is the embedded scenario. |
ALUContractTest: standalone shifter sweep |
20,480 cases: five valid operations × 256 A values × eight effective distances × both carry inputs. Every case drives B[7:3] = 0b11111. |
ALUContractTest: integrated checks |
Boundary and seeded random ALU cases, plus targeted X/Z cases for ignored upper distance bits, ignored carry at nonzero distance, and unknown carry at zero distance. |
The 20,480-case sweep is exhaustive over valid operations, data words, effective distances, and binary carry inputs, but not over every eight-bit B value, reserved local selection, or four-state pattern. It disables history recording; the embedded simulation contains only the 96 recorded cases.