User account creation filtered due to spam.

Bug 32714 - Wrong optimisation for -O3
Summary: Wrong optimisation for -O3
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2007-07-10 08:28 UTC by honza
Modified: 2007-07-10 09:23 UTC (History)
16 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description honza 2007-07-10 08:28:20 UTC
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:
Expected results:
to print:

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.
Comment 1 Richard Biener 2007-07-10 09:23:39 UTC
You violate C/C++ aliasing rules by reading/storing a value of type (char *) to
a memory location of type (float *).

*** This bug has been marked as a duplicate of 21920 ***