3 * File name: ir/tv/strcalc.c
9 * Copyright: (c) 2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
28 # include <string.h> /* memset/memcmp */
30 #include <assert.h> /* assertions */
31 #include <stdio.h> /* output for error messages */
32 #include <limits.h> /* definition of LONG_MIN, used in sc_get_val_from_long */
37 * local definitions and macros
39 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, calc_buffer_size)
40 #define _val(a) ((a)-SC_0)
41 #define _digit(a) ((a)+SC_0)
42 #define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
44 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__, __LINE__)
46 /* shortcut output for debugging */
47 # define sc_print_hex(a) sc_print((a), 0, SC_HEX)
48 # define sc_print_dec(a) sc_print((a), 0, SC_DEC)
49 # define sc_print_oct(a) sc_print((a), 0, SC_OCT)
50 # define sc_print_bin(a) sc_print((a), 0, SC_BIN)
52 #ifdef STRCALC_DEBUG_PRINTCOMP
53 # define DEBUGPRINTF_COMPUTATION(x) printf x
55 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
58 # define DEBUGPRINTF(x) printf x
60 # define DEBUGPRINTF(x) ((void)0)
67 static char *calc_buffer = NULL; /* buffer holding all results */
68 static char *output_buffer = NULL; /* buffer for output */
69 static int bit_pattern_size; /* maximum number of bits */
70 static int calc_buffer_size; /* size of internally stored values */
71 static int max_value_size; /* maximum size of values */
73 static int carry_flag; /**< some computation set the carry_flag:
74 - right shift if bits were lost due to shifting
75 - division if there was a remainder
76 However, the meaning of carry is machine dependent
77 and often defined in other ways! */
79 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
80 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
82 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
83 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
85 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
87 static const char and_table[16][16] = {
88 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
89 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
91 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
92 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
94 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
95 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
97 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
98 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
100 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
101 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
103 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
104 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
106 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
107 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
109 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
110 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
112 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
113 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
115 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
116 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
118 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
119 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
121 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
122 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
124 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
125 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
127 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
128 SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
130 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
131 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
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 static const char or_table[16][16] = {
137 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
138 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
140 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
141 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
143 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
144 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
146 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
147 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
149 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
150 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
152 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
153 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
155 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
156 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
158 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
159 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
161 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
162 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
164 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
165 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
167 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
168 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
170 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
171 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
173 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
174 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
176 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
177 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
179 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
180 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
182 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
183 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
185 static char const xor_table[16][16] = {
186 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
187 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
189 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
190 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
192 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
193 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
195 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
196 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
198 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
199 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
201 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
202 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
204 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
205 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
207 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
208 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
210 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
211 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
213 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
214 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
216 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
217 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
219 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
220 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
222 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
223 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
225 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
226 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
228 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
229 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
231 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
232 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
235 static char const add_table[16][16][2] = {
236 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
237 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
238 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
239 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
241 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
242 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
243 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
244 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
246 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
247 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
248 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
249 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
251 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
252 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
253 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
254 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
256 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
257 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
258 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
259 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
261 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
262 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
263 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
264 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
266 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
267 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
268 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
269 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
271 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
272 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
273 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
274 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
276 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
277 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
278 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
279 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
281 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
282 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
283 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
284 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
286 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
287 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
288 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
289 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
291 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
292 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
293 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
294 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
296 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
297 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
298 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
299 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
301 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
302 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
303 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
304 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
306 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
307 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
308 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
309 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
311 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
312 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
313 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
314 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
317 static char const mul_table[16][16][2] = {
318 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
319 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
320 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
321 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
323 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
324 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
325 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
326 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
328 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
329 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
330 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
331 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
333 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
334 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
335 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
336 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
338 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
339 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
340 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
341 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
343 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
344 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
345 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
346 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
348 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
349 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
350 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
351 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
353 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
354 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
355 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
356 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
358 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
359 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
360 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
361 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
363 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
364 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
365 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
366 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
368 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
369 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
370 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
371 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
373 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
374 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
375 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
376 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
378 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
379 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
380 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
381 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
383 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
384 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
385 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
386 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
388 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
389 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
390 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
391 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
393 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
394 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
395 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
396 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
399 static char const shrs_table[16][4][2] = {
400 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
401 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
402 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
403 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
404 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
405 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
406 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
407 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
408 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
409 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
410 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
411 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
412 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
413 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
414 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
415 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
418 /* for converting to binary string */
419 static const char *binary_table[16] = {
420 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
421 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
424 /*****************************************************************************
426 *****************************************************************************/
427 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
428 const char *file, int line)
431 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
432 while (len-- && *str) printf("%c", *str++); printf("\n");
433 while (--pos) printf(" "); printf("^\n");
438 * implements the bitwise NOT operation
440 static void _bitnot(const char *val, char *buffer)
444 for (counter = 0; counter<calc_buffer_size; counter++)
445 buffer[counter] = not_table[_val(val[counter])];
449 * implements the bitwise OR operation
451 static void _bitor(const char *val1, const char *val2, char *buffer)
455 for (counter = 0; counter<calc_buffer_size; counter++)
456 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
460 * implements the bitwise eXclusive OR operation
462 static void _bitxor(const char *val1, const char *val2, char *buffer)
466 for (counter = 0; counter<calc_buffer_size; counter++)
467 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
471 * implements the bitwise AND operation
473 static void _bitand(const char *val1, const char *val2, char *buffer)
477 for (counter = 0; counter<calc_buffer_size; counter++)
478 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
482 * returns the sign bit.
484 * @todo This implementation is wrong, as it returns the highest bit of the buffer
485 * NOT the highest bit depending on the real mode
487 static int _sign(const char *val)
489 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
493 * returns non-zero if bit at position pos is set
495 static int _bit(const char *val, int pos)
498 int nibble = pos >> 2;
500 return _bitisset(val[nibble], bit);
504 * Implements a fast ADD + 1
506 static void _inc(const char *val, char *buffer)
510 while (counter++ < calc_buffer_size)
519 /* No carry here, *val != SC_F */
520 *buffer = add_table[_val(*val)][SC_1][0];
524 /* here a carry could be lost, this is intended because this should
525 * happen only when a value changes sign. */
529 * Implements a unary MINUS
531 static void _negate(const char *val, char *buffer)
533 _bitnot(val, buffer);
534 _inc(buffer, buffer);
538 * Implements a binary ADD
540 * @todo The implementation of carry is wrong, as it is the
541 * calc_buffer_size carry, not the mode depending
543 static void _add(const char *val1, const char *val2, char *buffer)
546 const char *add1, *add2;
549 for (counter = 0; counter < calc_buffer_size; counter++)
551 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
552 add2 = add_table[_val(add1[0])][_val(carry)];
553 /* carry might be zero */
554 buffer[counter] = add2[0];
555 carry = add_table[_val(add1[1])][_val(add2[1])][0];
557 carry_flag = carry != SC_0;
561 * Implements a binary SUB
563 static void _sub(const char *val1, const char *val2, char *buffer)
565 char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
567 _negate(val2, temp_buffer);
568 _add(val1, temp_buffer, buffer);
572 * Implements a binary MUL
574 static void _mul(const char *val1, const char *val2, char *buffer)
576 char *temp_buffer; /* result buffer */
577 char *neg_val1; /* abs of val1 */
578 char *neg_val2; /* abs of val2 */
580 const char *mul, *add1, *add2; /* intermediate result containers */
581 char carry = SC_0; /* container for carries */
582 char sign = 0; /* marks result sign */
583 int c_inner, c_outer; /* loop counters */
585 temp_buffer = alloca(calc_buffer_size);
586 neg_val1 = alloca(calc_buffer_size);
587 neg_val2 = alloca(calc_buffer_size);
589 /* init result buffer to zeroes */
590 memset(temp_buffer, SC_0, calc_buffer_size);
592 /* the multiplication works only for positive values, for negative values *
593 * it is necessary to negate them and adjust the result accordingly */
594 if (_sign(val1) == -1) {
595 _negate(val1, neg_val1);
599 if (_sign(val2) == -1) {
600 _negate(val2, neg_val2);
605 for (c_outer = 0; c_outer < max_value_size; c_outer++)
607 if (val2[c_outer] != SC_0)
609 for (c_inner = 0; c_inner < max_value_size; c_inner++)
611 /* do the following calculation: *
612 * Add the current carry, the value at position c_outer+c_inner *
613 * and the result of the multiplication of val1[c_inner] and *
614 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
616 /* multiplicate the two digits */
617 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
618 /* add old value to result of multiplication */
619 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
620 /* add carry to the sum */
621 add2 = add_table[_val(add1[0])][_val(carry)];
623 /* all carries together result in new carry. This is always smaller *
625 * Both multiplicands, the carry and the value already in the temp *
626 * buffer are single digits and their value is therefore at most *
629 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
630 * The tables list all operations rem b, so the carry is at most *
631 * (b*b-1)rem b = -1rem b = b-1 */
632 carry = add_table[_val(mul[1])][_val(add1[1])][0];
633 carry = add_table[_val(carry)][_val(add2[1])][0];
635 temp_buffer[c_inner + c_outer] = add2[0];
638 /* A carry may hang over */
639 /* c_outer is always smaller than max_value_size! */
640 temp_buffer[max_value_size + c_outer] = carry;
646 _negate(temp_buffer, buffer);
648 memcpy(buffer, temp_buffer, calc_buffer_size);
652 * Shift the buffer to left and add a 4 bit digit
654 static void _push(const char digit, char *buffer)
658 for (counter = calc_buffer_size - 2; counter >= 0; counter--)
660 buffer[counter+1] = buffer[counter];
666 * Implements truncating integer division and remainder.
668 * Note: This is MOST slow
670 static void _divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
672 const char *dividend = rDividend;
673 const char *minus_divisor;
677 char div_sign = 0; /* remember division result sign */
678 char rem_sign = 0; /* remember remainder esult sign */
680 int c_dividend; /* loop counters */
682 neg_val1 = alloca(calc_buffer_size);
683 neg_val2 = alloca(calc_buffer_size);
685 /* clear result buffer */
686 memset(quot, SC_0, calc_buffer_size);
687 memset(rem, SC_0, calc_buffer_size);
689 /* if the divisor is zero this won't work (quot is zero) */
690 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
692 /* if the dividend is zero result is zero (quot is zero)*/
693 if (sc_comp(dividend, quot) == 0)
696 if (_sign(dividend) == -1) {
697 _negate(dividend, neg_val1);
703 _negate(divisor, neg_val2);
704 if (_sign(divisor) == -1) {
706 minus_divisor = divisor;
710 minus_divisor = neg_val2;
712 /* if divisor >= dividend division is easy
713 * (remember these are absolute values) */
714 switch (sc_comp(dividend, divisor))
716 case 0: /* dividend == divisor */
720 case -1: /* dividend < divisor */
721 memcpy(rem, rDividend, calc_buffer_size);
724 default: /* unluckily division is necessary :( */
728 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--)
730 _push(dividend[c_dividend], rem);
733 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
735 /* subtract until the remainder becomes negative, this should
736 * be faster than comparing remainder with divisor */
737 _add(rem, minus_divisor, rem);
739 while (_sign(rem) == 1)
741 quot[0] = add_table[_val(quot[0])][SC_1][0];
742 _add(rem, minus_divisor, rem);
745 /* subtracted one too much */
746 _add(rem, divisor, rem);
750 /* sets carry if remainder is non-zero ??? */
751 carry_flag = !sc_is_zero(rem);
761 * Implements a Shift Left, which can either preserve the sign bit
764 * @todo Assertions seems to be wrong
766 static void _shl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
775 assert((offset >= 0) || (0 && "negative leftshift"));
776 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
777 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
778 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
780 /* if shifting far enough the result is zero */
781 if (offset >= radius)
783 memset(buffer, SC_0, calc_buffer_size);
787 shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
790 /* shift the single digits some bytes (offset) and some bits (table)
792 for (counter = 0; counter < radius/4 - offset; counter++)
794 shl = mul_table[_val(val1[counter])][_val(shift)];
795 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
800 shl = mul_table[_val(val1[counter])][_val(shift)];
801 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
804 bitoffset = counter - 1;
807 /* fill with zeroes */
808 for (counter = 0; counter < offset; counter++) buffer[counter] = SC_0;
810 /* if the mode was signed, change sign when the mode's msb is now 1 */
811 offset = bitoffset + offset;
812 bitoffset = (radius-1) % 4;
813 if (is_signed && _bitisset(buffer[offset], bitoffset))
815 /* this sets the upper bits of the leftmost digit */
816 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
817 for (counter = offset+1; counter < calc_buffer_size; counter++)
819 buffer[counter] = SC_F;
822 else if (is_signed && !_bitisset(buffer[offset], bitoffset))
824 /* this unsets the upper bits of the leftmost digit */
825 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
826 for (counter = offset+1; counter < calc_buffer_size; counter++)
828 buffer[counter] = SC_0;
834 * Implements a Shift Right, which can either preserve the sign bit
837 * @todo Assertions seems to be wrong
839 static void _shr(const char *val1, char *buffer, long offset, int radius, unsigned is_signed, int signed_shift)
850 assert((offset >= 0) || (0 && "negative rightshift"));
851 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
852 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
853 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
855 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
857 /* if shifting far enough the result is either 0 or -1 */
858 if (offset >= radius)
860 if (!sc_is_zero(val1)) {
863 memset(buffer, sign, calc_buffer_size);
870 /* check if any bits are lost, and set carry_flag if so */
871 for (counter = 0; counter < offset; counter++)
873 if (val1[counter] != 0)
879 if ((_val(val1[counter]) & ((1<<shift)-1)) != 0)
883 /* shift digits to the right with offset, carry and all */
885 if (radius/4 - offset > 0) {
886 buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
889 for (; counter < radius/4 - offset; counter++)
891 shrs = shrs_table[_val(val1[counter + offset])][shift];
892 buffer[counter] = shrs[0];
893 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
896 /* the last digit is special in regard of signed/unsigned shift */
897 bitoffset = radius%4;
898 msd = (radius/4<calc_buffer_size)?(val1[radius/4]):(sign); /* most significant digit */
900 /* remove sign bits if mode was signed and this is an unsigned shift */
901 if (!signed_shift && is_signed) {
902 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
905 shrs = shrs_table[_val(msd)][shift];
907 /* signed shift and signed mode and negative value means all bits to the left are set */
908 if (signed_shift && is_signed && (_sign(val1) == -1)) {
909 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
911 buffer[counter] = shrs[0];
914 if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
916 /* fill with SC_F or SC_0 depending on sign */
917 for (counter++; counter < calc_buffer_size; counter++)
919 buffer[counter] = sign;
924 * Implements a Rotate Right.
925 * positive: low-order -> high order, negative other direction
927 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
930 temp1 = alloca(calc_buffer_size);
931 temp2 = alloca(calc_buffer_size);
933 offset = offset % radius;
935 /* rotation by multiples of the typelength is identity */
937 memmove(buffer, val1, calc_buffer_size);
941 _shl(val1, temp1, offset, radius, is_signed);
942 _shr(val1, temp2, radius - offset, radius, is_signed, 0);
943 _bitor(temp1, temp2, buffer);
944 carry_flag = 0; /* set by shr, but due to rot this is false */
947 /*****************************************************************************
948 * public functions, declared in strcalc.h
949 *****************************************************************************/
950 const void *sc_get_buffer(void)
952 return (void*)calc_buffer;
955 const int sc_get_buffer_length(void)
957 return calc_buffer_size;
960 /* XXX doesn't check for overflows */
961 void sc_val_from_str(const char *str, unsigned int len, void *buffer)
963 const char *orig_str = str;
964 unsigned int orig_len = len;
969 base = alloca(calc_buffer_size);
970 val = alloca(calc_buffer_size);
972 /* verify valid pointers (not null) */
974 /* a string no characters long is an error */
977 if (buffer == NULL) buffer = calc_buffer;
979 CLEAR_BUFFER(buffer);
980 memset(base, SC_0, calc_buffer_size);
981 memset(val, SC_0, calc_buffer_size);
983 /* strip leading spaces */
984 while ((len > 0) && (*str == ' ')) { len--; str++; }
986 /* if the first two characters are 0x or 0X -> hex
987 * if the first is a 0 -> oct
988 * else dec, strip leading -/+ and remember sign
990 * only a + or - sign is no number resulting in an error */
995 if (str[1] == 'x' || str[1] == 'X') /* hex */
999 base[1] = SC_1; base[0] = SC_0;
1005 base[1] = SC_0; base[0] = SC_8;
1013 base[1] = SC_0; base[0] = SC_A;
1022 base[1] = SC_0; base[0] = SC_A;
1026 default: /* dec, else would have begun with 0x or 0 */
1027 base[1] = SC_0; base[0] = SC_A;
1030 else /* dec, else would have begun with 0x or 0 */
1032 base[1] = SC_0; base[0] = SC_A;
1035 /* BEGIN string evaluation, from left to right */
1046 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1048 val[0] = _digit((*str)-'a'+10);
1050 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1059 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1061 val[0] = _digit((*str)-'A'+10);
1063 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1068 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
1070 val[0] = _digit((*str)-'0');
1072 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1084 val[0] = _digit((*str)-'0');
1089 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1090 } /* switch(*str) */
1092 /* Radix conversion from base b to base B:
1093 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1094 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1095 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1097 /* get ready for the next letter */
1101 } /* while (len > 0 ) */
1105 _negate(calc_buffer, calc_buffer);
1109 void sc_val_from_long(long value, void *buffer)
1112 char sign, is_minlong;
1114 if (buffer == NULL) buffer = calc_buffer;
1118 is_minlong = value == LONG_MIN;
1120 /* use absolute value, special treatment of MIN_LONG */
1128 CLEAR_BUFFER(buffer);
1130 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size))
1132 *pos++ = _digit(value & 0xf);
1139 _inc(buffer, buffer);
1141 _negate(buffer, buffer);
1145 void sc_val_from_ulong(unsigned long value, void *buffer)
1149 if (buffer == NULL) buffer = calc_buffer;
1152 while (pos < (char*)buffer + calc_buffer_size)
1154 *pos++ = _digit(value & 0xf);
1159 long sc_val_to_long(const void *val)
1164 for (i = calc_buffer_size - 1; i >= 0; i--)
1166 l = (l << 4) + _val(((char *)val)[i]);
1171 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1176 if (buffer == NULL) buffer = calc_buffer;
1177 CLEAR_BUFFER(buffer);
1179 if (!sign) return; /* unsigned means minimum is 0(zero) */
1183 bits = num_bits - 1;
1184 for (i = 0; i < bits/4; i++)
1187 *pos++ = min_digit[bits%4];
1189 for (i++; i <= calc_buffer_size - 1; i++)
1193 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1198 if (buffer == NULL) buffer = calc_buffer;
1199 CLEAR_BUFFER(buffer);
1202 bits = num_bits - sign;
1203 for (i = 0; i < bits/4; i++)
1206 *pos++ = max_digit[bits%4];
1208 for (i++; i <= calc_buffer_size - 1; i++)
1212 void sc_calc(const void* value1, const void* value2, unsigned op, void *buffer)
1214 char *unused_res; /* temp buffer holding unused result of divmod */
1216 const char *val1 = (const char *)value1;
1217 const char *val2 = (const char *)value2;
1219 unused_res = alloca(calc_buffer_size);
1221 CLEAR_BUFFER(calc_buffer);
1224 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1229 _negate(val1, calc_buffer);
1230 DEBUGPRINTF_COMPUTATION(("negated: %s\n", sc_print_hex(calc_buffer)));
1233 DEBUGPRINTF_COMPUTATION(("| "));
1234 _bitor(val1, val2, calc_buffer);
1237 DEBUGPRINTF_COMPUTATION(("& "));
1238 _bitand(val1, val2, calc_buffer);
1241 DEBUGPRINTF_COMPUTATION(("^ "));
1242 _bitxor(val1, val2, calc_buffer);
1245 _bitnot(val1, calc_buffer);
1246 DEBUGPRINTF_COMPUTATION(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1249 DEBUGPRINTF_COMPUTATION(("+ "));
1250 _add(val1, val2, calc_buffer);
1253 DEBUGPRINTF_COMPUTATION(("- "));
1254 _sub(val1, val2, calc_buffer);
1257 DEBUGPRINTF_COMPUTATION(("* "));
1258 _mul(val1, val2, calc_buffer);
1261 DEBUGPRINTF_COMPUTATION(("/ "));
1262 _divmod(val1, val2, calc_buffer, unused_res);
1265 DEBUGPRINTF_COMPUTATION(("%% "));
1266 _divmod(val1, val2, unused_res, calc_buffer);
1271 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1272 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1274 if ((buffer != NULL) && (buffer != calc_buffer))
1276 memcpy(buffer, calc_buffer, calc_buffer_size);
1280 void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op, void* buffer)
1282 const char *val1 = (const char *)value1;
1283 const char *val2 = (const char *)value2;
1287 offset = sc_val_to_long(val2);
1289 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1293 DEBUGPRINTF_COMPUTATION(("<< %ld ", offset));
1294 _shl(val1, calc_buffer, offset, radius, sign);
1297 DEBUGPRINTF_COMPUTATION((">> %ld ", offset));
1298 _shr(val1, calc_buffer, offset, radius, sign, 0);
1301 DEBUGPRINTF_COMPUTATION((">>> %ld ", offset));
1302 _shr(val1, calc_buffer, offset, radius, sign, 1);
1305 DEBUGPRINTF_COMPUTATION(("<<>> %ld ", offset));
1306 _rot(val1, calc_buffer, offset, radius, sign);
1311 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1313 if ((buffer != NULL) && (buffer != calc_buffer))
1315 memmove(buffer, calc_buffer, calc_buffer_size);
1319 int sc_comp(const void* value1, const void* value2)
1321 int counter = calc_buffer_size - 1;
1322 const char *val1 = (const char *)value1;
1323 const char *val2 = (const char *)value2;
1325 /* compare signs first:
1326 * the loop below can only compare values of the same sign! */
1327 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1329 /* loop until two digits differ, the values are equal if there
1330 * are no such two digits */
1331 while (val1[counter] == val2[counter])
1334 if (counter < 0) return 0;
1337 /* the leftmost digit is the most significant, so this returns
1338 * the correct result.
1339 * This implies the digit enum is ordered */
1340 return (val1[counter] > val2[counter]) ? (1) : (-1);
1343 int sc_get_highest_set_bit(const void *value)
1345 const char *val = (const char*)value;
1348 high = calc_buffer_size * 4 - 1;
1350 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1351 if (val[counter] == SC_0) high -= 4;
1353 if (val[counter] > SC_7) return high;
1354 else if (val[counter] > SC_3) return high - 1;
1355 else if (val[counter] > SC_1) return high - 2;
1356 else return high - 3;
1362 int sc_get_lowest_set_bit(const void *value)
1364 const char *val = (const char*)value;
1368 sign = (_sign(val)==1)?(SC_0):(SC_F);
1371 for (counter = 0; counter < calc_buffer_size; counter++) {
1372 if (val[counter] == SC_0) low += 4;
1374 if (val[counter] < SC_2) return low;
1375 else if (val[counter] < SC_4) return low + 1;
1376 else if (val[counter] < SC_8) return low + 2;
1377 else return low + 3;
1383 int sc_is_zero(const void *value)
1385 const char* val = (const char *)value;
1388 for (counter = 0; counter < calc_buffer_size; counter++) {
1389 if (val[counter] != SC_0) return 0;
1394 int sc_is_negative(const void *value)
1396 return _sign(value) == -1;
1399 int sc_had_carry(void)
1404 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1406 const char *val = (const char *)value;
1407 unsigned nibble_ofs = 2 * byte_ofs;
1410 /* the current scheme uses one byte to store a nibble */
1411 if (nibble_ofs >= len)
1414 res = _val(val[nibble_ofs]);
1415 if (len > nibble_ofs + 1)
1416 res |= _val(val[nibble_ofs + 1]) << 4;
1422 * convert to a string
1423 * XXX Doesn't check buffer bounds
1425 const char *sc_print(const void *value, unsigned bits, enum base_t base)
1427 static const char big_digits[] = "0123456789ABCDEF";
1428 static const char small_digits[] = "0123456789abcdef";
1430 char *base_val, *div1_res, *div2_res, *rem_res;
1431 int counter, nibbles, i, sign;
1434 const char *val = (const char *)value;
1438 const char *digits = small_digits;
1440 base_val = alloca(calc_buffer_size);
1441 div1_res = alloca(calc_buffer_size);
1442 div2_res = alloca(calc_buffer_size);
1443 rem_res = alloca(calc_buffer_size);
1445 pos = output_buffer + bit_pattern_size;
1450 bits = bit_pattern_size;
1451 #ifdef STRCALC_DEBUG_FULLPRINT
1455 nibbles = bits >> 2;
1459 digits = big_digits;
1461 for (counter = 0; counter < nibbles; ++counter) {
1462 *(--pos) = digits[_val(val[counter])];
1463 #ifdef STRCALC_DEBUG_GROUPPRINT
1464 if ((counter+1)%8 == 0)
1469 /* last nibble must be masked */
1471 x = and_table[_val(val[++counter])][bits & 3];
1472 *(--pos) = digits[_val(x)];
1475 /* now kill zeros */
1476 for (; counter > 1; --counter, ++pos) {
1477 #ifdef STRCALC_DEBUG_GROUPPRINT
1478 if (pos[0] == ' ') ++pos;
1486 for (counter = 0; counter < nibbles; ++counter) {
1488 p = binary_table[_val(val[counter])];
1495 /* last nibble must be masked */
1497 x = and_table[_val(val[++counter])][bits & 3];
1500 p = binary_table[_val(x)];
1507 /* now kill zeros */
1508 for (counter <<= 2; counter > 1; --counter, ++pos)
1515 memset(base_val, SC_0, calc_buffer_size);
1516 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1520 if (base == SC_DEC) {
1521 /* check for negative values */
1522 if (_bit(val, bits - 1)) {
1523 _negate(val, div2_res);
1529 /* transfer data into oscilating buffers */
1530 memset(div1_res, SC_0, calc_buffer_size);
1531 for (counter = 0; counter < nibbles; ++counter)
1532 div1_res[counter] = p[counter];
1534 /* last nibble must be masked */
1538 div1_res[counter] = and_table[_val(p[counter])][bits & 3];
1544 _divmod(m, base_val, n, rem_res);
1548 *(--pos) = digits[_val(rem_res[0])];
1551 for (i = 0; i < sizeof(div1_res); ++i)
1562 printf("%i\n", base);
1569 void init_strcalc(int precision)
1571 if (calc_buffer == NULL) {
1572 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1574 /* round up to multiple of 4 */
1575 precision = (precision + 3) & ~3;
1577 bit_pattern_size = (precision);
1578 calc_buffer_size = (precision / 2);
1579 max_value_size = (precision / 4);
1581 calc_buffer = malloc(calc_buffer_size+1 * sizeof(char));
1582 output_buffer = malloc(bit_pattern_size+1 * sizeof(char));
1584 if (calc_buffer == NULL || output_buffer == NULL) {
1585 assert(0 && "malloc failed");
1589 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));
1594 void finish_strcalc() {
1595 free(calc_buffer); calc_buffer = NULL;
1596 free(output_buffer); output_buffer = NULL;
1599 int sc_get_precision(void)
1601 return bit_pattern_size;