User account creation filtered due to spam.

Bug 41864 - GCC optimization error
Summary: GCC optimization error
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-10-29 05:38 UTC by Chuong Do
Modified: 2009-10-29 05:49 UTC (History)
35 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Chuong Do 2009-10-29 05:38:51 UTC
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.

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

chuongdo@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;
}
chuongdo@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.

chuongdo@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
chuongdo@kartoo:~/speed$ g++ -o bug2 bug2.cc
chuongdo@kartoo:~/speed$ ./bug2
1
chuongdo@kartoo:~/speed$ g++ -o bug2 bug2.cc -O2
chuongdo@kartoo:~/speed$ ./bug2
6.95328e-310
Comment 1 Andrew Pinski 2009-10-29 05:49:30 UTC
You are volating C/C++ aliasing rules even with the casts to void*.  What matters is the type of accesses.

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