[Bug c/48966] New: gcc 4.5 gets wrong answer at -O2
geir at cray dot com
gcc-bugzilla@gcc.gnu.org
Wed May 11 15:28:00 GMT 2011
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48966
Summary: gcc 4.5 gets wrong answer at -O2
Product: gcc
Version: 4.5.3
Status: UNCONFIRMED
Severity: minor
Priority: P3
Component: c
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: geir@cray.com
The following test gets the wrong answer for optimization level -O2 (and -O3)
for GCC 4.5.3. The correct answer occurs when -O1 is used. The problem does
occur in GCC 4.6.0:
$ cat ptest.c
#include <stdio.h>
double d0 = 0.49999999999999999;
double d1 = 0.99320549518091861;
double d2 = 0.00674833952334470;
double d3 = 0.00679450481908144;
int main() {
double tmp;
int answer = 0;
tmp = d3 - d2/(d1-d0) - d0;
answer = (*(((int *)&tmp)+1) >> 31) & 1;
printf("answer = %d\n",answer);
return answer;
}
$ gcc --version
gcc (GCC) 4.5.3 20110428 (Cray Inc.)
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -O1 ptest.c && ./a.out
answer = 1
$ gcc -O2 ptest.c && ./a.out
answer = 0 <----------- Wrong answer!!!
$ module swap gcc/4.5.3 gcc/4.6.0
$ gcc --version
gcc (GCC) 4.6.0 20110325 (Cray Inc.)
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
geir@kaibab:/lus/scratch/geir/ISU/ISU3475> gcc -O2 ptest.c && ./a.out
answer = 1
$
The processor being used is:
$ grep "model name" /proc/cpuinfo
model name : 6-Core AMD Opteron(tm) Processor 23 (D0)
model name : 6-Core AMD Opteron(tm) Processor 23 (D0)
model name : 6-Core AMD Opteron(tm) Processor 23 (D0)
model name : 6-Core AMD Opteron(tm) Processor 23 (D0)
model name : 6-Core AMD Opteron(tm) Processor 23 (D0)
model name : 6-Core AMD Opteron(tm) Processor 23 (D0)
$
More information about the Gcc-bugs
mailing list