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