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
32 # include <string.h> /* memset/memcmp */
34 #include <assert.h> /* assertions */
35 #include <stdio.h> /* output for error messages */
36 #include <limits.h> /* definition of LONG_MIN, used in sc_get_val_from_long */
43 * local definitions and macros
45 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, calc_buffer_size)
46 #define _val(a) ((a)-SC_0)
47 #define _digit(a) ((a)+SC_0)
48 #define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
50 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__, __LINE__)
52 /* shortcut output for debugging */
53 # define sc_print_hex(a) sc_print((a), 0, SC_HEX, 0)
54 # define sc_print_dec(a) sc_print((a), 0, SC_DEC, 1)
55 # define sc_print_oct(a) sc_print((a), 0, SC_OCT, 0)
56 # define sc_print_bin(a) sc_print((a), 0, SC_BIN, 0)
58 #ifdef STRCALC_DEBUG_PRINTCOMP
59 # define DEBUGPRINTF_COMPUTATION(x) printf x
61 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
64 # define DEBUGPRINTF(x) printf x
66 # define DEBUGPRINTF(x) ((void)0)
73 static char *calc_buffer = NULL; /* buffer holding all results */
74 static char *output_buffer = NULL; /* buffer for output */
75 static int bit_pattern_size; /* maximum number of bits */
76 static int calc_buffer_size; /* size of internally stored values */
77 static int max_value_size; /* maximum size of values */
79 static int carry_flag; /**< some computation set the carry_flag:
80 - right shift if bits were lost due to shifting
81 - division if there was a remainder
82 However, the meaning of carry is machine dependent
83 and often defined in other ways! */
85 static const char sex_digit[4] = { SC_E, SC_C, SC_8, SC_0 };
86 static const char zex_digit[4] = { SC_1, SC_3, SC_7, SC_F };
87 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
88 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
90 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
91 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
93 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
95 static const char and_table[16][16] = {
96 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
97 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
99 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
100 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
102 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
103 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
105 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
106 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
108 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
109 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
111 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
112 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
114 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
115 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
117 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
118 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
120 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
121 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
123 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
124 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
126 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
127 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
129 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
130 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
132 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
133 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
135 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
136 SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
138 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
139 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
141 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
142 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
144 static const char or_table[16][16] = {
145 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
146 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
148 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
149 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
151 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
152 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
154 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
155 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
157 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
158 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
160 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
161 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
163 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
164 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
166 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
167 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
169 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
170 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
172 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
173 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
175 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
176 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
178 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
179 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
181 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
182 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
184 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
185 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
187 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
188 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
190 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
191 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
193 static char const xor_table[16][16] = {
194 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
195 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
197 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
198 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
200 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
201 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
203 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
204 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
206 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
207 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
209 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
210 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
212 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
213 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
215 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
216 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
218 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
219 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
221 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
222 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
224 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
225 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
227 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
228 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
230 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
231 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
233 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
234 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
236 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
237 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
239 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
240 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
243 static char const add_table[16][16][2] = {
244 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
245 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
246 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
247 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
249 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
250 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
251 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
252 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
254 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
255 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
256 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
257 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
259 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
260 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
261 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
262 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
264 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
265 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
266 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
267 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
269 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
270 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
271 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
272 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
274 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
275 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
276 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
277 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
279 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
280 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
281 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
282 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
284 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
285 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
286 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
287 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
289 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
290 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
291 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
292 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
294 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
295 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
296 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
297 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
299 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
300 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
301 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
302 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
304 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
305 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
306 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
307 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
309 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
310 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
311 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
312 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
314 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
315 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
316 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
317 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
319 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
320 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
321 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
322 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
325 static char const mul_table[16][16][2] = {
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},
328 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
329 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
331 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
332 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
333 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
334 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
336 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
337 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
338 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
339 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
341 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
342 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
343 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
344 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
346 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
347 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
348 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
349 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
351 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
352 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
353 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
354 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
356 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
357 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
358 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
359 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
361 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
362 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
363 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
364 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
366 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
367 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
368 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
369 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
371 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
372 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
373 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
374 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
376 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
377 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
378 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
379 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
381 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
382 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
383 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
384 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
386 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
387 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
388 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
389 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
391 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
392 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
393 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
394 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
396 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
397 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
398 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
399 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
401 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
402 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
403 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
404 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
407 static char const shrs_table[16][4][2] = {
408 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
409 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
410 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
411 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
412 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
413 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
414 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
415 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
416 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
417 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
418 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
419 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
420 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
421 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
422 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
423 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
426 /** converting a digit to a binary string */
427 static const char *binary_table[16] = {
428 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
429 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
432 /*****************************************************************************
434 *****************************************************************************/
435 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
436 const char *file, int line) {
438 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
439 while (len-- && *str) printf("%c", *str++); printf("\n");
440 while (--pos) printf(" "); printf("^\n");
445 * implements the bitwise NOT operation
447 static void do_bitnot(const char *val, char *buffer) {
450 for (counter = 0; counter<calc_buffer_size; counter++)
451 buffer[counter] = not_table[_val(val[counter])];
455 * implements the bitwise OR operation
457 static void do_bitor(const char *val1, const char *val2, char *buffer) {
460 for (counter = 0; counter<calc_buffer_size; counter++)
461 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
465 * implements the bitwise eXclusive OR operation
467 static void do_bitxor(const char *val1, const char *val2, char *buffer) {
470 for (counter = 0; counter<calc_buffer_size; counter++)
471 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
475 * implements the bitwise AND operation
477 static void do_bitand(const char *val1, const char *val2, char *buffer) {
480 for (counter = 0; counter<calc_buffer_size; counter++)
481 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
485 * returns the sign bit.
487 * @todo This implementation is wrong, as it returns the highest bit of the buffer
488 * NOT the highest bit depending on the real mode
490 static int do_sign(const char *val) {
491 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
495 * returns non-zero if bit at position pos is set
497 static int do_bit(const char *val, int pos) {
499 int nibble = pos >> 2;
501 return _bitisset(val[nibble], bit);
505 * Implements a fast ADD + 1
507 static void do_inc(const char *val, char *buffer) {
510 while (counter++ < calc_buffer_size) {
515 /* No carry here, *val != SC_F */
516 *buffer = add_table[_val(*val)][SC_1][0];
520 /* here a carry could be lost, this is intended because this should
521 * happen only when a value changes sign. */
525 * Implements a unary MINUS
527 static void do_negate(const char *val, char *buffer) {
528 do_bitnot(val, buffer);
529 do_inc(buffer, buffer);
533 * Implements a binary ADD
535 * @todo The implementation of carry is wrong, as it is the
536 * calc_buffer_size carry, not the mode depending
538 static void do_add(const char *val1, const char *val2, char *buffer) {
540 const char *add1, *add2;
543 for (counter = 0; counter < calc_buffer_size; counter++) {
544 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
545 add2 = add_table[_val(add1[0])][_val(carry)];
546 /* carry might be zero */
547 buffer[counter] = add2[0];
548 carry = add_table[_val(add1[1])][_val(add2[1])][0];
550 carry_flag = carry != SC_0;
554 * Implements a binary SUB
556 static void do_sub(const char *val1, const char *val2, char *buffer) {
557 char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
559 do_negate(val2, temp_buffer);
560 do_add(val1, temp_buffer, buffer);
564 * Implements a binary MUL
566 static void do_mul(const char *val1, const char *val2, char *buffer) {
567 char *temp_buffer; /* result buffer */
568 char *neg_val1; /* abs of val1 */
569 char *neg_val2; /* abs of val2 */
571 const char *mul, *add1, *add2; /* intermediate result containers */
572 char carry = SC_0; /* container for carries */
573 char sign = 0; /* marks result sign */
574 int c_inner, c_outer; /* loop counters */
576 temp_buffer = alloca(calc_buffer_size);
577 neg_val1 = alloca(calc_buffer_size);
578 neg_val2 = alloca(calc_buffer_size);
580 /* init result buffer to zeros */
581 memset(temp_buffer, SC_0, calc_buffer_size);
583 /* the multiplication works only for positive values, for negative values *
584 * it is necessary to negate them and adjust the result accordingly */
585 if (do_sign(val1) == -1) {
586 do_negate(val1, neg_val1);
590 if (do_sign(val2) == -1) {
591 do_negate(val2, neg_val2);
596 for (c_outer = 0; c_outer < max_value_size; c_outer++) {
597 if (val2[c_outer] != SC_0) {
598 for (c_inner = 0; c_inner < max_value_size; c_inner++) {
599 /* do the following calculation: *
600 * Add the current carry, the value at position c_outer+c_inner *
601 * and the result of the multiplication of val1[c_inner] and *
602 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
604 /* multiplicate the two digits */
605 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
606 /* add old value to result of multiplication */
607 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
608 /* add carry to the sum */
609 add2 = add_table[_val(add1[0])][_val(carry)];
611 /* all carries together result in new carry. This is always smaller *
613 * Both multiplicands, the carry and the value already in the temp *
614 * buffer are single digits and their value is therefore at most *
617 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
618 * The tables list all operations rem b, so the carry is at most *
619 * (b*b-1)rem b = -1rem b = b-1 */
620 carry = add_table[_val(mul[1])][_val(add1[1])][0];
621 carry = add_table[_val(carry)][_val(add2[1])][0];
623 temp_buffer[c_inner + c_outer] = add2[0];
626 /* A carry may hang over */
627 /* c_outer is always smaller than max_value_size! */
628 temp_buffer[max_value_size + c_outer] = carry;
634 do_negate(temp_buffer, buffer);
636 memcpy(buffer, temp_buffer, calc_buffer_size);
640 * Shift the buffer to left and add a 4 bit digit
642 static void do_push(const char digit, char *buffer) {
645 for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
646 buffer[counter+1] = buffer[counter];
652 * Implements truncating integer division and remainder.
654 * Note: This is MOST slow
656 static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem) {
657 const char *dividend = rDividend;
658 const char *minus_divisor;
662 char div_sign = 0; /* remember division result sign */
663 char rem_sign = 0; /* remember remainder result sign */
665 int c_dividend; /* loop counters */
667 neg_val1 = alloca(calc_buffer_size);
668 neg_val2 = alloca(calc_buffer_size);
670 /* clear result buffer */
671 memset(quot, SC_0, calc_buffer_size);
672 memset(rem, SC_0, calc_buffer_size);
674 /* if the divisor is zero this won't work (quot is zero) */
675 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
677 /* if the dividend is zero result is zero (quot is zero) */
678 if (sc_comp(dividend, quot) == 0)
681 if (do_sign(dividend) == -1) {
682 do_negate(dividend, neg_val1);
688 do_negate(divisor, neg_val2);
689 if (do_sign(divisor) == -1) {
691 minus_divisor = divisor;
694 minus_divisor = neg_val2;
696 /* if divisor >= dividend division is easy
697 * (remember these are absolute values) */
698 switch (sc_comp(dividend, divisor)) {
699 case 0: /* dividend == divisor */
703 case -1: /* dividend < divisor */
704 memcpy(rem, rDividend, calc_buffer_size);
707 default: /* unluckily division is necessary :( */
711 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
712 do_push(dividend[c_dividend], rem);
715 if (sc_comp(rem, divisor) != -1) { /* remainder >= divisor */
716 /* subtract until the remainder becomes negative, this should
717 * be faster than comparing remainder with divisor */
718 do_add(rem, minus_divisor, rem);
720 while (do_sign(rem) == 1) {
721 quot[0] = add_table[_val(quot[0])][SC_1][0];
722 do_add(rem, minus_divisor, rem);
725 /* subtracted one too much */
726 do_add(rem, divisor, rem);
730 /* sets carry if remainder is non-zero ??? */
731 carry_flag = !sc_is_zero(rem);
734 do_negate(quot, quot);
741 * Implements a Shift Left, which can either preserve the sign bit
744 * @todo Assertions seems to be wrong
746 static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed) {
754 assert((shift_cnt >= 0) || (0 && "negative leftshift"));
755 assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
756 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
757 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
759 /* if shifting far enough the result is zero */
760 if (shift_cnt >= bitsize) {
761 memset(buffer, SC_0, calc_buffer_size);
765 shift = shift_table[_val(shift_cnt%4)]; /* this is 2 ** (offset % 4) */
766 shift_cnt = shift_cnt / 4;
768 /* shift the single digits some bytes (offset) and some bits (table)
770 for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
771 shl = mul_table[_val(val1[counter])][_val(shift)];
772 buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
776 shl = mul_table[_val(val1[counter])][_val(shift)];
777 buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
780 bitoffset = counter - 1;
783 /* fill with zeroes */
784 for (counter = 0; counter < shift_cnt; counter++)
785 buffer[counter] = SC_0;
787 /* if the mode was signed, change sign when the mode's msb is now 1 */
788 shift_cnt = bitoffset + shift_cnt;
789 bitoffset = (bitsize-1) % 4;
790 if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
791 /* this sets the upper bits of the leftmost digit */
792 buffer[shift_cnt] = or_table[_val(buffer[shift_cnt])][_val(min_digit[bitoffset])];
793 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
794 buffer[counter] = SC_F;
796 } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
797 /* this clears the upper bits of the leftmost digit */
798 buffer[shift_cnt] = and_table[_val(buffer[shift_cnt])][_val(max_digit[bitoffset])];
799 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
800 buffer[counter] = SC_0;
806 * Implements a Shift Right, which can either preserve the sign bit
809 * @param bitsize bitsize of the value to be shifted
811 * @todo Assertions seems to be wrong
813 static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift) {
818 int shift_mod, shift_nib;
823 assert((shift_cnt >= 0) || (0 && "negative rightshift"));
824 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
825 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
827 sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
829 /* if shifting far enough the result is either 0 or -1 */
830 if (shift_cnt >= bitsize) {
831 if (!sc_is_zero(val1)) {
834 memset(buffer, sign, calc_buffer_size);
838 shift_mod = shift_cnt & 3;
839 shift_nib = shift_cnt >> 2;
841 /* check if any bits are lost, and set carry_flag if so */
842 for (counter = 0; counter < shift_nib; ++counter) {
843 if (val1[counter] != 0) {
848 if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
851 /* shift digits to the right with offset, carry and all */
853 if ((bitsize >> 2) > shift_nib) {
854 buffer[counter] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
857 for (; counter < bitsize/4 - shift_nib; counter++) {
858 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
859 buffer[counter] = shrs[0];
860 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
863 /* the last digit is special in regard of signed/unsigned shift */
864 bitoffset = bitsize & 3;
865 msd = sign; /* most significant digit */
867 /* remove sign bits if mode was signed and this is an unsigned shift */
868 if (!signed_shift && is_signed) {
869 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
872 shrs = shrs_table[_val(msd)][shift_mod];
874 /* signed shift and signed mode and negative value means all bits to the left are set */
875 if (signed_shift && sign == SC_F) {
876 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
878 buffer[counter] = shrs[0];
882 buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
884 /* fill with SC_F or SC_0 depending on sign */
885 for (counter++; counter < calc_buffer_size; counter++) {
886 buffer[counter] = sign;
891 * Implements a Rotate Left.
892 * positive: low-order -> high order, negative other direction
894 static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed) {
896 temp1 = alloca(calc_buffer_size);
897 temp2 = alloca(calc_buffer_size);
899 offset = offset % radius;
901 /* rotation by multiples of the type length is identity */
903 memmove(buffer, val1, calc_buffer_size);
907 do_shl(val1, temp1, offset, radius, is_signed);
908 do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
909 do_bitor(temp1, temp2, buffer);
910 carry_flag = 0; /* set by shr, but due to rot this is false */
913 /*****************************************************************************
914 * public functions, declared in strcalc.h
915 *****************************************************************************/
916 const void *sc_get_buffer(void) {
917 return (void*)calc_buffer;
920 int sc_get_buffer_length(void) {
921 return calc_buffer_size;
925 * Do sign extension if the mode is signed, otherwise to zero extension.
927 void sign_extend(void *buffer, ir_mode *mode) {
928 char *calc_buffer = buffer;
929 int bits = get_mode_size_bits(mode) - 1;
930 int nibble = bits >> 2;
931 int max = max_digit[bits & 3];
934 if (mode_is_signed(mode)) {
935 if (calc_buffer[nibble] > max) {
936 /* sign bit is set, we need sign expansion */
938 for (i = nibble + 1; i < calc_buffer_size; ++i)
939 calc_buffer[i] = SC_F;
940 calc_buffer[nibble] = or_table[(int)calc_buffer[nibble]][(int)sex_digit[bits & 3]];
942 /* set all bits to zero */
943 for (i = nibble + 1; i < calc_buffer_size; ++i)
944 calc_buffer[i] = SC_0;
945 calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
948 /* do zero extension */
949 for (i = nibble + 1; i < calc_buffer_size; ++i)
950 calc_buffer[i] = SC_0;
951 calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
955 /* FIXME doesn't check for overflows */
956 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode) {
957 const char *orig_str = str;
958 unsigned int orig_len = len;
963 base = alloca(calc_buffer_size);
964 val = alloca(calc_buffer_size);
966 /* verify valid pointers (not null) */
968 /* a string no characters long is an error */
971 if (buffer == NULL) buffer = calc_buffer;
973 CLEAR_BUFFER(buffer);
977 /* strip leading spaces */
978 while ((len > 0) && (*str == ' ')) { len--; str++; }
980 /* if the first two characters are 0x or 0X -> hex
981 * if the first is a 0 -> oct
982 * else dec, strip leading -/+ and remember sign
984 * only a + or - sign is no number resulting in an error */
988 if (str[1] == 'x' || str[1] == 'X') { /* hex */
991 base[1] = SC_1; base[0] = SC_0;
995 base[1] = SC_0; base[0] = SC_8;
1002 base[1] = SC_0; base[0] = SC_A;
1009 base[1] = SC_0; base[0] = SC_A;
1012 default: /* dec, else would have begun with 0x or 0 */
1013 base[1] = SC_0; base[0] = SC_A;
1015 } else { /* dec, else would have begun with 0x or 0 */
1016 base[1] = SC_0; base[0] = SC_A;
1019 /* BEGIN string evaluation, from left to right */
1028 if (base[0] > SC_9 || base[1] > SC_0) { /* (base > 10) */
1029 val[0] = _digit((*str)-'a'+10);
1032 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1041 if (base[0] > SC_9 || base[1] > SC_0) { /* (base > 10) */
1042 val[0] = _digit((*str)-'A'+10);
1045 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1050 if (base[0] > SC_7 || base[1] > SC_0) { /* (base > 8) */
1051 val[0] = _digit((*str)-'0');
1054 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1065 val[0] = _digit((*str)-'0');
1069 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1070 } /* switch(*str) */
1072 /* Radix conversion from base b to base B:
1073 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1074 do_mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1075 do_add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1077 /* get ready for the next letter */
1080 } /* while (len > 0 ) */
1083 do_negate(calc_buffer, calc_buffer);
1085 /* beware: even if hex numbers have no sign, we need sign extension here */
1086 sign_extend(calc_buffer, mode);
1089 void sc_val_from_long(long value, void *buffer) {
1091 char sign, is_minlong;
1093 if (buffer == NULL) buffer = calc_buffer;
1097 is_minlong = value == LONG_MIN;
1099 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
1107 CLEAR_BUFFER(buffer);
1109 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
1110 *pos++ = _digit(value & 0xf);
1116 do_inc(buffer, buffer);
1118 do_negate(buffer, buffer);
1122 void sc_val_from_ulong(unsigned long value, void *buffer) {
1125 if (buffer == NULL) buffer = calc_buffer;
1128 while (pos < (unsigned char *)buffer + calc_buffer_size) {
1129 *pos++ = (unsigned char)_digit(value & 0xf);
1134 long sc_val_to_long(const void *val) {
1138 for (i = calc_buffer_size - 1; i >= 0; i--) {
1139 l = (l << 4) + _val(((char *)val)[i]);
1144 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1148 if (buffer == NULL) buffer = calc_buffer;
1149 CLEAR_BUFFER(buffer);
1151 if (!sign) return; /* unsigned means minimum is 0(zero) */
1155 bits = num_bits - 1;
1156 for (i = 0; i < bits/4; i++)
1159 *pos++ = min_digit[bits%4];
1161 for (i++; i <= calc_buffer_size - 1; i++)
1165 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1169 if (buffer == NULL) buffer = calc_buffer;
1170 CLEAR_BUFFER(buffer);
1173 bits = num_bits - sign;
1174 for (i = 0; i < bits/4; i++)
1177 *pos++ = max_digit[bits%4];
1179 for (i++; i <= calc_buffer_size - 1; i++)
1183 void sc_truncate(unsigned int num_bits, void *buffer) {
1184 char *cbuffer = buffer;
1185 char *pos = cbuffer + (num_bits / 4);
1186 char *end = cbuffer + calc_buffer_size;
1190 switch(num_bits % 4) {
1191 case 0: /* nothing to do */ break;
1192 case 1: *pos = and_table[_val(*pos)][SC_1]; pos++; break;
1193 case 2: *pos = and_table[_val(*pos)][SC_3]; pos++; break;
1194 case 3: *pos = and_table[_val(*pos)][SC_7]; pos++; break;
1197 for( ; pos < end; ++pos)
1201 int sc_comp(const void* value1, const void* value2) {
1202 int counter = calc_buffer_size - 1;
1203 const char *val1 = (const char *)value1;
1204 const char *val2 = (const char *)value2;
1206 /* compare signs first:
1207 * the loop below can only compare values of the same sign! */
1208 if (do_sign(val1) != do_sign(val2))
1209 return (do_sign(val1) == 1)?(1):(-1);
1211 /* loop until two digits differ, the values are equal if there
1212 * are no such two digits */
1213 while (val1[counter] == val2[counter]) {
1215 if (counter < 0) return 0;
1218 /* the leftmost digit is the most significant, so this returns
1219 * the correct result.
1220 * This implies the digit enum is ordered */
1221 return (val1[counter] > val2[counter]) ? (1) : (-1);
1224 int sc_get_highest_set_bit(const void *value) {
1225 const char *val = (const char*)value;
1228 high = calc_buffer_size * 4 - 1;
1230 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1231 if (val[counter] == SC_0)
1234 if (val[counter] > SC_7) return high;
1235 else if (val[counter] > SC_3) return high - 1;
1236 else if (val[counter] > SC_1) return high - 2;
1237 else return high - 3;
1243 int sc_get_lowest_set_bit(const void *value) {
1244 const char *val = (const char*)value;
1248 for (counter = 0; counter < calc_buffer_size; counter++) {
1249 switch (val[counter]) {
1276 int sc_get_bit_at(const void *value, unsigned pos) {
1277 const char *val = value;
1278 unsigned nibble = pos >> 2;
1280 if (and_table[(int) val[nibble]][(int) shift_table[pos & 3]] != SC_0)
1285 int sc_is_zero(const void *value) {
1286 const char* val = (const char *)value;
1289 for (counter = 0; counter < calc_buffer_size; ++counter) {
1290 if (val[counter] != SC_0)
1296 int sc_is_negative(const void *value) {
1297 return do_sign(value) == -1;
1300 int sc_had_carry(void) {
1304 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs) {
1305 const char *val = (const char *)value;
1306 int nibble_ofs = 2 * byte_ofs;
1309 /* the current scheme uses one byte to store a nibble */
1310 if (4 * nibble_ofs >= len)
1313 res = _val(val[nibble_ofs]);
1314 if (len > 4 * (nibble_ofs + 1))
1315 res |= _val(val[nibble_ofs + 1]) << 4;
1317 /* kick bits outsize */
1318 if (len - 8 * byte_ofs < 8) {
1319 res &= (1 << (len - 8 * byte_ofs)) - 1;
1325 * convert to a string
1326 * FIXME: Doesn't check buffer bounds
1328 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode) {
1329 static const char big_digits[] = "0123456789ABCDEF";
1330 static const char small_digits[] = "0123456789abcdef";
1332 char *base_val, *div1_res, *div2_res, *rem_res;
1333 int counter, nibbles, i, sign, mask;
1336 const char *val = (const char *)value;
1340 const char *digits = small_digits;
1342 base_val = alloca(calc_buffer_size);
1343 div1_res = alloca(calc_buffer_size);
1344 div2_res = alloca(calc_buffer_size);
1345 rem_res = alloca(calc_buffer_size);
1347 pos = output_buffer + bit_pattern_size;
1352 bits = bit_pattern_size;
1353 #ifdef STRCALC_DEBUG_FULLPRINT
1357 nibbles = bits >> 2;
1361 digits = big_digits;
1363 for (counter = 0; counter < nibbles; ++counter) {
1364 *(--pos) = digits[_val(val[counter])];
1365 #ifdef STRCALC_DEBUG_GROUPPRINT
1366 if ((counter+1)%8 == 0)
1371 /* last nibble must be masked */
1373 mask = zex_digit[bits & 3];
1374 x = and_table[_val(val[counter++])][mask];
1375 *(--pos) = digits[_val(x)];
1378 /* now kill zeros */
1379 for (; counter > 1; --counter, ++pos) {
1380 #ifdef STRCALC_DEBUG_GROUPPRINT
1381 if (pos[0] == ' ') ++pos;
1389 for (counter = 0; counter < nibbles; ++counter) {
1391 p = binary_table[_val(val[counter])];
1398 /* last nibble must be masked */
1400 mask = zex_digit[bits & 3];
1401 x = and_table[_val(val[counter++])][mask];
1404 p = binary_table[_val(x)];
1411 /* now kill zeros */
1412 for (counter <<= 2; counter > 1; --counter, ++pos)
1419 memset(base_val, SC_0, calc_buffer_size);
1420 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1424 if (signed_mode && base == SC_DEC) {
1425 /* check for negative values */
1426 if (do_bit(val, bits - 1)) {
1427 do_negate(val, div2_res);
1433 /* transfer data into oscillating buffers */
1434 memset(div1_res, SC_0, calc_buffer_size);
1435 for (counter = 0; counter < nibbles; ++counter)
1436 div1_res[counter] = p[counter];
1438 /* last nibble must be masked */
1440 mask = zex_digit[bits & 3];
1441 div1_res[counter] = and_table[_val(p[counter])][mask];
1448 do_divmod(m, base_val, n, rem_res);
1452 *(--pos) = digits[_val(rem_res[0])];
1455 for (i = 0; i < calc_buffer_size; ++i)
1466 panic("Unsupported base %d", base);
1471 void init_strcalc(int precision) {
1472 if (calc_buffer == NULL) {
1473 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1475 /* round up to multiple of 4 */
1476 precision = (precision + 3) & ~3;
1478 bit_pattern_size = (precision);
1479 calc_buffer_size = (precision / 2);
1480 max_value_size = (precision / 4);
1482 calc_buffer = XMALLOCN(char, calc_buffer_size + 1);
1483 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1485 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));
1490 void finish_strcalc(void) {
1491 free(calc_buffer); calc_buffer = NULL;
1492 free(output_buffer); output_buffer = NULL;
1495 int sc_get_precision(void) {
1496 return bit_pattern_size;
1500 void sc_add(const void *value1, const void *value2, void *buffer) {
1501 CLEAR_BUFFER(calc_buffer);
1504 DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1505 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1507 do_add(value1, value2, calc_buffer);
1509 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1511 if ((buffer != NULL) && (buffer != calc_buffer)) {
1512 memcpy(buffer, calc_buffer, calc_buffer_size);
1516 void sc_sub(const void *value1, const void *value2, void *buffer) {
1517 CLEAR_BUFFER(calc_buffer);
1520 DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1521 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1523 do_sub(value1, value2, calc_buffer);
1525 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1527 if ((buffer != NULL) && (buffer != calc_buffer)) {
1528 memcpy(buffer, calc_buffer, calc_buffer_size);
1532 void sc_neg(const void *value1, void *buffer) {
1535 DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1537 do_negate(value1, calc_buffer);
1539 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1541 if ((buffer != NULL) && (buffer != calc_buffer)) {
1542 memcpy(buffer, calc_buffer, calc_buffer_size);
1546 void sc_and(const void *value1, const void *value2, void *buffer) {
1547 CLEAR_BUFFER(calc_buffer);
1550 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1551 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1553 do_bitand(value1, value2, calc_buffer);
1555 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1557 if ((buffer != NULL) && (buffer != calc_buffer)) {
1558 memcpy(buffer, calc_buffer, calc_buffer_size);
1562 void sc_or(const void *value1, const void *value2, void *buffer) {
1563 CLEAR_BUFFER(calc_buffer);
1566 DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1567 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1569 do_bitor(value1, value2, calc_buffer);
1571 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1573 if ((buffer != NULL) && (buffer != calc_buffer)) {
1574 memcpy(buffer, calc_buffer, calc_buffer_size);
1578 void sc_xor(const void *value1, const void *value2, void *buffer) {
1579 CLEAR_BUFFER(calc_buffer);
1582 DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1583 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1585 do_bitxor(value1, value2, calc_buffer);
1587 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1589 if ((buffer != NULL) && (buffer != calc_buffer)) {
1590 memcpy(buffer, calc_buffer, calc_buffer_size);
1594 void sc_not(const void *value1, void *buffer) {
1595 CLEAR_BUFFER(calc_buffer);
1598 DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1600 do_bitnot(value1, calc_buffer);
1602 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1604 if ((buffer != NULL) && (buffer != calc_buffer)) {
1605 memcpy(buffer, calc_buffer, calc_buffer_size);
1609 void sc_mul(const void *value1, const void *value2, void *buffer) {
1610 CLEAR_BUFFER(calc_buffer);
1613 DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1614 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1616 do_mul(value1, value2, calc_buffer);
1618 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1620 if ((buffer != NULL) && (buffer != calc_buffer)) {
1621 memcpy(buffer, calc_buffer, calc_buffer_size);
1625 void sc_div(const void *value1, const void *value2, void *buffer) {
1626 /* temp buffer holding unused result of divmod */
1627 char *unused_res = alloca(calc_buffer_size);
1629 CLEAR_BUFFER(calc_buffer);
1632 DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1633 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1635 do_divmod(value1, value2, calc_buffer, unused_res);
1637 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1639 if ((buffer != NULL) && (buffer != calc_buffer)) {
1640 memcpy(buffer, calc_buffer, calc_buffer_size);
1644 void sc_mod(const void *value1, const void *value2, void *buffer) {
1645 /* temp buffer holding unused result of divmod */
1646 char *unused_res = alloca(calc_buffer_size);
1648 CLEAR_BUFFER(calc_buffer);
1651 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1652 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1654 do_divmod(value1, value2, unused_res, calc_buffer);
1656 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1658 if ((buffer != NULL) && (buffer != calc_buffer)) {
1659 memcpy(buffer, calc_buffer, calc_buffer_size);
1663 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer) {
1664 CLEAR_BUFFER(calc_buffer);
1667 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1668 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1670 do_divmod(value1, value2, div_buffer, mod_buffer);
1672 DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1676 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1679 DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1680 do_shl(val1, calc_buffer, shift_cnt, bitsize, sign);
1682 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1684 if ((buffer != NULL) && (buffer != calc_buffer)) {
1685 memmove(buffer, calc_buffer, calc_buffer_size);
1689 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1690 long offset = sc_val_to_long(val2);
1692 sc_shlI(val1, offset, bitsize, sign, buffer);
1695 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1698 DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1699 do_shr(val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1701 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1703 if ((buffer != NULL) && (buffer != calc_buffer)) {
1704 memmove(buffer, calc_buffer, calc_buffer_size);
1708 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1709 long shift_cnt = sc_val_to_long(val2);
1711 sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1714 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1715 long offset = sc_val_to_long(val2);
1719 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1720 do_shr(val1, calc_buffer, offset, bitsize, sign, 1);
1722 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1724 if ((buffer != NULL) && (buffer != calc_buffer)) {
1725 memmove(buffer, calc_buffer, calc_buffer_size);
1729 void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1730 long offset = sc_val_to_long(val2);
1734 DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1735 do_rotl(val1, calc_buffer, offset, bitsize, sign);
1737 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1739 if ((buffer != NULL) && (buffer != calc_buffer)) {
1740 memmove(buffer, calc_buffer, calc_buffer_size);
1744 void sc_zero(void *buffer) {
1746 buffer = calc_buffer;
1747 CLEAR_BUFFER(buffer);