Calling Fortran-functions returning complex numbers from C

Hi Christer,

|> I have a problem with calling complex Fortran functions from C that I do

|> not know how to solve. Currently I am working on an interface enabling

|> calls from C to the Fortran BLAS and LAPACK-libraries (linear algebra routines).

|> Everything works great except for a few functions returning complex numbers.

|> Functions returning real numbers and integers do work. Complex numbers

|> have been used as both input and output to subroutines with correct results.

|>

|> The complex functions work properly on Sun stations so this is probably

|> a specific problem with AIX (run on an IBM Power Parallel SP). Does anyone

|> know how to overcome these problems? I would be grateful for any kind of

|> information. I have used the following sources of information without any

|> success,

I suspect that the problem lies in the way that XL Fortran returns the

results of functions of type complex; the value is returned in FPR1 (real

part) and FPR2 (imaginary part) in the case of COMPLEX(4) and COMPLEX(8),

and in FPR1-FPR4 (FPR1 and FPR2 contain the real part and FPR3 and FPR4

contain the imaginary part) in the case of COMPLEX(16).

Functions of type real work because the value is returned in FPR1, and

the same is the case for C functions of type float and double. Functions

of type integer work because the value is returned in GPR3 (or GPR3 and GPR4

in the case of INTEGER(8)) and the same is the case for C functions of type

int, et al. However, there's no data type in C that will let you get at

anything beyond FPR1.

The calling conventions on Sun must be different, and permit the

complex case to go through.

One way around the problem might be to write a Fortran wrapper for

the functions of type complex. For example, if you're using a struct with

two real components as the return type of the function in C, your Fortran

wrapper would be a function of derived type declared in a similar fashion.

The Fortran wrapper could then call the BLAS or LAPACK routine and store

the result back into the derived type result. The calling conventions for

C functions returning structs are similar to those for Fortran functions of

derived type on AIX, so this approach should work. I don't know Sun's

calling conventions, so I don't know whether that approach would be

portable or only AIX-specific.

By the way, the section of the User's Guide to check would have been

the one entitled "Interlanguage Calls". It gets into all of the nitty

gritty details about how arguments are passed and how results are returned.

Please let me know if any of this isn't clear or if you encounter any

problems.

Thanks,

Henry

-------------------------------------------------------------------------------

Henry Zongaro XL Fortran/XL HPF Compiler Development

IBM SWS Toronto Lab Tie Line 778-6044 Phone (416) 448-6044