User account creation filtered due to spam.

Bug 26214 - gcc -O2 creates erroneous code
Summary: gcc -O2 creates erroneous code
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.0.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2006-02-10 17:12 UTC by steger
Modified: 2006-02-10 18:11 UTC (History)
58 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 steger 2006-02-10 17:12:16 UTC
On the following program, gcc -O2 creates incorrect code:

#include <stdio.h>

#define NUM 10000
#define MAX 10.0

__inline__ float rsqrt(float x)
  float xhalf = 0.5f * x;
  int i = *(int*)&x;
  i = 0x5f375a86 - (i>>1);
  x = *(float*)&i;
  x = x * (1.5f - xhalf * x * x);
  return x;

int main(void)
  int i, j;
  float x, y;

  for (i=0; i<NUM; i++)
    x = (i+1)*MAX/NUM;
    y = rsqrt(x);
    fprintf(stderr,"%10.6f -> %10.6f\n",x,y);

The same code works as expected with gcc 3.3.6.

Interestingly, if "fprintf(stderr," is changed to "printf(" the output changes significantly, but is still incorrect.
Comment 1 Andrew Pinski 2006-02-10 18:11:31 UTC
  x = *(float*)&i;

You are violating C aliasing rules.

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