2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Provides basic mathematical operations on values represented as strings.
24 * @author Mathias Heil
31 #include <string.h> /* memset/memcmp */
32 #include <assert.h> /* assertions */
33 #include <stdio.h> /* output for error messages */
34 #include <limits.h> /* definition of LONG_MIN, used in sc_get_val_from_long */
41 * local definitions and macros
43 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, calc_buffer_size)
44 #define _val(a) ((a)-SC_0)
45 #define _digit(a) ((a)+SC_0)
46 #define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
48 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__, __LINE__)
50 /* shortcut output for debugging */
51 # define sc_print_hex(a) sc_print((a), 0, SC_HEX, 0)
52 # define sc_print_dec(a) sc_print((a), 0, SC_DEC, 1)
53 # define sc_print_oct(a) sc_print((a), 0, SC_OCT, 0)
54 # define sc_print_bin(a) sc_print((a), 0, SC_BIN, 0)
56 #ifdef STRCALC_DEBUG_PRINTCOMP
57 # define DEBUGPRINTF_COMPUTATION(x) printf x
59 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
62 # define DEBUGPRINTF(x) printf x
64 # define DEBUGPRINTF(x) ((void)0)
71 static char *calc_buffer = NULL; /* buffer holding all results */
72 static char *output_buffer = NULL; /* buffer for output */
73 static int bit_pattern_size; /* maximum number of bits */
74 static int calc_buffer_size; /* size of internally stored values */
75 static int max_value_size; /* maximum size of values */
77 static int carry_flag; /**< some computation set the carry_flag:
78 - right shift if bits were lost due to shifting
79 - division if there was a remainder
80 However, the meaning of carry is machine dependent
81 and often defined in other ways! */
83 static const char sex_digit[4] = { SC_E, SC_C, SC_8, SC_0 };
84 static const char zex_digit[4] = { SC_1, SC_3, SC_7, SC_F };
85 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
86 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
88 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
89 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
91 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
93 static const char and_table[16][16] = {
94 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
95 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
97 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
98 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
100 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
101 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
103 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
104 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
106 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
107 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
109 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
110 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
112 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
113 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
115 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
116 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
118 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
119 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
121 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
122 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
124 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
125 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
127 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
128 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
130 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
131 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
133 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
134 SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
136 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
137 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
139 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
140 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
142 static const char or_table[16][16] = {
143 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
144 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
146 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
147 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
149 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
150 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
152 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
153 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
155 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
156 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
158 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
159 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
161 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
162 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
164 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
165 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
167 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
168 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
170 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
171 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
173 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
174 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
176 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
177 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
179 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
180 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
182 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
183 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
185 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
186 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
188 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
189 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
191 static char const xor_table[16][16] = {
192 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
193 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
195 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
196 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
198 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
199 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
201 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
202 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
204 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
205 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
207 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
208 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
210 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
211 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
213 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
214 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
216 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
217 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
219 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
220 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
222 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
223 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
225 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
226 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
228 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
229 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
231 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
232 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
234 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
235 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
237 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
238 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
241 static char const add_table[16][16][2] = {
242 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
243 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
244 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
245 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
247 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
248 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
249 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
250 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
252 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
253 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
254 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
255 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
257 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
258 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
259 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
260 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
262 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
263 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
264 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
265 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
267 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
268 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
269 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
270 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
272 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
273 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
274 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
275 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
277 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
278 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
279 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
280 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
282 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
283 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
284 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
285 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
287 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
288 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
289 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
290 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
292 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
293 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
294 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
295 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
297 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
298 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
299 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
300 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
302 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
303 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
304 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
305 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
307 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
308 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
309 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
310 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
312 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
313 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
314 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
315 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
317 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
318 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
319 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
320 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
323 static char const mul_table[16][16][2] = {
324 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
325 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
326 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
327 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
329 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
330 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
331 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
332 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
334 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
335 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
336 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
337 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
339 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
340 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
341 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
342 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
344 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
345 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
346 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
347 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
349 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
350 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
351 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
352 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
354 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
355 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
356 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
357 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
359 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
360 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
361 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
362 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
364 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
365 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
366 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
367 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
369 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
370 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
371 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
372 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
374 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
375 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
376 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
377 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
379 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
380 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
381 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
382 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
384 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
385 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
386 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
387 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
389 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
390 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
391 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
392 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
394 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
395 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
396 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
397 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
399 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
400 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
401 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
402 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
405 static char const shrs_table[16][4][2] = {
406 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
407 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
408 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
409 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
410 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
411 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
412 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
413 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
414 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
415 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
416 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
417 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
418 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
419 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
420 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
421 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
424 /** converting a digit to a binary string */
425 static const char *binary_table[16] = {
426 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
427 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
430 /*****************************************************************************
432 *****************************************************************************/
433 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
434 const char *file, int line) {
436 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
437 while (len-- && *str) printf("%c", *str++); printf("\n");
438 while (--pos) printf(" "); printf("^\n");
443 * implements the bitwise NOT operation
445 static void do_bitnot(const char *val, char *buffer) {
448 for (counter = 0; counter<calc_buffer_size; counter++)
449 buffer[counter] = not_table[_val(val[counter])];
453 * implements the bitwise OR operation
455 static void do_bitor(const char *val1, const char *val2, char *buffer) {
458 for (counter = 0; counter<calc_buffer_size; counter++)
459 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
463 * implements the bitwise eXclusive OR operation
465 static void do_bitxor(const char *val1, const char *val2, char *buffer) {
468 for (counter = 0; counter<calc_buffer_size; counter++)
469 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
473 * implements the bitwise AND operation
475 static void do_bitand(const char *val1, const char *val2, char *buffer) {
478 for (counter = 0; counter<calc_buffer_size; counter++)
479 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
483 * returns the sign bit.
485 * @todo This implementation is wrong, as it returns the highest bit of the buffer
486 * NOT the highest bit depending on the real mode
488 static int do_sign(const char *val) {
489 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
493 * returns non-zero if bit at position pos is set
495 static int do_bit(const char *val, int pos) {
497 int nibble = pos >> 2;
499 return _bitisset(val[nibble], bit);
503 * Implements a fast ADD + 1
505 static void do_inc(const char *val, char *buffer) {
508 while (counter++ < calc_buffer_size) {
513 /* No carry here, *val != SC_F */
514 *buffer = add_table[_val(*val)][SC_1][0];
518 /* here a carry could be lost, this is intended because this should
519 * happen only when a value changes sign. */
523 * Implements a unary MINUS
525 static void do_negate(const char *val, char *buffer) {
526 do_bitnot(val, buffer);
527 do_inc(buffer, buffer);
531 * Implements a binary ADD
533 * @todo The implementation of carry is wrong, as it is the
534 * calc_buffer_size carry, not the mode depending
536 static void do_add(const char *val1, const char *val2, char *buffer) {
538 const char *add1, *add2;
541 for (counter = 0; counter < calc_buffer_size; counter++) {
542 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
543 add2 = add_table[_val(add1[0])][_val(carry)];
544 /* carry might be zero */
545 buffer[counter] = add2[0];
546 carry = add_table[_val(add1[1])][_val(add2[1])][0];
548 carry_flag = carry != SC_0;
552 * Implements a binary SUB
554 static void do_sub(const char *val1, const char *val2, char *buffer) {
555 char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
557 do_negate(val2, temp_buffer);
558 do_add(val1, temp_buffer, buffer);
562 * Implements a binary MUL
564 static void do_mul(const char *val1, const char *val2, char *buffer) {
565 char *temp_buffer; /* result buffer */
566 char *neg_val1; /* abs of val1 */
567 char *neg_val2; /* abs of val2 */
569 const char *mul, *add1, *add2; /* intermediate result containers */
570 char carry = SC_0; /* container for carries */
571 char sign = 0; /* marks result sign */
572 int c_inner, c_outer; /* loop counters */
574 temp_buffer = alloca(calc_buffer_size);
575 neg_val1 = alloca(calc_buffer_size);
576 neg_val2 = alloca(calc_buffer_size);
578 /* init result buffer to zeros */
579 memset(temp_buffer, SC_0, calc_buffer_size);
581 /* the multiplication works only for positive values, for negative values *
582 * it is necessary to negate them and adjust the result accordingly */
583 if (do_sign(val1) == -1) {
584 do_negate(val1, neg_val1);
588 if (do_sign(val2) == -1) {
589 do_negate(val2, neg_val2);
594 for (c_outer = 0; c_outer < max_value_size; c_outer++) {
595 if (val2[c_outer] != SC_0) {
596 for (c_inner = 0; c_inner < max_value_size; c_inner++) {
597 /* do the following calculation: *
598 * Add the current carry, the value at position c_outer+c_inner *
599 * and the result of the multiplication of val1[c_inner] and *
600 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
602 /* multiplicate the two digits */
603 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
604 /* add old value to result of multiplication */
605 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
606 /* add carry to the sum */
607 add2 = add_table[_val(add1[0])][_val(carry)];
609 /* all carries together result in new carry. This is always smaller *
611 * Both multiplicands, the carry and the value already in the temp *
612 * buffer are single digits and their value is therefore at most *
615 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
616 * The tables list all operations rem b, so the carry is at most *
617 * (b*b-1)rem b = -1rem b = b-1 */
618 carry = add_table[_val(mul[1])][_val(add1[1])][0];
619 carry = add_table[_val(carry)][_val(add2[1])][0];
621 temp_buffer[c_inner + c_outer] = add2[0];
624 /* A carry may hang over */
625 /* c_outer is always smaller than max_value_size! */
626 temp_buffer[max_value_size + c_outer] = carry;
632 do_negate(temp_buffer, buffer);
634 memcpy(buffer, temp_buffer, calc_buffer_size);
638 * Shift the buffer to left and add a 4 bit digit
640 static void do_push(const char digit, char *buffer) {
643 for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
644 buffer[counter+1] = buffer[counter];
650 * Implements truncating integer division and remainder.
652 * Note: This is MOST slow
654 static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem) {
655 const char *dividend = rDividend;
656 const char *minus_divisor;
660 char div_sign = 0; /* remember division result sign */
661 char rem_sign = 0; /* remember remainder result sign */
663 int c_dividend; /* loop counters */
665 neg_val1 = alloca(calc_buffer_size);
666 neg_val2 = alloca(calc_buffer_size);
668 /* clear result buffer */
669 memset(quot, SC_0, calc_buffer_size);
670 memset(rem, SC_0, calc_buffer_size);
672 /* if the divisor is zero this won't work (quot is zero) */
673 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
675 /* if the dividend is zero result is zero (quot is zero) */
676 if (sc_comp(dividend, quot) == 0)
679 if (do_sign(dividend) == -1) {
680 do_negate(dividend, neg_val1);
686 do_negate(divisor, neg_val2);
687 if (do_sign(divisor) == -1) {
689 minus_divisor = divisor;
692 minus_divisor = neg_val2;
694 /* if divisor >= dividend division is easy
695 * (remember these are absolute values) */
696 switch (sc_comp(dividend, divisor)) {
697 case 0: /* dividend == divisor */
701 case -1: /* dividend < divisor */
702 memcpy(rem, rDividend, calc_buffer_size);
705 default: /* unluckily division is necessary :( */
709 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
710 do_push(dividend[c_dividend], rem);
713 if (sc_comp(rem, divisor) != -1) { /* remainder >= divisor */
714 /* subtract until the remainder becomes negative, this should
715 * be faster than comparing remainder with divisor */
716 do_add(rem, minus_divisor, rem);
718 while (do_sign(rem) == 1) {
719 quot[0] = add_table[_val(quot[0])][SC_1][0];
720 do_add(rem, minus_divisor, rem);
723 /* subtracted one too much */
724 do_add(rem, divisor, rem);
728 /* sets carry if remainder is non-zero ??? */
729 carry_flag = !sc_is_zero(rem);
732 do_negate(quot, quot);
739 * Implements a Shift Left, which can either preserve the sign bit
742 * @todo Assertions seems to be wrong
744 static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed) {
752 assert((shift_cnt >= 0) || (0 && "negative leftshift"));
753 assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
754 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
755 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
757 /* if shifting far enough the result is zero */
758 if (shift_cnt >= bitsize) {
759 memset(buffer, SC_0, calc_buffer_size);
763 shift = shift_table[_val(shift_cnt%4)]; /* this is 2 ** (offset % 4) */
764 shift_cnt = shift_cnt / 4;
766 /* shift the single digits some bytes (offset) and some bits (table)
768 for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
769 shl = mul_table[_val(val1[counter])][_val(shift)];
770 buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
774 shl = mul_table[_val(val1[counter])][_val(shift)];
775 buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
778 bitoffset = counter - 1;
781 /* fill with zeroes */
782 for (counter = 0; counter < shift_cnt; counter++)
783 buffer[counter] = SC_0;
785 /* if the mode was signed, change sign when the mode's msb is now 1 */
786 shift_cnt = bitoffset + shift_cnt;
787 bitoffset = (bitsize-1) % 4;
788 if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
789 /* this sets the upper bits of the leftmost digit */
790 buffer[shift_cnt] = or_table[_val(buffer[shift_cnt])][_val(min_digit[bitoffset])];
791 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
792 buffer[counter] = SC_F;
794 } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
795 /* this clears the upper bits of the leftmost digit */
796 buffer[shift_cnt] = and_table[_val(buffer[shift_cnt])][_val(max_digit[bitoffset])];
797 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
798 buffer[counter] = SC_0;
804 * Implements a Shift Right, which can either preserve the sign bit
807 * @param bitsize bitsize of the value to be shifted
809 * @todo Assertions seems to be wrong
811 static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift) {
816 int shift_mod, shift_nib;
821 assert((shift_cnt >= 0) || (0 && "negative rightshift"));
822 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
823 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
825 sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
827 /* if shifting far enough the result is either 0 or -1 */
828 if (shift_cnt >= bitsize) {
829 if (!sc_is_zero(val1)) {
832 memset(buffer, sign, calc_buffer_size);
836 shift_mod = shift_cnt & 3;
837 shift_nib = shift_cnt >> 2;
839 /* check if any bits are lost, and set carry_flag if so */
840 for (counter = 0; counter < shift_nib; ++counter) {
841 if (val1[counter] != 0) {
846 if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
849 /* shift digits to the right with offset, carry and all */
850 if ((bitsize >> 2) > shift_nib) {
851 buffer[0] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
853 for (counter = 1; counter < ((bitsize + 3) >> 2) - shift_nib; counter++) {
854 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
855 buffer[counter] = shrs[0];
856 buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
859 /* the last digit is special in regard of signed/unsigned shift */
860 bitoffset = bitsize & 3;
861 msd = sign; /* most significant digit */
863 /* remove sign bits if mode was signed and this is an unsigned shift */
864 if (!signed_shift && is_signed) {
865 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
868 shrs = shrs_table[_val(msd)][shift_mod];
870 /* signed shift and signed mode and negative value means all bits to the left are set */
871 if (signed_shift && sign == SC_F) {
872 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
874 buffer[counter] = shrs[0];
878 buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
880 /* fill with SC_F or SC_0 depending on sign */
881 for (counter++; counter < calc_buffer_size; counter++) {
882 buffer[counter] = sign;
887 * Implements a Rotate Left.
888 * positive: low-order -> high order, negative other direction
890 static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed) {
892 temp1 = alloca(calc_buffer_size);
893 temp2 = alloca(calc_buffer_size);
895 offset = offset % radius;
897 /* rotation by multiples of the type length is identity */
899 memmove(buffer, val1, calc_buffer_size);
903 do_shl(val1, temp1, offset, radius, is_signed);
904 do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
905 do_bitor(temp1, temp2, buffer);
906 carry_flag = 0; /* set by shr, but due to rot this is false */
909 /*****************************************************************************
910 * public functions, declared in strcalc.h
911 *****************************************************************************/
912 const void *sc_get_buffer(void) {
913 return (void*)calc_buffer;
916 int sc_get_buffer_length(void) {
917 return calc_buffer_size;
921 * Do sign extension if the mode is signed, otherwise to zero extension.
923 void sign_extend(void *buffer, ir_mode *mode) {
924 char *calc_buffer = buffer;
925 int bits = get_mode_size_bits(mode) - 1;
926 int nibble = bits >> 2;
927 int max = max_digit[bits & 3];
930 if (mode_is_signed(mode)) {
931 if (calc_buffer[nibble] > max) {
932 /* sign bit is set, we need sign expansion */
934 for (i = nibble + 1; i < calc_buffer_size; ++i)
935 calc_buffer[i] = SC_F;
936 calc_buffer[nibble] = or_table[(int)calc_buffer[nibble]][(int)sex_digit[bits & 3]];
938 /* set all bits to zero */
939 for (i = nibble + 1; i < calc_buffer_size; ++i)
940 calc_buffer[i] = SC_0;
941 calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
944 /* do zero extension */
945 for (i = nibble + 1; i < calc_buffer_size; ++i)
946 calc_buffer[i] = SC_0;
947 calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
951 /* FIXME doesn't check for overflows */
952 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode) {
953 const char *orig_str = str;
954 unsigned int orig_len = len;
959 base = alloca(calc_buffer_size);
960 val = alloca(calc_buffer_size);
962 /* verify valid pointers (not null) */
964 /* a string no characters long is an error */
967 if (buffer == NULL) buffer = calc_buffer;
969 CLEAR_BUFFER(buffer);
973 /* strip leading spaces */
974 while ((len > 0) && (*str == ' ')) { len--; str++; }
976 /* if the first two characters are 0x or 0X -> hex
977 * if the first is a 0 -> oct
978 * else dec, strip leading -/+ and remember sign
980 * only a + or - sign is no number resulting in an error */
984 if (str[1] == 'x' || str[1] == 'X') { /* hex */
987 base[1] = SC_1; base[0] = SC_0;
991 base[1] = SC_0; base[0] = SC_8;
998 base[1] = SC_0; base[0] = SC_A;
1005 base[1] = SC_0; base[0] = SC_A;
1008 default: /* dec, else would have begun with 0x or 0 */
1009 base[1] = SC_0; base[0] = SC_A;
1011 } else { /* dec, else would have begun with 0x or 0 */
1012 base[1] = SC_0; base[0] = SC_A;
1015 /* BEGIN string evaluation, from left to right */
1024 if (base[0] > SC_9 || base[1] > SC_0) { /* (base > 10) */
1025 val[0] = _digit((*str)-'a'+10);
1028 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1037 if (base[0] > SC_9 || base[1] > SC_0) { /* (base > 10) */
1038 val[0] = _digit((*str)-'A'+10);
1041 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1046 if (base[0] > SC_7 || base[1] > SC_0) { /* (base > 8) */
1047 val[0] = _digit((*str)-'0');
1050 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1061 val[0] = _digit((*str)-'0');
1065 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1066 } /* switch(*str) */
1068 /* Radix conversion from base b to base B:
1069 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1070 do_mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1071 do_add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1073 /* get ready for the next letter */
1076 } /* while (len > 0 ) */
1079 do_negate(calc_buffer, calc_buffer);
1081 /* beware: even if hex numbers have no sign, we need sign extension here */
1082 sign_extend(calc_buffer, mode);
1085 void sc_val_from_long(long value, void *buffer) {
1087 char sign, is_minlong;
1089 if (buffer == NULL) buffer = calc_buffer;
1093 is_minlong = value == LONG_MIN;
1095 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
1103 CLEAR_BUFFER(buffer);
1105 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
1106 *pos++ = _digit(value & 0xf);
1112 do_inc(buffer, buffer);
1114 do_negate(buffer, buffer);
1118 void sc_val_from_ulong(unsigned long value, void *buffer) {
1121 if (buffer == NULL) buffer = calc_buffer;
1124 while (pos < (unsigned char *)buffer + calc_buffer_size) {
1125 *pos++ = (unsigned char)_digit(value & 0xf);
1130 long sc_val_to_long(const void *val) {
1134 for (i = calc_buffer_size - 1; i >= 0; i--) {
1135 l = (l << 4) + _val(((char *)val)[i]);
1140 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1144 if (buffer == NULL) buffer = calc_buffer;
1145 CLEAR_BUFFER(buffer);
1147 if (!sign) return; /* unsigned means minimum is 0(zero) */
1151 bits = num_bits - 1;
1152 for (i = 0; i < bits/4; i++)
1155 *pos++ = min_digit[bits%4];
1157 for (i++; i <= calc_buffer_size - 1; i++)
1161 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1165 if (buffer == NULL) buffer = calc_buffer;
1166 CLEAR_BUFFER(buffer);
1169 bits = num_bits - sign;
1170 for (i = 0; i < bits/4; i++)
1173 *pos++ = max_digit[bits%4];
1175 for (i++; i <= calc_buffer_size - 1; i++)
1179 void sc_truncate(unsigned int num_bits, void *buffer) {
1180 char *cbuffer = buffer;
1181 char *pos = cbuffer + (num_bits / 4);
1182 char *end = cbuffer + calc_buffer_size;
1186 switch(num_bits % 4) {
1187 case 0: /* nothing to do */ break;
1188 case 1: *pos = and_table[_val(*pos)][SC_1]; pos++; break;
1189 case 2: *pos = and_table[_val(*pos)][SC_3]; pos++; break;
1190 case 3: *pos = and_table[_val(*pos)][SC_7]; pos++; break;
1193 for( ; pos < end; ++pos)
1197 int sc_comp(const void* value1, const void* value2) {
1198 int counter = calc_buffer_size - 1;
1199 const char *val1 = (const char *)value1;
1200 const char *val2 = (const char *)value2;
1202 /* compare signs first:
1203 * the loop below can only compare values of the same sign! */
1204 if (do_sign(val1) != do_sign(val2))
1205 return (do_sign(val1) == 1)?(1):(-1);
1207 /* loop until two digits differ, the values are equal if there
1208 * are no such two digits */
1209 while (val1[counter] == val2[counter]) {
1211 if (counter < 0) return 0;
1214 /* the leftmost digit is the most significant, so this returns
1215 * the correct result.
1216 * This implies the digit enum is ordered */
1217 return (val1[counter] > val2[counter]) ? (1) : (-1);
1220 int sc_get_highest_set_bit(const void *value) {
1221 const char *val = (const char*)value;
1224 high = calc_buffer_size * 4 - 1;
1226 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1227 if (val[counter] == SC_0)
1230 if (val[counter] > SC_7) return high;
1231 else if (val[counter] > SC_3) return high - 1;
1232 else if (val[counter] > SC_1) return high - 2;
1233 else return high - 3;
1239 int sc_get_lowest_set_bit(const void *value) {
1240 const char *val = (const char*)value;
1244 for (counter = 0; counter < calc_buffer_size; counter++) {
1245 switch (val[counter]) {
1272 int sc_get_bit_at(const void *value, unsigned pos) {
1273 const char *val = value;
1274 unsigned nibble = pos >> 2;
1276 if (and_table[(int) val[nibble]][(int) shift_table[pos & 3]] != SC_0)
1281 int sc_is_zero(const void *value) {
1282 const char* val = (const char *)value;
1285 for (counter = 0; counter < calc_buffer_size; ++counter) {
1286 if (val[counter] != SC_0)
1292 int sc_is_negative(const void *value) {
1293 return do_sign(value) == -1;
1296 int sc_had_carry(void) {
1300 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs) {
1301 const char *val = (const char *)value;
1302 int nibble_ofs = 2 * byte_ofs;
1305 /* the current scheme uses one byte to store a nibble */
1306 if (4 * nibble_ofs >= len)
1309 res = _val(val[nibble_ofs]);
1310 if (len > 4 * (nibble_ofs + 1))
1311 res |= _val(val[nibble_ofs + 1]) << 4;
1313 /* kick bits outsize */
1314 if (len - 8 * byte_ofs < 8) {
1315 res &= (1 << (len - 8 * byte_ofs)) - 1;
1321 * convert to a string
1322 * FIXME: Doesn't check buffer bounds
1324 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode) {
1325 static const char big_digits[] = "0123456789ABCDEF";
1326 static const char small_digits[] = "0123456789abcdef";
1328 char *base_val, *div1_res, *div2_res, *rem_res;
1329 int counter, nibbles, i, sign, mask;
1332 const char *val = (const char *)value;
1336 const char *digits = small_digits;
1338 base_val = alloca(calc_buffer_size);
1339 div1_res = alloca(calc_buffer_size);
1340 div2_res = alloca(calc_buffer_size);
1341 rem_res = alloca(calc_buffer_size);
1343 pos = output_buffer + bit_pattern_size;
1348 bits = bit_pattern_size;
1349 #ifdef STRCALC_DEBUG_FULLPRINT
1353 nibbles = bits >> 2;
1357 digits = big_digits;
1359 for (counter = 0; counter < nibbles; ++counter) {
1360 *(--pos) = digits[_val(val[counter])];
1361 #ifdef STRCALC_DEBUG_GROUPPRINT
1362 if ((counter+1)%8 == 0)
1367 /* last nibble must be masked */
1369 mask = zex_digit[bits & 3];
1370 x = and_table[_val(val[counter++])][mask];
1371 *(--pos) = digits[_val(x)];
1374 /* now kill zeros */
1375 for (; counter > 1; --counter, ++pos) {
1376 #ifdef STRCALC_DEBUG_GROUPPRINT
1377 if (pos[0] == ' ') ++pos;
1385 for (counter = 0; counter < nibbles; ++counter) {
1387 p = binary_table[_val(val[counter])];
1394 /* last nibble must be masked */
1396 mask = zex_digit[bits & 3];
1397 x = and_table[_val(val[counter++])][mask];
1400 p = binary_table[_val(x)];
1407 /* now kill zeros */
1408 for (counter <<= 2; counter > 1; --counter, ++pos)
1415 memset(base_val, SC_0, calc_buffer_size);
1416 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1420 if (signed_mode && base == SC_DEC) {
1421 /* check for negative values */
1422 if (do_bit(val, bits - 1)) {
1423 do_negate(val, div2_res);
1429 /* transfer data into oscillating buffers */
1430 memset(div1_res, SC_0, calc_buffer_size);
1431 for (counter = 0; counter < nibbles; ++counter)
1432 div1_res[counter] = p[counter];
1434 /* last nibble must be masked */
1436 mask = zex_digit[bits & 3];
1437 div1_res[counter] = and_table[_val(p[counter])][mask];
1444 do_divmod(m, base_val, n, rem_res);
1448 *(--pos) = digits[_val(rem_res[0])];
1451 for (i = 0; i < calc_buffer_size; ++i)
1462 panic("Unsupported base %d", base);
1467 void init_strcalc(int precision) {
1468 if (calc_buffer == NULL) {
1469 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1471 /* round up to multiple of 4 */
1472 precision = (precision + 3) & ~3;
1474 bit_pattern_size = (precision);
1475 calc_buffer_size = (precision / 2);
1476 max_value_size = (precision / 4);
1478 calc_buffer = XMALLOCN(char, calc_buffer_size + 1);
1479 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1481 DEBUGPRINTF(("init strcalc: \n\tPRECISION: %d\n\tCALC_BUFFER_SIZE = %d\n\tMAX_VALUE_SIZE = %d\n\tbuffer pointer: %p\n", precision, calc_buffer_size, max_value_size, calc_buffer));
1486 void finish_strcalc(void) {
1487 free(calc_buffer); calc_buffer = NULL;
1488 free(output_buffer); output_buffer = NULL;
1491 int sc_get_precision(void) {
1492 return bit_pattern_size;
1496 void sc_add(const void *value1, const void *value2, void *buffer) {
1497 CLEAR_BUFFER(calc_buffer);
1500 DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1501 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1503 do_add(value1, value2, calc_buffer);
1505 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1507 if ((buffer != NULL) && (buffer != calc_buffer)) {
1508 memcpy(buffer, calc_buffer, calc_buffer_size);
1512 void sc_sub(const void *value1, const void *value2, void *buffer) {
1513 CLEAR_BUFFER(calc_buffer);
1516 DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1517 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1519 do_sub(value1, value2, calc_buffer);
1521 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1523 if ((buffer != NULL) && (buffer != calc_buffer)) {
1524 memcpy(buffer, calc_buffer, calc_buffer_size);
1528 void sc_neg(const void *value1, void *buffer) {
1531 DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1533 do_negate(value1, calc_buffer);
1535 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1537 if ((buffer != NULL) && (buffer != calc_buffer)) {
1538 memcpy(buffer, calc_buffer, calc_buffer_size);
1542 void sc_and(const void *value1, const void *value2, void *buffer) {
1543 CLEAR_BUFFER(calc_buffer);
1546 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1547 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1549 do_bitand(value1, value2, calc_buffer);
1551 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1553 if ((buffer != NULL) && (buffer != calc_buffer)) {
1554 memcpy(buffer, calc_buffer, calc_buffer_size);
1558 void sc_or(const void *value1, const void *value2, void *buffer) {
1559 CLEAR_BUFFER(calc_buffer);
1562 DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1563 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1565 do_bitor(value1, value2, calc_buffer);
1567 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1569 if ((buffer != NULL) && (buffer != calc_buffer)) {
1570 memcpy(buffer, calc_buffer, calc_buffer_size);
1574 void sc_xor(const void *value1, const void *value2, void *buffer) {
1575 CLEAR_BUFFER(calc_buffer);
1578 DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1579 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1581 do_bitxor(value1, value2, calc_buffer);
1583 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1585 if ((buffer != NULL) && (buffer != calc_buffer)) {
1586 memcpy(buffer, calc_buffer, calc_buffer_size);
1590 void sc_not(const void *value1, void *buffer) {
1591 CLEAR_BUFFER(calc_buffer);
1594 DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1596 do_bitnot(value1, calc_buffer);
1598 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1600 if ((buffer != NULL) && (buffer != calc_buffer)) {
1601 memcpy(buffer, calc_buffer, calc_buffer_size);
1605 void sc_mul(const void *value1, const void *value2, void *buffer) {
1606 CLEAR_BUFFER(calc_buffer);
1609 DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1610 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1612 do_mul(value1, value2, calc_buffer);
1614 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1616 if ((buffer != NULL) && (buffer != calc_buffer)) {
1617 memcpy(buffer, calc_buffer, calc_buffer_size);
1621 void sc_div(const void *value1, const void *value2, void *buffer) {
1622 /* temp buffer holding unused result of divmod */
1623 char *unused_res = alloca(calc_buffer_size);
1625 CLEAR_BUFFER(calc_buffer);
1628 DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1629 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1631 do_divmod(value1, value2, calc_buffer, unused_res);
1633 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1635 if ((buffer != NULL) && (buffer != calc_buffer)) {
1636 memcpy(buffer, calc_buffer, calc_buffer_size);
1640 void sc_mod(const void *value1, const void *value2, void *buffer) {
1641 /* temp buffer holding unused result of divmod */
1642 char *unused_res = alloca(calc_buffer_size);
1644 CLEAR_BUFFER(calc_buffer);
1647 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1648 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1650 do_divmod(value1, value2, unused_res, calc_buffer);
1652 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1654 if ((buffer != NULL) && (buffer != calc_buffer)) {
1655 memcpy(buffer, calc_buffer, calc_buffer_size);
1659 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer) {
1660 CLEAR_BUFFER(calc_buffer);
1663 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1664 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1666 do_divmod(value1, value2, div_buffer, mod_buffer);
1668 DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1672 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1675 DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1676 do_shl(val1, calc_buffer, shift_cnt, bitsize, sign);
1678 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1680 if ((buffer != NULL) && (buffer != calc_buffer)) {
1681 memmove(buffer, calc_buffer, calc_buffer_size);
1685 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1686 long offset = sc_val_to_long(val2);
1688 sc_shlI(val1, offset, bitsize, sign, buffer);
1691 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1694 DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1695 do_shr(val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1697 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1699 if ((buffer != NULL) && (buffer != calc_buffer)) {
1700 memmove(buffer, calc_buffer, calc_buffer_size);
1704 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1705 long shift_cnt = sc_val_to_long(val2);
1707 sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1710 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1711 long offset = sc_val_to_long(val2);
1715 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1716 do_shr(val1, calc_buffer, offset, bitsize, sign, 1);
1718 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1720 if ((buffer != NULL) && (buffer != calc_buffer)) {
1721 memmove(buffer, calc_buffer, calc_buffer_size);
1725 void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1726 long offset = sc_val_to_long(val2);
1730 DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1731 do_rotl(val1, calc_buffer, offset, bitsize, sign);
1733 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1735 if ((buffer != NULL) && (buffer != calc_buffer)) {
1736 memmove(buffer, calc_buffer, calc_buffer_size);
1740 void sc_zero(void *buffer) {
1742 buffer = calc_buffer;
1743 CLEAR_BUFFER(buffer);