Fedor
Fedor

Reputation: 20997

Clang fused multiply-add depends on constancy of expression arguments

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.

Upvotes: 5

Views: 290

Answers (0)

Related Questions