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