如果只需要结果的低位部分,可以使用哪种 2 的补码整数运算而无需将输入中的高位清零?

2024-01-09

在汇编编程中,想要从寄存器的低位计算某些内容是相当常见的,但不能保证其他位清零。在 C 等高级语言中,您只需将输入转换为小尺寸,然后让编译器决定是否需要分别将每个输入的高位清零,或者是否可以在事实。

This is especially common for x86-64 (aka AMD64), for various reasons1, some of which are present in other ISAs.

I'll use 64bit x86 for examples, but the intent is to ask about/discuss 2's complement https://en.wikipedia.org/wiki/Two%27s_complement and unsigned binary arithmetic in general, since all modern CPUs use it https://stackoverflow.com/questions/2931630/how-are-negative-numbers-represented-in-32-bit-signed-integer. (Note that C and C++ don't guarantee two's complement4, and that signed overflow is undefined behaviour.)

As an example, consider a simple function that can compile to an LEA instruction2. (In the x86-64 SysV(Linux) ABI http://www.x86-64.org/documentation.html3, the first two function args are in rdi and rsi, with the return in rax. int is a 32bit type.)

; int intfunc(int a, int b) { return a + b*4 + 3; }
intfunc:
    lea  eax,  [edi + esi*4 + 3]  ; the obvious choice, but gcc can do better
    ret

gcc 知道加法,即使是负符号整数,也只能从右向左进位,因此输入的高位不会影响进入的内容eax。因此,它保存一个指令字节并使用 http://goo.gl/h3UKGC lea eax, [rdi + rsi*4 + 3]

还有哪些其他操作具有结果低位的这种属性,而不依赖于输入的高位?

为什么它有效?



脚注

1 Why this comes up frequently for x86-64 https://stackoverflow.com/tags/x86/info: x86-64 has variable-length instructions, where an extra prefix byte changes the operand size (from 32 to 64 or 16), so saving a byte is often possible in instructions that are otherwise executed at the same speed. It also has false-dependencies (AMD/P4/Silvermont) when writing the low 8b or 16b of a register (or a stall when later reading the full register (Intel pre-IvB)): For historical reasons, only writes to 32b sub-registers zero the rest of the 64b register https://stackoverflow.com/questions/11177137/why-do-most-x64-instructions-zero-the-upper-part-of-a-32-bit-register. Almost all arithmetic and logic can be used on on the low 8, 16, or 32bits, as well as the full 64bits, of general-purpose registers. Integer vector instructions are also rather non-orthogonal, with some operations not available for some element sizes.

此外,与 x86-32 不同,ABI 在寄存器中传递函数参数,并且对于窄类型,高位不需要为零。

2 LEA: Like other instructions, the default operand size of LEA https://stackoverflow.com/a/7071164/224132 is 32bit, but the default address size is 64bit. An operand-size prefix byte (0x66 or REX.W) can make the output operand size 16 or 64bit. An address-size prefix byte (0x67) can reduce the address size to 32bit (in 64bit mode) or 16bit (in 32bit mode). So in 64bit mode, lea eax, [edx+esi] takes one byte more than lea eax, [rdx+rsi].

这是可以做的lea rax, [edx+esi],但地址仍然只用 32 位计算(进位不会设置的位 32)rax)。你会得到相同的结果lea eax, [rdx+rsi],短了两个字节。因此,地址大小前缀对于以下情况没有用处:LEA,正如 Agner Fog 优秀的 objconv 反汇编器的反汇编输出中的注释所警告的那样。

3 x86 ABI: The caller doesn't have to zero (or sign-extend) the upper part of 64bit registers used to pass or return smaller types by value. A caller that wanted to use the return value as an array index would have to sign-extend it (with movzx rax, eax, or the special-case-for-eax instruction cdqe. (not to be confused with cdq, which sign-extends eax into edx:eax e.g. to set up for idiv.))

这意味着函数返回unsigned int可以在 64 位临时中计算其返回值rax,并且不需要mov eax, eax 将高位清零 https://stackoverflow.com/questions/11177137/why-do-most-x64-instructions-zero-the-upper-part-of-a-32-bit-register of rax。这种设计决策在大多数情况下效果很好:调用者通常不需要任何额外的指令来忽略上半部分中未定义的位。rax.


4 C and C++

