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