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