[llvm-bugs] [Bug 34870] New: [InstCombine] miscompile of __builtin_fmod

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Oct 7 07:21:34 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=34870

            Bug ID: 34870
           Summary: [InstCombine] miscompile of __builtin_fmod
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: spatel+llvm at rotateright.com
                CC: llvm-bugs at lists.llvm.org

#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().

http://en.cppreference.com/w/c/numeric/math/fmod
"If y is ±0 and x is not NaN, NaN is returned"

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20171007/fd5406fa/attachment-0001.html>


More information about the llvm-bugs mailing list