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