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