FMA instructions (with smooth multiplication) always give the same result as mul, and then add the instruction?

I have this assembly (AT & T syntax):

mulsd %xmm0, %xmm1 addsd %xmm1, %xmm2 

I want to replace it:

 vfmadd231sd %xmm0, %xmm1, %xmm2 

Will this conversion always leave an equivalent state in all involved registers and flags? Or will the result be slightly different? (If they are different, why?)

(About FMA instructions: http://en.wikipedia.org/wiki/FMA_instruction_set )

+6
source share
1 answer

No. In fact, the main part of the advantage of smooth multiplication-addition is that it (necessarily) does not give the same result as a separate multiply and add.

As an (somewhat far-fetched) example, suppose we have:

 double a = 1 + 0x1.0p-52 // 1 + 2**-52 double b = 1 - 0x1.0p-52 // 1 - 2**-52 

and we want to calculate a*b - 1 . The "mathematically accurate" value of a*b - 1 :

 (1 + 2**-52)(1 - 2**-52) - 1 = 1 + 2**-52 - 2**52 - 2**-104 - 1 = -2**-104 

but if we first calculate a*b using multiplication, round to 1.0, so a subsequent subtraction of 1.0 will result in a result of zero.

If we use fma(a,b,-1) instead, we eliminate the intermediate rounding of the work, which allows us to get a β€œreal” answer of -1.0p-104 .

Please note that we not only get a different result, but also other flags; separate multiplication and subtraction sets an inaccurate flag, while compiled multiple additions do not set any flags.

+12
source

Source: https://habr.com/ru/post/983884/


All Articles