The following code gives different results with -O3 (or -O2) and no
optimization.The -O3 output is wrong.
try:
$g++ -O3 test.cpp
$cat test.cpp
#include <iostream>
class foo
{
int _type;
public:
foo( int t ) : _type(t) {};
bool is_1() { return _type == 1; }
bool is_23() { return _type == 2 || _type == 3; }
};
void testit( foo& e1, foo& e2 )
{
if ( e1.is_1() && e2.is_1() )
std::cout << "If - 1.1\n";
else if ( e1.is_23() && e2.is_23() )
std::cout << "If - 23.23\n";
else if ( e1.is_1() && e2.is_23() )
std::cout << "If - 1.23\n";
else if ( e1.is_23() && e2.is_1() )
std::cout << "If - 23.1\n";
else
std::cout << "If - bad\n";
}
int main()
{
for ( int i = 1; i < 4; ++i )
for ( int j = 1; j < 4; ++j )
{
foo e1(i);
foo e2(j);
testit( e1, e2 );
}
return 0;
}
result with -O3 :
$./a.out
If - 1.1
If - 1.23
If - 1.23
If - 23.1
If - 23.23
If - 23.23
If - bad
If - 23.23
If - 23.23
result without optimization :
$./a.out
If - 1.1
If - 1.23
If - 1.23
If - 23.1
If - 23.23
If - 23.23
If - 23.1
If - 23.23
If - 23.23
satya
--
Summary: -O3 and -O2 give wrong results with gcc 4.1.2 on solaris
sparc
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: satyaakam at yahoo dot co dot in
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33249