<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 - [InstCombine] miscompile of __builtin_fmod"
   href="https://bugs.llvm.org/show_bug.cgi?id=34870">34870</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[InstCombine] miscompile of __builtin_fmod
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>All
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>spatel+llvm@rotateright.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>#include <stdio.h>

double foo(int cond, double x, double y) {
  double z = cond ? 0.0 : y;
  return __builtin_fmod(x, z);
}

int main() {
  printf("fmod(%f, %f) = %f\n", 1.0, 2.0, foo(0, 1.0, 2.0));

  // If the 1st param of foo() is true (non-zero), the 3rd param is replaced
  printf("fmod(%f, %f) = %f\n", 1.0, 0.0, foo(1, 1.0, 2.0));

  return 0;
}

$ clang -O0 fmod.c ; ./a.out
fmod(1.000000, 2.000000) = 1.000000
fmod(1.000000, 0.000000) = nan

$ clang -O1 fmod.c ; ./a.out
fmod(1.000000, 2.000000) = 1.000000
fmod(1.000000, 0.000000) = 1.000000

-----------------------------------------------------------------------------

The optimized code is wrong for the 2nd case -- assuming that __builtin_fmod()
should behave like the libm function fmod().

<a href="http://en.cppreference.com/w/c/numeric/math/fmod">http://en.cppreference.com/w/c/numeric/math/fmod</a>
"If y is ±0 and x is not NaN, NaN is returned"</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>