assembly - Do FMA (fused multiply-add) instructions always produce the same result as a mul then add instruction? -
i have assembly (at&t syntax):
mulsd %xmm0, %xmm1 addsd %xmm1, %xmm2
i want replace with:
vfmadd231sd %xmm0, %xmm1, %xmm2
will transformation leave equivalent state in involved registers , flags? or result floats differ in someway? (if differ, why that?)
(about fma instructions: http://en.wikipedia.org/wiki/fma_instruction_set)
no. in fact, major part of benefit of fused multiply-add not (necessarily) produce same result separate multiply , add.
as (somewhat contrived) example, suppose have:
double = 1 + 0x1.0p-52 // 1 + 2**-52 double b = 1 - 0x1.0p-52 // 1 - 2**-52
and want compute a*b - 1
. "mathematically 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 first compute a*b
using multiplication rounds 1.0, subsequent subtraction of 1.0 produces result of zero.
if use fma(a,b,-1)
instead, eliminate intermediate rounding of product, allows "real" answer, -1.0p-104
.
please note not different result, different flags have been set well; separate multiply , subtract sets inexact flag, whereas fused multiply-add not set flags.
Comments
Post a Comment