Bug 15397 - [3.4 only] [g77] c float function called from fortran gives wrong result
Summary: [3.4 only] [g77] c float function called from fortran gives wrong result
Status: RESOLVED WONTFIX
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 3.4.0
: P1 normal
Target Milestone: 3.4.6
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2004-05-12 14:56 UTC by Harald Vogt
Modified: 2014-02-16 13:13 UTC (History)
5 users (show)

See Also:
Host:
Target: x86_64-linux-gnu
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Harald Vogt 2004-05-12 14:56:07 UTC
This bug is specific for the x86_64 architecture. On 32 bit architectures it
does not occur.


[lx64 16:27] ~/test/cernlib/x86_64 > gcc -v -save-temps -O test1.f funct2.c
-lg2c -lfrtbegin
Reading specs from
/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/specs
Configured with: /usr/src/packages/BUILD/gcc-3.4.0/configure 
--prefix=/opt/products/gcc/3.4.0 --enable-shared --enable-cpp
--enable-threads=posix --enable-languages=c,c++,f77,java
--with-local-prefix=/opt/products/gcc/3.4.0
--with-gxx-include-dir=/opt/products/gcc/3.4.0/include/g++-3
Thread model: posix
gcc version 3.4.0
 /afs/ifh.de/@sys/products/gcc/3.4.0/bin/../libexec/gcc/x86_64-unknown-linux-gnu/3.4.0/f771
test1.f -quiet -dumpbase test1.f -mtune=k8 -auxbase test1 -O -version -o test1.s
GNU F77 version 3.4.0 (x86_64-unknown-linux-gnu)
        compiled by GNU C version 3.4.0.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
 as -V -Qy -o test1.o test1.s
GNU assembler version 2.14.90.0.5 (x86_64-suse-linux) using BFD version
2.14.90.0.5 20030722 (SuSE Linux)
 /afs/ifh.de/@sys/products/gcc/3.4.0/bin/../libexec/gcc/x86_64-unknown-linux-gnu/3.4.0/cc1
-E -quiet -v -iprefix
/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/
funct2.c -mtune=k8 -O -o funct2.i
ignoring nonexistent directory
"/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/../../../../x86_64-unknown-linux-gnu/include"
ignoring duplicate directory
"/opt/products/gcc/3.4.0/lib/gcc/x86_64-unknown-linux-gnu/3.4.0/include"
ignoring nonexistent directory
"/opt/products/gcc/3.4.0/lib/gcc/x86_64-unknown-linux-gnu/3.4.0/../../../../x86_64-unknown-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/include
 /opt/products/gcc/3.4.0/include
 /usr/include
End of search list.
 /afs/ifh.de/@sys/products/gcc/3.4.0/bin/../libexec/gcc/x86_64-unknown-linux-gnu/3.4.0/cc1
-fpreprocessed funct2.i -quiet -dumpbase funct2.c -mtune=k8 -auxbase funct2 -O
-version -o funct2.s
GNU C version 3.4.0 (x86_64-unknown-linux-gnu)
        compiled by GNU C version 3.4.0.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
 as -V -Qy -o funct2.o funct2.s
GNU assembler version 2.14.90.0.5 (x86_64-suse-linux) using BFD version
2.14.90.0.5 20030722 (SuSE Linux)
 /afs/ifh.de/@sys/products/gcc/3.4.0/bin/../libexec/gcc/x86_64-unknown-linux-gnu/3.4.0/collect2
--eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2
/usr/lib/../lib64/crt1.o /usr/lib/../lib64/crti.o
/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/crtbegin.o
-L/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0
-L/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc
-L/opt/products/gcc/3.4.0/lib/gcc/x86_64-unknown-linux-gnu/3.4.0
-L/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/../../../../lib64
-L/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/../../..
-L/opt/products/gcc/3.4.0/lib/gcc/x86_64-unknown-linux-gnu/3.4.0/../../../../lib64
-L/opt/products/gcc/3.4.0/lib/gcc/x86_64-unknown-linux-gnu/3.4.0/../../..
-L/lib/../lib64 -L/usr/lib/../lib64 test1.o funct2.o -lg2c -lfrtbegin -lgcc
-lgcc_eh -lc -lgcc -lgcc_eh
/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/crtend.o
/usr/lib/../lib64/crtn.o

