Overview:
When using optimisation level -O3 the code generated from attached example is
wrong. It is accumalating the stream increment and thus the function readFloat
reads twice the same number and then it skips two in a row.
Problem seems to be in this line 
    const float val = *(const float *&)stream;
when I change it to 
    const float val = *(const float *)stream;
it starts working even on -O3.

Steps to reproduce:
Compile this code with -O3.

#include <stdio.h>
float readFloat(const char *&stream)
{
    const float val = *(const float *&)stream;
    stream += sizeof(float);
    return val;
}
int main(int argc, char **argv)
{
    float stream[] = { 2.0f,
        1.0f, 2.0f,
        3.0f, 4.0f,
        5.0f, 6.0f };
    const char *stream2 = (const char *) stream;

    for (float i = 0, n = readFloat(stream2); i <= n; i += 1.0f) {
        const float x = readFloat(stream2);
        const float y = readFloat(stream2);
        printf("%f,%f\n", x,y);
    }
    return 0;
}

Actual results:
Prints:
1.000000,1.000000
3.000000,3.000000
5.000000,5.000000
Expected results:
to print:
1.000000,2.000000
3.000000,4.000000
5.000000,6.000000

Build date & platform:
gcc (GCC) 4.1.2 20070626 (Red Hat 4.1.2-13)

Additional Builds and Platforms:
gcc (GCC) 4.0.2
gcc (GCC) 4.1.1
gcc (GCC) 4.2.0

This bug is not reproducible on gcc 3.3 series.


-- 
           Summary: Wrong optimisation for -O3
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: honza at jikos dot cz
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32714

Reply via email to