Return complex from C to Fortran

2014年10月5日

C fortran MacPorts コンピュータ

t f B! P L
I wrote a wrapper in Fortran called dotwrp to avoid the problems in Accelerate (more specifically vecLib) framework (related post in Japanese).
I merged the pull request from mcg1969 who contributed C version of dotwrp several months ago. He created a more capable package called vecLibFort. I created a port of vecLibFort for MacPorts.
vecLibFort works fine on various version of OS X from Snow Leopard through Mavericks. However I encountered a problem on a PowerPC machine running Leopard. It is dated but I still use it because it is a big endian machine and I need Octave and ncl.
The problem is the one I solved with dotwrp in four years ago: complex return value. I get segfault when linked against the C wrapper but I don’t when linked against the Fortran wrapper.
After some Google searches I realize that the returning a struct is involved and architecture dependent. I noticed that the use of C99 complex types solves the problem.
C function works on Intel but fails on PPC:
fcplx addtwoc_(const fcplx *x, const fcplx *y)
{
  fcplx z;
  z.r = x->r + y->r;
  z.i = x->i + y->i;
  return z;
}
Modified C function works on both
#include 
float complex addtwoc_(const float complex *x, const float complex *y)
{
  float complex z;
  z = *x + *y;
  return z;
}
The caller program in Fortran
      program main

        complex x, y, z, addtwoc
        x = (1.0, 2.0)
        y = (2.0, 2.0)
        z = addtwoc(x, y)
        print *, "z = ", z, " x+y=", x+y
       
      end

このブログを検索

ブログ アーカイブ

Translate

QooQ