如果计数大于类型的宽度,右移是否是未定义的行为?

我刚刚检查了 C++ 标准.似乎以下代码不应该是未定义行为:

unsigned int val = 0x0FFFFFFF;无符号整数 res = val >>34;//res 应该是 0 C++ 标准,//但是 GCC 给出警告并且 res 是 67108863

来自标准:

<块引用>

E1 >> E2 的值是 E1 右移的 E2 位位置.如果E1有一个无符号类型或者如果 E1 有一个有符号类型和一个非负值,结果的值是商的整数部分E1/2^E2. 如果 E1 有符号类型和负值,则结果价值是实现定义的.

根据标准,由于 34 不是负数,变量 res 将为 0.

GCC 对代码片段给出以下警告,res67108863:

<块引用>

警告:右移计数>=类型宽度

我还检查了 GCC 发出的汇编代码.它只是调用SHRL,而对于SHRL 的Intel 指令文档,res 不是零.

这是否意味着 GCC 没有在 Intel 平台上实现标准行为?

解决方案

5.8 部分的 C++ 标准草案 Shift 操作符 在段落 1 中说(强调我的):

<块引用>

结果的类型是提升后的左操作数的类型.如果右操作数为负数,或者大于或等于提升的左操作数的位长度,则行为未定义.

所以如果 unsigned int 是 32 bits 或更少,那么这是未定义的,这正是 gcc 给你的警告.

I just checked the C++ standard. It seems the following code should NOT be undefined behavior:

unsigned int val = 0x0FFFFFFF;
unsigned int res = val >> 34;  // res should be 0 by C++ standard,
                               // but GCC gives warning and res is 67108863

And from the standard:

The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a non-negative value, the value of the result is the integral part of the quotient of E1/2^E2. If E1 has a signed type and a negative value, the resulting value is implementation-de?ned.

According to the standard, since 34 is NOT an negative number, the variable res will be 0.

GCC gives the following warning for the code snippet, and res is 67108863:

warning: right shift count >= width of type

I also checked the assembly code emitted by GCC. It just calls SHRL, and the Intel instruction document for SHRL, the res is not ZERO.

So does that mean GCC doesn't implement the standard behavior on Intel platform?

解决方案

The draft C++ standard in section 5.8 Shift operators in paragraph 1 says(emphasis mine):

The type of the result is that of the promoted left operand. The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand.

So if unsigned int is 32 bits or less then this is undefined which is exactly the warning that gcc is giving you.

相关文章