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
40 * local definitions and macros
42 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, calc_buffer_size)
43 #define _val(a) ((a)-SC_0)
44 #define _digit(a) ((a)+SC_0)
45 #define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
47 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__, __LINE__)
49 /* shortcut output for debugging */
50 # define sc_print_hex(a) sc_print((a), 0, SC_HEX, 0)
51 # define sc_print_dec(a) sc_print((a), 0, SC_DEC, 1)
52 # define sc_print_oct(a) sc_print((a), 0, SC_OCT, 0)
53 # define sc_print_bin(a) sc_print((a), 0, SC_BIN, 0)
55 #ifdef STRCALC_DEBUG_PRINTCOMP
56 # define DEBUGPRINTF_COMPUTATION(x) printf x
58 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
61 # define DEBUGPRINTF(x) printf x
63 # define DEBUGPRINTF(x) ((void)0)
70 static char *calc_buffer = NULL; /* buffer holding all results */
71 static char *output_buffer = NULL; /* buffer for output */
72 static int bit_pattern_size; /* maximum number of bits */
73 static int calc_buffer_size; /* size of internally stored values */
74 static int max_value_size; /* maximum size of values */
76 static int carry_flag; /**< some computation set the carry_flag:
77 - right shift if bits were lost due to shifting
78 - division if there was a remainder
79 However, the meaning of carry is machine dependent
80 and often defined in other ways! */
82 static const char sex_digit[4] = { SC_E, SC_C, SC_8, SC_0 };
83 static const char zex_digit[4] = { SC_1, SC_3, SC_7, SC_F };
84 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
85 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
87 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
89 static const char and_table[16][16] = {
90 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
91 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
93 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
94 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
96 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
97 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
99 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
100 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
102 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
103 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
105 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
106 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
108 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
109 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
111 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
112 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
114 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
115 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
117 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
118 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
120 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
121 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
123 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
124 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
126 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
127 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
129 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
130 SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
132 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
133 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
135 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
136 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
138 static const char or_table[16][16] = {
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 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
143 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
145 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
146 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
148 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
149 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
151 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
152 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
154 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
155 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
157 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
158 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
160 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
161 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
163 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
164 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
166 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
167 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
169 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
170 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
172 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
173 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
175 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
176 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
178 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
179 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
181 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
182 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
184 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
185 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
187 static char const xor_table[16][16] = {
188 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
189 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
191 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
192 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
194 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
195 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
197 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
198 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
200 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
201 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
203 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
204 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
206 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
207 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
209 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
210 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
212 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
213 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
215 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
216 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
218 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
219 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
221 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
222 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
224 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
225 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
227 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
228 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
230 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
231 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
233 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
234 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
237 static char const add_table[16][16][2] = {
238 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
239 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
240 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
241 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
243 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
244 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
245 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
246 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
248 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
249 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
250 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
251 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
253 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
254 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
255 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
256 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
258 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
259 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
260 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
261 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
263 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
264 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
265 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
266 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
268 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
269 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
270 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
271 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
273 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
274 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
275 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
276 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
278 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
279 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
280 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
281 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
283 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
284 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
285 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
286 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
288 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
289 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
290 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
291 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
293 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
294 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
295 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
296 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
298 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
299 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
300 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
301 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
303 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
304 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
305 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
306 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
308 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
309 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
310 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
311 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
313 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
314 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
315 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
316 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
319 static char const mul_table[16][16][2] = {
320 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
321 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
322 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
323 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
325 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
326 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
327 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
328 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
330 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
331 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
332 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
333 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
335 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
336 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
337 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
338 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
340 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
341 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
342 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
343 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
345 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
346 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
347 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
348 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
350 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
351 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
352 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
353 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
355 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
356 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
357 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
358 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
360 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
361 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
362 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
363 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
365 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
366 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
367 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
368 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
370 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
371 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
372 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
373 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
375 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
376 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
377 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
378 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
380 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
381 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
382 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
383 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
385 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
386 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
387 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
388 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
390 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
391 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
392 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
393 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
395 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
396 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
397 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
398 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
401 static char const shrs_table[16][4][2] = {
402 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
403 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
404 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
405 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
406 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
407 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
408 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
409 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
410 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
411 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
412 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
413 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
414 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
415 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
416 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
417 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
420 /** converting a digit to a binary string */
421 static const char *binary_table[16] = {
422 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
423 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
426 /*****************************************************************************
428 *****************************************************************************/
429 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
430 const char *file, int line) {
432 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
433 while (len-- && *str) printf("%c", *str++); printf("\n");
434 while (--pos) printf(" "); printf("^\n");
439 * implements the bitwise NOT operation
441 static void do_bitnot(const char *val, char *buffer) {
444 for (counter = 0; counter<calc_buffer_size; counter++)
445 buffer[counter] = val[counter] ^ SC_F;
449 * implements the bitwise OR operation
451 static void do_bitor(const char *val1, const char *val2, char *buffer) {
454 for (counter = 0; counter<calc_buffer_size; counter++)
455 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
459 * implements the bitwise eXclusive OR operation
461 static void do_bitxor(const char *val1, const char *val2, char *buffer) {
464 for (counter = 0; counter<calc_buffer_size; counter++)
465 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
469 * implements the bitwise AND operation
471 static void do_bitand(const char *val1, const char *val2, char *buffer) {
474 for (counter = 0; counter<calc_buffer_size; counter++)
475 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
479 * returns the sign bit.
481 * @todo This implementation is wrong, as it returns the highest bit of the buffer
482 * NOT the highest bit depending on the real mode
484 static int do_sign(const char *val) {
485 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
489 * returns non-zero if bit at position pos is set
491 static int do_bit(const char *val, int pos) {
493 int nibble = pos >> 2;
495 return _bitisset(val[nibble], bit);
499 * Implements a fast ADD + 1
501 static void do_inc(const char *val, char *buffer) {
504 while (counter++ < calc_buffer_size) {
509 /* No carry here, *val != SC_F */
510 *buffer = add_table[_val(*val)][SC_1][0];
514 /* here a carry could be lost, this is intended because this should
515 * happen only when a value changes sign. */
519 * Implements a unary MINUS
521 static void do_negate(const char *val, char *buffer) {
522 do_bitnot(val, buffer);
523 do_inc(buffer, buffer);
527 * Implements a binary ADD
529 * @todo The implementation of carry is wrong, as it is the
530 * calc_buffer_size carry, not the mode depending
532 static void do_add(const char *val1, const char *val2, char *buffer) {
534 const char *add1, *add2;
537 for (counter = 0; counter < calc_buffer_size; counter++) {
538 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
539 add2 = add_table[_val(add1[0])][_val(carry)];
540 /* carry might be zero */
541 buffer[counter] = add2[0];
542 carry = add_table[_val(add1[1])][_val(add2[1])][0];
544 carry_flag = carry != SC_0;
548 * Implements a binary SUB
550 static void do_sub(const char *val1, const char *val2, char *buffer) {
551 char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
553 do_negate(val2, temp_buffer);
554 do_add(val1, temp_buffer, buffer);
558 * Implements a binary MUL
560 static void do_mul(const char *val1, const char *val2, char *buffer) {
561 char *temp_buffer; /* result buffer */
562 char *neg_val1; /* abs of val1 */
563 char *neg_val2; /* abs of val2 */
565 const char *mul, *add1, *add2; /* intermediate result containers */
566 char carry = SC_0; /* container for carries */
567 char sign = 0; /* marks result sign */
568 int c_inner, c_outer; /* loop counters */
570 temp_buffer = alloca(calc_buffer_size);
571 neg_val1 = alloca(calc_buffer_size);
572 neg_val2 = alloca(calc_buffer_size);
574 /* init result buffer to zeros */
575 memset(temp_buffer, SC_0, calc_buffer_size);
577 /* the multiplication works only for positive values, for negative values *
578 * it is necessary to negate them and adjust the result accordingly */
579 if (do_sign(val1) == -1) {
580 do_negate(val1, neg_val1);
584 if (do_sign(val2) == -1) {
585 do_negate(val2, neg_val2);
590 for (c_outer = 0; c_outer < max_value_size; c_outer++) {
591 if (val2[c_outer] != SC_0) {
592 for (c_inner = 0; c_inner < max_value_size; c_inner++) {
593 /* do the following calculation: *
594 * Add the current carry, the value at position c_outer+c_inner *
595 * and the result of the multiplication of val1[c_inner] and *
596 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
598 /* multiplicate the two digits */
599 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
600 /* add old value to result of multiplication */
601 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
602 /* add carry to the sum */
603 add2 = add_table[_val(add1[0])][_val(carry)];
605 /* all carries together result in new carry. This is always smaller *
607 * Both multiplicands, the carry and the value already in the temp *
608 * buffer are single digits and their value is therefore at most *
611 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
612 * The tables list all operations rem b, so the carry is at most *
613 * (b*b-1)rem b = -1rem b = b-1 */
614 carry = add_table[_val(mul[1])][_val(add1[1])][0];
615 carry = add_table[_val(carry)][_val(add2[1])][0];
617 temp_buffer[c_inner + c_outer] = add2[0];
620 /* A carry may hang over */
621 /* c_outer is always smaller than max_value_size! */
622 temp_buffer[max_value_size + c_outer] = carry;
628 do_negate(temp_buffer, buffer);
630 memcpy(buffer, temp_buffer, calc_buffer_size);
634 * Shift the buffer to left and add a 4 bit digit
636 static void do_push(const char digit, char *buffer) {
639 for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
640 buffer[counter+1] = buffer[counter];
646 * Implements truncating integer division and remainder.
648 * Note: This is MOST slow
650 static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem) {
651 const char *dividend = rDividend;
652 const char *minus_divisor;
656 char div_sign = 0; /* remember division result sign */
657 char rem_sign = 0; /* remember remainder result sign */
659 int c_dividend; /* loop counters */
661 neg_val1 = alloca(calc_buffer_size);
662 neg_val2 = alloca(calc_buffer_size);
664 /* clear result buffer */
665 memset(quot, SC_0, calc_buffer_size);
666 memset(rem, SC_0, calc_buffer_size);
668 /* if the divisor is zero this won't work (quot is zero) */
669 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
671 /* if the dividend is zero result is zero (quot is zero) */
672 if (sc_comp(dividend, quot) == 0)
675 if (do_sign(dividend) == -1) {
676 do_negate(dividend, neg_val1);
682 do_negate(divisor, neg_val2);
683 if (do_sign(divisor) == -1) {
685 minus_divisor = divisor;
688 minus_divisor = neg_val2;
690 /* if divisor >= dividend division is easy
691 * (remember these are absolute values) */
692 switch (sc_comp(dividend, divisor)) {
693 case 0: /* dividend == divisor */
697 case -1: /* dividend < divisor */
698 memcpy(rem, dividend, calc_buffer_size);
701 default: /* unluckily division is necessary :( */
705 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
706 do_push(dividend[c_dividend], rem);
709 if (sc_comp(rem, divisor) != -1) { /* remainder >= divisor */
710 /* subtract until the remainder becomes negative, this should
711 * be faster than comparing remainder with divisor */
712 do_add(rem, minus_divisor, rem);
714 while (do_sign(rem) == 1) {
715 quot[0] = add_table[_val(quot[0])][SC_1][0];
716 do_add(rem, minus_divisor, rem);
719 /* subtracted one too much */
720 do_add(rem, divisor, rem);
724 /* sets carry if remainder is non-zero ??? */
725 carry_flag = !sc_is_zero(rem);
728 do_negate(quot, quot);
735 * Implements a Shift Left, which can either preserve the sign bit
738 * @todo Assertions seems to be wrong
740 static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed) {
748 assert((shift_cnt >= 0) || (0 && "negative leftshift"));
749 assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
750 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
751 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
753 /* if shifting far enough the result is zero */
754 if (shift_cnt >= bitsize) {
755 memset(buffer, SC_0, calc_buffer_size);
759 shift = shift_table[_val(shift_cnt%4)]; /* this is 2 ** (offset % 4) */
760 shift_cnt = shift_cnt / 4;
762 /* shift the single digits some bytes (offset) and some bits (table)
764 for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
765 shl = mul_table[_val(val1[counter])][_val(shift)];
766 buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
770 shl = mul_table[_val(val1[counter])][_val(shift)];
771 buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
774 bitoffset = counter - 1;
777 /* fill with zeroes */
778 for (counter = 0; counter < shift_cnt; counter++)
779 buffer[counter] = SC_0;
781 /* if the mode was signed, change sign when the mode's msb is now 1 */
782 shift_cnt = bitoffset + shift_cnt;
783 bitoffset = (bitsize-1) % 4;
784 if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
785 /* this sets the upper bits of the leftmost digit */
786 buffer[shift_cnt] = or_table[_val(buffer[shift_cnt])][_val(min_digit[bitoffset])];
787 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
788 buffer[counter] = SC_F;
790 } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
791 /* this clears the upper bits of the leftmost digit */
792 buffer[shift_cnt] = and_table[_val(buffer[shift_cnt])][_val(max_digit[bitoffset])];
793 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
794 buffer[counter] = SC_0;
800 * Implements a Shift Right, which can either preserve the sign bit
803 * @param bitsize bitsize of the value to be shifted
805 * @todo Assertions seems to be wrong
807 static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift) {
812 int shift_mod, shift_nib;
817 assert((shift_cnt >= 0) || (0 && "negative rightshift"));
818 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
819 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
821 sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
823 /* if shifting far enough the result is either 0 or -1 */
824 if (shift_cnt >= bitsize) {
825 if (!sc_is_zero(val1)) {
828 memset(buffer, sign, calc_buffer_size);
832 shift_mod = shift_cnt & 3;
833 shift_nib = shift_cnt >> 2;
835 /* check if any bits are lost, and set carry_flag if so */
836 for (counter = 0; counter < shift_nib; ++counter) {
837 if (val1[counter] != 0) {
842 if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
845 /* shift digits to the right with offset, carry and all */
846 buffer[0] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
847 for (counter = 1; counter < ((bitsize + 3) >> 2) - shift_nib; counter++) {
848 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
849 buffer[counter] = shrs[0];
850 buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
853 /* the last digit is special in regard of signed/unsigned shift */
854 bitoffset = bitsize & 3;
855 msd = sign; /* most significant digit */
857 /* remove sign bits if mode was signed and this is an unsigned shift */
858 if (!signed_shift && is_signed) {
859 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
862 shrs = shrs_table[_val(msd)][shift_mod];
864 /* signed shift and signed mode and negative value means all bits to the left are set */
865 if (signed_shift && sign == SC_F) {
866 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
868 buffer[counter] = shrs[0];
872 buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
874 /* fill with SC_F or SC_0 depending on sign */
875 for (counter++; counter < calc_buffer_size; counter++) {
876 buffer[counter] = sign;
881 * Implements a Rotate Left.
882 * positive: low-order -> high order, negative other direction
884 static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed) {
886 temp1 = alloca(calc_buffer_size);
887 temp2 = alloca(calc_buffer_size);
889 offset = offset % radius;
891 /* rotation by multiples of the type length is identity */
893 memmove(buffer, val1, calc_buffer_size);
897 do_shl(val1, temp1, offset, radius, is_signed);
898 do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
899 do_bitor(temp1, temp2, buffer);
900 carry_flag = 0; /* set by shr, but due to rot this is false */
903 /*****************************************************************************
904 * public functions, declared in strcalc.h
905 *****************************************************************************/
906 const void *sc_get_buffer(void) {
907 return (void*)calc_buffer;
910 int sc_get_buffer_length(void) {
911 return calc_buffer_size;
915 * Do sign extension if the mode is signed, otherwise to zero extension.
917 void sign_extend(void *buffer, ir_mode *mode) {
918 char *calc_buffer = buffer;
919 int bits = get_mode_size_bits(mode) - 1;
920 int nibble = bits >> 2;
921 int max = max_digit[bits & 3];
924 if (mode_is_signed(mode)) {
925 if (calc_buffer[nibble] > max) {
926 /* sign bit is set, we need sign expansion */
928 for (i = nibble + 1; i < calc_buffer_size; ++i)
929 calc_buffer[i] = SC_F;
930 calc_buffer[nibble] = or_table[(int)calc_buffer[nibble]][(int)sex_digit[bits & 3]];
932 /* set all bits to zero */
933 for (i = nibble + 1; i < calc_buffer_size; ++i)
934 calc_buffer[i] = SC_0;
935 calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
938 /* do zero extension */
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]];
945 /* FIXME doesn't check for overflows */
946 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode) {
947 const char *orig_str = str;
948 unsigned int orig_len = len;
953 base = alloca(calc_buffer_size);
954 val = alloca(calc_buffer_size);
956 /* verify valid pointers (not null) */
958 /* a string no characters long is an error */
961 if (buffer == NULL) buffer = calc_buffer;
963 CLEAR_BUFFER(buffer);
967 /* strip leading spaces */
968 while ((len > 0) && (*str == ' ')) { len--; str++; }
970 /* if the first two characters are 0x or 0X -> hex
971 * if the first is a 0 -> oct
972 * else dec, strip leading -/+ and remember sign
974 * only a + or - sign is no number resulting in an error */
978 if (str[1] == 'x' || str[1] == 'X') { /* hex */
981 base[1] = SC_1; base[0] = SC_0;
985 base[1] = SC_0; base[0] = SC_8;
992 base[1] = SC_0; base[0] = SC_A;
999 base[1] = SC_0; base[0] = SC_A;
1002 default: /* dec, else would have begun with 0x or 0 */
1003 base[1] = SC_0; base[0] = SC_A;
1005 } else { /* dec, else would have begun with 0x or 0 */
1006 base[1] = SC_0; base[0] = SC_A;
1009 /* BEGIN string evaluation, from left to right */
1018 if (base[0] > SC_A || base[1] > SC_0) { /* (base > 10) */
1019 val[0] = _digit((*str)-'a'+10);
1022 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1031 if (base[0] > SC_A || base[1] > SC_0) { /* (base > 10) */
1032 val[0] = _digit((*str)-'A'+10);
1035 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1040 if (base[0] > SC_8 || base[1] > SC_0) { /* (base > 8) */
1041 val[0] = _digit((*str)-'0');
1044 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1055 val[0] = _digit((*str)-'0');
1059 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1060 } /* switch(*str) */
1062 /* Radix conversion from base b to base B:
1063 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1064 do_mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1065 do_add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1067 /* get ready for the next letter */
1070 } /* while (len > 0 ) */
1073 do_negate(calc_buffer, calc_buffer);
1075 /* beware: even if hex numbers have no sign, we need sign extension here */
1076 sign_extend(calc_buffer, mode);
1079 void sc_val_from_long(long value, void *buffer) {
1081 char sign, is_minlong;
1083 if (buffer == NULL) buffer = calc_buffer;
1087 is_minlong = value == LONG_MIN;
1089 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
1097 CLEAR_BUFFER(buffer);
1099 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
1100 *pos++ = _digit(value & 0xf);
1106 do_inc(buffer, buffer);
1108 do_negate(buffer, buffer);
1112 void sc_val_from_ulong(unsigned long value, void *buffer) {
1115 if (buffer == NULL) buffer = calc_buffer;
1118 while (pos < (unsigned char *)buffer + calc_buffer_size) {
1119 *pos++ = (unsigned char)_digit(value & 0xf);
1124 long sc_val_to_long(const void *val) {
1128 for (i = calc_buffer_size - 1; i >= 0; i--) {
1129 l = (l << 4) + _val(((char *)val)[i]);
1134 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1138 if (buffer == NULL) buffer = calc_buffer;
1139 CLEAR_BUFFER(buffer);
1141 if (!sign) return; /* unsigned means minimum is 0(zero) */
1145 bits = num_bits - 1;
1146 for (i = 0; i < bits/4; i++)
1149 *pos++ = min_digit[bits%4];
1151 for (i++; i <= calc_buffer_size - 1; i++)
1155 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1159 if (buffer == NULL) buffer = calc_buffer;
1160 CLEAR_BUFFER(buffer);
1163 bits = num_bits - sign;
1164 for (i = 0; i < bits/4; i++)
1167 *pos++ = max_digit[bits%4];
1169 for (i++; i <= calc_buffer_size - 1; i++)
1173 void sc_truncate(unsigned int num_bits, void *buffer) {
1174 char *cbuffer = buffer;
1175 char *pos = cbuffer + (num_bits / 4);
1176 char *end = cbuffer + calc_buffer_size;
1180 switch(num_bits % 4) {
1181 case 0: /* nothing to do */ break;
1182 case 1: *pos = and_table[_val(*pos)][SC_1]; pos++; break;
1183 case 2: *pos = and_table[_val(*pos)][SC_3]; pos++; break;
1184 case 3: *pos = and_table[_val(*pos)][SC_7]; pos++; break;
1187 for( ; pos < end; ++pos)
1191 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 (do_sign(val1) != do_sign(val2))
1199 return (do_sign(val1) == 1)?(1):(-1);
1201 /* loop until two digits differ, the values are equal if there
1202 * are no such two digits */
1203 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) {
1215 const char *val = (const char*)value;
1218 high = calc_buffer_size * 4 - 1;
1220 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1221 if (val[counter] == SC_0)
1224 if (val[counter] > SC_7) return high;
1225 else if (val[counter] > SC_3) return high - 1;
1226 else if (val[counter] > SC_1) return high - 2;
1227 else return high - 3;
1233 int sc_get_lowest_set_bit(const void *value) {
1234 const char *val = (const char*)value;
1238 for (counter = 0; counter < calc_buffer_size; counter++) {
1239 switch (val[counter]) {
1266 int sc_get_bit_at(const void *value, unsigned pos) {
1267 const char *val = value;
1268 unsigned nibble = pos >> 2;
1270 if (and_table[(int) val[nibble]][(int) shift_table[pos & 3]] != SC_0)
1275 void sc_set_bit_at(void *value, unsigned pos)
1278 unsigned nibble = pos >> 2;
1280 val[nibble] = or_table[(int)val[nibble]][(int)shift_table[pos & 3]];
1283 int sc_is_zero(const void *value) {
1284 const char* val = (const char *)value;
1287 for (counter = 0; counter < calc_buffer_size; ++counter) {
1288 if (val[counter] != SC_0)
1294 int sc_is_negative(const void *value) {
1295 return do_sign(value) == -1;
1298 int sc_had_carry(void) {
1302 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs) {
1303 const char *val = (const char *)value;
1304 int nibble_ofs = 2 * byte_ofs;
1307 /* the current scheme uses one byte to store a nibble */
1308 if (4 * nibble_ofs >= len)
1311 res = _val(val[nibble_ofs]);
1312 if (len > 4 * (nibble_ofs + 1))
1313 res |= _val(val[nibble_ofs + 1]) << 4;
1315 /* kick bits outsize */
1316 if (len - 8 * byte_ofs < 8) {
1317 res &= (1 << (len - 8 * byte_ofs)) - 1;
1323 * convert to a string
1324 * FIXME: Doesn't check buffer bounds
1326 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode) {
1327 static const char big_digits[] = "0123456789ABCDEF";
1328 static const char small_digits[] = "0123456789abcdef";
1330 char *base_val, *div1_res, *div2_res, *rem_res;
1331 int counter, nibbles, i, sign, mask;
1334 const char *val = (const char *)value;
1338 const char *digits = small_digits;
1340 base_val = alloca(calc_buffer_size);
1341 div1_res = alloca(calc_buffer_size);
1342 div2_res = alloca(calc_buffer_size);
1343 rem_res = alloca(calc_buffer_size);
1345 pos = output_buffer + bit_pattern_size;
1350 bits = bit_pattern_size;
1351 #ifdef STRCALC_DEBUG_FULLPRINT
1355 nibbles = bits >> 2;
1359 digits = big_digits;
1361 for (counter = 0; counter < nibbles; ++counter) {
1362 *(--pos) = digits[_val(val[counter])];
1363 #ifdef STRCALC_DEBUG_GROUPPRINT
1364 if ((counter+1)%8 == 0)
1369 /* last nibble must be masked */
1371 mask = zex_digit[(bits & 3) - 1];
1372 x = and_table[_val(val[counter++])][mask];
1373 *(--pos) = digits[_val(x)];
1376 /* now kill zeros */
1377 for (; counter > 1; --counter, ++pos) {
1378 #ifdef STRCALC_DEBUG_GROUPPRINT
1379 if (pos[0] == ' ') ++pos;
1387 for (counter = 0; counter < nibbles; ++counter) {
1389 p = binary_table[_val(val[counter])];
1396 /* last nibble must be masked */
1398 mask = zex_digit[(bits & 3) - 1];
1399 x = and_table[_val(val[counter++])][mask];
1402 p = binary_table[_val(x)];
1409 /* now kill zeros */
1410 for (counter <<= 2; counter > 1; --counter, ++pos)
1417 memset(base_val, SC_0, calc_buffer_size);
1418 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1422 if (signed_mode && base == SC_DEC) {
1423 /* check for negative values */
1424 if (do_bit(val, bits - 1)) {
1425 do_negate(val, div2_res);
1431 /* transfer data into oscillating buffers */
1432 memset(div1_res, SC_0, calc_buffer_size);
1433 for (counter = 0; counter < nibbles; ++counter)
1434 div1_res[counter] = p[counter];
1436 /* last nibble must be masked */
1438 mask = zex_digit[(bits & 3) - 1];
1439 div1_res[counter] = and_table[_val(p[counter])][mask];
1446 do_divmod(m, base_val, n, rem_res);
1450 *(--pos) = digits[_val(rem_res[0])];
1453 for (i = 0; i < calc_buffer_size; ++i)
1464 panic("Unsupported base %d", base);
1469 void init_strcalc(int precision) {
1470 if (calc_buffer == NULL) {
1471 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1473 /* round up to multiple of 4 */
1474 precision = (precision + 3) & ~3;
1476 bit_pattern_size = (precision);
1477 calc_buffer_size = (precision / 2);
1478 max_value_size = (precision / 4);
1480 calc_buffer = XMALLOCN(char, calc_buffer_size + 1);
1481 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1483 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));
1488 void finish_strcalc(void) {
1489 free(calc_buffer); calc_buffer = NULL;
1490 free(output_buffer); output_buffer = NULL;
1493 int sc_get_precision(void) {
1494 return bit_pattern_size;
1498 void sc_add(const void *value1, const void *value2, void *buffer) {
1499 CLEAR_BUFFER(calc_buffer);
1502 DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1503 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1505 do_add(value1, value2, calc_buffer);
1507 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1509 if ((buffer != NULL) && (buffer != calc_buffer)) {
1510 memcpy(buffer, calc_buffer, calc_buffer_size);
1514 void sc_sub(const void *value1, const void *value2, void *buffer) {
1515 CLEAR_BUFFER(calc_buffer);
1518 DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1519 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1521 do_sub(value1, value2, calc_buffer);
1523 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1525 if ((buffer != NULL) && (buffer != calc_buffer)) {
1526 memcpy(buffer, calc_buffer, calc_buffer_size);
1530 void sc_neg(const void *value1, void *buffer) {
1533 DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1535 do_negate(value1, calc_buffer);
1537 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1539 if ((buffer != NULL) && (buffer != calc_buffer)) {
1540 memcpy(buffer, calc_buffer, calc_buffer_size);
1544 void sc_and(const void *value1, const void *value2, void *buffer) {
1545 CLEAR_BUFFER(calc_buffer);
1548 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1549 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1551 do_bitand(value1, value2, calc_buffer);
1553 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1555 if ((buffer != NULL) && (buffer != calc_buffer)) {
1556 memcpy(buffer, calc_buffer, calc_buffer_size);
1560 void sc_or(const void *value1, const void *value2, void *buffer) {
1561 CLEAR_BUFFER(calc_buffer);
1564 DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1565 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1567 do_bitor(value1, value2, calc_buffer);
1569 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1571 if ((buffer != NULL) && (buffer != calc_buffer)) {
1572 memcpy(buffer, calc_buffer, calc_buffer_size);
1576 void sc_xor(const void *value1, const void *value2, void *buffer) {
1577 CLEAR_BUFFER(calc_buffer);
1580 DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1581 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1583 do_bitxor(value1, value2, calc_buffer);
1585 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1587 if ((buffer != NULL) && (buffer != calc_buffer)) {
1588 memcpy(buffer, calc_buffer, calc_buffer_size);
1592 void sc_not(const void *value1, void *buffer) {
1593 CLEAR_BUFFER(calc_buffer);
1596 DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1598 do_bitnot(value1, calc_buffer);
1600 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1602 if ((buffer != NULL) && (buffer != calc_buffer)) {
1603 memcpy(buffer, calc_buffer, calc_buffer_size);
1607 void sc_mul(const void *value1, const void *value2, void *buffer) {
1608 CLEAR_BUFFER(calc_buffer);
1611 DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1612 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1614 do_mul(value1, value2, calc_buffer);
1616 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1618 if ((buffer != NULL) && (buffer != calc_buffer)) {
1619 memcpy(buffer, calc_buffer, calc_buffer_size);
1623 void sc_div(const void *value1, const void *value2, void *buffer) {
1624 /* temp buffer holding unused result of divmod */
1625 char *unused_res = alloca(calc_buffer_size);
1627 CLEAR_BUFFER(calc_buffer);
1630 DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1631 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1633 do_divmod(value1, value2, calc_buffer, unused_res);
1635 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1637 if ((buffer != NULL) && (buffer != calc_buffer)) {
1638 memcpy(buffer, calc_buffer, calc_buffer_size);
1642 void sc_mod(const void *value1, const void *value2, void *buffer) {
1643 /* temp buffer holding unused result of divmod */
1644 char *unused_res = alloca(calc_buffer_size);
1646 CLEAR_BUFFER(calc_buffer);
1649 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1650 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1652 do_divmod(value1, value2, unused_res, calc_buffer);
1654 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1656 if ((buffer != NULL) && (buffer != calc_buffer)) {
1657 memcpy(buffer, calc_buffer, calc_buffer_size);
1661 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer) {
1662 CLEAR_BUFFER(calc_buffer);
1665 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1666 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1668 do_divmod(value1, value2, div_buffer, mod_buffer);
1670 DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1674 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1677 DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1678 do_shl(val1, calc_buffer, shift_cnt, bitsize, sign);
1680 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1682 if ((buffer != NULL) && (buffer != calc_buffer)) {
1683 memmove(buffer, calc_buffer, calc_buffer_size);
1687 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1688 long offset = sc_val_to_long(val2);
1690 sc_shlI(val1, offset, bitsize, sign, buffer);
1693 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1696 DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1697 do_shr(val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1699 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1701 if ((buffer != NULL) && (buffer != calc_buffer)) {
1702 memmove(buffer, calc_buffer, calc_buffer_size);
1706 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1707 long shift_cnt = sc_val_to_long(val2);
1709 sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1712 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1713 long offset = sc_val_to_long(val2);
1717 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1718 do_shr(val1, calc_buffer, offset, bitsize, sign, 1);
1720 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1722 if ((buffer != NULL) && (buffer != calc_buffer)) {
1723 memmove(buffer, calc_buffer, calc_buffer_size);
1727 void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1728 long offset = sc_val_to_long(val2);
1732 DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1733 do_rotl(val1, calc_buffer, offset, bitsize, sign);
1735 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1737 if ((buffer != NULL) && (buffer != calc_buffer)) {
1738 memmove(buffer, calc_buffer, calc_buffer_size);
1742 void sc_zero(void *buffer) {
1744 buffer = calc_buffer;
1745 CLEAR_BUFFER(buffer);