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