Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Important Update: Community Platform Migration​. Learn more​>
29643 Discussions

Fortran function return value for C++ caller?

AONym
New Contributor II
1,314 Views
A Fortran function, REDUCE, is called from C++. The return value of the Fortran function is a user-defined type, Rational.

In CVF6.6, the code shown below works correctly. In IF9.1, there is a link error, because the C++ function prototype for Reduce has 2 args, and the compiler generates code for 1. In CVF6.6, the extra "argument" was a pointer to the memory where the function's return value was to be stored.

How can I store the Fortran function's return value in my C++ caller?

The Fortran module:

MODULE RationalArithmetic
IMPLICIT NONE
PRIVATE
PUBLIC :: rational

TYPE rational
PRIVATE
INTEGER :: num,denom
END TYPE rational

CONTAINS

TYPE(rational) ELEMENTAL FUNCTION Reduce(rat)
TYPE(rational), INTENT(IN) :: rat
[code which sets Reduce%num and Reduce%denom]
END FUNCTION Reduce

END MODULE RationalArithmetic

The C++ caller:

extern "C" void __stdcall RATIONALARITHMETIC_mp_REDUCE(Rational *rat1, Rational *rat);
RATIONALARITHMETIC_mp_REDUCE(&s/*Fortran function return*/, &s);

In IF9.1.025, the the compiler generates code that includes

_TEXT SEGMENT DWORD PUBLIC FLAT 'CODE'
; -- Begin _RATIONALARITHMETIC_mp_REDUCE@4
; mark_begin;
ALIGN 2
PUBLIC _RATIONALARITHMETIC_mp_REDUCE@4
_RATIONALARITHMETIC_mp_REDUCE@4 PROC NEAR
PUBLIC _RATIONALARITHMETIC_mp_REDUCE
_RATIONALARITHMETIC_mp_REDUCE::
; parameter 1(REDUCE): 8 + ebp
; parameter 2(RAT): 12 + ebp
$B22$1: ; Preds $B22$0

;;; TYPE(rational) ELEMENTAL FUNCTION Reduce(rat)
0 Kudos
2 Replies
Steven_L_Intel1
Employee
1,314 Views
I can confirm that this is a difference, albeit one I was not aware of before. It is also documented, though in a place you'd never think to look for it (in the section on mixing MASM and Fortran). It also matches the C behavior with structs. User defined types of 8 bytes or less are returned in the EAX or EAX:EDX registers.

I'll ask that the documentation in this area be enhanced.
0 Kudos
Steven_L_Intel1
Employee
1,314 Views
You can adjust the code in either the Fortran or C++ side to account for this difference. On the Fortran side, make it a subroutine with an extra first argument, and on the C++ side, make it a function returning a struct.
0 Kudos
Reply