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)
64 static char *calc_buffer = NULL; /* buffer holding all results */
65 static char *output_buffer = NULL; /* buffer for output */
66 static int BIT_PATTERN_SIZE; /* maximum number of bits */
67 static int CALC_BUFFER_SIZE; /* size of internally stored values */
68 static int MAX_VALUE_SIZE; /* maximum size of values */
70 static int carry_flag; /**< some computation set the carry_flag:
71 - rightshift if bits were lost due to shifting
72 - division if there was a remainder
73 However, the meaning of carry is machine dependant
74 and often devined in other ways! */
76 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
77 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
79 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
80 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
82 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
84 static const char and_table[16][16] = {
85 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
86 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
88 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
89 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
91 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
92 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
94 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
95 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
97 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
98 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
100 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
101 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
103 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
104 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
106 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
107 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
109 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
110 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
112 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
113 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
115 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
116 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
118 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
119 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
121 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
122 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
124 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
125 SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
127 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
128 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
130 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
131 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
133 static const char or_table[16][16] = {
134 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
135 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
137 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
138 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
140 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
141 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
143 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
144 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
146 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
147 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
149 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
150 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
152 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
153 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
155 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
156 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
158 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
159 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
161 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
162 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
164 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
165 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
167 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
168 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
170 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
171 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
173 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
174 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
176 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
177 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
179 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
180 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
182 static char const xor_table[16][16] = {
183 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
184 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
186 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
187 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
189 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
190 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
192 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
193 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
195 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
196 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
198 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
199 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
201 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
202 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
204 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
205 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
207 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
208 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
210 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
211 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
213 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
214 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
216 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
217 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
219 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
220 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
222 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
223 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
225 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
226 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
228 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
229 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
232 static char const add_table[16][16][2] = {
233 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
234 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
235 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
236 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
238 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
239 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
240 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
241 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
243 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
244 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
245 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
246 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
248 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
249 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
250 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
251 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
253 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
254 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
255 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
256 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
258 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
259 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
260 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
261 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
263 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
264 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
265 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
266 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
268 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
269 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
270 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
271 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
273 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
274 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
275 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
276 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
278 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
279 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
280 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
281 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
283 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
284 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
285 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
286 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
288 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
289 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
290 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
291 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
293 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
294 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
295 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
296 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
298 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
299 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
300 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
301 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
303 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
304 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
305 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
306 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
308 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
309 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
310 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
311 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
314 static char const mul_table[16][16][2] = {
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},
318 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
320 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
321 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
322 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
323 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
325 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
326 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
327 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
328 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
330 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
331 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
332 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
333 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
335 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
336 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
337 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
338 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
340 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
341 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
342 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
343 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
345 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
346 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
347 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
348 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
350 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
351 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
352 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
353 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
355 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
356 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
357 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
358 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
360 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
361 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
362 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
363 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
365 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
366 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
367 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
368 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
370 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
371 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
372 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
373 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
375 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
376 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
377 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
378 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
380 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
381 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
382 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
383 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
385 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
386 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
387 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
388 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
390 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
391 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
392 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
393 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
396 static char const shrs_table[16][4][2] = {
397 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
398 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
399 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
400 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
401 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
402 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
403 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
404 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
405 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
406 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
407 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
408 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
409 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
410 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
411 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
412 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
415 /* for converting to binary string */
416 static const char *binary_table[16] = {
417 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
418 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
421 /*****************************************************************************
423 *****************************************************************************/
424 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
425 const char *file, int line)
428 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
429 while (len-- && *str) printf("%c", *str++); printf("\n");
430 while (--pos) printf(" "); printf("^\n");
435 * implements the bitwise NOT operation
437 static void _bitnot(const char *val, char *buffer)
441 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
442 buffer[counter] = not_table[_val(val[counter])];
446 * implements the bitwise OR operation
448 static void _bitor(const char *val1, const char *val2, char *buffer)
452 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
453 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
457 * implements the bitwise eXclusive OR operation
459 static void _bitxor(const char *val1, const char *val2, char *buffer)
463 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
464 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
468 * implements the bitwise AND operation
470 static void _bitand(const char *val1, const char *val2, char *buffer)
474 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
475 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
479 * returns the sign bit.
481 * @todo This implementation is wrong, as it returns the highest bit of the buffer
482 * NOT the highest bit depending on the real mode
484 static int _sign(const char *val)
486 return (val[CALC_BUFFER_SIZE-1] <= SC_7) ? (1) : (-1);
490 * returns non-zero if bit at position pos is set
492 static int _bit(const char *val, int pos)
495 int nibble = pos >> 2;
497 return _bitisset(val[nibble], bit);
501 * Implements a fast ADD + 1
503 static void _inc(const char *val, char *buffer)
507 while (counter++ < CALC_BUFFER_SIZE)
516 /* No carry here, *val != SC_F */
517 *buffer = add_table[_val(*val)][SC_1][0];
521 /* here a carry could be lost, this is intended because this should
522 * happen only when a value changes sign. */
526 * Implements a unary MINUS
528 static void _negate(const char *val, char *buffer)
530 _bitnot(val, buffer);
531 _inc(buffer, buffer);
535 * Implements a binary ADD
537 * @todo The implementation of carry is wrong, as it is the
538 * CALC_BUFFER_SIZE carry, not the mode depending
540 static void _add(const char *val1, const char *val2, char *buffer)
543 const char *add1, *add2;
546 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++)
548 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
549 add2 = add_table[_val(add1[0])][_val(carry)];
550 /* carry might be zero */
551 buffer[counter] = add2[0];
552 carry = add_table[_val(add1[1])][_val(add2[1])][0];
554 carry_flag = carry != SC_0;
558 * Implements a binary SUB
560 static void _sub(const char *val1, const char *val2, char *buffer)
562 char temp_buffer[CALC_BUFFER_SIZE]; /* intermediate buffer to hold -val2 */
564 _negate(val2, temp_buffer);
565 _add(val1, temp_buffer, buffer);
569 * Implements a binary MUL
571 static void _mul(const char *val1, const char *val2, char *buffer)
573 char* temp_buffer; /* result buffer */
574 char* neg_val1; /* abs of val1 */
575 char* neg_val2; /* abs of val2 */
577 const char *mul, *add1, *add2; /* intermediate result containers */
578 char carry = SC_0; /* container for carries */
579 char sign = 0; /* marks result sign */
580 int c_inner, c_outer; /* loop counters */
582 temp_buffer = alloca(CALC_BUFFER_SIZE);
583 neg_val1 = alloca(CALC_BUFFER_SIZE);
584 neg_val2 = alloca(CALC_BUFFER_SIZE);
586 /* init result buffer to zeroes */
587 memset(temp_buffer, SC_0, CALC_BUFFER_SIZE);
589 /* the multiplication works only for positive values, for negative values *
590 * it is necessary to negate them and adjust the result accordingly */
591 if (_sign(val1) == -1) {
592 _negate(val1, neg_val1);
596 if (_sign(val2) == -1) {
597 _negate(val2, neg_val2);
602 for (c_outer = 0; c_outer < MAX_VALUE_SIZE; c_outer++)
604 if (val2[c_outer] != SC_0)
606 for (c_inner = 0; c_inner < MAX_VALUE_SIZE; c_inner++)
608 /* do the following calculation: *
609 * Add the current carry, the value at position c_outer+c_inner *
610 * and the result of the multiplication of val1[c_inner] and *
611 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
613 /* multiplicate the two digits */
614 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
615 /* add old value to result of multiplication */
616 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
617 /* add carry to the sum */
618 add2 = add_table[_val(add1[0])][_val(carry)];
620 /* all carries together result in new carry. This is always smaller *
622 * Both multiplicands, the carry and the value already in the temp *
623 * buffer are single digits and their value is therefore at most *
626 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
627 * The tables list all operations rem b, so the carry is at most *
628 * (b*b-1)rem b = -1rem b = b-1 */
629 carry = add_table[_val(mul[1])][_val(add1[1])][0];
630 carry = add_table[_val(carry)][_val(add2[1])][0];
632 temp_buffer[c_inner + c_outer] = add2[0];
635 /* A carry may hang over */
636 /* c_outer is always smaller than MAX_VALUE_SIZE! */
637 temp_buffer[MAX_VALUE_SIZE + c_outer] = carry;
643 _negate(temp_buffer, buffer);
645 memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
649 * Shift the buffer to left and add a 4 bit digit
651 static void _push(const char digit, char *buffer)
655 for (counter = CALC_BUFFER_SIZE - 2; counter >= 0; counter--)
657 buffer[counter+1] = buffer[counter];
663 * Implements truncating integer division and remainder.
665 * Note: This is MOST slow
667 static void _divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
669 const char *dividend = rDividend;
670 const char *minus_divisor;
674 char div_sign = 0; /* remember division result sign */
675 char rem_sign = 0; /* remember remainder esult sign */
677 int c_dividend; /* loop counters */
679 neg_val1 = alloca(CALC_BUFFER_SIZE);
680 neg_val2 = alloca(CALC_BUFFER_SIZE);
682 /* clear result buffer */
683 memset(quot, SC_0, CALC_BUFFER_SIZE);
684 memset(rem, SC_0, CALC_BUFFER_SIZE);
686 /* if the divisor is zero this won't work (quot is zero) */
687 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
689 /* if the dividend is zero result is zero (quot is zero)*/
690 if (sc_comp(dividend, quot) == 0)
693 if (_sign(dividend) == -1) {
694 _negate(dividend, neg_val1);
700 _negate(divisor, neg_val2);
701 if (_sign(divisor) == -1) {
703 minus_divisor = divisor;
707 minus_divisor = neg_val2;
709 /* if divisor >= dividend division is easy
710 * (remember these are absolute values) */
711 switch (sc_comp(dividend, divisor))
713 case 0: /* dividend == divisor */
717 case -1: /* dividend < divisor */
718 memcpy(rem, rDividend, CALC_BUFFER_SIZE);
721 default: /* unluckily division is necessary :( */
725 for (c_dividend = CALC_BUFFER_SIZE - 1; c_dividend >= 0; c_dividend--)
727 _push(dividend[c_dividend], rem);
730 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
732 /* subtract until the remainder becomes negative, this should
733 * be faster than comparing remainder with divisor */
734 _add(rem, minus_divisor, rem);
736 while (_sign(rem) == 1)
738 quot[0] = add_table[_val(quot[0])][SC_1][0];
739 _add(rem, minus_divisor, rem);
742 /* subtracted one too much */
743 _add(rem, divisor, rem);
747 /* sets carry if remainder is non-zero ??? */
748 carry_flag = !sc_is_zero(rem);
758 * Implements a Shift Left, which can either preserve the sign bit
761 * @todo Assertions seems to be wrong
763 static void _shl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
772 assert((offset >= 0) || (0 && "negative leftshift"));
773 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
774 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
775 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
777 /* if shifting far enough the result is zero */
778 if (offset >= radius)
780 memset(buffer, SC_0, CALC_BUFFER_SIZE);
784 shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
787 /* shift the single digits some bytes (offset) and some bits (table)
789 for (counter = 0; counter < radius/4 - offset; counter++)
791 shl = mul_table[_val(val1[counter])][_val(shift)];
792 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
797 shl = mul_table[_val(val1[counter])][_val(shift)];
798 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
801 bitoffset = counter - 1;
804 /* fill with zeroes */
805 for (counter = 0; counter < offset; counter++) buffer[counter] = SC_0;
807 /* if the mode was signed, change sign when the mode's msb is now 1 */
808 offset = bitoffset + offset;
809 bitoffset = (radius-1) % 4;
810 if (is_signed && _bitisset(buffer[offset], bitoffset))
812 /* this sets the upper bits of the leftmost digit */
813 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
814 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
816 buffer[counter] = SC_F;
819 else if (is_signed && !_bitisset(buffer[offset], bitoffset))
821 /* this unsets the upper bits of the leftmost digit */
822 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
823 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
825 buffer[counter] = SC_0;
831 * Implements a Shift Right, which can either preserve the sign bit
834 * @todo Assertions seems to be wrong
836 static void _shr(const char *val1, char *buffer, long offset, int radius, unsigned is_signed, int signed_shift)
847 assert((offset >= 0) || (0 && "negative rightshift"));
848 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
849 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
850 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
852 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
854 /* if shifting far enough the result is either 0 or -1 */
855 if (offset >= radius)
857 if (!sc_is_zero(val1)) {
860 memset(buffer, sign, CALC_BUFFER_SIZE);
867 /* check if any bits are lost, and set carry_flag if so */
868 for (counter = 0; counter < offset; counter++)
870 if (val1[counter] != 0)
876 if ((_val(val1[counter]) & ((1<<shift)-1)) != 0)
880 /* shift digits to the right with offset, carry and all */
882 if (radius/4 - offset > 0) {
883 buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
886 for (; counter < radius/4 - offset; counter++)
888 shrs = shrs_table[_val(val1[counter + offset])][shift];
889 buffer[counter] = shrs[0];
890 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
893 /* the last digit is special in regard of signed/unsigned shift */
894 bitoffset = radius%4;
895 msd = (radius/4<CALC_BUFFER_SIZE)?(val1[radius/4]):(sign); /* most significant digit */
897 /* remove sign bits if mode was signed and this is an unsigned shift */
898 if (!signed_shift && is_signed) {
899 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
902 shrs = shrs_table[_val(msd)][shift];
904 /* signed shift and signed mode and negative value means all bits to the left are set */
905 if (signed_shift && is_signed && (_sign(val1) == -1)) {
906 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
908 buffer[counter] = shrs[0];
911 if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
913 /* fill with SC_F or SC_0 depending on sign */
914 for (counter++; counter < CALC_BUFFER_SIZE; counter++)
916 buffer[counter] = sign;
921 * Implements a Rotate Right.
922 * positive: low-order -> high order, negative other direction
924 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
927 temp1 = alloca(CALC_BUFFER_SIZE);
928 temp2 = alloca(CALC_BUFFER_SIZE);
930 offset = offset % radius;
932 /* rotation by multiples of the typelength is identity */
934 memmove(buffer, val1, CALC_BUFFER_SIZE);
938 _shl(val1, temp1, offset, radius, is_signed);
939 _shr(val1, temp2, radius - offset, radius, is_signed, 0);
940 _bitor(temp1, temp2, buffer);
941 carry_flag = 0; /* set by shr, but due to rot this is false */
944 /*****************************************************************************
945 * public functions, declared in strcalc.h
946 *****************************************************************************/
947 const void *sc_get_buffer(void)
949 return (void*)calc_buffer;
952 const int sc_get_buffer_length(void)
954 return CALC_BUFFER_SIZE;
957 /* XXX doesn't check for overflows */
958 void sc_val_from_str(const char *str, unsigned int len, void *buffer)
960 const char *orig_str = str;
961 unsigned int orig_len = len;
966 base = alloca(CALC_BUFFER_SIZE);
967 val = alloca(CALC_BUFFER_SIZE);
969 /* verify valid pointers (not null) */
971 /* a string no characters long is an error */
974 if (buffer == NULL) buffer = calc_buffer;
976 CLEAR_BUFFER(buffer);
977 memset(base, SC_0, CALC_BUFFER_SIZE);
978 memset(val, SC_0, CALC_BUFFER_SIZE);
980 /* strip leading spaces */
981 while ((len > 0) && (*str == ' ')) { len--; str++; }
983 /* if the first two characters are 0x or 0X -> hex
984 * if the first is a 0 -> oct
985 * else dec, strip leading -/+ and remember sign
987 * only a + or - sign is no number resulting in an error */
992 if (str[1] == 'x' || str[1] == 'X') /* hex */
996 base[1] = SC_1; base[0] = SC_0;
1002 base[1] = SC_0; base[0] = SC_8;
1010 base[1] = SC_0; base[0] = SC_A;
1019 base[1] = SC_0; base[0] = SC_A;
1023 default: /* dec, else would have begun with 0x or 0 */
1024 base[1] = SC_0; base[0] = SC_A;
1027 else /* dec, else would have begun with 0x or 0 */
1029 base[1] = SC_0; base[0] = SC_A;
1032 /* BEGIN string evaluation, from left to right */
1043 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1045 val[0] = _digit((*str)-'a'+10);
1047 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1056 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1058 val[0] = _digit((*str)-'A'+10);
1060 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1065 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
1067 val[0] = _digit((*str)-'0');
1069 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1081 val[0] = _digit((*str)-'0');
1086 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1087 } /* switch(*str) */
1089 /* Radix conversion from base b to base B:
1090 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1091 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1092 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1094 /* get ready for the next letter */
1098 } /* while (len > 0 ) */
1102 _negate(calc_buffer, calc_buffer);
1106 void sc_val_from_long(long value, void *buffer)
1109 char sign, is_minlong;
1111 if (buffer == NULL) buffer = calc_buffer;
1115 is_minlong = value == LONG_MIN;
1117 /* use absolute value, special treatment of MIN_LONG */
1125 CLEAR_BUFFER(buffer);
1127 while ((value != 0) && (pos < (char*)buffer + CALC_BUFFER_SIZE))
1129 *pos++ = _digit(value & 0xf);
1136 _inc(buffer, buffer);
1138 _negate(buffer, buffer);
1142 void sc_val_from_ulong(unsigned long value, void *buffer)
1146 if (buffer == NULL) buffer = calc_buffer;
1149 while (pos < (char*)buffer + CALC_BUFFER_SIZE)
1151 *pos++ = _digit(value & 0xf);
1156 long sc_val_to_long(const void *val)
1161 for (i = CALC_BUFFER_SIZE - 1; i >= 0; i--)
1163 l = (l << 4) + _val(((char *)val)[i]);
1168 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1173 if (buffer == NULL) buffer = calc_buffer;
1174 CLEAR_BUFFER(buffer);
1176 if (!sign) return; /* unsigned means minimum is 0(zero) */
1180 bits = num_bits - 1;
1181 for (i = 0; i < bits/4; i++)
1184 *pos++ = min_digit[bits%4];
1186 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1190 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1195 if (buffer == NULL) buffer = calc_buffer;
1196 CLEAR_BUFFER(buffer);
1199 bits = num_bits - sign;
1200 for (i = 0; i < bits/4; i++)
1203 *pos++ = max_digit[bits%4];
1205 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1209 void sc_calc(const void* value1, const void* value2, unsigned op, void *buffer)
1211 char unused_res[CALC_BUFFER_SIZE]; /* temp buffer holding unused result of divmod */
1213 const char *val1 = (const char *)value1;
1214 const char *val2 = (const char *)value2;
1216 CLEAR_BUFFER(calc_buffer);
1219 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1224 _negate(val1, calc_buffer);
1225 DEBUGPRINTF_COMPUTATION(("negated: %s\n", sc_print_hex(calc_buffer)));
1228 DEBUGPRINTF_COMPUTATION(("| "));
1229 _bitor(val1, val2, calc_buffer);
1232 DEBUGPRINTF_COMPUTATION(("& "));
1233 _bitand(val1, val2, calc_buffer);
1236 DEBUGPRINTF_COMPUTATION(("^ "));
1237 _bitxor(val1, val2, calc_buffer);
1240 _bitnot(val1, calc_buffer);
1241 DEBUGPRINTF_COMPUTATION(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1244 DEBUGPRINTF_COMPUTATION(("+ "));
1245 _add(val1, val2, calc_buffer);
1248 DEBUGPRINTF_COMPUTATION(("- "));
1249 _sub(val1, val2, calc_buffer);
1252 DEBUGPRINTF_COMPUTATION(("* "));
1253 _mul(val1, val2, calc_buffer);
1256 DEBUGPRINTF_COMPUTATION(("/ "));
1257 _divmod(val1, val2, calc_buffer, unused_res);
1260 DEBUGPRINTF_COMPUTATION(("%% "));
1261 _divmod(val1, val2, unused_res, calc_buffer);
1266 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1267 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1269 if ((buffer != NULL) && (buffer != calc_buffer))
1271 memcpy(buffer, calc_buffer, CALC_BUFFER_SIZE);
1275 void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op, void* buffer)
1277 const char *val1 = (const char *)value1;
1278 const char *val2 = (const char *)value2;
1282 offset = sc_val_to_long(val2);
1284 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1288 DEBUGPRINTF_COMPUTATION(("<< %ld ", offset));
1289 _shl(val1, calc_buffer, offset, radius, sign);
1292 DEBUGPRINTF_COMPUTATION((">> %ld ", offset));
1293 _shr(val1, calc_buffer, offset, radius, sign, 0);
1296 DEBUGPRINTF_COMPUTATION((">>> %ld ", offset));
1297 _shr(val1, calc_buffer, offset, radius, sign, 1);
1300 DEBUGPRINTF_COMPUTATION(("<<>> %ld ", offset));
1301 _rot(val1, calc_buffer, offset, radius, sign);
1306 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1308 if ((buffer != NULL) && (buffer != calc_buffer))
1310 memmove(buffer, calc_buffer, CALC_BUFFER_SIZE);
1314 int sc_comp(const void* value1, const void* value2)
1316 int counter = CALC_BUFFER_SIZE - 1;
1317 const char *val1 = (const char *)value1;
1318 const char *val2 = (const char *)value2;
1320 /* compare signs first:
1321 * the loop below can only compare values of the same sign! */
1322 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1324 /* loop until two digits differ, the values are equal if there
1325 * are no such two digits */
1326 while (val1[counter] == val2[counter])
1329 if (counter < 0) return 0;
1332 /* the leftmost digit is the most significant, so this returns
1333 * the correct result.
1334 * This implies the digit enum is ordered */
1335 return (val1[counter] > val2[counter]) ? (1) : (-1);
1338 int sc_get_highest_set_bit(const void *value)
1340 const char *val = (const char*)value;
1343 high = CALC_BUFFER_SIZE * 4 - 1;
1345 for (counter = CALC_BUFFER_SIZE-1; counter >= 0; counter--) {
1346 if (val[counter] == SC_0) high -= 4;
1348 if (val[counter] > SC_7) return high;
1349 else if (val[counter] > SC_3) return high - 1;
1350 else if (val[counter] > SC_1) return high - 2;
1351 else return high - 3;
1357 int sc_get_lowest_set_bit(const void *value)
1359 const char *val = (const char*)value;
1363 sign = (_sign(val)==1)?(SC_0):(SC_F);
1366 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++) {
1367 if (val[counter] == SC_0) low += 4;
1369 if (val[counter] < SC_2) return low;
1370 else if (val[counter] < SC_4) return low + 1;
1371 else if (val[counter] < SC_8) return low + 2;
1372 else return low + 3;
1378 int sc_is_zero(const void *value)
1380 const char* val = (const char *)value;
1383 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++) {
1384 if (val[counter] != SC_0) return 0;
1389 int sc_is_negative(const void *value)
1391 return _sign(value) == -1;
1394 int sc_had_carry(void)
1399 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1401 const char *val = (const char *)value;
1402 unsigned nibble_ofs = 2 * byte_ofs;
1405 /* the current scheme uses one byte to store a nibble */
1406 if (nibble_ofs >= len)
1409 res = _val(val[nibble_ofs]);
1410 if (len > nibble_ofs + 1)
1411 res |= _val(val[nibble_ofs + 1]) << 4;
1417 * convert to a string
1418 * XXX Doesn't check buffer bounds
1420 const char *sc_print(const void *value, unsigned bits, enum base_t base)
1422 static const char big_digits[] = "0123456789ABCDEF";
1423 static const char small_digits[] = "0123456789abcdef";
1425 char *base_val, *div1_res, *div2_res, *rem_res;
1426 int counter, nibbles, i, sign;
1429 const char *val = (const char *)value;
1433 const char *digits = small_digits;
1435 base_val = alloca(CALC_BUFFER_SIZE);
1436 div1_res = alloca(CALC_BUFFER_SIZE);
1437 div2_res = alloca(CALC_BUFFER_SIZE);
1438 rem_res = alloca(CALC_BUFFER_SIZE);
1440 pos = output_buffer + BIT_PATTERN_SIZE;
1445 bits = BIT_PATTERN_SIZE;
1446 #ifdef STRCALC_DEBUG_FULLPRINT
1450 nibbles = bits >> 2;
1454 digits = big_digits;
1456 for (counter = 0; counter < nibbles; ++counter) {
1457 *(--pos) = digits[_val(val[counter])];
1458 #ifdef STRCALC_DEBUG_GROUPPRINT
1459 if ((counter+1)%8 == 0)
1464 /* last nibble must be masked */
1466 x = and_table[_val(val[++counter])][bits & 3];
1467 *(--pos) = digits[_val(x)];
1470 /* now kill zeros */
1471 for (; counter > 1; --counter, ++pos) {
1472 #ifdef STRCALC_DEBUG_GROUPPRINT
1473 if (pos[0] == ' ') ++pos;
1481 for (counter = 0; counter < nibbles; ++counter) {
1483 p = binary_table[_val(val[counter])];
1490 /* last nibble must be masked */
1492 x = and_table[_val(val[++counter])][bits & 3];
1495 p = binary_table[_val(x)];
1502 /* now kill zeros */
1503 for (counter <<= 2; counter > 1; --counter, ++pos)
1510 memset(base_val, SC_0, CALC_BUFFER_SIZE);
1511 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1515 if (base == SC_DEC) {
1516 /* check for negative values */
1517 if (_bit(val, bits - 1)) {
1518 _negate(val, div2_res);
1524 /* transfer data into oscilating buffers */
1525 memset(div1_res, SC_0, CALC_BUFFER_SIZE);
1526 for (counter = 0; counter < nibbles; ++counter)
1527 div1_res[counter] = p[counter];
1529 /* last nibble must be masked */
1533 div1_res[counter] = and_table[_val(p[counter])][bits & 3];
1539 _divmod(m, base_val, n, rem_res);
1543 *(--pos) = digits[_val(rem_res[0])];
1546 for (i = 0; i < sizeof(div1_res); ++i)
1557 printf("%i\n", base);
1564 void init_strcalc(int precision)
1566 if (calc_buffer == NULL) {
1567 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1569 /* round up to multiple of 4 */
1570 precision = (precision + 3) & ~3;
1572 BIT_PATTERN_SIZE = (precision);
1573 CALC_BUFFER_SIZE = (precision / 2);
1574 MAX_VALUE_SIZE = (precision / 4);
1576 calc_buffer = malloc(CALC_BUFFER_SIZE+1 * sizeof(char));
1577 output_buffer = malloc(BIT_PATTERN_SIZE+1 * sizeof(char));
1579 if (calc_buffer == NULL || output_buffer == NULL) {
1580 assert(0 && "malloc failed");
1584 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));
1589 void finish_strcalc() {
1590 free(calc_buffer); calc_buffer = NULL;
1591 free(output_buffer); output_buffer = NULL;
1594 int sc_get_precision(void)
1596 return BIT_PATTERN_SIZE;