Bug 42197 - g++ 4.1.2 on x86_64 gives differing results in O0, O1 and O2, O3
g++ 4.1.2 on x86_64 gives differing results in O0, O1 and O2, O3
Status: RESOLVED DUPLICATE of bug 21920
Product: gcc
Classification: Unclassified
Component: c++
unknown
: P3 normal
: ---
Assigned To: Not yet assigned to anyone
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2009-11-27 16:17 UTC by Tamas GAAL
Modified: 2009-11-27 21:39 UTC (History)
36 users (show)

See Also:
Host: x86_64 x86_64 x86_64
Target: x86_64 x86_64 x86_64
Build: x86_64 x86_64 x86_64
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 Tamas GAAL 2009-11-27 16:17:49 UTC
When compiling and executing the little demo program (enclosed) using g++ 4.1.2 on a x86_64 machine it gives differing results when compiled with -O0, -O1 versus -O2, -O3 optimisation flags. I suspect the latter results (with O2, O3) are wrong. 

The same (I think bad) result when tried with g++ 4.1.1, on a i686 i686 i386 machine, too.

Works well with any optimisation on an older Sun machine (sun4u sparc SUNW,Sun-Blade-1000), using gcc version 3.4.3 (csl-sol210-3_4-branch+sol_rpath).

Tamas Gaal

Enclosed: Code; log of experiments

// BEGIN CODE ----
// 
// test, on ------, an appartent compiler malfuction when compiled with -O3
//                 uname -a
//                Linux ---------- 2.6.24.7-92.fc8 #1 SMP Wed May 7 16:26:02 EDT 2008 x86_64 x86_64 x86_64 GNU/Linux
// 
// (Using gcc version 4.1.2 20070925 (Red Hat 4.1.2-33))
//
//                   Target: x86_64-redhat-linux
//                   Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-cpu=generic --host=x86_64-redhat-linux
// Thread model: posix
// gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)
// 
// Compile as:
//
// 1. g++ -O1 thisfile.cpp
// 2. g++ -O2 thisfile.cpp
// 3. g++ -O3 thisfile.cpp
//
// Problem: using the above architecture (x86_64) and compiler (g++ 4.1.2) the result from 1. differs from 2 and 3. I suspect 1. is correct and 2 and 3 are not.
//

#include <iostream>
using namespace std;

typedef int Weight;
// casts between Weight and other types
#define wgt2float(W)  (*((float*)&W))

int main (int argc, char* argv[])
{
  Weight w1=1097776405; // encodes ~ float 14.9212
  float fw1=wgt2float(w1);  // expecting ~ 14.9212
  float f1=14.9212;

  int w2=1084443932; // wgt2float(w)=5.10316
  float fw2=wgt2float(w2); // expecting ~ 5.10316
  float f2=5.10316; 

  cerr << "w1=" << w1 << " =?=expected: " << 1097776405 << endl; // expecting 1097776405
  cerr << "wgt2float(w1)=" << wgt2float(w1) << " =?=expected: " << 14.9212 << endl;   // expecting 14.9212
  cerr << "fw1=" << fw1 << " =?=expected: " << 14.9212 << endl; ; // expecting 14.9212
  cerr <<"f1="   << f1  << " =?=expected: " << 14.9212 << endl; // expecting 14.9212
  cerr << endl;
  cerr << "w2=" << w2  << " =?=expected: " << 1084443932  << endl; // expecting 1084443932
  cerr << "wgt2float(w2)=" << wgt2float(w2) << " =?=expected: " << 5.10316 << endl ; // expecting 5.10316 
  cerr << "fw2=" << fw2 << " =?=expected: " << 5.10316  << endl; ; // expecting 5.10316 
  cerr << "f2="  << f2  << " =?=expected: " << 5.10316  << endl;   // expecting 5.10316 
}
// END CODE ----

Log of experiments:
mymachine(17:03)C++[66]$ g++ -O0 mymachine_pb.cpp
mymachine(17:04)C++[67]$ ./a.out 
w1=1097776405 =?=expected: 1097776405
wgt2float(w1)=14.9212 =?=expected: 14.9212
fw1=14.9212 =?=expected: 14.9212
f1=14.9212 =?=expected: 14.9212

w2=1084443932 =?=expected: 1084443932
wgt2float(w2)=5.10316 =?=expected: 5.10316
fw2=5.10316 =?=expected: 5.10316
f2=5.10316 =?=expected: 5.10316
mymachine(17:04)C++[68]$ g++ -O3 mymachine_pb.cpp
mymachine(17:04)C++[69]$ ./a.out
w1=1097776405 =?=expected: 1097776405
wgt2float(w1)=14.9212 =?=expected: 14.9212
fw1=0 =?=expected: 14.9212
f1=14.9212 =?=expected: 14.9212

w2=1084443932 =?=expected: 1084443932
wgt2float(w2)=5.10316 =?=expected: 5.10316
fw2=0 =?=expected: 5.10316
f2=5.10316 =?=expected: 5.10316
mymachine(17:04)C++[70]$ g++ -O2 mymachine_pb.cpp
mymachine(17:04)C++[71]$ ./a.out
w1=1097776405 =?=expected: 1097776405
wgt2float(w1)=14.9212 =?=expected: 14.9212
fw1=0 =?=expected: 14.9212
f1=14.9212 =?=expected: 14.9212

w2=1084443932 =?=expected: 1084443932
wgt2float(w2)=5.10316 =?=expected: 5.10316
fw2=0 =?=expected: 5.10316
f2=5.10316 =?=expected: 5.10316
mymachine(17:04)C++[72]$ uname -a
Linux mymachine.--------- 2.6.24.7-92.fc8 #1 SMP Wed May 7 16:26:02 EDT 2008 x86_64 x86_64 x86_64 GNU/Linux
mymachine(17:04)C++[73]$ g++ -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-cpu=generic --host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)
mymachine (17:05)C++[74]$
Comment 1 Paolo Carlini 2009-11-27 16:48:05 UTC
4.1.x is very old and not maintained anymore. Try something more recent, eg, 4.4.x, and, before reporting anything make sure you are not violating aliasing rules (double check if the result changes with -fno-strict-aliasing and whether -Wstrict-aliasing triggers any warnings, actually it **does**...)
Comment 2 Paolo Carlini 2009-11-27 16:52:47 UTC
Reopening to...
Comment 3 Paolo Carlini 2009-11-27 16:53:09 UTC
... close as duplicate.

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