Cmath, which adds and multiplies immediately x * y + z

Is there a function in the library cmath, which sets the number 3 x, yand zreturns x*y+z?

+4
source share
1 answer

fma, which stands for Fused Multiply Add, was introduced in C99 and C ++ 11 :

#include <cassert>
#include <cmath>

int main() {
    assert(std::fabs(std::fma(2.0, 3.0, 4.0) - (2.0 * 3.0 + 4.0)) < 0.001);
}

Probable justification:

  • IEEE 754-2008 seems to have added support for the operation, requiring it to be done with one rounding instead of two.

    Thanks @ Lฦฐu for raising it in a comment.

  • , ARM x86, fma, /stdlibs .

    , .

    X86 FMA LEA: , , double.

+12

All Articles