[Python-ideas] Fused multiply-add (FMA)
juraj.sukop at gmail.com
Sun Jan 15 12:25:59 EST 2017
Fused multiply-add (henceforth FMA) is an operation which calculates the
product of two numbers and then the sum of the product and a third number
with just one floating-point rounding. More concretely:
r = x*y + z
The value of `r` is the same as if the RHS was calculated with infinite
precision and the rounded to a 32-bit single-precision or 64-bit
double-precision floating-point number .
Even though one FMA CPU instruction might be calculated faster than the two
separate instructions for multiply and add, its main advantage comes from
the increased precision of numerical computations that involve the
accumulation of products. Examples which benefit from using FMA are: dot
product , compensated arithmetic , polynomial evaluation , matrix
multiplication, Newton's method and many more .
C99 includes `fma` function to `math.h`  and emulates the calculation if
the FMA instruction is not present on the host CPU . PEP 7 states that
"Python versions greater than or equal to 3.6 use C89 with several select
C99 features" and that "Future C99 features may be added to this list in
the future depending on compiler support" .
This proposal is then about adding new `fma` function with the following
signature to `math` module:
math.fma(x, y, z)
'''Return a float representing the result of the operation `x*y + z` with
single rounding error, as defined by the platform C library. The result is
the same as if the operation was carried with infinite precision and
rounded to a floating-point number.'''
There is a simple module for Python 3 demonstrating the fused multiply-add
operation which was build with simple `python3 setup.py build` under Linux
Any feedback is greatly appreciated!
 S. Graillat, P. Langlois, N. Louvet. Accurate dot products with FMA.
 S. Graillat, Accurate Floating Point Product and Exponentiation. 2007.
 S. Graillat, P. Langlois, N. Louvet. Improving the compensated Horner
scheme with a Fused Multiply and Add. 2006
 J.-M. Muller, N. Brisebarre, F. de Dinechin, C.-P. Jeannerod, V.
Lefèvre, G. Melquiond, N. Revol, D. Stehlé, S. Torres. Handbook of
Floating-Point Arithmetic. 2010. Chapter 5
 ISO/IEC 9899:TC3, "22.214.171.124 The fma functions", Committee Draft -
Septermber 7, 2007
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas