Skip to content

§ 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-018 0x81 0x00 0 0x0 0 0x81
#1row-126 0x81 0x00 1 0x0 1 0x81
#2row-237 0x81 0x01 0 0x0 1 0x02
#3row-339 0x81 0x01 1 0x0 1 0x02
#4row-448 0x81 0x03 0 0x0 0 0x08
#5row-550 0x81 0x03 1 0x0 0 0x08
#6row-657 0x81 0x07 0 0x0 0 0x80
#7row-759 0x81 0x07 1 0x0 0 0x80
#8row-870 0x81 0x08 0 0x0 0 0x81
#9row-978 0x81 0x08 1 0x0 1 0x81
#10row-1089 0x81 0xFF 0 0x0 0 0x80
#11row-1191 0x81 0xFF 1 0x0 0 0x80
#12row-12111 0x81 0x00 0 0x1 0 0x81
#13row-13119 0x81 0x00 1 0x1 1 0x81
#14row-14130 0x81 0x01 0 0x1 1 0x40
#15row-15132 0x81 0x01 1 0x1 1 0x40
#16row-16141 0x81 0x03 0 0x1 0 0x10
#17row-17143 0x81 0x03 1 0x1 0 0x10
#18row-18150 0x81 0x07 0 0x1 0 0x01
#19row-19152 0x81 0x07 1 0x1 0 0x01
#20row-20163 0x81 0x08 0 0x1 0 0x81
#21row-21171 0x81 0x08 1 0x1 1 0x81
#22row-22182 0x81 0xFF 0 0x1 0 0x01
#23row-23184 0x81 0xFF 1 0x1 0 0x01
#24row-24203 0x81 0x00 0 0x2 0 0x81
#25row-25211 0x81 0x00 1 0x2 1 0x81
#26row-26222 0x81 0x01 0 0x2 1 0xC0
#27row-27224 0x81 0x01 1 0x2 1 0xC0
#28row-28232 0x81 0x03 0 0x2 0 0xF0
#29row-29234 0x81 0x03 1 0x2 0 0xF0
#30row-30240 0x81 0x07 0 0x2 0 0xFF
#31row-31242 0x81 0x07 1 0x2 0 0xFF
#32row-32253 0x81 0x08 0 0x2 0 0x81
#33row-33261 0x81 0x08 1 0x2 1 0x81
#34row-34271 0x81 0xFF 0 0x2 0 0xFF
#35row-35273 0x81 0xFF 1 0x2 0 0xFF
#36row-36284 0x81 0x00 0 0x3 0 0x81
#37row-37292 0x81 0x00 1 0x3 1 0x81
#38row-38303 0x81 0x01 0 0x3 1 0x03
#39row-39305 0x81 0x01 1 0x3 1 0x03
#40row-40314 0x81 0x03 0 0x3 0 0x0C
#41row-41316 0x81 0x03 1 0x3 0 0x0C
#42row-42323 0x81 0x07 0 0x3 0 0xC0
#43row-43325 0x81 0x07 1 0x3 0 0xC0
#44row-44336 0x81 0x08 0 0x3 0 0x81
#45row-45344 0x81 0x08 1 0x3 1 0x81
#46row-46357 0x81 0xFF 0 0x3 0 0xC0
#47row-47359 0x81 0xFF 1 0x3 0 0xC0
#48row-48375 0x81 0x00 0 0x4 0 0x81
#49row-49383 0x81 0x00 1 0x4 1 0x81
#50row-50394 0x81 0x01 0 0x4 1 0xC0
#51row-51396 0x81 0x01 1 0x4 1 0xC0
#52row-52405 0x81 0x03 0 0x4 0 0x30
#53row-53407 0x81 0x03 1 0x4 0 0x30
#54row-54414 0x81 0x07 0 0x4 0 0x03
#55row-55416 0x81 0x07 1 0x4 0 0x03
#56row-56427 0x81 0x08 0 0x4 0 0x81
#57row-57435 0x81 0x08 1 0x4 1 0x81
#58row-58448 0x81 0xFF 0 0x4 0 0x03
#59row-59450 0x81 0xFF 1 0x4 0 0x03
#60row-60461 0x81 0x00 0 0x5 0 0x00
#61row-61469 0x81 0x00 1 0x5 0 0x00
#62row-62480 0x81 0x01 0 0x5 0 0x00
#63row-63482 0x81 0x01 1 0x5 0 0x00
#64row-64491 0x81 0x03 0 0x5 0 0x00
#65row-65493 0x81 0x03 1 0x5 0 0x00
#66row-66500 0x81 0x07 0 0x5 0 0x00
#67row-67502 0x81 0x07 1 0x5 0 0x00
#68row-68513 0x81 0x08 0 0x5 0 0x00
#69row-69521 0x81 0x08 1 0x5 0 0x00
#70row-70532 0x81 0xFF 0 0x5 0 0x00
#71row-71534 0x81 0xFF 1 0x5 0 0x00
#72row-72545 0x81 0x00 0 0x6 0 0x00
#73row-73553 0x81 0x00 1 0x6 0 0x00
#74row-74564 0x81 0x01 0 0x6 0 0x00
#75row-75566 0x81 0x01 1 0x6 0 0x00
#76row-76575 0x81 0x03 0 0x6 0 0x00
#77row-77577 0x81 0x03 1 0x6 0 0x00
#78row-78584 0x81 0x07 0 0x6 0 0x00
#79row-79586 0x81 0x07 1 0x6 0 0x00
#80row-80597 0x81 0x08 0 0x6 0 0x00
#81row-81605 0x81 0x08 1 0x6 0 0x00
#82row-82616 0x81 0xFF 0 0x6 0 0x00
#83row-83618 0x81 0xFF 1 0x6 0 0x00
#84row-84629 0x81 0x00 0 0x7 0 0x00
#85row-85637 0x81 0x00 1 0x7 0 0x00
#86row-86648 0x81 0x01 0 0x7 0 0x00
#87row-87650 0x81 0x01 1 0x7 0 0x00
#88row-88659 0x81 0x03 0 0x7 0 0x00
#89row-89661 0x81 0x03 1 0x7 0 0x00
#90row-90668 0x81 0x07 0 0x7 0 0x00
#91row-91670 0x81 0x07 1 0x7 0 0x00
#92row-92681 0x81 0x08 0 0x7 0 0x00
#93row-93689 0x81 0x08 1 0x7 0 0x00
#94row-94700 0x81 0xFF 0 0x7 0 0x00
#95row-95702 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.