The Barrel Shifter
In the previous article, we established that constant shifts require zero logic gates because hardwired copper interconnects route shifted positions directly. However, when the shift amount is determined dynamically at run-time by an operand register, the circuit requires active logic. A sequential chain of single-bit shifters introduces variable delay that depends linearly on shift magnitude. This article examines the Barrel Shifter. A barrel shifter executes variable multi-bit shifts by any amount from 0 to $N-1$ in a single clock cycle with fixed logarithmic delay ($\log_2 N$ multiplexer stages).
Logarithmic Shift Decomposition Principle
Any non-negative integer shift amount $s$ ($0 \le s \le N-1$) can be expressed as a binary sum of powers of two:
$$s = \sum_{j=0}^{k-1} s_j \cdot 2^j = s_{k-1} 2^{k-1} + \dots + s_1 \cdot 2^1 + s_0 \cdot 2^0$$
where $k = \lceil \log_2 N \rceil$. Instead of shifting sequentially by one bit $s$ times, a barrel shifter cascades $k$ discrete stages:
- Stage 0 shifts by $2^0 = 1$ bit position if bit $s_0 = 1$.
- Stage 1 shifts by $2^1 = 2$ bit positions if bit $s_1 = 1$.
- Stage 2 shifts by $2^2 = 4$ bit positions if bit $s_2 = 1$.
- Stage $j$ shifts by $2^j$ bit positions if bit $s_j = 1$.
Each stage consists of a parallel bank of 2-to-1 multiplexers controlled by a single bit of the shift control vector ($s_j$).
Architecture of an 8-Bit Right Barrel Shifter
An 8-bit barrel shifter requires $k = \log_2 8 = 3$ stages controlled by shift bits $s_2, s_1, s_0$ (Figure 1):
- Stage 0 ($s_0$, Shift by 1): Multiplexer $i$ selects between input $D_i$ (pass-through when $s_0 = 0$) and input $D_{i+1}$ (shift by 1 when $s_0 = 1$). The most significant multiplexer inputs logic 0.
- Stage 1 ($s_1$, Shift by 2): Multiplexer $i$ selects between stage 0 output $D’i$ and $D’{i+2}$. The two upper multiplexers input logic 0.
- Stage 2 ($s_2$, Shift by 4): Multiplexer $i$ selects between stage 1 output $D’’i$ and $D’’{i+4}$. The four upper multiplexers input logic 0.
Numerical Trace Walkthrough ($180 \gg 5$)
Shift input $D = 1011_0100_2$ ($180_{10}$) right by $s = 5_{10} = 101_2$ ($s_2 = 1, s_1 = 0, s_0 = 1$):
- Theoretical result: $\lfloor 180 / 32 \rfloor = 5_{10} = 0000_0101_2$.
Trace data through the three stages:
- Stage 0 ($s_0 = 1$, Shift by 1): $$1011_0100_2 \to 0101_1010_2$$
- Stage 1 ($s_1 = 0$, Pass-Through): $$0101_1010_2 \to 0101_1010_2$$
- Stage 2 ($s_2 = 1$, Shift by 4): $$0101_1010_2 \to 0000_0101_2$$
Output result equals $0000_0101_2 = 5_{10}$. Total propagation delay equals exactly three 2-to-1 multiplexer delays.
Latency and Area Complexity
Total propagation delay is strictly deterministic:
$$t_{\text{shifter}} = \lceil \log_2 N \rceil \times t_{\text{MUX}}$$
Table 1 details hardware area and stage counts across word widths:
| Word Width ($N$) | Required Stages ($\log_2 N$) | 2-to-1 Multiplexers ($N \log_2 N$) | Equivalent Logic Gates |
|---|---|---|---|
| 8 bits | 3 | 24 | $\approx 72$ gates |
| 16 bits | 4 | 64 | $\approx 192$ gates |
| 32 bits | 5 | 160 | $\approx 480$ gates |
| 64 bits | 6 | 384 | $\approx 1{,}152$ gates |
Doubling operand width adds only one multiplexer propagation delay to the critical path.
Arithmetic Right Shift and Sign-Extension Gating
To support Arithmetic Shift Right (ASR), the circuit replaces zero fill bits with copies of the original sign bit ($D_{N-1}$):
$$\text{fill} = \text{arith_mode} \cdot D_{N-1}$$
The sign bit connects to the upper multiplexer inputs of each stage through AND gates. Because the sign bit remains constant across arithmetic right shifts, this modification requires only $N - 1$ AND gates without altering the multiplexer pipeline depth.
Bidirectional and Circular Rotation Extensions
A barrel shifter can support both left and right directions using input and output bit reversal stages (Figure 2):
- To execute a left shift:
- Reverse the input bit order: $D[7:0] \to D[0:7]$.
- Perform a right shift through the standard barrel shifter.
- Reverse the output bit order.
- Bit reversals use hardwired metal wiring with zero gate delay. Multiplexers at the input and output select reversed or non-reversed buses.
Circular rotations (ROL and ROR) connect vacated upper inputs to wrapped lower bit outputs rather than to constant fill lines.
The Funnel Shifter Architecture
A Funnel Shifter generalizes the barrel shifter architecture. It receives two $N$-bit operands ($A$ and $B$) concatenated into a $2N$-bit vector and extracts an $N$-bit window at any arbitrary offset $k$:
$$\text{Output}[N-1:0] = {A, B}[k + N - 1 : k]$$
Funnel shifters perform multiple datapath functions:
- Unaligned Memory Loads: Extracts misaligned words spanning adjacent memory boundaries.
- Floating-Point Alignment: Shifts smaller mantissas during exponent matching while retaining guard and sticky bits.
- Double-Word Rotations: Shifts 64-bit concatenated register pairs in 32-bit execution units.
Datapath Applications and FPGA Implementation
In ARM Cortex-M processors, the barrel shifter precedes the ALU in the primary datapath.
Instructions such as ADD R0, R1, R2, LSL #3 execute a variable shift and addition within a single pipeline cycle.
In fixed-point digital signal processing, barrel shifters rescale Q15 fractional products in one cycle.
On FPGAs, modern 6-input Look-Up Tables (LUT6) implement 4-to-1 multiplexers natively. Synthesis compilers fold two barrel shifter stages into a single LUT level, reducing 32-bit shifter depth from 5 to 3 LUT delays.
Practice Exercises
Problem 1: Construct a 4-bit right barrel shifter with a 2-bit shift control vector ($s_1, s_0$). Trace execution for input $D = 1101_2$ ($13_{10}$) shifted right by $s = 3_{10} = 11_2$.
View Solution
- Hardware Configuration:
- Stage 0 ($s_0$): Shifts by 1 position. Multiplexer $i$ selects between $D_i$ and $D_{i+1}$, with 0 at the MSB.
- Stage 1 ($s_1$): Shifts by 2 positions. Multiplexer $i$ selects between stage 0 output $D’i$ and $D’{i+2}$, with 0 at the upper two inputs.
- Execution Trace ($s_1 = 1, s_0 = 1$):
- Initial input: $1101_2$.
- Stage 0 ($s_0 = 1$, Shift 1): $1101_2 \to 0110_2$.
- Stage 1 ($s_1 = 1$, Shift 2): $0110_2 \to 0001_2$.
- Verification: $$\lfloor 13 / 2^3 \rfloor = \lfloor 13 / 8 \rfloor = 1_{10} = 0001_2$$ The output is verified.
Problem 2: A 32-bit barrel shifter implemented in an ASIC library has a multiplexer delay of 120 ps per stage. Calculate the total propagation delay through the shifter core and determine the maximum operating frequency when allowing 100 ps for register setup and 100 ps for clock skew.
View Solution
- Shifter Core Propagation Delay: $$\text{Stages} = \log_2 32 = 5 \text{ stages}$$ $$t_{\text{core}} = 5 \times 120\text{ ps} = 600\text{ ps}$$
- Total Clock Period ($T_{\text{clk}}$): $$T_{\text{clk}} = t_{\text{core}} + t_{\text{setup}} + t_{\text{skew}} = 600\text{ ps} + 100\text{ ps} + 100\text{ ps} = 800\text{ ps}$$
- Maximum Operating Frequency: $$f_{\text{max}} = \frac{1}{800\text{ ps}} = 1.25\text{ GHz}$$
The barrel shifter supports operating frequencies up to 1.25 GHz.
Problem 3: Specify the hardware modifications required to adapt an 8-bit right barrel shifter for arithmetic right shift operations.
View Solution
To support arithmetic right shifting:
- Identify the vacated upper inputs:
- Stage 0 requires 1 fill bit.
- Stage 1 requires 2 fill bits.
- Stage 2 requires 4 fill bits.
- Connect these seven fill inputs to the output of an AND gate that computes: $$\text{fill} = \text{arith_enable} \cdot D_7$$ where $D_7$ is the original sign bit of the input word.
- When
arith_enable= 0, the fill bits equal 0 (Logical Shift Right). Whenarith_enable= 1, the fill bits replicate sign bit $D_7$ (Arithmetic Shift Right with Sign Extension).
This concludes Section 2.3 on multipliers and shifters. In Section 2.4, we examine combinational timing hazards and glitch elimination techniques.