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.


-- 
           Summary: gcc -O2 creates erroneous code
           Product: gcc
           Version: 4.0.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: steger at mvtec dot com
 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=26214

Reply via email to