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