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 CLEAR_CALC_BUFFER() assert(calc_buffer); memset(calc_buffer, SC_0, CALC_BUFFER_SIZE)
21 #define _val(a) ((a)-SC_0)
22 #define _digit(a) ((a)+SC_0)
23 #define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
25 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__, __LINE__)
28 /* shortcut output for debugging only, gices always full precisition */
29 # define sc_print_hex(a) sc_print((a), 0, SC_HEX)
30 # define sc_print_dec(a) sc_print((a), 0, SC_DEC)
31 # define sc_print_oct(a) sc_print((a), 0, SC_OCT)
32 # define sc_print_bin(a) sc_print((a), 0, SC_BIN)
33 # define DEBUGPRINTF(x) printf x
35 # define DEBUGPRINTF(x) ((void)0)
42 static char *calc_buffer = NULL; /* buffer holding all results */
43 static char *output_buffer = NULL; /* buffer for output */
44 static int BIT_PATTERN_SIZE;
45 static int CALC_BUFFER_SIZE;
46 static int MAX_VALUE_SIZE;
48 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
49 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
51 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
52 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
54 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
56 static const char and_table[16][16] = {
57 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
58 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
60 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
61 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
63 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
64 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
66 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
67 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
69 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
70 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
72 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
73 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
75 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
76 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
78 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
79 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
81 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
82 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
84 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
85 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
87 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
88 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
90 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
91 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
93 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
94 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
96 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
97 SC_8, SC_9, SC_8, SC_9, SC_D, SC_E, SC_D, SC_E },
99 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
100 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
102 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
103 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
105 static const char or_table[16][16] = {
106 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
107 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
109 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
110 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
112 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
113 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
115 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
116 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
118 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
119 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
121 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
122 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
124 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
125 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
127 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
128 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
130 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
131 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
133 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
134 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
136 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
137 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
139 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
140 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
142 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
143 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
145 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
146 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
148 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
149 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
151 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
152 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
154 static char const xor_table[16][16] = {
155 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
156 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
158 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
159 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
161 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
162 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
164 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
165 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
167 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
168 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
170 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
171 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
173 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
174 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
176 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
177 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
179 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
180 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
182 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
183 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
185 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
186 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
188 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
189 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
191 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
192 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
194 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
195 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
197 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
198 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
200 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
201 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
204 static char const add_table[16][16][2] = {
205 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
206 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
207 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
208 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
210 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
211 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
212 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
213 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
215 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
216 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
217 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
218 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
220 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
221 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
222 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
223 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
225 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
226 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
227 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
228 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
230 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
231 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
232 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
233 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
235 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
236 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
237 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
238 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
240 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
241 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
242 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
243 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
245 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
246 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
247 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
248 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
250 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
251 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
252 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
253 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
255 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
256 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
257 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
258 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
260 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
261 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
262 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
263 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
265 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
266 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
267 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
268 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
270 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
271 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
272 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
273 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
275 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
276 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
277 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
278 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
280 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
281 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
282 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
283 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
286 static char const mul_table[16][16][2] = {
287 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
288 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
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} },
292 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
293 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
294 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
295 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
297 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
298 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
299 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
300 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
302 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
303 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
304 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
305 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
307 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
308 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
309 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
310 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
312 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
313 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
314 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
315 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
317 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
318 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
319 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
320 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
322 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
323 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
324 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
325 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
327 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
328 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
329 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
330 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
332 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
333 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
334 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
335 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
337 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
338 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
339 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
340 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
342 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
343 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
344 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
345 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
347 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
348 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
349 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
350 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
352 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
353 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
354 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
355 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
357 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
358 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
359 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
360 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
362 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
363 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
364 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
365 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
368 static char const shrs_table[16][4][2] = {
369 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
370 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
371 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
372 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
373 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
374 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
375 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
376 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
377 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
378 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
379 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
380 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
381 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
382 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
383 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
384 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
387 /* for converting to binary string */
388 static const char *binary_table[16] = {
389 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
390 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
393 /*****************************************************************************
395 *****************************************************************************/
396 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
397 const char *file, int line)
400 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
401 while (len-- && *str) printf("%c", *str++); printf("\n");
402 while (--pos) printf(" "); printf("^\n");
406 static void _bitnot(const char *val, char *buffer)
410 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
411 buffer[counter] = not_table[_val(val[counter])];
414 static void _bitor(const char *val1, const char *val2, char *buffer)
418 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
419 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
422 static void _bitxor(const char *val1, const char *val2, char *buffer)
426 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
427 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
430 static void _bitand(const char *val1, const char *val2, char *buffer)
434 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
435 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
438 static int _sign(const char *val)
440 return (val[CALC_BUFFER_SIZE-1] <= SC_7) ? (1) : (-1);
443 static void _inc(char *val, char *buffer)
447 while (counter++ < CALC_BUFFER_SIZE)
456 /* No carry here, *val != SC_F */
457 *buffer = add_table[_val(*val)][SC_1][0];
461 /* here a carry could be lost, this is intended because this will only
462 * happen when a value changes sign. */
465 static void _negate(const char *val, char *buffer)
467 _bitnot(val, buffer);
468 _inc(buffer, buffer);
471 static void _add(const char *val1, const char *val2, char *buffer)
474 const char *add1, *add2;
477 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++)
479 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
480 add2 = add_table[_val(add1[0])][_val(carry)];
481 /* carry might be zero */
482 buffer[counter] = add2[0];
483 carry = add_table[_val(add1[1])][_val(add2[1])][0];
485 /* loose last carry, which will occur only when changing sign */
488 static void _mul(const char *val1, const char *val2, char *buffer)
490 char temp_buffer[CALC_BUFFER_SIZE]; /* result buffer */
491 char neg_val1[CALC_BUFFER_SIZE]; /* abs of val1 */
492 char neg_val2[CALC_BUFFER_SIZE]; /* abs of val2 */
494 const char *mul, *add1, *add2; /* intermediate result containers */
495 char carry = SC_0; /* container for carries */
496 char sign = 0; /* marks result sign */
497 int c_inner, c_outer; /* loop counters */
499 /* init result buffer to zeroes */
500 memset(temp_buffer, SC_0, CALC_BUFFER_SIZE);
502 /* the multiplication works only for positive values, for negative values *
503 * it is necessary to negate them and adjust the result accordingly */
504 if (_sign(val1) == -1) {
505 _negate(val1, neg_val1);
509 if (_sign(val2) == -1) {
510 _negate(val2, neg_val2);
515 for (c_outer = 0; c_outer < MAX_VALUE_SIZE; c_outer++)
517 if (val2[c_outer] != SC_0)
519 for (c_inner = 0; c_inner < MAX_VALUE_SIZE; c_inner++)
521 /* do the following calculation: *
522 * Add the current carry, the value at position c_outer+c_inner *
523 * and the result of the multiplication of val1[c_inner] and *
524 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
526 /* multiplicate the two digits */
527 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
528 /* add old value to result of multiplication */
529 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
530 /* add carry to the sum */
531 add2 = add_table[_val(add1[0])][_val(carry)];
533 /* all carries together result in new carry. This is always smaller *
535 * Both multiplicands, the carry and the value already in the temp *
536 * buffer are single digits and their value is therefore at most *
539 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
540 * The tables list all operations rem b, so the carry is at most *
541 * (b*b-1)rem b = -1rem b = b-1 */
542 carry = add_table[_val(mul[1])][_val(add1[1])][0];
543 carry = add_table[_val(carry)][_val(add2[1])][0];
545 temp_buffer[c_inner + c_outer] = add2[0];
548 /* A carry may hang over */
549 /* c_outer is always smaller than MAX_VALUE_SIZE! */
550 temp_buffer[MAX_VALUE_SIZE + c_outer] = carry;
555 _negate(temp_buffer, buffer);
557 memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
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);
568 static void _push(const char digit, char *buffer)
572 for (counter = CALC_BUFFER_SIZE - 2; counter >= 0; counter--)
574 buffer[counter+1] = buffer[counter];
579 /* XXX: This is MOST slow */
580 static void _divmod(const char *dividend, const char *divisor, char *quot, char *rem)
582 const char *minus_divisor;
583 char neg_val1[CALC_BUFFER_SIZE];
584 char neg_val2[CALC_BUFFER_SIZE];
586 char sign = 0; /* remember result sign */
588 int c_dividend; /* loop counters */
590 /* clear result buffer */
591 memset(quot, SC_0, CALC_BUFFER_SIZE);
592 memset(rem, SC_0, CALC_BUFFER_SIZE);
594 /* if the dividend is zero result is zero (quot is zero)*/
595 if (sc_comp(dividend, quot) == 0)
597 /* if the divisor is zero this won't work (quot is zero) */
598 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
600 if (_sign(dividend) == -1)
602 _negate(dividend, neg_val1);
607 _negate(divisor, neg_val2);
608 if (_sign(divisor) == -1)
611 minus_divisor = divisor;
616 minus_divisor = neg_val2;
619 /* if divisor >= dividend quotision is easy
620 * (remember these are absolute values) */
621 switch (sc_comp(dividend, divisor))
623 case 0: /* dividend == divisor */
627 case -1: /* dividend < divisor */
628 memcpy(rem, dividend, CALC_BUFFER_SIZE);
631 default: /* unluckily quotision is necessary :( */
635 for (c_dividend = MAX_VALUE_SIZE - 1; c_dividend >= 0; c_dividend--)
637 _push(dividend[c_dividend], rem);
640 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
642 /* subtract until the remainder becomes negative, this should
643 * be faster than comparing remainder with divisor */
644 _add(rem, minus_divisor, rem);
646 while (_sign(rem) == 1)
648 quot[0] = add_table[_val(quot[0])][SC_1][0];
649 _add(rem, minus_divisor, rem);
652 /* subtracted one too much */
653 _add(rem, divisor, rem);
664 static void _shl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
673 assert((offset >= 0) || (0 && "negative leftshift"));
674 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
675 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
676 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
678 /* if shifting far enough the result is zero */
679 if (offset >= radius)
681 memset(buffer, SC_0, CALC_BUFFER_SIZE);
685 shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
688 /* shift the single digits some bytes (offset) and some bits (table)
690 for (counter = 0; counter < radius/4 - offset; counter++)
692 shl = mul_table[_val(val1[counter])][_val(shift)];
693 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
698 shl = mul_table[_val(val1[counter])][_val(shift)];
699 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
702 bitoffset = counter - 1;
705 /* fill with zeroes */
706 for (counter = 0; counter < offset; counter++) buffer[counter] = SC_0;
708 /* if the mode was signed, change sign when the mode's msb is now 1 */
709 offset = bitoffset + offset;
710 bitoffset = (radius-1) % 4;
711 if (is_signed && _bitisset(buffer[offset], bitoffset))
713 /* this sets the upper bits of the leftmost digit */
714 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
715 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
717 buffer[counter] = SC_F;
720 else if (is_signed && !_bitisset(buffer[offset], bitoffset))
722 /* this unsets the upper bits of the leftmost digit */
723 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
724 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
726 buffer[counter] = SC_0;
731 static void _shr(const char *val1, char *buffer, long offset, int radius, unsigned is_signed, int signed_shift)
742 assert((offset >= 0) || (0 && "negative rightshift"));
743 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
744 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
745 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
747 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
749 /* if shifting far enough the result is either 0 or -1 */
750 if (offset >= radius)
752 memset(buffer, sign, CALC_BUFFER_SIZE);
760 if (radius/4 - offset > 0) {
761 buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
765 /* shift digits to the right with offset, carry and all */
766 for (; counter < radius/4 - offset; counter++)
769 shrs = shrs_table[_val(val1[counter + offset])][shift];
770 buffer[counter] = shrs[0];
771 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
774 /* the last digit is special in regard of signed/unsigned shift */
775 bitoffset = radius%4;
776 msd = val1[radius/4]; /* most significant digit */
778 /* remove sign bits if mode was signed and this is an unsigned shift */
779 if (!signed_shift && is_signed) {
780 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
783 shrs = shrs_table[_val(msd)][shift];
785 /* signed shift and signed mode and negative value means all bits to the left are set */
786 if (signed_shift && is_signed && (_sign(val1) == -1)) {
787 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
789 buffer[counter] = shrs[0];
791 if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
793 /* fill with SC_F or SC_0 depending on sign */
794 for (counter++; counter < CALC_BUFFER_SIZE; counter++)
796 buffer[counter] = sign;
800 /* positive: low-order -> high order, negative other direction */
801 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
803 char temp1[CALC_BUFFER_SIZE];
804 char temp2[CALC_BUFFER_SIZE];
809 int counter, old_counter;
813 offset = offset % radius;
815 /* rotation by multiples of the typelength is identity */
817 memmove(buffer, val1, CALC_BUFFER_SIZE);
821 _shl(val1, temp1, offset, radius, is_signed);
822 _shr(val1, temp2, radius - offset, radius, is_signed, 0);
823 _bitor(temp1, temp2, buffer);
826 /*****************************************************************************
827 * public functions, declared in strcalc.h
828 *****************************************************************************/
829 const void *sc_get_buffer(void)
831 return (void*)calc_buffer;
834 const int sc_get_buffer_length(void)
836 return CALC_BUFFER_SIZE;
839 /* XXX doesn't check for overflows */
840 void sc_val_from_str(const char *str, unsigned int len)
842 const char *orig_str = str;
843 unsigned int orig_len = len;
846 char base[CALC_BUFFER_SIZE];
847 char val[CALC_BUFFER_SIZE];
849 /* verify valid pointers (not null) */
851 /* a string no characters long is an error */
855 memset(base, SC_0, CALC_BUFFER_SIZE);
856 memset(val, SC_0, CALC_BUFFER_SIZE);
858 /* strip leading spaces */
859 while ((len > 0) && (*str == ' ')) { len--; str++; }
861 /* if the first two characters are 0x or 0X -> hex
862 * if the first is a 0 -> oct
863 * else dec, strip leading -/+ and remember sign
865 * only a + or - sign is no number resulting in an error */
870 if (str[1] == 'x' || str[1] == 'X') /* hex */
874 base[1] = SC_1; base[0] = SC_0;
880 base[1] = SC_0; base[0] = SC_8;
888 base[1] = SC_0; base[0] = SC_A;
897 base[1] = SC_0; base[0] = SC_A;
901 default: /* dec, else would have begun with 0x or 0 */
902 base[1] = SC_0; base[0] = SC_A;
905 else /* dec, else would have begun with 0x or 0 */
907 base[1] = SC_0; base[0] = SC_A;
910 /* begin string evaluation, from left to right */
921 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
923 val[0] = _digit((*str)-'a'+10);
925 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
934 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
936 val[0] = _digit((*str)-'A'+10);
938 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
943 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
945 val[0] = _digit((*str)-'0');
947 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
959 val[0] = _digit((*str)-'0');
964 fail_char(orig_str, orig_len, *str, str-orig_str+1);
967 /* Radix conversion from base b to base B:
968 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
969 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
970 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
972 /* get ready for the next letter */
976 } /* while (len > 0 ) */
980 _negate(calc_buffer, calc_buffer);
984 void sc_val_from_long(long value)
992 /* FIXME MININT won't work */
993 if (sign) value = -value;
997 while ((value != 0) && (pos < calc_buffer + CALC_BUFFER_SIZE))
999 *pos++ = _digit(value % 16);
1003 if (sign) _negate(calc_buffer, calc_buffer);
1006 long sc_val_to_long(const void *val)
1011 for (i = CALC_BUFFER_SIZE - 1; i >= 0; i--)
1013 l = (l << 4) + _val(((char *)val)[i]);
1018 void sc_min_from_bits(unsigned int num_bits, unsigned int sign)
1023 CLEAR_CALC_BUFFER();
1024 if (!sign) return; /* unsigned means minimum is 0(zero) */
1028 bits = num_bits - 1;
1029 for (i = 0; i < bits/4; i++)
1032 *pos++ = min_digit[bits%4];
1034 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1038 void sc_max_from_bits(unsigned int num_bits, unsigned int sign)
1043 CLEAR_CALC_BUFFER();
1046 bits = num_bits - sign;
1047 for (i = 0; i < bits/4; i++)
1050 *pos++ = max_digit[bits%4];
1052 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1056 void sc_calc(const void* value1, const void* value2, unsigned op)
1058 char unused_res[CALC_BUFFER_SIZE]; /* temp buffer holding unused result of divmod */
1060 const char *val1 = (const char *)value1;
1061 const char *val2 = (const char *)value2;
1062 CLEAR_CALC_BUFFER();
1064 DEBUGPRINTF(("%s ", sc_print(value1, SC_HEX)));
1069 _negate(val1, calc_buffer);
1070 DEBUGPRINTF(("negated: %s\n", sc_print_hex(calc_buffer)));
1073 DEBUGPRINTF(("| "));
1074 _bitor(val1, val2, calc_buffer);
1077 DEBUGPRINTF(("& "));
1078 _bitand(val1, val2, calc_buffer);
1081 DEBUGPRINTF(("^ "));
1082 _bitxor(val1, val2, calc_buffer);
1085 _bitnot(val1, calc_buffer);
1086 DEBUGPRINTF(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1089 DEBUGPRINTF(("+ "));
1090 _add(val1, val2, calc_buffer);
1093 DEBUGPRINTF(("- "));
1094 _sub(val1, val2, calc_buffer);
1097 DEBUGPRINTF(("* "));
1098 _mul(val1, val2, calc_buffer);
1101 DEBUGPRINTF(("/ "));
1102 _divmod(val1, val2, calc_buffer, unused_res);
1105 DEBUGPRINTF(("%% "));
1106 _divmod(val1, val2, unused_res, calc_buffer);
1111 DEBUGPRINTF(("%s -> ", sc_print_hex(value2)));
1112 DEBUGPRINTF(("%s\n", sc_print_hex(calc_buffer)));
1115 void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op)
1117 const char *val1 = (const char *)value1;
1118 const char *val2 = (const char *)value2;
1121 offset = sc_val_to_long(val2);
1123 DEBUGPRINTF(("%s ", sc_print_hex(value1)));
1127 DEBUGPRINTF(("<< %d ", offset));
1128 _shl(val1, calc_buffer, offset, radius, sign);
1131 DEBUGPRINTF((">> %d ", offset));
1132 _shr(val1, calc_buffer, offset, radius, sign, 0);
1135 DEBUGPRINTF((">>> %d ", offset));
1136 _shr(val1, calc_buffer, offset, radius, sign, 1);
1139 DEBUGPRINTF(("<<>> %d ", offset));
1140 _rot(val1, calc_buffer, offset, radius, sign);
1145 DEBUGPRINTF(("-> %s\n", sc_print_hex(calc_buffer)));
1148 int sc_comp(const void* value1, const void* value2)
1150 int counter = CALC_BUFFER_SIZE - 1;
1151 const char *val1 = (const char *)value1;
1152 const char *val2 = (const char *)value2;
1154 /* compare signs first:
1155 * the loop below can only compare values of the same sign! */
1156 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1158 /* loop until two digits differ, the values are equal if there
1159 * are no such two digits */
1160 while (val1[counter] == val2[counter])
1163 if (counter < 0) return 0;
1166 /* the leftmost digit is the most significant, so this returns
1167 * the correct result.
1168 * This implies the digit enum is ordered */
1169 return (val1[counter] > val2[counter]) ? (1) : (-1);
1172 int sc_get_highest_set_bit(const void *value)
1174 const char *val = (const char*)value;
1178 high = CALC_BUFFER_SIZE * 4;
1180 for (counter = CALC_BUFFER_SIZE-1; counter >= 0; counter--) {
1181 if (val[counter] == SC_0) high -= 4;
1183 if (val[counter] > SC_7) return high;
1184 else if (val[counter] > SC_3) return high - 1;
1185 else if (val[counter] > SC_1) return high - 2;
1186 else return high - 3;
1192 int sc_get_lowest_set_bit(const void *value)
1194 const char *val = (const char*)value;
1198 sign = (_sign(val)==1)?(SC_0):(SC_F);
1201 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++) {
1202 if (val[counter] == SC_0) low += 4;
1204 if (val[counter] < SC_2) return low;
1205 else if (val[counter] < SC_4) return low + 1;
1206 else if (val[counter] < SC_8) return low + 2;
1207 else return low + 3;
1213 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1215 const char *val = (const char *)value;
1216 unsigned nibble_ofs = 2 * byte_ofs;
1219 /* the current scheme uses one byte to store a nibble */
1220 if (nibble_ofs >= len)
1223 res = _val(val[nibble_ofs]);
1224 if (len > nibble_ofs + 1)
1225 res |= _val(val[nibble_ofs + 1]) << 4;
1231 * convert to a string
1233 const char *sc_print(const void *value, unsigned bits, enum base_t base)
1235 char base_val[CALC_BUFFER_SIZE];
1236 char div1_res[CALC_BUFFER_SIZE];
1237 char div2_res[CALC_BUFFER_SIZE];
1238 char rem_res[CALC_BUFFER_SIZE];
1239 int counter, nibbles, i, sign;
1242 const char *val = (const char *)value;
1247 pos = output_buffer + BIT_PATTERN_SIZE;
1252 bits = BIT_PATTERN_SIZE;
1254 nibbles = bits >> 2;
1258 for (counter = 0; counter < nibbles; ++counter)
1259 *(--pos) = "0123456789abcdef"[_val(val[counter])];
1261 /* last nibble must be masked */
1263 x = and_table[_val(val[++counter])][bits & 3];
1264 *(--pos) = "0123456789abcdef"[_val(x)];
1267 /* now kill zeros */
1268 for (; counter > 1; --counter, ++pos)
1274 for (counter = 0; counter < nibbles; ++counter) {
1276 p = binary_table[_val(val[counter])];
1283 /* last nibble must be masked */
1285 x = and_table[_val(val[++counter])][bits & 3];
1288 p = binary_table[_val(x)];
1295 /* now kill zeros */
1296 for (counter <<= 2; counter > 1; --counter, ++pos)
1303 memset(base_val, SC_0, CALC_BUFFER_SIZE);
1304 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1308 if (base == SC_DEC) {
1309 /* check for negative values */
1310 if (_sign(val) == -1) {
1311 _negate(val, div2_res);
1317 /* transfer data into oscilating buffers */
1318 memset(div1_res, SC_0, CALC_BUFFER_SIZE);
1319 for (counter = 0; counter < nibbles; ++counter)
1320 div1_res[counter] = m[counter];
1322 /* last nibble must be masked */
1326 div1_res[counter] = and_table[_val(m[counter])][bits & 3];
1332 _divmod(m, base_val, n, rem_res);
1336 *(--pos) = "0123456789abcdef"[_val(rem_res[0])];
1339 for (i = 0; i < sizeof(div1_res); ++i)
1356 void init_strcalc(int precision_in_bytes)
1358 if (calc_buffer == NULL) {
1359 if (precision_in_bytes <= 0) precision_in_bytes = DEFAULT_PRECISION_IN_BYTES;
1361 BIT_PATTERN_SIZE = (8 * precision_in_bytes);
1362 CALC_BUFFER_SIZE = (4 * precision_in_bytes);
1363 MAX_VALUE_SIZE = (2 * precision_in_bytes);
1365 calc_buffer = malloc(CALC_BUFFER_SIZE * sizeof(char));
1366 output_buffer = malloc(BIT_PATTERN_SIZE * sizeof(char));
1368 if (calc_buffer == NULL || output_buffer == NULL)
1370 assert(0 && "malloc failed");
1374 DEBUGPRINTF(("init strcalc: \n\tPRECISION: %d\n\tCALC_BUFFER_SIZE = %d\n\tMAX_VALUE_SIZE = %d\n\tbuffer pointer: %p\n", precision_in_bytes, CALC_BUFFER_SIZE, MAX_VALUE_SIZE, calc_buffer));
1379 return CALC_BUFFER_SIZE/4;