This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/42197] New: g++ 4.1.2 on x86_64 gives differing results in O0, O1 and O2, O3


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]$


-- 
           Summary: g++ 4.1.2 on x86_64 gives differing results in O0, O1
                    and O2, O3
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tamas_gaal at yahoo dot com
 GCC build triplet: x86_64 x86_64 x86_64
  GCC host triplet: x86_64 x86_64 x86_64
GCC target triplet: x86_64 x86_64 x86_64


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]