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