<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - accuracy error by optimization of pow(x, -0.5) when -ffast-math is disabled"
   href="https://bugs.llvm.org/show_bug.cgi?id=44330">44330</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>accuracy error by optimization of pow(x, -0.5) when -ffast-math is disabled
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>9.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Transformation Utilities
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>utsumi.yuichiro@fujitsu.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following code causes accuracy error even when -ffast-math is disabled.

--------------- pow.c --------------------------
#include <math.h>
#include <stdio.h>

double (*libpow)(double, double) = pow;
double x = 1000000001.0;

int main() {
  double a = libpow(x, -0.5); // not optimized
  double b = pow(x, -0.5); // optimized

  printf("lib pow: %.20g, pow: %.20g\n", a, b);

  return 0;
}
--------------------------------------------------

This code includes `pow(x, -0.5)`.
The following is the result.

-------------------------------------------------------------------
$ clang -O1 pow.c -lm
$ ./a.out
lib pow: 3.1622776585872407456e-05, pow: 3.162277658587240068e-05
-------------------------------------------------------------------

The specified compile option is only `-O1` and `-ffast-math` is disabled but
the value of `lib pow` and `pow` differ.


This behavior is caused by
<a href="https://github.com/llvm/llvm-project/blob/release/9.x/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp#L1431">https://github.com/llvm/llvm-project/blob/release/9.x/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp#L1431</a>
I think the optimization pow(x, -0.5) -> 1.0/sqrt(x) is admitted only when fast
math flag exist.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>