Friday, 15 February 2013

assembly - Do FMA (fused multiply-add) instructions always produce the same result as a mul then add instruction? -


I have this assembly (AT & T syntax):

  mulsd% xmm0, % I want to replace it with:  
  vfmadd231sd% xmm0,% xmm1,% xmm2   

Pre>

Will this change always leave an equal state in the registered registrars and flags? Or will the result float in some degree? (If they are different, then why is it?)

(About FMA instructions :)

No. In fact, a large part of the benefit of fuse multiplication is that (not necessary) does not produce the same result as a separate multiplication and addition.

Example (to some degree contrived) As an example, let's say we have:

  double A = 1 + 0x1.0p-52 // 1 + 2 ** - 52 double B = 1 - 0x1.0p-52 // 1 - 2 ** - 52  

and we calculate a * b - 1 Want to do The "mathematical exact" value of a * b - 1 is:

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

But if we first for a * b Using multiplication, it makes goals 1.0, then the subsequent subtraction of 1.0 produces zero results.

If we use fma (a, b, -1) Instead, we end the intermediate goal of the product, to get us the "real" answer Is allowed, -1.0p-104 .

Please note that we not only have a different result, but different flags have been set too; A different multiplication and subtraction sets an irregular flag, whereas fuses multiplication does not set a flag.


No comments:

Post a Comment