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