1 /* Copyright (c) 2007-2014 Massachusetts Institute of Technology
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be
12  * included in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "nlopt.h"
27 
28 /*-----------------------------------------------------------------------*/
29 /* wrappers around f77 procedures */
30 
31 typedef void (*nlopt_f77_func)(double *val, const int *n, const double *x,
32 			       double *gradient, const int *need_gradient,
33 			       void *func_data);
34 
35 typedef void (*nlopt_f77_mfunc)(const int *m,
36 				double *val, const int *n, const double *x,
37 				double *gradient, const int *need_gradient,
38 				void *func_data);
39 
40 typedef struct {
41      nlopt_f77_func f;
42      nlopt_f77_mfunc mf;
43      void *f_data;
44 } f77_func_data;
45 
free_f77_func_data(void * p)46 static void *free_f77_func_data(void *p) { free(p); return NULL; }
dup_f77_func_data(void * p)47 static void *dup_f77_func_data(void *p) {
48      void *pnew = (void*) malloc(sizeof(f77_func_data));
49      if (pnew) memcpy(pnew, p, sizeof(f77_func_data));
50      return pnew;
51 }
52 
f77_func_wrap_old(int n,const double * x,double * grad,void * data)53 static double f77_func_wrap_old(int n, const double *x, double *grad, void *data)
54 {
55      f77_func_data *d = (f77_func_data *) data;
56      double val;
57      int need_gradient = grad != 0;
58      d->f(&val, &n, x, grad, &need_gradient, d->f_data);
59      return val;
60 }
61 
f77_func_wrap(unsigned n,const double * x,double * grad,void * data)62 static double f77_func_wrap(unsigned n, const double *x, double *grad, void *data)
63 {
64      f77_func_data *d = (f77_func_data *) data;
65      int ni = (int) n;
66      double val;
67      int need_gradient = grad != 0;
68      d->f(&val, &ni, x, grad, &need_gradient, d->f_data);
69      return val;
70 }
71 
f77_mfunc_wrap(unsigned m,double * result,unsigned n,const double * x,double * grad,void * data)72 static void f77_mfunc_wrap(unsigned m, double *result, unsigned n, const double *x, double *grad, void *data)
73 {
74      f77_func_data *d = (f77_func_data *) data;
75      int mi = (int) m;
76      int ni = (int) n;
77      int need_gradient = grad != 0;
78      d->mf(&mi, result, &ni, x, grad, &need_gradient, d->f_data);
79 }
80 
81 /*-----------------------------------------------------------------------*/
82 
83 #define F77_GET(name,NAME,T) void F77_(nlo_get_##name,NLO_GET_##NAME)(T *val, nlopt_opt *opt) { *val = (T) nlopt_get_##name(*opt); }
84 #define F77_SET(name,NAME,T) void F77_(nlo_set_##name,NLO_SET_##NAME)(int *ret, nlopt_opt *opt, T *val) { *ret = (int) nlopt_set_##name(*opt, *val); }
85 #define F77_GETSET(name,NAME,T) F77_GET(name,NAME,T) F77_SET(name,NAME,T)
86 
87 #define F77_GETA(name,NAME,T) void F77_(nlo_get_##name,NLO_GET_##NAME)(int *ret, nlopt_opt *opt, T *val) { *ret = (int) nlopt_get_##name(*opt, val); }
88 #define F77_SETA(name,NAME,T) void F77_(nlo_set_##name,NLO_SET_##NAME)(int *ret, nlopt_opt *opt, T *val) { *ret = (int) nlopt_set_##name(*opt, val); }
89 #define F77_GETSETA(name,NAME,T) F77_GETA(name,NAME,T) F77_SETA(name,NAME,T) F77_SET(name##1,NAME##1,T)
90 
91 /*-----------------------------------------------------------------------*/
92 /* rather than trying to detect the Fortran name-mangling scheme with
93    autoconf, we just include wrappers with all common name-mangling
94    schemes ... this avoids problems and also allows us to work with
95    multiple Fortran compilers on the same machine.  Since the Fortran
96    wrapper functions are so small, the library bloat of including them
97    multiple times is negligible and seems well worth the benefit. */
98 
99 #  define F77CALL(a, A) F77(a, A)
100 
101 /* name + underscore is by far the most common (gfortran, g77, Intel, ...) */
102 #  define F77(a, A) a ## _
103 #  include "f77funcs.h"
104 
105 /* also include g77 convention of name + double underscore for identifiers
106    containing underscores */
107 #  define F77_(a, A) a ## __
108 #  include "f77funcs_.h"
109 #  undef F77_
110 
111 /* AIX and HPUX use just the lower-case name */
112 #  undef F77
113 #  define F77(a, A) a
114 #  include "f77funcs.h"
115 
116 /* Old Cray Unicos, as well as several Windows Fortran compilers
117    (Digital/Compaq/HP Visual Fortran and Intel Fortran) use all-uppercase
118    name */
119 
120 /* Digital/Compaq/HP Visual Fortran, Intel Fortran.  stdcall attribute
121    is apparently required to adjust for calling conventions (callee
122    pops stack in stdcall).  See also:
123        http://msdn.microsoft.com/library/en-us/vccore98/html/_core_mixed.2d.language_programming.3a_.overview.asp
124 */
125 #  undef F77
126 #  undef F77CALL
127 #  define F77(a, A) NLOPT_STDCALL A
128 #  define F77CALL(a, A) A
129 #  include "f77funcs.h"
130