3 * File name: ir/tv/strcalc.c
9 * Copyright: (c) 2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
28 # include <string.h> /* memset/memcmp */
30 #include <assert.h> /* assertions */
31 #include <stdio.h> /* output for error messages */
32 #include <limits.h> /* definition of LONG_MIN, used in sc_get_val_from_long */
38 * local definitions and macros
40 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, calc_buffer_size)
41 #define _val(a) ((a)-SC_0)
42 #define _digit(a) ((a)+SC_0)
43 #define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
45 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__, __LINE__)
47 /* shortcut output for debugging */
48 # define sc_print_hex(a) sc_print((a), 0, SC_HEX, 0)
49 # define sc_print_dec(a) sc_print((a), 0, SC_DEC, 1)
50 # define sc_print_oct(a) sc_print((a), 0, SC_OCT, 0)
51 # define sc_print_bin(a) sc_print((a), 0, SC_BIN, 0)
53 #ifdef STRCALC_DEBUG_PRINTCOMP
54 # define DEBUGPRINTF_COMPUTATION(x) printf x
56 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
59 # define DEBUGPRINTF(x) printf x
61 # define DEBUGPRINTF(x) ((void)0)
68 static char *calc_buffer = NULL; /* buffer holding all results */
69 static char *output_buffer = NULL; /* buffer for output */
70 static int bit_pattern_size; /* maximum number of bits */
71 static int calc_buffer_size; /* size of internally stored values */
72 static int max_value_size; /* maximum size of values */
74 static int carry_flag; /**< some computation set the carry_flag:
75 - right shift if bits were lost due to shifting
76 - division if there was a remainder
77 However, the meaning of carry is machine dependent
78 and often defined in other ways! */
80 static const char sex_digit[4] = { SC_E, SC_C, SC_8, SC_0 };
81 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
82 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
84 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
85 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
87 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
89 static const char and_table[16][16] = {
90 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
91 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
93 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
94 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
96 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
97 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
99 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
100 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
102 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
103 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
105 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
106 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
108 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
109 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
111 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
112 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
114 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
115 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
117 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
118 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
120 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
121 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
123 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
124 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
126 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
127 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
129 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
130 SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
132 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
133 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
135 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
136 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
138 static const char or_table[16][16] = {
139 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
140 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
142 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
143 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
145 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
146 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
148 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
149 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
151 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
152 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
154 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
155 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
157 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
158 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
160 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
161 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
163 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
164 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
166 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
167 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
169 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
170 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
172 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
173 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
175 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
176 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
178 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
179 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
181 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
182 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
184 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
185 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
187 static char const xor_table[16][16] = {
188 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
189 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
191 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
192 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
194 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
195 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
197 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
198 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
200 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
201 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
203 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
204 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
206 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
207 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
209 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
210 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
212 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
213 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
215 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
216 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
218 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
219 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
221 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
222 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
224 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
225 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
227 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
228 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
230 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
231 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
233 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
234 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
237 static char const add_table[16][16][2] = {
238 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
239 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
240 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
241 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
243 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
244 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
245 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
246 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
248 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
249 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
250 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
251 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
253 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
254 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
255 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
256 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
258 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
259 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
260 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
261 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
263 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
264 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
265 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
266 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
268 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
269 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
270 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
271 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
273 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
274 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
275 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
276 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
278 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
279 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
280 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
281 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
283 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
284 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
285 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
286 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
288 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
289 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
290 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
291 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
293 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
294 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
295 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
296 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
298 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
299 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
300 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
301 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
303 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
304 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
305 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
306 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
308 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
309 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
310 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
311 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
313 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
314 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
315 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
316 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
319 static char const mul_table[16][16][2] = {
320 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
321 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
322 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
323 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
325 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
326 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
327 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
328 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
330 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
331 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
332 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
333 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
335 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
336 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
337 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
338 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
340 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
341 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
342 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
343 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
345 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
346 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
347 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
348 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
350 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
351 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
352 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
353 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
355 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
356 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
357 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
358 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
360 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
361 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
362 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
363 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
365 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
366 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
367 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
368 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
370 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
371 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
372 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
373 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
375 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
376 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
377 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
378 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
380 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
381 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
382 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
383 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
385 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
386 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
387 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
388 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
390 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
391 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
392 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
393 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
395 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
396 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
397 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
398 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
401 static char const shrs_table[16][4][2] = {
402 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
403 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
404 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
405 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
406 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
407 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
408 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
409 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
410 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
411 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
412 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
413 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
414 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
415 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
416 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
417 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
420 /** converting a digit to a binary string */
421 static const char *binary_table[16] = {
422 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
423 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
426 /*****************************************************************************
428 *****************************************************************************/
429 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
430 const char *file, int line)
433 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
434 while (len-- && *str) printf("%c", *str++); printf("\n");
435 while (--pos) printf(" "); printf("^\n");
440 * implements the bitwise NOT operation
442 static void _bitnot(const char *val, char *buffer)
446 for (counter = 0; counter<calc_buffer_size; counter++)
447 buffer[counter] = not_table[_val(val[counter])];
451 * implements the bitwise OR operation
453 static void _bitor(const char *val1, const char *val2, char *buffer)
457 for (counter = 0; counter<calc_buffer_size; counter++)
458 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
462 * implements the bitwise eXclusive OR operation
464 static void _bitxor(const char *val1, const char *val2, char *buffer)
468 for (counter = 0; counter<calc_buffer_size; counter++)
469 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
473 * implements the bitwise AND operation
475 static void _bitand(const char *val1, const char *val2, char *buffer)
479 for (counter = 0; counter<calc_buffer_size; counter++)
480 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
484 * returns the sign bit.
486 * @todo This implementation is wrong, as it returns the highest bit of the buffer
487 * NOT the highest bit depending on the real mode
489 static int _sign(const char *val)
491 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
495 * returns non-zero if bit at position pos is set
497 static int _bit(const char *val, int pos)
500 int nibble = pos >> 2;
502 return _bitisset(val[nibble], bit);
506 * Implements a fast ADD + 1
508 static void _inc(const char *val, char *buffer)
512 while (counter++ < calc_buffer_size)
521 /* No carry here, *val != SC_F */
522 *buffer = add_table[_val(*val)][SC_1][0];
526 /* here a carry could be lost, this is intended because this should
527 * happen only when a value changes sign. */
531 * Implements a unary MINUS
533 static void _negate(const char *val, char *buffer)
535 _bitnot(val, buffer);
536 _inc(buffer, buffer);
540 * Implements a binary ADD
542 * @todo The implementation of carry is wrong, as it is the
543 * calc_buffer_size carry, not the mode depending
545 static void _add(const char *val1, const char *val2, char *buffer)
548 const char *add1, *add2;
551 for (counter = 0; counter < calc_buffer_size; counter++)
553 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
554 add2 = add_table[_val(add1[0])][_val(carry)];
555 /* carry might be zero */
556 buffer[counter] = add2[0];
557 carry = add_table[_val(add1[1])][_val(add2[1])][0];
559 carry_flag = carry != SC_0;
563 * Implements a binary SUB
565 static void _sub(const char *val1, const char *val2, char *buffer)
567 char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
569 _negate(val2, temp_buffer);
570 _add(val1, temp_buffer, buffer);
574 * Implements a binary MUL
576 static void _mul(const char *val1, const char *val2, char *buffer)
578 char *temp_buffer; /* result buffer */
579 char *neg_val1; /* abs of val1 */
580 char *neg_val2; /* abs of val2 */
582 const char *mul, *add1, *add2; /* intermediate result containers */
583 char carry = SC_0; /* container for carries */
584 char sign = 0; /* marks result sign */
585 int c_inner, c_outer; /* loop counters */
587 temp_buffer = alloca(calc_buffer_size);
588 neg_val1 = alloca(calc_buffer_size);
589 neg_val2 = alloca(calc_buffer_size);
591 /* init result buffer to zeroes */
592 memset(temp_buffer, SC_0, calc_buffer_size);
594 /* the multiplication works only for positive values, for negative values *
595 * it is necessary to negate them and adjust the result accordingly */
596 if (_sign(val1) == -1) {
597 _negate(val1, neg_val1);
601 if (_sign(val2) == -1) {
602 _negate(val2, neg_val2);
607 for (c_outer = 0; c_outer < max_value_size; c_outer++)
609 if (val2[c_outer] != SC_0)
611 for (c_inner = 0; c_inner < max_value_size; c_inner++)
613 /* do the following calculation: *
614 * Add the current carry, the value at position c_outer+c_inner *
615 * and the result of the multiplication of val1[c_inner] and *
616 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
618 /* multiplicate the two digits */
619 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
620 /* add old value to result of multiplication */
621 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
622 /* add carry to the sum */
623 add2 = add_table[_val(add1[0])][_val(carry)];
625 /* all carries together result in new carry. This is always smaller *
627 * Both multiplicands, the carry and the value already in the temp *
628 * buffer are single digits and their value is therefore at most *
631 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
632 * The tables list all operations rem b, so the carry is at most *
633 * (b*b-1)rem b = -1rem b = b-1 */
634 carry = add_table[_val(mul[1])][_val(add1[1])][0];
635 carry = add_table[_val(carry)][_val(add2[1])][0];
637 temp_buffer[c_inner + c_outer] = add2[0];
640 /* A carry may hang over */
641 /* c_outer is always smaller than max_value_size! */
642 temp_buffer[max_value_size + c_outer] = carry;
648 _negate(temp_buffer, buffer);
650 memcpy(buffer, temp_buffer, calc_buffer_size);
654 * Shift the buffer to left and add a 4 bit digit
656 static void _push(const char digit, char *buffer)
660 for (counter = calc_buffer_size - 2; counter >= 0; counter--)
662 buffer[counter+1] = buffer[counter];
668 * Implements truncating integer division and remainder.
670 * Note: This is MOST slow
672 static void _divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
674 const char *dividend = rDividend;
675 const char *minus_divisor;
679 char div_sign = 0; /* remember division result sign */
680 char rem_sign = 0; /* remember remainder esult sign */
682 int c_dividend; /* loop counters */
684 neg_val1 = alloca(calc_buffer_size);
685 neg_val2 = alloca(calc_buffer_size);
687 /* clear result buffer */
688 memset(quot, SC_0, calc_buffer_size);
689 memset(rem, SC_0, calc_buffer_size);
691 /* if the divisor is zero this won't work (quot is zero) */
692 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
694 /* if the dividend is zero result is zero (quot is zero)*/
695 if (sc_comp(dividend, quot) == 0)
698 if (_sign(dividend) == -1) {
699 _negate(dividend, neg_val1);
705 _negate(divisor, neg_val2);
706 if (_sign(divisor) == -1) {
708 minus_divisor = divisor;
712 minus_divisor = neg_val2;
714 /* if divisor >= dividend division is easy
715 * (remember these are absolute values) */
716 switch (sc_comp(dividend, divisor))
718 case 0: /* dividend == divisor */
722 case -1: /* dividend < divisor */
723 memcpy(rem, rDividend, calc_buffer_size);
726 default: /* unluckily division is necessary :( */
730 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--)
732 _push(dividend[c_dividend], rem);
735 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
737 /* subtract until the remainder becomes negative, this should
738 * be faster than comparing remainder with divisor */
739 _add(rem, minus_divisor, rem);
741 while (_sign(rem) == 1)
743 quot[0] = add_table[_val(quot[0])][SC_1][0];
744 _add(rem, minus_divisor, rem);
747 /* subtracted one too much */
748 _add(rem, divisor, rem);
752 /* sets carry if remainder is non-zero ??? */
753 carry_flag = !sc_is_zero(rem);
763 * Implements a Shift Left, which can either preserve the sign bit
766 * @todo Assertions seems to be wrong
768 static void _shl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
777 assert((offset >= 0) || (0 && "negative leftshift"));
778 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
779 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
780 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
782 /* if shifting far enough the result is zero */
783 if (offset >= radius)
785 memset(buffer, SC_0, calc_buffer_size);
789 shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
792 /* shift the single digits some bytes (offset) and some bits (table)
794 for (counter = 0; counter < radius/4 - offset; counter++)
796 shl = mul_table[_val(val1[counter])][_val(shift)];
797 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
802 shl = mul_table[_val(val1[counter])][_val(shift)];
803 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
806 bitoffset = counter - 1;
809 /* fill with zeroes */
810 for (counter = 0; counter < offset; counter++) buffer[counter] = SC_0;
812 /* if the mode was signed, change sign when the mode's msb is now 1 */
813 offset = bitoffset + offset;
814 bitoffset = (radius-1) % 4;
815 if (is_signed && _bitisset(buffer[offset], bitoffset))
817 /* this sets the upper bits of the leftmost digit */
818 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
819 for (counter = offset+1; counter < calc_buffer_size; counter++)
821 buffer[counter] = SC_F;
824 else if (is_signed && !_bitisset(buffer[offset], bitoffset))
826 /* this clears the upper bits of the leftmost digit */
827 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
828 for (counter = offset+1; counter < calc_buffer_size; counter++)
830 buffer[counter] = SC_0;
836 * Implements a Shift Right, which can either preserve the sign bit
839 * @todo Assertions seems to be wrong
841 static void _shr(const char *val1, char *buffer, long offset, int radius, unsigned is_signed, int signed_shift)
852 assert((offset >= 0) || (0 && "negative rightshift"));
853 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
854 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
856 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
858 /* if shifting far enough the result is either 0 or -1 */
859 if (offset >= radius)
861 if (!sc_is_zero(val1)) {
864 memset(buffer, sign, calc_buffer_size);
871 /* check if any bits are lost, and set carry_flag if so */
872 for (counter = 0; counter < offset; counter++)
874 if (val1[counter] != 0)
880 if ((_val(val1[counter]) & ((1<<shift)-1)) != 0)
884 /* shift digits to the right with offset, carry and all */
886 if (radius/4 - offset > 0) {
887 buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
890 for (; counter < radius/4 - offset; counter++)
892 shrs = shrs_table[_val(val1[counter + offset])][shift];
893 buffer[counter] = shrs[0];
894 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
897 /* the last digit is special in regard of signed/unsigned shift */
898 bitoffset = radius%4;
899 msd = (radius/4<calc_buffer_size)?(val1[radius/4]):(sign); /* most significant digit */
901 /* remove sign bits if mode was signed and this is an unsigned shift */
902 if (!signed_shift && is_signed) {
903 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
906 shrs = shrs_table[_val(msd)][shift];
908 /* signed shift and signed mode and negative value means all bits to the left are set */
909 if (signed_shift && is_signed && (_sign(val1) == -1)) {
910 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
912 buffer[counter] = shrs[0];
915 if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
917 /* fill with SC_F or SC_0 depending on sign */
918 for (counter++; counter < calc_buffer_size; counter++)
920 buffer[counter] = sign;
925 * Implements a Rotate Right.
926 * positive: low-order -> high order, negative other direction
928 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
931 temp1 = alloca(calc_buffer_size);
932 temp2 = alloca(calc_buffer_size);
934 offset = offset % radius;
936 /* rotation by multiples of the type length is identity */
938 memmove(buffer, val1, calc_buffer_size);
942 _shl(val1, temp1, offset, radius, is_signed);
943 _shr(val1, temp2, radius - offset, radius, is_signed, 0);
944 _bitor(temp1, temp2, buffer);
945 carry_flag = 0; /* set by shr, but due to rot this is false */
948 /*****************************************************************************
949 * public functions, declared in strcalc.h
950 *****************************************************************************/
951 const void *sc_get_buffer(void)
953 return (void*)calc_buffer;
956 int sc_get_buffer_length(void)
958 return calc_buffer_size;
962 * Do sign extension if the mode is signed, expects all upper bits
965 void sign_extend(char *calc_buffer, ir_mode *mode) {
966 if (mode_is_signed(mode)) {
967 int bits = get_mode_size_bits(mode) - 1;
969 int max = max_digit[bits & 3];
972 if (calc_buffer[ofs] > max) {
973 /* sign bit is set, we need sign expansion */
975 for (i = ofs + 1; i < calc_buffer_size; ++i)
976 calc_buffer[i] = SC_F;
977 calc_buffer[ofs] = or_table[(int) calc_buffer[ofs]][(int) sex_digit[bits & 3]];
982 /* FIXME doesn't check for overflows */
983 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode)
985 const char *orig_str = str;
986 unsigned int orig_len = len;
991 base = alloca(calc_buffer_size);
992 val = alloca(calc_buffer_size);
994 /* verify valid pointers (not null) */
996 /* a string no characters long is an error */
999 if (buffer == NULL) buffer = calc_buffer;
1001 CLEAR_BUFFER(buffer);
1005 /* strip leading spaces */
1006 while ((len > 0) && (*str == ' ')) { len--; str++; }
1008 /* if the first two characters are 0x or 0X -> hex
1009 * if the first is a 0 -> oct
1010 * else dec, strip leading -/+ and remember sign
1012 * only a + or - sign is no number resulting in an error */
1017 if (str[1] == 'x' || str[1] == 'X') /* hex */
1021 base[1] = SC_1; base[0] = SC_0;
1027 base[1] = SC_0; base[0] = SC_8;
1035 base[1] = SC_0; base[0] = SC_A;
1044 base[1] = SC_0; base[0] = SC_A;
1048 default: /* dec, else would have begun with 0x or 0 */
1049 base[1] = SC_0; base[0] = SC_A;
1052 else /* dec, else would have begun with 0x or 0 */
1054 base[1] = SC_0; base[0] = SC_A;
1057 /* BEGIN string evaluation, from left to right */
1068 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1070 val[0] = _digit((*str)-'a'+10);
1072 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1081 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1083 val[0] = _digit((*str)-'A'+10);
1085 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1090 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
1092 val[0] = _digit((*str)-'0');
1094 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1106 val[0] = _digit((*str)-'0');
1111 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1112 } /* switch(*str) */
1114 /* Radix conversion from base b to base B:
1115 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1116 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1117 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1119 /* get ready for the next letter */
1123 } /* while (len > 0 ) */
1126 _negate(calc_buffer, calc_buffer);
1128 /* beware: even if hex numbers have no sign, we need sign extension here */
1129 sign_extend(calc_buffer, mode);
1132 void sc_val_from_long(long value, void *buffer)
1135 char sign, is_minlong;
1137 if (buffer == NULL) buffer = calc_buffer;
1141 is_minlong = value == LONG_MIN;
1143 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
1151 CLEAR_BUFFER(buffer);
1153 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size))
1155 *pos++ = _digit(value & 0xf);
1161 _inc(buffer, buffer);
1163 _negate(buffer, buffer);
1167 void sc_val_from_ulong(unsigned long value, void *buffer)
1171 if (buffer == NULL) buffer = calc_buffer;
1174 while (pos < (unsigned char *)buffer + calc_buffer_size)
1176 *pos++ = (unsigned char)_digit(value & 0xf);
1181 long sc_val_to_long(const void *val)
1186 for (i = calc_buffer_size - 1; i >= 0; i--)
1188 l = (l << 4) + _val(((char *)val)[i]);
1193 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1198 if (buffer == NULL) buffer = calc_buffer;
1199 CLEAR_BUFFER(buffer);
1201 if (!sign) return; /* unsigned means minimum is 0(zero) */
1205 bits = num_bits - 1;
1206 for (i = 0; i < bits/4; i++)
1209 *pos++ = min_digit[bits%4];
1211 for (i++; i <= calc_buffer_size - 1; i++)
1215 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1220 if (buffer == NULL) buffer = calc_buffer;
1221 CLEAR_BUFFER(buffer);
1224 bits = num_bits - sign;
1225 for (i = 0; i < bits/4; i++)
1228 *pos++ = max_digit[bits%4];
1230 for (i++; i <= calc_buffer_size - 1; i++)
1234 void sc_calc(const void* value1, const void* value2, unsigned op, void *buffer)
1236 char *unused_res; /* temp buffer holding unused result of divmod */
1238 const char *val1 = (const char *)value1;
1239 const char *val2 = (const char *)value2;
1241 unused_res = alloca(calc_buffer_size);
1243 CLEAR_BUFFER(calc_buffer);
1246 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1251 _negate(val1, calc_buffer);
1252 DEBUGPRINTF_COMPUTATION(("negated: %s\n", sc_print_hex(calc_buffer)));
1255 DEBUGPRINTF_COMPUTATION(("| "));
1256 _bitor(val1, val2, calc_buffer);
1259 DEBUGPRINTF_COMPUTATION(("& "));
1260 _bitand(val1, val2, calc_buffer);
1263 DEBUGPRINTF_COMPUTATION(("^ "));
1264 _bitxor(val1, val2, calc_buffer);
1267 _bitnot(val1, calc_buffer);
1268 DEBUGPRINTF_COMPUTATION(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1271 DEBUGPRINTF_COMPUTATION(("+ "));
1272 _add(val1, val2, calc_buffer);
1275 DEBUGPRINTF_COMPUTATION(("- "));
1276 _sub(val1, val2, calc_buffer);
1279 DEBUGPRINTF_COMPUTATION(("* "));
1280 _mul(val1, val2, calc_buffer);
1283 DEBUGPRINTF_COMPUTATION(("/ "));
1284 _divmod(val1, val2, calc_buffer, unused_res);
1287 DEBUGPRINTF_COMPUTATION(("%% "));
1288 _divmod(val1, val2, unused_res, calc_buffer);
1293 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1294 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1296 if ((buffer != NULL) && (buffer != calc_buffer))
1298 memcpy(buffer, calc_buffer, calc_buffer_size);
1302 void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op, void* buffer)
1304 const char *val1 = (const char *)value1;
1305 const char *val2 = (const char *)value2;
1309 offset = sc_val_to_long(val2);
1311 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1315 DEBUGPRINTF_COMPUTATION(("<< %ld ", offset));
1316 _shl(val1, calc_buffer, offset, radius, sign);
1319 DEBUGPRINTF_COMPUTATION((">> %ld ", offset));
1320 _shr(val1, calc_buffer, offset, radius, sign, 0);
1323 DEBUGPRINTF_COMPUTATION((">>> %ld ", offset));
1324 _shr(val1, calc_buffer, offset, radius, sign, 1);
1327 DEBUGPRINTF_COMPUTATION(("<<>> %ld ", offset));
1328 _rot(val1, calc_buffer, offset, radius, sign);
1333 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1335 if ((buffer != NULL) && (buffer != calc_buffer))
1337 memmove(buffer, calc_buffer, calc_buffer_size);
1341 int sc_comp(const void* value1, const void* value2)
1343 int counter = calc_buffer_size - 1;
1344 const char *val1 = (const char *)value1;
1345 const char *val2 = (const char *)value2;
1347 /* compare signs first:
1348 * the loop below can only compare values of the same sign! */
1349 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1351 /* loop until two digits differ, the values are equal if there
1352 * are no such two digits */
1353 while (val1[counter] == val2[counter])
1356 if (counter < 0) return 0;
1359 /* the leftmost digit is the most significant, so this returns
1360 * the correct result.
1361 * This implies the digit enum is ordered */
1362 return (val1[counter] > val2[counter]) ? (1) : (-1);
1365 int sc_get_highest_set_bit(const void *value)
1367 const char *val = (const char*)value;
1370 high = calc_buffer_size * 4 - 1;
1372 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1373 if (val[counter] == SC_0) high -= 4;
1375 if (val[counter] > SC_7) return high;
1376 else if (val[counter] > SC_3) return high - 1;
1377 else if (val[counter] > SC_1) return high - 2;
1378 else return high - 3;
1384 int sc_get_lowest_set_bit(const void *value)
1386 const char *val = (const char*)value;
1390 sign = (_sign(val)==1)?(SC_0):(SC_F);
1393 for (counter = 0; counter < calc_buffer_size; counter++) {
1394 if (val[counter] == SC_0) low += 4;
1396 if (val[counter] < SC_2) return low;
1397 else if (val[counter] < SC_4) return low + 1;
1398 else if (val[counter] < SC_8) return low + 2;
1399 else return low + 3;
1405 int sc_is_zero(const void *value)
1407 const char* val = (const char *)value;
1410 for (counter = 0; counter < calc_buffer_size; counter++) {
1411 if (val[counter] != SC_0) return 0;
1416 int sc_is_negative(const void *value)
1418 return _sign(value) == -1;
1421 int sc_had_carry(void)
1426 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1428 const char *val = (const char *)value;
1429 int nibble_ofs = 2 * byte_ofs;
1432 /* the current scheme uses one byte to store a nibble */
1433 if (nibble_ofs >= len)
1436 res = _val(val[nibble_ofs]);
1437 if (len > nibble_ofs + 1)
1438 res |= _val(val[nibble_ofs + 1]) << 4;
1444 * convert to a string
1445 * FIXME: Doesn't check buffer bounds
1447 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode)
1449 static const char big_digits[] = "0123456789ABCDEF";
1450 static const char small_digits[] = "0123456789abcdef";
1452 char *base_val, *div1_res, *div2_res, *rem_res;
1453 int counter, nibbles, i, sign;
1456 const char *val = (const char *)value;
1460 const char *digits = small_digits;
1462 base_val = alloca(calc_buffer_size);
1463 div1_res = alloca(calc_buffer_size);
1464 div2_res = alloca(calc_buffer_size);
1465 rem_res = alloca(calc_buffer_size);
1467 pos = output_buffer + bit_pattern_size;
1472 bits = bit_pattern_size;
1473 #ifdef STRCALC_DEBUG_FULLPRINT
1477 nibbles = bits >> 2;
1481 digits = big_digits;
1483 for (counter = 0; counter < nibbles; ++counter) {
1484 *(--pos) = digits[_val(val[counter])];
1485 #ifdef STRCALC_DEBUG_GROUPPRINT
1486 if ((counter+1)%8 == 0)
1491 /* last nibble must be masked */
1493 x = and_table[_val(val[++counter])][bits & 3];
1494 *(--pos) = digits[_val(x)];
1497 /* now kill zeros */
1498 for (; counter > 1; --counter, ++pos) {
1499 #ifdef STRCALC_DEBUG_GROUPPRINT
1500 if (pos[0] == ' ') ++pos;
1508 for (counter = 0; counter < nibbles; ++counter) {
1510 p = binary_table[_val(val[counter])];
1517 /* last nibble must be masked */
1519 x = and_table[_val(val[++counter])][bits & 3];
1522 p = binary_table[_val(x)];
1529 /* now kill zeros */
1530 for (counter <<= 2; counter > 1; --counter, ++pos)
1537 memset(base_val, SC_0, calc_buffer_size);
1538 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1542 if (signed_mode && base == SC_DEC) {
1543 /* check for negative values */
1544 if (_bit(val, bits - 1)) {
1545 _negate(val, div2_res);
1551 /* transfer data into oscillating buffers */
1552 memset(div1_res, SC_0, calc_buffer_size);
1553 for (counter = 0; counter < nibbles; ++counter)
1554 div1_res[counter] = p[counter];
1556 /* last nibble must be masked */
1560 div1_res[counter] = and_table[_val(p[counter])][bits & 3];
1566 _divmod(m, base_val, n, rem_res);
1570 *(--pos) = digits[_val(rem_res[0])];
1573 for (i = 0; i < calc_buffer_size; ++i)
1584 printf("%i\n", base);
1591 void init_strcalc(int precision)
1593 if (calc_buffer == NULL) {
1594 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1596 /* round up to multiple of 4 */
1597 precision = (precision + 3) & ~3;
1599 bit_pattern_size = (precision);
1600 calc_buffer_size = (precision / 2);
1601 max_value_size = (precision / 4);
1603 calc_buffer = xmalloc(calc_buffer_size+1 * sizeof(char));
1604 output_buffer = xmalloc(bit_pattern_size+1 * sizeof(char));
1606 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));
1611 void finish_strcalc() {
1612 free(calc_buffer); calc_buffer = NULL;
1613 free(output_buffer); output_buffer = NULL;
1616 int sc_get_precision(void)
1618 return bit_pattern_size;