Bug 59168

Summary: Wrong result with non-optimized (!) compilation
Product: gcc Reporter: Sarantis Pantazis <sarantis.pantazis>
Component: fortranAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: janus
Priority: P3    
Version: 4.8.2   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Attachments: Bug code

Description Sarantis Pantazis 2013-11-18 10:13:37 UTC
I wanted to check the difference in CPU time in the calculation of a sum when the terms are included in the same expression and when they are added separately. For this reason I wrote the attached code.

First I compiled with
"gfortran -o test testPartialSum.f08"
and got the following results:

   199204048.    
   67108864.0    
  0.117000006      0.244000003

Then I compiled with 
"gfortran -o test testPartialSum.f08 -O3"
and got the following results:

   200000000.    
   200000000.    
   7.80000016E-02   7.50000030E-02

It seems clear that the optimized compilation provides the correct results but the non-optimized one does not (which seems very weird to me!!!).

>uname -a
Linux i75454 3.7.10-1.16-desktop #1 SMP PREEMPT Fri May 31 20:21:23 UTC 2013 (97c14ba) i686 i686 i386 GNU/Linux

>cat /etc/issue
Welcome to openSUSE 12.3 "Dartmouth" - Kernel \r (\l).

>gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/home/myUserame/gcc-latest/gcc-4.8.2/lib/gcc/i686-pc-linux-gnu/4.8.2/lto-wrapper
Target: i686-pc-linux-gnu
Configured with: ../configure --prefix=/home/myUsername/gcc-latest/gcc-4.8.2 --enable-languages=c,fortran,c++
Thread model: posix
gcc version 4.8.2 (GCC)
Comment 1 Sarantis Pantazis 2013-11-18 10:14:44 UTC
Created attachment 31234 [details]
Bug code
Comment 2 janus 2013-11-18 13:01:06 UTC
Sorry, I can not reproduce this. I have tried with gfortran 4.7, 4.8 and trunk using -O0 up to -O3. The result is always:

   67108864.0    
   67108864.0

It seems that you have built gfortran yourself, right? Have you tried running the testsuite ('make check-gfortran')?


$ gfortran-4.8 -v
Using built-in specs.
COLLECT_GCC=gfortran-4.8
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.8.1-10ubuntu8' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu8)


$ gfortran-4.9 -v
Using built-in specs.
COLLECT_GCC=gfortran-4.9
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /home/jweil/gcc49/trunk/configure --program-suffix=-4.9 --enable-checking=release --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --enable-languages=c,c++,fortran --disable-bootstrap --disable-multilib
Thread model: posix
gcc version 4.9.0 20131117 (experimental) [trunk revision 204922] (GCC)
Comment 3 janus 2013-11-18 13:13:22 UTC
(In reply to janus from comment #2)
> Sorry, I can not reproduce this. I have tried with gfortran 4.7, 4.8 and
> trunk using -O0 up to -O3. The result is always:
> 
>    67108864.0    
>    67108864.0

... which of course is not the result you expect.

However, the solution is simple: Use -fdefault-real-8 or decleare your variables explicitly as real(8). Single precision, i.e. real(4), does not provide sufficient precision to represent the final value.

Closing as invalid.
Comment 4 Dominique d'Humieres 2013-11-18 18:46:20 UTC
> Sorry, I can not reproduce this. I have tried with gfortran 4.7, 4.8 
> and trunk using -O0 up to -O3. The result is always:
>
>   67108864.0    
>   67108864.0

Confirmed for 64 bit mode, but I get the results of comment #0 for 32 bit mode.
This is a duplicate of pr323, i.e., I get 67108864.0 if I compile the test with '-m32 -ffloat-store'.

*** This bug has been marked as a duplicate of bug 323 ***