C 和 C++ 专门做not需要二进制补码二进制有符号整数(除了C++ std::atomic types http://en.cppreference.com/w/cpp/atomic/atomic/operator_arith2). 还允许使用补码和符号/量值 https://stackoverflow.com/a/2931680/224132, 因此对于fully可移植 C,这些技巧仅适用于unsigned类型。显然,对于有符号运算,符号/数值表示中设置的符号位意味着其他位被减去,而不是相加。我还没有研究过补语的逻辑

However, bit-hacks http://www.hackersdelight.org/ that only work with two's complement https://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs are widespread http://aggregate.org/MAGIC/#Average%20of%20Integers, because in practice nobody cares about anything else. Many things that work with two's complement should also work with one's complement, since the sign bit still doesn't change the interpretation of the other bits: it just has a value of -(2N-1) (instead of 2N). Sign/magnitude representation does not have this property: the place value of every bit is positive or negative depending on the sign bit.

另请注意,C 编译器可以假设有符号溢出永远不会发生,因为这是未定义的行为。所以例如编译器可以并且确实假设(x+1) < x总是假的 http://goo.gl/h3UKGC。这使得在 C 中检测有符号溢出相当不方便。请注意无符号环绕(进位)和有符号溢出之间的区别 http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt.


可以与高位中的垃圾一起使用的宽操作:

  • 按位逻辑
  • 左移(包括*scale in [reg1 + reg2*scale + disp])
  • 加法/减法(因此LEA说明:永远不需要地址大小前缀。如果需要,只需使用所需的操作数大小来截断。)
  • 乘法的低半部分。例如16b x 16b -> 16b 可以用 32b x 32b -> 32b 来完成。你可以避免 LCP 停顿(和部分寄存器问题)imul r16, r/m16, imm16 https://stackoverflow.com/questions/34111959/packing-bcd-to-dpd-how-to-improve-this-amd64-assembly-routine通过使用 32 位imul r32, r/m32, imm32然后只读取结果的低 16 位。 (如果使用更宽的内存引用,请小心m32不过版本。)

    正如Intel的insn ref手册所指出的,2和3操作数形式imul可安全地用于无符号整数。输入的符号位不影响结果的 N 位N x N -> N位乘法。)

  • 2x (i.e. shift by x): Works at least on x86, where the shift count is masked, rather than saturated, down to the width of the operation, so high garbage in ecx, or even the high bits of cl, don't affect the shift count. Also applies to BMI2 flagless shifts (shlx etc), but not to vector shifts (pslld xmm, xmm/m128 etc, which saturate the count). Smart compilers optimize away masking of the shift count, allowing for a safe idiom for rotates in C (no undefined behaviour) https://stackoverflow.com/questions/776508/best-practices-for-circular-shift-rotate-operations-in-c.

显然,像进位/溢出/符号/零这样的标志都会受到更广泛操作的高位垃圾的影响。 x86 的移位将最后一位移出到进位标志中,因此这甚至会影响移位。

操作can't与高位中的垃圾一起使用:

  • 右移
  • 完全乘法:例如对于 16b x 16b -> 32b,请确保在执行 32b x 32b -> 32b 之前对输入的高 16 位进行零或符号扩展imul。或者使用16位单操作数mul or imul不方便地将结果放入dx:ax。 (有符号指令与无符号指令的选择将影响高 16b,就像 32b 之前的零或符号扩展一样imul.)

  • 内存寻址([rsi + rax]):根据需要进行符号或零扩展。没有[rsi + eax]寻址模式。

  • 除法和余数

  • log2 https://stackoverflow.com/questions/3272424/compute-fast-log-base-2-ceiling(即最高设置位的位置)
  • 尾随零计数(除非您知道您想要的部分中有一个设置位,或者只是在未找到检查时检查大于 N 的结果。)

Two's complement, like unsigned base 2, is a place-value system. The MSB for unsigned base2 has a place value of 2N-1 in an N bit number (e.g. 231). In 2's complement, the MSB has a value of -2N-1 (and thus works as a sign bit). The wikipedia article https://en.wikipedia.org/wiki/Two%27s_complement explains many other ways of understanding 2's complement and negating an unsigned base2 number.

关键是设置符号位不改变其他位的解释。加法和减法的工作方式与无符号 base2 完全相同,只是有符号和无符号结果的解释不同。 (例如。当符号位有进位但没有进位时,就会发生有符号溢出 http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt.)

此外,进位仅从 LSB 传播到 MSB(从右到左)。减法也是一样:不管高位有没有借,低位都借。如果这导致溢出或进位,则只有高位会受到影响。例如。:

 0x801F
