为什么Clang不优化为浮点数增加0.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-MATH
的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论