As I understand the gfortran docs, the -ff2c/-fno-f2c switches change how
functions returning complex numbers are implemented:

-ff2c: the return value is returned in an additional argument as g77 and other
fortran compilers do it (can be seen e. g. in
http://www.pgroup.com/doc/pvfug.pdf, chapter 11 and
http://www.intel.com/support/performancetools/libraries/mkl/sb/cs-017175.htm)

-fno-f2c (which is the default): the return value is returned in a C compatible
manner without an additional argument.

The latter does not work for me. To reproduce, compile the snippet


      complex function fcomplex_func(i)
        implicit none
        integer i
        fcomplex_func = cmplx(i, 2*i)
      end
      double complex function dcomplex_func(i)
        implicit none
        integer i
        dcomplex_func = dcmplx(i, 2*i)
      end


with gfortran and the snippet


#include <stdio.h>

typedef struct { float r; float i; } fcomplex;
typedef struct { double r; double i; } dcomplex;
#ifdef FF2C
extern fcomplex fcomplex_func__(int const *);
extern dcomplex dcomplex_func__(int const *);
#else
extern void fcomplex_func_(fcomplex *, int const *);
extern void dcomplex_func_(dcomplex *, int const *);
#endif

int main(){
        int const i = 12;
        int const j = 13;
        fcomplex fc = {7,8};
        dcomplex dc = {9,10};
#ifdef FF2C
        printf("c return %d %d\n", i, j);
        fc = fcomplex_func__(&i);
        dc = dcomplex_func__(&j);
#else
        printf("nonc return %d %d\n", i, j);
        fcomplex_func_(&fc, &i);
        dcomplex_func_(&dc, &j);
#endif
        printf("fcomplex_func: %d %g %g\n", i, fc.r, fc.i);
        printf("dcomplex_func: %d %lg %lg\n", j, dc.r, dc.i);
        return 0;
}


with gcc and link both together. The expected output of the program is

nonc return 12 13
fcomplex_func: 12 12 24
dcomplex_func: 13 13 26

but here it goes into an endless loop and prints

nonc return 12 13
fcomplex_func: 12 7 8
dcomplex_func: 13 13 26

repeatedly until manual abort. Note that the double complex result is correct,
but nevertheless the call of dcomplex_func does something bad (stack
corruption?). The endless loop persists if you comment out the call of
fcomplex_func.

If you use the -ff2c switch of gfortran, and define the FF2C preprocessor
variable for gcc everything works well, and the output is as expected

nonc return 12 13
fcomplex_func: 12 12 24
dcomplex_func: 13 13 26

. I tried 4.2.4 svn from today, and the debian packages 4.2.3-2 and 4.3.0-1,
and all versions show this problem.


-- 
           Summary: return type of complex functions not C compatible
           Product: gcc
           Version: 4.2.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Georg dot Baum at post dot rwth-aachen dot de
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35765

Reply via email to