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