chiark / gitweb /
untabify source files, make indenting more uniform with GNU indent -kr --no-tabs...
[nlopt.git] / src / api / f77api.c
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, double *gradient, const int *need_gradient, void *func_data);
32
33 typedef void (*nlopt_f77_mfunc) (const int *m, double *val, const int *n, const double *x, double *gradient, const int *need_gradient, void *func_data);
34
35 typedef struct {
36     nlopt_f77_func f;
37     nlopt_f77_mfunc mf;
38     void *f_data;
39 } f77_func_data;
40
41 static void *free_f77_func_data(void *p)
42 {
43     free(p);
44     return NULL;
45 }
46
47 static void *dup_f77_func_data(void *p)
48 {
49     void *pnew = (void *) malloc(sizeof(f77_func_data));
50     if (pnew)
51         memcpy(pnew, p, sizeof(f77_func_data));
52     return pnew;
53 }
54
55 static double f77_func_wrap_old(int n, const double *x, double *grad, void *data)
56 {
57     f77_func_data *d = (f77_func_data *) data;
58     double val;
59     int need_gradient = grad != 0;
60     d->f(&val, &n, x, grad, &need_gradient, d->f_data);
61     return val;
62 }
63
64 static double f77_func_wrap(unsigned n, const double *x, double *grad, void *data)
65 {
66     f77_func_data *d = (f77_func_data *) data;
67     int ni = (int) n;
68     double val;
69     int need_gradient = grad != 0;
70     d->f(&val, &ni, x, grad, &need_gradient, d->f_data);
71     return val;
72 }
73
74 static void f77_mfunc_wrap(unsigned m, double *result, unsigned n, const double *x, double *grad, void *data)
75 {
76     f77_func_data *d = (f77_func_data *) data;
77     int mi = (int) m;
78     int ni = (int) n;
79     int need_gradient = grad != 0;
80     d->mf(&mi, result, &ni, x, grad, &need_gradient, d->f_data);
81 }
82
83 /*-----------------------------------------------------------------------*/
84
85 #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); }
86 #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); }
87 #define F77_GETSET(name,NAME,T) F77_GET(name,NAME,T) F77_SET(name,NAME,T)
88
89 #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); }
90 #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); }
91 #define F77_GETSETA(name,NAME,T) F77_GETA(name,NAME,T) F77_SETA(name,NAME,T) F77_SET(name##1,NAME##1,T)
92
93 /*-----------------------------------------------------------------------*/
94 /* rather than trying to detect the Fortran name-mangling scheme with
95    autoconf, we just include wrappers with all common name-mangling
96    schemes ... this avoids problems and also allows us to work with
97    multiple Fortran compilers on the same machine.  Since the Fortran
98    wrapper functions are so small, the library bloat of including them
99    multiple times is negligible and seems well worth the benefit. */
100
101 #  define F77CALL(a, A) F77(a, A)
102
103 /* name + underscore is by far the most common (gfortran, g77, Intel, ...) */
104 #  define F77(a, A) a ## _
105 #  include "f77funcs.h"
106
107 /* also include g77 convention of name + double underscore for identifiers
108    containing underscores */
109 #  define F77_(a, A) a ## __
110 #  include "f77funcs_.h"
111 #  undef F77_
112
113 /* AIX and HPUX use just the lower-case name */
114 #  undef F77
115 #  define F77(a, A) a
116 #  include "f77funcs.h"
117
118 /* Old Cray Unicos, as well as several Windows Fortran compilers
119    (Digital/Compaq/HP Visual Fortran and Intel Fortran) use all-uppercase
120    name */
121
122 /* Digital/Compaq/HP Visual Fortran, Intel Fortran.  stdcall attribute
123    is apparently required to adjust for calling conventions (callee
124    pops stack in stdcall).  See also:
125        http://msdn.microsoft.com/library/en-us/vccore98/html/_core_mixed.2d.language_programming.3a_.overview.asp
126 */
127 #  undef F77
128 #  undef F77CALL
129 #  define F77(a, A) NLOPT_STDCALL A
130 #  define F77CALL(a, A) A
131 #  include "f77funcs.h"