<div dir="ltr">Hi Juraj,<div><br></div><div>I think this would be a very useful addition to the `math' module.</div><div><br></div><div>The gating issue is probably C compiler support.</div><div>The most important non-C99 C compiler for Python is probably MS Visual Studio.</div><div>And that one appears to support it:</div><div><br></div><div><a href="https://msdn.microsoft.com/en-us/library/mt720715.aspx">https://msdn.microsoft.com/en-us/library/mt720715.aspx</a><br></div><div><br></div><div>So +1 on the proposal.</div><div><br></div><div>Stephan</div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-01-15 18:25 GMT+01:00 Juraj Sukop <span dir="ltr"><<a href="mailto:juraj.sukop@gmail.com" target="_blank">juraj.sukop@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hello!</div><div><br></div><div>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:</div><div><br></div><div>    r = x*y + z</div><div><br></div><div>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 [1].</div><div><br></div><div>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 [2], compensated arithmetic [3], polynomial evaluation [4], matrix multiplication, Newton's method and many more [5].</div><div><br></div><div>C99 includes `fma` function to `math.h` [6] and emulates the calculation if the FMA instruction is not present on the host CPU [7]. 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" [8].</div><div><br></div><div>This proposal is then about adding new `fma` function with the following signature to `math` module:</div><div><br></div><div>    math.fma(x, y, z)</div><div>    </div><div>'''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.'''</div><div><br></div><div>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 [9].</div><div><br></div><div>Any feedback is greatly appreciated!</div><div><br></div><div>Juraj Sukop</div><div><br></div><div>[1] <a href="https://en.wikipedia.org/wiki/Multiply%E2%80%93accumulate_operation" target="_blank">https://en.wikipedia.org/wiki/<wbr>Multiply%E2%80%93accumulate_<wbr>operation</a></div><div>[2] S. Graillat, P. Langlois, N. Louvet. Accurate dot products with FMA. 2006</div><div>[3] S. Graillat, Accurate Floating Point Product and Exponentiation. 2007.</div><div>[4] S. Graillat, P. Langlois, N. Louvet. Improving the compensated Horner scheme with a Fused Multiply and Add. 2006</div><div>[5] 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</div><div>[6] ISO/IEC 9899:TC3, "7.12.13.1 The fma functions", Committee Draft - Septermber 7, 2007</div><div>[7] <a href="https://git.musl-libc.org/cgit/musl/tree/src/math/fma.c" target="_blank">https://git.musl-libc.org/<wbr>cgit/musl/tree/src/math/fma.c</a></div><div>[8] <a href="https://www.python.org/dev/peps/pep-0007/" target="_blank">https://www.python.org/dev/<wbr>peps/pep-0007/</a></div><div>[9] <a href="https://github.com/sukop/fma" target="_blank">https://github.com/sukop/fma</a></div></div>
<br>______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br></blockquote></div><br></div>