-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpn_ieee.c
486 lines (403 loc) · 12.6 KB
/
rpn_ieee.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* ReactOS Calc (RPN encoder/decoder for IEEE-754 engine)
*
* Copyright 2007-2017, Carlo Bramini
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "calc.h"
typedef struct {
calc_node_t node;
void *next;
} stack_node_t;
typedef void (*operator_call)(calc_number_t *, calc_number_t *, calc_number_t *);
typedef struct {
unsigned int prec;
operator_call op_f;
operator_call op_i;
operator_call op_p;
} calc_operator_t;
static stack_node_t *stack;
static calc_node_t temp;
static BOOL percent_mode;
static void rpn_add_f(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_sub_f(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_mul_f(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_div_f(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_mod_f(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_pow_f(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_sqr_f(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_and_f(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_or_f (calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_xor_f(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_shl_f(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_shr_f(calc_number_t *r, calc_number_t *a, calc_number_t *b);
/* Integer mode calculations */
static void rpn_add_i(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_sub_i(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_mul_i(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_div_i(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_mod_i(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_and_i(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_or_i (calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_xor_i(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_shl_i(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_shr_i(calc_number_t *r, calc_number_t *a, calc_number_t *b);
/* Percentage mode calculations */
static void rpn_add_p(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_sub_p(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_mul_p(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static void rpn_div_p(calc_number_t *r, calc_number_t *a, calc_number_t *b);
static const calc_operator_t operator_list[] = {
{ 0, NULL, NULL, NULL, }, // RPN_OPERATOR_PARENT
{ 0, NULL, NULL, NULL, }, // RPN_OPERATOR_PERCENT
{ 0, NULL, NULL, NULL, }, // RPN_OPERATOR_EQUAL
{ 1, rpn_or_f, rpn_or_i, NULL, }, // RPN_OPERATOR_OR
{ 2, rpn_xor_f, rpn_xor_i, NULL, }, // RPN_OPERATOR_XOR
{ 3, rpn_and_f, rpn_and_i, NULL, }, // RPN_OPERATOR_AND
{ 4, rpn_shl_f, rpn_shl_i, NULL, }, // RPN_OPERATOR_LSH
{ 4, rpn_shr_f, rpn_shr_i, NULL, }, // RPN_OPERATOR_RSH
{ 5, rpn_add_f, rpn_add_i, rpn_add_p, }, // RPN_OPERATOR_ADD
{ 5, rpn_sub_f, rpn_sub_i, rpn_sub_p, }, // RPN_OPERATOR_SUB
{ 6, rpn_mul_f, rpn_mul_i, rpn_mul_p, }, // RPN_OPERATOR_MULT
{ 6, rpn_div_f, rpn_div_i, rpn_div_p, }, // RPN_OPERATOR_DIV
{ 6, rpn_mod_f, rpn_mod_i, NULL, }, // RPN_OPERATOR_MOD
{ 7, rpn_pow_f, NULL, NULL, }, // RPN_OPERATOR_POW
{ 7, rpn_sqr_f, NULL, NULL, }, // RPN_OPERATOR_SQR
};
static calc_node_t *pop(void)
{
void *next;
if (stack == NULL)
return NULL;
/* copy the node */
temp = stack->node;
next = stack->next;
/* free the node */
free(stack);
stack = next;
return &temp;
}
static int is_stack_empty(void)
{
return (stack == NULL);
}
static void push(calc_node_t *op)
{
stack_node_t *z = (stack_node_t *)malloc(sizeof(stack_node_t));
z->node = *op;
z->next = stack;
stack = z;
}
/*
static unsigned int get_prec(unsigned int opc)
{
unsigned int x;
for (x=0; x<SIZEOF(operator_list); x++)
if (operator_list[x].opc == opc) break;
return operator_list[x].prec;
}
*/
/* Real mode calculations */
static void rpn_add_f(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->f = a->f + b->f;
}
static void rpn_sub_f(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->f = a->f - b->f;
}
static void rpn_mul_f(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->f = a->f * b->f;
}
static void rpn_div_f(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
if (b->f == 0)
calc.is_nan = TRUE;
else
r->f = a->f / b->f;
}
static void rpn_mod_f(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
double t;
if (b->f == 0)
calc.is_nan = TRUE;
else {
modf(a->f/b->f, &t);
r->f = a->f - (t * b->f);
}
}
static void rpn_and_f(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
calc_number_t ai, bi;
ai.i = logic_dbl2int(a);
bi.i = logic_dbl2int(b);
r->f = (long double)(ai.i & bi.i);
}
static void rpn_or_f(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
calc_number_t ai, bi;
ai.i = logic_dbl2int(a);
bi.i = logic_dbl2int(b);
r->f = (long double)(ai.i | bi.i);
}
static void rpn_xor_f(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
calc_number_t ai, bi;
ai.i = logic_dbl2int(a);
bi.i = logic_dbl2int(b);
r->f = (long double)(ai.i ^ bi.i);
}
static void rpn_shl_f(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
calc_number_t n;
modf(b->f, &n.f);
r->f = a->f * pow(2., n.f);
}
static void rpn_shr_f(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
calc_number_t n;
modf(b->f, &n.f);
r->f = a->f / pow(2., n.f);
}
static void rpn_pow_f(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->f = pow(a->f, b->f);
if (_finite(r->f) == 0 || _isnan(r->f))
calc.is_nan = TRUE;
}
static void rpn_sqr_f(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
if (b->f == 0)
calc.is_nan = TRUE;
else {
r->f = pow(a->f, 1./b->f);
if (_finite(r->f) == 0 || _isnan(r->f))
calc.is_nan = TRUE;
}
}
/* Integer mode calculations */
static void rpn_add_i(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->i = a->i + b->i;
}
static void rpn_sub_i(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->i = a->i - b->i;
}
static void rpn_mul_i(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->i = a->i * b->i;
}
static void rpn_div_i(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
if (b->i == 0)
calc.is_nan = TRUE;
else
r->i = a->i / b->i;
}
static void rpn_mod_i(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
if (b->i == 0)
calc.is_nan = TRUE;
else
r->i = a->i % b->i;
}
static void rpn_and_i(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->i = a->i & b->i;
}
static void rpn_or_i(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->i = a->i | b->i;
}
static void rpn_xor_i(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->i = a->i ^ b->i;
}
static void rpn_shl_i(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->i = a->i << b->i;
}
static void rpn_shr_i(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->i = a->i >> b->i;
}
/* Percent mode calculations */
static void rpn_add_p(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->f = a->f * (1. + b->f/100.);
}
static void rpn_sub_p(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->f = a->f * (1. - b->f/100.);
}
static void rpn_mul_p(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
r->f = a->f * b->f / 100.;
}
static void rpn_div_p(calc_number_t *r, calc_number_t *a, calc_number_t *b)
{
if (b->f == 0)
calc.is_nan = TRUE;
else
r->f = a->f * 100. / b->f;
}
void run_operator(calc_node_t *result,
calc_node_t *a,
calc_node_t *b,
unsigned int operation)
{
calc_number_t da, db, dc;
DWORD base = calc.base;
da = a->number;
db = b->number;
if (a->base == IDC_RADIO_DEC && b->base != IDC_RADIO_DEC) {
db.f = logic_int2dbl(&b->number);
base = IDC_RADIO_DEC;
} else
if (a->base != IDC_RADIO_DEC && b->base == IDC_RADIO_DEC) {
da.f = logic_int2dbl(&a->number);
base = IDC_RADIO_DEC;
}
if (base == IDC_RADIO_DEC) {
if (percent_mode) {
percent_mode = FALSE;
operator_list[operation].op_p(&dc, &da, &db);
} else
operator_list[operation].op_f(&dc, &da, &db);
if (_finite(dc.f) == 0)
calc.is_nan = TRUE;
} else {
operator_list[operation].op_i(&dc, &da, &db);
/* apply final limiter to result */
apply_int_mask(&dc);
}
if (a->base == IDC_RADIO_DEC && b->base != IDC_RADIO_DEC) {
result->number.i = logic_dbl2int(&dc);
apply_int_mask(&result->number);
} else
if (a->base != IDC_RADIO_DEC && b->base == IDC_RADIO_DEC)
result->number.f = dc.f;
else
result->number = dc;
}
static void evalStack(calc_number_t *number)
{
calc_node_t *op, ip;
unsigned int prec;
op = pop();
ip = *op;
prec = operator_list[ip.operation].prec;
while (!is_stack_empty()) {
op = pop();
if (prec <= operator_list[op->operation].prec) {
if (op->operation == RPN_OPERATOR_PARENT) continue;
calc.prev = ip.number;
run_operator(&ip, op, &ip, op->operation);
if (calc.is_nan) {
flush_postfix();
return;
}
} else {
push(op);
break;
}
}
if (ip.operation != RPN_OPERATOR_EQUAL && ip.operation != RPN_OPERATOR_PERCENT)
push(&ip);
calc.prev_operator = op->operation;
*number = ip.number;
}
int exec_infix2postfix(calc_number_t *number, unsigned int func)
{
calc_node_t tmp;
if (is_stack_empty() && func == RPN_OPERATOR_EQUAL) {
/* if a number has been entered with exponential */
/* notation, I may update it with normal mode */
if (calc.sci_in)
return 1;
return 0;
}
if (func == RPN_OPERATOR_PERCENT)
percent_mode = TRUE;
tmp.number = *number;
tmp.base = calc.base;
tmp.operation = func;
push(&tmp);
if (func == RPN_OPERATOR_NONE)
return 0;
if (func != RPN_OPERATOR_PARENT) {
calc.last_operator = func;
evalStack(number);
}
return 1;
}
void exec_change_infix(void)
{
stack_node_t *op = stack;
if (op == NULL)
return;
if (op->node.operation == RPN_OPERATOR_PARENT ||
op->node.operation == RPN_OPERATOR_PERCENT ||
op->node.operation == RPN_OPERATOR_EQUAL)
return;
/* remove the head, it will be re-inserted with new operator */
pop();
}
void exec_closeparent(calc_number_t *number)
{
calc_node_t *op, ip;
ip.number = *number;
ip.base = calc.base;
while (!is_stack_empty()) {
op = pop();
if (op->operation == RPN_OPERATOR_PARENT)
break;
run_operator(&ip, op, &ip, op->operation);
if (calc.is_nan) {
flush_postfix();
return;
}
}
*number = ip.number;
}
int eval_parent_count(void)
{
stack_node_t *s = stack;
int n = 0;
while (s != NULL) {
if (s->node.operation == RPN_OPERATOR_PARENT)
n++;
s = (stack_node_t *)(s->next);
}
return n;
}
void flush_postfix(void)
{
while (!is_stack_empty())
pop();
/* clear prev and last typed operators */
calc.prev_operator =
calc.last_operator = 0;
}
void start_rpn_engine(void)
{
stack = NULL;
}
void stop_rpn_engine(void)
{
}