In this program, I am taking a 64-bit double and reinterpreting it as a 64-bit
long long.  I am then reinterpreting that as a 64-bit double again.  The
expected output of the program is "1", but when compiling with -O2, the answer
is gibberish.

---------------------------

chuon...@kartoo:~/speed$ cat bug2.cc
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char **argv) {
  double x = 1;
  long long y = *(long long *)(void *)&x;
  double z = *(double *)(void *)&y;
  z = *(double *)(void *)&y;
  cerr << z << endl;
  return 0;
}
chuon...@kartoo:~/speed$ g++ --version
g++ (Ubuntu 4.3.3-5ubuntu4) 4.3.3
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

chuon...@kartoo:~/speed$ uname -a
Linux kartoo 2.6.28-15-generic #52-Ubuntu SMP Wed Sep 9 10:48:52 UTC 2009
x86_64 GNU/Linux
chuon...@kartoo:~/speed$ g++ -o bug2 bug2.cc
chuon...@kartoo:~/speed$ ./bug2
1
chuon...@kartoo:~/speed$ g++ -o bug2 bug2.cc -O2
chuon...@kartoo:~/speed$ ./bug2
6.95328e-310


-- 
           Summary: GCC optimization error
           Product: gcc
           Version: 4.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: chuongdo at cs dot stanford dot edu


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

Reply via email to