-
Notifications
You must be signed in to change notification settings - Fork 3
/
print_sum.c
executable file
·521 lines (502 loc) · 9.77 KB
/
print_sum.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
This file is part of fundamental a brute force searcher
for relationships between constants & formulae for sequences.
Copyright (C) 2004 D.J. Barrow [email protected] [email protected]
It is licensed under GPL v2.1.
*/
#include "fundamental.h"
#include <stdio.h>
#include "utils.h"
#include <string.h>
#include <alloca.h>
#include <stdlib.h>
char *operator_str[num_operators+1]=
{
#ifdef HAVE_FACTORIAL_OP
"!",
#endif
#ifdef HAVE_ABS_OP
"abs",
#endif
#ifdef HAVE_FLOOR_OP
"floor",
#endif
#ifdef HAVE_CEIL_OP
"ceil",
#endif
#ifdef HAVE_RINT_OP
"rint",
#endif
#ifdef HAVE_SIN_OP
"sin",
#endif
#ifdef HAVE_COS_OP
"cos",
#endif
#ifdef HAVE_TAN_OP
"tan",
#endif
#ifdef HAVE_ONES_COMPLIMENT_OP
"compliment",
#endif
#ifdef HAVE_NOT_OP
"not",
#endif
#ifdef HAVE_ADDITION_OP
"+",
#endif
#ifdef HAVE_SUBTRACTION_OP
"-",
#endif
#ifdef HAVE_MULTIPLY_OP
"*",
#endif
#ifdef HAVE_DIVIDE_OP
"/",
#endif
#ifdef HAVE_POWER_OP
"power",
#endif
#ifdef HAVE_MODULO_OP
"%",
#endif
#ifdef HAVE_AND_OP
"and",
#endif
#ifdef HAVE_OR_OP
"or",
#endif
#ifdef HAVE_XOR_OP
"xor",
#endif
#ifdef HAVE_LOG_SHIFT_OP
"log_shift",
#endif
#ifdef HAVE_LOG_RSHIFT_OP
"log_rshift",
#endif
#ifdef HAVE_LOG_LSHIFT_OP
"log_lshift",
#endif
#ifdef HAVE_ARITH_SHIFT_OP
"arith_shift",
#endif
#ifdef HAVE_ROTATE_OP
"rotate",
#endif
#ifdef HAVE_LROTATE_OP
"lrotate",
#endif
#ifdef HAVE_RROTATE_OP
"rrotate",
#endif
#ifdef HAVE_MASK_OP
"mask",
#endif
#ifdef HAVE_GETBIT_OP
"getbit",
#endif
#ifdef HAVE_LN_OP
"ln",
#endif
#ifdef HAVE_GCD_OP
"gcd",
#endif
#ifdef HAVE_EQ_COND
"==",
#endif
#ifdef HAVE_GT_COND
">",
#endif
#ifdef HAVE_GE_COND
">=",
#endif
#ifdef HAVE_LT_COND
"<",
#endif
#ifdef HAVE_LE_COND
"<=",
#endif
#ifdef HAVE_AND_COND
"&&",
#endif
#ifdef HAVE_OR_COND
"||",
#endif
#ifdef HAVE_MIN_OP
"min",
#endif
#ifdef HAVE_MAX_OP
"max",
#endif
(char *)0xdeadbeef,
};
#if defined(MULTIPLE_RESULTS) || defined(HAVE_FUNCTIONS)
void print_sum_preamble(sum_t *sum)
{
#ifdef MULTIPLE_RESULTS
dimension_t curr_result;
#endif
#ifdef HAVE_FUNCTIONS
printf("seed=%d ",sum->seed);
#endif
#ifdef MULTIPLE_RESULTS
for(curr_result=0;curr_result<MAX_NUM_RESULTS;curr_result++)
if(retvals[curr_result]==sequence_array_size)
printf("result=%d ",curr_result);
#endif
}
}
#endif
#ifdef HAVE_PRINT_SUM_INFIX
struct infix_tree **tree_members=NULL;
#ifdef HAVE_CONSTANTS_FILE
int pass;
#endif
void recurse_sum_infix(depth_t rec_depth,sum_t *sum)
{
stack_entry *curr;
struct infix_tree *curr_tree_member;
depth_t curr_child;
operation op;
if(rec_depth>=sum->stack_depth||rec_depth<0)
return;
curr=&sum->stack[rec_depth];
curr_tree_member=tree_members[rec_depth];
#ifdef SIGNED_OPERATION
if(curr->minus)
printf("-");
#endif
switch(curr->tag)
{
#ifdef HUNTER
#ifdef HAVE_FUNCTIONS
case function_tag:
printf("f");
goto print_comma_seperated;
break;
#endif
case dimension_tag:
printf("n["DIMENSION_FORMAT"]",curr->val);
break;
#endif
#ifdef HAVE_CONSTANTS_FILE
case constant_tag:
if(pass)
printf(NUMBER_FORMAT" ",fundamental_list[curr->val]->value);
else
printf("%s",fundamental_list[curr->val]->name);
break;
#endif
case integer_tag:
printf(STACKVAL_FORMAT,curr->val);
break;
case arithmetic_operation_tag:
op=curr->val;
#ifdef HAVE_UNARY_OPERATORS
if(curr_tree_member->depth==1)
{
switch(op)
{
#ifdef HAVE_FACTORIAL_OP
case factorial_op:
printf("(");
recurse_sum_infix(curr_tree_member->child[0],sum);
printf(")%s",operator_str[curr->val]);
break;
#endif
default:
printf("%s(",operator_str[curr->val]);
recurse_sum_infix(curr_tree_member->child[0],sum);
printf(")");
break;
}
}
else
#endif
#ifdef HAVE_BINARY_OPERATORS
{
switch(op)
{
#ifdef HAVE_ADDITION_OP
case addition_op:
#endif
#ifdef HAVE_SUBTRACTION_OP
case subtraction_op:
#endif
#ifdef HAVE_MULTIPLY_OP
case multiply_op:
#endif
#ifdef HAVE_DIVIDE_OP
case divide_op:
#endif
#ifdef HAVE_MODULO_OP
case modulo_op:
#endif
#ifdef HAVE_AND_OP
case and_op:
#endif
#ifdef HAVE_OR_OP
case or_op:
#endif
#ifdef HAVE_XOR_OP
case xor_op:
#endif
#ifdef HAVE_LOG_SHIFT_OP
case log_shift_op:
#endif
#ifdef HAVE_LOG_RSHIFT_OP
case log_rshift_op:
#endif
#ifdef HAVE_LOG_LSHIFT_OP
case log_lshift_op:
#endif
#ifdef HAVE_ARITH_SHIFT_OP
case arith_shift_op:
#endif
#ifdef HAVE_ROTATE_OP
case rotate_op:
#endif
#ifdef HAVE_LROTATE_OP
case lrotate_op:
#endif
#ifdef HAVE_RROTATE_OP
case rrotate_op:
#endif
printf("(");
recurse_sum_infix(curr_tree_member->child[0],sum);
#if defined(ARITHMETIC_OPERATORS)&&defined(SIGNED_OPERATION)
if(op!=addition||!stack[curr_tree_member->child[1]].minus)
#endif
printf(" %s ",operator_str[curr->val]);
recurse_sum_infix(curr_tree_member->child[1],sum);
printf(")");
break;
default:
printf("%s",operator_str[curr->val]);
goto print_comma_seperated;
}
}
#endif
break;
}
return;
print_comma_seperated:
printf("(");
for(curr_child=0;curr_child<curr_tree_member->depth;curr_child++)
{
recurse_sum_infix(curr_tree_member->child[curr_child],sum);
if(curr_child<curr_tree_member->depth-1)
printf(",");
}
printf(")");
return;
}
void print_sum_infix(sum_t *sum)
{
struct infix_tree *curr_tree_member;
int idx,idx1;
depth_t depth;
stack_entry *curr;
memset(tree_members,0,sizeof(struct infix_tree *)*sum->stack_depth);
for(idx=0;idx<sum->stack_depth;idx++)
{
curr=&sum->stack[idx];
depth=(is_operator(curr->tag) ? get_op_depth(curr):0);
curr_tree_member=tree_members[idx]=alloca(offsetof(struct infix_tree,child[depth]));
curr_tree_member->has_parent=FALSE;
curr_tree_member->depth=depth;
for(idx1=idx-1;idx1>=0&&depth>0;idx1--)
{
if(!tree_members[idx1]->has_parent)
{
tree_members[idx1]->has_parent=TRUE;
curr_tree_member->child[--depth]=idx1;
}
}
#if 0
if(depth!=0&&debug)
exit_error("bug in print_sum_infix\n");
#endif
}
#ifdef HAVE_CONSTANTS_FILE
for(pass=0;pass<(fundamental_list ? 2:1);pass++)
#endif
{
recurse_sum_infix(sum->stack_depth-1,sum);
printf("\n");
}
}
#endif
#ifdef HAVE_PRINT_SUM_RPN
void print_sum_rpn(sum_t *sum)
{
int idx
#ifdef HAVE_CONSTANTS_FILE
,pass
#endif
;
stack_entry *curr;
#ifdef HAVE_CONSTANTS_FILE
for(pass=0;pass<(fundamental_list ? 2:1);pass++)
#endif
{
for(idx=0;idx<sum->stack_depth;idx++)
{
curr=&sum->stack[idx];
#ifdef SIGNED_OPERATION
if(curr->minus)
printf("-");
#endif
switch(curr->tag)
{
#ifdef HUNTER
#ifdef HAVE_FUNCTIONS
case function_tag:
printf("f ");
break;
#endif
case dimension_tag:
printf("n["DIMENSION_FORMAT"] ",curr->val);
break;
#endif
#ifdef HAVE_CONSTANTS_FILE
case constant_tag:
if(pass)
printf(NUMBER_FORMAT" ",fundamental_list[curr->val]->value);
else
printf("%s ",fundamental_list[curr->val]->name);
break;
#endif
case integer_tag:
printf(STACKVAL_FORMAT" ",curr->val);
break;
case arithmetic_operation_tag:
printf(" %s ",operator_str[curr->val]);
break;
}
}
}
printf("\n");
}
#endif
void print_sum_postamble(sum_t *sum)
{
if(max_num_answers!=-1)
{
num_answers++;
if(num_answers>=max_num_answers)
exit(0);
}
}
void print_sum(sum_t *sum)
{
#if 0
print_sum_preamble(sum);
#endif
print_sum_rpn(sum);
print_sum_infix(sum);
print_sum_postamble(sum);
}
#ifdef HAVE_ERROR_MEASUREMENTS
char *error_measurment_str[NUM_ERROR_MEASUREMENTS]=
{
#ifdef HAVE_LMS_ERROR
"lms",
#endif
#ifdef HAVE_ABS_ERROR
"abs",
#endif
#ifdef HAVE_LMS_RATIOED_ERROR
"lms ratioed",
#endif
#ifdef HAVE_ABS_RATIOED_ERROR
"abs ratioed",
#endif
#ifdef HAVE_LMS_GT_ERROR
"lms gt",
#endif
#ifdef HAVE_GT_ERROR
"gt",
#endif
#ifdef HAVE_LMS_GT_RATIOED_ERROR
"lms gt ratioed",
#endif
#ifdef HAVE_GT_RATIOED_ERROR
"gt ratioed",
#endif
#ifdef HAVE_LMS_LT_ERROR
"lms lt",
#endif
#ifdef HAVE_LT_ERROR
"lt error",
#endif
#ifdef HAVE_LMS_LT_RATIOED_ERROR
"lms lt ratioed",
#endif
#ifdef HAVE_LT_RATIOED_ERROR
"lt ratioed",
#endif
};
#ifdef HAVE_DEBUG_ERROR_LIST
void debug_error_list_func(calculate_sum_result *retval)
{
result_t testvals=*get_array_member(array_indices);
#ifdef MULTIPLE_RESULTS
int curr_answer,num_answers=
#ifdef NUM_ANSWERS
NUM_ANSWERS
#else
testvals->num_answers
#endif
;
number_t *answers=
#if (!defined(NUM_ANSWERS)||defined(SPARSE_ARRAY_INDICES))
&testvals->answer[0];
#else
&testvals.answer[0];
#endif
for(curr_answer=0;curr_answer<num_answers;curr_answer++)
#endif /* MULTIPLE_RESULTS */
{
printf("result="NUMBER_FORMAT" expected="NUMBER_FORMAT"\n",
result_stack[0],
#ifdef MULTIPLE_RESULTS
*answers++
#else
testvals
#endif
);
}
}
#endif
void print_error_measurements()
{
int idx;
for(idx=0;idx<NUM_ERROR_MEASUREMENTS;idx++)
{
struct list_head *curr_error_list=&error_list[idx];
error_list_element *element;
if(!list_empty(curr_error_list))
{
printf("%s error list\n",error_measurment_str[idx]);
list_for_each(((struct list_head *)element),curr_error_list)
{
printf("error="ERROR_FORMAT"\n",element->error_val);
print_sum(&element->sum);
#ifdef HAVE_DEBUG_ERROR_LIST
{
calculate_sum_result result=calculate_sum(&element->sum,debug_error_list_func);
if(result.aborted)
{
fprintf(stderr,"BUG DEBUG_ERROR_LIST sum is illegal");
print_sum(&element->sum);
exit(-1);
}
}
#endif
}
}
}
}
#endif