Bug 251096 - Difference in behaviour with compiler-optimised code (-O2)
Summary: Difference in behaviour with compiler-optimised code (-O2)
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Fedora
Classification: Fedora
Component: gcc
Version: 7
Hardware: All
OS: Linux
low
low
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact:
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2007-08-07 03:56 UTC by David Campbell
Modified: 2007-11-30 22:12 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2007-08-07 06:56:59 UTC
Type: ---
Embargoed:


Attachments (Terms of Use)
bug.c to reproduce the problem (497 bytes, text/x-csrc)
2007-08-07 03:56 UTC, David Campbell
no flags Details

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.


Note You need to log in before you can comment on or make changes to this bug.