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