some cleanups in preparation for a new tarval_from_str interface
[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         assert('1'-'0' == 1
821                 && '2'-'0' == 2
822                 && '3'-'0' == 3
823                 && '4'-'0' == 4
824                 && '5'-'0' == 5
825                 && '6'-'0' == 6
826                 && '7'-'0' == 7
827                 && '8'-'0' == 8
828                 && '9'-'0' == 9);
829         assert('b'-'a' == 1
830                 && 'c'-'a' == 2
831                 && 'd'-'a' == 3
832                 && 'e'-'a' == 4
833                 && 'f'-'a' == 5);
834         assert('B'-'A' == 1
835                 && 'C'-'A' == 2
836                 && 'D'-'A' == 3
837                 && 'E'-'A' == 4
838                 && 'F'-'A' == 5);
839 }
840
841 int sc_val_from_str(char sign, unsigned base, const char *str,
842                     unsigned int len, void *buffer)
843 {
844         char *sc_base, *val;
845
846         assert(sign == -1 || sign == 1);
847         assert(str != NULL);
848         assert(len > 0);
849         check_ascii();
850
851         assert(base > 1 && base <= 16);
852         sc_base = alloca(calc_buffer_size);
853         sc_val_from_ulong(base, sc_base);
854
855         val = alloca(calc_buffer_size);
856         if (buffer == NULL)
857                 buffer = calc_buffer;
858
859         CLEAR_BUFFER(buffer);
860         CLEAR_BUFFER(val);
861
862         /* BEGIN string evaluation, from left to right */
863         while (len > 0) {
864                 char c = *str;
865                 unsigned v;
866                 if (c >= '0' && c <= '9')
867                         v = c - '0';
868                 else if (c >= 'A' && c <= 'Z')
869                         v = c - 'A';
870                 else if (c >= 'a' && c <= 'z')
871                         v = c - 'z';
872                 else
873                         return 0;
874
875                 if (v >= base)
876                         return 0;
877                 val[0] = v;
878
879                 /* Radix conversion from base b to base B:
880                  *  (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
881                 /* multiply current value with base */
882                 do_mul(sc_base, buffer, buffer);
883                 /* add next digit to current value  */
884                 do_add(val, buffer, buffer);
885
886                 /* get ready for the next letter */
887                 str++;
888                 len--;
889         } /* while (len > 0 ) */
890
891         if (sign < 0)
892                 do_negate(buffer, buffer);
893
894         return 1;
895 }
896
897 void sc_val_from_long(long value, void *buffer)
898 {
899         char *pos;
900         char sign, is_minlong;
901
902         if (buffer == NULL) buffer = calc_buffer;
903         pos = buffer;
904
905         sign = (value < 0);
906         is_minlong = value == LONG_MIN;
907
908         /* use absolute value, special treatment of MIN_LONG to avoid overflow */
909         if (sign) {
910                 if (is_minlong)
911                         value = -(value+1);
912                 else
913                         value = -value;
914         }
915
916         CLEAR_BUFFER(buffer);
917
918         while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
919                 *pos++ = _digit(value & 0xf);
920                 value >>= 4;
921         }
922
923         if (sign) {
924                 if (is_minlong)
925                         do_inc(buffer, buffer);
926
927                 do_negate(buffer, buffer);
928         }
929 }
930
931 void sc_val_from_ulong(unsigned long value, void *buffer)
932 {
933         unsigned char *pos;
934
935         if (buffer == NULL) buffer = calc_buffer;
936         pos = buffer;
937
938         while (pos < (unsigned char *)buffer + calc_buffer_size) {
939                 *pos++ = (unsigned char)_digit(value & 0xf);
940                 value >>= 4;
941         }
942 }
943
944 long sc_val_to_long(const void *val)
945 {
946         int i;
947         long l = 0;
948
949         for (i = calc_buffer_size - 1; i >= 0; i--) {
950                 l = (l << 4) + _val(((char *)val)[i]);
951         }
952         return l;
953 }
954
955 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
956 {
957         char *pos;
958         int i, bits;
959
960         if (buffer == NULL) buffer = calc_buffer;
961         CLEAR_BUFFER(buffer);
962
963         if (!sign) return;  /* unsigned means minimum is 0(zero) */
964
965         pos = buffer;
966
967         bits = num_bits - 1;
968         for (i = 0; i < bits/4; i++)
969                 *pos++ = SC_0;
970
971         *pos++ = min_digit[bits%4];
972
973         for (i++; i <= calc_buffer_size - 1; i++)
974                 *pos++ = SC_F;
975 }
976
977 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
978 {
979         char* pos;
980         int i, bits;
981
982         if (buffer == NULL) buffer = calc_buffer;
983         CLEAR_BUFFER(buffer);
984         pos = buffer;
985
986         bits = num_bits - sign;
987         for (i = 0; i < bits/4; i++)
988                 *pos++ = SC_F;
989
990         *pos++ = max_digit[bits%4];
991
992         for (i++; i <= calc_buffer_size - 1; i++)
993                 *pos++ = SC_0;
994 }
995
996 void sc_truncate(unsigned int num_bits, void *buffer)
997 {
998         char *cbuffer = buffer;
999         char *pos = cbuffer + (num_bits / 4);
1000         char *end = cbuffer + calc_buffer_size;
1001
1002         assert(pos < end);
1003
1004         switch (num_bits % 4) {
1005         case 0: /* nothing to do */ break;
1006         case 1: *pos++ &= SC_1; break;
1007         case 2: *pos++ &= SC_3; break;
1008         case 3: *pos++ &= SC_7; break;
1009         }
1010
1011         for ( ; pos < end; ++pos)
1012                 *pos = SC_0;
1013 }
1014
1015 int sc_comp(const void* value1, const void* value2)
1016 {
1017         int counter = calc_buffer_size - 1;
1018         const char *val1 = (const char *)value1;
1019         const char *val2 = (const char *)value2;
1020
1021         /* compare signs first:
1022          * the loop below can only compare values of the same sign! */
1023         if (do_sign(val1) != do_sign(val2))
1024                 return (do_sign(val1) == 1)?(1):(-1);
1025
1026         /* loop until two digits differ, the values are equal if there
1027          * are no such two digits */
1028         while (val1[counter] == val2[counter]) {
1029                 counter--;
1030                 if (counter < 0) return 0;
1031         }
1032
1033         /* the leftmost digit is the most significant, so this returns
1034          * the correct result.
1035          * This implies the digit enum is ordered */
1036         return (val1[counter] > val2[counter]) ? (1) : (-1);
1037 }
1038
1039 int sc_get_highest_set_bit(const void *value)
1040 {
1041         const char *val = (const char*)value;
1042         int high, counter;
1043
1044         high = calc_buffer_size * 4 - 1;
1045
1046         for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1047                 if (val[counter] == SC_0)
1048                         high -= 4;
1049                 else {
1050                         if (val[counter] > SC_7) return high;
1051                         else if (val[counter] > SC_3) return high - 1;
1052                         else if (val[counter] > SC_1) return high - 2;
1053                         else return high - 3;
1054                 }
1055         }
1056         return high;
1057 }
1058
1059 int sc_get_lowest_set_bit(const void *value)
1060 {
1061         const char *val = (const char*)value;
1062         int low, counter;
1063
1064         low = 0;
1065         for (counter = 0; counter < calc_buffer_size; counter++) {
1066                 switch (val[counter]) {
1067                 case SC_1:
1068                 case SC_3:
1069                 case SC_5:
1070                 case SC_7:
1071                 case SC_9:
1072                 case SC_B:
1073                 case SC_D:
1074                 case SC_F:
1075                         return low;
1076                 case SC_2:
1077                 case SC_6:
1078                 case SC_A:
1079                 case SC_E:
1080                         return low + 1;
1081                 case SC_4:
1082                 case SC_C:
1083                         return low + 2;
1084                 case SC_8:
1085                         return low + 3;
1086                 default:
1087                         low += 4;
1088                 }
1089         }
1090         return -1;
1091 }
1092
1093 int sc_get_bit_at(const void *value, unsigned pos)
1094 {
1095         const char *val = value;
1096         unsigned nibble = pos >> 2;
1097
1098         return (val[nibble] & SHIFT(pos & 3)) != SC_0;
1099 }
1100
1101 void sc_set_bit_at(void *value, unsigned pos)
1102 {
1103         char *val = value;
1104         unsigned nibble = pos >> 2;
1105
1106         val[nibble] |= SHIFT(pos & 3);
1107 }
1108
1109 int sc_is_zero(const void *value)
1110 {
1111         const char* val = (const char *)value;
1112         int counter;
1113
1114         for (counter = 0; counter < calc_buffer_size; ++counter) {
1115                 if (val[counter] != SC_0)
1116                         return 0;
1117         }
1118         return 1;
1119 }
1120
1121 int sc_is_negative(const void *value)
1122 {
1123         return do_sign(value) == -1;
1124 }
1125
1126 int sc_had_carry(void)
1127 {
1128         return carry_flag;
1129 }
1130
1131 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1132 {
1133         const char *val = (const char *)value;
1134         int nibble_ofs  = 2 * byte_ofs;
1135         unsigned char res;
1136
1137         /* the current scheme uses one byte to store a nibble */
1138         if (4 * nibble_ofs >= len)
1139                 return 0;
1140
1141         res = _val(val[nibble_ofs]);
1142         if (len > 4 * (nibble_ofs + 1))
1143                 res |= _val(val[nibble_ofs + 1]) << 4;
1144
1145         /* kick bits outsize */
1146         if (len - 8 * byte_ofs < 8) {
1147                 res &= (1 << (len - 8 * byte_ofs)) - 1;
1148         }
1149         return res;
1150 }
1151
1152 /*
1153  * convert to a string
1154  * FIXME: Doesn't check buffer bounds
1155  */
1156 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode)
1157 {
1158         static const char big_digits[]   = "0123456789ABCDEF";
1159         static const char small_digits[] = "0123456789abcdef";
1160
1161         char *base_val, *div1_res, *div2_res, *rem_res;
1162         int counter, nibbles, i, sign, mask;
1163         char x;
1164
1165         const char *val = (const char *)value;
1166         const char *p;
1167         char *m, *n, *t;
1168         char *pos;
1169         const char *digits = small_digits;
1170
1171         base_val = alloca(calc_buffer_size);
1172         div1_res = alloca(calc_buffer_size);
1173         div2_res = alloca(calc_buffer_size);
1174         rem_res  = alloca(calc_buffer_size);
1175
1176         pos = output_buffer + bit_pattern_size;
1177         *(--pos) = '\0';
1178
1179         /* special case */
1180         if (bits == 0) {
1181                 bits = bit_pattern_size;
1182 #ifdef STRCALC_DEBUG_FULLPRINT
1183                 bits <<= 1;
1184 #endif
1185         }
1186         nibbles = bits >> 2;
1187         switch (base) {
1188
1189         case SC_HEX:
1190                 digits = big_digits;
1191         case SC_hex:
1192                 for (counter = 0; counter < nibbles; ++counter) {
1193                         *(--pos) = digits[_val(val[counter])];
1194 #ifdef STRCALC_DEBUG_GROUPPRINT
1195                         if ((counter+1)%8 == 0)
1196                                 *(--pos) = ' ';
1197 #endif
1198                 }
1199
1200                 /* last nibble must be masked */
1201                 if (bits & 3) {
1202                         mask = zex_digit[(bits & 3) - 1];
1203                         x    = val[counter++] & mask;
1204                         *(--pos) = digits[_val(x)];
1205                 }
1206
1207                 /* now kill zeros */
1208                 for (; counter > 1; --counter, ++pos) {
1209 #ifdef STRCALC_DEBUG_GROUPPRINT
1210                         if (pos[0] == ' ') ++pos;
1211 #endif
1212                         if (pos[0] != '0')
1213                                 break;
1214                 }
1215                 break;
1216
1217         case SC_BIN:
1218                 for (counter = 0; counter < nibbles; ++counter) {
1219                         pos -= 4;
1220                         p = binary_table[_val(val[counter])];
1221                         pos[0] = p[0];
1222                         pos[1] = p[1];
1223                         pos[2] = p[2];
1224                         pos[3] = p[3];
1225                 }
1226
1227                 /* last nibble must be masked */
1228                 if (bits & 3) {
1229                         mask = zex_digit[(bits & 3) - 1];
1230                         x    = val[counter++] & mask;
1231
1232                         pos -= 4;
1233                         p = binary_table[_val(x)];
1234                         pos[0] = p[0];
1235                         pos[1] = p[1];
1236                         pos[2] = p[2];
1237                         pos[3] = p[3];
1238                 }
1239
1240                 /* now kill zeros */
1241                 for (counter <<= 2; counter > 1; --counter, ++pos)
1242                         if (pos[0] != '0')
1243                                 break;
1244                         break;
1245
1246         case SC_DEC:
1247         case SC_OCT:
1248                 memset(base_val, SC_0, calc_buffer_size);
1249                 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1250
1251                 p    = val;
1252                 sign = 0;
1253                 if (signed_mode && base == SC_DEC) {
1254                         /* check for negative values */
1255                         if (do_bit(val, bits - 1)) {
1256                                 do_negate(val, div2_res);
1257                                 sign = 1;
1258                                 p = div2_res;
1259                         }
1260                 }
1261
1262                 /* transfer data into oscillating buffers */
1263                 memset(div1_res, SC_0, calc_buffer_size);
1264                 for (counter = 0; counter < nibbles; ++counter)
1265                         div1_res[counter] = p[counter];
1266
1267                 /* last nibble must be masked */
1268                 if (bits & 3) {
1269                         mask = zex_digit[(bits & 3) - 1];
1270                         div1_res[counter] = p[counter] & mask;
1271                         ++counter;
1272                 }
1273
1274                 m = div1_res;
1275                 n = div2_res;
1276                 for (;;) {
1277                         do_divmod(m, base_val, n, rem_res);
1278                         t = m;
1279                         m = n;
1280                         n = t;
1281                         *(--pos) = digits[_val(rem_res[0])];
1282
1283                         x = 0;
1284                         for (i = 0; i < calc_buffer_size; ++i)
1285                                 x |= _val(m[i]);
1286
1287                         if (x == 0)
1288                                 break;
1289                 }
1290                 if (sign)
1291                         *(--pos) = '-';
1292                 break;
1293
1294         default:
1295                 panic("Unsupported base %d", base);
1296         }
1297         return pos;
1298 }
1299
1300 void init_strcalc(int precision)
1301 {
1302         if (calc_buffer == NULL) {
1303                 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1304
1305                 /* round up to multiple of 4 */
1306                 precision = (precision + 3) & ~3;
1307
1308                 bit_pattern_size = (precision);
1309                 calc_buffer_size = (precision / 2);
1310                 max_value_size   = (precision / 4);
1311
1312                 calc_buffer   = XMALLOCN(char, calc_buffer_size + 1);
1313                 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1314
1315                 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));
1316         }
1317 }
1318
1319
1320 void finish_strcalc(void)
1321 {
1322         free(calc_buffer);   calc_buffer   = NULL;
1323         free(output_buffer); output_buffer = NULL;
1324 }
1325
1326 int sc_get_precision(void)
1327 {
1328         return bit_pattern_size;
1329 }
1330
1331
1332 void sc_add(const void *value1, const void *value2, void *buffer)
1333 {
1334         CLEAR_BUFFER(calc_buffer);
1335         carry_flag = 0;
1336
1337         DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1338         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1339
1340         do_add(value1, value2, calc_buffer);
1341
1342         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1343
1344         if ((buffer != NULL) && (buffer != calc_buffer)) {
1345                 memcpy(buffer, calc_buffer, calc_buffer_size);
1346         }
1347 }
1348
1349 void sc_sub(const void *value1, const void *value2, void *buffer)
1350 {
1351         CLEAR_BUFFER(calc_buffer);
1352         carry_flag = 0;
1353
1354         DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1355         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1356
1357         do_sub(value1, value2, calc_buffer);
1358
1359         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1360
1361         if ((buffer != NULL) && (buffer != calc_buffer)) {
1362                 memcpy(buffer, calc_buffer, calc_buffer_size);
1363         }
1364 }
1365
1366 void sc_neg(const void *value1, void *buffer)
1367 {
1368         carry_flag = 0;
1369
1370         DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1371
1372         do_negate(value1, calc_buffer);
1373
1374         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1375
1376         if ((buffer != NULL) && (buffer != calc_buffer)) {
1377                 memcpy(buffer, calc_buffer, calc_buffer_size);
1378         }
1379 }
1380
1381 void sc_and(const void *value1, const void *value2, void *buffer)
1382 {
1383         CLEAR_BUFFER(calc_buffer);
1384         carry_flag = 0;
1385
1386         DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1387         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1388
1389         do_bitand(value1, value2, calc_buffer);
1390
1391         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1392
1393         if ((buffer != NULL) && (buffer != calc_buffer)) {
1394                 memcpy(buffer, calc_buffer, calc_buffer_size);
1395         }
1396 }
1397
1398 void sc_andnot(const void *value1, const void *value2, void *buffer)
1399 {
1400         CLEAR_BUFFER(calc_buffer);
1401         carry_flag = 0;
1402
1403         DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1404         DEBUGPRINTF_COMPUTATION(("~%s -> ", sc_print_hex(value2)));
1405
1406         do_bitandnot(value1, value2, calc_buffer);
1407
1408         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1409
1410         if (buffer != NULL && buffer != calc_buffer) {
1411                 memcpy(buffer, calc_buffer, calc_buffer_size);
1412         }
1413 }
1414
1415 void sc_or(const void *value1, const void *value2, void *buffer)
1416 {
1417         CLEAR_BUFFER(calc_buffer);
1418         carry_flag = 0;
1419
1420         DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1421         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1422
1423         do_bitor(value1, value2, calc_buffer);
1424
1425         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1426
1427         if ((buffer != NULL) && (buffer != calc_buffer)) {
1428                 memcpy(buffer, calc_buffer, calc_buffer_size);
1429         }
1430 }
1431
1432 void sc_xor(const void *value1, const void *value2, void *buffer)
1433 {
1434         CLEAR_BUFFER(calc_buffer);
1435         carry_flag = 0;
1436
1437         DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1438         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1439
1440         do_bitxor(value1, value2, calc_buffer);
1441
1442         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1443
1444         if ((buffer != NULL) && (buffer != calc_buffer)) {
1445                 memcpy(buffer, calc_buffer, calc_buffer_size);
1446         }
1447 }
1448
1449 void sc_not(const void *value1, void *buffer)
1450 {
1451         CLEAR_BUFFER(calc_buffer);
1452         carry_flag = 0;
1453
1454         DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1455
1456         do_bitnot(value1, calc_buffer);
1457
1458         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1459
1460         if ((buffer != NULL) && (buffer != calc_buffer)) {
1461                 memcpy(buffer, calc_buffer, calc_buffer_size);
1462         }
1463 }
1464
1465 void sc_mul(const void *value1, const void *value2, void *buffer)
1466 {
1467         CLEAR_BUFFER(calc_buffer);
1468         carry_flag = 0;
1469
1470         DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1471         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1472
1473         do_mul(value1, value2, calc_buffer);
1474
1475         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1476
1477         if ((buffer != NULL) && (buffer != calc_buffer)) {
1478                 memcpy(buffer, calc_buffer, calc_buffer_size);
1479         }
1480 }
1481
1482 void sc_div(const void *value1, const void *value2, void *buffer)
1483 {
1484         /* temp buffer holding unused result of divmod */
1485         char *unused_res = alloca(calc_buffer_size);
1486
1487         CLEAR_BUFFER(calc_buffer);
1488         carry_flag = 0;
1489
1490         DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1491         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1492
1493         do_divmod(value1, value2, calc_buffer, unused_res);
1494
1495         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1496
1497         if ((buffer != NULL) && (buffer != calc_buffer)) {
1498                 memcpy(buffer, calc_buffer, calc_buffer_size);
1499         }
1500 }
1501
1502 void sc_mod(const void *value1, const void *value2, void *buffer)
1503 {
1504         /* temp buffer holding unused result of divmod */
1505         char *unused_res = alloca(calc_buffer_size);
1506
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_divmod(value1, value2, unused_res, 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_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer)
1523 {
1524         CLEAR_BUFFER(calc_buffer);
1525         carry_flag = 0;
1526
1527         DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1528         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1529
1530         do_divmod(value1, value2, div_buffer, mod_buffer);
1531
1532         DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1533 }
1534
1535
1536 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1537 {
1538         carry_flag = 0;
1539
1540         DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1541         do_shl(val1, calc_buffer, shift_cnt, bitsize, sign);
1542
1543         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1544
1545         if ((buffer != NULL) && (buffer != calc_buffer)) {
1546                 memmove(buffer, calc_buffer, calc_buffer_size);
1547         }
1548 }
1549
1550 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1551 {
1552         long offset = sc_val_to_long(val2);
1553
1554         sc_shlI(val1, offset, bitsize, sign, buffer);
1555 }
1556
1557 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1558 {
1559         carry_flag = 0;
1560
1561         DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1562         do_shr(val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1563
1564         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1565
1566         if ((buffer != NULL) && (buffer != calc_buffer)) {
1567                 memmove(buffer, calc_buffer, calc_buffer_size);
1568         }
1569 }
1570
1571 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1572 {
1573         long shift_cnt = sc_val_to_long(val2);
1574
1575         sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1576 }
1577
1578 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1579 {
1580         long offset = sc_val_to_long(val2);
1581
1582         carry_flag = 0;
1583
1584         DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1585         do_shr(val1, calc_buffer, offset, bitsize, sign, 1);
1586
1587         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1588
1589         if ((buffer != NULL) && (buffer != calc_buffer)) {
1590                 memmove(buffer, calc_buffer, calc_buffer_size);
1591         }
1592 }
1593
1594 void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1595 {
1596         long offset = sc_val_to_long(val2);
1597
1598         carry_flag = 0;
1599
1600         DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1601         do_rotl(val1, calc_buffer, offset, bitsize, sign);
1602
1603         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1604
1605         if ((buffer != NULL) && (buffer != calc_buffer)) {
1606                 memmove(buffer, calc_buffer, calc_buffer_size);
1607         }
1608 }
1609
1610 void sc_zero(void *buffer)
1611 {
1612         if (buffer == NULL)
1613                 buffer = calc_buffer;
1614         CLEAR_BUFFER(buffer);
1615         carry_flag = 0;
1616 }