X * y + z does not automatically use FMA instruction

Using FMA automatically can cause surprises. Consider the following:

function f(a,b,c)
    @assert a*b ≥ c
    return sqrt(a*b-c)
end

a = 1.0 + 0.5^27
b = 1.0 - 0.5^27
c = 1.0
f(a,b,c)

It works as expected. Now suppose the compiler automatically uses FMA inside f, like this:

function f(a,b,c)
    @assert a*b ≥ c
    return sqrt(fma(a,b,-c))
end

Now f(a,b,c) throws and exception, because fma(a,b,-c) is negative, despite the assertion a*b ≥ c.

7 Likes