为什么Clang不优化为浮点数增加0.0的优化?

发布于 2025-02-08 16:30:48 字数 864 浏览 1 评论 0原文

用clang 14.0.0(x86-64,-o3)编译以下代码

double f (double x)
{
    return x + 5.0 + 0;
}

结果

.LCPI0_0:
  .quad 0x4014000000000000 # double 5
f(double): # @f(double)
  addsd xmm0, qword ptr [rip + .LCPI0_0]
  xorpd xmm1, xmm1
  addsd xmm0, xmm1
  ret

在哪种情况下,该序列

  xorpd xmm1, xmm1
  addsd xmm0, xmm1

有任何区别?

虽然我知道没有-FFAST-MAT​​H的float常数折叠是不可能的,但我看不出任何原因是Xord/addsd sequence需要序列:更改XMM0的位模式,我看不到它如何触发异常或具有任何其他副作用。

编辑:Clang的默认值为-fno-rounding-math请参阅手册/a>)。因此,可以安全地假设x + 5.0永远不会在-0.0中导致+0.0可以被视为no-op。

Compiling the following code with clang 14.0.0 (x86-64, -O3)

double f (double x)
{
    return x + 5.0 + 0;
}

results in

.LCPI0_0:
  .quad 0x4014000000000000 # double 5
f(double): # @f(double)
  addsd xmm0, qword ptr [rip + .LCPI0_0]
  xorpd xmm1, xmm1
  addsd xmm0, xmm1
  ret

(Godbolt).

In which situation does the sequence

  xorpd xmm1, xmm1
  addsd xmm0, xmm1

make any difference?

While I am aware that float constant folding without -ffast-math is no possible in general, I cannot see any reason why the xord/addsd sequence is needed: It does not change the bit pattern in xmm0, I cannot see how it could trigger an exception or have any other side effect.

Edit: clang's default is -fno-rounding-math (see manual). So it is safe to assume x + 5.0 never results in -0.0 and thus +0.0 can be considered a no-op.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文