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