/
REXXMATH.C
364 lines (328 loc) · 8.09 KB
/
REXXMATH.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**********************************************************************
* REXXMATH.c - (c) Zhitao Zeng Footprint Software 1994 *
* This program is released to the public domain, feel free to use *
* it for any purposes. *
* If you have any suggestions, please email to Zhitao Zeng at: *
* CIS: 72000,2440 *
* Internet: zzeng@footprint.com *
**********************************************************************/
#define INCL_ERRORS
#define INCL_REXXSAA
#define _DLL
#define _MT
#include <os2.h>
#include <rexxsaa.h>
#include <float.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define ENABLE_CACHE
RexxFunctionHandler MathLoadFuncs;
RexxFunctionHandler MathDropFuncs;
RexxFunctionHandler MathFunc;
typedef enum _MathFuncID {
fn_acos,
fn_asin,
fn_atan,
fn_atan2,
fn_ceil,
fn_cos,
fn_cosh,
fn_exp,
fn_fabs,
fn_floor,
fn_fmod,
fn_frexp,
fn_ldexp,
fn_log,
fn_log10,
fn_modf,
fn_pow,
fn_sin,
fn_sinh,
fn_sqrt,
fn_tan,
fn_tanh,
fn_erf,
fn_erfc,
fn_gamma,
fn_hypot,
fn_j0,
fn_j1,
fn_jn,
fn_y0,
fn_y1,
fn_yn,
fn_pi
} MathFuncID;
typedef struct _RxMathFunc
{
PSZ name;
MathFuncID id;
ULONG numargs;
} RxMathFunc, *PRxMathFunc;
static RxMathFunc RxMathFncTable[] =
{
"acos", fn_acos, 1,
"asin", fn_asin, 1,
"atan", fn_atan, 1,
"atan2", fn_atan2, 2,
"ceil", fn_ceil, 1,
"cos", fn_cos, 1,
"cosh", fn_cosh, 1,
"exp", fn_exp, 1,
"fabs", fn_fabs, 1,
"floor", fn_floor, 1,
"fmod", fn_fmod, 2,
"frexp", fn_frexp, 1,
"ldexp", fn_ldexp, 2,
"log", fn_log, 1,
"log10", fn_log10, 1,
"modf", fn_modf, 1,
"pow", fn_pow, 2,
"sin", fn_sin, 1,
"sinh", fn_sinh, 1,
"sqrt", fn_sqrt, 1,
"tan", fn_tan, 1,
"tanh", fn_tanh, 1,
"erf", fn_erf, 1,
"erfc", fn_erfc, 1,
"gamma", fn_gamma, 1,
"hypot", fn_hypot, 2,
"j0", fn_j0, 1,
"j1", fn_j1, 1,
"jn", fn_jn, 2,
"y0", fn_y0, 1,
"y1", fn_y1, 1,
"yn", fn_yn, 2,
"pi", fn_pi, 0,
};
#ifdef ENABLE_CACHE
static RxMathFunc RxMathFncCache =
{
"", -1, 0,
};
#endif
#define INVALID_ROUTINE 40 /* Error */
#define VALID_ROUTINE 0 /* Successful completion */
/*************************************************************************
* Function: MathLoadFuncs *
* Syntax: call MathLoadFuncs [option] *
* Return: null string *
*************************************************************************/
ULONG MathLoadFuncs(CHAR *name, ULONG numargs, RXSTRING args[],
CHAR *queuename, RXSTRING *retstr)
{
INT entries; /* Num of entries */
INT j; /* Counter */
retstr->strlength = 0; /* set return value */
entries = sizeof(RxMathFncTable)/sizeof(RxMathFunc);
RexxRegisterFunctionDll("MathLoadFuncs", "REXXMATH", "MathLoadFuncs");
RexxRegisterFunctionDll("MathDropFuncs", "REXXMATH", "MathDropFuncs");
for (j = 0; j < entries; j++) {
RexxRegisterFunctionDll(RxMathFncTable[j].name,
"REXXMATH", "MathFunc");
}
return VALID_ROUTINE;
}
/*************************************************************************
* Function: MathDropFuncs *
* Syntax: call MathDropFuncs *
* Return: NO_UTIL_ERROR - Successful. *
*************************************************************************/
ULONG MathDropFuncs(CHAR *name, ULONG numargs, RXSTRING args[],
CHAR *queuename, RXSTRING *retstr)
{
INT entries; /* Num of entries */
INT j; /* Counter */
retstr->strlength = 0; /* return a null string result*/
RexxDeregisterFunction("MathLoadFuncs");
RexxDeregisterFunction("MathDropFuncs");
entries = sizeof(RxMathFncTable)/sizeof(RxMathFunc);
for (j = 0; j < entries; j++)
RexxDeregisterFunction(RxMathFncTable[j].name);
return VALID_ROUTINE; /* no error on call */
}
int resolve_func(CHAR *name, ULONG numargs)
{
int entries = sizeof(RxMathFncTable)/sizeof(RxMathFunc);
register int i;
int fn_id = -1;
for (i=0; i < entries; i++)
if (stricmp(name, RxMathFncTable[i].name) == 0)
{
if (numargs == RxMathFncTable[i].numargs)
{
fn_id = RxMathFncTable[i].id;
#ifdef ENABLE_CACHE
RxMathFncCache.id = fn_id;
RxMathFncCache.name = RxMathFncTable[i].name;
RxMathFncCache.numargs = RxMathFncTable[i].numargs;
#endif
}
break;
}
return fn_id;
}
#ifdef ENABLE_CACHE
int cache_func(CHAR *name, ULONG numargs)
{
int fn_id = -1;
if (stricmp(name, RxMathFncCache.name) == 0)
if (numargs == RxMathFncCache.numargs)
fn_id = RxMathFncCache.id;
return fn_id;
}
#endif
/*************************************************************************
* Function: MathFunc *
*************************************************************************/
ULONG MathFunc(CHAR *name, ULONG numargs, RXSTRING args[],
CHAR *queuename, RXSTRING *retstr)
{
double x, y, result;
int i, fn_id = -1;
#ifdef ENABLE_CACHE
if (RxMathFncCache.id >= 0)
fn_id = cache_func(name, numargs);
if (fn_id < 0)
{
fn_id = resolve_func(name, numargs);
if (fn_id < 0)
return INVALID_ROUTINE;
}
#else
fn_id = resolve_func(name, numargs);
if (fn_id < 0)
return INVALID_ROUTINE;
#endif
x = atof(args[0].strptr);
if (numargs > 1L)
{
y = atof(args[1].strptr);
i = atoi(args[1].strptr);
}
switch (fn_id)
{
case fn_acos:
result = acos(x);
break;
case fn_asin:
result = asin(x);
break;
case fn_atan:
result = atan(x);
break;
case fn_atan2:
result = atan2(x, y);
break;
case fn_ceil:
result = ceil(x);
break;
case fn_cos:
result = cos(x);
break;
case fn_cosh:
result = cosh(x);
break;
case fn_exp:
result = exp(x);
break;
case fn_fabs:
result = fabs(x);
break;
case fn_floor:
result = floor(x);
break;
case fn_fmod:
result = fmod(x, y);
break;
case fn_frexp:
result = frexp(x, &i);
break;
case fn_ldexp:
result = ldexp(x, i);
break;
case fn_log:
result = log(x);
break;
case fn_log10:
result = log10(x);
break;
case fn_modf:
result = modf(x, &y);
break;
case fn_pow:
result = pow(x, y);
break;
case fn_sin:
result = sin(x);
break;
case fn_sinh:
result = sinh(x);
break;
case fn_sqrt:
result = sqrt(x);
break;
case fn_tan:
result = tan(x);
break;
case fn_tanh:
result = tanh(x);
break;
#ifdef __IBMC__
case fn_erf:
result = erf( x );
break;
case fn_erfc:
result = erfc( x );
break;
case fn_gamma:
result = gamma( x );
break;
#endif
case fn_hypot:
result = hypot( x, y );
break;
case fn_j0:
result = j0( x );
break;
case fn_j1:
result = j1( x );
break;
case fn_jn:
result = jn( i, x );
break;
case fn_y0:
result = y0( x );
break;
case fn_y1:
result = y1( x );
break;
case fn_yn:
result = yn( i, x );
break;
case fn_pi:
result = 3.1415926575;
break;
default:
return INVALID_ROUTINE;
}
switch (fn_id)
{
case fn_frexp:
sprintf(retstr->strptr, "%lf %i", result, i);
break;
case fn_modf:
sprintf(retstr->strptr, "%lf %lf", result, y);
break;
default:
sprintf(retstr->strptr, "%lf", result);
break;
}
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE;
}