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 zex_digit[4] = { SC_1, SC_3, SC_7, SC_F };
82 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
83 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
85 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
86 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
88 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
90 static const char and_table[16][16] = {
91 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
92 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
94 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
95 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
97 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
98 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
100 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
101 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
103 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
104 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
106 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
107 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
109 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
110 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
112 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
113 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
115 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
116 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
118 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
119 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
121 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
122 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
124 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
125 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
127 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
128 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
130 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
131 SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
133 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
134 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
136 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
137 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
139 static const char or_table[16][16] = {
140 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
141 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
143 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
144 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
146 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
147 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
149 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
150 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
152 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
153 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
155 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
156 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
158 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
159 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
161 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
162 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
164 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
165 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
167 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
168 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
170 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
171 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
173 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
174 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
176 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
177 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
179 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
180 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
182 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
183 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
185 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
186 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
188 static char const xor_table[16][16] = {
189 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
190 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
192 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
193 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
195 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
196 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
198 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
199 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
201 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
202 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
204 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
205 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
207 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
208 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
210 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
211 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
213 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
214 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
216 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
217 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
219 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
220 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
222 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
223 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
225 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
226 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
228 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
229 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
231 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
232 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
234 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
235 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
238 static char const add_table[16][16][2] = {
239 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
240 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
241 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
242 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
244 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
245 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
246 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
247 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
249 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
250 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
251 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
252 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
254 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
255 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
256 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
257 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
259 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
260 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
261 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
262 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
264 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
265 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
266 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
267 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
269 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
270 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
271 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
272 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
274 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
275 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
276 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
277 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
279 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
280 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
281 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
282 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
284 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
285 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
286 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
287 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
289 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
290 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
291 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
292 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
294 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
295 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
296 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
297 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
299 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
300 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
301 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
302 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
304 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
305 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
306 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
307 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
309 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
310 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
311 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
312 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
314 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
315 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
316 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
317 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
320 static char const mul_table[16][16][2] = {
321 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
322 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
323 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
324 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
326 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
327 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
328 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
329 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
331 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
332 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
333 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
334 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
336 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
337 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
338 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
339 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
341 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
342 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
343 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
344 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
346 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
347 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
348 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
349 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
351 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
352 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
353 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
354 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
356 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
357 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
358 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
359 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
361 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
362 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
363 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
364 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
366 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
367 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
368 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
369 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
371 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
372 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
373 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
374 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
376 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
377 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
378 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
379 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
381 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
382 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
383 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
384 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
386 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
387 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
388 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
389 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
391 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
392 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
393 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
394 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
396 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
397 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
398 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
399 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
402 static char const shrs_table[16][4][2] = {
403 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
404 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
405 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
406 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
407 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
408 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
409 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
410 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
411 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
412 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
413 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
414 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
415 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
416 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
417 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
418 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
421 /** converting a digit to a binary string */
422 static const char *binary_table[16] = {
423 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
424 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
427 /*****************************************************************************
429 *****************************************************************************/
430 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
431 const char *file, int line)
434 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
435 while (len-- && *str) printf("%c", *str++); printf("\n");
436 while (--pos) printf(" "); printf("^\n");
441 * implements the bitwise NOT operation
443 static void _bitnot(const char *val, char *buffer)
447 for (counter = 0; counter<calc_buffer_size; counter++)
448 buffer[counter] = not_table[_val(val[counter])];
452 * implements the bitwise OR operation
454 static void _bitor(const char *val1, const char *val2, char *buffer)
458 for (counter = 0; counter<calc_buffer_size; counter++)
459 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
463 * implements the bitwise eXclusive OR operation
465 static void _bitxor(const char *val1, const char *val2, char *buffer)
469 for (counter = 0; counter<calc_buffer_size; counter++)
470 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
474 * implements the bitwise AND operation
476 static void _bitand(const char *val1, const char *val2, char *buffer)
480 for (counter = 0; counter<calc_buffer_size; counter++)
481 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
485 * returns the sign bit.
487 * @todo This implementation is wrong, as it returns the highest bit of the buffer
488 * NOT the highest bit depending on the real mode
490 static int _sign(const char *val)
492 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
496 * returns non-zero if bit at position pos is set
498 static int _bit(const char *val, int pos)
501 int nibble = pos >> 2;
503 return _bitisset(val[nibble], bit);
507 * Implements a fast ADD + 1
509 static void _inc(const char *val, char *buffer)
513 while (counter++ < calc_buffer_size)
522 /* No carry here, *val != SC_F */
523 *buffer = add_table[_val(*val)][SC_1][0];
527 /* here a carry could be lost, this is intended because this should
528 * happen only when a value changes sign. */
532 * Implements a unary MINUS
534 static void _negate(const char *val, char *buffer)
536 _bitnot(val, buffer);
537 _inc(buffer, buffer);
541 * Implements a binary ADD
543 * @todo The implementation of carry is wrong, as it is the
544 * calc_buffer_size carry, not the mode depending
546 static void _add(const char *val1, const char *val2, char *buffer)
549 const char *add1, *add2;
552 for (counter = 0; counter < calc_buffer_size; counter++)
554 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
555 add2 = add_table[_val(add1[0])][_val(carry)];
556 /* carry might be zero */
557 buffer[counter] = add2[0];
558 carry = add_table[_val(add1[1])][_val(add2[1])][0];
560 carry_flag = carry != SC_0;
564 * Implements a binary SUB
566 static void _sub(const char *val1, const char *val2, char *buffer)
568 char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
570 _negate(val2, temp_buffer);
571 _add(val1, temp_buffer, buffer);
575 * Implements a binary MUL
577 static void _mul(const char *val1, const char *val2, char *buffer)
579 char *temp_buffer; /* result buffer */
580 char *neg_val1; /* abs of val1 */
581 char *neg_val2; /* abs of val2 */
583 const char *mul, *add1, *add2; /* intermediate result containers */
584 char carry = SC_0; /* container for carries */
585 char sign = 0; /* marks result sign */
586 int c_inner, c_outer; /* loop counters */
588 temp_buffer = alloca(calc_buffer_size);
589 neg_val1 = alloca(calc_buffer_size);
590 neg_val2 = alloca(calc_buffer_size);
592 /* init result buffer to zeroes */
593 memset(temp_buffer, SC_0, calc_buffer_size);
595 /* the multiplication works only for positive values, for negative values *
596 * it is necessary to negate them and adjust the result accordingly */
597 if (_sign(val1) == -1) {
598 _negate(val1, neg_val1);
602 if (_sign(val2) == -1) {
603 _negate(val2, neg_val2);
608 for (c_outer = 0; c_outer < max_value_size; c_outer++)
610 if (val2[c_outer] != SC_0)
612 for (c_inner = 0; c_inner < max_value_size; c_inner++)
614 /* do the following calculation: *
615 * Add the current carry, the value at position c_outer+c_inner *
616 * and the result of the multiplication of val1[c_inner] and *
617 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
619 /* multiplicate the two digits */
620 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
621 /* add old value to result of multiplication */
622 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
623 /* add carry to the sum */
624 add2 = add_table[_val(add1[0])][_val(carry)];
626 /* all carries together result in new carry. This is always smaller *
628 * Both multiplicands, the carry and the value already in the temp *
629 * buffer are single digits and their value is therefore at most *
632 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
633 * The tables list all operations rem b, so the carry is at most *
634 * (b*b-1)rem b = -1rem b = b-1 */
635 carry = add_table[_val(mul[1])][_val(add1[1])][0];
636 carry = add_table[_val(carry)][_val(add2[1])][0];
638 temp_buffer[c_inner + c_outer] = add2[0];
641 /* A carry may hang over */
642 /* c_outer is always smaller than max_value_size! */
643 temp_buffer[max_value_size + c_outer] = carry;
649 _negate(temp_buffer, buffer);
651 memcpy(buffer, temp_buffer, calc_buffer_size);
655 * Shift the buffer to left and add a 4 bit digit
657 static void _push(const char digit, char *buffer)
661 for (counter = calc_buffer_size - 2; counter >= 0; counter--)
663 buffer[counter+1] = buffer[counter];
669 * Implements truncating integer division and remainder.
671 * Note: This is MOST slow
673 static void _divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
675 const char *dividend = rDividend;
676 const char *minus_divisor;
680 char div_sign = 0; /* remember division result sign */
681 char rem_sign = 0; /* remember remainder esult sign */
683 int c_dividend; /* loop counters */
685 neg_val1 = alloca(calc_buffer_size);
686 neg_val2 = alloca(calc_buffer_size);
688 /* clear result buffer */
689 memset(quot, SC_0, calc_buffer_size);
690 memset(rem, SC_0, calc_buffer_size);
692 /* if the divisor is zero this won't work (quot is zero) */
693 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
695 /* if the dividend is zero result is zero (quot is zero)*/
696 if (sc_comp(dividend, quot) == 0)
699 if (_sign(dividend) == -1) {
700 _negate(dividend, neg_val1);
706 _negate(divisor, neg_val2);
707 if (_sign(divisor) == -1) {
709 minus_divisor = divisor;
713 minus_divisor = neg_val2;
715 /* if divisor >= dividend division is easy
716 * (remember these are absolute values) */
717 switch (sc_comp(dividend, divisor))
719 case 0: /* dividend == divisor */
723 case -1: /* dividend < divisor */
724 memcpy(rem, rDividend, calc_buffer_size);
727 default: /* unluckily division is necessary :( */
731 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--)
733 _push(dividend[c_dividend], rem);
736 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
738 /* subtract until the remainder becomes negative, this should
739 * be faster than comparing remainder with divisor */
740 _add(rem, minus_divisor, rem);
742 while (_sign(rem) == 1)
744 quot[0] = add_table[_val(quot[0])][SC_1][0];
745 _add(rem, minus_divisor, rem);
748 /* subtracted one too much */
749 _add(rem, divisor, rem);
753 /* sets carry if remainder is non-zero ??? */
754 carry_flag = !sc_is_zero(rem);
764 * Implements a Shift Left, which can either preserve the sign bit
767 * @todo Assertions seems to be wrong
769 static void _shl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
778 assert((offset >= 0) || (0 && "negative leftshift"));
779 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
780 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
781 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
783 /* if shifting far enough the result is zero */
784 if (offset >= radius)
786 memset(buffer, SC_0, calc_buffer_size);
790 shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
793 /* shift the single digits some bytes (offset) and some bits (table)
795 for (counter = 0; counter < radius/4 - offset; counter++)
797 shl = mul_table[_val(val1[counter])][_val(shift)];
798 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
803 shl = mul_table[_val(val1[counter])][_val(shift)];
804 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
807 bitoffset = counter - 1;
810 /* fill with zeroes */
811 for (counter = 0; counter < offset; counter++) buffer[counter] = SC_0;
813 /* if the mode was signed, change sign when the mode's msb is now 1 */
814 offset = bitoffset + offset;
815 bitoffset = (radius-1) % 4;
816 if (is_signed && _bitisset(buffer[offset], bitoffset))
818 /* this sets the upper bits of the leftmost digit */
819 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
820 for (counter = offset+1; counter < calc_buffer_size; counter++)
822 buffer[counter] = SC_F;
825 else if (is_signed && !_bitisset(buffer[offset], bitoffset))
827 /* this clears the upper bits of the leftmost digit */
828 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
829 for (counter = offset+1; counter < calc_buffer_size; counter++)
831 buffer[counter] = SC_0;
837 * Implements a Shift Right, which can either preserve the sign bit
840 * @todo Assertions seems to be wrong
842 static void _shr(const char *val1, char *buffer, long offset, int radius, unsigned is_signed, int signed_shift)
853 assert((offset >= 0) || (0 && "negative rightshift"));
854 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
855 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
857 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
859 /* if shifting far enough the result is either 0 or -1 */
860 if (offset >= radius)
862 if (!sc_is_zero(val1)) {
865 memset(buffer, sign, calc_buffer_size);
872 /* check if any bits are lost, and set carry_flag if so */
873 for (counter = 0; counter < offset; counter++)
875 if (val1[counter] != 0)
881 if ((_val(val1[counter]) & ((1<<shift)-1)) != 0)
885 /* shift digits to the right with offset, carry and all */
887 if (radius/4 - offset > 0) {
888 buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
891 for (; counter < radius/4 - offset; counter++)
893 shrs = shrs_table[_val(val1[counter + offset])][shift];
894 buffer[counter] = shrs[0];
895 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
898 /* the last digit is special in regard of signed/unsigned shift */
899 bitoffset = radius%4;
900 msd = (radius/4<calc_buffer_size)?(val1[radius/4]):(sign); /* most significant digit */
902 /* remove sign bits if mode was signed and this is an unsigned shift */
903 if (!signed_shift && is_signed) {
904 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
907 shrs = shrs_table[_val(msd)][shift];
909 /* signed shift and signed mode and negative value means all bits to the left are set */
910 if (signed_shift && is_signed && (_sign(val1) == -1)) {
911 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
913 buffer[counter] = shrs[0];
916 if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
918 /* fill with SC_F or SC_0 depending on sign */
919 for (counter++; counter < calc_buffer_size; counter++)
921 buffer[counter] = sign;
926 * Implements a Rotate Right.
927 * positive: low-order -> high order, negative other direction
929 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
932 temp1 = alloca(calc_buffer_size);
933 temp2 = alloca(calc_buffer_size);
935 offset = offset % radius;
937 /* rotation by multiples of the type length is identity */
939 memmove(buffer, val1, calc_buffer_size);
943 _shl(val1, temp1, offset, radius, is_signed);
944 _shr(val1, temp2, radius - offset, radius, is_signed, 0);
945 _bitor(temp1, temp2, buffer);
946 carry_flag = 0; /* set by shr, but due to rot this is false */
949 /*****************************************************************************
950 * public functions, declared in strcalc.h
951 *****************************************************************************/
952 const void *sc_get_buffer(void)
954 return (void*)calc_buffer;
957 int sc_get_buffer_length(void)
959 return calc_buffer_size;
963 * Do sign extension if the mode is signed, otherwise to zero extension.
965 void sign_extend(char *calc_buffer, ir_mode *mode) {
966 int bits = get_mode_size_bits(mode) - 1;
967 int nibble = bits >> 2;
968 int max = max_digit[bits & 3];
971 if (mode_is_signed(mode)) {
972 if (calc_buffer[nibble] > max) {
973 /* sign bit is set, we need sign expansion */
975 for (i = nibble + 1; i < calc_buffer_size; ++i)
976 calc_buffer[i] = SC_F;
977 calc_buffer[nibble] = or_table[(int)calc_buffer[nibble]][(int)sex_digit[bits & 3]];
979 /* set all bits to zero */
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]];
985 /* do zero extension */
986 for (i = nibble + 1; i < calc_buffer_size; ++i)
987 calc_buffer[i] = SC_0;
988 calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
992 /* FIXME doesn't check for overflows */
993 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode)
995 const char *orig_str = str;
996 unsigned int orig_len = len;
1001 base = alloca(calc_buffer_size);
1002 val = alloca(calc_buffer_size);
1004 /* verify valid pointers (not null) */
1006 /* a string no characters long is an error */
1009 if (buffer == NULL) buffer = calc_buffer;
1011 CLEAR_BUFFER(buffer);
1015 /* strip leading spaces */
1016 while ((len > 0) && (*str == ' ')) { len--; str++; }
1018 /* if the first two characters are 0x or 0X -> hex
1019 * if the first is a 0 -> oct
1020 * else dec, strip leading -/+ and remember sign
1022 * only a + or - sign is no number resulting in an error */
1027 if (str[1] == 'x' || str[1] == 'X') /* hex */
1031 base[1] = SC_1; base[0] = SC_0;
1037 base[1] = SC_0; base[0] = SC_8;
1045 base[1] = SC_0; base[0] = SC_A;
1054 base[1] = SC_0; base[0] = SC_A;
1058 default: /* dec, else would have begun with 0x or 0 */
1059 base[1] = SC_0; base[0] = SC_A;
1062 else /* dec, else would have begun with 0x or 0 */
1064 base[1] = SC_0; base[0] = SC_A;
1067 /* BEGIN string evaluation, from left to right */
1078 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1080 val[0] = _digit((*str)-'a'+10);
1082 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1091 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1093 val[0] = _digit((*str)-'A'+10);
1095 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1100 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
1102 val[0] = _digit((*str)-'0');
1104 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1116 val[0] = _digit((*str)-'0');
1121 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1122 } /* switch(*str) */
1124 /* Radix conversion from base b to base B:
1125 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1126 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1127 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1129 /* get ready for the next letter */
1133 } /* while (len > 0 ) */
1136 _negate(calc_buffer, calc_buffer);
1138 /* beware: even if hex numbers have no sign, we need sign extension here */
1139 sign_extend(calc_buffer, mode);
1142 void sc_val_from_long(long value, void *buffer)
1145 char sign, is_minlong;
1147 if (buffer == NULL) buffer = calc_buffer;
1151 is_minlong = value == LONG_MIN;
1153 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
1161 CLEAR_BUFFER(buffer);
1163 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size))
1165 *pos++ = _digit(value & 0xf);
1171 _inc(buffer, buffer);
1173 _negate(buffer, buffer);
1177 void sc_val_from_ulong(unsigned long value, void *buffer)
1181 if (buffer == NULL) buffer = calc_buffer;
1184 while (pos < (unsigned char *)buffer + calc_buffer_size)
1186 *pos++ = (unsigned char)_digit(value & 0xf);
1191 long sc_val_to_long(const void *val)
1196 for (i = calc_buffer_size - 1; i >= 0; i--)
1198 l = (l << 4) + _val(((char *)val)[i]);
1203 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1208 if (buffer == NULL) buffer = calc_buffer;
1209 CLEAR_BUFFER(buffer);
1211 if (!sign) return; /* unsigned means minimum is 0(zero) */
1215 bits = num_bits - 1;
1216 for (i = 0; i < bits/4; i++)
1219 *pos++ = min_digit[bits%4];
1221 for (i++; i <= calc_buffer_size - 1; i++)
1225 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1230 if (buffer == NULL) buffer = calc_buffer;
1231 CLEAR_BUFFER(buffer);
1234 bits = num_bits - sign;
1235 for (i = 0; i < bits/4; i++)
1238 *pos++ = max_digit[bits%4];
1240 for (i++; i <= calc_buffer_size - 1; i++)
1244 void sc_calc(const void* value1, const void* value2, unsigned op, void *buffer)
1246 char *unused_res; /* temp buffer holding unused result of divmod */
1248 const char *val1 = (const char *)value1;
1249 const char *val2 = (const char *)value2;
1251 unused_res = alloca(calc_buffer_size);
1253 CLEAR_BUFFER(calc_buffer);
1256 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1261 _negate(val1, calc_buffer);
1262 DEBUGPRINTF_COMPUTATION(("negated: %s\n", sc_print_hex(calc_buffer)));
1265 DEBUGPRINTF_COMPUTATION(("| "));
1266 _bitor(val1, val2, calc_buffer);
1269 DEBUGPRINTF_COMPUTATION(("& "));
1270 _bitand(val1, val2, calc_buffer);
1273 DEBUGPRINTF_COMPUTATION(("^ "));
1274 _bitxor(val1, val2, calc_buffer);
1277 _bitnot(val1, calc_buffer);
1278 DEBUGPRINTF_COMPUTATION(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1281 DEBUGPRINTF_COMPUTATION(("+ "));
1282 _add(val1, val2, calc_buffer);
1285 DEBUGPRINTF_COMPUTATION(("- "));
1286 _sub(val1, val2, calc_buffer);
1289 DEBUGPRINTF_COMPUTATION(("* "));
1290 _mul(val1, val2, calc_buffer);
1293 DEBUGPRINTF_COMPUTATION(("/ "));
1294 _divmod(val1, val2, calc_buffer, unused_res);
1297 DEBUGPRINTF_COMPUTATION(("%% "));
1298 _divmod(val1, val2, unused_res, calc_buffer);
1303 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1304 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1306 if ((buffer != NULL) && (buffer != calc_buffer))
1308 memcpy(buffer, calc_buffer, calc_buffer_size);
1312 void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op, void* buffer)
1314 const char *val1 = (const char *)value1;
1315 const char *val2 = (const char *)value2;
1319 offset = sc_val_to_long(val2);
1321 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1325 DEBUGPRINTF_COMPUTATION(("<< %ld ", offset));
1326 _shl(val1, calc_buffer, offset, radius, sign);
1329 DEBUGPRINTF_COMPUTATION((">> %ld ", offset));
1330 _shr(val1, calc_buffer, offset, radius, sign, 0);
1333 DEBUGPRINTF_COMPUTATION((">>> %ld ", offset));
1334 _shr(val1, calc_buffer, offset, radius, sign, 1);
1337 DEBUGPRINTF_COMPUTATION(("<<>> %ld ", offset));
1338 _rot(val1, calc_buffer, offset, radius, sign);
1343 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1345 if ((buffer != NULL) && (buffer != calc_buffer))
1347 memmove(buffer, calc_buffer, calc_buffer_size);
1351 int sc_comp(const void* value1, const void* value2)
1353 int counter = calc_buffer_size - 1;
1354 const char *val1 = (const char *)value1;
1355 const char *val2 = (const char *)value2;
1357 /* compare signs first:
1358 * the loop below can only compare values of the same sign! */
1359 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1361 /* loop until two digits differ, the values are equal if there
1362 * are no such two digits */
1363 while (val1[counter] == val2[counter])
1366 if (counter < 0) return 0;
1369 /* the leftmost digit is the most significant, so this returns
1370 * the correct result.
1371 * This implies the digit enum is ordered */
1372 return (val1[counter] > val2[counter]) ? (1) : (-1);
1375 int sc_get_highest_set_bit(const void *value)
1377 const char *val = (const char*)value;
1380 high = calc_buffer_size * 4 - 1;
1382 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1383 if (val[counter] == SC_0) high -= 4;
1385 if (val[counter] > SC_7) return high;
1386 else if (val[counter] > SC_3) return high - 1;
1387 else if (val[counter] > SC_1) return high - 2;
1388 else return high - 3;
1394 int sc_get_lowest_set_bit(const void *value)
1396 const char *val = (const char*)value;
1400 sign = (_sign(val)==1)?(SC_0):(SC_F);
1403 for (counter = 0; counter < calc_buffer_size; counter++) {
1404 if (val[counter] == SC_0) low += 4;
1406 if (val[counter] < SC_2) return low;
1407 else if (val[counter] < SC_4) return low + 1;
1408 else if (val[counter] < SC_8) return low + 2;
1409 else return low + 3;
1415 int sc_is_zero(const void *value)
1417 const char* val = (const char *)value;
1420 for (counter = 0; counter < calc_buffer_size; counter++) {
1421 if (val[counter] != SC_0) return 0;
1426 int sc_is_negative(const void *value)
1428 return _sign(value) == -1;
1431 int sc_had_carry(void)
1436 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1438 const char *val = (const char *)value;
1439 int nibble_ofs = 2 * byte_ofs;
1442 /* the current scheme uses one byte to store a nibble */
1443 if (nibble_ofs >= len)
1446 res = _val(val[nibble_ofs]);
1447 if (len > nibble_ofs + 1)
1448 res |= _val(val[nibble_ofs + 1]) << 4;
1454 * convert to a string
1455 * FIXME: Doesn't check buffer bounds
1457 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode)
1459 static const char big_digits[] = "0123456789ABCDEF";
1460 static const char small_digits[] = "0123456789abcdef";
1462 char *base_val, *div1_res, *div2_res, *rem_res;
1463 int counter, nibbles, i, sign;
1466 const char *val = (const char *)value;
1470 const char *digits = small_digits;
1472 base_val = alloca(calc_buffer_size);
1473 div1_res = alloca(calc_buffer_size);
1474 div2_res = alloca(calc_buffer_size);
1475 rem_res = alloca(calc_buffer_size);
1477 pos = output_buffer + bit_pattern_size;
1482 bits = bit_pattern_size;
1483 #ifdef STRCALC_DEBUG_FULLPRINT
1487 nibbles = bits >> 2;
1491 digits = big_digits;
1493 for (counter = 0; counter < nibbles; ++counter) {
1494 *(--pos) = digits[_val(val[counter])];
1495 #ifdef STRCALC_DEBUG_GROUPPRINT
1496 if ((counter+1)%8 == 0)
1501 /* last nibble must be masked */
1503 x = and_table[_val(val[++counter])][bits & 3];
1504 *(--pos) = digits[_val(x)];
1507 /* now kill zeros */
1508 for (; counter > 1; --counter, ++pos) {
1509 #ifdef STRCALC_DEBUG_GROUPPRINT
1510 if (pos[0] == ' ') ++pos;
1518 for (counter = 0; counter < nibbles; ++counter) {
1520 p = binary_table[_val(val[counter])];
1527 /* last nibble must be masked */
1529 x = and_table[_val(val[++counter])][bits & 3];
1532 p = binary_table[_val(x)];
1539 /* now kill zeros */
1540 for (counter <<= 2; counter > 1; --counter, ++pos)
1547 memset(base_val, SC_0, calc_buffer_size);
1548 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1552 if (signed_mode && base == SC_DEC) {
1553 /* check for negative values */
1554 if (_bit(val, bits - 1)) {
1555 _negate(val, div2_res);
1561 /* transfer data into oscillating buffers */
1562 memset(div1_res, SC_0, calc_buffer_size);
1563 for (counter = 0; counter < nibbles; ++counter)
1564 div1_res[counter] = p[counter];
1566 /* last nibble must be masked */
1570 div1_res[counter] = and_table[_val(p[counter])][bits & 3];
1576 _divmod(m, base_val, n, rem_res);
1580 *(--pos) = digits[_val(rem_res[0])];
1583 for (i = 0; i < calc_buffer_size; ++i)
1594 printf("%i\n", base);
1601 void init_strcalc(int precision)
1603 if (calc_buffer == NULL) {
1604 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1606 /* round up to multiple of 4 */
1607 precision = (precision + 3) & ~3;
1609 bit_pattern_size = (precision);
1610 calc_buffer_size = (precision / 2);
1611 max_value_size = (precision / 4);
1613 calc_buffer = xmalloc(calc_buffer_size+1 * sizeof(char));
1614 output_buffer = xmalloc(bit_pattern_size+1 * sizeof(char));
1616 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));
1621 void finish_strcalc() {
1622 free(calc_buffer); calc_buffer = NULL;
1623 free(output_buffer); output_buffer = NULL;
1626 int sc_get_precision(void)
1628 return bit_pattern_size;