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