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