Added decimal/octal/binary output for integer values
[libfirm] / ir / tv / strcalc.c
1 /****i* strcalc/implementation
2  *
3  * AUTHORS
4  *    Matthias Heil
5  *
6  * NOTES
7  ******/
8
9 #include <assert.h>   /* assertions */
10 #include <string.h>   /* memset/memcmp */
11
12 #include "strcalc.h"
13
14 #include <stdio.h>    /* output for error messages */
15 #include <stdlib.h>
16
17 /*
18  * local definitions and macros
19  */
20 #define BIT_PATTERN_SIZE (8 * BIGGEST_INTEGER_SIZE_IN_BYTES)
21 #define CALC_BUFFER_SIZE (4 * BIGGEST_INTEGER_SIZE_IN_BYTES)
22 #define MAX_VALUE_SIZE   (2 * BIGGEST_INTEGER_SIZE_IN_BYTES)
23
24 #define CLEAR_CALC_BUFFER() assert(calc_buffer); memset(calc_buffer, SC_0, CALC_BUFFER_SIZE)
25 #define _val(a) ((a)-SC_0)
26 #define _digit(a) ((a)+SC_0)
27 #define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
28
29 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__,  __LINE__)
30
31 /* shortcut output for debugging only, gices always full precisition */
32 #define sc_print_hex(a) sc_print((a), 0, SC_HEX)
33 #define sc_print_dec(a) sc_print((a), 0, SC_DEC)
34 #define sc_print_oct(a) sc_print((a), 0, SC_OCT)
35 #define sc_print_bin(a) sc_print((a), 0, SC_BIN)
36
37
38 #if 0
39 #  define DEBUGPRINTF(x) printf x
40 #else
41 #  define DEBUGPRINTF(x) ((void)0)
42 #endif
43
44 /*
45  * private variables
46  */
47
48 static char calc_buffer[CALC_BUFFER_SIZE];    /* buffer holding all results */
49
50 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
51 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
52
53 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
54                               SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
55
56 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
57
58 static const char and_table[16][16] = {
59                             { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
60                               SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
61
62                             { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
63                               SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
64
65                             { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
66                               SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
67
68                             { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
69                               SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
70
71                             { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
72                               SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
73
74                             { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
75                               SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
76
77                             { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
78                               SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
79
80                             { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
81                               SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
82
83                             { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
84                               SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
85
86                             { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
87                               SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
88
89                             { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
90                               SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
91
92                             { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
93                               SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
94
95                             { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
96                               SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
97
98                             { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
99                               SC_8, SC_9, SC_8, SC_9, SC_D, SC_E, SC_D, SC_E },
100
101                             { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
102                               SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
103
104                             { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
105                               SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
106
107 static const char or_table[16][16] = {
108                             { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
109                               SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
110
111                             { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
112                               SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
113
114                             { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
115                               SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
116
117                             { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
118                               SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
119
120                             { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
121                               SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
122
123                             { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
124                               SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
125
126                             { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
127                               SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
128
129                             { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
130                               SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
131
132                             { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
133                               SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
134
135                             { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
136                               SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
137
138                             { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
139                               SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
140
141                             { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
142                               SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
143
144                             { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
145                               SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
146
147                             { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
148                               SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
149
150                             { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
151                               SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
152
153                             { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
154                               SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
155
156 static char const xor_table[16][16] = {
157                              { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
158                                SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
159
160                              { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
161                                SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
162
163                              { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
164                                SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
165
166                              { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
167                                SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
168
169                              { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
170                                SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
171
172                              { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
173                                SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
174
175                              { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
176                                SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
177
178                              { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
179                                SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
180
181                              { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
182                                SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
183
184                              { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
185                                SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
186
187                              { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
188                                SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
189
190                              { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
191                                SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
192
193                              { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
194                                SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
195
196                              { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
197                                SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
198
199                              { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
200                                SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
201
202                              { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
203                                SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
204                                 };
205
206 static char const add_table[16][16][2] = {
207                        { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
208                          {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
209                          {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
210                          {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
211
212                        { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
213                          {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
214                          {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
215                          {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
216
217                        { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
218                          {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
219                          {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
220                          {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
221
222                        { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
223                          {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
224                          {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
225                          {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
226
227                        { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
228                          {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
229                          {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
230                          {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
231
232                        { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
233                          {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
234                          {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
235                          {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
236
237                        { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
238                          {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
239                          {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
240                          {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
241
242                        { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
243                          {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
244                          {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
245                          {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
246
247                        { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
248                          {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
249                          {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
250                          {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
251
252                        { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
253                          {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
254                          {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
255                          {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
256
257                        { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
258                          {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
259                          {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
260                          {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
261
262                        { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
263                          {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
264                          {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
265                          {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
266
267                        { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
268                          {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
269                          {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
270                          {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
271
272                        { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
273                          {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
274                          {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
275                          {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
276
277                        { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
278                          {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
279                          {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
280                          {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
281
282                        { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
283                          {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
284                          {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
285                          {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
286                              };
287
288 static char const mul_table[16][16][2] = {
289                        { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
290                          {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
291                          {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
292                          {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
293
294                        { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
295                          {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
296                          {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
297                          {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
298
299                        { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
300                          {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
301                          {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
302                          {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
303
304                        { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
305                          {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
306                          {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
307                          {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
308
309                        { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
310                          {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
311                          {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
312                          {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
313
314                        { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
315                          {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
316                          {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
317                          {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
318
319                        { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
320                          {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
321                          {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
322                          {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
323
324                        { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
325                          {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
326                          {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
327                          {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
328
329                        { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
330                          {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
331                          {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
332                          {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
333
334                        { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
335                          {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
336                          {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
337                          {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
338
339                        { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
340                          {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
341                          {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
342                          {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
343
344                        { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
345                          {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
346                          {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
347                          {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
348
349                        { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
350                          {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
351                          {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
352                          {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
353
354                        { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
355                          {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
356                          {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
357                          {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
358
359                        { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
360                          {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
361                          {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
362                          {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
363
364                        { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
365                          {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
366                          {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
367                          {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
368                              };
369
370 static char const shrs_table[16][4][2] = {
371                        { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
372                        { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
373                        { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
374                        { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
375                        { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
376                        { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
377                        { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
378                        { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
379                        { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
380                        { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
381                        { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
382                        { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
383                        { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
384                        { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
385                        { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
386                        { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
387                                    };
388
389 /* for converting to binary string */
390 static const char *binary_table[16] = {
391   "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
392   "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
393 };
394
395 /*****************************************************************************
396  * private functions
397  *****************************************************************************/
398 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
399                        const char *file, int line)
400 {
401   printf("ERROR:\n");
402   printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
403   while (len-- && *str) printf("%c", *str++); printf("\n");
404   while (--pos) printf(" "); printf("^\n");
405   exit(-1);
406 }
407
408 static void _bitnot(const char *val, char *buffer)
409 {
410   int counter;
411
412   for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
413     buffer[counter] = not_table[_val(val[counter])];
414 }
415
416 static void _bitor(const char *val1, const char *val2, char *buffer)
417 {
418   int counter;
419
420   for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
421     buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
422 }
423
424 static void _bitxor(const char *val1, const char *val2, char *buffer)
425 {
426   int counter;
427
428   for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
429     buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
430 }
431
432 static void _bitand(const char *val1, const char *val2, char *buffer)
433 {
434   int counter;
435
436   for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
437     buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
438 }
439
440 static int _sign(const char *val)
441 {
442   return (val[CALC_BUFFER_SIZE-1] < SC_7) ? (1) : (-1);
443 }
444
445 static void _inc(char *val, char *buffer)
446 {
447   int counter = 0;
448
449   while (counter++ < CALC_BUFFER_SIZE)
450   {
451     if (*val == SC_F)
452     {
453       *buffer++ = SC_0;
454       val++;
455     }
456     else
457     {
458       /* No carry here, *val != SC_F */
459       *buffer = add_table[_val(*val)][SC_1][0];
460       return;
461     }
462   }
463   /* here a carry could be lost, this is intended because this will only
464    * happen when a value changes sign. */
465 }
466
467 static void _negate(const char *val, char *buffer)
468 {
469   _bitnot(val, buffer);
470   _inc(buffer, buffer);
471 }
472
473 static void _add(const char *val1, const char *val2, char *buffer)
474 {
475   int counter;
476   const char *add1, *add2;
477   char carry = SC_0;
478
479   for (counter = 0; counter < CALC_BUFFER_SIZE; counter++)
480   {
481     add1 = add_table[_val(val1[counter])][_val(val2[counter])];
482     add2 = add_table[_val(add1[0])][_val(carry)];
483     /* carry might be zero */
484     buffer[counter] = add2[0];
485     carry = add_table[_val(add1[1])][_val(add2[1])][0];
486   }
487   /* loose last carry, which will occur only when changing sign */
488 }
489
490 static void _mul(const char *val1, const char *val2, char *buffer)
491 {
492   char temp_buffer[CALC_BUFFER_SIZE]; /* result buffer */
493   char neg_val1[CALC_BUFFER_SIZE];    /* abs of val1 */
494   char neg_val2[CALC_BUFFER_SIZE];    /* abs of val2 */
495
496   const char *mul, *add1, *add2;      /* intermediate result containers */
497   char carry = SC_0;                  /* container for carries */
498   char sign = 0;                      /* marks result sign */
499   int c_inner, c_outer;               /* loop counters */
500
501   /* init result buffer to zeroes */
502   memset(temp_buffer, SC_0, CALC_BUFFER_SIZE);
503
504   /* the multiplication works only for positive values, for negative values *
505    * it is necessary to negate them and adjust the result accordingly       */
506   if (_sign(val1) == -1) {
507     _negate(val1, neg_val1);
508     val1 = neg_val1;
509     sign ^= 1;
510   }
511   if (_sign(val2) == -1) {
512     _negate(val2, neg_val2);
513     val2 = neg_val2;
514     sign ^= 1;
515   }
516
517   for (c_outer = 0; c_outer < MAX_VALUE_SIZE; c_outer++)
518   {
519     if (val2[c_outer] != SC_0)
520     {
521       for (c_inner = 0; c_inner < MAX_VALUE_SIZE; c_inner++)
522       {
523         /* do the following calculation:                                    *
524          * Add the current carry, the value at position c_outer+c_inner     *
525          * and the result of the multiplication of val1[c_inner] and        *
526          * val2[c_outer]. This is the usual pen-and-paper multiplication.   */
527
528         /* multiplicate the two digits */
529         mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
530         /* add old value to result of multiplication */
531         add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
532         /* add carry to the sum */
533         add2 = add_table[_val(add1[0])][_val(carry)];
534
535         /* all carries together result in new carry. This is always smaller *
536          * than the base b:                                                 *
537          * Both multiplicands, the carry and the value already in the temp  *
538          * buffer are single digits and their value is therefore at most    *
539          * equal to (b-1).                                                  *
540          * This leads to:                                                   *
541          * (b-1)(b-1)+(b-1)+(b-1) = b*b-1                                   *
542          * The tables list all operations rem b, so the carry is at most    *
543          * (b*b-1)rem b = -1rem b = b-1                                     */
544         carry = add_table[_val(mul[1])][_val(add1[1])][0];
545         carry = add_table[_val(carry)][_val(add2[1])][0];
546
547         temp_buffer[c_inner + c_outer] = add2[0];
548       }
549
550       /* A carry may hang over */
551       /* c_outer is always smaller than MAX_VALUE_SIZE! */
552       temp_buffer[MAX_VALUE_SIZE + c_outer] = carry;
553     }
554   }
555
556   if (sign)
557     _negate(temp_buffer, buffer);
558   else
559     memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
560 }
561
562 static void _sub(const char *val1, const char *val2, char *buffer)
563 {
564   char temp_buffer[CALC_BUFFER_SIZE];  /* intermediate buffer to hold -val2 */
565
566   _negate(val2, temp_buffer);
567   _add(val1, temp_buffer, buffer);
568 }
569
570 static void _push(const char digit, char *buffer)
571 {
572   int counter;
573
574   for (counter = CALC_BUFFER_SIZE - 2; counter >= 0; counter--)
575   {
576     buffer[counter+1] = buffer[counter];
577   }
578   buffer[0] = digit;
579 }
580
581 /* XXX: This is MOST slow */
582 static void _divmod(const char *dividend, const char *divisor, char *quot, char *rem)
583 {
584   const char *minus_divisor;
585   char neg_val1[CALC_BUFFER_SIZE];
586   char neg_val2[CALC_BUFFER_SIZE];
587
588   char sign = 0;     /* remember result sign */
589
590   int c_dividend;      /* loop counters */
591
592   /* clear result buffer */
593   memset(quot, SC_0, CALC_BUFFER_SIZE);
594   memset(rem, SC_0, CALC_BUFFER_SIZE);
595
596   /* if the dividend is zero result is zero (quot is zero)*/
597   if (sc_comp(dividend, quot) == 0)
598     return;
599   /* if the divisor is zero this won't work (quot is zero) */
600   if (sc_comp(divisor, quot) == 0) assert(0 && "quotision by zero!");
601
602   if (_sign(dividend) == -1)
603   {
604     _negate(dividend, neg_val1);
605     sign ^= 1;
606     dividend = neg_val1;
607   }
608
609   _negate(divisor, neg_val2);
610   if (_sign(divisor) == -1)
611   {
612     sign ^= 1;
613     minus_divisor = divisor;
614     divisor = neg_val2;
615   }
616   else
617   {
618     minus_divisor = neg_val2;
619   }
620
621   /* if divisor >= dividend quotision is easy
622    * (remember these are absolute values) */
623   switch (sc_comp(dividend, divisor))
624   {
625     case 0: /* dividend == divisor */
626       quot[0] = SC_1;
627       return;
628
629     case -1: /* dividend < divisor */
630       memcpy(rem, dividend, CALC_BUFFER_SIZE);
631       return;
632
633     default: /* unluckily quotision is necessary :( */
634       break;
635   }
636
637   for (c_dividend = MAX_VALUE_SIZE - 1; c_dividend >= 0; c_dividend--)
638   {
639     _push(dividend[c_dividend], rem);
640     _push(SC_0, quot);
641
642     if (sc_comp(rem, divisor) != -1)    /* remainder >= divisor */
643     {
644       /* subtract until the remainder becomes negative, this should
645        * be faster than comparing remainder with divisor  */
646       _add(rem, minus_divisor, rem);
647
648       while (_sign(rem) == 1)
649       {
650         quot[0] = add_table[_val(quot[0])][SC_1][0];
651         _add(rem, minus_divisor, rem);
652       }
653
654       /* subtracted one too much */
655       _add(rem, divisor, rem);
656     }
657   }
658
659   if (sign)
660   {
661     _negate(quot, quot);
662     _negate(rem, rem);
663   }
664 }
665
666 static void _shl(const char *val1, const char *val2, char *buffer, unsigned radius, unsigned is_signed)
667 {
668   const char *shl;
669   char shift;
670   char carry = SC_0;
671
672   int counter;
673   int offset = 0;
674   int bitoffset = 0;
675
676   assert((_sign(val2) != -1) || (0 && "negative leftshift"));
677   assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
678   assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
679   assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
680
681   /* the whole value must be moved left the number of bytes represented
682    * by the value in quot, with bytes to the right set to zero */
683   /*XXX This might result in trouble */
684   for (counter = MAX_VALUE_SIZE - 1; counter >= 0; counter--)
685   {
686     offset = (offset << 4) | (_val(val2[counter]));
687   }
688
689   shift = shift_table[_val(offset%4)];      /* this is 2 ** (val2 % 4) */
690
691   /* if shifting far enough the result is zero */
692   if (offset >= radius)
693   {
694     memset(buffer, SC_0, CALC_BUFFER_SIZE);
695     return;
696   }
697   offset = offset / 4;
698
699   /* shift the single digits some bytes (offset) and some bits (table)
700    * to the left */
701   for (counter = 0; counter < CALC_BUFFER_SIZE - offset; counter++)
702   {
703     shl = mul_table[_val(val1[counter])][_val(shift)];
704     buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
705     carry = shl[1];
706   }
707
708   /* fill with zeroes */
709   for (counter = 0; counter < offset; counter++) buffer[counter] = 0;
710   /* if the mode was signed, change sign when the mode's msb is now 1 */
711   offset = (radius - 1) / 4;
712   bitoffset = (radius - 1) % 4;
713   if (is_signed && _bitisset(buffer[offset], bitoffset) && (_sign(buffer) == 1))
714   {
715     /* this sets the upper bits of the leftmost digit */
716     buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
717     for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
718     {
719       buffer[counter] = SC_F;
720     }
721   }
722   else if (is_signed && !_bitisset(buffer[offset], bitoffset) && (_sign(buffer) == -1))
723   {
724     /* this unsets the upper bits of the leftmost digit */
725     buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
726     /* zero the upper part of the value */
727     for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
728     {
729       buffer[counter] = SC_0;
730     }
731   }
732 }
733
734 static void _shr(const char *val1, const char *val2, char *buffer, unsigned radius, unsigned is_signed, int signed_shift)
735 {
736   const char *shrs;
737   char sign;
738   char msd;
739
740   int shift;
741
742   int counter;
743   int offset = 0;
744   int bitoffset = 0;
745
746   assert((_sign(val2) != -1) || (0 && "negative rightshift"));
747   assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
748   assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
749   assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
750
751   /*XXX get the value of val2, this might result in trouble *
752    * (but who wants shifts THAT far anyway)                 */
753   for (counter = MAX_VALUE_SIZE - 1; counter >= 0; counter--)
754   {
755     offset = (offset << 4) | (_val(val2[counter]));
756   }
757
758   shift = offset % 4;     /* this is val2 % 4 */
759
760   sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
761   /* if shifting far enough the result is either 0 or -1 */
762   if (offset >= radius)
763   {
764     memset(buffer, sign, CALC_BUFFER_SIZE);
765     return;
766   }
767   offset = offset / 4;
768
769   buffer[0] = shrs_table[_val(val1[offset])][shift][0];
770   /* shift digits to the right with offset, carry and all */
771   for (counter = 1; counter < radius/4; counter++)
772   {
773     shrs = shrs_table[_val(val1[counter + offset])][shift];
774     buffer[counter] = shrs[0];
775     buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
776   }
777
778   /* the last digit is special in regard of signed/unsigned shift */
779   /* counter = radius/4 (after for loop) */
780   bitoffset = radius%4;
781   msd = val1[counter];  /* most significant digit */
782
783   /* remove sign bits if mode was signed and this is an unsigned shift */
784   if (!signed_shift && is_signed) {
785     msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
786   }
787
788   shrs = shrs_table[_val(msd)][shift];
789
790   /* signed shift and signed mode and negative value means all bits to the left are set */
791   if (signed_shift && is_signed && (_sign(val1) == -1)) {
792     buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
793   } else {
794     buffer[counter] = shrs[0];
795   }
796   buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
797
798   /* fill with SC_F or SC_0 depending on sign */
799   for (counter++; counter < CALC_BUFFER_SIZE; counter++)
800   {
801     buffer[counter] = sign;
802   }
803 }
804
805 /* positive: low-order -> high order, negative other direction */
806 static void _rot(const char *val1, const char *val2, char *buffer, unsigned radius, unsigned is_signed)
807 {
808   char temp_buffer[CALC_BUFFER_SIZE];
809
810   const char *shl;
811   char carry = SC_0;
812
813   int counter, old_counter;
814   int shift;
815   int offset = 0;
816   int bitoffset;
817
818   /*XXX get the value of val2, this might result in trouble *
819    * (but who wants shifts THAT far anyway)                 */
820   for (counter = MAX_VALUE_SIZE - 1; counter >= 0; counter--)
821   {
822     offset = (offset << 4) | (_val(val2[counter]));
823   }
824   /* rotation by multiples of the typelength is identity */
825   offset = offset % radius;
826   if (offset == 0) {
827     memmove(buffer, val1, CALC_BUFFER_SIZE);
828     return;
829   }
830   /* rotation to the right is the same as rotation to the left
831    * when done by the right amount */
832   if (offset < 0) offset = radius + offset;
833
834   shift = _val(shift_table[offset % 4]);
835   offset = offset / 4;
836
837   DEBUGPRINTF(("offset: %d, shift: %d\n", offset, shift));
838   for (counter = 0; counter < radius/4 - offset; counter++)
839   {
840     shl = mul_table[_val(val1[counter])][_val(shift)];
841     temp_buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
842     carry = shl[1];
843     DEBUGPRINTF(("%d(%x): %s\n", counter, shl[0], sc_print_hex(temp_buffer)));
844   }
845   old_counter = counter;
846   for (; counter < radius/4; counter++)
847   {
848     shl = mul_table[_val(val1[counter])][_val(shift)];
849     temp_buffer[counter - old_counter] = or_table[_val(shl[0])][_val(carry)];
850     carry = shl[1];
851     DEBUGPRINTF(("%d(%x)> %s\n", counter, shl[0], sc_print_hex(temp_buffer)));
852   }
853   temp_buffer[counter - old_counter] = or_table[_val(temp_buffer[counter-old_counter])][_val(carry)];
854
855   offset = (radius-1)/4;
856   bitoffset - (radius-1)%4;
857   /* fill the rest of the buffer depending on msb and mode signedness*/
858   if (is_signed && _bitisset(temp_buffer[offset], bitoffset))
859   {
860
861   }
862   else
863   {
864
865   }
866
867   memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
868 }
869
870 /*****************************************************************************
871  * public functions, declared in strcalc.h
872  *****************************************************************************/
873 const void *sc_get_buffer(void)
874 {
875   return (void*)calc_buffer;
876 }
877
878 const int sc_get_buffer_length(void)
879 {
880   return CALC_BUFFER_SIZE;
881 }
882
883 void sc_val_from_str(const char *str, unsigned int len)
884 {
885   const char *orig_str = str;
886   unsigned int orig_len = len;
887
888   char sign = 0;
889   char base[CALC_BUFFER_SIZE];
890   char val[CALC_BUFFER_SIZE];
891
892   /* verify valid pointers (not null) */
893   assert(str);
894   /* a string no characters long is an error */
895   assert(len);
896
897   CLEAR_CALC_BUFFER();
898   memset(base, SC_0, CALC_BUFFER_SIZE);
899   memset(val, SC_0, CALC_BUFFER_SIZE);
900
901   /* strip leading spaces */
902   while ((len > 0) && (*str == ' ')) { len--; str++; }
903
904   /* if the first two characters are 0x or 0X -> hex
905    * if the first is a 0 -> oct
906    * else dec, strip leading -/+ and remember sign
907    *
908    * only a + or - sign is no number resulting in an error */
909   if (len >= 2)
910     switch (str[0])
911     {
912       case '0':
913         if (str[1] == 'x' || str[1] == 'X') /* hex */
914         {
915           str += 2;
916           len -= 2;
917           base[1] = SC_1; base[0] = SC_0;
918         }
919         else /* oct */
920         {
921           str += 1;
922           len -= 1;
923           base[1] = SC_0; base[0] = SC_8;
924         }
925         break;
926
927       case '+':
928         {
929           str += 1;
930           len -= 1;
931           base[1] = SC_0; base[0] = SC_A;
932         }
933         break;
934
935       case '-':
936         {
937           str += 1;
938           len -= 1;
939           sign = 1;
940           base[1] = SC_0; base[0] = SC_A;
941         }
942         break;
943
944       default: /* dec, else would have begun with 0x or 0 */
945         base[1] = SC_0; base[0] = SC_A;
946     }
947
948   else /* dec, else would have begun with 0x or 0 */
949   {
950     base[1] = SC_0; base[0] = SC_A;
951   }
952
953   /* begin string evaluation, from left to right */
954   while (len > 0)
955   {
956     switch (*str)
957     {
958       case 'f':
959       case 'e':
960       case 'd':
961       case 'c':
962       case 'b':
963       case 'a':
964         if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
965         {
966           val[0] = _digit((*str)-'a'+10);
967         }
968         else fail_char(orig_str, orig_len, *str, str-orig_str+1);
969         break;
970
971       case 'F':
972       case 'E':
973       case 'D':
974       case 'C':
975       case 'B':
976       case 'A':
977         if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
978         {
979           val[0] = _digit((*str)-'A'+10);
980         }
981         else fail_char(orig_str, orig_len, *str, str-orig_str+1);
982         break;
983
984       case '9':
985       case '8':
986         if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
987         {
988           val[0] = _digit((*str)-'0');
989         }
990         else fail_char(orig_str, orig_len, *str, str-orig_str+1);
991         break;
992
993       case '7':
994       case '6':
995       case '5':
996       case '4':
997       case '3':
998       case '2':
999       case '1':
1000       case '0':
1001         {
1002           val[0] = _digit((*str)-'0');
1003         }
1004         break;
1005
1006       default:
1007         fail_char(orig_str, orig_len, *str, str-orig_str+1);
1008     } /* switch(*str) */
1009
1010     /* Radix conversion from base b to base B:
1011      *  (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1012     _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1013     _add(val, calc_buffer, calc_buffer);  /* add next digit to current value  */
1014
1015     /* get ready for the next letter */
1016     str++;
1017     len--;
1018
1019   } /* while (len > 0 ) */
1020
1021   if (sign)
1022   {
1023     _negate(calc_buffer, calc_buffer);
1024   }
1025 }
1026
1027 void sc_val_from_long(long value)
1028 {
1029   char *pos;
1030   int sign;
1031
1032   pos = calc_buffer;
1033   sign = (value < 0);
1034
1035   /* FIXME MININT won't work */
1036   if (sign) value = -value;
1037
1038   CLEAR_CALC_BUFFER();
1039
1040   while ((value != 0) && (pos < calc_buffer + CALC_BUFFER_SIZE))
1041   {
1042     *pos++ = _digit(value % 16);
1043     value /= 16;
1044   }
1045
1046   if (sign) _negate(calc_buffer, calc_buffer);
1047 }
1048
1049 long sc_val_to_long(const void *val)
1050 {
1051   int i;
1052   long l = 0;
1053
1054   for (i = CALC_BUFFER_SIZE - 1; i >= 0; i--)
1055   {
1056     l = (l << 4) + _val(((char *)val)[i]);
1057   }
1058   return l;
1059 }
1060
1061 void sc_min_from_bits(unsigned int num_bits, unsigned int sign)
1062 {
1063   char* pos;
1064   int i, bits;
1065
1066   CLEAR_CALC_BUFFER();
1067   if (!sign) return;  /* unsigned means minimum is 0(zero) */
1068
1069   pos = calc_buffer;
1070
1071   bits = num_bits - 1;
1072   for (i = 0; i < bits/4; i++)
1073     *pos++ = SC_0;
1074
1075   *pos++ = min_digit[bits%4];
1076
1077   for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1078     *pos++ = SC_F;
1079 }
1080
1081 void sc_max_from_bits(unsigned int num_bits, unsigned int sign)
1082 {
1083   char* pos;
1084   int i, bits;
1085
1086   CLEAR_CALC_BUFFER();
1087   pos = calc_buffer;
1088
1089   bits = num_bits - sign;
1090   for (i = 0; i < bits/4; i++)
1091     *pos++ = SC_F;
1092
1093   *pos++ = max_digit[bits%4];
1094
1095   for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1096     *pos++ = SC_0;
1097 }
1098
1099 void sc_calc(const void* value1, const void* value2, unsigned op)
1100 {
1101   char unused_res[CALC_BUFFER_SIZE]; /* temp buffer holding unused result of divmod */
1102
1103   const char *val1 = (const char *)value1;
1104   const char *val2 = (const char *)value2;
1105   CLEAR_CALC_BUFFER();
1106
1107   DEBUGPRINTF(("%s ", sc_print(value1, SC_HEX)));
1108
1109   switch (op)
1110   {
1111     case SC_NEG:
1112       _negate(val1, calc_buffer);
1113       DEBUGPRINTF(("negated: %s\n", sc_print_hex(calc_buffer)));
1114       return;
1115     case SC_OR:
1116       DEBUGPRINTF(("| "));
1117       _bitor(val1, val2, calc_buffer);
1118       break;
1119     case SC_AND:
1120       DEBUGPRINTF(("& "));
1121       _bitand(val1, val2, calc_buffer);
1122       break;
1123     case SC_XOR:
1124       DEBUGPRINTF(("^ "));
1125       _bitxor(val1, val2, calc_buffer);
1126       break;
1127     case SC_NOT:
1128       _bitnot(val1, calc_buffer);
1129       DEBUGPRINTF(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1130       return;
1131     case SC_ADD:
1132       DEBUGPRINTF(("+ "));
1133       _add(val1, val2, calc_buffer);
1134       break;
1135     case SC_SUB:
1136       DEBUGPRINTF(("- "));
1137       _sub(val1, val2, calc_buffer);
1138       break;
1139     case SC_MUL:
1140       DEBUGPRINTF(("* "));
1141       _mul(val1, val2, calc_buffer);
1142       break;
1143     case SC_DIV:
1144       DEBUGPRINTF(("/ "));
1145       _divmod(val1, val2, calc_buffer, unused_res);
1146       break;
1147     case SC_MOD:
1148       DEBUGPRINTF(("%% "));
1149       _divmod(val1, val2, unused_res, calc_buffer);
1150       break;
1151     default:
1152       assert(0);
1153   }
1154   DEBUGPRINTF(("%s -> ", sc_print_hex(value2)));
1155   DEBUGPRINTF(("%s\n", sc_print_hex(calc_buffer)));
1156 }
1157
1158 void sc_bitcalc(const void* value1, const void* value2, unsigned radius, unsigned sign, unsigned op)
1159 {
1160   const char *val1 = (const char *)value1;
1161   const char *val2 = (const char *)value2;
1162   CLEAR_CALC_BUFFER();
1163
1164   DEBUGPRINTF(("%s ", sc_print_hex(value1)));
1165   switch (op)
1166   {
1167     case SC_SHL:
1168       DEBUGPRINTF(("<< "));
1169       _shl(val1, val2, calc_buffer, radius, sign);
1170       break;
1171     case SC_SHR:
1172       DEBUGPRINTF((">> "));
1173       _shr(val1, val2, calc_buffer, radius, sign, 0);
1174       break;
1175     case SC_SHRS:
1176       DEBUGPRINTF((">>> "));
1177       _shr(val1, val2, calc_buffer, radius, sign, 1);
1178       break;
1179     case SC_ROT:
1180       DEBUGPRINTF(("<<>> "));
1181       _rot(val1, val2, calc_buffer, radius, sign);
1182       break;
1183     default:
1184       assert(0);
1185   }
1186   DEBUGPRINTF(("%s -> ", sc_print_hex(value2)));
1187   DEBUGPRINTF(("%s\n", sc_print_hex(calc_buffer)));
1188 }
1189
1190 int sc_comp(const void* value1, const void* value2)
1191 {
1192   int counter = CALC_BUFFER_SIZE - 1;
1193   const char *val1 = (const char *)value1;
1194   const char *val2 = (const char *)value2;
1195
1196   /* compare signs first:
1197    * the loop below can only compare values of the same sign! */
1198   if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1199
1200   /* loop until two digits differ, the values are equal if there
1201    * are no such two digits */
1202   while (val1[counter] == val2[counter])
1203   {
1204     counter--;
1205     if (counter < 0) return 0;
1206   }
1207
1208   /* the leftmost digit is the most significant, so this returns
1209    * the correct result.
1210    * This implies the digit enum is ordered */
1211   return (val1[counter] > val2[counter]) ? (1) : (-1);
1212 }
1213
1214 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1215 {
1216   const char *val     = (const char *)value;
1217   unsigned nibble_ofs = 2 * byte_ofs;
1218   unsigned char res;
1219
1220   /* the current scheme uses one byte to store a nibble */
1221   if (nibble_ofs >= len)
1222     return 0;
1223
1224   res = _val(val[nibble_ofs]);
1225   if (len > nibble_ofs + 1)
1226     res |= _val(val[nibble_ofs + 1]) << 4;
1227
1228   return res;
1229 }
1230
1231 /*
1232  * convert to a string
1233  */
1234 const char *sc_print(const void *value, unsigned bits, enum base_t base)
1235 {
1236   char base_val[CALC_BUFFER_SIZE];
1237   char div1_res[CALC_BUFFER_SIZE];
1238   char div2_res[CALC_BUFFER_SIZE];
1239   char rem_res[CALC_BUFFER_SIZE];
1240   int counter, nibbles, i;
1241   char x;
1242
1243   const char *val = (const char *)value;
1244   const char *p;
1245   char *m, *n, *t;
1246   char *pos;
1247   static char *buf = NULL;
1248
1249   if (! buf) {
1250     /* TODO: this buffer could be allocated in the initialising phase too */
1251     buf = malloc(BIT_PATTERN_SIZE + 1);
1252     if (! buf)
1253       return NULL;
1254   }
1255
1256   pos = buf + BIT_PATTERN_SIZE;
1257   *pos = '\0';
1258
1259   /* special case */
1260   if (bits == 0)
1261     bits = BIT_PATTERN_SIZE;
1262
1263   nibbles = bits >> 2;
1264   switch (base) {
1265
1266   case SC_HEX:
1267     for (counter = 0; counter < nibbles; ++counter)
1268       *(--pos) = "0123456789abcdef"[_val(val[counter])];
1269
1270     /* last nibble must be masked */
1271     if (bits & 3) {
1272       x = and_table[_val(val[++counter])][bits & 3];
1273       *(--pos) = "0123456789abcdef"[_val(x)];
1274     }
1275
1276     /* now kill zeros */
1277     for (; counter > 1; --counter, ++pos)
1278       if (pos[0] != '0')
1279         break;
1280     break;
1281
1282   case SC_BIN:
1283     for (counter = 0; counter < nibbles; ++counter) {
1284       pos -= 4;
1285       p = binary_table[_val(val[counter])];
1286       pos[0] = p[0];
1287       pos[1] = p[1];
1288       pos[2] = p[2];
1289       pos[3] = p[3];
1290     }
1291
1292     /* last nibble must be masked */
1293     if (bits & 3) {
1294       x = and_table[_val(val[++counter])][bits & 3];
1295
1296       pos -= 4;
1297       p = binary_table[_val(x)];
1298       pos[0] = p[0];
1299       pos[1] = p[1];
1300       pos[2] = p[2];
1301       pos[3] = p[3];
1302     }
1303
1304     /* now kill zeros */
1305     for (counter <<= 2; counter > 1; --counter, ++pos)
1306       if (pos[0] != '0')
1307         break;
1308     break;
1309
1310   case SC_DEC:
1311   case SC_OCT:
1312     memset(base_val, SC_0, CALC_BUFFER_SIZE);
1313     base_val[0] = base == SC_DEC ? SC_A : SC_8;
1314
1315     /* transfer data into oscilating buffers */
1316     memset(div1_res, SC_0, CALC_BUFFER_SIZE);
1317     for (counter = 0; counter < nibbles; ++counter)
1318       div1_res[counter] = val[counter];
1319
1320     /* last nibble must be masked */
1321     if (bits & 3) {
1322       ++counter;
1323
1324       div1_res[counter] = and_table[_val(val[counter])][bits & 3];
1325     }
1326
1327     m = div1_res;
1328     n = div2_res;
1329     for (;;) {
1330       _divmod(m, base_val, n, rem_res);
1331       t = m;
1332       m = n;
1333       n = t;
1334       *(--pos) = "0123456789abcdef"[_val(rem_res[0])];
1335
1336       x = 0;
1337       for (i = 0; i < sizeof(div1_res); ++i)
1338         x |= _val(m[i]);
1339
1340       if (x == 0)
1341         break;
1342     }
1343     break;
1344
1345   default:
1346     assert(0);
1347     return NULL;
1348 }
1349   return pos;
1350 }