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