1 /****i* strcalc/implementation
9 #include <assert.h> /* assertions */
10 #include <string.h> /* memset/memcmp */
14 #include <stdio.h> /* output for error messages */
17 /*****************************************************************************
18 * local definitions and macros
19 *****************************************************************************/
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 # define DEBUGPRINTF(...) printf(__VA_ARGS__)
34 # define DEBUGPRINTF(...) ((void)0)
37 /*****************************************************************************
39 *****************************************************************************/
41 static char calc_buffer[CALC_BUFFER_SIZE]; /* buffer holding all results */
43 static char quot[CALC_BUFFER_SIZE]; /* temp buffer holding result of divmod */
44 static char rem[CALC_BUFFER_SIZE]; /* temp buffer holding remainder of divmod */
46 static char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
47 static char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
49 static char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
50 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
52 static char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
54 static char and_table[16][16] = {
55 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
56 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
58 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
59 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
61 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
62 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
64 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
65 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
67 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
68 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
70 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
71 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
73 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
74 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
76 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
77 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
79 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
80 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
82 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
83 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
85 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
86 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
88 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
89 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
91 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
92 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
94 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
95 SC_8, SC_9, SC_8, SC_9, SC_D, SC_E, SC_D, SC_E },
97 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
98 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
100 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
101 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
103 static char or_table[16][16] = {
104 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
105 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
107 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
108 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
110 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
111 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
113 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
114 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
116 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
117 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
119 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
120 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
122 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
123 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
125 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
126 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
128 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
129 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
131 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
132 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
134 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
135 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
137 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
138 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
140 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
141 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
143 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
144 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
146 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
147 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
149 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
150 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
152 static char xor_table[16][16] = {
153 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
154 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
156 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
157 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
159 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
160 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
162 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
163 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
165 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
166 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
168 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
169 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
171 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
172 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
174 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
175 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
177 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
178 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
180 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
181 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
183 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
184 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
186 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
187 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
189 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
190 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
192 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
193 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
195 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
196 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
198 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
199 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
202 static char add_table[16][16][2] = {
203 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
204 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
205 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
206 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
208 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
209 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
210 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
211 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
213 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
214 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
215 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
216 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
218 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
219 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
220 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
221 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
223 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
224 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
225 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
226 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
228 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
229 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
230 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
231 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
233 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
234 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
235 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
236 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
238 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
239 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
240 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
241 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
243 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
244 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
245 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
246 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
248 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
249 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
250 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
251 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
253 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
254 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
255 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
256 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
258 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
259 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
260 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
261 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
263 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
264 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
265 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
266 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
268 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
269 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
270 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
271 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
273 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
274 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
275 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
276 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
278 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
279 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
280 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
281 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
284 static char mul_table[16][16][2] = {
285 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
286 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
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} },
290 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
291 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
292 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
293 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
295 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
296 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
297 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
298 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
300 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
301 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
302 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
303 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
305 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
306 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
307 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
308 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
310 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
311 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
312 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
313 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
315 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
316 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
317 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
318 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
320 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
321 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
322 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
323 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
325 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
326 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
327 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
328 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
330 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
331 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
332 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
333 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
335 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
336 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
337 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
338 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
340 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
341 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
342 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
343 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
345 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
346 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
347 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
348 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
350 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
351 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
352 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
353 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
355 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
356 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
357 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
358 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
360 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
361 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
362 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
363 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
366 static char shrs_table[16][4][2] = {
367 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
368 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
369 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
370 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
371 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
372 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
373 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
374 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
375 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
376 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
377 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
378 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
379 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
380 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
381 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
382 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
384 /*****************************************************************************
386 *****************************************************************************/
387 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
388 const char *file, int line)
391 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
392 while (len-- && *str) printf("%c", *str++); printf("\n");
393 while (--pos) printf(" "); printf("^\n");
397 static void _bitnot(const char *val, char *buffer)
401 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
402 buffer[counter] = not_table[_val(val[counter])];
405 static void _bitor(const char *val1, const char *val2, char *buffer)
409 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
410 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
413 static void _bitxor(const char *val1, const char *val2, char *buffer)
417 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
418 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
421 static void _bitand(const char *val1, const char *val2, char *buffer)
425 for (counter = 0; counter<CALC_BUFFER_SIZE; counter++)
426 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
429 static int _sign(const char *val)
431 return (val[CALC_BUFFER_SIZE-1] < SC_7) ? (1) : (-1);
434 static void _inc(char *val, char *buffer)
438 while (counter++ < CALC_BUFFER_SIZE)
447 /* No carry here, *val != SC_F */
448 *buffer = add_table[_val(*val)][SC_1][0];
452 /* here a carry could be lost, this is intended because this will only
453 * happen when a value changes sign. */
456 static void _negate(const char *val, char *buffer)
458 _bitnot(val, buffer);
459 _inc(buffer, buffer);
462 static void _add(const char *val1, const char *val2, char *buffer)
468 for (counter = 0; counter < CALC_BUFFER_SIZE; counter++)
470 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
471 add2 = add_table[_val(add1[0])][_val(carry)];
472 /* carry might be zero */
473 buffer[counter] = add2[0];
474 carry = add_table[_val(add1[1])][_val(add2[1])][0];
476 /* loose last carry, which will occur only when changing sign */
479 static void _mul(const char *val1, const char *val2, char *buffer)
481 static char temp_buffer[CALC_BUFFER_SIZE]; /* result buffer */
482 static char neg_val1[CALC_BUFFER_SIZE]; /* abs of val1 */
483 static char neg_val2[CALC_BUFFER_SIZE]; /* abs of val2 */
485 char *mul, *add1, *add2; /* intermediate result containers */
486 char carry = SC_0; /* container for carries */
487 char sign = 0; /* marks result sign */
488 int c_inner, c_outer; /* loop counters */
490 /* init result buffer to zeroes */
491 memset(temp_buffer, SC_0, CALC_BUFFER_SIZE);
493 /* the multiplication works only for positive values, for negative values *
494 * it is necessary to negate them and adjust the result accordingly */
495 if (_sign(val1) == -1) {
496 _negate(val1, neg_val1);
500 if (_sign(val2) == -1) {
501 _negate(val2, neg_val2);
506 for (c_outer = 0; c_outer < MAX_VALUE_SIZE; c_outer++)
508 if (val2[c_outer] != SC_0)
510 for (c_inner = 0; c_inner < MAX_VALUE_SIZE; c_inner++)
512 /* do the following calculation: *
513 * Add the current carry, the value at position c_outer+c_inner *
514 * and the result of the multiplication of val1[c_inner] and *
515 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
517 /* multiplicate the two digits */
518 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
519 /* add old value to result of multiplication */
520 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
521 /* add carry to the sum */
522 add2 = add_table[_val(add1[0])][_val(carry)];
524 /* all carries together result in new carry. This is always smaller *
526 * Both multiplicands, the carry and the value already in the temp *
527 * buffer are single digits and their value is therefore at most *
530 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
531 * The tables list all operations rem b, so the carry is at most *
532 * (b*b-1)rem b = -1rem b = b-1 */
533 carry = add_table[_val(mul[1])][_val(add1[1])][0];
534 carry = add_table[_val(carry)][_val(add2[1])][0];
536 temp_buffer[c_inner + c_outer] = add2[0];
539 /* A carry may hang over */
540 /* c_outer is always smaller than MAX_VALUE_SIZE! */
541 temp_buffer[MAX_VALUE_SIZE + c_outer] = carry;
545 if (sign) _negate(temp_buffer, temp_buffer);
547 memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
550 static void _sub(const char *val1, const char *val2, char *buffer)
552 char temp_buffer[CALC_BUFFER_SIZE]; /* intermediate buffer to hold -val2 */
554 _negate(val2, temp_buffer);
555 _add(val1, temp_buffer, buffer);
558 static void _push(const char digit, char *buffer)
562 for (counter = CALC_BUFFER_SIZE - 2; counter >= 0; counter--)
564 buffer[counter+1] = buffer[counter];
569 /* XXX: This is MOST slow */
570 static void _divmod(const char *dividend, const char *divisor)
572 const char *minus_divisor;
573 static char neg_val1[CALC_BUFFER_SIZE];
574 static char neg_val2[CALC_BUFFER_SIZE];
576 char sign = 0; /* remember result sign */
578 int c_dividend; /* loop counters */
580 /* clear result buffer */
581 memset(quot, SC_0, CALC_BUFFER_SIZE);
582 memset(rem, SC_0, CALC_BUFFER_SIZE);
583 /* if the dividend is zero result is zero (quot is zero)*/
584 if (sc_comp(dividend, quot) == 0) return;
585 /* if the divisor is zero this won't work (quot is zero) */
586 if (sc_comp(divisor, quot) == 0) assert(0 && "quotision by zero!");
588 if (_sign(dividend) == -1)
590 _negate(dividend, neg_val1);
595 _negate(divisor, neg_val2);
596 if (_sign(divisor) == -1)
599 minus_divisor = divisor;
604 minus_divisor = neg_val2;
607 /* if divisor >= dividend quotision is easy
608 * (remember these are absolute values) */
609 switch (sc_comp(dividend, divisor))
611 case 0: /* dividend == divisor */
615 case -1: /* dividend < divisor */
616 memcpy(rem, dividend, CALC_BUFFER_SIZE);
619 default: /* unluckily quotision is necessary :( */
623 for (c_dividend = MAX_VALUE_SIZE - 1; c_dividend >= 0; c_dividend--)
625 _push(dividend[c_dividend], rem);
628 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
630 /* subtract until the remainder becomes negative, this should
631 * be faster than comparing remainder with divisor */
632 _add(rem, minus_divisor, rem);
634 while (_sign(rem) == 1)
636 quot[0] = add_table[_val(quot[0])][SC_1][0];
637 _add(rem, minus_divisor, rem);
640 /* subtracted one too much */
641 _add(rem, divisor, rem);
652 static void _shl(const char *val1, const char *val2, char *buffer, unsigned radius, unsigned is_signed)
662 assert((_sign(val2) != -1) || (0 && "negative leftshift"));
663 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
664 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
665 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
667 /* the whole value must be moved left the number of bytes represented
668 * by the value in quot, with bytes to the right set to zero */
669 /*XXX This might result in trouble */
670 for (counter = MAX_VALUE_SIZE - 1; counter >= 0; counter--)
672 offset = (offset << 4) | (_val(val2[counter]));
675 shift = shift_table[_val(offset%4)]; /* this is 2 ** (val2 % 4) */
677 /* if shifting far enough the result is zero */
678 if (offset >= radius)
680 memset(buffer, SC_0, CALC_BUFFER_SIZE);
685 /* shift the single digits some bytes (offset) and some bits (table)
687 for (counter = 0; counter < CALC_BUFFER_SIZE - offset; counter++)
689 shl = mul_table[_val(val1[counter])][_val(shift)];
690 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
694 /* fill with zeroes */
695 for (counter = 0; counter < offset; counter++) buffer[counter] = 0;
696 /* if the mode was signed, change sign when the mode's msb is now 1 */
697 offset = (radius - 1) / 4;
698 bitoffset = (radius - 1) % 4;
699 if (is_signed && _bitisset(buffer[offset], bitoffset) && (_sign(buffer) == 1))
701 /* this sets the upper bits of the leftmost digit */
702 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
703 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
705 buffer[counter] = SC_F;
708 else if (is_signed && !_bitisset(buffer[offset], bitoffset) && (_sign(buffer) == -1))
710 /* this unsets the upper bits of the leftmost digit */
711 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
712 /* zero the upper part of the value */
713 for (counter = offset+1; counter < CALC_BUFFER_SIZE; counter++)
715 buffer[counter] = SC_0;
720 static void _shr(const char *val1, const char *val2, char *buffer, unsigned radius, unsigned is_signed, int signed_shift)
732 assert((_sign(val2) != -1) || (0 && "negative rightshift"));
733 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
734 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
735 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
737 /*XXX get the value of val2, this might result in trouble *
738 * (but who wants shifts THAT far anyway) */
739 for (counter = MAX_VALUE_SIZE - 1; counter >= 0; counter--)
741 offset = (offset << 4) | (_val(val2[counter]));
744 shift = offset % 4; /* this is val2 % 4 */
746 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
747 /* if shifting far enough the result is either 0 or -1 */
748 if (offset >= radius)
750 memset(buffer, sign, CALC_BUFFER_SIZE);
755 buffer[0] = shrs_table[_val(val1[offset])][shift][0];
756 /* shift digits to the right with offset, carry and all */
757 for (counter = 1; counter < radius/4; counter++)
759 shrs = shrs_table[_val(val1[counter + offset])][shift];
760 buffer[counter] = shrs[0];
761 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
764 /* the last digit is special in regard of signed/unsigned shift */
765 /* counter = radius/4 (after for loop) */
766 bitoffset = radius%4;
767 msd = val1[counter]; /* most significant digit */
769 /* remove sign bits if mode was signed and this is an unsigned shift */
770 if (!signed_shift && is_signed) {
771 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
774 shrs = shrs_table[_val(msd)][shift];
776 /* signed shift and signed mode and negative value means all bits to the left are set */
777 if (signed_shift && is_signed && (_sign(val1) == -1)) {
778 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
780 buffer[counter] = shrs[0];
782 buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
784 /* fill with SC_F or SC_0 depending on sign */
785 for (counter++; counter < CALC_BUFFER_SIZE; counter++)
787 buffer[counter] = sign;
791 /* positive: low-order -> high order, negative other direction */
792 static void _rot(const char *val1, const char *val2, char *buffer, unsigned radius, unsigned is_signed)
794 static char temp_buffer[CALC_BUFFER_SIZE];
799 int counter, old_counter;
804 /*XXX get the value of val2, this might result in trouble *
805 * (but who wants shifts THAT far anyway) */
806 for (counter = MAX_VALUE_SIZE - 1; counter >= 0; counter--)
808 offset = (offset << 4) | (_val(val2[counter]));
810 /* rotation by multiples of the typelength is identity */
811 offset = offset % radius;
813 memmove(buffer, val1, CALC_BUFFER_SIZE);
816 /* rotation to the right is the same as rotation to the left
817 * when done by the right amount */
818 if (offset < 0) offset = radius + offset;
820 shift = _val(shift_table[offset % 4]);
823 DEBUGPRINTF("offset: %d, shift: %d\n", offset, shift);
824 for (counter = 0; counter < radius/4 - offset; counter++)
826 shl = mul_table[_val(val1[counter])][_val(shift)];
827 temp_buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
829 DEBUGPRINTF("%d(%x): %s\n", counter, shl[0], sc_print_hex(temp_buffer));
831 old_counter = counter;
832 for (; counter < radius/4; counter++)
834 shl = mul_table[_val(val1[counter])][_val(shift)];
835 temp_buffer[counter - old_counter] = or_table[_val(shl[0])][_val(carry)];
837 DEBUGPRINTF("%d(%x)> %s\n", counter, shl[0], sc_print_hex(temp_buffer));
839 temp_buffer[counter - old_counter] = or_table[_val(temp_buffer[counter-old_counter])][_val(carry)];
841 offset = (radius-1)/4;
842 bitoffset - (radius-1)%4;
843 /* fill the rest of the buffer depending on msb and mode signedness*/
844 if (is_signed && _bitisset(temp_buffer[offset], bitoffset))
853 memcpy(buffer, temp_buffer, CALC_BUFFER_SIZE);
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 void sc_val_from_str(const char *str, unsigned int len)
871 const char *orig_str = str;
872 unsigned int orig_len = len;
875 static char base[CALC_BUFFER_SIZE];
876 static char val[CALC_BUFFER_SIZE];
878 /* verify valid pointers (not null) */
880 /* a string no characters long is an error */
884 memset(base, SC_0, CALC_BUFFER_SIZE);
885 memset(val, SC_0, CALC_BUFFER_SIZE);
887 /* strip leading spaces */
888 while ((len > 0) && (*str == ' ')) { len--; str++; }
890 /* if the first two characters are 0x or 0X -> hex
891 * if the first is a 0 -> oct
892 * else dec, strip leading -/+ and remember sign
894 * only a + or - sign is no number resulting in an error */
899 if (str[1] == 'x' || str[1] == 'X') /* hex */
903 base[1] = SC_1; base[0] = SC_0;
909 base[1] = SC_0; base[0] = SC_8;
917 base[1] = SC_0; base[0] = SC_A;
926 base[1] = SC_0; base[0] = SC_A;
930 default: /* dec, else would have begun with 0x or 0 */
931 base[1] = SC_0; base[0] = SC_A;
934 else /* dec, else would have begun with 0x or 0 */
936 base[1] = SC_0; base[0] = SC_A;
939 /* begin string evaluation, from left to right */
950 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
952 val[0] = _digit((*str)-'a'+10);
954 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
963 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
965 val[0] = _digit((*str)-'A'+10);
967 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
972 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
974 val[0] = _digit((*str)-'0');
976 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
988 val[0] = _digit((*str)-'0');
993 fail_char(orig_str, orig_len, *str, str-orig_str+1);
996 /* Radix conversion from base b to base B:
997 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
998 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
999 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1001 /* get ready for the next letter */
1005 } /* while (len > 0 ) */
1009 _negate(calc_buffer, calc_buffer);
1013 void sc_val_from_long(long value)
1021 /* FIXME MININT won't work */
1022 if (sign) value = -value;
1024 CLEAR_CALC_BUFFER();
1026 while ((value != 0) && (pos < calc_buffer + CALC_BUFFER_SIZE))
1028 *pos++ = _digit(value % 16);
1032 if (sign) _negate(calc_buffer, calc_buffer);
1035 long sc_val_to_long(const void *val)
1040 for (i = CALC_BUFFER_SIZE - 1; i >= 0; i--)
1042 l = (l << 4) + _val(((char *)val)[i]);
1047 void sc_min_from_bits(unsigned int num_bits, unsigned int sign)
1052 CLEAR_CALC_BUFFER();
1053 if (!sign) return; /* unsigned means minimum is 0(zero) */
1057 bits = num_bits - 1;
1058 for (i = 0; i < bits/4; i++)
1061 *pos++ = min_digit[bits%4];
1063 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1067 void sc_max_from_bits(unsigned int num_bits, unsigned int sign)
1072 CLEAR_CALC_BUFFER();
1075 bits = num_bits - sign;
1076 for (i = 0; i < bits/4; i++)
1079 *pos++ = max_digit[bits%4];
1081 for (i++; i <= CALC_BUFFER_SIZE - 1; i++)
1085 void sc_calc(const void* value1, const void* value2, unsigned op)
1087 const char *val1 = (char *)value1;
1088 const char *val2 = (char *)value2;
1089 CLEAR_CALC_BUFFER();
1091 DEBUGPRINTF("%s ", sc_print(value1, SC_HEX));
1096 _negate(val1, calc_buffer);
1097 DEBUGPRINTF("negated: %s\n", sc_print(calc_buffer, SC_HEX));
1101 _bitor(val1, val2, calc_buffer);
1105 _bitand(val1, val2, calc_buffer);
1109 _bitxor(val1, val2, calc_buffer);
1112 _bitnot(val1, calc_buffer);
1113 DEBUGPRINTF("bit-negated: %s\n", sc_print(calc_buffer, SC_HEX));
1117 _add(val1, val2, calc_buffer);
1121 _sub(val1, val2, calc_buffer);
1125 _mul(val1, val2, calc_buffer);
1129 _divmod(val1, val2);
1130 memcpy(calc_buffer, quot, CALC_BUFFER_SIZE);
1134 _divmod(val1, val2);
1135 memcpy(calc_buffer, rem, CALC_BUFFER_SIZE);
1140 DEBUGPRINTF("%s -> ", sc_print(value2, SC_HEX));
1141 DEBUGPRINTF("%s\n", sc_print(calc_buffer, SC_HEX));
1143 void sc_bitcalc(const void* value1, const void* value2, unsigned radius, unsigned sign, unsigned op)
1145 const char *val1 = (char *)value1;
1146 const char *val2 = (char *)value2;
1147 CLEAR_CALC_BUFFER();
1149 DEBUGPRINTF("%s ", sc_print(value1, SC_HEX));
1154 _shl(val1, val2, calc_buffer, radius, sign);
1158 _shr(val1, val2, calc_buffer, radius, sign, 0);
1161 DEBUGPRINTF(">>> ");
1162 _shr(val1, val2, calc_buffer, radius, sign, 1);
1165 DEBUGPRINTF("<<>> ");
1166 _rot(val1, val2, calc_buffer, radius, sign);
1171 DEBUGPRINTF("%s -> ", sc_print(value2, SC_HEX));
1172 DEBUGPRINTF("%s\n", sc_print(calc_buffer, SC_HEX));
1175 int sc_comp(const void* value1, const void* value2)
1177 int counter = CALC_BUFFER_SIZE - 1;
1178 const char *val1 = (char *)value1;
1179 const char *val2 = (char *)value2;
1181 /* compare signs first:
1182 * the loop below can only compare values of the same sign! */
1183 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1185 /* loop until two digits differ, the values are equal if there
1186 * are no such two digits */
1187 while (val1[counter] == val2[counter])
1190 if (counter < 0) return 0;
1193 /* the leftmost digit is the most significant, so this returns
1194 * the correct result.
1195 * This implies the digit enum is ordered */
1196 return (val1[counter] > val2[counter]) ? (1) : (-1);
1199 char *sc_print(const void *value, unsigned base)
1203 const char *val = (char*)value;
1205 static char *buf = NULL;
1207 if (buf != NULL) free(buf);
1208 buf = malloc(BIT_PATTERN_SIZE);
1210 pos = buf + BIT_PATTERN_SIZE - 1;
1216 for (counter = 0; counter < MAX_VALUE_SIZE; counter++)
1218 if (val[counter] < SC_A)
1219 *(--pos) = val[counter] + '0';
1221 *(--pos) = val[counter] + 'a' - 10;