/*
-----------------------------------------------------------------------
   Description: C-to-Fortran symbol name "mangler" macros
                  Define a macro to mangle function names so that they can be
                  recognized by the Fortran linker.  Specifically, F77_FUNC_
                  is designed to mangle identifiers containing case and
                  underscore modifications to symbol names. 

   Authors:     FFTW folks at MIT

   Revisions:

   DATE         AUTHOR          DESCRIPTION
   -----------  --------------  ---------------------------------------
   05/21/00     rdh       	modified for general mangling in fftlib

-----------------------------------------------------------------------
*/

#ifndef MANGLE_F77_H  
#define MANGLE_F77_H

#ifdef FORTRANIZE_LOWERCASE
#  ifdef FORTRANIZE_EXTRA_UNDERSCORE
#    define F77_FUNC_(x,X) x ## _
#  else
#    define F77_FUNC_(x,X) x
#  endif
#endif

#ifdef FORTRANIZE_LOWERCASE_UNDERSCORE
#  ifdef FORTRANIZE_EXTRA_UNDERSCORE
#    define F77_FUNC_(x,X) x ## __
#  else
#    define F77_FUNC_(x,X) x ## _
#  endif
#endif

#ifdef FORTRANIZE_UPPERCASE
#  ifdef FORTRANIZE_EXTRA_UNDERSCORE
#    define F77_FUNC_(x,X) X ## _
#  else
#    define F77_FUNC_(x,X) X
#  endif
#endif

#ifdef FORTRANIZE_UPPERCASE_UNDERSCORE
#  ifdef FORTRANIZE_EXTRA_UNDERSCORE
#    define F77_FUNC_(x,X) X ## __
#  else
#    define F77_FUNC_(x,X) X ## _
#  endif
#endif

#ifndef F77_FUNC_
#    define F77_FUNC_(x,X) x ## _
#endif

#if _VMS
#    define F77_FUNC_(x,X) X
#endif

#endif /* MANGLE_F77_H */


/*
-----------------------------------------------------------------------
   Description: libnmfft_f77.c

       FORTRAN-callable "wrappers" for some of the FFT routines.  To
       make these routines callable from FORTRAN, three things had to
       be done:

       * The routine names have to be in the style that is expected by
         the FORTRAN linker.  This is accomplished with the F77_FUNC_
         macro routines developed by FFTW folks at MIT.  
         (See the file "mangle_f77.h".)

       * All parameters must be passed by reference.

       * Return values had to be converted into parameters (some
         Fortran implementations seem to have trouble calling C functions
         that return a value).

       The wrapper routines have the same name as the wrapped routine,
       except that "_f77" has been appended.

   Authors:     RDH, original format borrowed from FFTW folks at MIT

   Revisions:

   DATE         AUTHOR          DESCRIPTION
   -----------  --------------  ---------------------------------------
   05/21/00     rdh         	baseline
   07/11/01     rdh         	modified nm_fft_f77 to have return value

-----------------------------------------------------------------------
*/

#include "libnmfft.h"

/* Function declarations */
extern void fft_init (FFT_Plan *plan, fft_int fftsize, fft_int fftflags);
extern void fft_free (FFT_Plan *plan);
extern void fft_work (fft_cmplx *a, FFT_Plan *plan);
extern fft_int nm_fft (fft_cmplx *cbuf, fft_int fftsize, fft_int fftflags);
extern fft_int nm_fftsize (fft_int n, fft_int flags, fft_int searchdir);

void F77_FUNC_(fft_init_f77,FFT_INIT_F77)
(FFT_Plan *plan, fft_int *fftsize, fft_int *fftflags)
{
    fft_int fftsize_f77, fftflags_f77;
    fftsize_f77  = (fft_int) *fftsize;
    fftflags_f77 = (fft_int) *fftflags;
    fft_init(plan,fftsize_f77,fftflags_f77);
    return;
}

void F77_FUNC_(fft_free_f77,FFT_FREE_F77)
(FFT_Plan *plan)
{
    fft_free(plan);
    return;
}

void F77_FUNC_(fft_work_f77,FFT_WORK_F77)
(fft_cmplx *a, FFT_Plan *plan)
{
    fft_work(a,plan);
    return;
}

void F77_FUNC_(nm_fft_f77,NM_FFT_F77)      
(fft_cmplx *cbuf, fft_int *fftsize, fft_int *fftflags, fft_int *irtn)
{
    fft_int fftsize_f77, fftflags_f77, irtn_f77;
    fftsize_f77  = (fft_int) *fftsize;
    fftflags_f77 = (fft_int) *fftflags;
    irtn_f77 = nm_fft(cbuf,fftsize_f77,fftflags_f77);
    *irtn = irtn_f77;
    return;
}

void F77_FUNC_(nm_fftsize_f77,NM_FFTSIZE_F77)
(fft_int *nfft, fft_int *flags, fft_int *srchdir, fft_int *nopt)
{
    fft_int nfft_f77, flags_f77, srchdir_f77, nopt_f77;
    nfft_f77    = (fft_int) *nfft;
    flags_f77   = (fft_int) *flags;
    srchdir_f77 = (fft_int) *srchdir;
    nopt_f77  = nm_fftsize(nfft_f77,flags_f77,srchdir_f77);
    *nopt     = nopt_f77;
    return;
}