example code

test1.f:

      program test
*
*     error connected with return value from c routines
*
      implicit none
*
      real funct1, funct2, retval
      integer funct3, iretval

*     for calling a fortran function
      retval  = funct1 (1.1111)
      print *, 'in test1: retval from fortran call is ', retval
      retval  = funct2 (1.1111)
      print *, 'in test1: retval from c(float) call is ', retval
      iretval = funct3 (2)
      print *, 'in test1: retval from c(int)   call is ', iretval
      stop

      end

      real function funct1 ( inval )

      implicit none

      real inval, retval

      retval = inval
      print *, 'funct1: return value = ', retval
      funct1 = retval

      end

funct2.c:

float funct2_( float* value ) {
  float retval = *value ;
  printf ("funct2: return value = %f\n", retval);
  return retval ;
}
int funct3_( int* value ) {
  int retval = *value ;
  printf ("funct2: return value = %d\n", retval);
  return retval ;
}

Job output:
[lx64 16:45] ~/test/cernlib/x86_64 > ./a.out
 funct1: return value =   1.11109996
 in test1: retval from fortran call is   1.11109996
funct2: return value = 1.111100
 in test1: retval from c(float) call is   0.
funct2: return value = 2
 in test1: retval from c(int)   call is  2
Comment 1 Toon Moene 2004-05-29 13:36:22 UTC
This is a g77 problem.
Comment 2 Andrew Pinski 2004-09-18 00:10:04 UTC
*** Bug 17541 has been marked as a duplicate of this bug. ***
Comment 3 Giovanni Bajo 2004-10-07 09:34:35 UTC
Toon, can you confirm this bug as actual on the 3.4 branch? Is this a 
regression? Can this be double-checked with gfortran on mainline?
Comment 4 Thomas Koenig 2004-11-17 09:18:53 UTC
(In reply to comment #3)

I checked this with gcc-4.0-20041107 on a ia64, and the problem didn't occur:

$ gfortran -v
Reading specs from /home/zfkts/lib/gcc/ia64-unknown-linux-gnu/4.0.0/specs
Configured with: ../gcc-4.0-20041107/configure --prefix=/home/zfkts
--enable-languages=c,c++,f95
Thread model: posix
gcc version 4.0.0 20041107 (experimental)
$ gfortran -O test1.f funct2.c
funct2.c: In function 'funct2_':
funct2.c:3: warning: incompatible implicit declaration of built-in function 'printf'
funct2.c: In function 'funct3_':
funct2.c:8: warning: incompatible implicit declaration of built-in function 'printf'
$ ./a.out
 funct1: return value =    1.111100
 in test1: retval from fortran call is    1.111100
funct2: return value = 1.111100
 in test1: retval from c(float) call is    1.111100
funct2: return value = 2
 in test1: retval from c(int)   call is            2
STOP 0
Comment 5 Harald Vogt 2004-11-23 08:53:53 UTC
(In reply to comment #0)
> This bug is specific for the x86_64 architecture. On 32 bit architectures it
> does not occur.
> 
> 
> [lx64 16:27] ~/test/cernlib/x86_64 > gcc -v -save-temps -O test1.f funct2.c
> -lg2c -lfrtbegin
> Reading specs from
>
/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/specs
> Configured with: /usr/src/packages/BUILD/gcc-3.4.0/configure 
> --prefix=/opt/products/gcc/3.4.0 --enable-shared --enable-cpp
> --enable-threads=posix --enable-languages=c,c++,f77,java
> --with-local-prefix=/opt/products/gcc/3.4.0
> --with-gxx-include-dir=/opt/products/gcc/3.4.0/include/g++-3
> Thread model: posix
> gcc version 3.4.0
> 
/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../libexec/gcc/x86_64-unknown-linux-gnu/3.4.0/f771
> test1.f -quiet -dumpbase test1.f -mtune=k8 -auxbase test1 -O -version -o test1.s
> GNU F77 version 3.4.0 (x86_64-unknown-linux-gnu)
>         compiled by GNU C version 3.4.0.
> GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
>  as -V -Qy -o test1.o test1.s
> GNU assembler version 2.14.90.0.5 (x86_64-suse-linux) using BFD version
> 2.14.90.0.5 20030722 (SuSE Linux)
> 
/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../libexec/gcc/x86_64-unknown-linux-gnu/3.4.0/cc1
> -E -quiet -v -iprefix
> /afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/
> funct2.c -mtune=k8 -O -o funct2.i
> ignoring nonexistent directory
>
"/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/../../../../x86_64-unknown-linux-gnu/include"
> ignoring duplicate directory
> "/opt/products/gcc/3.4.0/lib/gcc/x86_64-unknown-linux-gnu/3.4.0/include"
> ignoring nonexistent directory
>
"/opt/products/gcc/3.4.0/lib/gcc/x86_64-unknown-linux-gnu/3.4.0/../../../../x86_64-unknown-linux-gnu/include"
> #include "..." search starts here:
> #include <...> search starts here:
> 
/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/include
>  /opt/products/gcc/3.4.0/include
>  /usr/include
> End of search list.
> 
/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../libexec/gcc/x86_64-unknown-linux-gnu/3.4.0/cc1
> -fpreprocessed funct2.i -quiet -dumpbase funct2.c -mtune=k8 -auxbase funct2 -O
> -version -o funct2.s
> GNU C version 3.4.0 (x86_64-unknown-linux-gnu)
>         compiled by GNU C version 3.4.0.
> GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
>  as -V -Qy -o funct2.o funct2.s
> GNU assembler version 2.14.90.0.5 (x86_64-suse-linux) using BFD version
> 2.14.90.0.5 20030722 (SuSE Linux)
> 
/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../libexec/gcc/x86_64-unknown-linux-gnu/3.4.0/collect2
> --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2
> /usr/lib/../lib64/crt1.o /usr/lib/../lib64/crti.o
>
/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/crtbegin.o
>
-L/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0
> -L/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc
> -L/opt/products/gcc/3.4.0/lib/gcc/x86_64-unknown-linux-gnu/3.4.0
>
-L/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/../../../../lib64
>
-L/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/../../..
> -L/opt/products/gcc/3.4.0/lib/gcc/x86_64-unknown-linux-gnu/3.4.0/../../../../lib64
> -L/opt/products/gcc/3.4.0/lib/gcc/x86_64-unknown-linux-gnu/3.4.0/../../..
> -L/lib/../lib64 -L/usr/lib/../lib64 test1.o funct2.o -lg2c -lfrtbegin -lgcc
> -lgcc_eh -lc -lgcc -lgcc_eh
>
/afs/ifh.de/@sys/products/gcc/3.4.0/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.0/crtend.o
> /usr/lib/../lib64/crtn.o
> 
> example code
> 
> test1.f:
> 
>       program test
> *
> *     error connected with return value from c routines
> *
>       implicit none
> *
>       real funct1, funct2, retval
>       integer funct3, iretval
> 
> *     for calling a fortran function
>       retval  = funct1 (1.1111)
>       print *, 'in test1: retval from fortran call is ', retval
>       retval  = funct2 (1.1111)
>       print *, 'in test1: retval from c(float) call is ', retval
>       iretval = funct3 (2)
>       print *, 'in test1: retval from c(int)   call is ', iretval
>       stop
> 
>       end
> 
>       real function funct1 ( inval )
> 
>       implicit none
> 
>       real inval, retval
> 
>       retval = inval
>       print *, 'funct1: return value = ', retval
>       funct1 = retval
> 
>       end
> 
> funct2.c:
> 
> float funct2_( float* value ) {
>   float retval = *value ;
>   printf ("funct2: return value = %f\n", retval);
>   return retval ;
> }
> int funct3_( int* value ) {
>   int retval = *value ;
>   printf ("funct2: return value = %d\n", retval);
>   return retval ;
> }
> 
> Job output:
> [lx64 16:45] ~/test/cernlib/x86_64 > ./a.out
>  funct1: return value =   1.11109996
>  in test1: retval from fortran call is   1.11109996
> funct2: return value = 1.111100
>  in test1: retval from c(float) call is   0.
> funct2: return value = 2
>  in test1: retval from c(int)   call is  2

(In reply to comment #4)
> (In reply to comment #3)
> 
> I checked this with gcc-4.0-20041107 on a ia64, and the problem didn't occur:
> 
> $ gfortran -v
> Reading specs from /home/zfkts/lib/gcc/ia64-unknown-linux-gnu/4.0.0/specs
> Configured with: ../gcc-4.0-20041107/configure --prefix=/home/zfkts
> --enable-languages=c,c++,f95
> Thread model: posix
> gcc version 4.0.0 20041107 (experimental)
> $ gfortran -O test1.f funct2.c
> funct2.c: In function 'funct2_':
> funct2.c:3: warning: incompatible implicit declaration of built-in function
'printf'
> funct2.c: In function 'funct3_':
> funct2.c:8: warning: incompatible implicit declaration of built-in function
'printf'
> $ ./a.out
>  funct1: return value =    1.111100
>  in test1: retval from fortran call is    1.111100
> funct2: return value = 1.111100
>  in test1: retval from c(float) call is    1.111100
> funct2: return value = 2
>  in test1: retval from c(int)   call is            2
> STOP 0

May be the following may give a hint to the g77 developers:
if using the -fno-f2c compile option the error does not occur on AMD64
architectures.
Comment 6 Andrew Pinski 2005-06-12 03:23:19 UTC
(In reply to comment #5)
> May be the following may give a hint to the g77 developers:
> if using the -fno-f2c compile option the error does not occur on AMD64
> architectures.

That means the ABI is different with those two.

I don't think this is a bug.
Comment 7 Michael Meissner 2005-07-06 19:22:02 UTC
FWIW, I tried this with the 4.0 compiler on an AMD64 system, and it doesn't fail
with gfortran.

I did bring over the 3.4.4 release, and tried it with g77, and it failed as
reported.  It passed if I used -m32 with the traditional x86 calling sequence.

If I link the 3.4.4 fortran object with the 4.0.0 C object, it fails.  If I link
the 4.0.0 fortran with the 3.4.4 C object, it passes.

Looking at the code for -O1, between 3.4.4 and 4.0.0, I see, the 4.0.0 generates:
	call	funct2_
	movss	%xmm0, 12(%rsp)

and the 3.4.4 fortran generates:
	call	funct2_
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 16(%rsp)

Ie, g77 is expecting float values to be returned as a double, and then doing the
conversion.
Comment 8 Harald Vogt 2005-09-08 15:34:32 UTC
(In reply to comment #7)

gfortran has changed the default!
Looking in the gfortran manual the -fno-f2c is now the default and -ff2c will
switch back to the g77 default. Therefore gfortran gives a correct result. Can
you check the code with "gfortran -ff2c"?
Comment 9 Kevin B. McCarty 2005-09-22 14:17:04 UTC
Could anyone find out whether this g77 bug applies to other 64-bit Linux
architectures as well as AMD64?  I'm thinking of Alpha, Itanium, S390x, and
64-bit PowerPC and SPARC.  Thanks in advance for any such information,
especially on the first two of these.
Comment 10 Gabriel Dos Reis 2006-02-28 09:23:30 UTC
won't fix for 3.4.6.
Comment 11 Jackie Rosen 2014-02-16 13:13:21 UTC Comment hidden (spam)