This is the mail archive of the gcc-prs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

optimization/323: optimized code gives strange floating point results



>Number:         323
>Category:       optimization
>Synopsis:       optimized code gives strange floating point results
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Wed Jun 14 14:16:01 PDT 2000
>Closed-Date:
>Last-Modified:
>Originator:     Brian Mirtich
>Release:        gcc version 2.95.2 19991024 (release)
>Organization:
>Environment:
(Red Hat) Linux 2.2.12-20, i686 pc
>Description:
A simple program gives incorrect floating point results
when compiled with the -O flag.  The complete source code
is in the How-To-Repeat section below.

This program prints "error" when compiled with -O; clearly 
wrong.  The unoptimized code correctly prints no output.
Also, if I try to print out the value of y2 before the
comparison with y, then the correct value is output, 
and there is no "error" message, even when the code is
optimized.  In other words, interposing a print statement
between the declaration (and initialization) of y2 and the
comparison changes the program behavior.  The error
disappears for certain different values of the numerical
constants.  The error also goes away when an "l" suffix is 
appended to the two 1.0 constants.  However, it is also
possible to create the same type of error using only
variables and no numerical constants in the assignments to
y and y2; in this situation there is no "l" suffix 
workaround.

The gzipped .ii file is attached. 
Here is the output of gcc:

> /usr/local/bin/gcc -v --save-temps -O bug.C
Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/specs
gcc version 2.95.2 19991024 (release)
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/cpp -lang-c++ -v -D__GNUC__=2 -D__GNUG__=2 -D__GNUC_MINOR__=95 -D__cplusplus -D__ELF__ -Dunix -D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS -D__OPTIMIZE__ -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__ -Di686 -Dpentiumpro -D__i686 -D__i686__ -D__pentiumpro -D__pentiumpro__ bug.C bug.ii
GNU CPP version 2.95.2 19991024 (release) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3
 /usr/local/include
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../i686-pc-linux-gnu/include
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/include
 /usr/include
End of search list.
The following default directories have been omitted from the search path:
End of omitted list.
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/cc1plus bug.ii -quiet -dumpbase bug.cc -O -version -o bug.s
GNU C++ version 2.95.2 19991024 (release) (i686-pc-linux-gnu) compiled by GNU C version 2.95.2 19991024 (release).
 as -V -Qy -o bug.o bug.s
GNU assembler version 2.9.1 (i386-redhat-linux), using BFD version 2.9.1.0.24
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/collect2 -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/crtbegin.o -L/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2 -L/usr/local/lib bug.o -lgcc -lc -lgcc /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/crtend.o /usr/lib/crtn.o

>How-To-Repeat:
#include <stdio.h>

void test(double x, double y)
{
  const double y2 = x + 1.0;
  if (y != y2) printf("error\n");
}

void main()
{
  const double x = .012;
  const double y = x + 1.0;

  test(x, y);
}

>Fix:
This bug is elusive and only occurs for certain floating
point values.  It goes away by tweaking the program in
various ways (e.g. printing out the value of y2 before
comparing it) or by compiling unoptimized. 
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="bug.ii.gz"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="bug.ii.gz"

H4sICDLyRzkCA2J1Z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KTI9pZehYr4Njwq6ZvaFSbzvrkP6svY+sDlpQhSOq5tR+0q
Af6JgiEUPRfcfV1Z3Jfdf4/utBjWuDGhNvbaWyZ2UbbbhenQIZha/wYNSQ3dRYTPu2TTM+kVb8TM
3UM3fo0t77N8/9abjmQrq5dpA7stzlK5vRs2wxUvDlrvLQbeWYCX0LfUCXslwRPTFedpbccuuowH
2UZUj74gk5OyojqKidGE32fMWiIUaUXvfVQxxMGnPoUYqTa3yBN2ecLJmAi1jj+Rr2KNRCcTKtse
ph9+kl/egZ8+gWGFYM36XXOFLgDgt3A2Uf85x9kUzITfv5+CXX9JcochUGZmAbPQ3SVd6hrCJraR
diO3Lrk4YbVnE+vmsXPchPWw3C/MW6idv+yNr8U4E2hKu9CieWGKm8iyjnaaMkM17zzIoDNQjcV3
OIIpNWRn6cwlxfv38KnBbuxqMOz/nRWMvtW1gktr7lo2nU2rRo1rwPH6Wvhb9tyWl9WmZzJ40xVr
s1cV4Tcwf17MMd5//QnmRjvrndJ3s/M1dtJbuJe3zgJW/3Pcq4+9UIzc1XLRc+/+Cu2FP//PPcDH
z6wMl1jjJrhbayzDzzOon16mTEteDsV3Pu7eM7yHxfmcvY2Ite2FavjiT+srnckJs+Jf0xOe1hiH
XZikEwO9ZyR3Pl/4qw4jmQ8uMhFRthdO9B8shVufpEQAAA==

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]