Bug 47282

Summary: GCC 4.2.4 (Ubuntu) gives different result for the same computation with -O3, depending on irrelevant code
Product: gcc Reporter: Igor Chudov <ichudov>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal    
Priority: P3    
Version: 4.2.4   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Attachments: tstdouble.cpp -- compile with gcc-4.2 -O3 -o tstdouble tstdouble.cpp

Description Igor Chudov 2011-01-13 21:52:29 UTC
Created attachment 22961 [details]
tstdouble.cpp -- compile with gcc-4.2 -O3 -o tstdouble tstdouble.cpp

The attached C++ code depends only on stdio.h and string.h.

It calls the same function (DoubleToInt) twice: once from main( ... ) and once from Test( ... ). 

Both main() and Test() call DoubleToInt with exact same values of v and factor. 

DoubleToInt include some junk code surrounded by "if( False() ) { ... }". That junk code, though it never executes, has the effect of altering the result of calling DoubleToInt, so the second call's result does not match the first call's result. 

Even taking out a couple of lines from the big condition of if statement inside if( False() ) { ... }, causes the values to become correct.

Similarly, taking out printf in DoubleToInt, also causes the printed values to be correct (identical). 

So some flakiness exists in computations, depending on irrelevant code that never executes. We tried very hard to distill it into smallest possible code. 

This seems to NOT occur in GCC 4.3 and GCC 4.4. It only occurs with -O3.

To compile: 

   gcc-4.2 -O3 -o tstdouble tstdouble.cpp
Comment 1 Drea Pinski 2011-01-13 21:55:05 UTC
What target are you doing your tests on?
Comment 2 Igor Chudov 2011-01-13 22:00:31 UTC
(In reply to comment #1)
> What target are you doing your tests on?

This is a 32 bit Ubuntu machine, so target -s -m32.

Thanks!

Igor
Comment 3 Igor Chudov 2011-01-13 22:01:12 UTC
(In reply to comment #2)
> (In reply to comment #1)
> > What target are you doing your tests on?
> 
> This is a 32 bit Ubuntu machine, so target -s -m32.
> 
> Thanks!
> 
> Igor

It is a 32 bit i386 machine.
Comment 4 Drea Pinski 2011-01-13 22:06:14 UTC
The problem is excessive percussion.

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