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