# 桶形移位器

桶形移位器是一种组合数字电路，能够在单次操作中将数据字移位或循环移动任意数量的位位置，移位量由控制输入指定。与每个时钟周期只移动一个位置的移位寄存器不同，桶形移位器可以在一个周期内完成对 n 位字的任意指定移位，这正是它出现在通用处理器和数字信号处理器执行单元中的原因。<sup>[1](https://www.d.umn.edu/~gshute/logic/barrel-shifter.html)</sup><sup> • </sup><sup>[2](https://www.princeton.edu/~rblee/ELE572Papers/Fall04Readings/Shifter_Schulte.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 功能 | 在单周期内将 n 位字移位或循环移动 0 到 n−1 之间的任意量，属于纯组合逻辑，不含时序元件<sup>[1](https://www.d.umn.edu/~gshute/logic/barrel-shifter.html)</sup><sup> • </sup><sup>[3](https://iare.ac.in/sites/default/files/NewRegulationsSyllabi/UG20/B.TECH_ECE_UG20_ACADEMIC_REGULATIONS_AND_COURSE_CATALOG_2020-2021.pdf)</sup> |
| 结构 | log₂(n) 级多路复用器；由移位量的位 bₖ 控制的那一级在 bₖ = 1 时将数据移动 2ᵏ 个位置<sup>[2](https://www.princeton.edu/~rblee/ELE572Papers/Fall04Readings/Shifter_Schulte.pdf)</sup> |
| 硬件成本 | 处理 N 个符号的对数结构移位器需要 ⌈log₂(N)⌉ 行多路复用器，总计 N·nbit_data·⌈log₂(N)⌉ 个多路复用器<sup>[4](https://patents.justia.com/patent/20100293212)</sup> |
| 延迟 | 理论上恒定，每条路径至多经过一个传输门，与移位器规模无关；实际中输入电容随最大移位宽度增大<sup>[5](https://people.ee.duke.edu/~jmorizio/ece261/classlectures/arithcircuits2.pdf)</sup> |
| 覆盖的操作 | 逻辑右移、算术右移、循环右移、逻辑左移、算术左移、循环左移<sup>[2](https://www.princeton.edu/~rblee/ELE572Papers/Fall04Readings/Shifter_Schulte.pdf)</sup> |
| ARM 中的使用 | ARM 数据处理指令的第二个操作数在进入算术逻辑单元之前先经过桶形移位器，因此移位与算术运算合并为一条指令<sup>[6](https://seojuncha.github.io/arm/assembly/tutorial/2025/09/25/arm-sfhiter-operand.html)</sup> |
| 替代方案 | 串行移位器成本更低但速度更慢；Flute RISC-V 核心等处理器设计将两者都作为微架构选项提供<sup>[7](https://github.com/bluespec/Flute)</sup> |

## 工作原理

一个 n 位对数桶形移位器使用 log₂(n) 级，其中 n 为 2 的幂。移位量的每一位控制一级：进入由 bₖ 控制的那一级的数据，若该位为 1 则移动 2ᵏ 个位置，否则原样通过。第一级移一位，第二级移两位，第三级移四位，依此类推。若每一级都激活，总移位量为 2ⁿ + 2ⁿ⁻¹ + ... + 1 = 2ⁿ⁺¹ − 1，覆盖了 0 到 n−1 的完整范围；移位 n 个位置通常不等同于不移位，因为该操作会丢弃被移出的位。<sup>[2](https://www.princeton.edu/~rblee/ELE572Papers/Fall04Readings/Shifter_Schulte.pdf)</sup><sup> • </sup><sup>[8](https://electronics.stackexchange.com/questions/244129/barrel-shifter-using-multiplexer-how-to-go-about-it)</sup><sup> • </sup><sup>[16](https://patents.google.com/patent/US6877019B2/en)</sup>

多路复用器的数量可直接推出。每个多路复用器产生一个输出位，因此一个三级 8 位移位器使用 8 × 3 = 24 个二输入多路复用器。一般而言，处理 N 个符号的对数结构桶形移位器需要 ⌈log₂(N)⌉ 行多路复用器，即 N·nbit_data·⌈log₂(N)⌉ 个多路复用器，这在给定移位能力下使多路复用器数量最少。<sup>[8](https://electronics.stackexchange.com/questions/244129/barrel-shifter-using-multiplexer-how-to-go-about-it)</sup><sup> • </sup><sup>[4](https://patents.justia.com/patent/20100293212)</sup>

**末端填充与操作选择。** 六种标准操作的差别仅在于空出的位位置填充什么：逻辑移位填零，算术右移复制符号位，循环移位填入绕回的位。一个右移/循环移位器只需增加一个在 0 和符号位之间选择的首级多路复用器，以及各级中在移位数据与绕回低位之间选择的多路复用器，即可提供全部六种操作。<sup>[2](https://www.princeton.edu/~rblee/ELE572Papers/Fall04Readings/Shifter_Schulte.pdf)</sup>

**用右移器实现左移。** 硬件通常只实现右移，通过数据反转来获得左移操作：一行 n 个多路复用器将输入字反转，右移器对反转后的数据进行操作，第二行再将输出反转回来。当不需要左移时，反转行让数据原样通过。一个等效的技巧利用了以下事实：左循环移位 B 位等于右循环移位 n − B 位，而对于 2 的幂的 n，n − B 就是 B 的二进制补码；这可以完全省去反转多路复用器。<sup>[2](https://www.princeton.edu/~rblee/ELE572Papers/Fall04Readings/Shifter_Schulte.pdf)</sup> 同样的反转技术出现在 RISC-V 算术逻辑单元设计中，单个右桶形移位器通过三个步骤处理 n 位左移：反转操作数、右移 n 位、再将结果反转回来。<sup>[9](https://github.com/uninghetvatly/alu-32bit-riscv)</sup>

## 在处理器中的使用

**ARM。** 桶形移位器是 ARM 架构的标志性特征。每条数据处理指令的第二个操作数在进入算术逻辑单元之前都经过桶形移位器，因此移位与算术或逻辑运算在一条指令内完成。ARM 桶形移位器提供五种移位和循环移位类型：LSL（逻辑左移）、LSR（逻辑右移）、ASR（算术右移）、ROR（循环右移）和 RRX（扩展循环右移，即通过进位位的 33 位循环移位）。<sup>[6](https://seojuncha.github.io/arm/assembly/tutorial/2025/09/25/arm-sfhiter-operand.html)</sup><sup> • </sup><sup>[10](https://www.davespace.co.uk/arm/introduction-to-arm/barrel-shifter.html)</sup> 这一位置安排还服务于常量编码：ARM 立即数由一个 8 位值循环移位形成，使得 `MOV r0, r0, LSL #1`（乘以二）或 `ADD r4, r4, r4, LSL #4`（乘以十七）等指令无需额外指令即可运行。<sup>[10](https://www.davespace.co.uk/arm/introduction-to-arm/barrel-shifter.html)</sup><sup> • </sup><sup>[6](https://seojuncha.github.io/arm/assembly/tutorial/2025/09/25/arm-sfhiter-operand.html)</sup>

**RISC-V。** RISC-V 整数指令集定义了三条移位指令：sll、srl 和 sra，提供寄存器-寄存器和立即数两种形式（slli、srli、srai）。对于 32 位字，移位量取自第二个操作数的 5 个最低有效位；对于 64 位字，取自最低 6 位，因为按整个字宽移位等同于不移位。<sup>[11](https://www.cliffsnotes.com/study-notes/33400187)</sup> 开源核心采用参数化桶形移位器拓扑在一个时钟周期内完成移位，VexiiRiscv 项目则用单个左桶形移位器实现全部 RISC-V 整数移位指令，右移时在前后进行位反转。<sup>[12](https://github.com/eymay/ALU)</sup><sup> • </sup><sup>[13](https://spinalhdl.github.io/VexiiRiscv/doc/vexiiriscv/execute/BarrelShifterPlugin.html)</sup>

**浮点单元。** 桶形移位器被认为是浮点加法器中不可或缺的电路：它在加法前对齐较小操作数的尾数，在加法后对求和后的尾数进行规格化。传统设计在这两个步骤中使用两个独立的桶形移位器；双向桶形移位器可以完成这两个步骤，已报道的 FPGA 结果表明面积和延迟均有降低，同时最高时钟频率更高、功耗更低。<sup>[14](https://www.sciencedirect.com/science/article/abs/pii/S0141933123000881)</sup>

## 设计权衡

桶形移位器以硬件换取速度。其传播延迟理论上恒定，至多经过一个传输门，与移位器规模和移位位置数无关，尽管在实际布局中缓冲器输入端的电容随最大移位宽度增大。其面积主要由布线而非逻辑决定。<sup>[5](https://people.ee.duke.edu/~jmorizio/ece261/classlectures/arithcircuits2.pdf)</sup> 无法承受这一成本的设计者会选择串行移位器，它成本更低但速度更慢，一些处理器项目将这一选择作为构建时选项提供。<sup>[7](https://github.com/bluespec/Flute)</sup>

利哈伊大学计算机体系结构与算术实验室的 Matthew R. Pillmeier、Michael J. Schulte 和 E. George Walters III 进行了一项比较研究，考察了四种桶形移位器设计在多种操作数规模下的表现，发现数据反转桶形移位器面积更小，而基于掩码的数据反转桶形移位器延迟低于其他设计。基于掩码的方法还能在移位的同时并行计算溢出标志和零标志，这在需要这些标志时很有吸引力。<sup>[2](https://www.princeton.edu/~rblee/ELE572Papers/Fall04Readings/Shifter_Schulte.pdf)</sup><sup> • </sup><sup>[15](https://pure.psu.edu/en/publications/design-alternatives-for-barrel-shifters/)</sup>

对数结构有其自身的布局代价：多路复用器之间的路径长度差异很大，且布局需要大量导线交叉点，这使物理设计变得复杂。<sup>[4](https://patents.justia.com/patent/20100293212)</sup>

## References

1. [A Barrel Shifter, Gerald Shute, University of Minnesota Duluth](https://www.d.umn.edu/~gshute/logic/barrel-shifter.html)
2. [Pillmeier, M. R., Schulte, M. J., Walters, E. G. (2002). Design alternatives for barrel shifters. Proceedings of SPIE 4791, 436–447](https://www.princeton.edu/~rblee/ELE572Papers/Fall04Readings/Shifter_Schulte.pdf)
3. [B.Tech ECE course material, IARE (barrel shifter as combinational logic)](https://iare.ac.in/sites/default/files/NewRegulationsSyllabi/UG20/B.TECH_ECE_UG20_ACADEMIC_REGULATIONS_AND_COURSE_CATALOG_2020-2021.pdf)
4. [U.S. Patent Application 20100293212, Barrel Shifter, Justia Patents](https://patents.justia.com/patent/20100293212)
5. [Arithmetic Circuits-2, ECE 261 lecture notes, Duke University (James Morizio)](https://people.ee.duke.edu/~jmorizio/ece261/classlectures/arithcircuits2.pdf)
6. [A Complete Guide to Shifter Operands (ARM32), Seojun's Personal Site](https://seojuncha.github.io/arm/assembly/tutorial/2025/09/25/arm-sfhiter-operand.html)
7. [Flute: RISC-V CPU, Bluespec, GitHub](https://github.com/bluespec/Flute)
8. [Barrel shifter using multiplexer: how to go about it, Electrical Engineering Stack Exchange](https://electronics.stackexchange.com/questions/244129/barrel-shifter-using-multiplexer-how-to-go-about-it)
9. [alu-32bit-riscv: 32-bit ALU for RISC-V, GitHub](https://github.com/uninghetvatly/alu-32bit-riscv)
10. [ARM: Introduction to ARM: Barrel Shifter, DaveSpace](https://www.davespace.co.uk/arm/introduction-to-arm/barrel-shifter.html)
11. [RISC-V Shift Instructions: Understanding Barrel Shifters, ENSC 350 lecture notes, Simon Fraser University](https://www.cliffsnotes.com/study-notes/33400187)
12. [ALU: A fast ALU design for RISC-V hardware, GitHub](https://github.com/eymay/ALU)
13. [BarrelShifterPlugin, VexiiRiscv documentation](https://spinalhdl.github.io/VexiiRiscv/doc/vexiiriscv/execute/BarrelShifterPlugin.html)
14. [Area-latency efficient floating point adder using interleaved alignment and normalization, Microprocessors and Microsystems 99 (2023)](https://www.sciencedirect.com/science/article/abs/pii/S0141933123000881)
15. [Design alternatives for barrel shifters, Penn State research portal](https://pure.psu.edu/en/publications/design-alternatives-for-barrel-shifters/)
16. [US6877019B2 - Barrel shifter - Google Patents](https://patents.google.com/patent/US6877019B2/en)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 计算机硬件 › Processors & processor engineering › Computer architecture theory › CPU internal structure

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/barrel-shifter

English original: https://www.edgepedia.org/barrel-shifter
