说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C9-ARM64/arithmetic.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
9.3. Arithmetic Instructions
9.3.1. 常用算术指令
9.3.1. Common Arithmetic Instructions
A64 ISA 实现了若干条与 ALU 执行的算术运算相对应的指令。表 1 列出了阅读 ARM 汇编时可能遇到的几条算术指令。
The A64 ISA implements several instructions that correspond to arithmetic operations performed by the ALU. Table 1 lists several arithmetic instructions that one may encounter when reading ARM assembly.
表 1. 常用指令
Table 1. Common Instructions
| 指令 | 含义 |
|---|---|
add D, O1, O2 |
D = O1 + O2 |
sub D, O1, O2 |
D = O1 - O2 |
neg D, O1 |
D = -(O1) |
Instruction Translation add D, O1, O2D = O1 + O2 sub D, O1, O2D = O1 - O2 neg D, O1D = -(O1)
add 和 sub 指令对应于加法和减法,除了目标寄存器之外还需要两个操作数。与之不同,neg 指令除了目标寄存器之外只需要一个操作数。
The
addandsubinstructions correspond to addition and subtraction and require two operands in addition to the destination register. In contrast, theneginstruction requires only one operand in addition to the destination register.
表 1 中的三条指令还有带进位的形式,能让指令使用可选的进位条件标志 C。当无符号运算溢出时,这个一位的进位标志被置位。我们会在下一节介绍其他条件控制标志,这里先介绍进位标志,以便引出另外几条算术指令。带进位的形式及其大致含义见表 2。
The three instructions in Table 1 also have carry forms that enable the instruction to use the optional carry condition flag,
C. The one-bit carry flag is set when an unsigned operation overflows. We cover other condition control flags in the following section, but describe the carry flag here to introduce the additional arithmetic instructions. The carry forms and their rough translation are shown in Table 2.
表 2. 常用指令的带进位形式
Table 2. Carry Forms for Common Instructions
| 指令 | 含义 |
|---|---|
adc D, O1, O2 |
D = O1 + O2 + C |
sbc D, O1, O2 |
D = O1 - O2 - ~C |
ngc D, O1 |
D = -(O1) - ~C |
Instruction Translation adc D, O1, O2D = O1 + O2 + Csbc D, O1, O2D = O1 - O2 - ~Cngc D, O1D = -(O1) - ~C
上面这些指令还有一个可选的 s 后缀。使用 s 后缀时(例如 adds),表示这条算术运算会设置条件标志。
The above instructions also have an optional
ssuffix. When thessuffix is used (e.g.,adds), it indicates that the arithmetic operation is setting condition flags.
乘法与除法
Multiplication and Division
表 3. 常用乘法与除法指令
Table 3. Common Multiplication and Division Instructions
| 指令 | 含义 |
|---|---|
mul D, O1, O2 |
D = O1 × O2 |
udiv D, O1, O2 |
D = O1 / O2(32 位无符号) |
sdiv D, O1, O2 |
D = O1 / O2(64 位有符号) |
Instruction Translation mul D, O1, O2D = O1 × O2 udiv D, O1, O2D = O1 / O2 (32-bit unsigned) sdiv D, O1, O2D = O1 / O2 (64-bit signed)
译注 · udiv 与 sdiv 的区别
表中括号和下一段的说法有误:udiv 与 sdiv 的区别在于无符号与有符号,而不是 32 位与 64 位——两条指令都既可以用 w 寄存器做 32 位除法,也可以用 x 寄存器做 64 位除法。译者用 Clang 汇编 udiv x0, x1, x2、udiv w0, w1, w2、sdiv x0, x1, x2、sdiv w0, w1, w2,四条都是合法指令。
最常用的乘法和除法指令见表 3。mul 指令对两个操作数进行运算,并把乘积放到目标 D 中。除法运算没有通用形式;udiv 和 sdiv 指令分别对 32 位和 64 位数据进行运算。注意,不能把 32 位寄存器与 64 位寄存器相乘。
The most common multiplication and division instructions are shown in Table 3. The
mulinstruction operates on two operands and places the product in the destination D. The division operation does not have a generic form; theudivandsdivinstructions operate on 32-bit and 64-bit data, respectively. Note that you cannot multiply 32-bit registers with 64-bit registers.
此外,ARMv8-A 还为乘法提供了复合形式,让 CPU 能在一条指令中完成更复杂的运算。这些指令见表 4。
In addition, ARMv8-A provides composite forms for multiplication, allowing the CPU to perform more sophisticated operations in a single instruction. These instructions are shown in Table 4.
表 4. 复合乘法指令
Table 4. Composite Multiplication Instructions
| 指令 | 含义 |
|---|---|
madd D, O1, O2, O3 |
D = O3 + (O1 × O2) |
msub D, O1, O2, O3 |
D = O3 - (O1 × O2) |
mneg D, O1, O2 |
D = -(O1 × O2) |
Instruction Translation madd D, O1, O2, O3D = O3 + (O1 × O2) msub D, O1, O2, O3D = O3 - (O1 × O2) mneg D, O1, O2D = -(O1 × O2)
9.3.2. 移位指令
9.3.2. Bit Shifting Instructions
移位指令让编译器能执行移位运算。乘法和除法指令通常需要很长时间才能执行完。对于是 2 的幂的乘数和除数,移位为编译器提供了一条捷径。例如,要计算 77 * 4,大多数编译器会把这个运算翻译成 77 << 2,以避免使用 mul 指令。同样,要计算 77 / 4,编译器通常会把这个运算翻译成 77 >> 2,以避免使用 sdiv 指令。
Bit shifting instructions enable the compiler to perform bit shifting operations. Multiplication and division instructions typically take a long time to execute. Bit shifting offers the compiler a shortcut for multiplicands and divisors that are powers of 2. For example, to compute
77 * 4, most compilers will translate this operation to77 << 2to avoid the use of amulinstruction. Likewise, to compute77 / 4, a compiler typically translates this operation to77 >> 2to avoid using thesdivinstruction.
译注 · 有符号除法不只是右移
对有符号整数,x / 4 不能简单地变成 x >> 2:C 的除法向零取整,而算术右移向负无穷取整(例如 -1 >> 2 得 -1,而 -1 / 4 得 0)。译者用 clang -O2 -target aarch64-linux-gnu 编译 int q(int x){return x/4;},得到的是 add w8, w0, #3 / cmp w0, #0 / csel w8, w8, w0, lt / asr w0, w8, #2,即负数先加 3 再右移;而 unsigned 版本只有一条 lsr w0, w0, #2。
记住,左移和右移会根据目标是算术(有符号)移位还是逻辑(无符号)移位,被翻译成不同的指令。
Keep in mind that left and right bit shifts translate to different instructions based on whether the goal is an arithmetic (signed) or logical (unsigned) shift.
表 5. 移位指令
Table 5. Bit Shift Instructions
| 指令 | 含义 | 算术还是逻辑? |
|---|---|---|
lsl D, R, #v |
D = R << v |
逻辑或算术 |
lsr D, R, #v |
D = R >> v |
逻辑 |
asr D, R, #v |
D = R >> v |
算术 |
ror D, R, #v |
D = R >>> v |
都不是(循环移位) |
Instruction Translation Arithmetic or Logical? lsl D, R, #vD = R <<vlogical or arithmetic lsr D, R, #vD = R >>vlogical asr D, R, #vD = R >>varithmetic ror D, R, #vD = R >>>vneither (rotate)
除了目标寄存器之外,每条移位指令接收两个操作数;一个通常是寄存器(用 R 表示),另一个是 6 位的移位量(v)。在 64 位系统上,移位量编码为单个字节(因为移位超过 63 位没有意义)。移位量 v 必须是一个常数,或者存放在一个组成部分寄存器中。
In addition to the destination register, each shift instruction takes two operands; one is usually a register (denoted by R) and the other is a 6-bit shift value (v). On 64-bit systems, the shift value is encoded as a single byte (since it doesn’t make sense to shift past 63). The shift value v must either be a constant or stored in a component register.
最后一条移位指令 ror 需要特别讨论。ror 指令对各位进行循环移位(rotate),用最低有效位替换最高有效位。我们用 >>> 符号表示循环移位指令。
The last bit shifting instruction,
ror, requires special discussion. Therorinstruction rotates the bits, replacing the most significant bits with the least significant bits. We represent the rotate shift instruction using the>>>symbol.
原书注 · 不同版本的指令帮助我们在汇编层面区分类型(Different versions of instructions help us distinguish types at an assembly level)
在汇编层面,没有类型的概念。不过,回想一下,编译器可以根据代码层面的类型选用组成部分寄存器。同样,回想一下,右移的行为取决于值是有符号还是无符号的。在汇编层面,编译器用不同的指令来区分逻辑移位和算术移位!
At the assembly level, there is no notion of types. However, recall that the compiler can choose to use component registers based on the types present at the code level. Similarly, recall that shift right works differently depending on whether the value is signed or unsigned. At the assembly level, the compiler uses separate instructions to distinguish between logical and arithmetic shifts!
9.3.3. 位运算指令
9.3.3. Bitwise Instructions
位运算指令让编译器能对数据执行位运算。编译器使用位运算的一种方式是进行某些优化。例如,编译器可能会选择用运算 77 & 3 来实现 77 mod 4,而不用代价更高的 sdiv 指令。
Bitwise instructions enable the compiler to perform bitwise operations on data. One way in which the compiler uses bitwise operations is for certain optimizations. For example, a compiler may choose to implement 77 mod 4 with the operation
77 & 3in lieu of the more expensivesdivinstruction.
表 6 列出了常用的位运算指令,以及利用取反的复合位运算指令。
Table 6 lists common bitwise instructions, and composite bitwise instructions that utilize negation.
表 6. 位运算
Table 6. Bitwise Operations
| 指令 | 含义 |
|---|---|
and D, O1, O2 |
D = O1 & O2 |
orr D, O1, O2 |
D = O1 ` |
eor D, O1, O2 |
D = O1 ^ O2 |
mvn D, O |
D = ~O |
bic D, O1, O2 |
D = O1 & ~O2 |
orn D, O1, O2 |
D = O1 ` |
eon D, O1, O2 |
D = O1 ^ ~O2 |
Instruction Translation and D, O1, O2D = O1 &O2orr D, O1, O2D = O1 ` eor D, O1, O2D = O1 ^O2mvn D, OD = ~Obic D, O1, O2D = O1 &~O2orn D, O1, O2D = O1 ` eon D, O1, O2D = O1 ^~O2
记住,按位取反与取负(neg)不同。mvn 指令只翻转操作数的各位,不加 1。小心不要混淆这两条指令。
Remember that bitwise not is distinct from negation (
neg). Themvninstruction flips the bits of the operand but does not add 1. Be careful not to confuse these two instructions.
原书警示 · 只在 C 代码确实需要时才使用位运算!(Use bitwise operations only when needed in your C code!)
读完本节,你可能很想把 C 代码中常见的算术运算替换成移位和其他位运算。我们不建议这样做。大多数现代编译器都足够聪明,会在合适的时候用位运算替换简单的算术运算,程序员不必亲自去做。一般来说,程序员应当尽可能把代码的可读性放在首位,避免过早优化。
After reading this section, it may be tempting to replace common arithmetic operations in your C code with bitwise shifts and other operations. This is not recommended. Most modern compilers are smart enough to replace simple arithmetic operations with bitwise operations when it makes sense, making it unnecessary for the programmer to do so. As a general rule, programmers should prioritize code readability whenever possible and avoid premature optimization.