The program below is miscompiled using
gfortran -O2 -o m m.f90; ./m
gives:
y= 0.60653065945526063 2*y= 2.0000000000000000
(the second of the printed numbers should equal twice the first). Using
gfortran -fno-inline -O2 -o m m.f90
works OK.
The compiler is:
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: /home/jerry/gcc/trunk/configure --prefix=/usr/local/gfortran
--enable-languages=c,fortran --disable-libmudflap --enable-libgomp
--disable-shared
Thread model: posix
gcc version 4.5.0 20090831 (experimental) [trunk revision 151238] (GCC)
program m
double precision :: y,z
call b(1.0d0,y,z)
print*,'y= ', y, ' 2*y=', z
contains
subroutine b( x, y, z)
implicit none
double precision :: x,y,z
integer :: i, k
double precision :: h, r
y = 1.0d0
z = 0.0d0
h = 0
DO k = 1,10
h = h + 1.0d0/k
r = 1
DO i = 1,k
r = (x/(2*i) ) * r
END DO
y = y + (-1)**k * r
z = z + (-1)**(k+1) * h * r
IF ( ABS(2*k/x*r) < 1d-6 ) EXIT
END DO
z = 2*y
end subroutine b
end program m
--
Summary: miscompilation at -O2
Product: gcc
Version: 4.5.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: jpr at csc dot fi
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41212