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