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];
523 carry_flag = carry != SC_0;
526 static void _mul(const char *val1, const char *val2, char *buffer)
528 char* temp_buffer; /* result buffer */
529 char* neg_val1; /* abs of val1 */
530 char* neg_val2; /* abs of val2 */
532 const char *mul, *add1, *add2; /* intermediate result containers */
533 char carry = SC_0; /* container for carries */
534 char sign = 0; /* marks result sign */
535 int c_inner, c_outer; /* loop counters */
537 temp_buffer = alloca(CALC_BUFFER_SIZE);
538 neg_val1 = alloca(CALC_BUFFER_SIZE);
539 neg_val2 = alloca(CALC_BUFFER_SIZE);
541 /* init result buffer to zeroes */
542 memset(temp_buffer, SC_0, CALC_BUFFER_SIZE);
544 /* the multiplication works only for positive values, for negative values *
545 * it is necessary to negate them and adjust the result accordingly */
546 if (_sign(val1) == -1) {
547 _negate(val1, neg_val1);
551 if (_sign(val2) == -1) {
552 _negate(val2, neg_val2);
557 for (c_outer = 0; c_outer < MAX_VALUE_SIZE; c_outer++)
559 if (val2[c_outer] != SC_0)
561 for (c_inner = 0; c_inner < MAX_VALUE_SIZE; c_inner++)
563 /* do the following calculation: *
564 * Add the current carry, the value at position c_outer+c_inner *
565 * and the result of the multiplication of val1[c_inner] and *
566 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
568 /* multiplicate the two digits */
569 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
570 /* add old value to result of multiplication */
571 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
572 /* add carry to the sum */
573 add2 = add_table[_val(add1[0])][_val(carry)];
575 /* all carries together result in new carry. This is always smaller *
577 * Both multiplicands, the carry and the value already in the temp *
578 * buffer are single digits and their value is therefore at most *
581 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
582 * The tables list all operations rem b, so the carry is at most *
583 * (b*b-1)rem b = -1rem b = b-1 */
584 carry = add_table[_val(mul[1])][_val(add1[1])][0];
585 carry = add_table[_val(carry)][_val(add2[1])][0];
587 temp_buffer[c_inner + c_outer] = add2[0];
590 /* A carry may hang over */
591 /* c_outer is always smaller than MAX_VALUE_SIZE! */
592 temp_buffer[MAX_VALUE_SIZE + c_outer] = carry;
598 _negate(temp_buffer, buffer);
600 memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
603 static void _sub(const char *val1, const char *val2, char *buffer)
605 char *temp_buffer; /* intermediate buffer to hold -val2 */
606 temp_buffer = alloca(CALC_BUFFER_SIZE);
608 _negate(val2, temp_buffer);
609 _add(val1, temp_buffer, buffer);
612 static void _push(const char digit, char *buffer)
616 for (counter = CALC_BUFFER_SIZE - 2; counter >= 0; counter--)
618 buffer[counter+1] = buffer[counter];
623 /* XXX: This is MOST slow */
624 static void _divmod(const char *dividend, const char *divisor, char *quot, char *rem)
626 const char *minus_divisor;
630 char sign = 0; /* remember result sign */
632 int c_dividend; /* loop counters */
634 neg_val1 = alloca(CALC_BUFFER_SIZE);
635 neg_val2 = alloca(CALC_BUFFER_SIZE);
637 /* clear result buffer */
638 memset(quot, SC_0, CALC_BUFFER_SIZE);
639 memset(rem, SC_0, CALC_BUFFER_SIZE);
641 /* if the divisor is zero this won't work (quot is zero) */
642 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
644 /* if the dividend is zero result is zero (quot is zero)*/
645 if (sc_comp(dividend, quot) == 0)
648 if (_sign(dividend) == -1)
650 _negate(dividend, neg_val1);
655 _negate(divisor, neg_val2);
656 if (_sign(divisor) == -1)
659 minus_divisor = divisor;
664 minus_divisor = neg_val2;
667 /* if divisor >= dividend division is easy
668 * (remember these are absolute values) */
669 switch (sc_comp(dividend, divisor))
671 case 0: /* dividend == divisor */
675 case -1: /* dividend < divisor */
676 memcpy(rem, dividend, CALC_BUFFER_SIZE);
679 default: /* unluckily division is necessary :( */
683 for (c_dividend = CALC_BUFFER_SIZE - 1; c_dividend >= 0; c_dividend--)
685 _push(dividend[c_dividend], rem);
688 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
690 /* subtract until the remainder becomes negative, this should
691 * be faster than comparing remainder with divisor */
692 _add(rem, minus_divisor, rem);
694 while (_sign(rem) == 1)
696 quot[0] = add_table[_val(quot[0])][SC_1][0];
697 _add(rem, minus_divisor, rem);
700 /* subtracted one too much */
701 _add(rem, divisor, rem);
705 carry_flag = !sc_is_zero(rem);
714 static void _shl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
723 assert((offset >= 0) || (0 && "negative leftshift"));
724 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
725 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
726 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
728 /* if shifting far enough the result is zero */
729 if (offset >= radius)
731 memset(buffer, SC_0, CALC_BUFFER_SIZE);
735 shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
738 /* shift the single digits some bytes (offset) and some bits (table)
740 for (counter = 0; counter < radius/4 - offset; counter++)
742 shl = mul_table[_val(val1[counter])][_val(shift)];
743 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
748 shl = mul_table[_val(val1[counter])][_val(shift)];
749 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
752 bitoffset = counter - 1;
755 /* fill with zeroes */
756 for (counter = 0; counter < offset; counter++) buffer[counter] = SC_0;
758 /* if the mode was signed, change sign when the mode's msb is now 1 */
759 offset = bitoffset + offset;
760 bitoffset = (radius-1) % 4;
761 if (is_signed && _bitisset(buffer[offset], bitoffset))
763 /* this sets the upper bits of the leftmost digit */
764 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
765 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
767 buffer[counter] = SC_F;
770 else if (is_signed && !_bitisset(buffer[offset], bitoffset))
772 /* this unsets the upper bits of the leftmost digit */
773 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
774 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
776 buffer[counter] = SC_0;
781 static void _shr(const char *val1, char *buffer, long offset, int radius, unsigned is_signed, int signed_shift)
792 assert((offset >= 0) || (0 && "negative rightshift"));
793 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
794 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
795 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
797 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
799 /* if shifting far enough the result is either 0 or -1 */
800 if (offset >= radius)
802 if (!sc_is_zero(val1)) {
805 memset(buffer, sign, CALC_BUFFER_SIZE);
812 /* check if any bits are lost, and set carry_flag if so */
813 for (counter = 0; counter < offset; counter++)
815 if (val1[counter] != 0)
821 if ((_val(val1[counter]) & ((1<<shift)-1)) != 0)
825 /* shift digits to the right with offset, carry and all */
827 if (radius/4 - offset > 0) {
828 buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
831 for (; counter < radius/4 - offset; counter++)
833 shrs = shrs_table[_val(val1[counter + offset])][shift];
834 buffer[counter] = shrs[0];
835 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
838 /* the last digit is special in regard of signed/unsigned shift */
839 bitoffset = radius%4;
840 msd = (radius/4<CALC_BUFFER_SIZE)?(val1[radius/4]):(sign); /* most significant digit */
842 /* remove sign bits if mode was signed and this is an unsigned shift */
843 if (!signed_shift && is_signed) {
844 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
847 shrs = shrs_table[_val(msd)][shift];
849 /* signed shift and signed mode and negative value means all bits to the left are set */
850 if (signed_shift && is_signed && (_sign(val1) == -1)) {
851 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
853 buffer[counter] = shrs[0];
856 if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
858 /* fill with SC_F or SC_0 depending on sign */
859 for (counter++; counter < CALC_BUFFER_SIZE; counter++)
861 buffer[counter] = sign;
865 /* positive: low-order -> high order, negative other direction */
866 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
869 temp1 = alloca(CALC_BUFFER_SIZE);
870 temp2 = alloca(CALC_BUFFER_SIZE);
872 offset = offset % radius;
874 /* rotation by multiples of the typelength is identity */
876 memmove(buffer, val1, CALC_BUFFER_SIZE);
880 _shl(val1, temp1, offset, radius, is_signed);
881 _shr(val1, temp2, radius - offset, radius, is_signed, 0);
882 _bitor(temp1, temp2, buffer);
883 carry_flag = 0; /* set by shr, but due to rot this is false */
886 /*****************************************************************************
887 * public functions, declared in strcalc.h
888 *****************************************************************************/
889 const void *sc_get_buffer(void)
891 return (void*)calc_buffer;
894 const int sc_get_buffer_length(void)
896 return CALC_BUFFER_SIZE;
899 /* XXX doesn't check for overflows */
900 void sc_val_from_str(const char *str, unsigned int len, void *buffer)
902 const char *orig_str = str;
903 unsigned int orig_len = len;
908 base = alloca(CALC_BUFFER_SIZE);
909 val = alloca(CALC_BUFFER_SIZE);
911 /* verify valid pointers (not null) */
913 /* a string no characters long is an error */
916 if (buffer == NULL) buffer = calc_buffer;
918 CLEAR_BUFFER(buffer);
919 memset(base, SC_0, CALC_BUFFER_SIZE);
920 memset(val, SC_0, CALC_BUFFER_SIZE);
922 /* strip leading spaces */
923 while ((len > 0) && (*str == ' ')) { len--; str++; }
925 /* if the first two characters are 0x or 0X -> hex
926 * if the first is a 0 -> oct
927 * else dec, strip leading -/+ and remember sign
929 * only a + or - sign is no number resulting in an error */
934 if (str[1] == 'x' || str[1] == 'X') /* hex */
938 base[1] = SC_1; base[0] = SC_0;
944 base[1] = SC_0; base[0] = SC_8;
952 base[1] = SC_0; base[0] = SC_A;
961 base[1] = SC_0; base[0] = SC_A;
965 default: /* dec, else would have begun with 0x or 0 */
966 base[1] = SC_0; base[0] = SC_A;
969 else /* dec, else would have begun with 0x or 0 */
971 base[1] = SC_0; base[0] = SC_A;
974 /* BEGIN string evaluation, from left to right */
985 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
987 val[0] = _digit((*str)-'a'+10);
989 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
998 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1000 val[0] = _digit((*str)-'A'+10);
1002 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1007 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
1009 val[0] = _digit((*str)-'0');
1011 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1023 val[0] = _digit((*str)-'0');
1028 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1029 } /* switch(*str) */
1031 /* Radix conversion from base b to base B:
1032 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1033 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1034 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1036 /* get ready for the next letter */
1040 } /* while (len > 0 ) */
1044 _negate(calc_buffer, calc_buffer);
1048 void sc_val_from_long(long value, void *buffer)
1051 char sign, is_minlong;
1053 if (buffer == NULL) buffer = calc_buffer;
1057 is_minlong = value == LONG_MIN;
1059 /* use absolute value, special treatment of MIN_LONG */
1067 CLEAR_BUFFER(buffer);
1069 while ((value != 0) && (pos < (char*)buffer + CALC_BUFFER_SIZE))
1071 *pos++ = _digit(value & 0xf);
1078 _inc(buffer, buffer);
1080 _negate(buffer, buffer);
1084 void sc_val_from_ulong(unsigned long value, void *buffer)
1088 if (buffer == NULL) buffer = calc_buffer;
1091 while (pos < (char*)buffer + CALC_BUFFER_SIZE)
1093 *pos++ = _digit(value & 0xf);
1098 long sc_val_to_long(const void *val)
1103 for (i = CALC_BUFFER_SIZE - 1; i >= 0; i--)
1105 l = (l << 4) + _val(((char *)val)[i]);
1110 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1115 if (buffer == NULL) buffer = calc_buffer;
1116 CLEAR_BUFFER(buffer);
1118 if (!sign) return; /* unsigned means minimum is 0(zero) */
1122 bits = num_bits - 1;
1123 for (i = 0; i < bits/4; i++)
1126 *pos++ = min_digit[bits%4];
1128 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1132 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1137 if (buffer == NULL) buffer = calc_buffer;
1138 CLEAR_BUFFER(buffer);
1141 bits = num_bits - sign;
1142 for (i = 0; i < bits/4; i++)
1145 *pos++ = max_digit[bits%4];
1147 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1151 void sc_calc(const void* value1, const void* value2, unsigned op, void *buffer)
1153 char *unused_res; /* temp buffer holding unused result of divmod */
1155 const char *val1 = (const char *)value1;
1156 const char *val2 = (const char *)value2;
1158 unused_res = alloca(CALC_BUFFER_SIZE);
1159 CLEAR_BUFFER(calc_buffer);
1162 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1167 _negate(val1, calc_buffer);
1168 DEBUGPRINTF_COMPUTATION(("negated: %s\n", sc_print_hex(calc_buffer)));
1171 DEBUGPRINTF_COMPUTATION(("| "));
1172 _bitor(val1, val2, calc_buffer);
1175 DEBUGPRINTF_COMPUTATION(("& "));
1176 _bitand(val1, val2, calc_buffer);
1179 DEBUGPRINTF_COMPUTATION(("^ "));
1180 _bitxor(val1, val2, calc_buffer);
1183 _bitnot(val1, calc_buffer);
1184 DEBUGPRINTF_COMPUTATION(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1187 DEBUGPRINTF_COMPUTATION(("+ "));
1188 _add(val1, val2, calc_buffer);
1191 DEBUGPRINTF_COMPUTATION(("- "));
1192 _sub(val1, val2, calc_buffer);
1195 DEBUGPRINTF_COMPUTATION(("* "));
1196 _mul(val1, val2, calc_buffer);
1199 DEBUGPRINTF_COMPUTATION(("/ "));
1200 _divmod(val1, val2, calc_buffer, unused_res);
1203 DEBUGPRINTF_COMPUTATION(("%% "));
1204 _divmod(val1, val2, unused_res, calc_buffer);
1209 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1210 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1212 if ((buffer != NULL) && (buffer != calc_buffer))
1214 memcpy(buffer, calc_buffer, CALC_BUFFER_SIZE);
1218 void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op, void* buffer)
1220 const char *val1 = (const char *)value1;
1221 const char *val2 = (const char *)value2;
1225 offset = sc_val_to_long(val2);
1227 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1231 DEBUGPRINTF_COMPUTATION(("<< %ld ", offset));
1232 _shl(val1, calc_buffer, offset, radius, sign);
1235 DEBUGPRINTF_COMPUTATION((">> %ld ", offset));
1236 _shr(val1, calc_buffer, offset, radius, sign, 0);
1239 DEBUGPRINTF_COMPUTATION((">>> %ld ", offset));
1240 _shr(val1, calc_buffer, offset, radius, sign, 1);
1243 DEBUGPRINTF_COMPUTATION(("<<>> %ld ", offset));
1244 _rot(val1, calc_buffer, offset, radius, sign);
1249 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1251 if ((buffer != NULL) && (buffer != calc_buffer))
1253 memmove(buffer, calc_buffer, CALC_BUFFER_SIZE);
1257 int sc_comp(const void* value1, const void* value2)
1259 int counter = CALC_BUFFER_SIZE - 1;
1260 const char *val1 = (const char *)value1;
1261 const char *val2 = (const char *)value2;
1263 /* compare signs first:
1264 * the loop below can only compare values of the same sign! */
1265 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1267 /* loop until two digits differ, the values are equal if there
1268 * are no such two digits */
1269 while (val1[counter] == val2[counter])
1272 if (counter < 0) return 0;
1275 /* the leftmost digit is the most significant, so this returns
1276 * the correct result.
1277 * This implies the digit enum is ordered */
1278 return (val1[counter] > val2[counter]) ? (1) : (-1);
1281 int sc_get_highest_set_bit(const void *value)
1283 const char *val = (const char*)value;
1286 high = CALC_BUFFER_SIZE * 4 - 1;
1288 for (counter = CALC_BUFFER_SIZE-1; counter >= 0; counter--) {
1289 if (val[counter] == SC_0) high -= 4;
1291 if (val[counter] > SC_7) return high;
1292 else if (val[counter] > SC_3) return high - 1;
1293 else if (val[counter] > SC_1) return high - 2;
1294 else return high - 3;
1300 int sc_get_lowest_set_bit(const void *value)
1302 const char *val = (const char*)value;
1306 sign = (_sign(val)==1)?(SC_0):(SC_F);
1309 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++) {
1310 if (val[counter] == SC_0) low += 4;
1312 if (val[counter] < SC_2) return low;
1313 else if (val[counter] < SC_4) return low + 1;
1314 else if (val[counter] < SC_8) return low + 2;
1315 else return low + 3;
1321 int sc_is_zero(const void *value)
1323 const char* val = (const char *)value;
1326 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++) {
1327 if (val[counter] != SC_0) return 0;
1332 int sc_is_negative(const void *value)
1334 return _sign(value) == -1;
1337 int sc_had_carry(void)
1342 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1344 const char *val = (const char *)value;
1345 unsigned nibble_ofs = 2 * byte_ofs;
1348 /* the current scheme uses one byte to store a nibble */
1349 if (nibble_ofs >= len)
1352 res = _val(val[nibble_ofs]);
1353 if (len > nibble_ofs + 1)
1354 res |= _val(val[nibble_ofs + 1]) << 4;
1360 * convert to a string
1361 * XXX Doesn't check buffer bounds
1363 const char *sc_print(const void *value, unsigned bits, enum base_t base)
1365 static const char big_digits[] = "0123456789ABCDEF";
1366 static const char small_digits[] = "0123456789abcdef";
1368 char *base_val, *div1_res, *div2_res, *rem_res;
1369 int counter, nibbles, i, sign;
1372 const char *val = (const char *)value;
1376 const char *digits = small_digits;
1378 base_val = alloca(CALC_BUFFER_SIZE);
1379 div1_res = alloca(CALC_BUFFER_SIZE);
1380 div2_res = alloca(CALC_BUFFER_SIZE);
1381 rem_res = alloca(CALC_BUFFER_SIZE);
1383 pos = output_buffer + BIT_PATTERN_SIZE;
1388 bits = BIT_PATTERN_SIZE;
1389 #ifdef STRCALC_DEBUG_FULLPRINT
1393 nibbles = bits >> 2;
1397 digits = big_digits;
1399 for (counter = 0; counter < nibbles; ++counter) {
1400 *(--pos) = digits[_val(val[counter])];
1401 #ifdef STRCALC_DEBUG_GROUPPRINT
1402 if ((counter+1)%8 == 0)
1407 /* last nibble must be masked */
1409 x = and_table[_val(val[++counter])][bits & 3];
1410 *(--pos) = digits[_val(x)];
1413 /* now kill zeros */
1414 for (; counter > 1; --counter, ++pos) {
1415 #ifdef STRCALC_DEBUG_GROUPPRINT
1416 if (pos[0] == ' ') ++pos;
1424 for (counter = 0; counter < nibbles; ++counter) {
1426 p = binary_table[_val(val[counter])];
1433 /* last nibble must be masked */
1435 x = and_table[_val(val[++counter])][bits & 3];
1438 p = binary_table[_val(x)];
1445 /* now kill zeros */
1446 for (counter <<= 2; counter > 1; --counter, ++pos)
1453 memset(base_val, SC_0, CALC_BUFFER_SIZE);
1454 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1458 if (base == SC_DEC) {
1459 /* check for negative values */
1460 if (_bit(val, bits - 1)) {
1461 _negate(val, div2_res);
1467 /* transfer data into oscilating buffers */
1468 memset(div1_res, SC_0, CALC_BUFFER_SIZE);
1469 for (counter = 0; counter < nibbles; ++counter)
1470 div1_res[counter] = p[counter];
1472 /* last nibble must be masked */
1476 div1_res[counter] = and_table[_val(p[counter])][bits & 3];
1482 _divmod(m, base_val, n, rem_res);
1486 *(--pos) = digits[_val(rem_res[0])];
1489 for (i = 0; i < sizeof(div1_res); ++i)
1500 printf("%i\n", base);
1507 void init_strcalc(int precision)
1509 if (calc_buffer == NULL) {
1510 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1512 /* round up to multiple of 4 */
1513 precision = (precision + 3) & ~3;
1515 BIT_PATTERN_SIZE = (precision);
1516 CALC_BUFFER_SIZE = (precision / 2);
1517 MAX_VALUE_SIZE = (precision / 4);
1519 calc_buffer = malloc(CALC_BUFFER_SIZE+1 * sizeof(char));
1520 output_buffer = malloc(BIT_PATTERN_SIZE+1 * sizeof(char));
1522 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));
1532 void finish_strcalc() {
1533 free(calc_buffer); calc_buffer = NULL;
1534 free(output_buffer); output_buffer = NULL;
1537 int sc_get_precision(void)
1539 return BIT_PATTERN_SIZE;