- renamed a lot of parameters to more sensible names
[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         printf("ERROR:\n");
441         printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
442         while (len-- && *str) printf("%c", *str++); printf("\n");
443         while (--pos) printf(" "); printf("^\n");
444         exit(-1);
445 }
446
447 /**
448  * implements the bitwise NOT operation
449  */
450 static void _bitnot(const char *val, char *buffer) {
451         int counter;
452
453         for (counter = 0; counter<calc_buffer_size; counter++)
454                 buffer[counter] = not_table[_val(val[counter])];
455 }
456
457 /**
458  * implements the bitwise OR operation
459  */
460 static void _bitor(const char *val1, const char *val2, char *buffer) {
461         int counter;
462
463         for (counter = 0; counter<calc_buffer_size; counter++)
464                 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
465 }
466
467 /**
468  * implements the bitwise eXclusive OR operation
469  */
470 static void _bitxor(const char *val1, const char *val2, char *buffer) {
471         int counter;
472
473         for (counter = 0; counter<calc_buffer_size; counter++)
474                 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
475 }
476
477 /**
478  * implements the bitwise AND operation
479  */
480 static void _bitand(const char *val1, const char *val2, char *buffer) {
481         int counter;
482
483         for (counter = 0; counter<calc_buffer_size; counter++)
484                 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
485 }
486
487 /**
488  * returns the sign bit.
489  *
490  * @todo This implementation is wrong, as it returns the highest bit of the buffer
491  *       NOT the highest bit depending on the real mode
492  */
493 static int _sign(const char *val) {
494         return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
495 }
496
497 /**
498  * returns non-zero if bit at position pos is set
499  */
500 static int _bit(const char *val, int pos) {
501         int bit    = pos & 3;
502         int nibble = pos >> 2;
503
504         return _bitisset(val[nibble], bit);
505 }
506
507 /**
508  * Implements a fast ADD + 1
509  */
510 static void _inc(const char *val, char *buffer) {
511         int counter = 0;
512
513         while (counter++ < calc_buffer_size) {
514                 if (*val == SC_F) {
515                         *buffer++ = SC_0;
516                         val++;
517                 } else {
518                         /* No carry here, *val != SC_F */
519                         *buffer = add_table[_val(*val)][SC_1][0];
520                         return;
521                 }
522         }
523         /* here a carry could be lost, this is intended because this should
524          * happen only when a value changes sign. */
525 }
526
527 /**
528  * Implements a unary MINUS
529  */
530 static void _negate(const char *val, char *buffer) {
531         _bitnot(val, buffer);
532         _inc(buffer, buffer);
533 }
534
535 /**
536  * Implements a binary ADD
537  *
538  * @todo The implementation of carry is wrong, as it is the
539  *       calc_buffer_size carry, not the mode depending
540  */
541 static void _add(const char *val1, const char *val2, char *buffer) {
542         int counter;
543         const char *add1, *add2;
544         char carry = SC_0;
545
546         for (counter = 0; counter < calc_buffer_size; counter++)   {
547                 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
548                 add2 = add_table[_val(add1[0])][_val(carry)];
549                 /* carry might be zero */
550                 buffer[counter] = add2[0];
551                 carry = add_table[_val(add1[1])][_val(add2[1])][0];
552         }
553         carry_flag = carry != SC_0;
554 }
555
556 /**
557  * Implements a binary SUB
558  */
559 static void _sub(const char *val1, const char *val2, char *buffer) {
560         char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
561
562         _negate(val2, temp_buffer);
563         _add(val1, temp_buffer, buffer);
564 }
565
566 /**
567  * Implements a binary MUL
568  */
569 static void _mul(const char *val1, const char *val2, char *buffer) {
570         char *temp_buffer; /* result buffer */
571         char *neg_val1;    /* abs of val1 */
572         char *neg_val2;    /* abs of val2 */
573
574         const char *mul, *add1, *add2;      /* intermediate result containers */
575         char carry = SC_0;                  /* container for carries */
576         char sign = 0;                      /* marks result sign */
577         int c_inner, c_outer;               /* loop counters */
578
579         temp_buffer = alloca(calc_buffer_size);
580         neg_val1 = alloca(calc_buffer_size);
581         neg_val2 = alloca(calc_buffer_size);
582
583         /* init result buffer to zeroes */
584         memset(temp_buffer, SC_0, calc_buffer_size);
585
586         /* the multiplication works only for positive values, for negative values *
587          * it is necessary to negate them and adjust the result accordingly       */
588         if (_sign(val1) == -1) {
589                 _negate(val1, neg_val1);
590                 val1 = neg_val1;
591                 sign ^= 1;
592         }
593         if (_sign(val2) == -1) {
594                 _negate(val2, neg_val2);
595                 val2 = neg_val2;
596                 sign ^= 1;
597         }
598
599         for (c_outer = 0; c_outer < max_value_size; c_outer++) {
600                 if (val2[c_outer] != SC_0) {
601                         for (c_inner = 0; c_inner < max_value_size; c_inner++) {
602                                 /* do the following calculation:                                    *
603                                  * Add the current carry, the value at position c_outer+c_inner     *
604                                  * and the result of the multiplication of val1[c_inner] and        *
605                                  * val2[c_outer]. This is the usual pen-and-paper multiplication.   */
606
607                                 /* multiplicate the two digits */
608                                 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
609                                 /* add old value to result of multiplication */
610                                 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
611                                 /* add carry to the sum */
612                                 add2 = add_table[_val(add1[0])][_val(carry)];
613
614                                 /* all carries together result in new carry. This is always smaller *
615                                  * than the base b:                                                 *
616                                  * Both multiplicands, the carry and the value already in the temp  *
617                                  * buffer are single digits and their value is therefore at most    *
618                                  * equal to (b-1).                                                  *
619                                  * This leads to:                                                   *
620                                  * (b-1)(b-1)+(b-1)+(b-1) = b*b-1                                   *
621                                  * The tables list all operations rem b, so the carry is at most    *
622                                  * (b*b-1)rem b = -1rem b = b-1                                     */
623                                 carry = add_table[_val(mul[1])][_val(add1[1])][0];
624                                 carry = add_table[_val(carry)][_val(add2[1])][0];
625
626                                 temp_buffer[c_inner + c_outer] = add2[0];
627                         }
628
629                         /* A carry may hang over */
630                         /* c_outer is always smaller than max_value_size! */
631                         temp_buffer[max_value_size + c_outer] = carry;
632                         carry = SC_0;
633                 }
634         }
635
636         if (sign)
637                 _negate(temp_buffer, buffer);
638         else
639                 memcpy(buffer, temp_buffer, calc_buffer_size);
640 }
641
642 /**
643  * Shift the buffer to left and add a 4 bit digit
644  */
645 static void _push(const char digit, char *buffer) {
646         int counter;
647
648         for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
649                 buffer[counter+1] = buffer[counter];
650         }
651         buffer[0] = digit;
652 }
653
654 /**
655  * Implements truncating integer division and remainder.
656  *
657  * Note: This is MOST slow
658  */
659 static void _divmod(const char *rDividend, const char *divisor, char *quot, char *rem) {
660         const char *dividend = rDividend;
661         const char *minus_divisor;
662         char *neg_val1;
663         char *neg_val2;
664
665         char div_sign = 0;     /* remember division result sign */
666         char rem_sign = 0;     /* remember remainder result sign */
667
668         int c_dividend;      /* loop counters */
669
670         neg_val1 = alloca(calc_buffer_size);
671         neg_val2 = alloca(calc_buffer_size);
672
673         /* clear result buffer */
674         memset(quot, SC_0, calc_buffer_size);
675         memset(rem, SC_0, calc_buffer_size);
676
677         /* if the divisor is zero this won't work (quot is zero) */
678         if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
679
680         /* if the dividend is zero result is zero (quot is zero) */
681         if (sc_comp(dividend, quot) == 0)
682                 return;
683
684         if (_sign(dividend) == -1) {
685                 _negate(dividend, neg_val1);
686                 div_sign ^= 1;
687                 rem_sign ^= 1;
688                 dividend = neg_val1;
689         }
690
691         _negate(divisor, neg_val2);
692         if (_sign(divisor) == -1) {
693                 div_sign ^= 1;
694                 minus_divisor = divisor;
695                 divisor = neg_val2;
696         } else
697                 minus_divisor = neg_val2;
698
699         /* if divisor >= dividend division is easy
700          * (remember these are absolute values) */
701         switch (sc_comp(dividend, divisor)) {
702         case 0: /* dividend == divisor */
703                 quot[0] = SC_1;
704                 goto end;
705
706         case -1: /* dividend < divisor */
707                 memcpy(rem, rDividend, calc_buffer_size);
708                 goto end;
709
710         default: /* unluckily division is necessary :( */
711                 break;
712         }
713
714         for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
715                 _push(dividend[c_dividend], rem);
716                 _push(SC_0, quot);
717
718                 if (sc_comp(rem, divisor) != -1) {  /* remainder >= divisor */
719                         /* subtract until the remainder becomes negative, this should
720                          * be faster than comparing remainder with divisor  */
721                         _add(rem, minus_divisor, rem);
722
723                         while (_sign(rem) == 1) {
724                                 quot[0] = add_table[_val(quot[0])][SC_1][0];
725                                 _add(rem, minus_divisor, rem);
726                         }
727
728                         /* subtracted one too much */
729                         _add(rem, divisor, rem);
730                 }
731         }
732 end:
733         /* sets carry if remainder is non-zero ??? */
734         carry_flag = !sc_is_zero(rem);
735
736         if (div_sign)
737                 _negate(quot, quot);
738
739         if (rem_sign)
740                 _negate(rem, rem);
741 }
742
743 /**
744  * Implements a Shift Left, which can either preserve the sign bit
745  * or not.
746  *
747  * @todo Assertions seems to be wrong
748  */
749 static void _shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed) {
750         const char *shl;
751         char shift;
752         char carry = SC_0;
753
754         int counter;
755         int bitoffset = 0;
756
757         assert((shift_cnt >= 0) || (0 && "negative leftshift"));
758         assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
759         assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
760         assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
761
762         /* if shifting far enough the result is zero */
763         if (shift_cnt >= bitsize) {
764                 memset(buffer, SC_0, calc_buffer_size);
765                 return;
766         }
767
768         shift = shift_table[_val(shift_cnt%4)];      /* this is 2 ** (offset % 4) */
769         shift_cnt = shift_cnt / 4;
770
771         /* shift the single digits some bytes (offset) and some bits (table)
772          * to the left */
773         for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
774                 shl = mul_table[_val(val1[counter])][_val(shift)];
775                 buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
776                 carry = shl[1];
777         }
778         if (bitsize%4 > 0) {
779                 shl = mul_table[_val(val1[counter])][_val(shift)];
780                 buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
781                 bitoffset = counter;
782         } else {
783                 bitoffset = counter - 1;
784         }
785
786         /* fill with zeroes */
787         for (counter = 0; counter < shift_cnt; counter++)
788                 buffer[counter] = SC_0;
789
790         /* if the mode was signed, change sign when the mode's msb is now 1 */
791         shift_cnt = bitoffset + shift_cnt;
792         bitoffset = (bitsize-1) % 4;
793         if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
794                 /* this sets the upper bits of the leftmost digit */
795                 buffer[shift_cnt] = or_table[_val(buffer[shift_cnt])][_val(min_digit[bitoffset])];
796                 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
797                         buffer[counter] = SC_F;
798                 }
799         } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
800                 /* this clears the upper bits of the leftmost digit */
801                 buffer[shift_cnt] = and_table[_val(buffer[shift_cnt])][_val(max_digit[bitoffset])];
802                 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
803                         buffer[counter] = SC_0;
804                 }
805         }
806 }
807
808 /**
809  * Implements a Shift Right, which can either preserve the sign bit
810  * or not.
811  *
812  * @param bitsize   bitsize of the value to be shifted
813  *
814  * @todo Assertions seems to be wrong
815  */
816 static void _shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift) {
817         const char *shrs;
818         char sign;
819         char msd;
820
821         int shift_mod, shift_nib;
822
823         int counter;
824         int bitoffset = 0;
825
826         assert((shift_cnt >= 0) || (0 && "negative rightshift"));
827         assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
828         assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
829
830         sign = signed_shift && _bit(val1, bitsize - 1) ? SC_F : SC_0;
831
832         /* if shifting far enough the result is either 0 or -1 */
833         if (shift_cnt >= bitsize) {
834                 if (!sc_is_zero(val1)) {
835                         carry_flag = 1;
836                 }
837                 memset(buffer, sign, calc_buffer_size);
838                 return;
839         }
840
841         shift_mod = shift_cnt &  3;
842         shift_nib = shift_cnt >> 2;
843
844         /* check if any bits are lost, and set carry_flag if so */
845         for (counter = 0; counter < shift_nib; ++counter) {
846                 if (val1[counter] != 0) {
847                         carry_flag = 1;
848                         break;
849                 }
850         }
851         if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
852                 carry_flag = 1;
853
854         /* shift digits to the right with offset, carry and all */
855         counter = 0;
856         if ((bitsize >> 2) > shift_nib) {
857                 buffer[counter] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
858                 counter = 1;
859         }
860         for (; counter < bitsize/4 - shift_nib; counter++) {
861                 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
862                 buffer[counter] = shrs[0];
863                 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
864         }
865
866         /* the last digit is special in regard of signed/unsigned shift */
867         bitoffset = bitsize & 3;
868         msd = sign;  /* most significant digit */
869
870         /* remove sign bits if mode was signed and this is an unsigned shift */
871         if (!signed_shift && is_signed) {
872                 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
873         }
874
875         shrs = shrs_table[_val(msd)][shift_mod];
876
877         /* signed shift and signed mode and negative value means all bits to the left are set */
878         if (signed_shift && sign == SC_F) {
879                 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
880         } else {
881                 buffer[counter] = shrs[0];
882         }
883
884         if (counter > 0)
885                 buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
886
887         /* fill with SC_F or SC_0 depending on sign */
888         for (counter++; counter < calc_buffer_size; counter++) {
889                 buffer[counter] = sign;
890         }
891 }
892
893 /**
894  * Implements a Rotate Right.
895  * positive: low-order -> high order, negative other direction
896  */
897 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed) {
898         char *temp1, *temp2;
899         temp1 = alloca(calc_buffer_size);
900         temp2 = alloca(calc_buffer_size);
901
902         offset = offset % radius;
903
904         /* rotation by multiples of the type length is identity */
905         if (offset == 0) {
906                 memmove(buffer, val1, calc_buffer_size);
907                 return;
908         }
909
910         _shl(val1, temp1, offset, radius, is_signed);
911         _shr(val1, temp2, radius - offset, radius, is_signed, 0);
912         _bitor(temp1, temp2, buffer);
913         carry_flag = 0; /* set by shr, but due to rot this is false */
914 }
915
916 /*****************************************************************************
917  * public functions, declared in strcalc.h
918  *****************************************************************************/
919 const void *sc_get_buffer(void) {
920         return (void*)calc_buffer;
921 }
922
923 int sc_get_buffer_length(void) {
924         return calc_buffer_size;
925 }
926
927 /**
928  * Do sign extension if the mode is signed, otherwise to zero extension.
929  */
930 void sign_extend(void *buffer, ir_mode *mode) {
931         char *calc_buffer = buffer;
932         int bits          = get_mode_size_bits(mode) - 1;
933         int nibble        = bits >> 2;
934         int max           = max_digit[bits & 3];
935         int i;
936
937         if (mode_is_signed(mode)) {
938                 if (calc_buffer[nibble] > max) {
939                         /* sign bit is set, we need sign expansion */
940
941                         for (i = nibble + 1; i < calc_buffer_size; ++i)
942                                 calc_buffer[i] = SC_F;
943                         calc_buffer[nibble] = or_table[(int)calc_buffer[nibble]][(int)sex_digit[bits & 3]];
944                 } else {
945                         /* set all bits to zero */
946                         for (i = nibble + 1; i < calc_buffer_size; ++i)
947                                 calc_buffer[i] = SC_0;
948                         calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
949                 }
950         } else {
951                 /* do zero extension */
952                 for (i = nibble + 1; i < calc_buffer_size; ++i)
953                         calc_buffer[i] = SC_0;
954                 calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
955         }
956 }
957
958 /* FIXME doesn't check for overflows */
959 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode) {
960         const char *orig_str = str;
961         unsigned int orig_len = len;
962
963         char sign = 0;
964         char *base, *val;
965
966         base = alloca(calc_buffer_size);
967         val = alloca(calc_buffer_size);
968
969         /* verify valid pointers (not null) */
970         assert(str);
971         /* a string no characters long is an error */
972         assert(len);
973
974         if (buffer == NULL) buffer = calc_buffer;
975
976         CLEAR_BUFFER(buffer);
977         CLEAR_BUFFER(base);
978         CLEAR_BUFFER(val);
979
980         /* strip leading spaces */
981         while ((len > 0) && (*str == ' ')) { len--; str++; }
982
983         /* if the first two characters are 0x or 0X -> hex
984          * if the first is a 0 -> oct
985          * else dec, strip leading -/+ and remember sign
986          *
987          * only a + or - sign is no number resulting in an error */
988         if (len >= 2) {
989                 switch (str[0]) {
990                 case '0':
991                         if (str[1] == 'x' || str[1] == 'X') { /* hex */
992                                 str += 2;
993                                 len -= 2;
994                                 base[1] = SC_1; base[0] = SC_0;
995                         } else { /* oct */
996                                 str += 1;
997                                 len -= 1;
998                                 base[1] = SC_0; base[0] = SC_8;
999                         }
1000                         break;
1001
1002                 case '+':
1003                         str += 1;
1004                         len -= 1;
1005                         base[1] = SC_0; base[0] = SC_A;
1006                         break;
1007
1008                 case '-':
1009                         str += 1;
1010                         len -= 1;
1011                         sign = 1;
1012                         base[1] = SC_0; base[0] = SC_A;
1013                         break;
1014
1015                 default: /* dec, else would have begun with 0x or 0 */
1016                         base[1] = SC_0; base[0] = SC_A;
1017                 }
1018         } else { /* dec, else would have begun with 0x or 0 */
1019                 base[1] = SC_0; base[0] = SC_A;
1020         }
1021
1022         /* BEGIN string evaluation, from left to right */
1023         while (len > 0) {
1024                 switch (*str) {
1025                 case 'f':
1026                 case 'e':
1027                 case 'd':
1028                 case 'c':
1029                 case 'b':
1030                 case 'a':
1031                         if (base[0] > SC_9 || base[1] > SC_0) { /* (base > 10) */
1032                                 val[0] = _digit((*str)-'a'+10);
1033                         }
1034                         else
1035                                 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1036                         break;
1037
1038                 case 'F':
1039                 case 'E':
1040                 case 'D':
1041                 case 'C':
1042                 case 'B':
1043                 case 'A':
1044                         if (base[0] > SC_9 || base[1] > SC_0) { /* (base > 10) */
1045                                 val[0] = _digit((*str)-'A'+10);
1046                         }
1047                         else
1048                                 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1049                         break;
1050
1051                 case '9':
1052                 case '8':
1053                         if (base[0] > SC_7 || base[1] > SC_0) { /* (base > 8) */
1054                                 val[0] = _digit((*str)-'0');
1055                         }
1056                         else
1057                                 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1058                         break;
1059
1060                 case '7':
1061                 case '6':
1062                 case '5':
1063                 case '4':
1064                 case '3':
1065                 case '2':
1066                 case '1':
1067                 case '0':
1068                         val[0] = _digit((*str)-'0');
1069                         break;
1070
1071                 default:
1072                         fail_char(orig_str, orig_len, *str, str-orig_str+1);
1073                 } /* switch(*str) */
1074
1075                 /* Radix conversion from base b to base B:
1076                  *  (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1077                 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1078                 _add(val, calc_buffer, calc_buffer);  /* add next digit to current value  */
1079
1080                 /* get ready for the next letter */
1081                 str++;
1082                 len--;
1083         } /* while (len > 0 ) */
1084
1085         if (sign)
1086                 _negate(calc_buffer, calc_buffer);
1087
1088         /* beware: even if hex numbers have no sign, we need sign extension here */
1089         sign_extend(calc_buffer, mode);
1090 }
1091
1092 void sc_val_from_long(long value, void *buffer) {
1093         char *pos;
1094         char sign, is_minlong;
1095
1096         if (buffer == NULL) buffer = calc_buffer;
1097         pos = buffer;
1098
1099         sign = (value < 0);
1100         is_minlong = value == LONG_MIN;
1101
1102         /* use absolute value, special treatment of MIN_LONG to avoid overflow */
1103         if (sign) {
1104                 if (is_minlong)
1105                         value = -(value+1);
1106                 else
1107                         value = -value;
1108         }
1109
1110         CLEAR_BUFFER(buffer);
1111
1112         while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
1113                 *pos++ = _digit(value & 0xf);
1114                 value >>= 4;
1115         }
1116
1117         if (sign) {
1118                 if (is_minlong)
1119                         _inc(buffer, buffer);
1120
1121                 _negate(buffer, buffer);
1122         }
1123 }
1124
1125 void sc_val_from_ulong(unsigned long value, void *buffer) {
1126         unsigned char *pos;
1127
1128         if (buffer == NULL) buffer = calc_buffer;
1129         pos = buffer;
1130
1131         while (pos < (unsigned char *)buffer + calc_buffer_size) {
1132                 *pos++ = (unsigned char)_digit(value & 0xf);
1133                 value >>= 4;
1134         }
1135 }
1136
1137 long sc_val_to_long(const void *val) {
1138         int i;
1139         long l = 0;
1140
1141         for (i = calc_buffer_size - 1; i >= 0; i--) {
1142                 l = (l << 4) + _val(((char *)val)[i]);
1143         }
1144         return l;
1145 }
1146
1147 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1148         char *pos;
1149         int i, bits;
1150
1151         if (buffer == NULL) buffer = calc_buffer;
1152         CLEAR_BUFFER(buffer);
1153
1154         if (!sign) return;  /* unsigned means minimum is 0(zero) */
1155
1156         pos = buffer;
1157
1158         bits = num_bits - 1;
1159         for (i = 0; i < bits/4; i++)
1160                 *pos++ = SC_0;
1161
1162         *pos++ = min_digit[bits%4];
1163
1164         for (i++; i <= calc_buffer_size - 1; i++)
1165                 *pos++ = SC_F;
1166 }
1167
1168 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1169         char* pos;
1170         int i, bits;
1171
1172         if (buffer == NULL) buffer = calc_buffer;
1173         CLEAR_BUFFER(buffer);
1174         pos = buffer;
1175
1176         bits = num_bits - sign;
1177         for (i = 0; i < bits/4; i++)
1178                 *pos++ = SC_F;
1179
1180         *pos++ = max_digit[bits%4];
1181
1182         for (i++; i <= calc_buffer_size - 1; i++)
1183                 *pos++ = SC_0;
1184 }
1185
1186 void sc_truncate(unsigned int num_bits, void *buffer) {
1187         char *cbuffer = buffer;
1188         char *pos = cbuffer + (num_bits / 4);
1189         char *end = cbuffer + calc_buffer_size;
1190
1191         assert(pos < end);
1192
1193         switch(num_bits % 4) {
1194         case 0: /* nothing to do */ break;
1195         case 1: *pos = and_table[_val(*pos)][SC_1]; pos++; break;
1196         case 2: *pos = and_table[_val(*pos)][SC_3]; pos++; break;
1197         case 3: *pos = and_table[_val(*pos)][SC_7]; pos++; break;
1198         }
1199
1200         for( ; pos < end; ++pos)
1201                 *pos = SC_0;
1202 }
1203
1204 int sc_comp(const void* value1, const void* value2) {
1205         int counter = calc_buffer_size - 1;
1206         const char *val1 = (const char *)value1;
1207         const char *val2 = (const char *)value2;
1208
1209         /* compare signs first:
1210          * the loop below can only compare values of the same sign! */
1211         if (_sign(val1) != _sign(val2))
1212                 return (_sign(val1) == 1)?(1):(-1);
1213
1214         /* loop until two digits differ, the values are equal if there
1215          * are no such two digits */
1216         while (val1[counter] == val2[counter]) {
1217                 counter--;
1218                 if (counter < 0) return 0;
1219         }
1220
1221         /* the leftmost digit is the most significant, so this returns
1222          * the correct result.
1223          * This implies the digit enum is ordered */
1224         return (val1[counter] > val2[counter]) ? (1) : (-1);
1225 }
1226
1227 int sc_get_highest_set_bit(const void *value) {
1228         const char *val = (const char*)value;
1229         int high, counter;
1230
1231         high = calc_buffer_size * 4 - 1;
1232
1233         for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1234                 if (val[counter] == SC_0)
1235                         high -= 4;
1236                 else {
1237                         if (val[counter] > SC_7) return high;
1238                         else if (val[counter] > SC_3) return high - 1;
1239                         else if (val[counter] > SC_1) return high - 2;
1240                         else return high - 3;
1241                 }
1242         }
1243         return high;
1244 }
1245
1246 int sc_get_lowest_set_bit(const void *value) {
1247         const char *val = (const char*)value;
1248         int low, counter;
1249
1250         low = 0;
1251         for (counter = 0; counter < calc_buffer_size; counter++) {
1252                 switch (val[counter]) {
1253                 case SC_1:
1254                 case SC_3:
1255                 case SC_5:
1256                 case SC_7:
1257                 case SC_9:
1258                 case SC_B:
1259                 case SC_D:
1260                 case SC_F:
1261                         return low;
1262                 case SC_2:
1263                 case SC_6:
1264                 case SC_A:
1265                 case SC_E:
1266                         return low + 1;
1267                 case SC_4:
1268                 case SC_C:
1269                         return low + 2;
1270                 case SC_8:
1271                         return low + 3;
1272                 default:
1273                         low += 4;
1274                 }
1275         }
1276         return -1;
1277 }
1278
1279 int sc_is_zero(const void *value) {
1280         const char* val = (const char *)value;
1281         int counter;
1282
1283         for (counter = 0; counter < calc_buffer_size; ++counter) {
1284                 if (val[counter] != SC_0)
1285                         return 0;
1286         }
1287         return 1;
1288 }
1289
1290 int sc_is_negative(const void *value) {
1291         return _sign(value) == -1;
1292 }
1293
1294 int sc_had_carry(void) {
1295         return carry_flag;
1296 }
1297
1298 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs) {
1299         const char *val = (const char *)value;
1300         int nibble_ofs  = 2 * byte_ofs;
1301         unsigned char res;
1302
1303         /* the current scheme uses one byte to store a nibble */
1304         if (4 * nibble_ofs >= len)
1305                 return 0;
1306
1307         res = _val(val[nibble_ofs]);
1308         if (len > 4 * (nibble_ofs + 1))
1309                 res |= _val(val[nibble_ofs + 1]) << 4;
1310
1311         /* kick bits outsize */
1312         if (len < (int) (8*byte_ofs)) {
1313                 res &= 0xFF >> (8*byte_ofs - len);
1314         }
1315         return res;
1316 }
1317
1318 /*
1319  * convert to a string
1320  * FIXME: Doesn't check buffer bounds
1321  */
1322 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode) {
1323         static const char big_digits[]   = "0123456789ABCDEF";
1324         static const char small_digits[] = "0123456789abcdef";
1325
1326         char *base_val, *div1_res, *div2_res, *rem_res;
1327         int counter, nibbles, i, sign;
1328         char x;
1329
1330         const char *val = (const char *)value;
1331         const char *p;
1332         char *m, *n, *t;
1333         char *pos;
1334         const char *digits = small_digits;
1335
1336         base_val = alloca(calc_buffer_size);
1337         div1_res = alloca(calc_buffer_size);
1338         div2_res = alloca(calc_buffer_size);
1339         rem_res  = alloca(calc_buffer_size);
1340
1341         pos = output_buffer + bit_pattern_size;
1342         *(--pos) = '\0';
1343
1344         /* special case */
1345         if (bits == 0) {
1346                 bits = bit_pattern_size;
1347 #ifdef STRCALC_DEBUG_FULLPRINT
1348                 bits <<= 1;
1349 #endif
1350         }
1351         nibbles = bits >> 2;
1352         switch (base) {
1353
1354         case SC_HEX:
1355                 digits = big_digits;
1356         case SC_hex:
1357                 for (counter = 0; counter < nibbles; ++counter) {
1358                         *(--pos) = digits[_val(val[counter])];
1359 #ifdef STRCALC_DEBUG_GROUPPRINT
1360                         if ((counter+1)%8 == 0)
1361                                 *(--pos) = ' ';
1362 #endif
1363                 }
1364
1365                 /* last nibble must be masked */
1366                 if (bits & 3) {
1367                         x = and_table[_val(val[++counter])][bits & 3];
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                         x = and_table[_val(val[++counter])][bits & 3];
1394
1395                         pos -= 4;
1396                         p = binary_table[_val(x)];
1397                         pos[0] = p[0];
1398                         pos[1] = p[1];
1399                         pos[2] = p[2];
1400                         pos[3] = p[3];
1401                 }
1402
1403                 /* now kill zeros */
1404                 for (counter <<= 2; counter > 1; --counter, ++pos)
1405                         if (pos[0] != '0')
1406                                 break;
1407                         break;
1408
1409         case SC_DEC:
1410         case SC_OCT:
1411                 memset(base_val, SC_0, calc_buffer_size);
1412                 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1413
1414                 p    = val;
1415                 sign = 0;
1416                 if (signed_mode && base == SC_DEC) {
1417                         /* check for negative values */
1418                         if (_bit(val, bits - 1)) {
1419                                 _negate(val, div2_res);
1420                                 sign = 1;
1421                                 p = div2_res;
1422                         }
1423                 }
1424
1425                 /* transfer data into oscillating buffers */
1426                 memset(div1_res, SC_0, calc_buffer_size);
1427                 for (counter = 0; counter < nibbles; ++counter)
1428                         div1_res[counter] = p[counter];
1429
1430                 /* last nibble must be masked */
1431                 if (bits & 3) {
1432                         ++counter;
1433
1434                         div1_res[counter] = and_table[_val(p[counter])][bits & 3];
1435                 }
1436
1437                 m = div1_res;
1438                 n = div2_res;
1439                 for (;;) {
1440                         _divmod(m, base_val, n, rem_res);
1441                         t = m;
1442                         m = n;
1443                         n = t;
1444                         *(--pos) = digits[_val(rem_res[0])];
1445
1446                         x = 0;
1447                         for (i = 0; i < calc_buffer_size; ++i)
1448                                 x |= _val(m[i]);
1449
1450                         if (x == 0)
1451                                 break;
1452                 }
1453                 if (sign)
1454                         *(--pos) = '-';
1455                 break;
1456
1457         default:
1458                 printf("%i\n", base);
1459                 assert(0);
1460                 return NULL;
1461         }
1462         return pos;
1463 }
1464
1465 void init_strcalc(int precision) {
1466         if (calc_buffer == NULL) {
1467                 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1468
1469                 /* round up to multiple of 4 */
1470                 precision = (precision + 3) & ~3;
1471
1472                 bit_pattern_size = (precision);
1473                 calc_buffer_size = (precision / 2);
1474                 max_value_size   = (precision / 4);
1475
1476                 calc_buffer   = xmalloc(calc_buffer_size+1 * sizeof(char));
1477                 output_buffer = xmalloc(bit_pattern_size+1 * sizeof(char));
1478
1479                 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));
1480         }
1481 }
1482
1483
1484 void finish_strcalc(void) {
1485         free(calc_buffer);   calc_buffer   = NULL;
1486         free(output_buffer); output_buffer = NULL;
1487 }
1488
1489 int sc_get_precision(void) {
1490         return bit_pattern_size;
1491 }
1492
1493
1494 void sc_add(const void *value1, const void *value2, void *buffer) {
1495         CLEAR_BUFFER(calc_buffer);
1496         carry_flag = 0;
1497
1498         DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1499         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1500
1501         _add(value1, value2, calc_buffer);
1502
1503         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1504
1505         if ((buffer != NULL) && (buffer != calc_buffer)) {
1506                 memcpy(buffer, calc_buffer, calc_buffer_size);
1507         }
1508 }
1509
1510 void sc_sub(const void *value1, const void *value2, void *buffer) {
1511         CLEAR_BUFFER(calc_buffer);
1512         carry_flag = 0;
1513
1514         DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1515         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1516
1517         _sub(value1, value2, calc_buffer);
1518
1519         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1520
1521         if ((buffer != NULL) && (buffer != calc_buffer)) {
1522                 memcpy(buffer, calc_buffer, calc_buffer_size);
1523         }
1524 }
1525
1526 void sc_neg(const void *value1, void *buffer) {
1527         carry_flag = 0;
1528
1529         DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1530
1531         _negate(value1, calc_buffer);
1532
1533         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1534
1535         if ((buffer != NULL) && (buffer != calc_buffer)) {
1536                 memcpy(buffer, calc_buffer, calc_buffer_size);
1537         }
1538 }
1539
1540 void sc_and(const void *value1, const void *value2, void *buffer) {
1541         CLEAR_BUFFER(calc_buffer);
1542         carry_flag = 0;
1543
1544         DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1545         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1546
1547         _bitand(value1, value2, calc_buffer);
1548
1549         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1550
1551         if ((buffer != NULL) && (buffer != calc_buffer)) {
1552                 memcpy(buffer, calc_buffer, calc_buffer_size);
1553         }
1554 }
1555
1556 void sc_or(const void *value1, const void *value2, void *buffer) {
1557         CLEAR_BUFFER(calc_buffer);
1558         carry_flag = 0;
1559
1560         DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1561         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1562
1563         _bitor(value1, value2, calc_buffer);
1564
1565         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1566
1567         if ((buffer != NULL) && (buffer != calc_buffer)) {
1568                 memcpy(buffer, calc_buffer, calc_buffer_size);
1569         }
1570 }
1571
1572 void sc_xor(const void *value1, const void *value2, void *buffer) {
1573         CLEAR_BUFFER(calc_buffer);
1574         carry_flag = 0;
1575
1576         DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1577         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1578
1579         _bitxor(value1, value2, calc_buffer);
1580
1581         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1582
1583         if ((buffer != NULL) && (buffer != calc_buffer)) {
1584                 memcpy(buffer, calc_buffer, calc_buffer_size);
1585         }
1586 }
1587
1588 void sc_not(const void *value1, void *buffer) {
1589         CLEAR_BUFFER(calc_buffer);
1590         carry_flag = 0;
1591
1592         DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1593
1594         _bitnot(value1, calc_buffer);
1595
1596         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1597
1598         if ((buffer != NULL) && (buffer != calc_buffer)) {
1599                 memcpy(buffer, calc_buffer, calc_buffer_size);
1600         }
1601 }
1602
1603 void sc_mul(const void *value1, const void *value2, void *buffer) {
1604         CLEAR_BUFFER(calc_buffer);
1605         carry_flag = 0;
1606
1607         DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1608         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1609
1610         _mul(value1, value2, calc_buffer);
1611
1612         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1613
1614         if ((buffer != NULL) && (buffer != calc_buffer)) {
1615                 memcpy(buffer, calc_buffer, calc_buffer_size);
1616         }
1617 }
1618
1619 void sc_div(const void *value1, const void *value2, void *buffer) {
1620         /* temp buffer holding unused result of divmod */
1621         char *unused_res = alloca(calc_buffer_size);
1622
1623         CLEAR_BUFFER(calc_buffer);
1624         carry_flag = 0;
1625
1626         DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1627         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1628
1629         _divmod(value1, value2, calc_buffer, unused_res);
1630
1631         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1632
1633         if ((buffer != NULL) && (buffer != calc_buffer)) {
1634                 memcpy(buffer, calc_buffer, calc_buffer_size);
1635         }
1636 }
1637
1638 void sc_mod(const void *value1, const void *value2, void *buffer) {
1639         /* temp buffer holding unused result of divmod */
1640         char *unused_res = alloca(calc_buffer_size);
1641
1642         CLEAR_BUFFER(calc_buffer);
1643         carry_flag = 0;
1644
1645         DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1646         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1647
1648         _divmod(value1, value2, unused_res, calc_buffer);
1649
1650         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1651
1652         if ((buffer != NULL) && (buffer != calc_buffer)) {
1653                 memcpy(buffer, calc_buffer, calc_buffer_size);
1654         }
1655 }
1656
1657 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer) {
1658         CLEAR_BUFFER(calc_buffer);
1659         carry_flag = 0;
1660
1661         DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1662         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1663
1664         _divmod(value1, value2, div_buffer, mod_buffer);
1665
1666         DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1667 }
1668
1669
1670 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1671         carry_flag = 0;
1672
1673         DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1674         _shl(val1, calc_buffer, shift_cnt, bitsize, sign);
1675
1676         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1677
1678         if ((buffer != NULL) && (buffer != calc_buffer)) {
1679                 memmove(buffer, calc_buffer, calc_buffer_size);
1680         }
1681 }
1682
1683 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1684         long offset = sc_val_to_long(val2);
1685
1686         sc_shlI(val1, offset, bitsize, sign, buffer);
1687 }
1688
1689 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1690         carry_flag = 0;
1691
1692         DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1693         _shr(val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1694
1695         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1696
1697         if ((buffer != NULL) && (buffer != calc_buffer)) {
1698                 memmove(buffer, calc_buffer, calc_buffer_size);
1699         }
1700 }
1701
1702 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1703         long shift_cnt = sc_val_to_long(val2);
1704
1705         sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1706 }
1707
1708 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1709         long offset = sc_val_to_long(val2);
1710
1711         carry_flag = 0;
1712
1713         DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1714         _shr(val1, calc_buffer, offset, bitsize, sign, 1);
1715
1716         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1717
1718         if ((buffer != NULL) && (buffer != calc_buffer)) {
1719                 memmove(buffer, calc_buffer, calc_buffer_size);
1720         }
1721 }
1722
1723 void sc_rot(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1724         long offset = sc_val_to_long(val2);
1725
1726         carry_flag = 0;
1727
1728         DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1729         _rot(val1, calc_buffer, offset, bitsize, sign);
1730
1731         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1732
1733         if ((buffer != NULL) && (buffer != calc_buffer)) {
1734                 memmove(buffer, calc_buffer, calc_buffer_size);
1735         }
1736 }
1737
1738 void sc_zero(void *buffer) {
1739         if (buffer == NULL)
1740                 buffer = calc_buffer;
1741         CLEAR_BUFFER(buffer);
1742         carry_flag = 0;
1743 }