为什么 Clang 优化 x * 1.0 而不是 x + 0.0?

Why does Clang optimize away x * 1.0 but NOT x + 0.0?

为什么 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);
}

(同时标记为 C 和 C++,因为我想知道两者的答案是否不同。)

如果 x-0.0

x += 0.0 就不是 NOOP。不过,优化器可以去掉整个循环,因为没有使用结果。一般来说,很难说出优化器为什么会做出这样的决定。

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.

加法案例

在默认的舍入模式下 (Round-to-Nearest, Ties-to-Even),我们看到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 与加数的 none 符号不同,而+0.0 两个 的加数符号不同,违反了本条款。
  • +0.0,那么这就简化为上面在 加法案例 中考虑的加法案例 (+0.0) + (-0.0),根据 §6.3p3 被裁定为给出 +0.0.

由于在所有情况下输入值作为输出都是合法的,因此可以将 x-0.0 视为空操作,将 x == x-0.0 视为重言式。

改变价值的优化

IEEE 754-2008 标准有以下有趣的引用:

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.
  • [...]

由于所有 NaN 和所有无穷大共享相同的指数,并且 x+0.0x*1.0 的正确舍入结果对于有限 x 具有与 [=11= 完全相同的大小], 他们的指数是一样的

sNaNs

信号 NaN 是浮点陷阱值;它们是特殊的 NaN 值,用作浮点操作数会导致无效操作异常 (SIGFPE)。如果触发异常的循环被优化掉,软件的行为将不再相同。

然而,作为 user2357112 ,C11 标准明确未定义发送 NaN 信号的行为 (sNaN),因此允许编译器假设它们不会发生,因此它们引发的异常也不会发生。 C++11 标准省略了对发送 NaN 信号的行为的描述,因此也未定义它。

舍入模式

在替代舍入模式中,允许的优化可能会改变。例如,在 Round-to-Negative-Infinity 模式下,优化 x+0.0 -> x 变为允许,但 x-0.0 -> x 变为禁止。

为了防止 GCC 采用默认的舍入模式和行为,可以将实验标志 -frounding-math 传递给 GCC。

结论

Clang 和 GCC,即使在 -O3,仍然符合 IEEE-754。这意味着它必须遵守 IEEE-754 标准的上述规则。 x+0.0 对于所有 x 在这些规则下 x 不完全相同 ,但是 x*1.0 可能是选择如此: 即当我们

  1. x 为 NaN 时,遵循不改变传递有效载荷的建议。
  2. 保留 NaN 结果的符号位 * 1.0
  3. x 不是 NaN 时,在 quotient/product 期间服从对符号位进行 XOR 的命令。

要启用 IEEE-754 不安全优化 (x+0.0) -> x,需要将标志 -ffast-math 传递给 Clang 或 GCC。