在 Linux kernel ,我找到了以下代码:
static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
{
#define HALF_LONG_BITS (BITS_PER_LONG / 2)
return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
}
该代码用于将系统调用参数组合成一个更宽的变量,例如在 ia32 上 pwritev
的偏移量。在两个 32 位寄存器中指定。在 x64 上,
loff_t
和 unsigned long
都是 64 位宽。在这种情况下,high
变量被忽略,只有 low
用来。在 ia32,loff_t
是 64 位宽和 unsigned long
是 32 位宽。在这种情况下,两个参数 high
和 low
结合在一起。我想知道为什么代码移位两次而不是一次。在 commit message 中有更多关于此代码的信息。并在 LWN 文章中:System calls and 64-bit architectures ,但没有解释双位移位。
最佳答案
测试应用程序中的以下警告帮助我解决了这个问题:
test.c:8:27: warning: left shift count >= width of type [-Wshift-count-overflow]
8 | return (((loff_t)high << (2*HALF_LONG_BITS))) | low;
双位移位可防止未定义的行为。来自 C spec :6.5.7 3) ... If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
在 64 位机器上,两个
loff_t
和 long
是 64 位宽。如果我们立即进行转移,我们将转移 high
按 64 位,根据上述声明是未定义的行为。分两步做 high
成 0。PS:我编写了一个测试程序来调查这个问题,令我惊讶的是,当我用一个位移位替换两个位移位时,得到了不同的结果。
关于c - 为什么要分两步进行位移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68684149/