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.
17 #include <assert.h> /* assertions */
18 #include <string.h> /* memset/memcmp */
19 #include <stdio.h> /* output for error messages */
20 #include <limits.h> /* definition of LONG_MIN, used in sc_get_val_from_long */
23 * local definitions and macros
25 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, CALC_BUFFER_SIZE)
26 #define _val(a) ((a)-SC_0)
27 #define _digit(a) ((a)+SC_0)
28 #define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
30 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__, __LINE__)
32 /* shortcut output for debugging */
33 # define sc_print_hex(a) sc_print((a), 0, SC_HEX)
34 # define sc_print_dec(a) sc_print((a), 0, SC_DEC)
35 # define sc_print_oct(a) sc_print((a), 0, SC_OCT)
36 # define sc_print_bin(a) sc_print((a), 0, SC_BIN)
38 #ifdef STRCALC_DEBUG_PRINTCOMP
39 # define DEBUGPRINTF_COMPUTATION(x) printf x
41 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
44 # define DEBUGPRINTF(x) printf x
46 # define DEBUGPRINTF(x) ((void)0)
54 static char *calc_buffer = NULL; /* buffer holding all results */
55 static char *output_buffer = NULL; /* buffer for output */
56 static int BIT_PATTERN_SIZE; /* maximum number of bits */
57 static int CALC_BUFFER_SIZE; /* size of internally stored values */
58 static int MAX_VALUE_SIZE; /* maximum size of values */
60 static int carry_flag; /* some computation set carry_flag: */
61 /* rightshift if bits were lost due to shifting */
62 /* division if there was a remainder */
64 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
65 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
67 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
68 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
70 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
72 static const char and_table[16][16] = {
73 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
74 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
76 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
77 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
79 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
80 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
82 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
83 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
85 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
86 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
88 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
89 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
91 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
92 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
94 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
95 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
97 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
98 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
100 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
101 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
103 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
104 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
106 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
107 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
109 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
110 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
112 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
113 SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
115 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
116 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
118 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
119 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
121 static const char or_table[16][16] = {
122 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
123 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
125 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
126 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
128 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
129 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
131 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
132 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
134 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
135 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
137 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
138 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
140 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
141 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
143 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
144 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
146 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
147 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
149 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
150 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
152 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
153 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
155 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
156 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
158 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
159 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
161 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
162 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
164 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
165 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
167 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
168 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
170 static char const xor_table[16][16] = {
171 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
172 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
174 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
175 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
177 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
178 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
180 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
181 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
183 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
184 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
186 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
187 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
189 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
190 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
192 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
193 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
195 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
196 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
198 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
199 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
201 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
202 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
204 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
205 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
207 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
208 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
210 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
211 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
213 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
214 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
216 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
217 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
220 static char const add_table[16][16][2] = {
221 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
222 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
223 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
224 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
226 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
227 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
228 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
229 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
231 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
232 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
233 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
234 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
236 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
237 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
238 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
239 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
241 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
242 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
243 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
244 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
246 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
247 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
248 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
249 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
251 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
252 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
253 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
254 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
256 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
257 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
258 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
259 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
261 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
262 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
263 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
264 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
266 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
267 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
268 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
269 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
271 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
272 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
273 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
274 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
276 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
277 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
278 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
279 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
281 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
282 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
283 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
284 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
286 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
287 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
288 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
289 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
291 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
292 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
293 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
294 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
296 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
297 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
298 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
299 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
302 static char const mul_table[16][16][2] = {
303 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
304 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
305 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
306 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
308 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
309 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
310 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
311 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
313 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
314 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
315 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
316 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
318 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
319 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
320 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
321 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
323 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
324 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
325 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
326 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
328 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
329 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
330 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
331 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
333 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
334 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
335 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
336 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
338 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
339 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
340 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
341 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
343 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
344 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
345 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
346 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
348 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
349 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
350 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
351 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
353 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
354 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
355 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
356 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
358 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
359 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
360 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
361 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
363 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
364 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
365 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
366 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
368 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
369 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
370 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
371 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
373 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
374 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
375 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
376 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
378 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
379 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
380 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
381 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
384 static char const shrs_table[16][4][2] = {
385 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
386 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
387 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
388 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
389 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
390 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
391 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
392 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
393 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
394 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
395 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
396 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
397 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
398 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
399 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
400 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
403 /* for converting to binary string */
404 static const char *binary_table[16] = {
405 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
406 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
409 /*****************************************************************************
411 *****************************************************************************/
412 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
413 const char *file, int line)
416 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
417 while (len-- && *str) printf("%c", *str++); printf("\n");
418 while (--pos) printf(" "); printf("^\n");
422 static void _bitnot(const char *val, char *buffer)
426 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
427 buffer[counter] = not_table[_val(val[counter])];
430 static void _bitor(const char *val1, const char *val2, char *buffer)
434 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
435 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
438 static void _bitxor(const char *val1, const char *val2, char *buffer)
442 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
443 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
446 static void _bitand(const char *val1, const char *val2, char *buffer)
450 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
451 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
454 static int _sign(const char *val)
456 return (val[CALC_BUFFER_SIZE-1] <= SC_7) ? (1) : (-1);
460 * returns non-zero if bit at position pos is set
462 static int _bit(const char *val, int pos)
465 int nibble = pos >> 2;
467 return _bitisset(val[nibble], bit);
470 static void _inc(char *val, char *buffer)
474 while (counter++ < CALC_BUFFER_SIZE)
483 /* No carry here, *val != SC_F */
484 *buffer = add_table[_val(*val)][SC_1][0];
488 /* here a carry could be lost, this is intended because this should
489 * happen only when a value changes sign. */
492 static void _negate(const char *val, char *buffer)
494 _bitnot(val, buffer);
495 _inc(buffer, buffer);
498 static void _add(const char *val1, const char *val2, char *buffer)
501 const char *add1, *add2;
504 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++)
506 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
507 add2 = add_table[_val(add1[0])][_val(carry)];
508 /* carry might be zero */
509 buffer[counter] = add2[0];
510 carry = add_table[_val(add1[1])][_val(add2[1])][0];
514 static void _mul(const char *val1, const char *val2, char *buffer)
516 char* temp_buffer; /* result buffer */
517 char* neg_val1; /* abs of val1 */
518 char* neg_val2; /* abs of val2 */
520 const char *mul, *add1, *add2; /* intermediate result containers */
521 char carry = SC_0; /* container for carries */
522 char sign = 0; /* marks result sign */
523 int c_inner, c_outer; /* loop counters */
525 temp_buffer = alloca(CALC_BUFFER_SIZE);
526 neg_val1 = alloca(CALC_BUFFER_SIZE);
527 neg_val2 = alloca(CALC_BUFFER_SIZE);
529 /* init result buffer to zeroes */
530 memset(temp_buffer, SC_0, CALC_BUFFER_SIZE);
532 /* the multiplication works only for positive values, for negative values *
533 * it is necessary to negate them and adjust the result accordingly */
534 if (_sign(val1) == -1) {
535 _negate(val1, neg_val1);
539 if (_sign(val2) == -1) {
540 _negate(val2, neg_val2);
545 for (c_outer = 0; c_outer < MAX_VALUE_SIZE; c_outer++)
547 if (val2[c_outer] != SC_0)
549 for (c_inner = 0; c_inner < MAX_VALUE_SIZE; c_inner++)
551 /* do the following calculation: *
552 * Add the current carry, the value at position c_outer+c_inner *
553 * and the result of the multiplication of val1[c_inner] and *
554 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
556 /* multiplicate the two digits */
557 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
558 /* add old value to result of multiplication */
559 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
560 /* add carry to the sum */
561 add2 = add_table[_val(add1[0])][_val(carry)];
563 /* all carries together result in new carry. This is always smaller *
565 * Both multiplicands, the carry and the value already in the temp *
566 * buffer are single digits and their value is therefore at most *
569 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
570 * The tables list all operations rem b, so the carry is at most *
571 * (b*b-1)rem b = -1rem b = b-1 */
572 carry = add_table[_val(mul[1])][_val(add1[1])][0];
573 carry = add_table[_val(carry)][_val(add2[1])][0];
575 temp_buffer[c_inner + c_outer] = add2[0];
578 /* A carry may hang over */
579 /* c_outer is always smaller than MAX_VALUE_SIZE! */
580 temp_buffer[MAX_VALUE_SIZE + c_outer] = carry;
586 _negate(temp_buffer, buffer);
588 memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
591 static void _sub(const char *val1, const char *val2, char *buffer)
593 char *temp_buffer; /* intermediate buffer to hold -val2 */
594 temp_buffer = alloca(CALC_BUFFER_SIZE);
596 _negate(val2, temp_buffer);
597 _add(val1, temp_buffer, buffer);
600 static void _push(const char digit, char *buffer)
604 for (counter = CALC_BUFFER_SIZE - 2; counter >= 0; counter--)
606 buffer[counter+1] = buffer[counter];
611 /* XXX: This is MOST slow */
612 static void _divmod(const char *dividend, const char *divisor, char *quot, char *rem)
614 const char *minus_divisor;
618 char sign = 0; /* remember result sign */
620 int c_dividend; /* loop counters */
622 neg_val1 = alloca(CALC_BUFFER_SIZE);
623 neg_val2 = alloca(CALC_BUFFER_SIZE);
625 /* clear result buffer */
626 memset(quot, SC_0, CALC_BUFFER_SIZE);
627 memset(rem, SC_0, CALC_BUFFER_SIZE);
629 /* if the divisor is zero this won't work (quot is zero) */
630 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
632 /* if the dividend is zero result is zero (quot is zero)*/
633 if (sc_comp(dividend, quot) == 0)
636 if (_sign(dividend) == -1)
638 _negate(dividend, neg_val1);
643 _negate(divisor, neg_val2);
644 if (_sign(divisor) == -1)
647 minus_divisor = divisor;
652 minus_divisor = neg_val2;
655 /* if divisor >= dividend division is easy
656 * (remember these are absolute values) */
657 switch (sc_comp(dividend, divisor))
659 case 0: /* dividend == divisor */
663 case -1: /* dividend < divisor */
664 memcpy(rem, dividend, CALC_BUFFER_SIZE);
667 default: /* unluckily division is necessary :( */
671 for (c_dividend = CALC_BUFFER_SIZE - 1; c_dividend >= 0; c_dividend--)
673 _push(dividend[c_dividend], rem);
676 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
678 /* subtract until the remainder becomes negative, this should
679 * be faster than comparing remainder with divisor */
680 _add(rem, minus_divisor, rem);
682 while (_sign(rem) == 1)
684 quot[0] = add_table[_val(quot[0])][SC_1][0];
685 _add(rem, minus_divisor, rem);
688 /* subtracted one too much */
689 _add(rem, divisor, rem);
693 carry_flag = !sc_is_zero(rem);
702 static void _shl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
711 assert((offset >= 0) || (0 && "negative leftshift"));
712 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
713 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
714 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
716 /* if shifting far enough the result is zero */
717 if (offset >= radius)
719 memset(buffer, SC_0, CALC_BUFFER_SIZE);
723 shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
726 /* shift the single digits some bytes (offset) and some bits (table)
728 for (counter = 0; counter < radius/4 - offset; counter++)
730 shl = mul_table[_val(val1[counter])][_val(shift)];
731 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
736 shl = mul_table[_val(val1[counter])][_val(shift)];
737 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
740 bitoffset = counter - 1;
743 /* fill with zeroes */
744 for (counter = 0; counter < offset; counter++) buffer[counter] = SC_0;
746 /* if the mode was signed, change sign when the mode's msb is now 1 */
747 offset = bitoffset + offset;
748 bitoffset = (radius-1) % 4;
749 if (is_signed && _bitisset(buffer[offset], bitoffset))
751 /* this sets the upper bits of the leftmost digit */
752 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
753 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
755 buffer[counter] = SC_F;
758 else if (is_signed && !_bitisset(buffer[offset], bitoffset))
760 /* this unsets the upper bits of the leftmost digit */
761 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
762 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
764 buffer[counter] = SC_0;
769 static void _shr(const char *val1, char *buffer, long offset, int radius, unsigned is_signed, int signed_shift)
780 assert((offset >= 0) || (0 && "negative rightshift"));
781 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
782 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
783 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
785 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
787 /* if shifting far enough the result is either 0 or -1 */
788 if (offset >= radius)
790 if (!sc_is_zero(val1)) {
793 memset(buffer, sign, CALC_BUFFER_SIZE);
800 /* check if any bits are lost, and set carry_flag if so */
801 for (counter = 0; counter < offset; counter++)
803 if (val1[counter] != 0)
809 if ((_val(val1[counter]) & ((1<<shift)-1)) != 0)
813 /* shift digits to the right with offset, carry and all */
815 if (radius/4 - offset > 0) {
816 buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
819 for (; counter < radius/4 - offset; counter++)
821 shrs = shrs_table[_val(val1[counter + offset])][shift];
822 buffer[counter] = shrs[0];
823 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
826 /* the last digit is special in regard of signed/unsigned shift */
827 bitoffset = radius%4;
828 msd = (radius/4<CALC_BUFFER_SIZE)?(val1[radius/4]):(sign); /* most significant digit */
830 /* remove sign bits if mode was signed and this is an unsigned shift */
831 if (!signed_shift && is_signed) {
832 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
835 shrs = shrs_table[_val(msd)][shift];
837 /* signed shift and signed mode and negative value means all bits to the left are set */
838 if (signed_shift && is_signed && (_sign(val1) == -1)) {
839 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
841 buffer[counter] = shrs[0];
844 if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
846 /* fill with SC_F or SC_0 depending on sign */
847 for (counter++; counter < CALC_BUFFER_SIZE; counter++)
849 buffer[counter] = sign;
853 /* positive: low-order -> high order, negative other direction */
854 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
857 temp1 = alloca(CALC_BUFFER_SIZE);
858 temp2 = alloca(CALC_BUFFER_SIZE);
860 offset = offset % radius;
862 /* rotation by multiples of the typelength is identity */
864 memmove(buffer, val1, CALC_BUFFER_SIZE);
868 _shl(val1, temp1, offset, radius, is_signed);
869 _shr(val1, temp2, radius - offset, radius, is_signed, 0);
870 _bitor(temp1, temp2, buffer);
871 carry_flag = 0; /* set by shr, but due to rot this is false */
874 /*****************************************************************************
875 * public functions, declared in strcalc.h
876 *****************************************************************************/
877 const void *sc_get_buffer(void)
879 return (void*)calc_buffer;
882 const int sc_get_buffer_length(void)
884 return CALC_BUFFER_SIZE;
887 /* XXX doesn't check for overflows */
888 void sc_val_from_str(const char *str, unsigned int len, void *buffer)
890 const char *orig_str = str;
891 unsigned int orig_len = len;
896 base = alloca(CALC_BUFFER_SIZE);
897 val = alloca(CALC_BUFFER_SIZE);
899 /* verify valid pointers (not null) */
901 /* a string no characters long is an error */
904 if (buffer == NULL) buffer = calc_buffer;
906 CLEAR_BUFFER(buffer);
907 memset(base, SC_0, CALC_BUFFER_SIZE);
908 memset(val, SC_0, CALC_BUFFER_SIZE);
910 /* strip leading spaces */
911 while ((len > 0) && (*str == ' ')) { len--; str++; }
913 /* if the first two characters are 0x or 0X -> hex
914 * if the first is a 0 -> oct
915 * else dec, strip leading -/+ and remember sign
917 * only a + or - sign is no number resulting in an error */
922 if (str[1] == 'x' || str[1] == 'X') /* hex */
926 base[1] = SC_1; base[0] = SC_0;
932 base[1] = SC_0; base[0] = SC_8;
940 base[1] = SC_0; base[0] = SC_A;
949 base[1] = SC_0; base[0] = SC_A;
953 default: /* dec, else would have begun with 0x or 0 */
954 base[1] = SC_0; base[0] = SC_A;
957 else /* dec, else would have begun with 0x or 0 */
959 base[1] = SC_0; base[0] = SC_A;
962 /* BEGIN string evaluation, from left to right */
973 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
975 val[0] = _digit((*str)-'a'+10);
977 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
986 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
988 val[0] = _digit((*str)-'A'+10);
990 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
995 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
997 val[0] = _digit((*str)-'0');
999 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1011 val[0] = _digit((*str)-'0');
1016 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1017 } /* switch(*str) */
1019 /* Radix conversion from base b to base B:
1020 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1021 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1022 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1024 /* get ready for the next letter */
1028 } /* while (len > 0 ) */
1032 _negate(calc_buffer, calc_buffer);
1036 void sc_val_from_long(long value, void *buffer)
1039 char sign, is_minlong;
1041 if (buffer == NULL) buffer = calc_buffer;
1045 is_minlong = value == LONG_MIN;
1047 /* use absolute value, special treatment of MIN_LONG */
1055 CLEAR_BUFFER(buffer);
1057 while ((value != 0) && (pos < (char*)buffer + CALC_BUFFER_SIZE))
1059 *pos++ = _digit(value & 0xf);
1066 _inc(buffer, buffer);
1068 _negate(buffer, buffer);
1072 void sc_val_from_ulong(unsigned long value, void *buffer)
1076 if (buffer == NULL) buffer = calc_buffer;
1079 while (pos < (char*)buffer + CALC_BUFFER_SIZE)
1081 *pos++ = _digit(value & 0xf);
1086 long sc_val_to_long(const void *val)
1091 for (i = CALC_BUFFER_SIZE - 1; i >= 0; i--)
1093 l = (l << 4) + _val(((char *)val)[i]);
1098 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1103 if (buffer == NULL) buffer = calc_buffer;
1104 CLEAR_BUFFER(buffer);
1106 if (!sign) return; /* unsigned means minimum is 0(zero) */
1110 bits = num_bits - 1;
1111 for (i = 0; i < bits/4; i++)
1114 *pos++ = min_digit[bits%4];
1116 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1120 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1125 if (buffer == NULL) buffer = calc_buffer;
1126 CLEAR_BUFFER(buffer);
1129 bits = num_bits - sign;
1130 for (i = 0; i < bits/4; i++)
1133 *pos++ = max_digit[bits%4];
1135 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1139 void sc_calc(const void* value1, const void* value2, unsigned op, void *buffer)
1141 char *unused_res; /* temp buffer holding unused result of divmod */
1143 const char *val1 = (const char *)value1;
1144 const char *val2 = (const char *)value2;
1146 unused_res = alloca(CALC_BUFFER_SIZE);
1147 CLEAR_BUFFER(calc_buffer);
1150 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1155 _negate(val1, calc_buffer);
1156 DEBUGPRINTF_COMPUTATION(("negated: %s\n", sc_print_hex(calc_buffer)));
1159 DEBUGPRINTF_COMPUTATION(("| "));
1160 _bitor(val1, val2, calc_buffer);
1163 DEBUGPRINTF_COMPUTATION(("& "));
1164 _bitand(val1, val2, calc_buffer);
1167 DEBUGPRINTF_COMPUTATION(("^ "));
1168 _bitxor(val1, val2, calc_buffer);
1171 _bitnot(val1, calc_buffer);
1172 DEBUGPRINTF_COMPUTATION(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1175 DEBUGPRINTF_COMPUTATION(("+ "));
1176 _add(val1, val2, calc_buffer);
1179 DEBUGPRINTF_COMPUTATION(("- "));
1180 _sub(val1, val2, calc_buffer);
1183 DEBUGPRINTF_COMPUTATION(("* "));
1184 _mul(val1, val2, calc_buffer);
1187 DEBUGPRINTF_COMPUTATION(("/ "));
1188 _divmod(val1, val2, calc_buffer, unused_res);
1191 DEBUGPRINTF_COMPUTATION(("%% "));
1192 _divmod(val1, val2, unused_res, calc_buffer);
1197 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1198 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1200 if ((buffer != NULL) && (buffer != calc_buffer))
1202 memcpy(buffer, calc_buffer, CALC_BUFFER_SIZE);
1206 void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op, void* buffer)
1208 const char *val1 = (const char *)value1;
1209 const char *val2 = (const char *)value2;
1213 offset = sc_val_to_long(val2);
1215 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1219 DEBUGPRINTF_COMPUTATION(("<< %ld ", offset));
1220 _shl(val1, calc_buffer, offset, radius, sign);
1223 DEBUGPRINTF_COMPUTATION((">> %ld ", offset));
1224 _shr(val1, calc_buffer, offset, radius, sign, 0);
1227 DEBUGPRINTF_COMPUTATION((">>> %ld ", offset));
1228 _shr(val1, calc_buffer, offset, radius, sign, 1);
1231 DEBUGPRINTF_COMPUTATION(("<<>> %ld ", offset));
1232 _rot(val1, calc_buffer, offset, radius, sign);
1237 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1239 if ((buffer != NULL) && (buffer != calc_buffer))
1241 memmove(buffer, calc_buffer, CALC_BUFFER_SIZE);
1245 int sc_comp(const void* value1, const void* value2)
1247 int counter = CALC_BUFFER_SIZE - 1;
1248 const char *val1 = (const char *)value1;
1249 const char *val2 = (const char *)value2;
1251 /* compare signs first:
1252 * the loop below can only compare values of the same sign! */
1253 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1255 /* loop until two digits differ, the values are equal if there
1256 * are no such two digits */
1257 while (val1[counter] == val2[counter])
1260 if (counter < 0) return 0;
1263 /* the leftmost digit is the most significant, so this returns
1264 * the correct result.
1265 * This implies the digit enum is ordered */
1266 return (val1[counter] > val2[counter]) ? (1) : (-1);
1269 int sc_get_highest_set_bit(const void *value)
1271 const char *val = (const char*)value;
1274 high = CALC_BUFFER_SIZE * 4 - 1;
1276 for (counter = CALC_BUFFER_SIZE-1; counter >= 0; counter--) {
1277 if (val[counter] == SC_0) high -= 4;
1279 if (val[counter] > SC_7) return high;
1280 else if (val[counter] > SC_3) return high - 1;
1281 else if (val[counter] > SC_1) return high - 2;
1282 else return high - 3;
1288 int sc_get_lowest_set_bit(const void *value)
1290 const char *val = (const char*)value;
1294 sign = (_sign(val)==1)?(SC_0):(SC_F);
1297 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++) {
1298 if (val[counter] == SC_0) low += 4;
1300 if (val[counter] < SC_2) return low;
1301 else if (val[counter] < SC_4) return low + 1;
1302 else if (val[counter] < SC_8) return low + 2;
1303 else return low + 3;
1309 int sc_is_zero(const void *value)
1311 const char* val = (const char *)value;
1314 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++) {
1315 if (val[counter] != SC_0) return 0;
1320 int sc_is_negative(const void *value)
1322 return _sign(value) == -1;
1325 int sc_had_carry(void)
1330 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1332 const char *val = (const char *)value;
1333 unsigned nibble_ofs = 2 * byte_ofs;
1336 /* the current scheme uses one byte to store a nibble */
1337 if (nibble_ofs >= len)
1340 res = _val(val[nibble_ofs]);
1341 if (len > nibble_ofs + 1)
1342 res |= _val(val[nibble_ofs + 1]) << 4;
1348 * convert to a string
1349 * XXX Doesn't check buffer bounds
1351 const char *sc_print(const void *value, unsigned bits, enum base_t base)
1353 static const char big_digits[] = "0123456789ABCDEF";
1354 static const char small_digits[] = "0123456789abcdef";
1356 char *base_val, *div1_res, *div2_res, *rem_res;
1357 int counter, nibbles, i, sign;
1360 const char *val = (const char *)value;
1364 const char *digits = small_digits;
1366 base_val = alloca(CALC_BUFFER_SIZE);
1367 div1_res = alloca(CALC_BUFFER_SIZE);
1368 div2_res = alloca(CALC_BUFFER_SIZE);
1369 rem_res = alloca(CALC_BUFFER_SIZE);
1371 pos = output_buffer + BIT_PATTERN_SIZE;
1376 bits = BIT_PATTERN_SIZE;
1377 #ifdef STRCALC_DEBUG_FULLPRINT
1381 nibbles = bits >> 2;
1385 digits = big_digits;
1387 for (counter = 0; counter < nibbles; ++counter) {
1388 *(--pos) = digits[_val(val[counter])];
1389 #ifdef STRCALC_DEBUG_GROUPPRINT
1390 if ((counter+1)%8 == 0)
1395 /* last nibble must be masked */
1397 x = and_table[_val(val[++counter])][bits & 3];
1398 *(--pos) = digits[_val(x)];
1401 /* now kill zeros */
1402 for (; counter > 1; --counter, ++pos) {
1403 #ifdef STRCALC_DEBUG_GROUPPRINT
1404 if (pos[0] == ' ') ++pos;
1412 for (counter = 0; counter < nibbles; ++counter) {
1414 p = binary_table[_val(val[counter])];
1421 /* last nibble must be masked */
1423 x = and_table[_val(val[++counter])][bits & 3];
1426 p = binary_table[_val(x)];
1433 /* now kill zeros */
1434 for (counter <<= 2; counter > 1; --counter, ++pos)
1441 memset(base_val, SC_0, CALC_BUFFER_SIZE);
1442 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1446 if (base == SC_DEC) {
1447 /* check for negative values */
1448 if (_bit(val, bits - 1)) {
1449 _negate(val, div2_res);
1455 /* transfer data into oscilating buffers */
1456 memset(div1_res, SC_0, CALC_BUFFER_SIZE);
1457 for (counter = 0; counter < nibbles; ++counter)
1458 div1_res[counter] = p[counter];
1460 /* last nibble must be masked */
1464 div1_res[counter] = and_table[_val(p[counter])][bits & 3];
1470 _divmod(m, base_val, n, rem_res);
1474 *(--pos) = digits[_val(rem_res[0])];
1477 for (i = 0; i < sizeof(div1_res); ++i)
1488 printf("%i\n", base);
1495 void init_strcalc(int precision)
1497 if (calc_buffer == NULL) {
1498 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1500 /* round up to multiple of 4 */
1501 if (precision & 0x3) precision += 4 - (precision&0x3);
1503 BIT_PATTERN_SIZE = (precision);
1504 CALC_BUFFER_SIZE = (precision / 2);
1505 MAX_VALUE_SIZE = (precision / 4);
1507 calc_buffer = malloc(CALC_BUFFER_SIZE+1 * sizeof(char));
1508 output_buffer = malloc(BIT_PATTERN_SIZE+1 * sizeof(char));
1510 if (calc_buffer == NULL || output_buffer == NULL)
1512 assert(0 && "malloc failed");
1516 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));
1519 int sc_get_precision(void)
1521 return BIT_PATTERN_SIZE;