- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么 Clang 优化掉了这段代码中的循环
#include <time.h>
#include <stdio.h>
static size_t const N = 1 << 27;
static double arr[N] = { /* initialize to zero */ };
int main()
{
clock_t const start = clock();
for (int i = 0; i < N; ++i) { arr[i] *= 1.0; }
printf("%u ms\n", (unsigned)(clock() - start) * 1000 / CLOCKS_PER_SEC);
}
#include <time.h>
#include <stdio.h>
static size_t const N = 1 << 27;
static double arr[N] = { /* initialize to zero */ };
int main()
{
clock_t const start = clock();
for (int i = 0; i < N; ++i) { arr[i] += 0.0; }
printf("%u ms\n", (unsigned)(clock() - start) * 1000 / CLOCKS_PER_SEC);
}
最佳答案
IEEE 754-2008 浮点运算标准和 ISO/IEC 10967 Language Independent Arithmetic (LIA) Standard, Part 1回答为什么会这样。
IEEE 754 § 6.3 The sign bit
When either an input or result is NaN, this standard does not interpret the sign of a NaN. Note, however, that operations on bit strings — copy, negate, abs, copySign — specify the sign bit of a NaN result, sometimes based upon the sign bit of a NaN operand. The logical predicate totalOrder is also affected by the sign bit of a NaN operand. For all other operations, this standard does not specify the sign bit of a NaN result, even when there is only one input NaN, or when the NaN is produced from an invalid operation.
When neither the inputs nor result are NaN, the sign of a product or quotient is the exclusive OR of the operands’ signs; the sign of a sum, or of a difference x − y regarded as a sum x + (−y), differs from at most one of the addends’ signs; and the sign of the result of conversions, the quantize operation, the roundTo-Integral operations, and the roundToIntegralExact (see 5.3.1) is the sign of the first or only operand. These rules shall apply even when operands or results are zero or infinite.
When the sum of two operands with opposite signs (or the difference of two operands with like signs) is exactly zero, the sign of that sum (or difference) shall be +0 in all rounding-direction attributes except roundTowardNegative; under that attribute, the sign of an exact zero sum (or difference) shall be −0. However, x + x = x − (−x) retains the same sign as x even when x is zero.
x+0.0
生产
x
, 除非
x
是
-0.0
:在这种情况下,我们有两个符号相反的操作数的和,它们的和为零,第 6.3 节第 3 段规则此加法产生
+0.0
.
+0.0
与原始
-0.0
按位不相同,以及
-0.0
是可能作为输入出现的合法值,编译器必须放入将潜在负零转换为
+0.0
的代码。 .
x+0.0
, 如果
x
-0.0
,然后 x
本身就是一个可接受的输出值。 -0.0
,那么输出值一定是 +0.0
,这与 -0.0
按位不同. x*1.0
不会出现这样的问题.如
x
:
x*1.0 == x
总是。 +/- infinity
,那么结果是 +/- infinity
同一个标志。 NaN
,那么根据IEEE 754 § 6.2.3 NaN Propagation
An operation that propagates a NaN operand to its result and has a single NaN as an input should produce a NaN with the payload of the input NaN if representable in the destination format.
NaN*1.0
的指数和尾数(虽然不是符号)建议与输入 NaN
保持不变.根据上面的第 6.3p1 节未指定符号,但实现可能指定它与源 NaN
相同。 . +/- 0.0
,那么结果是 0
其符号位与 1.0
的符号位异或,与§6.3p2 一致。由于1.0
的符号位是 0
,输出值与输入值不变。因此,x*1.0 == x
即使 x
是(负)零。 x-0.0
也是一个空操作,因为它等价于
x + (-0.0)
.如
x
是
NaN
,那么 §6.3p1 和 §6.2.3 的适用方式与加法和乘法大致相同。 +/- infinity
,那么结果是 +/- infinity
同一个标志。 x-0.0 == x
总是。 -0.0
,然后根据 §6.3p2 我们有“[...] 和的符号,或被视为和 x + (-y) 的差 x - y 的符号,与最多一个加数的符号不同;” .这迫使我们分配 -0.0
作为 (-0.0) + (-0.0)
的结果, 因为 -0.0
与无加数的符号不同,而 +0.0
与 的符号不同两个的增补,违反了本条款。 +0.0
,那么这简化为加法情况 (+0.0) + (-0.0)
上面在加法的情况下考虑过,根据第 6.3p3 节的规定,它被裁定为 +0.0
. x-0.0
无操作,和
x == x-0.0
同义反复。
IEEE 754 § 10.4 Literal meaning and value-changing optimizations
[...]
The following value-changing transformations, among others, preserve the literal meaning of the source code:
- Applying the identity property 0 + x when x is not zero and is not a signaling NaN and the result has the same exponent as x.
- Applying the identity property 1 × x when x is not a signaling NaN and the result has the same exponent as x.
- Changing the payload or sign bit of a quiet NaN.
- [...]
x+0.0
的正确舍入结果和
x*1.0
对于有限
x
与
x
的震级完全相同,它们的指数相同。
sNaN
) 的行为,因此允许编译器假设它们不会发生,因此它们引发的异常也不会发生。 C++11 标准省略了对 NaN 信号行为的描述,因此也未定义。
x+0.0 -> x
变得允许,但
x-0.0 -> x
变得禁止。
-frounding-math
可以传递给GCC。
-O3
, 仍然符合 IEEE-754。这意味着它必须遵守 IEEE-754 标准的上述规则。
x+0.0
是
位不相同 至
x
所有
x
在这些规则下,但是
x*1.0
可以选择这样:即,当我们
x
的有效负载当它是 NaN 时。 * 1.0
. x
时,遵守在商/乘积期间对符号位进行异或的顺序不是 NaN。 (x+0.0) -> x
,国旗
-ffast-math
需要传递给 Clang 或 GCC。
关于c++ - 为什么 Clang 优化掉 x * 1.0 而不是 x + 0.0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33272994/
我正在编写一个快速的 preg_replace 来从 CSS 中删除注释。 CSS 注释通常有这样的语法: /* Development Classes*/ /* Un-comment me for
使用 MySQL,我有三个表: 项目: ID name 1 "birthday party" 2 "soccer match" 3 "wine tasting evening" 4
我是一名优秀的程序员,十分优秀!