[BUG] GCC 4.1 bug - Optimization breaks variables of type double

Le Thanh Trung u0407784@nus.edu.sg
Thu Nov 15 17:12:00 GMT 2007


Hi,

When I compile my program without optimization (-O2 or -O3), it works
normally. However when I compile the code with -O2 or -O3 (GCC version
4.1), my program output wrong data. In addition, I did not see this
problem when I used GCC 3.4.6.

Information regarding my GCC:
OS: Centos 5  (a clone of Redhat Enterprise 5)
GCC: 4.1.1 20070105 (Red Hat 4.1.1-52)

Command:
   gcc -g -Wall gensvmparams.c -lm -o gensvmparams
   gcc -g -Wall -O2 gensvmparams -c -lm -o gensvmparams
   
My code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
  double g = pow(2, -7);
  double c = 2;
  double cc = 1;
  double gg = -7;
  double cMax;
  cMax = pow(2,5);
  cMax = cMax + 0.000001;
  double gMax;
  gMax = pow(2,-3);
  gMax = gMax + 0.000001;

  while (c < cMax)  //gcc -O2 change the cMax to something different
than 32
  {
    gg = -7;
    g = pow(2, -7);
    while (g < gMax)
    {
      printf("5 %lf %lf\n", c, g);
      gg = gg + 0.25; 
      g = pow(2, gg);
    }
    cc = cc + 0.25;
    c = pow(2, cc);
  }
  return 0;
}

P.S: When I add "volatile" to the declaration of cMax and gMax, my
program works correctly for all cases.

Thanks,
Regards,
Le Thanh Trung




More information about the Gcc-bugs mailing list