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