http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47964
Summary: logical || returns false result, optimization level 02
or 03
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: critical
Priority: P3
Component: c++
AssignedTo: unassig...@gcc.gnu.org
ReportedBy: rob.bob@hotmail.com
A very simple expression
return a < C || ~a < C;
produces incorrect result if compiled with optimization level O2 or O3.
"a" is an unsigned, "C" is const unsigned,
gcc (SUSE Linux) 4.5.0 20100604 [gcc-4_5-branch revision 160292].
Optimization levels O1 and O0 work fine.
Note that all works fine if compiled with O2 or O3 and gcc version
gcc (SUSE Linux) 4.3.2 [gcc-4_3-branch revision 141291]
Valgrind reports no errors. Please read the few lines of code below.
Please read cout statements, and the printout. They demonstrate the
problem. t_mdb_id is unsigned (typedef).
You will see that the same logical || in cout also produces incorrect
results.
The function is in anonymous namespace.
When I copy the function to a simple
project the problem disappears.
Note that removing "inline" changes the result to the correct
value if cout statements stay as they are, but does not change the
result if complied with cout statements commented out.
inline bool is_valid( t_mdb_id a0 )
{
std::cout << " a0: " << a0 << " ~a0: " << (~a0) << " cv: " <<
exampler::CONST_VOID << std::endl;
std::cout << " l: " << (a0 < exampler::CONST_VOID) << std::endl;
std::cout << " r: " << (~a0 < exampler::CONST_VOID) << std::endl;
std::cout << " is: " << ((a0 < exampler::CONST_VOID) || (~a0 <
exampler::CONST_VOID)) << std::endl;
return a0 < exampler::CONST_VOID || ~a0 < exampler::CONST_VOID;
}
---
a0: 1 ~a0: 4294967294 cv: 101
l: 1
r: 0
is: 0
---