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