<div dir="ltr"><div dir="ltr">On Wed, Sep 8, 2021 at 1:58 PM Joerg Sonnenberger via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Wed, Sep 08, 2021 at 06:04:08PM +0000, Chris Tetreault via llvm-dev wrote:<br>
> As a developer (who always reads the docs and generally makes good life<br>
> choices), if I turn on -ffast-math, I want the compiler to produce the<br>
> fastest possible floating point math code possible, floating point<br>
> semantics be darned. Given this viewpoint, my opinion on this topic is<br>
> that the compiler should do whatever it wants, given the constraints of<br>
> the documented behavior of NaN.<br>
<br>
There is a huge different between optimisations that assume NaN is not<br>
present and breaking checks for them. I'm not convinced at all that<br>
constant-folding isnan to false will actually speed up real world code.<br></blockquote><div><br></div><div>I am.</div><div><br></div><div>Here's the first 20ish lines of the code in question, which you can see in full at <a href="https://github.com/abseil/abseil-cpp/blob/master/absl/strings/numbers.cc">https://github.com/abseil/abseil-cpp/blob/master/absl/strings/numbers.cc</a> :<br><br><div style="color:rgb(0,0,0);background-color:rgb(255,255,254);font-family:Consolas,"Liberation Mono",Courier,monospace,"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;line-height:19px;white-space:pre"><div>size_t numbers_internal::SixDigitsToBuffer(<span style="color:rgb(0,0,255)">double</span> d, <span style="color:rgb(0,0,255)">char</span>* <span style="color:rgb(0,0,255)">const</span> buffer) {</div><div>  <span style="color:rgb(0,0,255)">static_assert</span>(std::numeric_limits<<span style="color:rgb(0,0,255)">float</span>>::is_iec559,</div><div>                <span style="color:rgb(163,21,21)">"IEEE-754/IEC-559 support only"</span>);</div><br><div>  <span style="color:rgb(0,0,255)">char</span>* out = buffer;  <span style="color:rgb(0,128,0)">// we write data to out</span></div><br><div>  <span style="color:rgb(0,0,255)">if</span> (std::isnan(d)) {</div><div>    strcpy(out, <span style="color:rgb(163,21,21)">"nan"</span>);  <span style="color:rgb(0,128,0)">// NOLINT(runtime/printf)</span></div><div>    <span style="color:rgb(0,0,255)">return</span> <span style="color:rgb(9,134,88)">3</span>;</div><div>  }</div><div>  <span style="color:rgb(0,0,255)">if</span> (d == <span style="color:rgb(9,134,88)">0</span>) {  <span style="color:rgb(0,128,0)">// +0 and -0 are handled here</span></div><div>    <span style="color:rgb(0,0,255)">if</span> (std::signbit(d)) *out++ = <span style="color:rgb(163,21,21)">'-'</span>;</div><div>    *out++ = <span style="color:rgb(163,21,21)">'0'</span>;</div><div>    *out = <span style="color:rgb(9,134,88)">0</span>;</div><div>    <span style="color:rgb(0,0,255)">return</span> out - buffer;</div><div>  }</div><div>  <span style="color:rgb(0,0,255)">if</span> (d < <span style="color:rgb(9,134,88)">0</span>) {</div><div>    *out++ = <span style="color:rgb(163,21,21)">'-'</span>;</div><div>    d = -d;</div><div>  }</div><div>  <span style="color:rgb(0,0,255)">if</span> (std::isinf(d)) {</div><div>    strcpy(out, <span style="color:rgb(163,21,21)">"inf"</span>);  <span style="color:rgb(0,128,0)">// NOLINT(runtime/printf)</span></div><div>    <span style="color:rgb(0,0,255)">return</span> out + <span style="color:rgb(9,134,88)">3</span> - buffer;</div><div>  }</div><br></div><br>This routine formats a double-precision floating-point number as 6 digits, the same way "%g" would do it.  The calls to std::isnan and std::isinf are a measurable slowdown.</div><div><br></div><div>(In fact, as a result of this discussion I'm realizing that the call to isinf can be replaced with a comparison with positive infinity, since negative infinity is not a possibility here.  Patch in progress...)</div><div><br></div><div>-- Jorg</div></div></div>