Sunday, August 13, 2017

Commutative FP Addition and Multiplication

In computer arithmetic, floating point addition and multiplication cannot be associative, because of rounding.



I.e. (A+B)+C != A+(B+C)



But... FP add and multiply are frequently also not commutative, in floating point instruction sets.  Not fundamentally, not because of rounding, but because of NaN and other special value propagation.



I.e.

   FADD fd := fa + fb      !=       FADD fd := fb + fa    



because, if both operands are QNaNs, the instruction set may be defined to propagate the QNaN in the first operand.



This simple rule allows compiler control - but it breaks commutativity.





Losing commutativity has several downsides:



a) the instruction set cannot use operand order to provide extra information, essentially an extra opcode bit.



b) it means that a compiler cannot freely reverse operands.



Does not matter for

   FADD fd := fa + fb      !=       FADD fd := fb + fa    


But does matter for srcdst:



   FADD fa +=fb      !=       FADD fb += fa    


    





Other NaN propagation rules would support commutativity.



E.g. choose the NaN whose value is smallest, if the entire NaN is interpreted as an integer bitpattern.  Or largest. Signed or unsigned.



(This may be appropriate if something like a line number is encoded in the NaN.)



E.g. merge NaN operands in some way.


No comments:

Post a Comment