Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29272 Discussions

Problem passing arguments of type REAL*16 (long double) between C and Fortran

eirikf
Beginner
1,228 Views
I try to make use of Fortran to evaluate the square roots
to quad precision by calling an external function sqrtq(long double*)
from C on Itanium2 running Linux. icc is version 8.0, ifort is version 8.0.
The Fortran source for sqrtq is in sqrtq.f:
##########################
real*16 function sqrtq(x)
real*16 x
sqrtq = sqrt(x)
end function sqrtq
##########################
The C-code is compiled: icc -c -g my_source.c
The Fortran-code is compiled: ifort -c -nus sqrtq.f
The linking is performed: icc -lm -o my_exe my_source.o sqrtq.o
When running the executable my_exe, it chrashes with segmentation faultwhen calling sqrtq(&x), where x is of type long double (16 bytes).
However, the executable runs fine if the source is modified to
use REAL*8 (Fortran)and double (C) types, respectively. I would really appreciate if somebody could provide me with a clue to what I'm doing wrong.
0 Kudos
3 Replies
Steven_L_Intel1
Employee
1,228 Views
C's long double is not the same as Fortran's REAL*16.
What's the C declaration of sqrtq?
0 Kudos
eirikf
Beginner
1,228 Views
I thought that since sizeof(long double) returns 16 on
the platform (Linux on Itanium2) to run the code, the types REAL*16 and long double would match.
The source C file "my_source.c" where sqrtq is called is:
###########################################################
#include "stdio.h"

extern long double sqrtq(long double*);

int main(int argc,char* argv[])
{
long double x = 2.0L,y=0.0L;
printf("%s%.34Le%s","The value of x is: ",x,". ");
y = sqrtq(&x);
printf("%s%.34Le%s","The square root is: ",y,". ");
}
#############################################################
Then compiling on Itanium2:
> ifort -g -c -nus sqrtq.f
> icc -g -o my_exe my_source.c sqrtq.o
> ./my_exe
> The value of x is: 2.0000000000000000000000000000000000e+00.
> The square root is: 0.0000000000000000000000000000000000e+00.

a bit different behaviour than stated previously, no crash, but
sqrtq fails to assign its return value to the y-variable. Also,
using gdb and stepping through the program :

> sqrtq (sqrtq=-0, x=4.2613729662861140329509188262660732e+257) at sqrtq.f:3

when entering sqrtq(&x) in gdb. Seems like there is a problem
passing the contents of the x-variable into the Fortran function
sqrtq(x) ?
0 Kudos
Steven_L_Intel1
Employee
1,228 Views
I am not a C expert, but I think the following is true:
1. The C long double type is an 80-bit extended type that is not compatible with Fortran's REAL*16.
2. I think C is introducing a "quad" type that is REAL*16 compatible, but I don't know the status.
3. I doubt gdb knows anything about REAL*16.
0 Kudos
Reply