fma (fused multiply-add): a faster, more accurate instruction
added on 2021/12/03 @ 14:58:57 | 2034 views| category: gamedev

When people look at my shader code, one of the most frequently asked questions is why I use the GLSL fma instruction (or its HLSL equivalent mad) so frequently. In spite of the punny title of this post, fma actually stands for fused multiply-add, i.e. it implements the formula fma(a,b,c)=a*b+c. It is faster than separate multiplication and addition because on most CPUs and GPUs it counts as one instruction. It also introduces less rounding error. The real question is whether you should rely on the compiler to use it as appropriate or not. This post explains why I don't and shows a few neat numerical tricks that benefit from fma.

fma() in GLSL:

tags: #programming #glsl