-0x9123
-------
 0xeefc

低8位,0xFC,不要依赖于他们借来的东西。它们“环绕”并将借位传递给高 8 位。

因此,加法和减法具有结果的低位不依赖于操作数的任何高位的特性。

Since LEA仅使用加法(和左移),使用默认地址大小总是可以的。延迟截断直到操作数大小对结果起作用总是可以的。

(例外:16 位代码可以使用地址大小前缀进行 32 位数学运算。在 32 位或 64 位代码中,地址大小前缀会减小宽度而不是增加宽度。)


乘法可以被认为是重复加法,或者移位加法。低半部分不受任何高位的影响。在这个 4 位示例中,我写出了所有加到低 2 位结果位中的位积。仅涉及任一源的低 2 位。很明显,这通常是有效的:部分乘积在加法之前会被移位,因此源中的高位通常不会影响结果中的低位。

See 维基百科有一个更大的版本,有更详细的解释 https://en.wikipedia.org/wiki/Binary_multiplier#More_advanced_approach:_signed_integers。有很多好的谷歌点击二进制有符号乘法 https://www.google.ca/?q=binary%20signed%20multiplication,包括一些教材。

    *Warning*: This diagram is probably slightly bogus.


       ABCD   A has a place value of -2^3 = -8
     * abcd   a has a place value of -2^3 = -8
     ------
   RRRRrrrr

   AAAAABCD * d  sign-extended partial products
 + AAAABCD  * c
 + AAABCD   * b
 - AABCD    * a  (a * A = +2^6, since the negatives cancel)
  ----------
          D*d
         ^
         C*d+D*c

进行有符号乘法而不是无符号乘法仍然会在下半部分给出相同的结果(本例中为低 4 位)。部分积的符号扩展仅发生在结果的上半部分。

这个解释不是很彻底(甚至可能有错误),但是有充分的证据表明它是真实的并且可以在生产代码中安全使用:

  • 海湾合作委员会使用imul来计算unsigned long两个的乘积unsigned long输入。请参阅 Godbolt 编译器资源管理器上的 gcc 利用 LEA 实现其他功能的示例 http://gcc.godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(j:1,options:(compileOnChange:'0'),source:'%23include+%3Cstdint.h%3E%0A%0Aint+intfunc(int+a,+int+b)+%7B+return+a+%2B+b*4+%2B+3%3B+%7D%0A%0Along+longfunc(int+a,+int+b)++++++++++%7B+return+a+%2B+b*4+%2B+3%3B+%7D%0Along+longfunc_longa(long+a,+int+b)+++%7B+return+a+%2B+b*4+%2B+3%3B+%7D%0Along+longfunc_longb(int+a,+long+b)+++%7B+return+a+%2B+b*4+%2B+3%3B+%7D%0Along+longfunc_longboth(long+a,+long+b)%7Breturn+a+%2B+b*4+%2B+3%3B+%7D%0A%0Aunsigned+long+longmul(unsigned+int+a,+unsigned+int+b)+%7B+return+(unsigned+long)a+*+(unsigned+long)b%3B+%7D%0A%0A//+signed+overflow+is+undefined+behaviour:+compilers+optimize+by+pretending+it+doesn!'t+happen%0Aint+overflow_check(int+x)+%7B+return+(x%2B1)+%3C+x%3B+%7D%0Aint+carry_check(unsigned+x)+%7B+return+(x%2B1)+%3C+x%3B+%7D'),l:'5',n:'1',o:'C%2B%2B+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:g62,filters:(b:'0',commentOnly:'0',directives:'0',intel:'0'),options:'-xc+-std%3Dgnu11+-Wall+-O3+-march%3Dhaswell'),l:'5',n:'0',o:'%231+with+x86-64+gcc+6.2',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4.

  • 英特尔的 insn 参考手册说:

二操作数和三操作数形式也可以与无符号一起使用 操作数,因为无论如何,乘积的下半部分都是相同的 操作数是否有符号。然而,CF 和 OF 标志 不能用来确定结果的上半部分是否是 非零。

  • Intel 的设计决定只引入 2 和 3 操作数形式imul, not mul.

显然,按位二进制逻辑运算(和/或/异或/非)独立处理每个位:位位置的结果仅取决于该位位置的输入值。位移也相当明显。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果只需要结果的低位部分,可以使用哪种 2 的补码整数运算而无需将输入中的高位清零? 的相关文章

随机推荐