2

Given the following C code,

    double a = 4.00;
    double b = 123.456789;
    double c = 15.123;

    double res = a*b + c;

    return res;

compiling with riscv64-linux-gcc -g -o2 generates the following

    double res = a*b + c;
   102e8:   fe843707            fld fa4,-24(s0)
   102ec:   fe043787            fld fa5,-32(s0)
   102f0:   12f777d3            fmul.d  fa5,fa4,fa5
   102f4:   fd843707            fld fa4,-40(s0)
   102f8:   02f777d3            fadd.d  fa5,fa4,fa5
   102fc:   fcf43827            fsd fa5,-48(s0)

Meaning that the compiler did not recognize that this is a fused multiplication/addition operation (FMADD).

So, I have two questions:

  • Is there a means to force the compiler to generate FMADD.D instead of the separate MUL.D and ADD.D instructions?

  • What mad the compiler choose to implement it this way?

(from what I know, FMA is generally more efficient that separate MUL and ADD, and also, 1 instruction is generally more efficient than 2 from a power and speed point of view, so I'm a little bit confused about the choices being made by GCC)

noureddine-as
  • 453
  • 4
  • 12
  • 1
    Well, first of all, you could check the macro FP_FAST_FMA (in math.h). This will at least tell you whether or not GCC would consider FMA to actually be faster that MUL/ADD on your platform. And if it is, you could use the fma() function instead (which hopefully will cause the compiler to emit an FMA instruction) – Felix G Nov 18 '19 at 10:37
  • 2
    There's the `fma()` library function which GCC knows how to inline when an FMA instruction is available. Did you enable optimization? It looks like no, or GCC would have done constant-propagation. GCC might have done FP contraction on that expression even at `-O0` but chose not to look for it. `-o2` sets the output filename to `2`. GCC does default to FP_CONTRACT enabled, even across expressions. – Peter Cordes Nov 18 '19 at 10:37
  • Yup, with optimization enabled we get contraction of `a*b+c` into FMA, for a function that takes args (so constant-propagation can't just turn it into a load of the answer): https://godbolt.org/z/wHpCxZ – Peter Cordes Nov 18 '19 at 10:40
  • You may also have a look at https://stackoverflow.com/questions/43352510/difference-in-gcc-ffp-contract-options – Giovanni Cerretani Nov 18 '19 at 11:29
  • @PeterCordes ow, my bad, it's was due to the -o flag. Correcting to -O produced the FMADD instruction. Thanks! – noureddine-as Nov 18 '19 at 13:09

0 Answers0