<div dir="ltr">Hi Victor,<div><br></div><div>The fallback implementations in the various libc take care</div><div>to preserve the correct rounding behaviour.</div><div><br></div><div>Let me stress that *fused* multiply-add means the specific rounding</div><div>behaviour as defined in the standard IEEE-754 2008</div><div>(i.e. with guaranteed *no* intermediate rounding).</div><div> </div><div>So the following would not be a valid FMA fallback</div><div><br></div><div>double bad_fma(double x, double y, double z) {</div><div> return x*y + z;</div><div>}</div><div><br></div><div>Now in practice, people want FMA for two reasons.</div><div>1. They need the additional precision.</div><div>2. They want the performance of a hardware FMA instruction.</div><div><br></div><div>Now, admittedly, the second category would be satisfied with the bad_fma fallback.</div><div>However, I don't think 2. is a very compelling reason for fma *in pure Python code*, since</div><div>the performance advantage would probably be dwarfed by interpreter overhead.</div><div><br></div><div>So I would estimate that approx. 100% of the target audience of math.fma would want to use</div><div>it for the increased accuracy. So providing a fallback which does not, in fact, give that</div><div>accuracy would not make people happy.</div><div><br></div><div>Upshot: if we want to provide a software fallback in the Python code, we need to do something</div><div>slow and complicated like musl does. Possibly by actually using the musl code.</div><div><br></div><div>Either that, or we rely on the Python-external libc implementation always.</div><div><br></div><div>Stephan</div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">2017-01-16 9:45 GMT+01:00 Victor Stinner <span dir="ltr"><<a href="mailto:victor.stinner@gmail.com" target="_blank">victor.stinner@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">2017-01-15 18:25 GMT+01:00 Juraj Sukop <<a href="mailto:juraj.sukop@gmail.com">juraj.sukop@gmail.com</a>>:<br>
</span><span class="">> C99 includes `fma` function to `math.h` [6] and emulates the calculation if<br>
> the FMA instruction is not present on the host CPU [7].<br>
<br>
</span>If even the libc function has a fallback on x*y followed by +z, it's<br>
fine to add such function to the Python stdlib. It means that Python<br>
can do the same if the libc lacks a fma() function. In the math<br>
module, the trend is more to implement missing functions or add<br>
special code to workaround bugs or limitations of libc functions.<br>
<span class="HOEnZb"><font color="#888888"><br>
Victor<br>
</font></span><div class="HOEnZb"><div class="h5">______________________________<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>
</div></div></blockquote></div><br></div></div>