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.
22 # include <string.h> /* memset/memcmp */
24 #include <assert.h> /* assertions */
25 #include <stdio.h> /* output for error messages */
26 #include <limits.h> /* definition of LONG_MIN, used in sc_get_val_from_long */
32 * local definitions and macros
34 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, calc_buffer_size)
35 #define _val(a) ((a)-SC_0)
36 #define _digit(a) ((a)+SC_0)
37 #define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
39 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__, __LINE__)
41 /* shortcut output for debugging */
42 # define sc_print_hex(a) sc_print((a), 0, SC_HEX, 0)
43 # define sc_print_dec(a) sc_print((a), 0, SC_DEC, 1)
44 # define sc_print_oct(a) sc_print((a), 0, SC_OCT, 0)
45 # define sc_print_bin(a) sc_print((a), 0, SC_BIN, 0)
47 #ifdef STRCALC_DEBUG_PRINTCOMP
48 # define DEBUGPRINTF_COMPUTATION(x) printf x
50 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
53 # define DEBUGPRINTF(x) printf x
55 # define DEBUGPRINTF(x) ((void)0)
62 static char *calc_buffer = NULL; /* buffer holding all results */
63 static char *output_buffer = NULL; /* buffer for output */
64 static int bit_pattern_size; /* maximum number of bits */
65 static int calc_buffer_size; /* size of internally stored values */
66 static int max_value_size; /* maximum size of values */
68 static int carry_flag; /**< some computation set the carry_flag:
69 - right shift if bits were lost due to shifting
70 - division if there was a remainder
71 However, the meaning of carry is machine dependent
72 and often defined in other ways! */
74 static const char sex_digit[4] = { SC_E, SC_C, SC_8, SC_0 };
75 static const char zex_digit[4] = { SC_1, SC_3, SC_7, SC_F };
76 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
77 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
79 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
80 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
82 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
84 static const char and_table[16][16] = {
85 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
86 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
88 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
89 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
91 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
92 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
94 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
95 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
97 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
98 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
100 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
101 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
103 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
104 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
106 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
107 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
109 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
110 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
112 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
113 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
115 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
116 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
118 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
119 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
121 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
122 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
124 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
125 SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
127 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
128 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
130 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
131 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
133 static const char or_table[16][16] = {
134 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
135 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
137 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
138 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
140 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
141 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
143 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
144 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
146 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
147 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
149 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
150 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
152 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
153 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
155 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
156 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
158 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
159 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
161 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
162 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
164 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
165 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
167 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
168 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
170 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
171 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
173 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
174 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
176 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
177 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
179 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
180 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
182 static char const xor_table[16][16] = {
183 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
184 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
186 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
187 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
189 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
190 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
192 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
193 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
195 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
196 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
198 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
199 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
201 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
202 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
204 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
205 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
207 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
208 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
210 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
211 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
213 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
214 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
216 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
217 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
219 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
220 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
222 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
223 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
225 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
226 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
228 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
229 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
232 static char const add_table[16][16][2] = {
233 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
234 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
235 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
236 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
238 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
239 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
240 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
241 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
243 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
244 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
245 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
246 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
248 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
249 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
250 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
251 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
253 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
254 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
255 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
256 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
258 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
259 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
260 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
261 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
263 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
264 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
265 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
266 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
268 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
269 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
270 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
271 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
273 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
274 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
275 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
276 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
278 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
279 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
280 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
281 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
283 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
284 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
285 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
286 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
288 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
289 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
290 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
291 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
293 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
294 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
295 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
296 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
298 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
299 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
300 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
301 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
303 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
304 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
305 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
306 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
308 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
309 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
310 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
311 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
314 static char const mul_table[16][16][2] = {
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 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
320 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
321 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
322 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
323 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
325 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
326 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
327 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
328 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
330 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
331 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
332 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
333 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
335 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
336 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
337 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
338 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
340 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
341 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
342 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
343 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
345 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
346 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
347 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
348 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
350 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
351 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
352 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
353 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
355 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
356 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
357 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
358 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
360 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
361 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
362 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
363 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
365 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
366 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
367 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
368 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
370 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
371 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
372 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
373 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
375 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
376 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
377 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
378 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
380 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
381 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
382 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
383 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
385 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
386 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
387 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
388 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
390 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
391 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
392 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
393 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
396 static char const shrs_table[16][4][2] = {
397 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
398 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
399 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
400 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
401 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
402 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
403 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
404 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
405 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
406 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
407 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
408 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
409 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
410 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
411 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
412 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
415 /** converting a digit to a binary string */
416 static const char *binary_table[16] = {
417 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
418 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
421 /*****************************************************************************
423 *****************************************************************************/
424 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
425 const char *file, int line)
428 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
429 while (len-- && *str) printf("%c", *str++); printf("\n");
430 while (--pos) printf(" "); printf("^\n");
435 * implements the bitwise NOT operation
437 static void _bitnot(const char *val, char *buffer)
441 for (counter = 0; counter<calc_buffer_size; counter++)
442 buffer[counter] = not_table[_val(val[counter])];
446 * implements the bitwise OR operation
448 static void _bitor(const char *val1, const char *val2, char *buffer)
452 for (counter = 0; counter<calc_buffer_size; counter++)
453 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
457 * implements the bitwise eXclusive OR operation
459 static void _bitxor(const char *val1, const char *val2, char *buffer)
463 for (counter = 0; counter<calc_buffer_size; counter++)
464 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
468 * implements the bitwise AND operation
470 static void _bitand(const char *val1, const char *val2, char *buffer)
474 for (counter = 0; counter<calc_buffer_size; counter++)
475 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
479 * returns the sign bit.
481 * @todo This implementation is wrong, as it returns the highest bit of the buffer
482 * NOT the highest bit depending on the real mode
484 static int _sign(const char *val)
486 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
490 * returns non-zero if bit at position pos is set
492 static int _bit(const char *val, int pos)
495 int nibble = pos >> 2;
497 return _bitisset(val[nibble], bit);
501 * Implements a fast ADD + 1
503 static void _inc(const char *val, char *buffer)
507 while (counter++ < calc_buffer_size)
516 /* No carry here, *val != SC_F */
517 *buffer = add_table[_val(*val)][SC_1][0];
521 /* here a carry could be lost, this is intended because this should
522 * happen only when a value changes sign. */
526 * Implements a unary MINUS
528 static void _negate(const char *val, char *buffer)
530 _bitnot(val, buffer);
531 _inc(buffer, buffer);
535 * Implements a binary ADD
537 * @todo The implementation of carry is wrong, as it is the
538 * calc_buffer_size carry, not the mode depending
540 static void _add(const char *val1, const char *val2, char *buffer)
543 const char *add1, *add2;
546 for (counter = 0; counter < calc_buffer_size; counter++)
548 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
549 add2 = add_table[_val(add1[0])][_val(carry)];
550 /* carry might be zero */
551 buffer[counter] = add2[0];
552 carry = add_table[_val(add1[1])][_val(add2[1])][0];
554 carry_flag = carry != SC_0;
558 * Implements a binary SUB
560 static void _sub(const char *val1, const char *val2, char *buffer)
562 char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
564 _negate(val2, temp_buffer);
565 _add(val1, temp_buffer, buffer);
569 * Implements a binary MUL
571 static void _mul(const char *val1, const char *val2, char *buffer)
573 char *temp_buffer; /* result buffer */
574 char *neg_val1; /* abs of val1 */
575 char *neg_val2; /* abs of val2 */
577 const char *mul, *add1, *add2; /* intermediate result containers */
578 char carry = SC_0; /* container for carries */
579 char sign = 0; /* marks result sign */
580 int c_inner, c_outer; /* loop counters */
582 temp_buffer = alloca(calc_buffer_size);
583 neg_val1 = alloca(calc_buffer_size);
584 neg_val2 = alloca(calc_buffer_size);
586 /* init result buffer to zeroes */
587 memset(temp_buffer, SC_0, calc_buffer_size);
589 /* the multiplication works only for positive values, for negative values *
590 * it is necessary to negate them and adjust the result accordingly */
591 if (_sign(val1) == -1) {
592 _negate(val1, neg_val1);
596 if (_sign(val2) == -1) {
597 _negate(val2, neg_val2);
602 for (c_outer = 0; c_outer < max_value_size; c_outer++)
604 if (val2[c_outer] != SC_0)
606 for (c_inner = 0; c_inner < max_value_size; c_inner++)
608 /* do the following calculation: *
609 * Add the current carry, the value at position c_outer+c_inner *
610 * and the result of the multiplication of val1[c_inner] and *
611 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
613 /* multiplicate the two digits */
614 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
615 /* add old value to result of multiplication */
616 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
617 /* add carry to the sum */
618 add2 = add_table[_val(add1[0])][_val(carry)];
620 /* all carries together result in new carry. This is always smaller *
622 * Both multiplicands, the carry and the value already in the temp *
623 * buffer are single digits and their value is therefore at most *
626 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
627 * The tables list all operations rem b, so the carry is at most *
628 * (b*b-1)rem b = -1rem b = b-1 */
629 carry = add_table[_val(mul[1])][_val(add1[1])][0];
630 carry = add_table[_val(carry)][_val(add2[1])][0];
632 temp_buffer[c_inner + c_outer] = add2[0];
635 /* A carry may hang over */
636 /* c_outer is always smaller than max_value_size! */
637 temp_buffer[max_value_size + c_outer] = carry;
643 _negate(temp_buffer, buffer);
645 memcpy(buffer, temp_buffer, calc_buffer_size);
649 * Shift the buffer to left and add a 4 bit digit
651 static void _push(const char digit, char *buffer)
655 for (counter = calc_buffer_size - 2; counter >= 0; counter--)
657 buffer[counter+1] = buffer[counter];
663 * Implements truncating integer division and remainder.
665 * Note: This is MOST slow
667 static void _divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
669 const char *dividend = rDividend;
670 const char *minus_divisor;
674 char div_sign = 0; /* remember division result sign */
675 char rem_sign = 0; /* remember remainder esult sign */
677 int c_dividend; /* loop counters */
679 neg_val1 = alloca(calc_buffer_size);
680 neg_val2 = alloca(calc_buffer_size);
682 /* clear result buffer */
683 memset(quot, SC_0, calc_buffer_size);
684 memset(rem, SC_0, calc_buffer_size);
686 /* if the divisor is zero this won't work (quot is zero) */
687 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
689 /* if the dividend is zero result is zero (quot is zero)*/
690 if (sc_comp(dividend, quot) == 0)
693 if (_sign(dividend) == -1) {
694 _negate(dividend, neg_val1);
700 _negate(divisor, neg_val2);
701 if (_sign(divisor) == -1) {
703 minus_divisor = divisor;
707 minus_divisor = neg_val2;
709 /* if divisor >= dividend division is easy
710 * (remember these are absolute values) */
711 switch (sc_comp(dividend, divisor))
713 case 0: /* dividend == divisor */
717 case -1: /* dividend < divisor */
718 memcpy(rem, rDividend, calc_buffer_size);
721 default: /* unluckily division is necessary :( */
725 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--)
727 _push(dividend[c_dividend], rem);
730 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
732 /* subtract until the remainder becomes negative, this should
733 * be faster than comparing remainder with divisor */
734 _add(rem, minus_divisor, rem);
736 while (_sign(rem) == 1)
738 quot[0] = add_table[_val(quot[0])][SC_1][0];
739 _add(rem, minus_divisor, rem);
742 /* subtracted one too much */
743 _add(rem, divisor, rem);
747 /* sets carry if remainder is non-zero ??? */
748 carry_flag = !sc_is_zero(rem);
758 * Implements a Shift Left, which can either preserve the sign bit
761 * @todo Assertions seems to be wrong
763 static void _shl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
772 assert((offset >= 0) || (0 && "negative leftshift"));
773 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
774 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
775 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
777 /* if shifting far enough the result is zero */
778 if (offset >= radius)
780 memset(buffer, SC_0, calc_buffer_size);
784 shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
787 /* shift the single digits some bytes (offset) and some bits (table)
789 for (counter = 0; counter < radius/4 - offset; counter++)
791 shl = mul_table[_val(val1[counter])][_val(shift)];
792 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
797 shl = mul_table[_val(val1[counter])][_val(shift)];
798 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
801 bitoffset = counter - 1;
804 /* fill with zeroes */
805 for (counter = 0; counter < offset; counter++) buffer[counter] = SC_0;
807 /* if the mode was signed, change sign when the mode's msb is now 1 */
808 offset = bitoffset + offset;
809 bitoffset = (radius-1) % 4;
810 if (is_signed && _bitisset(buffer[offset], bitoffset))
812 /* this sets the upper bits of the leftmost digit */
813 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
814 for (counter = offset+1; counter < calc_buffer_size; counter++)
816 buffer[counter] = SC_F;
819 else if (is_signed && !_bitisset(buffer[offset], bitoffset))
821 /* this clears the upper bits of the leftmost digit */
822 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
823 for (counter = offset+1; counter < calc_buffer_size; counter++)
825 buffer[counter] = SC_0;
831 * Implements a Shift Right, which can either preserve the sign bit
834 * @todo Assertions seems to be wrong
836 static void _shr(const char *val1, char *buffer, long offset, int radius, unsigned is_signed, int signed_shift)
847 assert((offset >= 0) || (0 && "negative rightshift"));
848 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
849 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
851 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
853 /* if shifting far enough the result is either 0 or -1 */
854 if (offset >= radius)
856 if (!sc_is_zero(val1)) {
859 memset(buffer, sign, calc_buffer_size);
866 /* check if any bits are lost, and set carry_flag if so */
867 for (counter = 0; counter < offset; counter++)
869 if (val1[counter] != 0)
875 if ((_val(val1[counter]) & ((1<<shift)-1)) != 0)
879 /* shift digits to the right with offset, carry and all */
881 if (radius/4 - offset > 0) {
882 buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
885 for (; counter < radius/4 - offset; counter++)
887 shrs = shrs_table[_val(val1[counter + offset])][shift];
888 buffer[counter] = shrs[0];
889 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
892 /* the last digit is special in regard of signed/unsigned shift */
893 bitoffset = radius%4;
894 msd = (radius/4<calc_buffer_size)?(val1[radius/4]):(sign); /* most significant digit */
896 /* remove sign bits if mode was signed and this is an unsigned shift */
897 if (!signed_shift && is_signed) {
898 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
901 shrs = shrs_table[_val(msd)][shift];
903 /* signed shift and signed mode and negative value means all bits to the left are set */
904 if (signed_shift && is_signed && (_sign(val1) == -1)) {
905 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
907 buffer[counter] = shrs[0];
910 if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
912 /* fill with SC_F or SC_0 depending on sign */
913 for (counter++; counter < calc_buffer_size; counter++)
915 buffer[counter] = sign;
920 * Implements a Rotate Right.
921 * positive: low-order -> high order, negative other direction
923 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
926 temp1 = alloca(calc_buffer_size);
927 temp2 = alloca(calc_buffer_size);
929 offset = offset % radius;
931 /* rotation by multiples of the type length is identity */
933 memmove(buffer, val1, calc_buffer_size);
937 _shl(val1, temp1, offset, radius, is_signed);
938 _shr(val1, temp2, radius - offset, radius, is_signed, 0);
939 _bitor(temp1, temp2, buffer);
940 carry_flag = 0; /* set by shr, but due to rot this is false */
943 /*****************************************************************************
944 * public functions, declared in strcalc.h
945 *****************************************************************************/
946 const void *sc_get_buffer(void)
948 return (void*)calc_buffer;
951 int sc_get_buffer_length(void)
953 return calc_buffer_size;
957 * Do sign extension if the mode is signed, otherwise to zero extension.
959 void sign_extend(char *calc_buffer, ir_mode *mode) {
960 int bits = get_mode_size_bits(mode) - 1;
961 int nibble = bits >> 2;
962 int max = max_digit[bits & 3];
965 if (mode_is_signed(mode)) {
966 if (calc_buffer[nibble] > max) {
967 /* sign bit is set, we need sign expansion */
969 for (i = nibble + 1; i < calc_buffer_size; ++i)
970 calc_buffer[i] = SC_F;
971 calc_buffer[nibble] = or_table[(int)calc_buffer[nibble]][(int)sex_digit[bits & 3]];
973 /* set all bits to zero */
974 for (i = nibble + 1; i < calc_buffer_size; ++i)
975 calc_buffer[i] = SC_0;
976 calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
979 /* do zero extension */
980 for (i = nibble + 1; i < calc_buffer_size; ++i)
981 calc_buffer[i] = SC_0;
982 calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
986 /* FIXME doesn't check for overflows */
987 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode)
989 const char *orig_str = str;
990 unsigned int orig_len = len;
995 base = alloca(calc_buffer_size);
996 val = alloca(calc_buffer_size);
998 /* verify valid pointers (not null) */
1000 /* a string no characters long is an error */
1003 if (buffer == NULL) buffer = calc_buffer;
1005 CLEAR_BUFFER(buffer);
1009 /* strip leading spaces */
1010 while ((len > 0) && (*str == ' ')) { len--; str++; }
1012 /* if the first two characters are 0x or 0X -> hex
1013 * if the first is a 0 -> oct
1014 * else dec, strip leading -/+ and remember sign
1016 * only a + or - sign is no number resulting in an error */
1021 if (str[1] == 'x' || str[1] == 'X') /* hex */
1025 base[1] = SC_1; base[0] = SC_0;
1031 base[1] = SC_0; base[0] = SC_8;
1039 base[1] = SC_0; base[0] = SC_A;
1048 base[1] = SC_0; base[0] = SC_A;
1052 default: /* dec, else would have begun with 0x or 0 */
1053 base[1] = SC_0; base[0] = SC_A;
1056 else /* dec, else would have begun with 0x or 0 */
1058 base[1] = SC_0; base[0] = SC_A;
1061 /* BEGIN string evaluation, from left to right */
1072 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1074 val[0] = _digit((*str)-'a'+10);
1076 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1085 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1087 val[0] = _digit((*str)-'A'+10);
1089 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1094 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
1096 val[0] = _digit((*str)-'0');
1098 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1110 val[0] = _digit((*str)-'0');
1115 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1116 } /* switch(*str) */
1118 /* Radix conversion from base b to base B:
1119 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1120 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1121 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1123 /* get ready for the next letter */
1127 } /* while (len > 0 ) */
1130 _negate(calc_buffer, calc_buffer);
1132 /* beware: even if hex numbers have no sign, we need sign extension here */
1133 sign_extend(calc_buffer, mode);
1136 void sc_val_from_long(long value, void *buffer)
1139 char sign, is_minlong;
1141 if (buffer == NULL) buffer = calc_buffer;
1145 is_minlong = value == LONG_MIN;
1147 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
1155 CLEAR_BUFFER(buffer);
1157 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size))
1159 *pos++ = _digit(value & 0xf);
1165 _inc(buffer, buffer);
1167 _negate(buffer, buffer);
1171 void sc_val_from_ulong(unsigned long value, void *buffer)
1175 if (buffer == NULL) buffer = calc_buffer;
1178 while (pos < (unsigned char *)buffer + calc_buffer_size)
1180 *pos++ = (unsigned char)_digit(value & 0xf);
1185 long sc_val_to_long(const void *val)
1190 for (i = calc_buffer_size - 1; i >= 0; i--)
1192 l = (l << 4) + _val(((char *)val)[i]);
1197 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1202 if (buffer == NULL) buffer = calc_buffer;
1203 CLEAR_BUFFER(buffer);
1205 if (!sign) return; /* unsigned means minimum is 0(zero) */
1209 bits = num_bits - 1;
1210 for (i = 0; i < bits/4; i++)
1213 *pos++ = min_digit[bits%4];
1215 for (i++; i <= calc_buffer_size - 1; i++)
1219 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1224 if (buffer == NULL) buffer = calc_buffer;
1225 CLEAR_BUFFER(buffer);
1228 bits = num_bits - sign;
1229 for (i = 0; i < bits/4; i++)
1232 *pos++ = max_digit[bits%4];
1234 for (i++; i <= calc_buffer_size - 1; i++)
1238 void sc_calc(const void* value1, const void* value2, unsigned op, void *buffer)
1240 char *unused_res; /* temp buffer holding unused result of divmod */
1242 const char *val1 = (const char *)value1;
1243 const char *val2 = (const char *)value2;
1245 unused_res = alloca(calc_buffer_size);
1247 CLEAR_BUFFER(calc_buffer);
1250 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1255 _negate(val1, calc_buffer);
1256 DEBUGPRINTF_COMPUTATION(("negated: %s\n", sc_print_hex(calc_buffer)));
1259 DEBUGPRINTF_COMPUTATION(("| "));
1260 _bitor(val1, val2, calc_buffer);
1263 DEBUGPRINTF_COMPUTATION(("& "));
1264 _bitand(val1, val2, calc_buffer);
1267 DEBUGPRINTF_COMPUTATION(("^ "));
1268 _bitxor(val1, val2, calc_buffer);
1271 _bitnot(val1, calc_buffer);
1272 DEBUGPRINTF_COMPUTATION(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1275 DEBUGPRINTF_COMPUTATION(("+ "));
1276 _add(val1, val2, calc_buffer);
1279 DEBUGPRINTF_COMPUTATION(("- "));
1280 _sub(val1, val2, calc_buffer);
1283 DEBUGPRINTF_COMPUTATION(("* "));
1284 _mul(val1, val2, calc_buffer);
1287 DEBUGPRINTF_COMPUTATION(("/ "));
1288 _divmod(val1, val2, calc_buffer, unused_res);
1291 DEBUGPRINTF_COMPUTATION(("%% "));
1292 _divmod(val1, val2, unused_res, calc_buffer);
1297 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1298 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1300 if ((buffer != NULL) && (buffer != calc_buffer))
1302 memcpy(buffer, calc_buffer, calc_buffer_size);
1306 void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op, void* buffer)
1308 const char *val1 = (const char *)value1;
1309 const char *val2 = (const char *)value2;
1313 offset = sc_val_to_long(val2);
1315 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1319 DEBUGPRINTF_COMPUTATION(("<< %ld ", offset));
1320 _shl(val1, calc_buffer, offset, radius, sign);
1323 DEBUGPRINTF_COMPUTATION((">> %ld ", offset));
1324 _shr(val1, calc_buffer, offset, radius, sign, 0);
1327 DEBUGPRINTF_COMPUTATION((">>> %ld ", offset));
1328 _shr(val1, calc_buffer, offset, radius, sign, 1);
1331 DEBUGPRINTF_COMPUTATION(("<<>> %ld ", offset));
1332 _rot(val1, calc_buffer, offset, radius, sign);
1337 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1339 if ((buffer != NULL) && (buffer != calc_buffer))
1341 memmove(buffer, calc_buffer, calc_buffer_size);
1345 int sc_comp(const void* value1, const void* value2)
1347 int counter = calc_buffer_size - 1;
1348 const char *val1 = (const char *)value1;
1349 const char *val2 = (const char *)value2;
1351 /* compare signs first:
1352 * the loop below can only compare values of the same sign! */
1353 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1355 /* loop until two digits differ, the values are equal if there
1356 * are no such two digits */
1357 while (val1[counter] == val2[counter])
1360 if (counter < 0) return 0;
1363 /* the leftmost digit is the most significant, so this returns
1364 * the correct result.
1365 * This implies the digit enum is ordered */
1366 return (val1[counter] > val2[counter]) ? (1) : (-1);
1369 int sc_get_highest_set_bit(const void *value)
1371 const char *val = (const char*)value;
1374 high = calc_buffer_size * 4 - 1;
1376 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1377 if (val[counter] == SC_0) high -= 4;
1379 if (val[counter] > SC_7) return high;
1380 else if (val[counter] > SC_3) return high - 1;
1381 else if (val[counter] > SC_1) return high - 2;
1382 else return high - 3;
1388 int sc_get_lowest_set_bit(const void *value)
1390 const char *val = (const char*)value;
1394 sign = (_sign(val)==1)?(SC_0):(SC_F);
1397 for (counter = 0; counter < calc_buffer_size; counter++) {
1398 if (val[counter] == SC_0) low += 4;
1400 if (val[counter] < SC_2) return low;
1401 else if (val[counter] < SC_4) return low + 1;
1402 else if (val[counter] < SC_8) return low + 2;
1403 else return low + 3;
1409 int sc_is_zero(const void *value)
1411 const char* val = (const char *)value;
1414 for (counter = 0; counter < calc_buffer_size; counter++) {
1415 if (val[counter] != SC_0) return 0;
1420 int sc_is_negative(const void *value)
1422 return _sign(value) == -1;
1425 int sc_had_carry(void)
1430 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1432 const char *val = (const char *)value;
1433 int nibble_ofs = 2 * byte_ofs;
1436 /* the current scheme uses one byte to store a nibble */
1437 if (nibble_ofs >= len)
1440 res = _val(val[nibble_ofs]);
1441 if (len > nibble_ofs + 1)
1442 res |= _val(val[nibble_ofs + 1]) << 4;
1448 * convert to a string
1449 * FIXME: Doesn't check buffer bounds
1451 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode)
1453 static const char big_digits[] = "0123456789ABCDEF";
1454 static const char small_digits[] = "0123456789abcdef";
1456 char *base_val, *div1_res, *div2_res, *rem_res;
1457 int counter, nibbles, i, sign;
1460 const char *val = (const char *)value;
1464 const char *digits = small_digits;
1466 base_val = alloca(calc_buffer_size);
1467 div1_res = alloca(calc_buffer_size);
1468 div2_res = alloca(calc_buffer_size);
1469 rem_res = alloca(calc_buffer_size);
1471 pos = output_buffer + bit_pattern_size;
1476 bits = bit_pattern_size;
1477 #ifdef STRCALC_DEBUG_FULLPRINT
1481 nibbles = bits >> 2;
1485 digits = big_digits;
1487 for (counter = 0; counter < nibbles; ++counter) {
1488 *(--pos) = digits[_val(val[counter])];
1489 #ifdef STRCALC_DEBUG_GROUPPRINT
1490 if ((counter+1)%8 == 0)
1495 /* last nibble must be masked */
1497 x = and_table[_val(val[++counter])][bits & 3];
1498 *(--pos) = digits[_val(x)];
1501 /* now kill zeros */
1502 for (; counter > 1; --counter, ++pos) {
1503 #ifdef STRCALC_DEBUG_GROUPPRINT
1504 if (pos[0] == ' ') ++pos;
1512 for (counter = 0; counter < nibbles; ++counter) {
1514 p = binary_table[_val(val[counter])];
1521 /* last nibble must be masked */
1523 x = and_table[_val(val[++counter])][bits & 3];
1526 p = binary_table[_val(x)];
1533 /* now kill zeros */
1534 for (counter <<= 2; counter > 1; --counter, ++pos)
1541 memset(base_val, SC_0, calc_buffer_size);
1542 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1546 if (signed_mode && base == SC_DEC) {
1547 /* check for negative values */
1548 if (_bit(val, bits - 1)) {
1549 _negate(val, div2_res);
1555 /* transfer data into oscillating buffers */
1556 memset(div1_res, SC_0, calc_buffer_size);
1557 for (counter = 0; counter < nibbles; ++counter)
1558 div1_res[counter] = p[counter];
1560 /* last nibble must be masked */
1564 div1_res[counter] = and_table[_val(p[counter])][bits & 3];
1570 _divmod(m, base_val, n, rem_res);
1574 *(--pos) = digits[_val(rem_res[0])];
1577 for (i = 0; i < calc_buffer_size; ++i)
1588 printf("%i\n", base);
1595 void init_strcalc(int precision)
1597 if (calc_buffer == NULL) {
1598 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1600 /* round up to multiple of 4 */
1601 precision = (precision + 3) & ~3;
1603 bit_pattern_size = (precision);
1604 calc_buffer_size = (precision / 2);
1605 max_value_size = (precision / 4);
1607 calc_buffer = xmalloc(calc_buffer_size+1 * sizeof(char));
1608 output_buffer = xmalloc(bit_pattern_size+1 * sizeof(char));
1610 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));
1615 void finish_strcalc() {
1616 free(calc_buffer); calc_buffer = NULL;
1617 free(output_buffer); output_buffer = NULL;
1620 int sc_get_precision(void)
1622 return bit_pattern_size;