1 /****i* strcalc/implementation
12 #include <assert.h> /* assertions */
13 #include <string.h> /* memset/memcmp */
14 #include <stdio.h> /* output for error messages */
15 #include <values.h> /* definition of MIN_LONG, used in sc_get_val_from_long */
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__)
27 #ifdef STRCALC_DEBUG_COMPUTATION
28 # define DEBUGPRINTF_COMPUTATION(x) printf x
30 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
33 # define DEBUGPRINTF(x) printf x
35 # define DEBUGPRINTF(x) ((void)0)
43 static char *calc_buffer = NULL; /* buffer holding all results */
44 static char *output_buffer = NULL; /* buffer for output */
45 static int BIT_PATTERN_SIZE; /* maximum number of bits */
46 static int CALC_BUFFER_SIZE; /* size of internally stored values */
47 static int MAX_VALUE_SIZE; /* maximum size of values */
49 static int carry_flag; /* some computation set carry_flag: */
50 /* rightshift if bits were lost due to shifting */
51 /* division if there was a remainder */
53 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
54 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
56 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
57 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
59 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
61 static const char and_table[16][16] = {
62 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
63 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
65 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
66 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
68 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
69 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
71 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
72 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
74 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
75 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
77 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
78 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
80 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
81 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
83 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
84 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
86 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
87 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
89 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
90 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
92 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
93 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
95 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
96 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
98 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
99 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
101 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
102 SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
104 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
105 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
107 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
108 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
110 static const char or_table[16][16] = {
111 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
112 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
114 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
115 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
117 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
118 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
120 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
121 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
123 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
124 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
126 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
127 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
129 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
130 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
132 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
133 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
135 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
136 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
138 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
139 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
141 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
142 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
144 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
145 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
147 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
148 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
150 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
151 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
153 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
154 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
156 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
157 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
159 static char const xor_table[16][16] = {
160 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
161 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
163 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
164 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
166 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
167 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
169 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
170 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
172 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
173 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
175 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
176 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
178 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
179 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
181 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
182 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
184 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
185 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
187 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
188 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
190 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
191 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
193 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
194 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
196 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
197 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
199 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
200 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
202 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
203 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
205 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
206 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
209 static char const add_table[16][16][2] = {
210 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
211 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
212 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
213 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
215 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
216 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
217 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
218 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
220 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
221 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
222 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
223 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
225 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
226 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
227 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
228 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
230 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
231 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
232 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
233 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
235 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
236 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
237 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
238 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
240 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
241 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
242 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
243 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
245 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
246 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
247 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
248 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
250 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
251 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
252 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
253 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
255 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
256 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
257 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
258 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
260 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
261 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
262 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
263 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
265 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
266 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
267 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
268 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
270 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
271 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
272 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
273 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
275 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
276 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
277 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
278 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
280 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
281 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
282 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
283 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
285 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
286 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
287 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
288 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
291 static char const mul_table[16][16][2] = {
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},
295 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
297 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
298 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
299 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
300 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
302 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
303 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
304 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
305 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
307 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
308 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
309 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
310 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
312 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
313 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
314 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
315 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
317 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
318 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
319 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
320 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
322 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
323 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
324 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
325 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
327 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
328 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
329 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
330 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
332 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
333 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
334 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
335 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
337 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
338 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
339 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
340 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
342 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
343 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
344 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
345 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
347 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
348 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
349 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
350 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
352 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
353 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
354 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
355 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
357 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
358 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
359 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
360 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
362 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
363 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
364 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
365 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
367 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
368 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
369 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
370 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
373 static char const shrs_table[16][4][2] = {
374 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
375 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
376 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
377 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
378 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
379 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
380 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
381 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
382 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
383 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
384 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
385 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
386 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
387 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
388 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
389 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
392 /* for converting to binary string */
393 static const char *binary_table[16] = {
394 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
395 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
398 /*****************************************************************************
400 *****************************************************************************/
401 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
402 const char *file, int line)
405 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
406 while (len-- && *str) printf("%c", *str++); printf("\n");
407 while (--pos) printf(" "); printf("^\n");
411 static void _bitnot(const char *val, char *buffer)
415 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
416 buffer[counter] = not_table[_val(val[counter])];
419 static void _bitor(const char *val1, const char *val2, char *buffer)
423 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
424 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
427 static void _bitxor(const char *val1, const char *val2, char *buffer)
431 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
432 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
435 static void _bitand(const char *val1, const char *val2, char *buffer)
439 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
440 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
443 static int _sign(const char *val)
445 return (val[CALC_BUFFER_SIZE-1] <= SC_7) ? (1) : (-1);
449 * returns non-zero if bit at position pos is set
451 static int _bit(const char *val, int pos)
454 int nibble = pos >> 2;
456 return _bitisset(val[nibble], bit);
459 static void _inc(char *val, char *buffer)
463 while (counter++ < CALC_BUFFER_SIZE)
472 /* No carry here, *val != SC_F */
473 *buffer = add_table[_val(*val)][SC_1][0];
477 /* here a carry could be lost, this is intended because this should
478 * happen only when a value changes sign. */
481 static void _negate(const char *val, char *buffer)
483 _bitnot(val, buffer);
484 _inc(buffer, buffer);
487 static void _add(const char *val1, const char *val2, char *buffer)
490 const char *add1, *add2;
493 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++)
495 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
496 add2 = add_table[_val(add1[0])][_val(carry)];
497 /* carry might be zero */
498 buffer[counter] = add2[0];
499 carry = add_table[_val(add1[1])][_val(add2[1])][0];
503 static void _mul(const char *val1, const char *val2, char *buffer)
505 char temp_buffer[CALC_BUFFER_SIZE]; /* result buffer */
506 char neg_val1[CALC_BUFFER_SIZE]; /* abs of val1 */
507 char neg_val2[CALC_BUFFER_SIZE]; /* abs of val2 */
509 const char *mul, *add1, *add2; /* intermediate result containers */
510 char carry = SC_0; /* container for carries */
511 char sign = 0; /* marks result sign */
512 int c_inner, c_outer; /* loop counters */
514 /* init result buffer to zeroes */
515 memset(temp_buffer, SC_0, CALC_BUFFER_SIZE);
517 /* the multiplication works only for positive values, for negative values *
518 * it is necessary to negate them and adjust the result accordingly */
519 if (_sign(val1) == -1) {
520 _negate(val1, neg_val1);
524 if (_sign(val2) == -1) {
525 _negate(val2, neg_val2);
530 for (c_outer = 0; c_outer < MAX_VALUE_SIZE; c_outer++)
532 if (val2[c_outer] != SC_0)
534 for (c_inner = 0; c_inner < MAX_VALUE_SIZE; c_inner++)
536 /* do the following calculation: *
537 * Add the current carry, the value at position c_outer+c_inner *
538 * and the result of the multiplication of val1[c_inner] and *
539 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
541 /* multiplicate the two digits */
542 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
543 /* add old value to result of multiplication */
544 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
545 /* add carry to the sum */
546 add2 = add_table[_val(add1[0])][_val(carry)];
548 /* all carries together result in new carry. This is always smaller *
550 * Both multiplicands, the carry and the value already in the temp *
551 * buffer are single digits and their value is therefore at most *
554 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
555 * The tables list all operations rem b, so the carry is at most *
556 * (b*b-1)rem b = -1rem b = b-1 */
557 carry = add_table[_val(mul[1])][_val(add1[1])][0];
558 carry = add_table[_val(carry)][_val(add2[1])][0];
560 temp_buffer[c_inner + c_outer] = add2[0];
563 /* A carry may hang over */
564 /* c_outer is always smaller than MAX_VALUE_SIZE! */
565 temp_buffer[MAX_VALUE_SIZE + c_outer] = carry;
571 _negate(temp_buffer, buffer);
573 memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
576 static void _sub(const char *val1, const char *val2, char *buffer)
578 char temp_buffer[CALC_BUFFER_SIZE]; /* intermediate buffer to hold -val2 */
580 _negate(val2, temp_buffer);
581 _add(val1, temp_buffer, buffer);
584 static void _push(const char digit, char *buffer)
588 for (counter = CALC_BUFFER_SIZE - 2; counter >= 0; counter--)
590 buffer[counter+1] = buffer[counter];
595 /* XXX: This is MOST slow */
596 static void _divmod(const char *dividend, const char *divisor, char *quot, char *rem)
598 const char *minus_divisor;
599 char neg_val1[CALC_BUFFER_SIZE];
600 char neg_val2[CALC_BUFFER_SIZE];
602 char sign = 0; /* remember result sign */
604 int c_dividend; /* loop counters */
606 /* clear result buffer */
607 memset(quot, SC_0, CALC_BUFFER_SIZE);
608 memset(rem, SC_0, CALC_BUFFER_SIZE);
610 /* if the dividend is zero result is zero (quot is zero)*/
611 if (sc_comp(dividend, quot) == 0)
613 /* if the divisor is zero this won't work (quot is zero) */
614 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
616 if (_sign(dividend) == -1)
618 _negate(dividend, neg_val1);
623 _negate(divisor, neg_val2);
624 if (_sign(divisor) == -1)
627 minus_divisor = divisor;
632 minus_divisor = neg_val2;
635 /* if divisor >= dividend division is easy
636 * (remember these are absolute values) */
637 switch (sc_comp(dividend, divisor))
639 case 0: /* dividend == divisor */
643 case -1: /* dividend < divisor */
644 memcpy(rem, dividend, CALC_BUFFER_SIZE);
647 default: /* unluckily division is necessary :( */
651 for (c_dividend = CALC_BUFFER_SIZE - 1; c_dividend >= 0; c_dividend--)
653 _push(dividend[c_dividend], rem);
656 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
658 /* subtract until the remainder becomes negative, this should
659 * be faster than comparing remainder with divisor */
660 _add(rem, minus_divisor, rem);
662 while (_sign(rem) == 1)
664 quot[0] = add_table[_val(quot[0])][SC_1][0];
665 _add(rem, minus_divisor, rem);
668 /* subtracted one too much */
669 _add(rem, divisor, rem);
673 carry_flag = !sc_is_zero(rem);
682 static void _shl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
691 assert((offset >= 0) || (0 && "negative leftshift"));
692 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
693 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
694 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
696 /* if shifting far enough the result is zero */
697 if (offset >= radius)
699 memset(buffer, SC_0, CALC_BUFFER_SIZE);
703 shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
706 /* shift the single digits some bytes (offset) and some bits (table)
708 for (counter = 0; counter < radius/4 - offset; counter++)
710 shl = mul_table[_val(val1[counter])][_val(shift)];
711 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
716 shl = mul_table[_val(val1[counter])][_val(shift)];
717 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
720 bitoffset = counter - 1;
723 /* fill with zeroes */
724 for (counter = 0; counter < offset; counter++) buffer[counter] = SC_0;
726 /* if the mode was signed, change sign when the mode's msb is now 1 */
727 offset = bitoffset + offset;
728 bitoffset = (radius-1) % 4;
729 if (is_signed && _bitisset(buffer[offset], bitoffset))
731 /* this sets the upper bits of the leftmost digit */
732 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
733 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
735 buffer[counter] = SC_F;
738 else if (is_signed && !_bitisset(buffer[offset], bitoffset))
740 /* this unsets the upper bits of the leftmost digit */
741 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
742 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
744 buffer[counter] = SC_0;
749 static void _shr(const char *val1, char *buffer, long offset, int radius, unsigned is_signed, int signed_shift)
760 assert((offset >= 0) || (0 && "negative rightshift"));
761 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
762 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
763 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
765 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
767 /* if shifting far enough the result is either 0 or -1 */
768 if (offset >= radius)
770 memset(buffer, sign, CALC_BUFFER_SIZE);
777 /* check if any bits are lost, and set carry_flag is so */
778 for (counter = 0; counter < offset; counter++)
780 if (val1[counter] != carry_flag)
786 if ((carry_flag == 0) && (_val(val1[counter]) & shift != 0))
789 /* shift digits to the right with offset, carry and all */
791 if (radius/4 - offset > 0) {
792 buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
795 for (; counter < radius/4 - offset; counter++)
797 shrs = shrs_table[_val(val1[counter + offset])][shift];
798 buffer[counter] = shrs[0];
799 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
802 /* the last digit is special in regard of signed/unsigned shift */
803 bitoffset = radius%4;
804 msd = (radius/4<CALC_BUFFER_SIZE)?(val1[radius/4]):(sign); /* most significant digit */
806 /* remove sign bits if mode was signed and this is an unsigned shift */
807 if (!signed_shift && is_signed) {
808 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
811 shrs = shrs_table[_val(msd)][shift];
813 /* signed shift and signed mode and negative value means all bits to the left are set */
814 if (signed_shift && is_signed && (_sign(val1) == -1)) {
815 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
817 buffer[counter] = shrs[0];
820 if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
822 /* fill with SC_F or SC_0 depending on sign */
823 for (counter++; counter < CALC_BUFFER_SIZE; counter++)
825 buffer[counter] = sign;
829 /* positive: low-order -> high order, negative other direction */
830 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
832 char temp1[CALC_BUFFER_SIZE];
833 char temp2[CALC_BUFFER_SIZE];
838 int counter, old_counter;
842 offset = offset % radius;
844 /* rotation by multiples of the typelength is identity */
846 memmove(buffer, val1, CALC_BUFFER_SIZE);
850 _shl(val1, temp1, offset, radius, is_signed);
851 _shr(val1, temp2, radius - offset, radius, is_signed, 0);
852 _bitor(temp1, temp2, buffer);
853 carry_flag = 0; /* set by shr, but due to rot this is false */
856 /*****************************************************************************
857 * public functions, declared in strcalc.h
858 *****************************************************************************/
859 const void *sc_get_buffer(void)
861 return (void*)calc_buffer;
864 const int sc_get_buffer_length(void)
866 return CALC_BUFFER_SIZE;
869 /* XXX doesn't check for overflows */
870 void sc_val_from_str(const char *str, unsigned int len)
872 const char *orig_str = str;
873 unsigned int orig_len = len;
876 char base[CALC_BUFFER_SIZE];
877 char val[CALC_BUFFER_SIZE];
879 /* verify valid pointers (not null) */
881 /* a string no characters long is an error */
885 memset(base, SC_0, CALC_BUFFER_SIZE);
886 memset(val, SC_0, CALC_BUFFER_SIZE);
888 /* strip leading spaces */
889 while ((len > 0) && (*str == ' ')) { len--; str++; }
891 /* if the first two characters are 0x or 0X -> hex
892 * if the first is a 0 -> oct
893 * else dec, strip leading -/+ and remember sign
895 * only a + or - sign is no number resulting in an error */
900 if (str[1] == 'x' || str[1] == 'X') /* hex */
904 base[1] = SC_1; base[0] = SC_0;
910 base[1] = SC_0; base[0] = SC_8;
918 base[1] = SC_0; base[0] = SC_A;
927 base[1] = SC_0; base[0] = SC_A;
931 default: /* dec, else would have begun with 0x or 0 */
932 base[1] = SC_0; base[0] = SC_A;
935 else /* dec, else would have begun with 0x or 0 */
937 base[1] = SC_0; base[0] = SC_A;
940 /* BEGIN string evaluation, from left to right */
951 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
953 val[0] = _digit((*str)-'a'+10);
955 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
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);
973 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
975 val[0] = _digit((*str)-'0');
977 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
989 val[0] = _digit((*str)-'0');
994 fail_char(orig_str, orig_len, *str, str-orig_str+1);
997 /* Radix conversion from base b to base B:
998 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
999 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1000 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1002 /* get ready for the next letter */
1006 } /* while (len > 0 ) */
1010 _negate(calc_buffer, calc_buffer);
1014 void sc_val_from_long(long value)
1017 char sign, is_minlong;
1021 is_minlong = value == LONG_MIN;
1023 /* use absolute value, special treatment of MIN_LONG */
1031 CLEAR_CALC_BUFFER();
1033 while ((value != 0) && (pos < calc_buffer + CALC_BUFFER_SIZE))
1035 *pos++ = _digit(value % 16);
1041 if (is_minlong) _inc(calc_buffer, calc_buffer);
1042 _negate(calc_buffer, calc_buffer);
1046 long sc_val_to_long(const void *val)
1051 for (i = CALC_BUFFER_SIZE - 1; i >= 0; i--)
1053 l = (l << 4) + _val(((char *)val)[i]);
1058 void sc_min_from_bits(unsigned int num_bits, unsigned int sign)
1063 CLEAR_CALC_BUFFER();
1064 if (!sign) return; /* unsigned means minimum is 0(zero) */
1068 bits = num_bits - 1;
1069 for (i = 0; i < bits/4; i++)
1072 *pos++ = min_digit[bits%4];
1074 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1078 void sc_max_from_bits(unsigned int num_bits, unsigned int sign)
1083 CLEAR_CALC_BUFFER();
1086 bits = num_bits - sign;
1087 for (i = 0; i < bits/4; i++)
1090 *pos++ = max_digit[bits%4];
1092 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1096 void sc_calc(const void* value1, const void* value2, unsigned op)
1098 char unused_res[CALC_BUFFER_SIZE]; /* temp buffer holding unused result of divmod */
1100 const char *val1 = (const char *)value1;
1101 const char *val2 = (const char *)value2;
1102 CLEAR_CALC_BUFFER();
1105 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1110 _negate(val1, calc_buffer);
1111 DEBUGPRINTF_COMPUTATION(("negated: %s\n", sc_print_hex(calc_buffer)));
1114 DEBUGPRINTF_COMPUTATION(("| "));
1115 _bitor(val1, val2, calc_buffer);
1118 DEBUGPRINTF_COMPUTATION(("& "));
1119 _bitand(val1, val2, calc_buffer);
1122 DEBUGPRINTF_COMPUTATION(("^ "));
1123 _bitxor(val1, val2, calc_buffer);
1126 _bitnot(val1, calc_buffer);
1127 DEBUGPRINTF_COMPUTATION(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1130 DEBUGPRINTF_COMPUTATION(("+ "));
1131 _add(val1, val2, calc_buffer);
1134 DEBUGPRINTF_COMPUTATION(("- "));
1135 _sub(val1, val2, calc_buffer);
1138 DEBUGPRINTF_COMPUTATION(("* "));
1139 _mul(val1, val2, calc_buffer);
1142 DEBUGPRINTF_COMPUTATION(("/ "));
1143 _divmod(val1, val2, calc_buffer, unused_res);
1146 DEBUGPRINTF_COMPUTATION(("%% "));
1147 _divmod(val1, val2, unused_res, calc_buffer);
1152 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1153 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1156 void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op)
1158 const char *val1 = (const char *)value1;
1159 const char *val2 = (const char *)value2;
1163 offset = sc_val_to_long(val2);
1165 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1169 DEBUGPRINTF_COMPUTATION(("<< %d ", offset));
1170 _shl(val1, calc_buffer, offset, radius, sign);
1173 DEBUGPRINTF_COMPUTATION((">> %d ", offset));
1174 _shr(val1, calc_buffer, offset, radius, sign, 0);
1177 DEBUGPRINTF_COMPUTATION((">>> %d ", offset));
1178 _shr(val1, calc_buffer, offset, radius, sign, 1);
1181 DEBUGPRINTF_COMPUTATION(("<<>> %d ", offset));
1182 _rot(val1, calc_buffer, offset, radius, sign);
1187 DEBUGPRINTF_COMPUTATION(("-> %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 int sc_get_highest_set_bit(const void *value)
1216 const char *val = (const char*)value;
1220 high = CALC_BUFFER_SIZE * 4;
1222 for (counter = CALC_BUFFER_SIZE-1; counter >= 0; counter--) {
1223 if (val[counter] == SC_0) high -= 4;
1225 if (val[counter] > SC_7) return high;
1226 else if (val[counter] > SC_3) return high - 1;
1227 else if (val[counter] > SC_1) return high - 2;
1228 else return high - 3;
1234 int sc_get_lowest_set_bit(const void *value)
1236 const char *val = (const char*)value;
1240 sign = (_sign(val)==1)?(SC_0):(SC_F);
1243 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++) {
1244 if (val[counter] == SC_0) low += 4;
1246 if (val[counter] < SC_2) return low;
1247 else if (val[counter] < SC_4) return low + 1;
1248 else if (val[counter] < SC_8) return low + 2;
1249 else return low + 3;
1255 int sc_is_zero(const void *value)
1257 const char* val = (const char *)value;
1260 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++) {
1261 if (val[counter] != SC_0) return 0;
1266 int sc_is_negative(const void *value)
1268 return _sign(value) == -1;
1271 int sc_had_carry(void)
1276 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1278 const char *val = (const char *)value;
1279 unsigned nibble_ofs = 2 * byte_ofs;
1282 /* the current scheme uses one byte to store a nibble */
1283 if (nibble_ofs >= len)
1286 res = _val(val[nibble_ofs]);
1287 if (len > nibble_ofs + 1)
1288 res |= _val(val[nibble_ofs + 1]) << 4;
1294 * convert to a string
1296 const char *sc_print(const void *value, unsigned bits, enum base_t base)
1298 static const char big_digits[] = "0123456789ABCDEF";
1299 static const char small_digits[] = "0123456789abcdef";
1301 char base_val[CALC_BUFFER_SIZE];
1302 char div1_res[CALC_BUFFER_SIZE];
1303 char div2_res[CALC_BUFFER_SIZE];
1304 char rem_res[CALC_BUFFER_SIZE];
1305 int counter, nibbles, i, sign;
1308 const char *val = (const char *)value;
1312 const char *digits = small_digits;
1314 pos = output_buffer + BIT_PATTERN_SIZE;
1319 bits = BIT_PATTERN_SIZE;
1320 #ifdef STRCALC_DEBUG_FULLPRINT
1324 nibbles = bits >> 2;
1328 digits = big_digits;
1330 for (counter = 0; counter < nibbles; ++counter) {
1331 *(--pos) = digits[_val(val[counter])];
1332 #ifdef STRCALC_DEBUG_GROUPPRINT
1333 if ((counter+1)%8 == 0)
1338 /* last nibble must be masked */
1340 x = and_table[_val(val[++counter])][bits & 3];
1341 *(--pos) = digits[_val(x)];
1344 /* now kill zeros */
1345 for (; counter > 1; --counter, ++pos) {
1346 #ifdef STRCALC_DEBUG_GROUPPRINT
1347 if (pos[0] == ' ') ++pos;
1355 for (counter = 0; counter < nibbles; ++counter) {
1357 p = binary_table[_val(val[counter])];
1364 /* last nibble must be masked */
1366 x = and_table[_val(val[++counter])][bits & 3];
1369 p = binary_table[_val(x)];
1376 /* now kill zeros */
1377 for (counter <<= 2; counter > 1; --counter, ++pos)
1384 memset(base_val, SC_0, CALC_BUFFER_SIZE);
1385 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1389 if (base == SC_DEC) {
1390 /* check for negative values */
1391 if (_bit(val, bits - 1)) {
1392 _negate(val, div2_res);
1398 /* transfer data into oscilating buffers */
1399 memset(div1_res, SC_0, CALC_BUFFER_SIZE);
1400 for (counter = 0; counter < nibbles; ++counter)
1401 div1_res[counter] = p[counter];
1403 /* last nibble must be masked */
1407 div1_res[counter] = and_table[_val(p[counter])][bits & 3];
1413 _divmod(m, base_val, n, rem_res);
1417 *(--pos) = digits[_val(rem_res[0])];
1420 for (i = 0; i < sizeof(div1_res); ++i)
1437 void init_strcalc(int precision_in_bytes)
1439 if (calc_buffer == NULL) {
1440 if (precision_in_bytes <= 0) precision_in_bytes = DEFAULT_PRECISION_IN_BYTES;
1442 BIT_PATTERN_SIZE = (8 * precision_in_bytes);
1443 CALC_BUFFER_SIZE = (4 * precision_in_bytes);
1444 MAX_VALUE_SIZE = (2 * precision_in_bytes);
1446 calc_buffer = malloc(CALC_BUFFER_SIZE * sizeof(char));
1447 output_buffer = malloc(BIT_PATTERN_SIZE * sizeof(char));
1449 if (calc_buffer == NULL || output_buffer == NULL)
1451 assert(0 && "malloc failed");
1455 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));
1460 return CALC_BUFFER_SIZE/4;