Surprising @fastmath behavior

According to the docs
https://llvm.org/docs/LangRef.html#fast-math-flags
@fastmath enables the nnan flag, which allows for optimizations to assume the arguments and result are not NaN.

With this in mind I would say that what you encountered is to be expected.

If you use muladd you get the correct behaviour.

function foo(a, b, c)
    x = muladd(a,b,c)
    return isnan(x) ? 0.0 : x
end

code_native(foo, (Float64, Float64, Float64); debuginfo = :none, syntax = :intel)

outputs

vfmadd213sd	xmm0, xmm1, xmm2 # xmm0 = (xmm1 * xmm0) + xmm2
vcmpordsd	xmm1, xmm0, xmm0
vandpd	xmm0, xmm1, xmm0
ret
3 Likes