Chris@42: /*
Chris@42:  * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@42:  * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@42:  *
Chris@42:  * This program is free software; you can redistribute it and/or modify
Chris@42:  * it under the terms of the GNU General Public License as published by
Chris@42:  * the Free Software Foundation; either version 2 of the License, or
Chris@42:  * (at your option) any later version.
Chris@42:  *
Chris@42:  * This program is distributed in the hope that it will be useful,
Chris@42:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@42:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Chris@42:  * GNU General Public License for more details.
Chris@42:  *
Chris@42:  * You should have received a copy of the GNU General Public License
Chris@42:  * along with this program; if not, write to the Free Software
Chris@42:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Chris@42:  *
Chris@42:  */
Chris@42: 
Chris@42: #include "api.h"
Chris@42: #include "dft.h"
Chris@42: #include "rdft.h"
Chris@42: 
Chris@42: #include "x77.h"
Chris@42: 
Chris@42: /* if F77_FUNC is not defined, then we don't know how to mangle identifiers
Chris@42:    for the Fortran linker, and we must omit the f77 API. */
Chris@42: #if defined(F77_FUNC) || defined(WINDOWS_F77_MANGLING)
Chris@42: 
Chris@42: /*-----------------------------------------------------------------------*/
Chris@42: /* some internal functions used by the f77 api */
Chris@42: 
Chris@42: /* in fortran, the natural array ordering is column-major, which
Chris@42:    corresponds to reversing the dimensions relative to C's row-major */
Chris@42: static int *reverse_n(int rnk, const int *n)
Chris@42: {
Chris@42:      int *nrev;
Chris@42:      int i;
Chris@42:      A(FINITE_RNK(rnk));
Chris@42:      nrev = (int *) MALLOC(sizeof(int) * (unsigned)rnk, PROBLEMS);
Chris@42:      for (i = 0; i < rnk; ++i)
Chris@42:           nrev[rnk - i - 1] = n[i];
Chris@42:      return nrev;
Chris@42: }
Chris@42: 
Chris@42: /* f77 doesn't have data structures, so we have to pass iodims as
Chris@42:    parallel arrays */
Chris@42: static X(iodim) *make_dims(int rnk, const int *n,
Chris@42: 			   const int *is, const int *os)
Chris@42: {
Chris@42:      X(iodim) *dims;
Chris@42:      int i;
Chris@42:      A(FINITE_RNK(rnk));
Chris@42:      dims = (X(iodim) *) MALLOC(sizeof(X(iodim)) * (unsigned)rnk, PROBLEMS);
Chris@42:      for (i = 0; i < rnk; ++i) {
Chris@42:           dims[i].n = n[i];
Chris@42:           dims[i].is = is[i];
Chris@42:           dims[i].os = os[i];
Chris@42:      }
Chris@42:      return dims;
Chris@42: }
Chris@42: 
Chris@42: typedef struct {
Chris@42:      void (*f77_write_char)(char *, void *);
Chris@42:      void *data;
Chris@42: } write_char_data;
Chris@42: 
Chris@42: static void write_char(char c, void *d)
Chris@42: {
Chris@42:      write_char_data *ad = (write_char_data *) d;
Chris@42:      ad->f77_write_char(&c, ad->data);
Chris@42: }
Chris@42: 
Chris@42: typedef struct {
Chris@42:      void (*f77_read_char)(int *, void *);
Chris@42:      void *data;
Chris@42: } read_char_data;
Chris@42: 
Chris@42: static int read_char(void *d)
Chris@42: {
Chris@42:      read_char_data *ed = (read_char_data *) d;
Chris@42:      int c;
Chris@42:      ed->f77_read_char(&c, ed->data);
Chris@42:      return (c < 0 ? EOF : c);
Chris@42: }
Chris@42: 
Chris@42: static X(r2r_kind) *ints2kinds(int rnk, const int *ik)
Chris@42: {
Chris@42:      if (!FINITE_RNK(rnk) || rnk == 0)
Chris@42: 	  return 0;
Chris@42:      else {
Chris@42: 	  int i;
Chris@42: 	  X(r2r_kind) *k;
Chris@42: 
Chris@42: 	  k = (X(r2r_kind) *) MALLOC(sizeof(X(r2r_kind)) * (unsigned)rnk, PROBLEMS);
Chris@42: 	  /* reverse order for Fortran -> C */
Chris@42: 	  for (i = 0; i < rnk; ++i)
Chris@42: 	       k[i] = (X(r2r_kind)) ik[rnk - 1 - i];
Chris@42: 	  return k;
Chris@42:      }
Chris@42: }
Chris@42: 
Chris@42: /*-----------------------------------------------------------------------*/
Chris@42: 
Chris@42: #define F77(a, A) F77x(x77(a), X77(A))
Chris@42: 
Chris@42: #ifndef WINDOWS_F77_MANGLING
Chris@42: 
Chris@42: #if defined(F77_FUNC)
Chris@42: #  define F77x(a, A) F77_FUNC(a, A)
Chris@42: #  include "f77funcs.h"
Chris@42: #endif
Chris@42: 
Chris@42: /* If identifiers with underscores are mangled differently than those
Chris@42:    without underscores, then we include *both* mangling versions.  The
Chris@42:    reason is that the only Fortran compiler that does such differing
Chris@42:    mangling is currently g77 (which adds an extra underscore to names
Chris@42:    with underscores), whereas other compilers running on the same
Chris@42:    machine are likely to use non-underscored mangling.  (I'm sick
Chris@42:    of users complaining that FFTW works with g77 but not with e.g.
Chris@42:    pgf77 or ifc on the same machine.)  Note that all FFTW identifiers
Chris@42:    contain underscores, and configure picks g77 by default. */
Chris@42: #if defined(F77_FUNC_) && !defined(F77_FUNC_EQUIV)
Chris@42: #  undef F77x
Chris@42: #  define F77x(a, A) F77_FUNC_(a, A)
Chris@42: #  include "f77funcs.h"
Chris@42: #endif
Chris@42: 
Chris@42: #else /* WINDOWS_F77_MANGLING */
Chris@42: 
Chris@42: /* Various mangling conventions common (?) under Windows. */
Chris@42: 
Chris@42: /* g77 */
Chris@42: #  define WINDOWS_F77_FUNC(a, A) a ## __
Chris@42: #  define F77x(a, A) WINDOWS_F77_FUNC(a, A)
Chris@42: #  include "f77funcs.h"
Chris@42: 
Chris@42: /* Intel, etc. */
Chris@42: #  undef WINDOWS_F77_FUNC
Chris@42: #  define WINDOWS_F77_FUNC(a, A) a ## _
Chris@42: #  include "f77funcs.h"
Chris@42: 
Chris@42: /* Digital/Compaq/HP Visual Fortran, Intel Fortran.  stdcall attribute
Chris@42:    is apparently required to adjust for calling conventions (callee
Chris@42:    pops stack in stdcall).  See also:
Chris@42:        http://msdn.microsoft.com/library/en-us/vccore98/html/_core_mixed.2d.language_programming.3a_.overview.asp
Chris@42: */
Chris@42: #  undef WINDOWS_F77_FUNC
Chris@42: #  if defined(__GNUC__)
Chris@42: #    define WINDOWS_F77_FUNC(a, A) __attribute__((stdcall)) A
Chris@42: #  elif defined(_MSC_VER) || defined(_ICC) || defined(_STDCALL_SUPPORTED)
Chris@42: #    define WINDOWS_F77_FUNC(a, A) __stdcall A
Chris@42: #  else
Chris@42: #    define WINDOWS_F77_FUNC(a, A) A /* oh well */
Chris@42: #  endif
Chris@42: #  include "f77funcs.h"
Chris@42: 
Chris@42: #endif /* WINDOWS_F77_MANGLING */
Chris@42: 
Chris@42: #endif				/* F77_FUNC */