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) ((digit & 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 or_table[16][16] = {
90 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
91 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
93 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
94 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
96 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
97 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
99 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
100 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
102 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
103 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
105 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
106 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
108 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
109 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
111 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
112 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
114 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
115 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
117 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
118 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
120 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
121 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
123 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
124 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
126 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
127 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
129 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
130 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
132 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
133 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
135 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
136 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
138 static char const add_table[16][16][2] = {
139 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
140 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
141 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
142 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
144 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
145 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
146 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
147 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
149 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
150 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
151 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
152 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
154 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
155 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
156 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
157 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
159 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
160 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
161 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
162 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
164 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
165 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
166 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
167 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
169 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
170 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
171 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
172 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
174 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
175 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
176 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
177 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
179 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
180 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
181 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
182 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
184 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
185 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
186 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
187 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
189 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
190 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
191 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
192 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
194 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
195 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
196 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
197 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
199 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
200 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
201 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
202 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
204 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
205 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
206 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
207 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
209 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
210 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
211 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
212 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
214 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
215 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
216 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
217 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
220 static char const mul_table[16][16][2] = {
221 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
222 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
223 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
224 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
226 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
227 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
228 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
229 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
231 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
232 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
233 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
234 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
236 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
237 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
238 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
239 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
241 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
242 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
243 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
244 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
246 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
247 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
248 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
249 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
251 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
252 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
253 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
254 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
256 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
257 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
258 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
259 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
261 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
262 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
263 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
264 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
266 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
267 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
268 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
269 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
271 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
272 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
273 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
274 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
276 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
277 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
278 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
279 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
281 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
282 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
283 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
284 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
286 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
287 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
288 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
289 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
291 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
292 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
293 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
294 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
296 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
297 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
298 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
299 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
302 static char const shrs_table[16][4][2] = {
303 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
304 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
305 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
306 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
307 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
308 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
309 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
310 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
311 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
312 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
313 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
314 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
315 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
316 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
317 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
318 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
321 /** converting a digit to a binary string */
322 static const char *binary_table[16] = {
323 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
324 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
327 /*****************************************************************************
329 *****************************************************************************/
330 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
331 const char *file, int line) {
333 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
334 while (len-- && *str) printf("%c", *str++); printf("\n");
335 while (--pos) printf(" "); printf("^\n");
340 * implements the bitwise NOT operation
342 static void do_bitnot(const char *val, char *buffer) {
345 for (counter = 0; counter<calc_buffer_size; counter++)
346 buffer[counter] = val[counter] ^ SC_F;
350 * implements the bitwise OR operation
352 static void do_bitor(const char *val1, const char *val2, char *buffer) {
355 for (counter = 0; counter<calc_buffer_size; counter++)
356 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
360 * implements the bitwise eXclusive OR operation
362 static void do_bitxor(const char *val1, const char *val2, char *buffer) {
365 for (counter = 0; counter<calc_buffer_size; counter++)
366 buffer[counter] = val1[counter] ^ val2[counter];
370 * implements the bitwise AND operation
372 static void do_bitand(const char *val1, const char *val2, char *buffer) {
375 for (counter = 0; counter<calc_buffer_size; counter++)
376 buffer[counter] = val1[counter] & val2[counter];
380 * returns the sign bit.
382 * @todo This implementation is wrong, as it returns the highest bit of the buffer
383 * NOT the highest bit depending on the real mode
385 static int do_sign(const char *val) {
386 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
390 * returns non-zero if bit at position pos is set
392 static int do_bit(const char *val, int pos) {
394 int nibble = pos >> 2;
396 return _bitisset(val[nibble], bit);
400 * Implements a fast ADD + 1
402 static void do_inc(const char *val, char *buffer) {
405 while (counter++ < calc_buffer_size) {
410 /* No carry here, *val != SC_F */
411 *buffer = add_table[_val(*val)][SC_1][0];
415 /* here a carry could be lost, this is intended because this should
416 * happen only when a value changes sign. */
420 * Implements a unary MINUS
422 static void do_negate(const char *val, char *buffer) {
423 do_bitnot(val, buffer);
424 do_inc(buffer, buffer);
428 * Implements a binary ADD
430 * @todo The implementation of carry is wrong, as it is the
431 * calc_buffer_size carry, not the mode depending
433 static void do_add(const char *val1, const char *val2, char *buffer) {
435 const char *add1, *add2;
438 for (counter = 0; counter < calc_buffer_size; counter++) {
439 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
440 add2 = add_table[_val(add1[0])][_val(carry)];
441 /* carry might be zero */
442 buffer[counter] = add2[0];
443 carry = add_table[_val(add1[1])][_val(add2[1])][0];
445 carry_flag = carry != SC_0;
449 * Implements a binary SUB
451 static void do_sub(const char *val1, const char *val2, char *buffer) {
452 char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
454 do_negate(val2, temp_buffer);
455 do_add(val1, temp_buffer, buffer);
459 * Implements a binary MUL
461 static void do_mul(const char *val1, const char *val2, char *buffer) {
462 char *temp_buffer; /* result buffer */
463 char *neg_val1; /* abs of val1 */
464 char *neg_val2; /* abs of val2 */
466 const char *mul, *add1, *add2; /* intermediate result containers */
467 char carry = SC_0; /* container for carries */
468 char sign = 0; /* marks result sign */
469 int c_inner, c_outer; /* loop counters */
471 temp_buffer = alloca(calc_buffer_size);
472 neg_val1 = alloca(calc_buffer_size);
473 neg_val2 = alloca(calc_buffer_size);
475 /* init result buffer to zeros */
476 memset(temp_buffer, SC_0, calc_buffer_size);
478 /* the multiplication works only for positive values, for negative values *
479 * it is necessary to negate them and adjust the result accordingly */
480 if (do_sign(val1) == -1) {
481 do_negate(val1, neg_val1);
485 if (do_sign(val2) == -1) {
486 do_negate(val2, neg_val2);
491 for (c_outer = 0; c_outer < max_value_size; c_outer++) {
492 if (val2[c_outer] != SC_0) {
493 for (c_inner = 0; c_inner < max_value_size; c_inner++) {
494 /* do the following calculation: *
495 * Add the current carry, the value at position c_outer+c_inner *
496 * and the result of the multiplication of val1[c_inner] and *
497 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
499 /* multiplicate the two digits */
500 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
501 /* add old value to result of multiplication */
502 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
503 /* add carry to the sum */
504 add2 = add_table[_val(add1[0])][_val(carry)];
506 /* all carries together result in new carry. This is always smaller *
508 * Both multiplicands, the carry and the value already in the temp *
509 * buffer are single digits and their value is therefore at most *
512 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
513 * The tables list all operations rem b, so the carry is at most *
514 * (b*b-1)rem b = -1rem b = b-1 */
515 carry = add_table[_val(mul[1])][_val(add1[1])][0];
516 carry = add_table[_val(carry)][_val(add2[1])][0];
518 temp_buffer[c_inner + c_outer] = add2[0];
521 /* A carry may hang over */
522 /* c_outer is always smaller than max_value_size! */
523 temp_buffer[max_value_size + c_outer] = carry;
529 do_negate(temp_buffer, buffer);
531 memcpy(buffer, temp_buffer, calc_buffer_size);
535 * Shift the buffer to left and add a 4 bit digit
537 static void do_push(const char digit, char *buffer) {
540 for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
541 buffer[counter+1] = buffer[counter];
547 * Implements truncating integer division and remainder.
549 * Note: This is MOST slow
551 static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem) {
552 const char *dividend = rDividend;
553 const char *minus_divisor;
557 char div_sign = 0; /* remember division result sign */
558 char rem_sign = 0; /* remember remainder result sign */
560 int c_dividend; /* loop counters */
562 neg_val1 = alloca(calc_buffer_size);
563 neg_val2 = alloca(calc_buffer_size);
565 /* clear result buffer */
566 memset(quot, SC_0, calc_buffer_size);
567 memset(rem, SC_0, calc_buffer_size);
569 /* if the divisor is zero this won't work (quot is zero) */
570 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
572 /* if the dividend is zero result is zero (quot is zero) */
573 if (sc_comp(dividend, quot) == 0)
576 if (do_sign(dividend) == -1) {
577 do_negate(dividend, neg_val1);
583 do_negate(divisor, neg_val2);
584 if (do_sign(divisor) == -1) {
586 minus_divisor = divisor;
589 minus_divisor = neg_val2;
591 /* if divisor >= dividend division is easy
592 * (remember these are absolute values) */
593 switch (sc_comp(dividend, divisor)) {
594 case 0: /* dividend == divisor */
598 case -1: /* dividend < divisor */
599 memcpy(rem, dividend, calc_buffer_size);
602 default: /* unluckily division is necessary :( */
606 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
607 do_push(dividend[c_dividend], rem);
610 if (sc_comp(rem, divisor) != -1) { /* remainder >= divisor */
611 /* subtract until the remainder becomes negative, this should
612 * be faster than comparing remainder with divisor */
613 do_add(rem, minus_divisor, rem);
615 while (do_sign(rem) == 1) {
616 quot[0] = add_table[_val(quot[0])][SC_1][0];
617 do_add(rem, minus_divisor, rem);
620 /* subtracted one too much */
621 do_add(rem, divisor, rem);
625 /* sets carry if remainder is non-zero ??? */
626 carry_flag = !sc_is_zero(rem);
629 do_negate(quot, quot);
636 * Implements a Shift Left, which can either preserve the sign bit
639 * @todo Assertions seems to be wrong
641 static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed) {
649 assert((shift_cnt >= 0) || (0 && "negative leftshift"));
650 assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
651 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
652 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
654 /* if shifting far enough the result is zero */
655 if (shift_cnt >= bitsize) {
656 memset(buffer, SC_0, calc_buffer_size);
660 shift = shift_table[_val(shift_cnt%4)]; /* this is 2 ** (offset % 4) */
661 shift_cnt = shift_cnt / 4;
663 /* shift the single digits some bytes (offset) and some bits (table)
665 for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
666 shl = mul_table[_val(val1[counter])][_val(shift)];
667 buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
671 shl = mul_table[_val(val1[counter])][_val(shift)];
672 buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
675 bitoffset = counter - 1;
678 /* fill with zeroes */
679 for (counter = 0; counter < shift_cnt; counter++)
680 buffer[counter] = SC_0;
682 /* if the mode was signed, change sign when the mode's msb is now 1 */
683 shift_cnt = bitoffset + shift_cnt;
684 bitoffset = (bitsize-1) % 4;
685 if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
686 /* this sets the upper bits of the leftmost digit */
687 buffer[shift_cnt] = or_table[_val(buffer[shift_cnt])][_val(min_digit[bitoffset])];
688 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
689 buffer[counter] = SC_F;
691 } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
692 /* this clears the upper bits of the leftmost digit */
693 buffer[shift_cnt] &= max_digit[bitoffset];
694 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
695 buffer[counter] = SC_0;
701 * Implements a Shift Right, which can either preserve the sign bit
704 * @param bitsize bitsize of the value to be shifted
706 * @todo Assertions seems to be wrong
708 static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift) {
713 int shift_mod, shift_nib;
718 assert((shift_cnt >= 0) || (0 && "negative rightshift"));
719 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
720 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
722 sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
724 /* if shifting far enough the result is either 0 or -1 */
725 if (shift_cnt >= bitsize) {
726 if (!sc_is_zero(val1)) {
729 memset(buffer, sign, calc_buffer_size);
733 shift_mod = shift_cnt & 3;
734 shift_nib = shift_cnt >> 2;
736 /* check if any bits are lost, and set carry_flag if so */
737 for (counter = 0; counter < shift_nib; ++counter) {
738 if (val1[counter] != 0) {
743 if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
746 /* shift digits to the right with offset, carry and all */
747 buffer[0] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
748 for (counter = 1; counter < ((bitsize + 3) >> 2) - shift_nib; counter++) {
749 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
750 buffer[counter] = shrs[0];
751 buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
754 /* the last digit is special in regard of signed/unsigned shift */
755 bitoffset = bitsize & 3;
756 msd = sign; /* most significant digit */
758 /* remove sign bits if mode was signed and this is an unsigned shift */
759 if (!signed_shift && is_signed) {
760 msd &= max_digit[bitoffset];
763 shrs = shrs_table[_val(msd)][shift_mod];
765 /* signed shift and signed mode and negative value means all bits to the left are set */
766 if (signed_shift && sign == SC_F) {
767 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
769 buffer[counter] = shrs[0];
773 buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
775 /* fill with SC_F or SC_0 depending on sign */
776 for (counter++; counter < calc_buffer_size; counter++) {
777 buffer[counter] = sign;
782 * Implements a Rotate Left.
783 * positive: low-order -> high order, negative other direction
785 static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed) {
787 temp1 = alloca(calc_buffer_size);
788 temp2 = alloca(calc_buffer_size);
790 offset = offset % radius;
792 /* rotation by multiples of the type length is identity */
794 memmove(buffer, val1, calc_buffer_size);
798 do_shl(val1, temp1, offset, radius, is_signed);
799 do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
800 do_bitor(temp1, temp2, buffer);
801 carry_flag = 0; /* set by shr, but due to rot this is false */
804 /*****************************************************************************
805 * public functions, declared in strcalc.h
806 *****************************************************************************/
807 const void *sc_get_buffer(void) {
808 return (void*)calc_buffer;
811 int sc_get_buffer_length(void) {
812 return calc_buffer_size;
816 * Do sign extension if the mode is signed, otherwise to zero extension.
818 void sign_extend(void *buffer, ir_mode *mode) {
819 char *calc_buffer = buffer;
820 int bits = get_mode_size_bits(mode) - 1;
821 int nibble = bits >> 2;
822 int max = max_digit[bits & 3];
825 if (mode_is_signed(mode)) {
826 if (calc_buffer[nibble] > max) {
827 /* sign bit is set, we need sign expansion */
829 for (i = nibble + 1; i < calc_buffer_size; ++i)
830 calc_buffer[i] = SC_F;
831 calc_buffer[nibble] = or_table[(int)calc_buffer[nibble]][(int)sex_digit[bits & 3]];
833 /* set all bits to zero */
834 for (i = nibble + 1; i < calc_buffer_size; ++i)
835 calc_buffer[i] = SC_0;
836 calc_buffer[nibble] &= zex_digit[bits & 3];
839 /* do zero extension */
840 for (i = nibble + 1; i < calc_buffer_size; ++i)
841 calc_buffer[i] = SC_0;
842 calc_buffer[nibble] &= zex_digit[bits & 3];
846 /* FIXME doesn't check for overflows */
847 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode) {
848 const char *orig_str = str;
849 unsigned int orig_len = len;
854 base = alloca(calc_buffer_size);
855 val = alloca(calc_buffer_size);
857 /* verify valid pointers (not null) */
859 /* a string no characters long is an error */
862 if (buffer == NULL) buffer = calc_buffer;
864 CLEAR_BUFFER(buffer);
868 /* strip leading spaces */
869 while ((len > 0) && (*str == ' ')) { len--; str++; }
871 /* if the first two characters are 0x or 0X -> hex
872 * if the first is a 0 -> oct
873 * else dec, strip leading -/+ and remember sign
875 * only a + or - sign is no number resulting in an error */
879 if (str[1] == 'x' || str[1] == 'X') { /* hex */
882 base[1] = SC_1; base[0] = SC_0;
886 base[1] = SC_0; base[0] = SC_8;
893 base[1] = SC_0; base[0] = SC_A;
900 base[1] = SC_0; base[0] = SC_A;
903 default: /* dec, else would have begun with 0x or 0 */
904 base[1] = SC_0; base[0] = SC_A;
906 } else { /* dec, else would have begun with 0x or 0 */
907 base[1] = SC_0; base[0] = SC_A;
910 /* BEGIN string evaluation, from left to right */
919 if (base[0] > SC_A || base[1] > SC_0) { /* (base > 10) */
920 val[0] = _digit((*str)-'a'+10);
923 fail_char(orig_str, orig_len, *str, str-orig_str+1);
932 if (base[0] > SC_A || base[1] > SC_0) { /* (base > 10) */
933 val[0] = _digit((*str)-'A'+10);
936 fail_char(orig_str, orig_len, *str, str-orig_str+1);
941 if (base[0] > SC_8 || base[1] > SC_0) { /* (base > 8) */
942 val[0] = _digit((*str)-'0');
945 fail_char(orig_str, orig_len, *str, str-orig_str+1);
956 val[0] = _digit((*str)-'0');
960 fail_char(orig_str, orig_len, *str, str-orig_str+1);
963 /* Radix conversion from base b to base B:
964 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
965 do_mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
966 do_add(val, calc_buffer, calc_buffer); /* add next digit to current value */
968 /* get ready for the next letter */
971 } /* while (len > 0 ) */
974 do_negate(calc_buffer, calc_buffer);
976 /* beware: even if hex numbers have no sign, we need sign extension here */
977 sign_extend(calc_buffer, mode);
980 void sc_val_from_long(long value, void *buffer) {
982 char sign, is_minlong;
984 if (buffer == NULL) buffer = calc_buffer;
988 is_minlong = value == LONG_MIN;
990 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
998 CLEAR_BUFFER(buffer);
1000 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
1001 *pos++ = _digit(value & 0xf);
1007 do_inc(buffer, buffer);
1009 do_negate(buffer, buffer);
1013 void sc_val_from_ulong(unsigned long value, void *buffer) {
1016 if (buffer == NULL) buffer = calc_buffer;
1019 while (pos < (unsigned char *)buffer + calc_buffer_size) {
1020 *pos++ = (unsigned char)_digit(value & 0xf);
1025 long sc_val_to_long(const void *val) {
1029 for (i = calc_buffer_size - 1; i >= 0; i--) {
1030 l = (l << 4) + _val(((char *)val)[i]);
1035 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1039 if (buffer == NULL) buffer = calc_buffer;
1040 CLEAR_BUFFER(buffer);
1042 if (!sign) return; /* unsigned means minimum is 0(zero) */
1046 bits = num_bits - 1;
1047 for (i = 0; i < bits/4; i++)
1050 *pos++ = min_digit[bits%4];
1052 for (i++; i <= calc_buffer_size - 1; i++)
1056 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1060 if (buffer == NULL) buffer = calc_buffer;
1061 CLEAR_BUFFER(buffer);
1064 bits = num_bits - sign;
1065 for (i = 0; i < bits/4; i++)
1068 *pos++ = max_digit[bits%4];
1070 for (i++; i <= calc_buffer_size - 1; i++)
1074 void sc_truncate(unsigned int num_bits, void *buffer) {
1075 char *cbuffer = buffer;
1076 char *pos = cbuffer + (num_bits / 4);
1077 char *end = cbuffer + calc_buffer_size;
1081 switch(num_bits % 4) {
1082 case 0: /* nothing to do */ break;
1083 case 1: *pos++ &= SC_1; break;
1084 case 2: *pos++ &= SC_3; break;
1085 case 3: *pos++ &= SC_7; break;
1088 for( ; pos < end; ++pos)
1092 int sc_comp(const void* value1, const void* value2) {
1093 int counter = calc_buffer_size - 1;
1094 const char *val1 = (const char *)value1;
1095 const char *val2 = (const char *)value2;
1097 /* compare signs first:
1098 * the loop below can only compare values of the same sign! */
1099 if (do_sign(val1) != do_sign(val2))
1100 return (do_sign(val1) == 1)?(1):(-1);
1102 /* loop until two digits differ, the values are equal if there
1103 * are no such two digits */
1104 while (val1[counter] == val2[counter]) {
1106 if (counter < 0) return 0;
1109 /* the leftmost digit is the most significant, so this returns
1110 * the correct result.
1111 * This implies the digit enum is ordered */
1112 return (val1[counter] > val2[counter]) ? (1) : (-1);
1115 int sc_get_highest_set_bit(const void *value) {
1116 const char *val = (const char*)value;
1119 high = calc_buffer_size * 4 - 1;
1121 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1122 if (val[counter] == SC_0)
1125 if (val[counter] > SC_7) return high;
1126 else if (val[counter] > SC_3) return high - 1;
1127 else if (val[counter] > SC_1) return high - 2;
1128 else return high - 3;
1134 int sc_get_lowest_set_bit(const void *value) {
1135 const char *val = (const char*)value;
1139 for (counter = 0; counter < calc_buffer_size; counter++) {
1140 switch (val[counter]) {
1167 int sc_get_bit_at(const void *value, unsigned pos) {
1168 const char *val = value;
1169 unsigned nibble = pos >> 2;
1171 return (val[nibble] & shift_table[pos & 3]) != SC_0;
1174 void sc_set_bit_at(void *value, unsigned pos)
1177 unsigned nibble = pos >> 2;
1179 val[nibble] = or_table[(int)val[nibble]][(int)shift_table[pos & 3]];
1182 int sc_is_zero(const void *value) {
1183 const char* val = (const char *)value;
1186 for (counter = 0; counter < calc_buffer_size; ++counter) {
1187 if (val[counter] != SC_0)
1193 int sc_is_negative(const void *value) {
1194 return do_sign(value) == -1;
1197 int sc_had_carry(void) {
1201 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs) {
1202 const char *val = (const char *)value;
1203 int nibble_ofs = 2 * byte_ofs;
1206 /* the current scheme uses one byte to store a nibble */
1207 if (4 * nibble_ofs >= len)
1210 res = _val(val[nibble_ofs]);
1211 if (len > 4 * (nibble_ofs + 1))
1212 res |= _val(val[nibble_ofs + 1]) << 4;
1214 /* kick bits outsize */
1215 if (len - 8 * byte_ofs < 8) {
1216 res &= (1 << (len - 8 * byte_ofs)) - 1;
1222 * convert to a string
1223 * FIXME: Doesn't check buffer bounds
1225 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode) {
1226 static const char big_digits[] = "0123456789ABCDEF";
1227 static const char small_digits[] = "0123456789abcdef";
1229 char *base_val, *div1_res, *div2_res, *rem_res;
1230 int counter, nibbles, i, sign, mask;
1233 const char *val = (const char *)value;
1237 const char *digits = small_digits;
1239 base_val = alloca(calc_buffer_size);
1240 div1_res = alloca(calc_buffer_size);
1241 div2_res = alloca(calc_buffer_size);
1242 rem_res = alloca(calc_buffer_size);
1244 pos = output_buffer + bit_pattern_size;
1249 bits = bit_pattern_size;
1250 #ifdef STRCALC_DEBUG_FULLPRINT
1254 nibbles = bits >> 2;
1258 digits = big_digits;
1260 for (counter = 0; counter < nibbles; ++counter) {
1261 *(--pos) = digits[_val(val[counter])];
1262 #ifdef STRCALC_DEBUG_GROUPPRINT
1263 if ((counter+1)%8 == 0)
1268 /* last nibble must be masked */
1270 mask = zex_digit[(bits & 3) - 1];
1271 x = val[counter++] & mask;
1272 *(--pos) = digits[_val(x)];
1275 /* now kill zeros */
1276 for (; counter > 1; --counter, ++pos) {
1277 #ifdef STRCALC_DEBUG_GROUPPRINT
1278 if (pos[0] == ' ') ++pos;
1286 for (counter = 0; counter < nibbles; ++counter) {
1288 p = binary_table[_val(val[counter])];
1295 /* last nibble must be masked */
1297 mask = zex_digit[(bits & 3) - 1];
1298 x = val[counter++] & mask;
1301 p = binary_table[_val(x)];
1308 /* now kill zeros */
1309 for (counter <<= 2; counter > 1; --counter, ++pos)
1316 memset(base_val, SC_0, calc_buffer_size);
1317 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1321 if (signed_mode && base == SC_DEC) {
1322 /* check for negative values */
1323 if (do_bit(val, bits - 1)) {
1324 do_negate(val, div2_res);
1330 /* transfer data into oscillating buffers */
1331 memset(div1_res, SC_0, calc_buffer_size);
1332 for (counter = 0; counter < nibbles; ++counter)
1333 div1_res[counter] = p[counter];
1335 /* last nibble must be masked */
1337 mask = zex_digit[(bits & 3) - 1];
1338 div1_res[counter] = p[counter] & mask;
1345 do_divmod(m, base_val, n, rem_res);
1349 *(--pos) = digits[_val(rem_res[0])];
1352 for (i = 0; i < calc_buffer_size; ++i)
1363 panic("Unsupported base %d", base);
1368 void init_strcalc(int precision) {
1369 if (calc_buffer == NULL) {
1370 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1372 /* round up to multiple of 4 */
1373 precision = (precision + 3) & ~3;
1375 bit_pattern_size = (precision);
1376 calc_buffer_size = (precision / 2);
1377 max_value_size = (precision / 4);
1379 calc_buffer = XMALLOCN(char, calc_buffer_size + 1);
1380 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1382 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));
1387 void finish_strcalc(void) {
1388 free(calc_buffer); calc_buffer = NULL;
1389 free(output_buffer); output_buffer = NULL;
1392 int sc_get_precision(void) {
1393 return bit_pattern_size;
1397 void sc_add(const void *value1, const void *value2, void *buffer) {
1398 CLEAR_BUFFER(calc_buffer);
1401 DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1402 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1404 do_add(value1, value2, calc_buffer);
1406 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1408 if ((buffer != NULL) && (buffer != calc_buffer)) {
1409 memcpy(buffer, calc_buffer, calc_buffer_size);
1413 void sc_sub(const void *value1, const void *value2, void *buffer) {
1414 CLEAR_BUFFER(calc_buffer);
1417 DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1418 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1420 do_sub(value1, value2, calc_buffer);
1422 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1424 if ((buffer != NULL) && (buffer != calc_buffer)) {
1425 memcpy(buffer, calc_buffer, calc_buffer_size);
1429 void sc_neg(const void *value1, void *buffer) {
1432 DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1434 do_negate(value1, calc_buffer);
1436 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1438 if ((buffer != NULL) && (buffer != calc_buffer)) {
1439 memcpy(buffer, calc_buffer, calc_buffer_size);
1443 void sc_and(const void *value1, const void *value2, void *buffer) {
1444 CLEAR_BUFFER(calc_buffer);
1447 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1448 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1450 do_bitand(value1, value2, calc_buffer);
1452 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1454 if ((buffer != NULL) && (buffer != calc_buffer)) {
1455 memcpy(buffer, calc_buffer, calc_buffer_size);
1459 void sc_or(const void *value1, const void *value2, void *buffer) {
1460 CLEAR_BUFFER(calc_buffer);
1463 DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1464 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1466 do_bitor(value1, value2, calc_buffer);
1468 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1470 if ((buffer != NULL) && (buffer != calc_buffer)) {
1471 memcpy(buffer, calc_buffer, calc_buffer_size);
1475 void sc_xor(const void *value1, const void *value2, void *buffer) {
1476 CLEAR_BUFFER(calc_buffer);
1479 DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1480 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1482 do_bitxor(value1, value2, calc_buffer);
1484 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1486 if ((buffer != NULL) && (buffer != calc_buffer)) {
1487 memcpy(buffer, calc_buffer, calc_buffer_size);
1491 void sc_not(const void *value1, void *buffer) {
1492 CLEAR_BUFFER(calc_buffer);
1495 DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1497 do_bitnot(value1, calc_buffer);
1499 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1501 if ((buffer != NULL) && (buffer != calc_buffer)) {
1502 memcpy(buffer, calc_buffer, calc_buffer_size);
1506 void sc_mul(const void *value1, const void *value2, void *buffer) {
1507 CLEAR_BUFFER(calc_buffer);
1510 DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1511 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1513 do_mul(value1, value2, calc_buffer);
1515 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1517 if ((buffer != NULL) && (buffer != calc_buffer)) {
1518 memcpy(buffer, calc_buffer, calc_buffer_size);
1522 void sc_div(const void *value1, const void *value2, void *buffer) {
1523 /* temp buffer holding unused result of divmod */
1524 char *unused_res = alloca(calc_buffer_size);
1526 CLEAR_BUFFER(calc_buffer);
1529 DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1530 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1532 do_divmod(value1, value2, calc_buffer, unused_res);
1534 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1536 if ((buffer != NULL) && (buffer != calc_buffer)) {
1537 memcpy(buffer, calc_buffer, calc_buffer_size);
1541 void sc_mod(const void *value1, const void *value2, void *buffer) {
1542 /* temp buffer holding unused result of divmod */
1543 char *unused_res = alloca(calc_buffer_size);
1545 CLEAR_BUFFER(calc_buffer);
1548 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1549 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1551 do_divmod(value1, value2, unused_res, 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_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer) {
1561 CLEAR_BUFFER(calc_buffer);
1564 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1565 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1567 do_divmod(value1, value2, div_buffer, mod_buffer);
1569 DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1573 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1576 DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1577 do_shl(val1, calc_buffer, shift_cnt, bitsize, sign);
1579 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1581 if ((buffer != NULL) && (buffer != calc_buffer)) {
1582 memmove(buffer, calc_buffer, calc_buffer_size);
1586 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1587 long offset = sc_val_to_long(val2);
1589 sc_shlI(val1, offset, bitsize, sign, buffer);
1592 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1595 DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1596 do_shr(val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1598 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1600 if ((buffer != NULL) && (buffer != calc_buffer)) {
1601 memmove(buffer, calc_buffer, calc_buffer_size);
1605 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1606 long shift_cnt = sc_val_to_long(val2);
1608 sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1611 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1612 long offset = sc_val_to_long(val2);
1616 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1617 do_shr(val1, calc_buffer, offset, bitsize, sign, 1);
1619 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1621 if ((buffer != NULL) && (buffer != calc_buffer)) {
1622 memmove(buffer, calc_buffer, calc_buffer_size);
1626 void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1627 long offset = sc_val_to_long(val2);
1631 DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1632 do_rotl(val1, calc_buffer, offset, bitsize, sign);
1634 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1636 if ((buffer != NULL) && (buffer != calc_buffer)) {
1637 memmove(buffer, calc_buffer, calc_buffer_size);
1641 void sc_zero(void *buffer) {
1643 buffer = calc_buffer;
1644 CLEAR_BUFFER(buffer);