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