112. Applying the Vector API to compute FMA
In a nutshell, Fused Multiply Add (FMA) is the mathematical computation (a*b) + c, which is heavily exploited in matrix multiplications. That’s all we need to cover for this problem, but if you need a primer on FMA, consider Java Coding Problems, First Edition, Chapter 1, problem 38.
Implementing FMA via the Vector API can be done via the fma(float b, float c)
or fma(Vector<Float> b, Vector<Float> c)
operation, the latter is the one you’ll see in an example shortly.
Let’s assume that we have the following two arrays:
float[] x = new float[]{1f, 2f, 3f, 5f, 1f, 8f};
float[] y = new float[]{4f, 5f, 2f, 8f, 5f, 4f};
Computing FMA(x
, y
) can be expressed as the following sequence: 4+0=4 → 10+4=14 → 6+14=20 → 40+20=60 → 5+60=65 → 32+65=97. So, FMA(x
, y
) = 97. Expressing this sequence via the Vector API can be done as shown in the following code:
private static...