Bug 13890 - -O1, -O2, and -O3 fail to determine two values are equal without a delay
Summary: -O1, -O2, and -O3 fail to determine two values are equal without a delay
Status: RESOLVED DUPLICATE of bug 323
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.2
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-01-27 22:44 UTC by Paul Blankenbaker
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host: i386-redhat-linux
Target: i386-redhat-linux
Build: i386-redhat-linux
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 Paul Blankenbaker 2004-01-27 22:44:13 UTC
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.
Comment 1 Wolfgang Bangerth 2004-01-27 22:51:14 UTC
So are the joys of floating point computations. 
 
This is a duplicate of PR 323. 
 
W. 

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