Bug 251096

Summary: Difference in behaviour with compiler-optimised code (-O2)
Product: [Fedora] Fedora Reporter: David Campbell <david>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact:
Severity: low Docs Contact:
Priority: low    
Version: 7   
Target Milestone: ---   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2007-08-07 06:56:59 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
bug.c to reproduce the problem none

Description David Campbell 2007-08-07 03:56:14 UTC
Description of problem:

This issue arose from tracing a problem with an infinite loop in the startup of
f7 build of xsane, the scanner software, but I've boiled the issue down to a
tiny C program that can be compiled with or without optimisation and in each of
those cases you can see that the generated executable gives a different result.

Version-Release number of selected component (if applicable):

Vigor12 2.6.22.1-41.fc7
gcc version 4.1.2 20070502 (Red Hat 4.1.2-12)

How reproducible:

always

Steps to Reproduce:
1. Save the attached bug.c C program
2. Compile:  gcc -o bug bug.c
3. Run: ./bug
4. Observe result 39019943
5. Compile:  gcc -O2 -o buggo bug.c
6. Run:  ./buggo
7. Observe result:  39019942

Also note that if you uncomment the commented printf statements in the attached
C source, you get a different result!!!!  Something is rather weird.

Actual results:

The two results were different

Expected results:

The two results should be the same

Comment 1 David Campbell 2007-08-07 03:56:15 UTC
Created attachment 160796 [details]
bug.c to reproduce the problem

Comment 2 David Campbell 2007-08-07 04:03:47 UTC
This difference in behaviour is also visible in gcc 2.96 on an old linux.


Comment 3 Jakub Jelinek 2007-08-07 06:56:59 UTC
That's caused by excess precision, not a bug, but a thing you need to take
into account when programming i?87.
See e.g. info gcc --index-search=ffloat-store
or e.g.
http://www.network-theory.co.uk/docs/gccintro/gccintro_70.html
If this is undesirable in your program, you can either use
-ffloat-store option which will slow down your program, but ensure every
assignment to a double (or float) variable will do the rounding to that
precision, or if you have sufficiently new CPU, compile with
-msse2 -mfpmath=sse
or if you have 64-bit x86_64 CPU, compile 64-bit.