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__)
32 /* shortcut output for debugging only, gices always full precisition */
33 # define sc_print_hex(a) sc_print((a), 0, SC_HEX)
34 # define sc_print_dec(a) sc_print((a), 0, SC_DEC)
35 # define sc_print_oct(a) sc_print((a), 0, SC_OCT)
36 # define sc_print_bin(a) sc_print((a), 0, SC_BIN)
37 # define DEBUGPRINTF(x) printf x
39 # define DEBUGPRINTF(x) ((void)0)
46 static char *calc_buffer = NULL; /* buffer holding all results */
47 static char *output_buffer = NULL; /* buffer for output */
48 static int BIT_PATTERN_SIZE;
49 static int CALC_BUFFER_SIZE;
50 static int MAX_VALUE_SIZE;
52 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
53 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
55 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
56 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
58 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
60 static const char and_table[16][16] = {
61 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
62 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
64 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
65 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
67 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
68 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
70 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
71 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
73 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
74 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
76 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
77 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
79 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
80 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
82 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
83 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
85 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
86 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
88 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
89 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
91 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
92 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
94 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
95 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
97 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
98 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
100 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
101 SC_8, SC_9, SC_8, SC_9, SC_D, SC_E, SC_D, SC_E },
103 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
104 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
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 static const char or_table[16][16] = {
110 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
111 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
113 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
114 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
116 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
117 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
119 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
120 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
122 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
123 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
125 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
126 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
128 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
129 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
131 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
132 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
134 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
135 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
137 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
138 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
140 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
141 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
143 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
144 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
146 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
147 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
149 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
150 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
152 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
153 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
155 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
156 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
158 static char const xor_table[16][16] = {
159 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
160 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
162 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
163 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
165 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
166 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
168 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
169 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
171 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
172 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
174 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
175 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
177 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
178 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
180 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
181 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
183 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
184 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
186 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
187 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
189 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
190 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
192 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
193 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
195 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
196 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
198 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
199 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
201 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
202 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
204 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
205 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
208 static char const add_table[16][16][2] = {
209 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
210 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
211 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
212 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
214 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
215 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
216 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
217 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
219 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
220 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
221 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
222 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
224 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
225 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
226 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
227 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
229 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
230 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
231 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
232 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
234 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
235 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
236 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
237 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
239 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
240 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
241 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
242 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
244 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
245 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
246 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
247 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
249 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
250 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
251 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
252 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
254 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
255 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
256 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
257 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
259 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
260 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
261 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
262 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
264 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
265 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
266 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
267 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
269 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
270 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
271 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
272 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
274 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
275 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
276 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
277 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
279 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
280 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
281 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
282 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
284 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
285 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
286 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
287 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
290 static char const mul_table[16][16][2] = {
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},
293 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
294 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
296 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
297 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
298 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
299 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
301 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
302 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
303 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
304 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
306 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
307 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
308 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
309 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
311 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
312 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
313 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
314 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
316 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
317 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
318 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
319 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
321 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
322 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
323 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
324 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
326 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
327 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
328 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
329 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
331 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
332 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
333 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
334 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
336 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
337 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
338 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
339 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
341 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
342 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
343 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
344 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
346 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
347 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
348 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
349 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
351 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
352 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
353 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
354 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
356 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
357 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
358 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
359 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
361 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
362 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
363 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
364 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
366 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
367 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
368 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
369 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
372 static char const shrs_table[16][4][2] = {
373 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
374 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
375 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
376 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
377 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
378 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
379 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
380 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
381 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
382 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
383 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
384 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
385 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
386 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
387 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
388 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
391 /* for converting to binary string */
392 static const char *binary_table[16] = {
393 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
394 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
397 /*****************************************************************************
399 *****************************************************************************/
400 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
401 const char *file, int line)
404 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
405 while (len-- && *str) printf("%c", *str++); printf("\n");
406 while (--pos) printf(" "); printf("^\n");
410 static void _bitnot(const char *val, char *buffer)
414 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
415 buffer[counter] = not_table[_val(val[counter])];
418 static void _bitor(const char *val1, const char *val2, char *buffer)
422 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
423 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
426 static void _bitxor(const char *val1, const char *val2, char *buffer)
430 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
431 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
434 static void _bitand(const char *val1, const char *val2, char *buffer)
438 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
439 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
442 static int _sign(const char *val)
444 return (val[CALC_BUFFER_SIZE-1] < SC_7) ? (1) : (-1);
447 static void _inc(char *val, char *buffer)
451 while (counter++ < CALC_BUFFER_SIZE)
460 /* No carry here, *val != SC_F */
461 *buffer = add_table[_val(*val)][SC_1][0];
465 /* here a carry could be lost, this is intended because this will only
466 * happen when a value changes sign. */
469 static void _negate(const char *val, char *buffer)
471 _bitnot(val, buffer);
472 _inc(buffer, buffer);
475 static void _add(const char *val1, const char *val2, char *buffer)
478 const char *add1, *add2;
481 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++)
483 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
484 add2 = add_table[_val(add1[0])][_val(carry)];
485 /* carry might be zero */
486 buffer[counter] = add2[0];
487 carry = add_table[_val(add1[1])][_val(add2[1])][0];
489 /* loose last carry, which will occur only when changing sign */
492 static void _mul(const char *val1, const char *val2, char *buffer)
494 char temp_buffer[CALC_BUFFER_SIZE]; /* result buffer */
495 char neg_val1[CALC_BUFFER_SIZE]; /* abs of val1 */
496 char neg_val2[CALC_BUFFER_SIZE]; /* abs of val2 */
498 const char *mul, *add1, *add2; /* intermediate result containers */
499 char carry = SC_0; /* container for carries */
500 char sign = 0; /* marks result sign */
501 int c_inner, c_outer; /* loop counters */
503 /* init result buffer to zeroes */
504 memset(temp_buffer, SC_0, CALC_BUFFER_SIZE);
506 /* the multiplication works only for positive values, for negative values *
507 * it is necessary to negate them and adjust the result accordingly */
508 if (_sign(val1) == -1) {
509 _negate(val1, neg_val1);
513 if (_sign(val2) == -1) {
514 _negate(val2, neg_val2);
519 for (c_outer = 0; c_outer < MAX_VALUE_SIZE; c_outer++)
521 if (val2[c_outer] != SC_0)
523 for (c_inner = 0; c_inner < MAX_VALUE_SIZE; c_inner++)
525 /* do the following calculation: *
526 * Add the current carry, the value at position c_outer+c_inner *
527 * and the result of the multiplication of val1[c_inner] and *
528 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
530 /* multiplicate the two digits */
531 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
532 /* add old value to result of multiplication */
533 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
534 /* add carry to the sum */
535 add2 = add_table[_val(add1[0])][_val(carry)];
537 /* all carries together result in new carry. This is always smaller *
539 * Both multiplicands, the carry and the value already in the temp *
540 * buffer are single digits and their value is therefore at most *
543 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
544 * The tables list all operations rem b, so the carry is at most *
545 * (b*b-1)rem b = -1rem b = b-1 */
546 carry = add_table[_val(mul[1])][_val(add1[1])][0];
547 carry = add_table[_val(carry)][_val(add2[1])][0];
549 temp_buffer[c_inner + c_outer] = add2[0];
552 /* A carry may hang over */
553 /* c_outer is always smaller than MAX_VALUE_SIZE! */
554 temp_buffer[MAX_VALUE_SIZE + c_outer] = carry;
559 _negate(temp_buffer, buffer);
561 memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
564 static void _sub(const char *val1, const char *val2, char *buffer)
566 char temp_buffer[CALC_BUFFER_SIZE]; /* intermediate buffer to hold -val2 */
568 _negate(val2, temp_buffer);
569 _add(val1, temp_buffer, buffer);
572 static void _push(const char digit, char *buffer)
576 for (counter = CALC_BUFFER_SIZE - 2; counter >= 0; counter--)
578 buffer[counter+1] = buffer[counter];
583 /* XXX: This is MOST slow */
584 static void _divmod(const char *dividend, const char *divisor, char *quot, char *rem)
586 const char *minus_divisor;
587 char neg_val1[CALC_BUFFER_SIZE];
588 char neg_val2[CALC_BUFFER_SIZE];
590 char sign = 0; /* remember result sign */
592 int c_dividend; /* loop counters */
594 /* clear result buffer */
595 memset(quot, SC_0, CALC_BUFFER_SIZE);
596 memset(rem, SC_0, CALC_BUFFER_SIZE);
598 /* if the dividend is zero result is zero (quot is zero)*/
599 if (sc_comp(dividend, quot) == 0)
601 /* if the divisor is zero this won't work (quot is zero) */
602 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
604 if (_sign(dividend) == -1)
606 _negate(dividend, neg_val1);
611 _negate(divisor, neg_val2);
612 if (_sign(divisor) == -1)
615 minus_divisor = divisor;
620 minus_divisor = neg_val2;
623 /* if divisor >= dividend quotision is easy
624 * (remember these are absolute values) */
625 switch (sc_comp(dividend, divisor))
627 case 0: /* dividend == divisor */
631 case -1: /* dividend < divisor */
632 memcpy(rem, dividend, CALC_BUFFER_SIZE);
635 default: /* unluckily quotision is necessary :( */
639 for (c_dividend = MAX_VALUE_SIZE - 1; c_dividend >= 0; c_dividend--)
641 _push(dividend[c_dividend], rem);
644 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
646 /* subtract until the remainder becomes negative, this should
647 * be faster than comparing remainder with divisor */
648 _add(rem, minus_divisor, rem);
650 while (_sign(rem) == 1)
652 quot[0] = add_table[_val(quot[0])][SC_1][0];
653 _add(rem, minus_divisor, rem);
656 /* subtracted one too much */
657 _add(rem, divisor, rem);
668 static void _shl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
677 assert((offset >= 0) || (0 && "negative leftshift"));
678 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
679 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
680 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
682 /* if shifting far enough the result is zero */
683 if (offset >= radius)
685 memset(buffer, SC_0, CALC_BUFFER_SIZE);
689 shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
692 /* shift the single digits some bytes (offset) and some bits (table)
694 for (counter = 0; counter < radius/4 - offset; counter++)
696 shl = mul_table[_val(val1[counter])][_val(shift)];
697 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
702 shl = mul_table[_val(val1[counter])][_val(shift)];
703 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
706 bitoffset = counter - 1;
709 /* fill with zeroes */
710 for (counter = 0; counter < offset; counter++) buffer[counter] = SC_0;
712 /* if the mode was signed, change sign when the mode's msb is now 1 */
713 offset = bitoffset + offset;
714 bitoffset = (radius-1) % 4;
715 if (is_signed && _bitisset(buffer[offset], bitoffset))
717 /* this sets the upper bits of the leftmost digit */
718 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
719 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
721 buffer[counter] = SC_F;
724 else if (is_signed && !_bitisset(buffer[offset], bitoffset))
726 /* this unsets the upper bits of the leftmost digit */
727 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
728 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
730 buffer[counter] = SC_0;
735 static void _shr(const char *val1, char *buffer, long offset, int radius, unsigned is_signed, int signed_shift)
746 assert((offset >= 0) || (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 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
753 /* if shifting far enough the result is either 0 or -1 */
754 if (offset >= radius)
756 memset(buffer, sign, CALC_BUFFER_SIZE);
764 if (radius/4 - offset > 0) {
765 buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
769 /* shift digits to the right with offset, carry and all */
770 for (; counter < radius/4 - offset; 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 bitoffset = radius%4;
780 msd = val1[radius/4]; /* most significant digit */
782 /* remove sign bits if mode was signed and this is an unsigned shift */
783 if (!signed_shift && is_signed) {
784 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
787 shrs = shrs_table[_val(msd)][shift];
789 /* signed shift and signed mode and negative value means all bits to the left are set */
790 if (signed_shift && is_signed && (_sign(val1) == -1)) {
791 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
793 buffer[counter] = shrs[0];
795 if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
797 /* fill with SC_F or SC_0 depending on sign */
798 for (counter++; counter < CALC_BUFFER_SIZE; counter++)
800 buffer[counter] = sign;
804 /* positive: low-order -> high order, negative other direction */
805 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
807 char temp1[CALC_BUFFER_SIZE];
808 char temp2[CALC_BUFFER_SIZE];
813 int counter, old_counter;
817 offset = offset % radius;
819 /* rotation by multiples of the typelength is identity */
821 memmove(buffer, val1, CALC_BUFFER_SIZE);
825 _shl(val1, temp1, offset, radius, is_signed);
826 _shr(val1, temp2, radius - offset, radius, is_signed, 0);
827 _bitor(temp1, temp2, buffer);
830 /*****************************************************************************
831 * public functions, declared in strcalc.h
832 *****************************************************************************/
833 const void *sc_get_buffer(void)
835 return (void*)calc_buffer;
838 const int sc_get_buffer_length(void)
840 return CALC_BUFFER_SIZE;
843 /* XXX doesn't check for overflows */
844 void sc_val_from_str(const char *str, unsigned int len)
846 const char *orig_str = str;
847 unsigned int orig_len = len;
850 char base[CALC_BUFFER_SIZE];
851 char val[CALC_BUFFER_SIZE];
853 /* verify valid pointers (not null) */
855 /* a string no characters long is an error */
859 memset(base, SC_0, CALC_BUFFER_SIZE);
860 memset(val, SC_0, CALC_BUFFER_SIZE);
862 /* strip leading spaces */
863 while ((len > 0) && (*str == ' ')) { len--; str++; }
865 /* if the first two characters are 0x or 0X -> hex
866 * if the first is a 0 -> oct
867 * else dec, strip leading -/+ and remember sign
869 * only a + or - sign is no number resulting in an error */
874 if (str[1] == 'x' || str[1] == 'X') /* hex */
878 base[1] = SC_1; base[0] = SC_0;
884 base[1] = SC_0; base[0] = SC_8;
892 base[1] = SC_0; base[0] = SC_A;
901 base[1] = SC_0; base[0] = SC_A;
905 default: /* dec, else would have begun with 0x or 0 */
906 base[1] = SC_0; base[0] = SC_A;
909 else /* dec, else would have begun with 0x or 0 */
911 base[1] = SC_0; base[0] = SC_A;
914 /* begin string evaluation, from left to right */
925 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
927 val[0] = _digit((*str)-'a'+10);
929 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
938 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
940 val[0] = _digit((*str)-'A'+10);
942 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
947 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
949 val[0] = _digit((*str)-'0');
951 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
963 val[0] = _digit((*str)-'0');
968 fail_char(orig_str, orig_len, *str, str-orig_str+1);
971 /* Radix conversion from base b to base B:
972 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
973 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
974 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
976 /* get ready for the next letter */
980 } /* while (len > 0 ) */
984 _negate(calc_buffer, calc_buffer);
988 void sc_val_from_long(long value)
996 /* FIXME MININT won't work */
997 if (sign) value = -value;
1001 while ((value != 0) && (pos < calc_buffer + CALC_BUFFER_SIZE))
1003 *pos++ = _digit(value % 16);
1007 if (sign) _negate(calc_buffer, calc_buffer);
1010 long sc_val_to_long(const void *val)
1015 for (i = CALC_BUFFER_SIZE - 1; i >= 0; i--)
1017 l = (l << 4) + _val(((char *)val)[i]);
1022 void sc_min_from_bits(unsigned int num_bits, unsigned int sign)
1027 CLEAR_CALC_BUFFER();
1028 if (!sign) return; /* unsigned means minimum is 0(zero) */
1032 bits = num_bits - 1;
1033 for (i = 0; i < bits/4; i++)
1036 *pos++ = min_digit[bits%4];
1038 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1042 void sc_max_from_bits(unsigned int num_bits, unsigned int sign)
1047 CLEAR_CALC_BUFFER();
1050 bits = num_bits - sign;
1051 for (i = 0; i < bits/4; i++)
1054 *pos++ = max_digit[bits%4];
1056 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1060 void sc_calc(const void* value1, const void* value2, unsigned op)
1062 char unused_res[CALC_BUFFER_SIZE]; /* temp buffer holding unused result of divmod */
1064 const char *val1 = (const char *)value1;
1065 const char *val2 = (const char *)value2;
1066 CLEAR_CALC_BUFFER();
1068 DEBUGPRINTF(("%s ", sc_print(value1, SC_HEX)));
1073 _negate(val1, calc_buffer);
1074 DEBUGPRINTF(("negated: %s\n", sc_print_hex(calc_buffer)));
1077 DEBUGPRINTF(("| "));
1078 _bitor(val1, val2, calc_buffer);
1081 DEBUGPRINTF(("& "));
1082 _bitand(val1, val2, calc_buffer);
1085 DEBUGPRINTF(("^ "));
1086 _bitxor(val1, val2, calc_buffer);
1089 _bitnot(val1, calc_buffer);
1090 DEBUGPRINTF(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1093 DEBUGPRINTF(("+ "));
1094 _add(val1, val2, calc_buffer);
1097 DEBUGPRINTF(("- "));
1098 _sub(val1, val2, calc_buffer);
1101 DEBUGPRINTF(("* "));
1102 _mul(val1, val2, calc_buffer);
1105 DEBUGPRINTF(("/ "));
1106 _divmod(val1, val2, calc_buffer, unused_res);
1109 DEBUGPRINTF(("%% "));
1110 _divmod(val1, val2, unused_res, calc_buffer);
1115 DEBUGPRINTF(("%s -> ", sc_print_hex(value2)));
1116 DEBUGPRINTF(("%s\n", sc_print_hex(calc_buffer)));
1119 void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op)
1121 const char *val1 = (const char *)value1;
1122 const char *val2 = (const char *)value2;
1125 offset = sc_val_to_long(val2);
1127 DEBUGPRINTF(("%s ", sc_print_hex(value1)));
1131 DEBUGPRINTF(("<< %d ", offset));
1132 _shl(val1, calc_buffer, offset, radius, sign);
1135 DEBUGPRINTF((">> %d ", offset));
1136 _shr(val1, calc_buffer, offset, radius, sign, 0);
1139 DEBUGPRINTF((">>> %d ", offset));
1140 _shr(val1, calc_buffer, offset, radius, sign, 1);
1143 DEBUGPRINTF(("<<>> %d ", offset));
1144 _rot(val1, calc_buffer, offset, radius, sign);
1149 DEBUGPRINTF(("-> %s\n", sc_print_hex(calc_buffer)));
1152 int sc_comp(const void* value1, const void* value2)
1154 int counter = CALC_BUFFER_SIZE - 1;
1155 const char *val1 = (const char *)value1;
1156 const char *val2 = (const char *)value2;
1158 /* compare signs first:
1159 * the loop below can only compare values of the same sign! */
1160 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1162 /* loop until two digits differ, the values are equal if there
1163 * are no such two digits */
1164 while (val1[counter] == val2[counter])
1167 if (counter < 0) return 0;
1170 /* the leftmost digit is the most significant, so this returns
1171 * the correct result.
1172 * This implies the digit enum is ordered */
1173 return (val1[counter] > val2[counter]) ? (1) : (-1);
1176 int sc_get_highest_set_bit(const void *value)
1178 const char *val = (const char*)value;
1182 high = CALC_BUFFER_SIZE * 4;
1184 for (counter = CALC_BUFFER_SIZE-1; counter >= 0; counter--) {
1185 if (val[counter] == SC_0) high -= 4;
1187 if (val[counter] > SC_7) return high;
1188 else if (val[counter] > SC_3) return high - 1;
1189 else if (val[counter] > SC_1) return high - 2;
1190 else return high - 3;
1196 int sc_get_lowest_set_bit(const void *value)
1198 const char *val = (const char*)value;
1202 sign = (_sign(val)==1)?(SC_0):(SC_F);
1205 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++) {
1206 if (val[counter] == SC_0) low += 4;
1208 if (val[counter] < SC_2) return low;
1209 else if (val[counter] < SC_4) return low + 1;
1210 else if (val[counter] < SC_8) return low + 2;
1211 else return low + 3;
1217 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1219 const char *val = (const char *)value;
1220 unsigned nibble_ofs = 2 * byte_ofs;
1223 /* the current scheme uses one byte to store a nibble */
1224 if (nibble_ofs >= len)
1227 res = _val(val[nibble_ofs]);
1228 if (len > nibble_ofs + 1)
1229 res |= _val(val[nibble_ofs + 1]) << 4;
1235 * convert to a string
1237 const char *sc_print(const void *value, unsigned bits, enum base_t base)
1239 char base_val[CALC_BUFFER_SIZE];
1240 char div1_res[CALC_BUFFER_SIZE];
1241 char div2_res[CALC_BUFFER_SIZE];
1242 char rem_res[CALC_BUFFER_SIZE];
1243 int counter, nibbles, i;
1246 const char *val = (const char *)value;
1251 pos = output_buffer + BIT_PATTERN_SIZE;
1256 bits = BIT_PATTERN_SIZE;
1258 nibbles = bits >> 2;
1262 for (counter = 0; counter < nibbles; ++counter)
1263 *(--pos) = "0123456789abcdef"[_val(val[counter])];
1265 /* last nibble must be masked */
1267 x = and_table[_val(val[++counter])][bits & 3];
1268 *(--pos) = "0123456789abcdef"[_val(x)];
1271 /* now kill zeros */
1272 for (; counter > 1; --counter, ++pos)
1278 for (counter = 0; counter < nibbles; ++counter) {
1280 p = binary_table[_val(val[counter])];
1287 /* last nibble must be masked */
1289 x = and_table[_val(val[++counter])][bits & 3];
1292 p = binary_table[_val(x)];
1299 /* now kill zeros */
1300 for (counter <<= 2; counter > 1; --counter, ++pos)
1307 memset(base_val, SC_0, CALC_BUFFER_SIZE);
1308 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1310 /* transfer data into oscilating buffers */
1311 memset(div1_res, SC_0, CALC_BUFFER_SIZE);
1312 for (counter = 0; counter < nibbles; ++counter)
1313 div1_res[counter] = val[counter];
1315 /* last nibble must be masked */
1319 div1_res[counter] = and_table[_val(val[counter])][bits & 3];
1325 _divmod(m, base_val, n, rem_res);
1329 *(--pos) = "0123456789abcdef"[_val(rem_res[0])];
1332 for (i = 0; i < sizeof(div1_res); ++i)
1347 void init_strcalc(int precision_in_bytes)
1349 if (calc_buffer == NULL) {
1350 if (precision_in_bytes <= 0) precision_in_bytes = DEFAULT_PRECISION_IN_BYTES;
1352 BIT_PATTERN_SIZE = (8 * precision_in_bytes);
1353 CALC_BUFFER_SIZE = (4 * precision_in_bytes);
1354 MAX_VALUE_SIZE = (2 * precision_in_bytes);
1356 calc_buffer = malloc(CALC_BUFFER_SIZE * sizeof(char));
1357 output_buffer = malloc(BIT_PATTERN_SIZE * sizeof(char));
1359 if (calc_buffer == NULL || output_buffer == NULL)
1361 assert(0 && "malloc failed");
1365 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));
1370 return CALC_BUFFER_SIZE/4;