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