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