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++/13890] New: -O1, -O2, and -O3 fail to determine two values are equal without a delay


I run across an issue related to different branching when various levels of
optimization are specified. I've encapsulated an example of the behavior in the
simple C++ code attached at the bottom of this note.

The problem appease both on a standard RedHat 9.0 and Fedora Core 1 release. The
following shows the output of gcc --version:

[pkb@salsa tool]$ gcc --version
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
Copyright (C) 2002 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.
[pkb@salsa tool]$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

[root@quesadilla tmp]# gcc --version
gcc (GCC) 3.3.2 20031022 (Red Hat Linux 3.3.2-1)
Copyright (C) 2003 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.
 [root@quesadilla tmp]# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.3.2 20031022 (Red Hat Linux 3.3.2-1)



The following shows the output from the attached example based upon the
optimization level (the first run produces the correct output):

[root@quesadilla tmp]# g++ t.cc
[root@quesadilla tmp]# ./a.out
same on first check
same on first check
same on first check
same on first check
same on first check
same on first check
same on first check
same on first check
same on first check
same on first check
[root@quesadilla tmp]# g++ -O2 t.cc
[root@quesadilla tmp]# ./a.out
[root@quesadilla tmp]# g++ -O3 t.cc
[root@quesadilla tmp]# ./a.out
[root@quesadilla tmp]# g++ -O1 t.cc
[root@quesadilla tmp]# ./a.out
now they are the same!
[root@quesadilla tmp]#

Here is the example, which encapsulates the problem (this is not real code -
just my way of taking the problem that showed up and putting it into a simple
C++ file for demonstration purposes):

#include <cstdlib>
#include <iostream>
 
using namespace std;
 
double foo() {
  static double next=1.1;
  static bool computeNext=true;
  if (computeNext) {
    next *= std::sqrt(next);
  }
  computeNext = !computeNext;
  return next;
}
 
int main(int argc, char** argv) {
  for (int j = 0; j < 10; j++) {
    //    double ong = foo();
    //    double cng = foo();
    double ong = std::sqrt(7.0);
    double cng = std::sqrt(7.0);
 
    if (ong == cng) {
      std::cerr << "same on first check\n";
    } else if (ong != cng) {
      for (double k = 1.1; k < 10; k *= std::sqrt(k));
      if (ong == cng) {
        std::cerr << "now they are the same!\n";
        return 1;
      }
    } else {
      std::cerr << "not same, then they were?\n";
      return 2;
    }
  }
  return 0;
}

Please feel free to contact me with any questions.

-- 
           Summary: -O1, -O2, and -O3 fail to determine two values are equal
                    without a delay
           Product: gcc
           Version: 3.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: paul_blankenbaker at yahoo dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i386-redhat-linux
  GCC host triplet: i386-redhat-linux
GCC target triplet: i386-redhat-linux


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


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