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 SHIFT(count) (SC_1 << (count))
44 #define _val(a) ((a)-SC_0)
45 #define _digit(a) ((a)+SC_0)
46 #define _bitisset(digit, pos) (((digit) & SHIFT(pos)) != SC_0)
48 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__, __LINE__)
50 /* shortcut output for debugging */
51 # define sc_print_hex(a) sc_print((a), 0, SC_HEX, 0)
52 # define sc_print_dec(a) sc_print((a), 0, SC_DEC, 1)
53 # define sc_print_oct(a) sc_print((a), 0, SC_OCT, 0)
54 # define sc_print_bin(a) sc_print((a), 0, SC_BIN, 0)
56 #ifdef STRCALC_DEBUG_PRINTCOMP
57 # define DEBUGPRINTF_COMPUTATION(x) printf x
59 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
62 # define DEBUGPRINTF(x) printf x
64 # define DEBUGPRINTF(x) ((void)0)
71 static char *calc_buffer = NULL; /* buffer holding all results */
72 static char *output_buffer = NULL; /* buffer for output */
73 static int bit_pattern_size; /* maximum number of bits */
74 static int calc_buffer_size; /* size of internally stored values */
75 static int max_value_size; /* maximum size of values */
77 static int carry_flag; /**< some computation set the carry_flag:
78 - right shift if bits were lost due to shifting
79 - division if there was a remainder
80 However, the meaning of carry is machine dependent
81 and often defined in other ways! */
83 static const char sex_digit[4] = { SC_E, SC_C, SC_8, SC_0 };
84 static const char zex_digit[4] = { SC_1, SC_3, SC_7, SC_F };
85 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
86 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
88 static char const add_table[16][16][2] = {
89 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
90 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
91 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
92 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
94 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
95 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
96 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
97 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
99 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
100 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
101 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
102 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
104 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
105 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
106 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
107 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
109 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
110 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
111 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
112 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
114 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
115 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
116 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
117 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
119 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
120 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
121 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
122 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
124 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
125 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
126 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
127 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
129 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
130 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
131 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
132 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
134 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
135 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
136 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
137 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
139 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
140 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
141 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
142 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
144 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
145 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
146 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
147 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
149 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
150 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
151 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
152 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
154 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
155 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
156 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
157 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
159 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
160 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
161 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
162 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
164 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
165 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
166 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
167 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
170 static char const mul_table[16][16][2] = {
171 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
172 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
173 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
174 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
176 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
177 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
178 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
179 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
181 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
182 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
183 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
184 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
186 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
187 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
188 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
189 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
191 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
192 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
193 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
194 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
196 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
197 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
198 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
199 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
201 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
202 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
203 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
204 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
206 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
207 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
208 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
209 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
211 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
212 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
213 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
214 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
216 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
217 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
218 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
219 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
221 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
222 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
223 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
224 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
226 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
227 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
228 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
229 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
231 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
232 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
233 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
234 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
236 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
237 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
238 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
239 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
241 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
242 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
243 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
244 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
246 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
247 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
248 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
249 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
252 static char const shrs_table[16][4][2] = {
253 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
254 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
255 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
256 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
257 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
258 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
259 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
260 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
261 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
262 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
263 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
264 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
265 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
266 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
267 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
268 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
271 /** converting a digit to a binary string */
272 static const char *binary_table[16] = {
273 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
274 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
277 /*****************************************************************************
279 *****************************************************************************/
280 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
281 const char *file, int line) {
283 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
284 while (len-- && *str) printf("%c", *str++); printf("\n");
285 while (--pos) printf(" "); printf("^\n");
290 * implements the bitwise NOT operation
292 static void do_bitnot(const char *val, char *buffer)
296 for (counter = 0; counter<calc_buffer_size; counter++)
297 buffer[counter] = val[counter] ^ SC_F;
301 * implements the bitwise OR operation
303 static void do_bitor(const char *val1, const char *val2, char *buffer)
307 for (counter = 0; counter<calc_buffer_size; counter++)
308 buffer[counter] = val1[counter] | val2[counter];
312 * implements the bitwise eXclusive OR operation
314 static void do_bitxor(const char *val1, const char *val2, char *buffer)
318 for (counter = 0; counter<calc_buffer_size; counter++)
319 buffer[counter] = val1[counter] ^ val2[counter];
323 * implements the bitwise AND operation
325 static void do_bitand(const char *val1, const char *val2, char *buffer)
329 for (counter = 0; counter<calc_buffer_size; counter++)
330 buffer[counter] = val1[counter] & val2[counter];
334 * implements the bitwise AND not operation
336 static void do_bitandnot(const char *val1, const char *val2, char *buffer)
340 for (counter = 0; counter < calc_buffer_size; ++counter)
341 buffer[counter] = val1[counter] & (SC_F ^ val2[counter]);
345 * returns the sign bit.
347 * @todo This implementation is wrong, as it returns the highest bit of the buffer
348 * NOT the highest bit depending on the real mode
350 static int do_sign(const char *val)
352 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
356 * returns non-zero if bit at position pos is set
358 static int do_bit(const char *val, int pos)
361 int nibble = pos >> 2;
363 return _bitisset(val[nibble], bit);
367 * Implements a fast ADD + 1
369 static void do_inc(const char *val, char *buffer)
373 while (counter++ < calc_buffer_size) {
378 /* No carry here, *val != SC_F */
379 *buffer = add_table[_val(*val)][SC_1][0];
383 /* here a carry could be lost, this is intended because this should
384 * happen only when a value changes sign. */
388 * Implements a unary MINUS
390 static void do_negate(const char *val, char *buffer)
392 do_bitnot(val, buffer);
393 do_inc(buffer, buffer);
397 * Implements a binary ADD
399 * @todo The implementation of carry is wrong, as it is the
400 * calc_buffer_size carry, not the mode depending
402 static void do_add(const char *val1, const char *val2, char *buffer)
405 const char *add1, *add2;
408 for (counter = 0; counter < calc_buffer_size; counter++) {
409 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
410 add2 = add_table[_val(add1[0])][_val(carry)];
411 /* carry might be zero */
412 buffer[counter] = add2[0];
413 carry = add_table[_val(add1[1])][_val(add2[1])][0];
415 carry_flag = carry != SC_0;
419 * Implements a binary SUB
421 static void do_sub(const char *val1, const char *val2, char *buffer)
423 char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
425 do_negate(val2, temp_buffer);
426 do_add(val1, temp_buffer, buffer);
430 * Implements a binary MUL
432 static void do_mul(const char *val1, const char *val2, char *buffer)
434 char *temp_buffer; /* result buffer */
435 char *neg_val1; /* abs of val1 */
436 char *neg_val2; /* abs of val2 */
438 const char *mul, *add1, *add2; /* intermediate result containers */
439 char carry = SC_0; /* container for carries */
440 char sign = 0; /* marks result sign */
441 int c_inner, c_outer; /* loop counters */
443 temp_buffer = alloca(calc_buffer_size);
444 neg_val1 = alloca(calc_buffer_size);
445 neg_val2 = alloca(calc_buffer_size);
447 /* init result buffer to zeros */
448 memset(temp_buffer, SC_0, calc_buffer_size);
450 /* the multiplication works only for positive values, for negative values *
451 * it is necessary to negate them and adjust the result accordingly */
452 if (do_sign(val1) == -1) {
453 do_negate(val1, neg_val1);
457 if (do_sign(val2) == -1) {
458 do_negate(val2, neg_val2);
463 for (c_outer = 0; c_outer < max_value_size; c_outer++) {
464 if (val2[c_outer] != SC_0) {
465 for (c_inner = 0; c_inner < max_value_size; c_inner++) {
466 /* do the following calculation: *
467 * Add the current carry, the value at position c_outer+c_inner *
468 * and the result of the multiplication of val1[c_inner] and *
469 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
471 /* multiplicate the two digits */
472 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
473 /* add old value to result of multiplication */
474 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
475 /* add carry to the sum */
476 add2 = add_table[_val(add1[0])][_val(carry)];
478 /* all carries together result in new carry. This is always smaller *
480 * Both multiplicands, the carry and the value already in the temp *
481 * buffer are single digits and their value is therefore at most *
484 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
485 * The tables list all operations rem b, so the carry is at most *
486 * (b*b-1)rem b = -1rem b = b-1 */
487 carry = add_table[_val(mul[1])][_val(add1[1])][0];
488 carry = add_table[_val(carry)][_val(add2[1])][0];
490 temp_buffer[c_inner + c_outer] = add2[0];
493 /* A carry may hang over */
494 /* c_outer is always smaller than max_value_size! */
495 temp_buffer[max_value_size + c_outer] = carry;
501 do_negate(temp_buffer, buffer);
503 memcpy(buffer, temp_buffer, calc_buffer_size);
507 * Shift the buffer to left and add a 4 bit digit
509 static void do_push(const char digit, char *buffer)
513 for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
514 buffer[counter+1] = buffer[counter];
520 * Implements truncating integer division and remainder.
522 * Note: This is MOST slow
524 static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
526 const char *dividend = rDividend;
527 const char *minus_divisor;
531 char div_sign = 0; /* remember division result sign */
532 char rem_sign = 0; /* remember remainder result sign */
534 int c_dividend; /* loop counters */
536 neg_val1 = alloca(calc_buffer_size);
537 neg_val2 = alloca(calc_buffer_size);
539 /* clear result buffer */
540 memset(quot, SC_0, calc_buffer_size);
541 memset(rem, SC_0, calc_buffer_size);
543 /* if the divisor is zero this won't work (quot is zero) */
544 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
546 /* if the dividend is zero result is zero (quot is zero) */
547 if (sc_comp(dividend, quot) == 0)
550 if (do_sign(dividend) == -1) {
551 do_negate(dividend, neg_val1);
557 do_negate(divisor, neg_val2);
558 if (do_sign(divisor) == -1) {
560 minus_divisor = divisor;
563 minus_divisor = neg_val2;
565 /* if divisor >= dividend division is easy
566 * (remember these are absolute values) */
567 switch (sc_comp(dividend, divisor)) {
568 case 0: /* dividend == divisor */
572 case -1: /* dividend < divisor */
573 memcpy(rem, dividend, calc_buffer_size);
576 default: /* unluckily division is necessary :( */
580 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
581 do_push(dividend[c_dividend], rem);
584 if (sc_comp(rem, divisor) != -1) { /* remainder >= divisor */
585 /* subtract until the remainder becomes negative, this should
586 * be faster than comparing remainder with divisor */
587 do_add(rem, minus_divisor, rem);
589 while (do_sign(rem) == 1) {
590 quot[0] = add_table[_val(quot[0])][SC_1][0];
591 do_add(rem, minus_divisor, rem);
594 /* subtracted one too much */
595 do_add(rem, divisor, rem);
599 /* sets carry if remainder is non-zero ??? */
600 carry_flag = !sc_is_zero(rem);
603 do_negate(quot, quot);
610 * Implements a Shift Left, which can either preserve the sign bit
613 * @todo Assertions seems to be wrong
615 static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed)
624 assert((shift_cnt >= 0) || (0 && "negative leftshift"));
625 assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
626 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
627 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
629 /* if shifting far enough the result is zero */
630 if (shift_cnt >= bitsize) {
631 memset(buffer, SC_0, calc_buffer_size);
635 shift = SHIFT(shift_cnt % 4); /* this is 2 ** (offset % 4) */
636 shift_cnt = shift_cnt / 4;
638 /* shift the single digits some bytes (offset) and some bits (table)
640 for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
641 shl = mul_table[_val(val1[counter])][_val(shift)];
642 buffer[counter + shift_cnt] = shl[0] | carry;
646 shl = mul_table[_val(val1[counter])][_val(shift)];
647 buffer[counter + shift_cnt] = shl[0] | carry;
650 bitoffset = counter - 1;
653 /* fill with zeroes */
654 for (counter = 0; counter < shift_cnt; counter++)
655 buffer[counter] = SC_0;
657 /* if the mode was signed, change sign when the mode's msb is now 1 */
658 shift_cnt = bitoffset + shift_cnt;
659 bitoffset = (bitsize-1) % 4;
660 if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
661 /* this sets the upper bits of the leftmost digit */
662 buffer[shift_cnt] |= min_digit[bitoffset];
663 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
664 buffer[counter] = SC_F;
666 } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
667 /* this clears the upper bits of the leftmost digit */
668 buffer[shift_cnt] &= max_digit[bitoffset];
669 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
670 buffer[counter] = SC_0;
676 * Implements a Shift Right, which can either preserve the sign bit
679 * @param bitsize bitsize of the value to be shifted
681 * @todo Assertions seems to be wrong
683 static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift)
689 int shift_mod, shift_nib;
694 assert((shift_cnt >= 0) || (0 && "negative rightshift"));
695 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
696 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
698 sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
700 /* if shifting far enough the result is either 0 or -1 */
701 if (shift_cnt >= bitsize) {
702 if (!sc_is_zero(val1)) {
705 memset(buffer, sign, calc_buffer_size);
709 shift_mod = shift_cnt & 3;
710 shift_nib = shift_cnt >> 2;
712 /* check if any bits are lost, and set carry_flag if so */
713 for (counter = 0; counter < shift_nib; ++counter) {
714 if (val1[counter] != 0) {
719 if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
722 /* shift digits to the right with offset, carry and all */
723 buffer[0] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
724 for (counter = 1; counter < ((bitsize + 3) >> 2) - shift_nib; counter++) {
725 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
726 buffer[counter] = shrs[0];
727 buffer[counter - 1] |= shrs[1];
730 /* the last digit is special in regard of signed/unsigned shift */
731 bitoffset = bitsize & 3;
732 msd = sign; /* most significant digit */
734 /* remove sign bits if mode was signed and this is an unsigned shift */
735 if (!signed_shift && is_signed) {
736 msd &= max_digit[bitoffset];
739 shrs = shrs_table[_val(msd)][shift_mod];
741 /* signed shift and signed mode and negative value means all bits to the left are set */
742 if (signed_shift && sign == SC_F) {
743 buffer[counter] = shrs[0] | min_digit[bitoffset];
745 buffer[counter] = shrs[0];
749 buffer[counter - 1] |= shrs[1];
751 /* fill with SC_F or SC_0 depending on sign */
752 for (counter++; counter < calc_buffer_size; counter++) {
753 buffer[counter] = sign;
758 * Implements a Rotate Left.
759 * positive: low-order -> high order, negative other direction
761 static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
764 temp1 = alloca(calc_buffer_size);
765 temp2 = alloca(calc_buffer_size);
767 offset = offset % radius;
769 /* rotation by multiples of the type length is identity */
771 memmove(buffer, val1, calc_buffer_size);
775 do_shl(val1, temp1, offset, radius, is_signed);
776 do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
777 do_bitor(temp1, temp2, buffer);
778 carry_flag = 0; /* set by shr, but due to rot this is false */
781 /*****************************************************************************
782 * public functions, declared in strcalc.h
783 *****************************************************************************/
784 const void *sc_get_buffer(void)
786 return (void*)calc_buffer;
789 int sc_get_buffer_length(void)
791 return calc_buffer_size;
795 * Do sign extension if the mode is signed, otherwise to zero extension.
797 void sign_extend(void *buffer, ir_mode *mode)
799 char *calc_buffer = buffer;
800 int bits = get_mode_size_bits(mode) - 1;
801 int nibble = bits >> 2;
802 int max = max_digit[bits & 3];
805 if (mode_is_signed(mode)) {
806 if (calc_buffer[nibble] > max) {
807 /* sign bit is set, we need sign expansion */
809 for (i = nibble + 1; i < calc_buffer_size; ++i)
810 calc_buffer[i] = SC_F;
811 calc_buffer[nibble] |= sex_digit[bits & 3];
813 /* set all bits to zero */
814 for (i = nibble + 1; i < calc_buffer_size; ++i)
815 calc_buffer[i] = SC_0;
816 calc_buffer[nibble] &= zex_digit[bits & 3];
819 /* do zero extension */
820 for (i = nibble + 1; i < calc_buffer_size; ++i)
821 calc_buffer[i] = SC_0;
822 calc_buffer[nibble] &= zex_digit[bits & 3];
826 /* FIXME doesn't check for overflows */
827 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode)
829 const char *orig_str = str;
830 unsigned int orig_len = len;
835 base = alloca(calc_buffer_size);
836 val = alloca(calc_buffer_size);
838 /* verify valid pointers (not null) */
840 /* a string no characters long is an error */
843 if (buffer == NULL) buffer = calc_buffer;
845 CLEAR_BUFFER(buffer);
849 /* strip leading spaces */
850 while ((len > 0) && (*str == ' ')) { len--; str++; }
852 /* if the first two characters are 0x or 0X -> hex
853 * if the first is a 0 -> oct
854 * else dec, strip leading -/+ and remember sign
856 * only a + or - sign is no number resulting in an error */
860 if (str[1] == 'x' || str[1] == 'X') { /* hex */
863 base[1] = SC_1; base[0] = SC_0;
867 base[1] = SC_0; base[0] = SC_8;
874 base[1] = SC_0; base[0] = SC_A;
881 base[1] = SC_0; base[0] = SC_A;
884 default: /* dec, else would have begun with 0x or 0 */
885 base[1] = SC_0; base[0] = SC_A;
887 } else { /* dec, else would have begun with 0x or 0 */
888 base[1] = SC_0; base[0] = SC_A;
891 /* BEGIN string evaluation, from left to right */
900 if (base[0] > SC_A || base[1] > SC_0) { /* (base > 10) */
901 val[0] = _digit((*str)-'a'+10);
904 fail_char(orig_str, orig_len, *str, str-orig_str+1);
913 if (base[0] > SC_A || base[1] > SC_0) { /* (base > 10) */
914 val[0] = _digit((*str)-'A'+10);
917 fail_char(orig_str, orig_len, *str, str-orig_str+1);
922 if (base[0] > SC_8 || base[1] > SC_0) { /* (base > 8) */
923 val[0] = _digit((*str)-'0');
926 fail_char(orig_str, orig_len, *str, str-orig_str+1);
937 val[0] = _digit((*str)-'0');
941 fail_char(orig_str, orig_len, *str, str-orig_str+1);
944 /* Radix conversion from base b to base B:
945 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
946 do_mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
947 do_add(val, calc_buffer, calc_buffer); /* add next digit to current value */
949 /* get ready for the next letter */
952 } /* while (len > 0 ) */
955 do_negate(calc_buffer, calc_buffer);
957 /* beware: even if hex numbers have no sign, we need sign extension here */
958 sign_extend(calc_buffer, mode);
961 void sc_val_from_long(long value, void *buffer)
964 char sign, is_minlong;
966 if (buffer == NULL) buffer = calc_buffer;
970 is_minlong = value == LONG_MIN;
972 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
980 CLEAR_BUFFER(buffer);
982 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
983 *pos++ = _digit(value & 0xf);
989 do_inc(buffer, buffer);
991 do_negate(buffer, buffer);
995 void sc_val_from_ulong(unsigned long value, void *buffer)
999 if (buffer == NULL) buffer = calc_buffer;
1002 while (pos < (unsigned char *)buffer + calc_buffer_size) {
1003 *pos++ = (unsigned char)_digit(value & 0xf);
1008 long sc_val_to_long(const void *val)
1013 for (i = calc_buffer_size - 1; i >= 0; i--) {
1014 l = (l << 4) + _val(((char *)val)[i]);
1019 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1024 if (buffer == NULL) buffer = calc_buffer;
1025 CLEAR_BUFFER(buffer);
1027 if (!sign) return; /* unsigned means minimum is 0(zero) */
1031 bits = num_bits - 1;
1032 for (i = 0; i < bits/4; i++)
1035 *pos++ = min_digit[bits%4];
1037 for (i++; i <= calc_buffer_size - 1; i++)
1041 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1046 if (buffer == NULL) buffer = calc_buffer;
1047 CLEAR_BUFFER(buffer);
1050 bits = num_bits - sign;
1051 for (i = 0; i < bits/4; i++)
1054 *pos++ = max_digit[bits%4];
1056 for (i++; i <= calc_buffer_size - 1; i++)
1060 void sc_truncate(unsigned int num_bits, void *buffer)
1062 char *cbuffer = buffer;
1063 char *pos = cbuffer + (num_bits / 4);
1064 char *end = cbuffer + calc_buffer_size;
1068 switch(num_bits % 4) {
1069 case 0: /* nothing to do */ break;
1070 case 1: *pos++ &= SC_1; break;
1071 case 2: *pos++ &= SC_3; break;
1072 case 3: *pos++ &= SC_7; break;
1075 for( ; pos < end; ++pos)
1079 int sc_comp(const void* value1, const void* value2)
1081 int counter = calc_buffer_size - 1;
1082 const char *val1 = (const char *)value1;
1083 const char *val2 = (const char *)value2;
1085 /* compare signs first:
1086 * the loop below can only compare values of the same sign! */
1087 if (do_sign(val1) != do_sign(val2))
1088 return (do_sign(val1) == 1)?(1):(-1);
1090 /* loop until two digits differ, the values are equal if there
1091 * are no such two digits */
1092 while (val1[counter] == val2[counter]) {
1094 if (counter < 0) return 0;
1097 /* the leftmost digit is the most significant, so this returns
1098 * the correct result.
1099 * This implies the digit enum is ordered */
1100 return (val1[counter] > val2[counter]) ? (1) : (-1);
1103 int sc_get_highest_set_bit(const void *value)
1105 const char *val = (const char*)value;
1108 high = calc_buffer_size * 4 - 1;
1110 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1111 if (val[counter] == SC_0)
1114 if (val[counter] > SC_7) return high;
1115 else if (val[counter] > SC_3) return high - 1;
1116 else if (val[counter] > SC_1) return high - 2;
1117 else return high - 3;
1123 int sc_get_lowest_set_bit(const void *value)
1125 const char *val = (const char*)value;
1129 for (counter = 0; counter < calc_buffer_size; counter++) {
1130 switch (val[counter]) {
1157 int sc_get_bit_at(const void *value, unsigned pos)
1159 const char *val = value;
1160 unsigned nibble = pos >> 2;
1162 return (val[nibble] & SHIFT(pos & 3)) != SC_0;
1165 void sc_set_bit_at(void *value, unsigned pos)
1168 unsigned nibble = pos >> 2;
1170 val[nibble] |= SHIFT(pos & 3);
1173 int sc_is_zero(const void *value)
1175 const char* val = (const char *)value;
1178 for (counter = 0; counter < calc_buffer_size; ++counter) {
1179 if (val[counter] != SC_0)
1185 int sc_is_negative(const void *value)
1187 return do_sign(value) == -1;
1190 int sc_had_carry(void)
1195 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1197 const char *val = (const char *)value;
1198 int nibble_ofs = 2 * byte_ofs;
1201 /* the current scheme uses one byte to store a nibble */
1202 if (4 * nibble_ofs >= len)
1205 res = _val(val[nibble_ofs]);
1206 if (len > 4 * (nibble_ofs + 1))
1207 res |= _val(val[nibble_ofs + 1]) << 4;
1209 /* kick bits outsize */
1210 if (len - 8 * byte_ofs < 8) {
1211 res &= (1 << (len - 8 * byte_ofs)) - 1;
1217 * convert to a string
1218 * FIXME: Doesn't check buffer bounds
1220 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode)
1222 static const char big_digits[] = "0123456789ABCDEF";
1223 static const char small_digits[] = "0123456789abcdef";
1225 char *base_val, *div1_res, *div2_res, *rem_res;
1226 int counter, nibbles, i, sign, mask;
1229 const char *val = (const char *)value;
1233 const char *digits = small_digits;
1235 base_val = alloca(calc_buffer_size);
1236 div1_res = alloca(calc_buffer_size);
1237 div2_res = alloca(calc_buffer_size);
1238 rem_res = alloca(calc_buffer_size);
1240 pos = output_buffer + bit_pattern_size;
1245 bits = bit_pattern_size;
1246 #ifdef STRCALC_DEBUG_FULLPRINT
1250 nibbles = bits >> 2;
1254 digits = big_digits;
1256 for (counter = 0; counter < nibbles; ++counter) {
1257 *(--pos) = digits[_val(val[counter])];
1258 #ifdef STRCALC_DEBUG_GROUPPRINT
1259 if ((counter+1)%8 == 0)
1264 /* last nibble must be masked */
1266 mask = zex_digit[(bits & 3) - 1];
1267 x = val[counter++] & mask;
1268 *(--pos) = digits[_val(x)];
1271 /* now kill zeros */
1272 for (; counter > 1; --counter, ++pos) {
1273 #ifdef STRCALC_DEBUG_GROUPPRINT
1274 if (pos[0] == ' ') ++pos;
1282 for (counter = 0; counter < nibbles; ++counter) {
1284 p = binary_table[_val(val[counter])];
1291 /* last nibble must be masked */
1293 mask = zex_digit[(bits & 3) - 1];
1294 x = val[counter++] & mask;
1297 p = binary_table[_val(x)];
1304 /* now kill zeros */
1305 for (counter <<= 2; counter > 1; --counter, ++pos)
1312 memset(base_val, SC_0, calc_buffer_size);
1313 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1317 if (signed_mode && base == SC_DEC) {
1318 /* check for negative values */
1319 if (do_bit(val, bits - 1)) {
1320 do_negate(val, div2_res);
1326 /* transfer data into oscillating buffers */
1327 memset(div1_res, SC_0, calc_buffer_size);
1328 for (counter = 0; counter < nibbles; ++counter)
1329 div1_res[counter] = p[counter];
1331 /* last nibble must be masked */
1333 mask = zex_digit[(bits & 3) - 1];
1334 div1_res[counter] = p[counter] & mask;
1341 do_divmod(m, base_val, n, rem_res);
1345 *(--pos) = digits[_val(rem_res[0])];
1348 for (i = 0; i < calc_buffer_size; ++i)
1359 panic("Unsupported base %d", base);
1364 void init_strcalc(int precision)
1366 if (calc_buffer == NULL) {
1367 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1369 /* round up to multiple of 4 */
1370 precision = (precision + 3) & ~3;
1372 bit_pattern_size = (precision);
1373 calc_buffer_size = (precision / 2);
1374 max_value_size = (precision / 4);
1376 calc_buffer = XMALLOCN(char, calc_buffer_size + 1);
1377 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1379 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));
1384 void finish_strcalc(void)
1386 free(calc_buffer); calc_buffer = NULL;
1387 free(output_buffer); output_buffer = NULL;
1390 int sc_get_precision(void)
1392 return bit_pattern_size;
1396 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)
1415 CLEAR_BUFFER(calc_buffer);
1418 DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1419 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1421 do_sub(value1, value2, calc_buffer);
1423 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1425 if ((buffer != NULL) && (buffer != calc_buffer)) {
1426 memcpy(buffer, calc_buffer, calc_buffer_size);
1430 void sc_neg(const void *value1, void *buffer)
1434 DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1436 do_negate(value1, calc_buffer);
1438 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1440 if ((buffer != NULL) && (buffer != calc_buffer)) {
1441 memcpy(buffer, calc_buffer, calc_buffer_size);
1445 void sc_and(const void *value1, const void *value2, void *buffer)
1447 CLEAR_BUFFER(calc_buffer);
1450 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1451 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1453 do_bitand(value1, value2, calc_buffer);
1455 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1457 if ((buffer != NULL) && (buffer != calc_buffer)) {
1458 memcpy(buffer, calc_buffer, calc_buffer_size);
1462 void sc_andnot(const void *value1, const void *value2, void *buffer)
1464 CLEAR_BUFFER(calc_buffer);
1467 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1468 DEBUGPRINTF_COMPUTATION(("~%s -> ", sc_print_hex(value2)));
1470 do_bitandnot(value1, value2, calc_buffer);
1472 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1474 if (buffer != NULL && buffer != calc_buffer) {
1475 memcpy(buffer, calc_buffer, calc_buffer_size);
1479 void sc_or(const void *value1, const void *value2, void *buffer)
1481 CLEAR_BUFFER(calc_buffer);
1484 DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1485 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1487 do_bitor(value1, value2, calc_buffer);
1489 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1491 if ((buffer != NULL) && (buffer != calc_buffer)) {
1492 memcpy(buffer, calc_buffer, calc_buffer_size);
1496 void sc_xor(const void *value1, const void *value2, void *buffer)
1498 CLEAR_BUFFER(calc_buffer);
1501 DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1502 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1504 do_bitxor(value1, value2, calc_buffer);
1506 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1508 if ((buffer != NULL) && (buffer != calc_buffer)) {
1509 memcpy(buffer, calc_buffer, calc_buffer_size);
1513 void sc_not(const void *value1, void *buffer)
1515 CLEAR_BUFFER(calc_buffer);
1518 DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1520 do_bitnot(value1, calc_buffer);
1522 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1524 if ((buffer != NULL) && (buffer != calc_buffer)) {
1525 memcpy(buffer, calc_buffer, calc_buffer_size);
1529 void sc_mul(const void *value1, const void *value2, void *buffer)
1531 CLEAR_BUFFER(calc_buffer);
1534 DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1535 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1537 do_mul(value1, value2, 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_div(const void *value1, const void *value2, void *buffer)
1548 /* temp buffer holding unused result of divmod */
1549 char *unused_res = alloca(calc_buffer_size);
1551 CLEAR_BUFFER(calc_buffer);
1554 DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1555 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1557 do_divmod(value1, value2, calc_buffer, unused_res);
1559 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1561 if ((buffer != NULL) && (buffer != calc_buffer)) {
1562 memcpy(buffer, calc_buffer, calc_buffer_size);
1566 void sc_mod(const void *value1, const void *value2, void *buffer)
1568 /* temp buffer holding unused result of divmod */
1569 char *unused_res = alloca(calc_buffer_size);
1571 CLEAR_BUFFER(calc_buffer);
1574 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1575 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1577 do_divmod(value1, value2, unused_res, calc_buffer);
1579 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1581 if ((buffer != NULL) && (buffer != calc_buffer)) {
1582 memcpy(buffer, calc_buffer, calc_buffer_size);
1586 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer)
1588 CLEAR_BUFFER(calc_buffer);
1591 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1592 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1594 do_divmod(value1, value2, div_buffer, mod_buffer);
1596 DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1600 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1604 DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1605 do_shl(val1, calc_buffer, shift_cnt, bitsize, sign);
1607 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1609 if ((buffer != NULL) && (buffer != calc_buffer)) {
1610 memmove(buffer, calc_buffer, calc_buffer_size);
1614 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1616 long offset = sc_val_to_long(val2);
1618 sc_shlI(val1, offset, bitsize, sign, buffer);
1621 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1625 DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1626 do_shr(val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1628 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1630 if ((buffer != NULL) && (buffer != calc_buffer)) {
1631 memmove(buffer, calc_buffer, calc_buffer_size);
1635 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1637 long shift_cnt = sc_val_to_long(val2);
1639 sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1642 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1644 long offset = sc_val_to_long(val2);
1648 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1649 do_shr(val1, calc_buffer, offset, bitsize, sign, 1);
1651 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1653 if ((buffer != NULL) && (buffer != calc_buffer)) {
1654 memmove(buffer, calc_buffer, calc_buffer_size);
1658 void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1660 long offset = sc_val_to_long(val2);
1664 DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1665 do_rotl(val1, calc_buffer, offset, bitsize, sign);
1667 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1669 if ((buffer != NULL) && (buffer != calc_buffer)) {
1670 memmove(buffer, calc_buffer, calc_buffer_size);
1674 void sc_zero(void *buffer)
1677 buffer = calc_buffer;
1678 CLEAR_BUFFER(buffer);