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

Naive C interopability question

eos_pengwern
Beginner
526 Views
Hello,

In a Fortran DLL which is called from C, I use iso_c_binding which defines the numeric kinds c_double, c_int etc.

I have been in the practice of pedantically writing 'entry routines' into the Fortran DLL, where all the incoming arguments are converted into their equivalent native Fortran types, for example:

[cpp]subroutine foo(bar) bind(c, name='foo')

    !DEC$ ATTRIBUTES DLLEXPORT :: foo

    real(c_double), intent(inout) :: bar

    real(kind(1d0)) :: bar_F

    bar_F=real(bar, kind(1d0))
    call Native_Fortran_Foo(bar_F)
    bar=real(bar_F, c_double)

end subroutine foo
    
Obviously this works well enough, but there is a time and memory overhead which grows significant when the arguments being passed are actually high-resolution images.[/cpp]
[cpp]In reality, I suspect this is wholly unnecessary: c_double is just a constant, equal to 8, so in the example above bar_F has exactly the same binary representation as bar, at least in Windows. My question is, will this always be true, for example in Linux or (whisper it softly) in other Fortran compilers? Is it legitimate to write:




[/cpp]
[cpp]    if (c_double.eq.kind(1d0)) then
       call Native_Fortran_Foo(bar)
    else
       bar_F=real(bar, kind(1d0))
       call Native_Fortran_Foo(bar_F)
       bar=real(bar_F, c_double)
    end if

[/cpp]
[cpp]...or will this turn around and bite me if I compile the code on another platform?




[/cpp]
0 Kudos
1 Solution
Steven_L_Intel1
Employee
526 Views
What you propose should be portable.

View solution in original post

0 Kudos
2 Replies
eos_pengwern
Beginner
526 Views

Aaargghh. I thought I had sussed the code insertion function, but it got me again. Sorry for the garbled format.
0 Kudos
Steven_L_Intel1
Employee
527 Views
What you propose should be portable.
0 Kudos
Reply