<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 - Miscompile with -ffast-math and -ftree-vectorize on arithmetic involving doubles and unsigned longs"
   href="https://bugs.llvm.org/show_bug.cgi?id=43609">43609</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Miscompile with -ffast-math and -ftree-vectorize on arithmetic involving doubles and unsigned longs
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>8.0
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>C
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>rlcamp.pdx@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following program:

#include <stdlib.h>
#include <stdio.h>

__attribute((noinline)) void fill_vector(double * x, const double dx, const
size_t X) {
    /* fill vector with uniformly spaced values */
    for (size_t ix = 0; ix < X; ix++)
        x[ix] = (ix + 0.5) * dx;
}

int main(void) {
    const size_t X = 19;
    double * x = malloc(sizeof(double) * X);

    fill_vector(x, 1.0, X);

    /* print the values */
    for (size_t ix = 0; ix < X; ix++)
        fprintf(stdout, "x[%zu] = %g\n", ix, x[ix]);
}

produces the following output in Clang 8 and newer with -Ofast (or -O1
-ffast-math -ftree-vectorize):

x[0] = 0
x[1] = 2
x[2] = 2
x[3] = 4
x[4] = 4
x[5] = 6
x[6] = 6
x[7] = 8
x[8] = 8
x[9] = 10
x[10] = 10
x[11] = 12
x[12] = 12
x[13] = 14
x[14] = 14
x[15] = 16
x[16] = 16.5
x[17] = 17.5
x[18] = 18.5

Note the change from incorrect to correct behaviour between the groups-of-4 and
the last three entries. 

Changing the loop variable from "size_t" to "unsigned int" makes the problem go
away. Compiling with "-fno-associative-math" or, strangely,
"-fno-reciprocal-math" also makes it go away.</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>