1 /****i* strcalc/implementation
9 #include <assert.h> /* assertions */
10 #include <string.h> /* memset/memcmp */
14 #include <stdio.h> /* output for error messages */
18 * local definitions and macros
20 #define BIT_PATTERN_SIZE (8 * BIGGEST_INTEGER_SIZE_IN_BYTES)
21 #define CALC_BUFFER_SIZE (4 * BIGGEST_INTEGER_SIZE_IN_BYTES)
22 #define MAX_VALUE_SIZE (2 * BIGGEST_INTEGER_SIZE_IN_BYTES)
24 #define CLEAR_CALC_BUFFER() assert(calc_buffer); memset(calc_buffer, SC_0, CALC_BUFFER_SIZE)
25 #define _val(a) ((a)-SC_0)
26 #define _digit(a) ((a)+SC_0)
27 #define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
29 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__, __LINE__)
31 /* shortcut output for debugging only, gices always full precisition */
32 #define sc_print_hex(a) sc_print((a), 0, SC_HEX)
33 #define sc_print_dec(a) sc_print((a), 0, SC_DEC)
34 #define sc_print_oct(a) sc_print((a), 0, SC_OCT)
35 #define sc_print_bin(a) sc_print((a), 0, SC_BIN)
39 # define DEBUGPRINTF(x) printf x
41 # define DEBUGPRINTF(x) ((void)0)
48 static char calc_buffer[CALC_BUFFER_SIZE]; /* buffer holding all results */
50 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
51 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
53 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
54 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
56 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
58 static const char and_table[16][16] = {
59 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
60 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
62 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
63 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
65 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
66 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
68 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
69 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
71 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
72 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
74 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
75 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
77 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
78 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
80 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
81 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
83 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
84 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
86 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
87 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
89 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
90 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
92 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
93 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
95 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
96 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
98 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
99 SC_8, SC_9, SC_8, SC_9, SC_D, SC_E, SC_D, SC_E },
101 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
102 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
104 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
105 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
107 static const char or_table[16][16] = {
108 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
109 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
111 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
112 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
114 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
115 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
117 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
118 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
120 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
121 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
123 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
124 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
126 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
127 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
129 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
130 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
132 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
133 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
135 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
136 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
138 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
139 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
141 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
142 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
144 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
145 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
147 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
148 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
150 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
151 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
153 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
154 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
156 static char const xor_table[16][16] = {
157 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
158 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
160 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
161 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
163 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
164 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
166 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
167 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
169 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
170 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
172 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
173 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
175 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
176 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
178 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
179 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
181 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
182 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
184 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
185 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
187 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
188 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
190 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
191 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
193 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
194 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
196 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
197 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
199 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
200 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
202 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
203 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
206 static char const add_table[16][16][2] = {
207 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
208 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
209 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
210 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
212 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
213 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
214 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
215 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
217 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
218 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
219 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
220 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
222 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
223 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
224 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
225 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
227 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
228 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
229 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
230 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
232 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
233 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
234 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
235 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
237 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
238 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
239 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
240 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
242 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
243 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
244 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
245 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
247 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
248 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
249 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
250 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
252 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
253 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
254 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
255 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
257 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
258 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
259 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
260 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
262 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
263 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
264 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
265 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
267 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
268 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
269 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
270 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
272 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
273 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
274 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
275 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
277 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
278 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
279 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
280 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
282 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
283 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
284 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
285 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
288 static char const mul_table[16][16][2] = {
289 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
290 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
291 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
292 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
294 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
295 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
296 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
297 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
299 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
300 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
301 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
302 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
304 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
305 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
306 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
307 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
309 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
310 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
311 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
312 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
314 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
315 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
316 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
317 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
319 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
320 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
321 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
322 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
324 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
325 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
326 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
327 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
329 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
330 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
331 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
332 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
334 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
335 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
336 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
337 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
339 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
340 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
341 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
342 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
344 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
345 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
346 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
347 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
349 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
350 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
351 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
352 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
354 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
355 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
356 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
357 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
359 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
360 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
361 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
362 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
364 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
365 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
366 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
367 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
370 static char const shrs_table[16][4][2] = {
371 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
372 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
373 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
374 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
375 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
376 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
377 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
378 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
379 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
380 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
381 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
382 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
383 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
384 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
385 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
386 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
389 /* for converting to binary string */
390 static const char *binary_table[16] = {
391 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
392 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
395 /*****************************************************************************
397 *****************************************************************************/
398 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
399 const char *file, int line)
402 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
403 while (len-- && *str) printf("%c", *str++); printf("\n");
404 while (--pos) printf(" "); printf("^\n");
408 static void _bitnot(const char *val, char *buffer)
412 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
413 buffer[counter] = not_table[_val(val[counter])];
416 static void _bitor(const char *val1, const char *val2, char *buffer)
420 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
421 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
424 static void _bitxor(const char *val1, const char *val2, char *buffer)
428 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
429 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
432 static void _bitand(const char *val1, const char *val2, char *buffer)
436 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
437 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
440 static int _sign(const char *val)
442 return (val[CALC_BUFFER_SIZE-1] < SC_7) ? (1) : (-1);
445 static void _inc(char *val, char *buffer)
449 while (counter++ < CALC_BUFFER_SIZE)
458 /* No carry here, *val != SC_F */
459 *buffer = add_table[_val(*val)][SC_1][0];
463 /* here a carry could be lost, this is intended because this will only
464 * happen when a value changes sign. */
467 static void _negate(const char *val, char *buffer)
469 _bitnot(val, buffer);
470 _inc(buffer, buffer);
473 static void _add(const char *val1, const char *val2, char *buffer)
476 const char *add1, *add2;
479 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++)
481 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
482 add2 = add_table[_val(add1[0])][_val(carry)];
483 /* carry might be zero */
484 buffer[counter] = add2[0];
485 carry = add_table[_val(add1[1])][_val(add2[1])][0];
487 /* loose last carry, which will occur only when changing sign */
490 static void _mul(const char *val1, const char *val2, char *buffer)
492 char temp_buffer[CALC_BUFFER_SIZE]; /* result buffer */
493 char neg_val1[CALC_BUFFER_SIZE]; /* abs of val1 */
494 char neg_val2[CALC_BUFFER_SIZE]; /* abs of val2 */
496 const char *mul, *add1, *add2; /* intermediate result containers */
497 char carry = SC_0; /* container for carries */
498 char sign = 0; /* marks result sign */
499 int c_inner, c_outer; /* loop counters */
501 /* init result buffer to zeroes */
502 memset(temp_buffer, SC_0, CALC_BUFFER_SIZE);
504 /* the multiplication works only for positive values, for negative values *
505 * it is necessary to negate them and adjust the result accordingly */
506 if (_sign(val1) == -1) {
507 _negate(val1, neg_val1);
511 if (_sign(val2) == -1) {
512 _negate(val2, neg_val2);
517 for (c_outer = 0; c_outer < MAX_VALUE_SIZE; c_outer++)
519 if (val2[c_outer] != SC_0)
521 for (c_inner = 0; c_inner < MAX_VALUE_SIZE; c_inner++)
523 /* do the following calculation: *
524 * Add the current carry, the value at position c_outer+c_inner *
525 * and the result of the multiplication of val1[c_inner] and *
526 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
528 /* multiplicate the two digits */
529 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
530 /* add old value to result of multiplication */
531 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
532 /* add carry to the sum */
533 add2 = add_table[_val(add1[0])][_val(carry)];
535 /* all carries together result in new carry. This is always smaller *
537 * Both multiplicands, the carry and the value already in the temp *
538 * buffer are single digits and their value is therefore at most *
541 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
542 * The tables list all operations rem b, so the carry is at most *
543 * (b*b-1)rem b = -1rem b = b-1 */
544 carry = add_table[_val(mul[1])][_val(add1[1])][0];
545 carry = add_table[_val(carry)][_val(add2[1])][0];
547 temp_buffer[c_inner + c_outer] = add2[0];
550 /* A carry may hang over */
551 /* c_outer is always smaller than MAX_VALUE_SIZE! */
552 temp_buffer[MAX_VALUE_SIZE + c_outer] = carry;
557 _negate(temp_buffer, buffer);
559 memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
562 static void _sub(const char *val1, const char *val2, char *buffer)
564 char temp_buffer[CALC_BUFFER_SIZE]; /* intermediate buffer to hold -val2 */
566 _negate(val2, temp_buffer);
567 _add(val1, temp_buffer, buffer);
570 static void _push(const char digit, char *buffer)
574 for (counter = CALC_BUFFER_SIZE - 2; counter >= 0; counter--)
576 buffer[counter+1] = buffer[counter];
581 /* XXX: This is MOST slow */
582 static void _divmod(const char *dividend, const char *divisor, char *quot, char *rem)
584 const char *minus_divisor;
585 char neg_val1[CALC_BUFFER_SIZE];
586 char neg_val2[CALC_BUFFER_SIZE];
588 char sign = 0; /* remember result sign */
590 int c_dividend; /* loop counters */
592 /* clear result buffer */
593 memset(quot, SC_0, CALC_BUFFER_SIZE);
594 memset(rem, SC_0, CALC_BUFFER_SIZE);
596 /* if the dividend is zero result is zero (quot is zero)*/
597 if (sc_comp(dividend, quot) == 0)
599 /* if the divisor is zero this won't work (quot is zero) */
600 if (sc_comp(divisor, quot) == 0) assert(0 && "quotision by zero!");
602 if (_sign(dividend) == -1)
604 _negate(dividend, neg_val1);
609 _negate(divisor, neg_val2);
610 if (_sign(divisor) == -1)
613 minus_divisor = divisor;
618 minus_divisor = neg_val2;
621 /* if divisor >= dividend quotision is easy
622 * (remember these are absolute values) */
623 switch (sc_comp(dividend, divisor))
625 case 0: /* dividend == divisor */
629 case -1: /* dividend < divisor */
630 memcpy(rem, dividend, CALC_BUFFER_SIZE);
633 default: /* unluckily quotision is necessary :( */
637 for (c_dividend = MAX_VALUE_SIZE - 1; c_dividend >= 0; c_dividend--)
639 _push(dividend[c_dividend], rem);
642 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
644 /* subtract until the remainder becomes negative, this should
645 * be faster than comparing remainder with divisor */
646 _add(rem, minus_divisor, rem);
648 while (_sign(rem) == 1)
650 quot[0] = add_table[_val(quot[0])][SC_1][0];
651 _add(rem, minus_divisor, rem);
654 /* subtracted one too much */
655 _add(rem, divisor, rem);
666 static void _shl(const char *val1, const char *val2, char *buffer, unsigned radius, unsigned is_signed)
676 assert((_sign(val2) != -1) || (0 && "negative leftshift"));
677 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
678 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
679 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
681 /* the whole value must be moved left the number of bytes represented
682 * by the value in quot, with bytes to the right set to zero */
683 /*XXX This might result in trouble */
684 for (counter = MAX_VALUE_SIZE - 1; counter >= 0; counter--)
686 offset = (offset << 4) | (_val(val2[counter]));
689 shift = shift_table[_val(offset%4)]; /* this is 2 ** (val2 % 4) */
691 /* if shifting far enough the result is zero */
692 if (offset >= radius)
694 memset(buffer, SC_0, CALC_BUFFER_SIZE);
699 /* shift the single digits some bytes (offset) and some bits (table)
701 for (counter = 0; counter < CALC_BUFFER_SIZE - offset; counter++)
703 shl = mul_table[_val(val1[counter])][_val(shift)];
704 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
708 /* fill with zeroes */
709 for (counter = 0; counter < offset; counter++) buffer[counter] = 0;
710 /* if the mode was signed, change sign when the mode's msb is now 1 */
711 offset = (radius - 1) / 4;
712 bitoffset = (radius - 1) % 4;
713 if (is_signed && _bitisset(buffer[offset], bitoffset) && (_sign(buffer) == 1))
715 /* this sets the upper bits of the leftmost digit */
716 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
717 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
719 buffer[counter] = SC_F;
722 else if (is_signed && !_bitisset(buffer[offset], bitoffset) && (_sign(buffer) == -1))
724 /* this unsets the upper bits of the leftmost digit */
725 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
726 /* zero the upper part of the value */
727 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
729 buffer[counter] = SC_0;
734 static void _shr(const char *val1, const char *val2, char *buffer, unsigned radius, unsigned is_signed, int signed_shift)
746 assert((_sign(val2) != -1) || (0 && "negative rightshift"));
747 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
748 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
749 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
751 /*XXX get the value of val2, this might result in trouble *
752 * (but who wants shifts THAT far anyway) */
753 for (counter = MAX_VALUE_SIZE - 1; counter >= 0; counter--)
755 offset = (offset << 4) | (_val(val2[counter]));
758 shift = offset % 4; /* this is val2 % 4 */
760 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
761 /* if shifting far enough the result is either 0 or -1 */
762 if (offset >= radius)
764 memset(buffer, sign, CALC_BUFFER_SIZE);
769 buffer[0] = shrs_table[_val(val1[offset])][shift][0];
770 /* shift digits to the right with offset, carry and all */
771 for (counter = 1; counter < radius/4; counter++)
773 shrs = shrs_table[_val(val1[counter + offset])][shift];
774 buffer[counter] = shrs[0];
775 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
778 /* the last digit is special in regard of signed/unsigned shift */
779 /* counter = radius/4 (after for loop) */
780 bitoffset = radius%4;
781 msd = val1[counter]; /* most significant digit */
783 /* remove sign bits if mode was signed and this is an unsigned shift */
784 if (!signed_shift && is_signed) {
785 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
788 shrs = shrs_table[_val(msd)][shift];
790 /* signed shift and signed mode and negative value means all bits to the left are set */
791 if (signed_shift && is_signed && (_sign(val1) == -1)) {
792 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
794 buffer[counter] = shrs[0];
796 buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
798 /* fill with SC_F or SC_0 depending on sign */
799 for (counter++; counter < CALC_BUFFER_SIZE; counter++)
801 buffer[counter] = sign;
805 /* positive: low-order -> high order, negative other direction */
806 static void _rot(const char *val1, const char *val2, char *buffer, unsigned radius, unsigned is_signed)
808 char temp_buffer[CALC_BUFFER_SIZE];
813 int counter, old_counter;
818 /*XXX get the value of val2, this might result in trouble *
819 * (but who wants shifts THAT far anyway) */
820 for (counter = MAX_VALUE_SIZE - 1; counter >= 0; counter--)
822 offset = (offset << 4) | (_val(val2[counter]));
824 /* rotation by multiples of the typelength is identity */
825 offset = offset % radius;
827 memmove(buffer, val1, CALC_BUFFER_SIZE);
830 /* rotation to the right is the same as rotation to the left
831 * when done by the right amount */
832 if (offset < 0) offset = radius + offset;
834 shift = _val(shift_table[offset % 4]);
837 DEBUGPRINTF(("offset: %d, shift: %d\n", offset, shift));
838 for (counter = 0; counter < radius/4 - offset; counter++)
840 shl = mul_table[_val(val1[counter])][_val(shift)];
841 temp_buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
843 DEBUGPRINTF(("%d(%x): %s\n", counter, shl[0], sc_print_hex(temp_buffer)));
845 old_counter = counter;
846 for (; counter < radius/4; counter++)
848 shl = mul_table[_val(val1[counter])][_val(shift)];
849 temp_buffer[counter - old_counter] = or_table[_val(shl[0])][_val(carry)];
851 DEBUGPRINTF(("%d(%x)> %s\n", counter, shl[0], sc_print_hex(temp_buffer)));
853 temp_buffer[counter - old_counter] = or_table[_val(temp_buffer[counter-old_counter])][_val(carry)];
855 offset = (radius-1)/4;
856 bitoffset - (radius-1)%4;
857 /* fill the rest of the buffer depending on msb and mode signedness*/
858 if (is_signed && _bitisset(temp_buffer[offset], bitoffset))
867 memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
870 /*****************************************************************************
871 * public functions, declared in strcalc.h
872 *****************************************************************************/
873 const void *sc_get_buffer(void)
875 return (void*)calc_buffer;
878 const int sc_get_buffer_length(void)
880 return CALC_BUFFER_SIZE;
883 void sc_val_from_str(const char *str, unsigned int len)
885 const char *orig_str = str;
886 unsigned int orig_len = len;
889 char base[CALC_BUFFER_SIZE];
890 char val[CALC_BUFFER_SIZE];
892 /* verify valid pointers (not null) */
894 /* a string no characters long is an error */
898 memset(base, SC_0, CALC_BUFFER_SIZE);
899 memset(val, SC_0, CALC_BUFFER_SIZE);
901 /* strip leading spaces */
902 while ((len > 0) && (*str == ' ')) { len--; str++; }
904 /* if the first two characters are 0x or 0X -> hex
905 * if the first is a 0 -> oct
906 * else dec, strip leading -/+ and remember sign
908 * only a + or - sign is no number resulting in an error */
913 if (str[1] == 'x' || str[1] == 'X') /* hex */
917 base[1] = SC_1; base[0] = SC_0;
923 base[1] = SC_0; base[0] = SC_8;
931 base[1] = SC_0; base[0] = SC_A;
940 base[1] = SC_0; base[0] = SC_A;
944 default: /* dec, else would have begun with 0x or 0 */
945 base[1] = SC_0; base[0] = SC_A;
948 else /* dec, else would have begun with 0x or 0 */
950 base[1] = SC_0; base[0] = SC_A;
953 /* begin string evaluation, from left to right */
964 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
966 val[0] = _digit((*str)-'a'+10);
968 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
977 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
979 val[0] = _digit((*str)-'A'+10);
981 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
986 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
988 val[0] = _digit((*str)-'0');
990 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1002 val[0] = _digit((*str)-'0');
1007 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1008 } /* switch(*str) */
1010 /* Radix conversion from base b to base B:
1011 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1012 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1013 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1015 /* get ready for the next letter */
1019 } /* while (len > 0 ) */
1023 _negate(calc_buffer, calc_buffer);
1027 void sc_val_from_long(long value)
1035 /* FIXME MININT won't work */
1036 if (sign) value = -value;
1038 CLEAR_CALC_BUFFER();
1040 while ((value != 0) && (pos < calc_buffer + CALC_BUFFER_SIZE))
1042 *pos++ = _digit(value % 16);
1046 if (sign) _negate(calc_buffer, calc_buffer);
1049 long sc_val_to_long(const void *val)
1054 for (i = CALC_BUFFER_SIZE - 1; i >= 0; i--)
1056 l = (l << 4) + _val(((char *)val)[i]);
1061 void sc_min_from_bits(unsigned int num_bits, unsigned int sign)
1066 CLEAR_CALC_BUFFER();
1067 if (!sign) return; /* unsigned means minimum is 0(zero) */
1071 bits = num_bits - 1;
1072 for (i = 0; i < bits/4; i++)
1075 *pos++ = min_digit[bits%4];
1077 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1081 void sc_max_from_bits(unsigned int num_bits, unsigned int sign)
1086 CLEAR_CALC_BUFFER();
1089 bits = num_bits - sign;
1090 for (i = 0; i < bits/4; i++)
1093 *pos++ = max_digit[bits%4];
1095 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1099 void sc_calc(const void* value1, const void* value2, unsigned op)
1101 char unused_res[CALC_BUFFER_SIZE]; /* temp buffer holding unused result of divmod */
1103 const char *val1 = (const char *)value1;
1104 const char *val2 = (const char *)value2;
1105 CLEAR_CALC_BUFFER();
1107 DEBUGPRINTF(("%s ", sc_print(value1, SC_HEX)));
1112 _negate(val1, calc_buffer);
1113 DEBUGPRINTF(("negated: %s\n", sc_print_hex(calc_buffer)));
1116 DEBUGPRINTF(("| "));
1117 _bitor(val1, val2, calc_buffer);
1120 DEBUGPRINTF(("& "));
1121 _bitand(val1, val2, calc_buffer);
1124 DEBUGPRINTF(("^ "));
1125 _bitxor(val1, val2, calc_buffer);
1128 _bitnot(val1, calc_buffer);
1129 DEBUGPRINTF(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1132 DEBUGPRINTF(("+ "));
1133 _add(val1, val2, calc_buffer);
1136 DEBUGPRINTF(("- "));
1137 _sub(val1, val2, calc_buffer);
1140 DEBUGPRINTF(("* "));
1141 _mul(val1, val2, calc_buffer);
1144 DEBUGPRINTF(("/ "));
1145 _divmod(val1, val2, calc_buffer, unused_res);
1148 DEBUGPRINTF(("%% "));
1149 _divmod(val1, val2, unused_res, calc_buffer);
1154 DEBUGPRINTF(("%s -> ", sc_print_hex(value2)));
1155 DEBUGPRINTF(("%s\n", sc_print_hex(calc_buffer)));
1158 void sc_bitcalc(const void* value1, const void* value2, unsigned radius, unsigned sign, unsigned op)
1160 const char *val1 = (const char *)value1;
1161 const char *val2 = (const char *)value2;
1162 CLEAR_CALC_BUFFER();
1164 DEBUGPRINTF(("%s ", sc_print_hex(value1)));
1168 DEBUGPRINTF(("<< "));
1169 _shl(val1, val2, calc_buffer, radius, sign);
1172 DEBUGPRINTF((">> "));
1173 _shr(val1, val2, calc_buffer, radius, sign, 0);
1176 DEBUGPRINTF((">>> "));
1177 _shr(val1, val2, calc_buffer, radius, sign, 1);
1180 DEBUGPRINTF(("<<>> "));
1181 _rot(val1, val2, calc_buffer, radius, sign);
1186 DEBUGPRINTF(("%s -> ", sc_print_hex(value2)));
1187 DEBUGPRINTF(("%s\n", sc_print_hex(calc_buffer)));
1190 int sc_comp(const void* value1, const void* value2)
1192 int counter = CALC_BUFFER_SIZE - 1;
1193 const char *val1 = (const char *)value1;
1194 const char *val2 = (const char *)value2;
1196 /* compare signs first:
1197 * the loop below can only compare values of the same sign! */
1198 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1200 /* loop until two digits differ, the values are equal if there
1201 * are no such two digits */
1202 while (val1[counter] == val2[counter])
1205 if (counter < 0) return 0;
1208 /* the leftmost digit is the most significant, so this returns
1209 * the correct result.
1210 * This implies the digit enum is ordered */
1211 return (val1[counter] > val2[counter]) ? (1) : (-1);
1214 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1216 const char *val = (const char *)value;
1217 unsigned nibble_ofs = 2 * byte_ofs;
1220 /* the current scheme uses one byte to store a nibble */
1221 if (nibble_ofs >= len)
1224 res = _val(val[nibble_ofs]);
1225 if (len > nibble_ofs + 1)
1226 res |= _val(val[nibble_ofs + 1]) << 4;
1232 * convert to a string
1234 const char *sc_print(const void *value, unsigned bits, enum base_t base)
1236 char base_val[CALC_BUFFER_SIZE];
1237 char div1_res[CALC_BUFFER_SIZE];
1238 char div2_res[CALC_BUFFER_SIZE];
1239 char rem_res[CALC_BUFFER_SIZE];
1240 int counter, nibbles, i;
1243 const char *val = (const char *)value;
1247 static char *buf = NULL;
1250 /* TODO: this buffer could be allocated in the initialising phase too */
1251 buf = malloc(BIT_PATTERN_SIZE + 1);
1256 pos = buf + BIT_PATTERN_SIZE;
1261 bits = BIT_PATTERN_SIZE;
1263 nibbles = bits >> 2;
1267 for (counter = 0; counter < nibbles; ++counter)
1268 *(--pos) = "0123456789abcdef"[_val(val[counter])];
1270 /* last nibble must be masked */
1272 x = and_table[_val(val[++counter])][bits & 3];
1273 *(--pos) = "0123456789abcdef"[_val(x)];
1276 /* now kill zeros */
1277 for (; counter > 1; --counter, ++pos)
1283 for (counter = 0; counter < nibbles; ++counter) {
1285 p = binary_table[_val(val[counter])];
1292 /* last nibble must be masked */
1294 x = and_table[_val(val[++counter])][bits & 3];
1297 p = binary_table[_val(x)];
1304 /* now kill zeros */
1305 for (counter <<= 2; counter > 1; --counter, ++pos)
1312 memset(base_val, SC_0, CALC_BUFFER_SIZE);
1313 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1315 /* transfer data into oscilating buffers */
1316 memset(div1_res, SC_0, CALC_BUFFER_SIZE);
1317 for (counter = 0; counter < nibbles; ++counter)
1318 div1_res[counter] = val[counter];
1320 /* last nibble must be masked */
1324 div1_res[counter] = and_table[_val(val[counter])][bits & 3];
1330 _divmod(m, base_val, n, rem_res);
1334 *(--pos) = "0123456789abcdef"[_val(rem_res[0])];
1337 for (i = 0; i < sizeof(div1_res); ++i)