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