c - 为什么要分两步进行位移?

标签 c linux bit-manipulation unspecified-behavior

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_tunsigned long都是 64 位宽。在这种情况下,high变量被忽略,只有 low用来。在 ia32,loff_t是 64 位宽和 unsigned long是 32 位宽。在这种情况下,两个参数 highlow结合在一起。
我想知道为什么代码移位两次而不是一次。在 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_tlong是 64 位宽。如果我们立即进行转移,我们将转移 high按 64 位,根据上述声明是未定义的行为。分两步做 high成 0。

PS:我编写了一个测试程序来调查这个问题,令我惊讶的是,当我用一个位移位替换两个位移位时,得到了不同的结果。

关于c - 为什么要分两步进行位移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68684149/

相关文章:

c - 链表 createList 与 initNode 函数

c - 当我输入数组中元素的值时,在 for 循环中,在 for 循环之后然后进行转换

linux - grep 具有可变数量的空格

javascript - 与左移运算符混淆

c - memcpy 似乎被卡住了

不带参数在函数内部调用函数

linux - 你将如何制作一个 shell 脚本来监控挂载和日志问题?

java - -bash : java: command not found in Centos5

optimization - 快速饱和并在ARM asm中移动两个半字

go - 在 Go 中将 uint16 转换为 int16 的正确方法