5

As indicated in the answer to clang 14.0.0 floating point optimizations, Clang since version 14 applies fused multiply add (FMA) instructions even for constant computations performed at compile-time.

At the same time, one can observe that the result depends on formal constancy of expression arguments:

#include <stdio.h>

int main() {
    const float A = 2.1f;
    const float B = 0.1f;
          float C = 0.1f;
    float V = A * B - A * B;
    float W = A * C - A * C;
    printf( "%g %g", V, W );
}

In Clang the program prints 0 1.49011e-10, online demo: https://godbolt.org/z/a3fcYG7ob

From the assembly code, it can be seen that both V and W are evaluated in compile-time. Is there some rule that dictates that only W can be evaluated using FMA instruction?

Adding -mno-fma command line option for disabling FMA instruction does not change anything in the result.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Fedor
  • 17,146
  • 13
  • 40
  • 131
  • 1
    Also related: [Why does C++ rounding behavior (for compile-time constants) change if math is moved to an inline function?](https://stackoverflow.com/q/76214764) re: effect of const or constexpr on FP-contraction (to FMA) vs. constant-propagation. Also [clang 14.0.0 floating point optimizations](https://stackoverflow.com/q/73985098) – Peter Cordes Jul 04 '23 at 20:40
  • don't think this is what you want, but it seems you can force compile usage of FMA by explicitly using it: https://godbolt.org/z/adjxYr19K – Sam Mason Jul 05 '23 at 09:37

0 Answers0