-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops.c
318 lines (284 loc) · 8.98 KB
/
ops.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
#include <ctype.h>
#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "ops.h"
#pragma STDC FENV_ACCESS ON
static op_status op_status_for_fenv_except() {
int exceptions =
fetestexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW | FE_INVALID);
if (exceptions & FE_DIVBYZERO)
return OP_DIV_BY_ZERO;
else if (exceptions & FE_OVERFLOW)
return OP_OVERFLOW;
else if (exceptions & FE_UNDERFLOW)
return OP_UNDERFLOW;
else if (exceptions & FE_INVALID)
return OP_INVALID_OP;
return OP_SUCCESS;
}
static op_status add(long double *res, ...) {
va_list args;
va_start(args, res);
long double operand_1 = va_arg(args, long double);
long double operand_2 = va_arg(args, long double);
va_end(args);
*res = operand_1 + operand_2;
return op_status_for_fenv_except();
}
static op_status subtract(long double *res, ...) {
va_list args;
va_start(args, res);
long double operand_1 = va_arg(args, long double);
long double operand_2 = va_arg(args, long double);
va_end(args);
*res = operand_1 - operand_2;
return op_status_for_fenv_except();
}
static op_status multiply(long double *res, ...) {
va_list args;
va_start(args, res);
long double operand_1 = va_arg(args, long double);
long double operand_2 = va_arg(args, long double);
va_end(args);
*res = operand_1 * operand_2;
return op_status_for_fenv_except();
}
static op_status divide(long double *res, ...) {
va_list args;
va_start(args, res);
long double operand_1 = va_arg(args, long double);
long double operand_2 = va_arg(args, long double);
va_end(args);
*res = operand_1 / operand_2;
return op_status_for_fenv_except();
}
static op_status power(long double *res, ...) {
va_list args;
va_start(args, res);
long double operand_1 = va_arg(args, long double);
long double operand_2 = va_arg(args, long double);
va_end(args);
*res = powl(operand_1, operand_2);
return op_status_for_fenv_except();
}
static op_status square_root(long double *res, ...) {
va_list args;
va_start(args, res);
long double operand_1 = va_arg(args, long double);
va_end(args);
*res = sqrtl(operand_1);
return op_status_for_fenv_except();
}
static op_status modulo(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
long double y = va_arg(args, long double);
va_end(args);
*res = fmodl(x, y);
return op_status_for_fenv_except();
}
static op_status factorial(long double *res, ...) {
// FIXME: reject non-integers
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = 1;
for (long double i = 2; i <= x; i++) {
*res *= i;
}
return op_status_for_fenv_except();
}
static op_status sine(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = sinl(x);
return op_status_for_fenv_except();
}
static op_status cosine(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = cosl(x);
return op_status_for_fenv_except();
}
static op_status tangent(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = tanl(x);
return op_status_for_fenv_except();
}
static op_status hsin(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = sinhl(x);
return op_status_for_fenv_except();
}
static op_status hcos(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = coshl(x);
return op_status_for_fenv_except();
}
static op_status htan(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = tanhl(x);
return op_status_for_fenv_except();
}
static op_status arcsin(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = asinl(x);
return op_status_for_fenv_except();
}
static op_status arccos(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = acosl(x);
return op_status_for_fenv_except();
}
static op_status arctan(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = atanl(x);
return op_status_for_fenv_except();
}
static op_status arcsinh(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = asinhl(x);
return op_status_for_fenv_except();
}
static op_status arccosh(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = acoshl(x);
return op_status_for_fenv_except();
}
static op_status arctanh(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = atanhl(x);
return op_status_for_fenv_except();
}
static op_status ln(long double *res, ...) {
va_list args;
va_start(args, res);
long double x = va_arg(args, long double);
va_end(args);
*res = logl(x);
return op_status_for_fenv_except();
}
static op_status log_base(long double *res, ...) {
va_list args;
va_start(args, res);
long double base = va_arg(args, long double);
long double x = va_arg(args, long double);
va_end(args);
if (base == 2) {
*res = log2l(x);
} else if (base == 10) {
*res = log10l(x);
} else
*res = logl(x) / logl(base);
return op_status_for_fenv_except();
}
static op_status pi_constant(long double *res, ...) {
*res = 3.14159265358979323846;
return OP_SUCCESS;
}
static op_status e_constant(long double *res, ...) {
*res = 2.71828182845904523536;
return OP_SUCCESS;
}
static op_status op_abort(long double *res, ...) {
fprintf(stderr, "BUG: Calling a callback on parentheses. Aborting...");
exit(EXIT_FAILURE);
return OP_ERROR;
}
#define OPS_NUM 26
#define DEF_OP(_op, _cb, _operands_num, prec) \
(struct operator) { \
.op = _op, .cb = _cb, .operands_num = _operands_num, \
.precedence = prec \
}
static const struct operator operators[OPS_NUM] = {
DEF_OP("(", op_abort, 0, 0), DEF_OP(")", op_abort, 0, 0),
DEF_OP("*", multiply, 2, 2), DEF_OP("/", divide, 2, 2),
DEF_OP("+", add, 2, 3), DEF_OP("-", subtract, 2, 3),
DEF_OP("^", power, 2, 1), DEF_OP("%", modulo, 2, 1),
DEF_OP("!", factorial, 1, 1), DEF_OP("sqrt", square_root, 1, 1),
DEF_OP("sin", sine, 1, 1), DEF_OP("cos", cosine, 1, 1),
DEF_OP("tan", tangent, 1, 1), DEF_OP("sinh", hsin, 1, 1),
DEF_OP("cosh", hcos, 1, 1), DEF_OP("tanh", htan, 1, 1),
DEF_OP("arcsin", arcsin, 1, 1), DEF_OP("arccos", arccos, 1, 1),
DEF_OP("arctan", arctan, 1, 1), DEF_OP("arcsinh", arcsinh, 1, 1),
DEF_OP("arccosh", arccosh, 1, 1), DEF_OP("arctanh", arctanh, 1, 1),
DEF_OP("ln", ln, 1, 1), DEF_OP("log", log_base, 2, 1),
DEF_OP("PI", pi_constant, 0, 0), DEF_OP("e", e_constant, 0, 0),
};
/* find a registered operator contained in `expr` */
int get_operator(struct operator* op_struct, const char *expr) {
const struct operator* cur_cand = NULL;
unsigned char cur_cand_len = 0;
for (unsigned char i = 0; i < OPS_NUM; i++) {
unsigned char j;
for (j = 0; operators[i].op[j] && expr[j]; j++) {
if (tolower(operators[i].op[j]) != tolower(expr[j]))
break;
}
if (!operators[i].op[j]) {
if (j > cur_cand_len) {
// found a longer match
cur_cand = &operators[i];
cur_cand_len = j;
}
}
}
#ifdef EVAL_DEBUG
if (cur_cand) {
LOG_FMSG("found operator '%s'", cur_cand->op);
} else {
LOG_FMSG("found no operator in '%s'", expr);
}
#endif
if (cur_cand) {
memcpy(op_struct, cur_cand, sizeof(struct operator));
return 1;
}
return 0;
}