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 <assert.h> /* assertions */
29 #include <string.h> /* memset/memcmp */
30 #include <stdio.h> /* output for error messages */
31 #include <limits.h> /* definition of LONG_MIN, used in sc_get_val_from_long */
34 * local definitions and macros
36 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, CALC_BUFFER_SIZE)
37 #define _val(a) ((a)-SC_0)
38 #define _digit(a) ((a)+SC_0)
39 #define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
41 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__, __LINE__)
43 /* shortcut output for debugging */
44 # define sc_print_hex(a) sc_print((a), 0, SC_HEX)
45 # define sc_print_dec(a) sc_print((a), 0, SC_DEC)
46 # define sc_print_oct(a) sc_print((a), 0, SC_OCT)
47 # define sc_print_bin(a) sc_print((a), 0, SC_BIN)
49 #ifdef STRCALC_DEBUG_PRINTCOMP
50 # define DEBUGPRINTF_COMPUTATION(x) printf x
52 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
55 # define DEBUGPRINTF(x) printf x
57 # define DEBUGPRINTF(x) ((void)0)
65 static char *calc_buffer = NULL; /* buffer holding all results */
66 static char *output_buffer = NULL; /* buffer for output */
67 static int BIT_PATTERN_SIZE; /* maximum number of bits */
68 static int CALC_BUFFER_SIZE; /* size of internally stored values */
69 static int MAX_VALUE_SIZE; /* maximum size of values */
71 static int carry_flag; /* some computation set carry_flag: */
72 /* rightshift if bits were lost due to shifting */
73 /* division if there was a remainder */
75 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
76 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
78 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
79 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
81 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
83 static const char and_table[16][16] = {
84 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
85 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
87 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
88 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
90 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
91 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
93 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
94 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
96 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
97 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
99 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
100 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
102 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
103 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
105 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
106 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
108 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
109 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
111 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
112 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
114 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
115 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
117 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
118 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
120 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
121 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
123 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
124 SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
126 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
127 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
129 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
130 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
132 static const char or_table[16][16] = {
133 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
134 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
136 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
137 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
139 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
140 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
142 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
143 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
145 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
146 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
148 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
149 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
151 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
152 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
154 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
155 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
157 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
158 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
160 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
161 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
163 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
164 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
166 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
167 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
169 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
170 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
172 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
173 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
175 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
176 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
178 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
179 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
181 static char const xor_table[16][16] = {
182 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
183 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
185 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
186 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
188 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
189 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
191 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
192 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
194 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
195 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
197 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
198 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
200 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
201 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
203 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
204 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
206 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
207 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
209 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
210 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
212 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
213 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
215 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
216 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
218 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
219 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
221 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
222 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
224 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
225 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
227 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
228 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
231 static char const add_table[16][16][2] = {
232 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
233 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
234 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
235 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
237 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
238 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
239 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
240 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
242 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
243 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
244 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
245 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
247 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
248 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
249 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
250 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
252 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
253 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
254 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
255 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
257 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
258 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
259 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
260 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
262 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
263 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
264 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
265 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
267 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
268 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
269 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
270 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
272 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
273 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
274 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
275 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
277 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
278 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
279 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
280 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
282 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
283 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
284 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
285 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
287 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
288 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
289 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
290 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
292 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
293 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
294 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
295 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
297 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
298 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
299 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
300 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
302 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
303 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
304 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
305 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
307 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
308 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
309 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
310 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
313 static char const mul_table[16][16][2] = {
314 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
315 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
316 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
317 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
319 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
320 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
321 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
322 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
324 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
325 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
326 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
327 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
329 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
330 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
331 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
332 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
334 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
335 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
336 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
337 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
339 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
340 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
341 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
342 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
344 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
345 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
346 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
347 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
349 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
350 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
351 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
352 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
354 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
355 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
356 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
357 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
359 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
360 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
361 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
362 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
364 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
365 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
366 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
367 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
369 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
370 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
371 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
372 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
374 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
375 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
376 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
377 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
379 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
380 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
381 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
382 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
384 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
385 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
386 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
387 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
389 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
390 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
391 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
392 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
395 static char const shrs_table[16][4][2] = {
396 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
397 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
398 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
399 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
400 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
401 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
402 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
403 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
404 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
405 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
406 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
407 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
408 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
409 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
410 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
411 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
414 /* for converting to binary string */
415 static const char *binary_table[16] = {
416 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
417 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
420 /*****************************************************************************
422 *****************************************************************************/
423 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
424 const char *file, int line)
427 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
428 while (len-- && *str) printf("%c", *str++); printf("\n");
429 while (--pos) printf(" "); printf("^\n");
433 static void _bitnot(const char *val, char *buffer)
437 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
438 buffer[counter] = not_table[_val(val[counter])];
441 static void _bitor(const char *val1, const char *val2, char *buffer)
445 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
446 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
449 static void _bitxor(const char *val1, const char *val2, char *buffer)
453 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
454 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
457 static void _bitand(const char *val1, const char *val2, char *buffer)
461 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
462 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
465 static int _sign(const char *val)
467 return (val[CALC_BUFFER_SIZE-1] <= SC_7) ? (1) : (-1);
471 * returns non-zero if bit at position pos is set
473 static int _bit(const char *val, int pos)
476 int nibble = pos >> 2;
478 return _bitisset(val[nibble], bit);
481 static void _inc(char *val, char *buffer)
485 while (counter++ < CALC_BUFFER_SIZE)
494 /* No carry here, *val != SC_F */
495 *buffer = add_table[_val(*val)][SC_1][0];
499 /* here a carry could be lost, this is intended because this should
500 * happen only when a value changes sign. */
503 static void _negate(const char *val, char *buffer)
505 _bitnot(val, buffer);
506 _inc(buffer, buffer);
509 static void _add(const char *val1, const char *val2, char *buffer)
512 const char *add1, *add2;
515 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++)
517 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
518 add2 = add_table[_val(add1[0])][_val(carry)];
519 /* carry might be zero */
520 buffer[counter] = add2[0];
521 carry = add_table[_val(add1[1])][_val(add2[1])][0];
525 static void _mul(const char *val1, const char *val2, char *buffer)
527 char* temp_buffer; /* result buffer */
528 char* neg_val1; /* abs of val1 */
529 char* neg_val2; /* abs of val2 */
531 const char *mul, *add1, *add2; /* intermediate result containers */
532 char carry = SC_0; /* container for carries */
533 char sign = 0; /* marks result sign */
534 int c_inner, c_outer; /* loop counters */
536 temp_buffer = alloca(CALC_BUFFER_SIZE);
537 neg_val1 = alloca(CALC_BUFFER_SIZE);
538 neg_val2 = alloca(CALC_BUFFER_SIZE);
540 /* init result buffer to zeroes */
541 memset(temp_buffer, SC_0, CALC_BUFFER_SIZE);
543 /* the multiplication works only for positive values, for negative values *
544 * it is necessary to negate them and adjust the result accordingly */
545 if (_sign(val1) == -1) {
546 _negate(val1, neg_val1);
550 if (_sign(val2) == -1) {
551 _negate(val2, neg_val2);
556 for (c_outer = 0; c_outer < MAX_VALUE_SIZE; c_outer++)
558 if (val2[c_outer] != SC_0)
560 for (c_inner = 0; c_inner < MAX_VALUE_SIZE; c_inner++)
562 /* do the following calculation: *
563 * Add the current carry, the value at position c_outer+c_inner *
564 * and the result of the multiplication of val1[c_inner] and *
565 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
567 /* multiplicate the two digits */
568 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
569 /* add old value to result of multiplication */
570 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
571 /* add carry to the sum */
572 add2 = add_table[_val(add1[0])][_val(carry)];
574 /* all carries together result in new carry. This is always smaller *
576 * Both multiplicands, the carry and the value already in the temp *
577 * buffer are single digits and their value is therefore at most *
580 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
581 * The tables list all operations rem b, so the carry is at most *
582 * (b*b-1)rem b = -1rem b = b-1 */
583 carry = add_table[_val(mul[1])][_val(add1[1])][0];
584 carry = add_table[_val(carry)][_val(add2[1])][0];
586 temp_buffer[c_inner + c_outer] = add2[0];
589 /* A carry may hang over */
590 /* c_outer is always smaller than MAX_VALUE_SIZE! */
591 temp_buffer[MAX_VALUE_SIZE + c_outer] = carry;
597 _negate(temp_buffer, buffer);
599 memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
602 static void _sub(const char *val1, const char *val2, char *buffer)
604 char *temp_buffer; /* intermediate buffer to hold -val2 */
605 temp_buffer = alloca(CALC_BUFFER_SIZE);
607 _negate(val2, temp_buffer);
608 _add(val1, temp_buffer, buffer);
611 static void _push(const char digit, char *buffer)
615 for (counter = CALC_BUFFER_SIZE - 2; counter >= 0; counter--)
617 buffer[counter+1] = buffer[counter];
622 /* XXX: This is MOST slow */
623 static void _divmod(const char *dividend, const char *divisor, char *quot, char *rem)
625 const char *minus_divisor;
629 char sign = 0; /* remember result sign */
631 int c_dividend; /* loop counters */
633 neg_val1 = alloca(CALC_BUFFER_SIZE);
634 neg_val2 = alloca(CALC_BUFFER_SIZE);
636 /* clear result buffer */
637 memset(quot, SC_0, CALC_BUFFER_SIZE);
638 memset(rem, SC_0, CALC_BUFFER_SIZE);
640 /* if the divisor is zero this won't work (quot is zero) */
641 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
643 /* if the dividend is zero result is zero (quot is zero)*/
644 if (sc_comp(dividend, quot) == 0)
647 if (_sign(dividend) == -1)
649 _negate(dividend, neg_val1);
654 _negate(divisor, neg_val2);
655 if (_sign(divisor) == -1)
658 minus_divisor = divisor;
663 minus_divisor = neg_val2;
666 /* if divisor >= dividend division is easy
667 * (remember these are absolute values) */
668 switch (sc_comp(dividend, divisor))
670 case 0: /* dividend == divisor */
674 case -1: /* dividend < divisor */
675 memcpy(rem, dividend, CALC_BUFFER_SIZE);
678 default: /* unluckily division is necessary :( */
682 for (c_dividend = CALC_BUFFER_SIZE - 1; c_dividend >= 0; c_dividend--)
684 _push(dividend[c_dividend], rem);
687 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
689 /* subtract until the remainder becomes negative, this should
690 * be faster than comparing remainder with divisor */
691 _add(rem, minus_divisor, rem);
693 while (_sign(rem) == 1)
695 quot[0] = add_table[_val(quot[0])][SC_1][0];
696 _add(rem, minus_divisor, rem);
699 /* subtracted one too much */
700 _add(rem, divisor, rem);
704 carry_flag = !sc_is_zero(rem);
713 static void _shl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
722 assert((offset >= 0) || (0 && "negative leftshift"));
723 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
724 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
725 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
727 /* if shifting far enough the result is zero */
728 if (offset >= radius)
730 memset(buffer, SC_0, CALC_BUFFER_SIZE);
734 shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
737 /* shift the single digits some bytes (offset) and some bits (table)
739 for (counter = 0; counter < radius/4 - offset; counter++)
741 shl = mul_table[_val(val1[counter])][_val(shift)];
742 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
747 shl = mul_table[_val(val1[counter])][_val(shift)];
748 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
751 bitoffset = counter - 1;
754 /* fill with zeroes */
755 for (counter = 0; counter < offset; counter++) buffer[counter] = SC_0;
757 /* if the mode was signed, change sign when the mode's msb is now 1 */
758 offset = bitoffset + offset;
759 bitoffset = (radius-1) % 4;
760 if (is_signed && _bitisset(buffer[offset], bitoffset))
762 /* this sets the upper bits of the leftmost digit */
763 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
764 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
766 buffer[counter] = SC_F;
769 else if (is_signed && !_bitisset(buffer[offset], bitoffset))
771 /* this unsets the upper bits of the leftmost digit */
772 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
773 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
775 buffer[counter] = SC_0;
780 static void _shr(const char *val1, char *buffer, long offset, int radius, unsigned is_signed, int signed_shift)
791 assert((offset >= 0) || (0 && "negative rightshift"));
792 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
793 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
794 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
796 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
798 /* if shifting far enough the result is either 0 or -1 */
799 if (offset >= radius)
801 if (!sc_is_zero(val1)) {
804 memset(buffer, sign, CALC_BUFFER_SIZE);
811 /* check if any bits are lost, and set carry_flag if so */
812 for (counter = 0; counter < offset; counter++)
814 if (val1[counter] != 0)
820 if ((_val(val1[counter]) & ((1<<shift)-1)) != 0)
824 /* shift digits to the right with offset, carry and all */
826 if (radius/4 - offset > 0) {
827 buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
830 for (; counter < radius/4 - offset; counter++)
832 shrs = shrs_table[_val(val1[counter + offset])][shift];
833 buffer[counter] = shrs[0];
834 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
837 /* the last digit is special in regard of signed/unsigned shift */
838 bitoffset = radius%4;
839 msd = (radius/4<CALC_BUFFER_SIZE)?(val1[radius/4]):(sign); /* most significant digit */
841 /* remove sign bits if mode was signed and this is an unsigned shift */
842 if (!signed_shift && is_signed) {
843 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
846 shrs = shrs_table[_val(msd)][shift];
848 /* signed shift and signed mode and negative value means all bits to the left are set */
849 if (signed_shift && is_signed && (_sign(val1) == -1)) {
850 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
852 buffer[counter] = shrs[0];
855 if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
857 /* fill with SC_F or SC_0 depending on sign */
858 for (counter++; counter < CALC_BUFFER_SIZE; counter++)
860 buffer[counter] = sign;
864 /* positive: low-order -> high order, negative other direction */
865 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
868 temp1 = alloca(CALC_BUFFER_SIZE);
869 temp2 = alloca(CALC_BUFFER_SIZE);
871 offset = offset % radius;
873 /* rotation by multiples of the typelength is identity */
875 memmove(buffer, val1, CALC_BUFFER_SIZE);
879 _shl(val1, temp1, offset, radius, is_signed);
880 _shr(val1, temp2, radius - offset, radius, is_signed, 0);
881 _bitor(temp1, temp2, buffer);
882 carry_flag = 0; /* set by shr, but due to rot this is false */
885 /*****************************************************************************
886 * public functions, declared in strcalc.h
887 *****************************************************************************/
888 const void *sc_get_buffer(void)
890 return (void*)calc_buffer;
893 const int sc_get_buffer_length(void)
895 return CALC_BUFFER_SIZE;
898 /* XXX doesn't check for overflows */
899 void sc_val_from_str(const char *str, unsigned int len, void *buffer)
901 const char *orig_str = str;
902 unsigned int orig_len = len;
907 base = alloca(CALC_BUFFER_SIZE);
908 val = alloca(CALC_BUFFER_SIZE);
910 /* verify valid pointers (not null) */
912 /* a string no characters long is an error */
915 if (buffer == NULL) buffer = calc_buffer;
917 CLEAR_BUFFER(buffer);
918 memset(base, SC_0, CALC_BUFFER_SIZE);
919 memset(val, SC_0, CALC_BUFFER_SIZE);
921 /* strip leading spaces */
922 while ((len > 0) && (*str == ' ')) { len--; str++; }
924 /* if the first two characters are 0x or 0X -> hex
925 * if the first is a 0 -> oct
926 * else dec, strip leading -/+ and remember sign
928 * only a + or - sign is no number resulting in an error */
933 if (str[1] == 'x' || str[1] == 'X') /* hex */
937 base[1] = SC_1; base[0] = SC_0;
943 base[1] = SC_0; base[0] = SC_8;
951 base[1] = SC_0; base[0] = SC_A;
960 base[1] = SC_0; base[0] = SC_A;
964 default: /* dec, else would have begun with 0x or 0 */
965 base[1] = SC_0; base[0] = SC_A;
968 else /* dec, else would have begun with 0x or 0 */
970 base[1] = SC_0; base[0] = SC_A;
973 /* BEGIN string evaluation, from left to right */
984 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
986 val[0] = _digit((*str)-'a'+10);
988 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
997 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
999 val[0] = _digit((*str)-'A'+10);
1001 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1006 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
1008 val[0] = _digit((*str)-'0');
1010 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1022 val[0] = _digit((*str)-'0');
1027 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1028 } /* switch(*str) */
1030 /* Radix conversion from base b to base B:
1031 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1032 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1033 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1035 /* get ready for the next letter */
1039 } /* while (len > 0 ) */
1043 _negate(calc_buffer, calc_buffer);
1047 void sc_val_from_long(long value, void *buffer)
1050 char sign, is_minlong;
1052 if (buffer == NULL) buffer = calc_buffer;
1056 is_minlong = value == LONG_MIN;
1058 /* use absolute value, special treatment of MIN_LONG */
1066 CLEAR_BUFFER(buffer);
1068 while ((value != 0) && (pos < (char*)buffer + CALC_BUFFER_SIZE))
1070 *pos++ = _digit(value & 0xf);
1077 _inc(buffer, buffer);
1079 _negate(buffer, buffer);
1083 void sc_val_from_ulong(unsigned long value, void *buffer)
1087 if (buffer == NULL) buffer = calc_buffer;
1090 while (pos < (char*)buffer + CALC_BUFFER_SIZE)
1092 *pos++ = _digit(value & 0xf);
1097 long sc_val_to_long(const void *val)
1102 for (i = CALC_BUFFER_SIZE - 1; i >= 0; i--)
1104 l = (l << 4) + _val(((char *)val)[i]);
1109 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1114 if (buffer == NULL) buffer = calc_buffer;
1115 CLEAR_BUFFER(buffer);
1117 if (!sign) return; /* unsigned means minimum is 0(zero) */
1121 bits = num_bits - 1;
1122 for (i = 0; i < bits/4; i++)
1125 *pos++ = min_digit[bits%4];
1127 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1131 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1136 if (buffer == NULL) buffer = calc_buffer;
1137 CLEAR_BUFFER(buffer);
1140 bits = num_bits - sign;
1141 for (i = 0; i < bits/4; i++)
1144 *pos++ = max_digit[bits%4];
1146 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1150 void sc_calc(const void* value1, const void* value2, unsigned op, void *buffer)
1152 char *unused_res; /* temp buffer holding unused result of divmod */
1154 const char *val1 = (const char *)value1;
1155 const char *val2 = (const char *)value2;
1157 unused_res = alloca(CALC_BUFFER_SIZE);
1158 CLEAR_BUFFER(calc_buffer);
1161 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1166 _negate(val1, calc_buffer);
1167 DEBUGPRINTF_COMPUTATION(("negated: %s\n", sc_print_hex(calc_buffer)));
1170 DEBUGPRINTF_COMPUTATION(("| "));
1171 _bitor(val1, val2, calc_buffer);
1174 DEBUGPRINTF_COMPUTATION(("& "));
1175 _bitand(val1, val2, calc_buffer);
1178 DEBUGPRINTF_COMPUTATION(("^ "));
1179 _bitxor(val1, val2, calc_buffer);
1182 _bitnot(val1, calc_buffer);
1183 DEBUGPRINTF_COMPUTATION(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1186 DEBUGPRINTF_COMPUTATION(("+ "));
1187 _add(val1, val2, calc_buffer);
1190 DEBUGPRINTF_COMPUTATION(("- "));
1191 _sub(val1, val2, calc_buffer);
1194 DEBUGPRINTF_COMPUTATION(("* "));
1195 _mul(val1, val2, calc_buffer);
1198 DEBUGPRINTF_COMPUTATION(("/ "));
1199 _divmod(val1, val2, calc_buffer, unused_res);
1202 DEBUGPRINTF_COMPUTATION(("%% "));
1203 _divmod(val1, val2, unused_res, calc_buffer);
1208 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1209 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1211 if ((buffer != NULL) && (buffer != calc_buffer))
1213 memcpy(buffer, calc_buffer, CALC_BUFFER_SIZE);
1217 void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op, void* buffer)
1219 const char *val1 = (const char *)value1;
1220 const char *val2 = (const char *)value2;
1224 offset = sc_val_to_long(val2);
1226 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1230 DEBUGPRINTF_COMPUTATION(("<< %ld ", offset));
1231 _shl(val1, calc_buffer, offset, radius, sign);
1234 DEBUGPRINTF_COMPUTATION((">> %ld ", offset));
1235 _shr(val1, calc_buffer, offset, radius, sign, 0);
1238 DEBUGPRINTF_COMPUTATION((">>> %ld ", offset));
1239 _shr(val1, calc_buffer, offset, radius, sign, 1);
1242 DEBUGPRINTF_COMPUTATION(("<<>> %ld ", offset));
1243 _rot(val1, calc_buffer, offset, radius, sign);
1248 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1250 if ((buffer != NULL) && (buffer != calc_buffer))
1252 memmove(buffer, calc_buffer, CALC_BUFFER_SIZE);
1256 int sc_comp(const void* value1, const void* value2)
1258 int counter = CALC_BUFFER_SIZE - 1;
1259 const char *val1 = (const char *)value1;
1260 const char *val2 = (const char *)value2;
1262 /* compare signs first:
1263 * the loop below can only compare values of the same sign! */
1264 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1266 /* loop until two digits differ, the values are equal if there
1267 * are no such two digits */
1268 while (val1[counter] == val2[counter])
1271 if (counter < 0) return 0;
1274 /* the leftmost digit is the most significant, so this returns
1275 * the correct result.
1276 * This implies the digit enum is ordered */
1277 return (val1[counter] > val2[counter]) ? (1) : (-1);
1280 int sc_get_highest_set_bit(const void *value)
1282 const char *val = (const char*)value;
1285 high = CALC_BUFFER_SIZE * 4 - 1;
1287 for (counter = CALC_BUFFER_SIZE-1; counter >= 0; counter--) {
1288 if (val[counter] == SC_0) high -= 4;
1290 if (val[counter] > SC_7) return high;
1291 else if (val[counter] > SC_3) return high - 1;
1292 else if (val[counter] > SC_1) return high - 2;
1293 else return high - 3;
1299 int sc_get_lowest_set_bit(const void *value)
1301 const char *val = (const char*)value;
1305 sign = (_sign(val)==1)?(SC_0):(SC_F);
1308 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++) {
1309 if (val[counter] == SC_0) low += 4;
1311 if (val[counter] < SC_2) return low;
1312 else if (val[counter] < SC_4) return low + 1;
1313 else if (val[counter] < SC_8) return low + 2;
1314 else return low + 3;
1320 int sc_is_zero(const void *value)
1322 const char* val = (const char *)value;
1325 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++) {
1326 if (val[counter] != SC_0) return 0;
1331 int sc_is_negative(const void *value)
1333 return _sign(value) == -1;
1336 int sc_had_carry(void)
1341 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1343 const char *val = (const char *)value;
1344 unsigned nibble_ofs = 2 * byte_ofs;
1347 /* the current scheme uses one byte to store a nibble */
1348 if (nibble_ofs >= len)
1351 res = _val(val[nibble_ofs]);
1352 if (len > nibble_ofs + 1)
1353 res |= _val(val[nibble_ofs + 1]) << 4;
1359 * convert to a string
1360 * XXX Doesn't check buffer bounds
1362 const char *sc_print(const void *value, unsigned bits, enum base_t base)
1364 static const char big_digits[] = "0123456789ABCDEF";
1365 static const char small_digits[] = "0123456789abcdef";
1367 char *base_val, *div1_res, *div2_res, *rem_res;
1368 int counter, nibbles, i, sign;
1371 const char *val = (const char *)value;
1375 const char *digits = small_digits;
1377 base_val = alloca(CALC_BUFFER_SIZE);
1378 div1_res = alloca(CALC_BUFFER_SIZE);
1379 div2_res = alloca(CALC_BUFFER_SIZE);
1380 rem_res = alloca(CALC_BUFFER_SIZE);
1382 pos = output_buffer + BIT_PATTERN_SIZE;
1387 bits = BIT_PATTERN_SIZE;
1388 #ifdef STRCALC_DEBUG_FULLPRINT
1392 nibbles = bits >> 2;
1396 digits = big_digits;
1398 for (counter = 0; counter < nibbles; ++counter) {
1399 *(--pos) = digits[_val(val[counter])];
1400 #ifdef STRCALC_DEBUG_GROUPPRINT
1401 if ((counter+1)%8 == 0)
1406 /* last nibble must be masked */
1408 x = and_table[_val(val[++counter])][bits & 3];
1409 *(--pos) = digits[_val(x)];
1412 /* now kill zeros */
1413 for (; counter > 1; --counter, ++pos) {
1414 #ifdef STRCALC_DEBUG_GROUPPRINT
1415 if (pos[0] == ' ') ++pos;
1423 for (counter = 0; counter < nibbles; ++counter) {
1425 p = binary_table[_val(val[counter])];
1432 /* last nibble must be masked */
1434 x = and_table[_val(val[++counter])][bits & 3];
1437 p = binary_table[_val(x)];
1444 /* now kill zeros */
1445 for (counter <<= 2; counter > 1; --counter, ++pos)
1452 memset(base_val, SC_0, CALC_BUFFER_SIZE);
1453 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1457 if (base == SC_DEC) {
1458 /* check for negative values */
1459 if (_bit(val, bits - 1)) {
1460 _negate(val, div2_res);
1466 /* transfer data into oscilating buffers */
1467 memset(div1_res, SC_0, CALC_BUFFER_SIZE);
1468 for (counter = 0; counter < nibbles; ++counter)
1469 div1_res[counter] = p[counter];
1471 /* last nibble must be masked */
1475 div1_res[counter] = and_table[_val(p[counter])][bits & 3];
1481 _divmod(m, base_val, n, rem_res);
1485 *(--pos) = digits[_val(rem_res[0])];
1488 for (i = 0; i < sizeof(div1_res); ++i)
1499 printf("%i\n", base);
1506 void init_strcalc(int precision)
1508 if (calc_buffer == NULL) {
1509 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1511 /* round up to multiple of 4 */
1512 if (precision & 0x3) precision += 4 - (precision&0x3);
1514 BIT_PATTERN_SIZE = (precision);
1515 CALC_BUFFER_SIZE = (precision / 2);
1516 MAX_VALUE_SIZE = (precision / 4);
1518 calc_buffer = malloc(CALC_BUFFER_SIZE+1 * sizeof(char));
1519 output_buffer = malloc(BIT_PATTERN_SIZE+1 * sizeof(char));
1521 if (calc_buffer == NULL || output_buffer == NULL)
1523 assert(0 && "malloc failed");
1527 DEBUGPRINTF(("init strcalc: \n\tPRECISION: %d\n\tCALC_BUFFER_SIZE = %d\n\tMAX_VALUE_SIZE = %d\n\tbuffer pointer: %p\n", precision, CALC_BUFFER_SIZE, MAX_VALUE_SIZE, calc_buffer));
1530 int sc_get_precision(void)
1532 return BIT_PATTERN_SIZE;