FPK
FPK

Reputation: 2098

Why does clang not optimize away adding 0.0 to a floating-point number?

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.

Upvotes: 1

Views: 202

Answers (0)

Related Questions