2 * Copyright (C) 1995-2011 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 /* shortcut output for debugging */
49 # define sc_print_hex(a) sc_print((a), 0, SC_HEX, 0)
50 # define sc_print_dec(a) sc_print((a), 0, SC_DEC, 1)
51 # define sc_print_oct(a) sc_print((a), 0, SC_OCT, 0)
52 # define sc_print_bin(a) sc_print((a), 0, SC_BIN, 0)
54 #ifdef STRCALC_DEBUG_PRINTCOMP
55 # define DEBUGPRINTF_COMPUTATION(x) printf x
57 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
60 # define DEBUGPRINTF(x) printf x
62 # define DEBUGPRINTF(x) ((void)0)
69 static char *calc_buffer = NULL; /* buffer holding all results */
70 static char *output_buffer = NULL; /* buffer for output */
71 static int bit_pattern_size; /* maximum number of bits */
72 static int calc_buffer_size; /* size of internally stored values */
73 static int max_value_size; /* maximum size of values */
75 static int carry_flag; /**< some computation set the carry_flag:
76 - right shift if bits were lost due to shifting
77 - division if there was a remainder
78 However, the meaning of carry is machine dependent
79 and often defined in other ways! */
81 static const char sex_digit[4] = { SC_E, SC_C, SC_8, SC_0 };
82 static const char zex_digit[4] = { SC_1, SC_3, SC_7, SC_F };
83 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
84 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
86 static char const add_table[16][16][2] = {
87 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
88 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
89 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
90 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
92 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
93 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
94 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
95 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
97 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
98 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
99 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
100 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
102 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
103 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
104 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
105 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
107 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
108 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
109 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
110 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
112 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
113 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
114 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
115 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
117 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
118 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
119 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
120 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
122 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
123 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
124 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
125 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
127 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
128 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
129 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
130 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
132 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
133 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
134 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
135 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
137 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
138 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
139 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
140 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
142 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
143 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
144 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
145 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
147 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
148 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
149 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
150 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
152 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
153 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
154 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
155 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
157 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
158 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
159 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
160 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
162 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
163 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
164 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
165 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
168 static char const mul_table[16][16][2] = {
169 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
170 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
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} },
174 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
175 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
176 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
177 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
179 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
180 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
181 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
182 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
184 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
185 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
186 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
187 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
189 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
190 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
191 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
192 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
194 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
195 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
196 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
197 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
199 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
200 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
201 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
202 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
204 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
205 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
206 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
207 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
209 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
210 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
211 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
212 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
214 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
215 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
216 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
217 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
219 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
220 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
221 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
222 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
224 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
225 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
226 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
227 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
229 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
230 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
231 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
232 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
234 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
235 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
236 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
237 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
239 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
240 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
241 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
242 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
244 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
245 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
246 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
247 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
250 static char const shrs_table[16][4][2] = {
251 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
252 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
253 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
254 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
255 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
256 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
257 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
258 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
259 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
260 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
261 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
262 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
263 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
264 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
265 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
266 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
269 /** converting a digit to a binary string */
270 static char const *const binary_table[] = {
271 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
272 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
275 /*****************************************************************************
277 *****************************************************************************/
280 * implements the bitwise NOT operation
282 static void do_bitnot(const char *val, char *buffer)
286 for (counter = 0; counter<calc_buffer_size; counter++)
287 buffer[counter] = val[counter] ^ SC_F;
291 * implements the bitwise OR operation
293 static void do_bitor(const char *val1, const char *val2, char *buffer)
297 for (counter = 0; counter<calc_buffer_size; counter++)
298 buffer[counter] = val1[counter] | val2[counter];
302 * implements the bitwise eXclusive OR operation
304 static void do_bitxor(const char *val1, const char *val2, char *buffer)
308 for (counter = 0; counter<calc_buffer_size; counter++)
309 buffer[counter] = val1[counter] ^ val2[counter];
313 * implements the bitwise AND operation
315 static void do_bitand(const char *val1, const char *val2, char *buffer)
319 for (counter = 0; counter<calc_buffer_size; counter++)
320 buffer[counter] = val1[counter] & val2[counter];
324 * implements the bitwise AND not operation
326 static void do_bitandnot(const char *val1, const char *val2, char *buffer)
330 for (counter = 0; counter < calc_buffer_size; ++counter)
331 buffer[counter] = val1[counter] & (SC_F ^ val2[counter]);
335 * returns the sign bit.
337 * @todo This implementation is wrong, as it returns the highest bit of the buffer
338 * NOT the highest bit depending on the real mode
340 static int do_sign(const char *val)
342 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
346 * returns non-zero if bit at position pos is set
348 static int do_bit(const char *val, int pos)
351 int nibble = pos >> 2;
353 return _bitisset(val[nibble], bit);
357 * Implements a fast ADD + 1
359 static void do_inc(const char *val, char *buffer)
363 while (counter++ < calc_buffer_size) {
368 /* No carry here, *val != SC_F */
369 *buffer = add_table[_val(*val)][SC_1][0];
373 /* here a carry could be lost, this is intended because this should
374 * happen only when a value changes sign. */
378 * Implements a unary MINUS
380 static void do_negate(const char *val, char *buffer)
382 do_bitnot(val, buffer);
383 do_inc(buffer, buffer);
387 * Implements a binary ADD
389 * @todo The implementation of carry is wrong, as it is the
390 * calc_buffer_size carry, not the mode depending
392 static void do_add(const char *val1, const char *val2, char *buffer)
395 const char *add1, *add2;
398 for (counter = 0; counter < calc_buffer_size; counter++) {
399 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
400 add2 = add_table[_val(add1[0])][_val(carry)];
401 /* carry might be zero */
402 buffer[counter] = add2[0];
403 carry = add_table[_val(add1[1])][_val(add2[1])][0];
405 carry_flag = carry != SC_0;
409 * Implements a binary SUB
411 static void do_sub(const char *val1, const char *val2, char *buffer)
413 char *temp_buffer = (char*) alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
415 do_negate(val2, temp_buffer);
416 do_add(val1, temp_buffer, buffer);
420 * Implements a binary MUL
422 static void do_mul(const char *val1, const char *val2, char *buffer)
424 char *temp_buffer; /* result buffer */
425 char *neg_val1; /* abs of val1 */
426 char *neg_val2; /* abs of val2 */
428 const char *mul, *add1, *add2; /* intermediate result containers */
429 char carry = SC_0; /* container for carries */
430 char sign = 0; /* marks result sign */
431 int c_inner, c_outer; /* loop counters */
433 temp_buffer = (char*) alloca(calc_buffer_size);
434 neg_val1 = (char*) alloca(calc_buffer_size);
435 neg_val2 = (char*) alloca(calc_buffer_size);
437 /* init result buffer to zeros */
438 memset(temp_buffer, SC_0, calc_buffer_size);
440 /* the multiplication works only for positive values, for negative values *
441 * it is necessary to negate them and adjust the result accordingly */
442 if (do_sign(val1) == -1) {
443 do_negate(val1, neg_val1);
447 if (do_sign(val2) == -1) {
448 do_negate(val2, neg_val2);
453 for (c_outer = 0; c_outer < max_value_size; c_outer++) {
454 if (val2[c_outer] != SC_0) {
455 for (c_inner = 0; c_inner < max_value_size; c_inner++) {
456 /* do the following calculation: *
457 * Add the current carry, the value at position c_outer+c_inner *
458 * and the result of the multiplication of val1[c_inner] and *
459 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
461 /* multiplicate the two digits */
462 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
463 /* add old value to result of multiplication */
464 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
465 /* add carry to the sum */
466 add2 = add_table[_val(add1[0])][_val(carry)];
468 /* all carries together result in new carry. This is always smaller *
470 * Both multiplicands, the carry and the value already in the temp *
471 * buffer are single digits and their value is therefore at most *
474 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
475 * The tables list all operations rem b, so the carry is at most *
476 * (b*b-1)rem b = -1rem b = b-1 */
477 carry = add_table[_val(mul[1])][_val(add1[1])][0];
478 carry = add_table[_val(carry)][_val(add2[1])][0];
480 temp_buffer[c_inner + c_outer] = add2[0];
483 /* A carry may hang over */
484 /* c_outer is always smaller than max_value_size! */
485 temp_buffer[max_value_size + c_outer] = carry;
491 do_negate(temp_buffer, buffer);
493 memcpy(buffer, temp_buffer, calc_buffer_size);
497 * Shift the buffer to left and add a 4 bit digit
499 static void do_push(const char digit, char *buffer)
503 for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
504 buffer[counter+1] = buffer[counter];
510 * Implements truncating integer division and remainder.
512 * Note: This is MOST slow
514 static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
516 const char *dividend = rDividend;
517 const char *minus_divisor;
521 char div_sign = 0; /* remember division result sign */
522 char rem_sign = 0; /* remember remainder result sign */
524 int c_dividend; /* loop counters */
526 neg_val1 = (char*) alloca(calc_buffer_size);
527 neg_val2 = (char*) alloca(calc_buffer_size);
529 /* clear result buffer */
530 memset(quot, SC_0, calc_buffer_size);
531 memset(rem, SC_0, calc_buffer_size);
533 /* if the divisor is zero this won't work (quot is zero) */
534 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
536 /* if the dividend is zero result is zero (quot is zero) */
537 if (sc_comp(dividend, quot) == 0)
540 if (do_sign(dividend) == -1) {
541 do_negate(dividend, neg_val1);
547 do_negate(divisor, neg_val2);
548 if (do_sign(divisor) == -1) {
550 minus_divisor = divisor;
553 minus_divisor = neg_val2;
555 /* if divisor >= dividend division is easy
556 * (remember these are absolute values) */
557 switch (sc_comp(dividend, divisor)) {
558 case 0: /* dividend == divisor */
562 case -1: /* dividend < divisor */
563 memcpy(rem, dividend, calc_buffer_size);
566 default: /* unluckily division is necessary :( */
570 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
571 do_push(dividend[c_dividend], rem);
574 if (sc_comp(rem, divisor) != -1) { /* remainder >= divisor */
575 /* subtract until the remainder becomes negative, this should
576 * be faster than comparing remainder with divisor */
577 do_add(rem, minus_divisor, rem);
579 while (do_sign(rem) == 1) {
580 quot[0] = add_table[_val(quot[0])][SC_1][0];
581 do_add(rem, minus_divisor, rem);
584 /* subtracted one too much */
585 do_add(rem, divisor, rem);
589 /* sets carry if remainder is non-zero ??? */
590 carry_flag = !sc_is_zero(rem);
593 do_negate(quot, quot);
600 * Implements a Shift Left, which can either preserve the sign bit
603 * @todo Assertions seems to be wrong
605 static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed)
614 assert((shift_cnt >= 0) || (0 && "negative leftshift"));
615 assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
616 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
617 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
619 /* if shifting far enough the result is zero */
620 if (shift_cnt >= bitsize) {
621 memset(buffer, SC_0, calc_buffer_size);
625 shift = SHIFT(shift_cnt % 4); /* this is 2 ** (offset % 4) */
626 shift_cnt = shift_cnt / 4;
628 /* shift the single digits some bytes (offset) and some bits (table)
630 for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
631 shl = mul_table[_val(val1[counter])][_val(shift)];
632 buffer[counter + shift_cnt] = shl[0] | carry;
636 shl = mul_table[_val(val1[counter])][_val(shift)];
637 buffer[counter + shift_cnt] = shl[0] | carry;
640 bitoffset = counter - 1;
643 /* fill with zeroes */
644 for (counter = 0; counter < shift_cnt; counter++)
645 buffer[counter] = SC_0;
647 /* if the mode was signed, change sign when the mode's msb is now 1 */
648 shift_cnt = bitoffset + shift_cnt;
649 bitoffset = (bitsize-1) % 4;
650 if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
651 /* this sets the upper bits of the leftmost digit */
652 buffer[shift_cnt] |= min_digit[bitoffset];
653 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
654 buffer[counter] = SC_F;
656 } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
657 /* this clears the upper bits of the leftmost digit */
658 buffer[shift_cnt] &= max_digit[bitoffset];
659 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
660 buffer[counter] = SC_0;
666 * Implements a Shift Right, which can either preserve the sign bit
669 * @param bitsize bitsize of the value to be shifted
671 * @todo Assertions seems to be wrong
673 static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift)
679 int shift_mod, shift_nib;
684 assert((shift_cnt >= 0) || (0 && "negative rightshift"));
685 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
686 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
688 sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
690 /* if shifting far enough the result is either 0 or -1 */
691 if (shift_cnt >= bitsize) {
692 if (!sc_is_zero(val1)) {
695 memset(buffer, sign, calc_buffer_size);
699 shift_mod = shift_cnt & 3;
700 shift_nib = shift_cnt >> 2;
702 /* check if any bits are lost, and set carry_flag if so */
703 for (counter = 0; counter < shift_nib; ++counter) {
704 if (val1[counter] != 0) {
709 if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
712 /* shift digits to the right with offset, carry and all */
713 buffer[0] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
714 for (counter = 1; counter < ((bitsize + 3) >> 2) - shift_nib; counter++) {
715 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
716 buffer[counter] = shrs[0];
717 buffer[counter - 1] |= shrs[1];
720 /* the last digit is special in regard of signed/unsigned shift */
721 bitoffset = bitsize & 3;
722 msd = sign; /* most significant digit */
724 /* remove sign bits if mode was signed and this is an unsigned shift */
725 if (!signed_shift && is_signed) {
726 msd &= max_digit[bitoffset];
729 shrs = shrs_table[_val(msd)][shift_mod];
731 /* signed shift and signed mode and negative value means all bits to the left are set */
732 if (signed_shift && sign == SC_F) {
733 buffer[counter] = shrs[0] | min_digit[bitoffset];
735 buffer[counter] = shrs[0];
739 buffer[counter - 1] |= shrs[1];
741 /* fill with SC_F or SC_0 depending on sign */
742 for (counter++; counter < calc_buffer_size; counter++) {
743 buffer[counter] = sign;
748 * Implements a Rotate Left.
749 * positive: low-order -> high order, negative other direction
751 static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
754 temp1 = (char*) alloca(calc_buffer_size);
755 temp2 = (char*) alloca(calc_buffer_size);
757 offset = offset % radius;
759 /* rotation by multiples of the type length is identity */
761 memmove(buffer, val1, calc_buffer_size);
765 do_shl(val1, temp1, offset, radius, is_signed);
766 do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
767 do_bitor(temp1, temp2, buffer);
768 carry_flag = 0; /* set by shr, but due to rot this is false */
771 /*****************************************************************************
772 * public functions, declared in strcalc.h
773 *****************************************************************************/
774 const void *sc_get_buffer(void)
776 return (void*)calc_buffer;
779 int sc_get_buffer_length(void)
781 return calc_buffer_size;
785 * Do sign extension if the mode is signed, otherwise to zero extension.
787 void sign_extend(void *buffer, ir_mode *mode)
789 char *calc_buffer = (char*) buffer;
790 int bits = get_mode_size_bits(mode) - 1;
791 int nibble = bits >> 2;
792 int max = max_digit[bits & 3];
795 if (mode_is_signed(mode)) {
796 if (calc_buffer[nibble] > max) {
797 /* sign bit is set, we need sign expansion */
799 for (i = nibble + 1; i < calc_buffer_size; ++i)
800 calc_buffer[i] = SC_F;
801 calc_buffer[nibble] |= sex_digit[bits & 3];
803 /* set all bits to zero */
804 for (i = nibble + 1; i < calc_buffer_size; ++i)
805 calc_buffer[i] = SC_0;
806 calc_buffer[nibble] &= zex_digit[bits & 3];
809 /* do zero extension */
810 for (i = nibble + 1; i < calc_buffer_size; ++i)
811 calc_buffer[i] = SC_0;
812 calc_buffer[nibble] &= zex_digit[bits & 3];
816 /* we assume that '0'-'9', 'a'-'z' and 'A'-'Z' are a range.
817 * The C-standard does theoretically allow otherwise. */
818 static inline void check_ascii(void)
820 /* C standard guarantees that '0'-'9' is a range */
833 int sc_val_from_str(char sign, unsigned base, const char *str,
834 size_t len, void *buffer)
838 assert(sign == -1 || sign == 1);
843 assert(base > 1 && base <= 16);
844 sc_base = (char*) alloca(calc_buffer_size);
845 sc_val_from_ulong(base, sc_base);
847 val = (char*) alloca(calc_buffer_size);
849 buffer = calc_buffer;
851 CLEAR_BUFFER(buffer);
854 /* BEGIN string evaluation, from left to right */
858 if (c >= '0' && c <= '9')
860 else if (c >= 'A' && c <= 'F')
862 else if (c >= 'a' && c <= 'f')
871 /* Radix conversion from base b to base B:
872 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
873 /* multiply current value with base */
874 do_mul(sc_base, (const char*) buffer, (char*) buffer);
875 /* add next digit to current value */
876 do_add(val, (const char*) buffer, (char*) buffer);
878 /* get ready for the next letter */
884 do_negate((const char*) buffer, (char*) buffer);
889 void sc_val_from_long(long value, void *buffer)
892 char sign, is_minlong;
894 if (buffer == NULL) buffer = calc_buffer;
895 pos = (char*) buffer;
898 is_minlong = value == LONG_MIN;
900 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
908 CLEAR_BUFFER(buffer);
910 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
911 *pos++ = _digit(value & 0xf);
917 do_inc((const char*) buffer, (char*) buffer);
919 do_negate((const char*) buffer, (char*) buffer);
923 void sc_val_from_ulong(unsigned long value, void *buffer)
927 if (buffer == NULL) buffer = calc_buffer;
928 pos = (unsigned char*) buffer;
930 while (pos < (unsigned char *)buffer + calc_buffer_size) {
931 *pos++ = (unsigned char)_digit(value & 0xf);
936 long sc_val_to_long(const void *val)
941 for (i = calc_buffer_size - 1; i >= 0; i--) {
942 l = (l << 4) + _val(((char *)val)[i]);
947 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
952 if (buffer == NULL) buffer = calc_buffer;
953 CLEAR_BUFFER(buffer);
955 if (!sign) return; /* unsigned means minimum is 0(zero) */
957 pos = (char*) buffer;
960 for (i = 0; i < bits/4; i++)
963 *pos++ = min_digit[bits%4];
965 for (i++; i <= calc_buffer_size - 1; i++)
969 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
974 if (buffer == NULL) buffer = calc_buffer;
975 CLEAR_BUFFER(buffer);
976 pos = (char*) buffer;
978 bits = num_bits - sign;
979 for (i = 0; i < bits/4; i++)
982 *pos++ = max_digit[bits%4];
984 for (i++; i <= calc_buffer_size - 1; i++)
988 void sc_truncate(unsigned int num_bits, void *buffer)
990 char *cbuffer = (char*) buffer;
991 char *pos = cbuffer + (num_bits / 4);
992 char *end = cbuffer + calc_buffer_size;
996 switch (num_bits % 4) {
997 case 0: /* nothing to do */ break;
998 case 1: *pos++ &= SC_1; break;
999 case 2: *pos++ &= SC_3; break;
1000 case 3: *pos++ &= SC_7; break;
1003 for ( ; pos < end; ++pos)
1007 int sc_comp(const void* value1, const void* value2)
1009 int counter = calc_buffer_size - 1;
1010 const char *val1 = (const char *)value1;
1011 const char *val2 = (const char *)value2;
1013 /* compare signs first:
1014 * the loop below can only compare values of the same sign! */
1015 if (do_sign(val1) != do_sign(val2))
1016 return (do_sign(val1) == 1)?(1):(-1);
1018 /* loop until two digits differ, the values are equal if there
1019 * are no such two digits */
1020 while (val1[counter] == val2[counter]) {
1022 if (counter < 0) return 0;
1025 /* the leftmost digit is the most significant, so this returns
1026 * the correct result.
1027 * This implies the digit enum is ordered */
1028 return (val1[counter] > val2[counter]) ? (1) : (-1);
1031 int sc_get_highest_set_bit(const void *value)
1033 const char *val = (const char*)value;
1036 high = calc_buffer_size * 4 - 1;
1038 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1039 if (val[counter] == SC_0)
1042 if (val[counter] > SC_7) return high;
1043 else if (val[counter] > SC_3) return high - 1;
1044 else if (val[counter] > SC_1) return high - 2;
1045 else return high - 3;
1051 int sc_get_lowest_set_bit(const void *value)
1053 const char *val = (const char*)value;
1057 for (counter = 0; counter < calc_buffer_size; counter++) {
1058 switch (val[counter]) {
1085 int sc_get_bit_at(const void *value, unsigned pos)
1087 const char *val = (const char*) value;
1088 unsigned nibble = pos >> 2;
1090 return (val[nibble] & SHIFT(pos & 3)) != SC_0;
1093 void sc_set_bit_at(void *value, unsigned pos)
1095 char *val = (char*) value;
1096 unsigned nibble = pos >> 2;
1098 val[nibble] |= SHIFT(pos & 3);
1101 int sc_is_zero(const void *value)
1103 const char* val = (const char *)value;
1106 for (counter = 0; counter < calc_buffer_size; ++counter) {
1107 if (val[counter] != SC_0)
1113 int sc_is_negative(const void *value)
1115 return do_sign((const char*) value) == -1;
1118 int sc_had_carry(void)
1123 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1125 const char *val = (const char *)value;
1126 int nibble_ofs = 2 * byte_ofs;
1129 /* the current scheme uses one byte to store a nibble */
1130 if (4 * nibble_ofs >= len)
1133 res = _val(val[nibble_ofs]);
1134 if (len > 4 * (nibble_ofs + 1))
1135 res |= _val(val[nibble_ofs + 1]) << 4;
1137 /* kick bits outsize */
1138 if (len - 8 * byte_ofs < 8) {
1139 res &= (1 << (len - 8 * byte_ofs)) - 1;
1145 * convert to a string
1146 * FIXME: Doesn't check buffer bounds
1148 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode)
1150 static const char big_digits[] = "0123456789ABCDEF";
1151 static const char small_digits[] = "0123456789abcdef";
1153 char *base_val, *div1_res, *div2_res, *rem_res;
1154 int counter, nibbles, i, sign, mask;
1157 const char *val = (const char *)value;
1161 const char *digits = small_digits;
1163 base_val = (char*) alloca(calc_buffer_size);
1164 div1_res = (char*) alloca(calc_buffer_size);
1165 div2_res = (char*) alloca(calc_buffer_size);
1166 rem_res = (char*) alloca(calc_buffer_size);
1168 pos = output_buffer + bit_pattern_size;
1173 bits = bit_pattern_size;
1174 #ifdef STRCALC_DEBUG_FULLPRINT
1178 nibbles = bits >> 2;
1182 digits = big_digits;
1184 for (counter = 0; counter < nibbles; ++counter) {
1185 *(--pos) = digits[_val(val[counter])];
1186 #ifdef STRCALC_DEBUG_GROUPPRINT
1187 if ((counter+1)%8 == 0)
1192 /* last nibble must be masked */
1194 mask = zex_digit[(bits & 3) - 1];
1195 x = val[counter++] & mask;
1196 *(--pos) = digits[_val(x)];
1199 /* now kill zeros */
1200 for (; counter > 1; --counter, ++pos) {
1201 #ifdef STRCALC_DEBUG_GROUPPRINT
1202 if (pos[0] == ' ') ++pos;
1210 for (counter = 0; counter < nibbles; ++counter) {
1212 p = binary_table[_val(val[counter])];
1219 /* last nibble must be masked */
1221 mask = zex_digit[(bits & 3) - 1];
1222 x = val[counter++] & mask;
1225 p = binary_table[_val(x)];
1232 /* now kill zeros */
1233 for (counter <<= 2; counter > 1; --counter, ++pos)
1240 memset(base_val, SC_0, calc_buffer_size);
1241 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1245 if (signed_mode && base == SC_DEC) {
1246 /* check for negative values */
1247 if (do_bit(val, bits - 1)) {
1248 do_negate(val, div2_res);
1254 /* transfer data into oscillating buffers */
1255 memset(div1_res, SC_0, calc_buffer_size);
1256 for (counter = 0; counter < nibbles; ++counter)
1257 div1_res[counter] = p[counter];
1259 /* last nibble must be masked */
1261 mask = zex_digit[(bits & 3) - 1];
1262 div1_res[counter] = p[counter] & mask;
1269 do_divmod(m, base_val, n, rem_res);
1273 *(--pos) = digits[_val(rem_res[0])];
1276 for (i = 0; i < calc_buffer_size; ++i)
1287 panic("Unsupported base %d", base);
1292 void init_strcalc(int precision)
1294 if (calc_buffer == NULL) {
1295 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1297 /* round up to multiple of 4 */
1298 precision = (precision + 3) & ~3;
1300 bit_pattern_size = (precision);
1301 calc_buffer_size = (precision / 2);
1302 max_value_size = (precision / 4);
1304 calc_buffer = XMALLOCN(char, calc_buffer_size + 1);
1305 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1307 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));
1312 void finish_strcalc(void)
1314 free(calc_buffer); calc_buffer = NULL;
1315 free(output_buffer); output_buffer = NULL;
1318 int sc_get_precision(void)
1320 return bit_pattern_size;
1324 void sc_add(const void *value1, const void *value2, void *buffer)
1326 CLEAR_BUFFER(calc_buffer);
1329 DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1330 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1332 do_add((const char*) value1, (const char*) value2, (char*) calc_buffer);
1334 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1336 if ((buffer != NULL) && (buffer != calc_buffer)) {
1337 memcpy(buffer, calc_buffer, calc_buffer_size);
1341 void sc_sub(const void *value1, const void *value2, void *buffer)
1343 CLEAR_BUFFER(calc_buffer);
1346 DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1347 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1349 do_sub((const char*) value1, (const char*) value2, calc_buffer);
1351 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1353 if ((buffer != NULL) && (buffer != calc_buffer)) {
1354 memcpy(buffer, calc_buffer, calc_buffer_size);
1358 void sc_neg(const void *value1, void *buffer)
1362 DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1364 do_negate((const char*) value1, calc_buffer);
1366 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1368 if ((buffer != NULL) && (buffer != calc_buffer)) {
1369 memcpy(buffer, calc_buffer, calc_buffer_size);
1373 void sc_and(const void *value1, const void *value2, void *buffer)
1375 CLEAR_BUFFER(calc_buffer);
1378 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1379 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1381 do_bitand((const char*) value1, (const char*) value2, calc_buffer);
1383 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1385 if ((buffer != NULL) && (buffer != calc_buffer)) {
1386 memcpy(buffer, calc_buffer, calc_buffer_size);
1390 void sc_andnot(const void *value1, const void *value2, void *buffer)
1392 CLEAR_BUFFER(calc_buffer);
1395 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1396 DEBUGPRINTF_COMPUTATION(("~%s -> ", sc_print_hex(value2)));
1398 do_bitandnot((const char*) value1, (const char*) value2, calc_buffer);
1400 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1402 if (buffer != NULL && buffer != calc_buffer) {
1403 memcpy(buffer, calc_buffer, calc_buffer_size);
1407 void sc_or(const void *value1, const void *value2, void *buffer)
1409 CLEAR_BUFFER(calc_buffer);
1412 DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1413 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1415 do_bitor((const char*) value1, (const char*) value2, calc_buffer);
1417 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1419 if ((buffer != NULL) && (buffer != calc_buffer)) {
1420 memcpy(buffer, calc_buffer, calc_buffer_size);
1424 void sc_xor(const void *value1, const void *value2, void *buffer)
1426 CLEAR_BUFFER(calc_buffer);
1429 DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1430 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1432 do_bitxor((const char*) value1, (const char*) value2, calc_buffer);
1434 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1436 if ((buffer != NULL) && (buffer != calc_buffer)) {
1437 memcpy(buffer, calc_buffer, calc_buffer_size);
1441 void sc_not(const void *value1, void *buffer)
1443 CLEAR_BUFFER(calc_buffer);
1446 DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1448 do_bitnot((const char*) value1, calc_buffer);
1450 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1452 if ((buffer != NULL) && (buffer != calc_buffer)) {
1453 memcpy(buffer, calc_buffer, calc_buffer_size);
1457 void sc_mul(const void *value1, const void *value2, void *buffer)
1459 CLEAR_BUFFER(calc_buffer);
1462 DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1463 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1465 do_mul((const char*) value1, (const char*) value2, calc_buffer);
1467 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1469 if ((buffer != NULL) && (buffer != calc_buffer)) {
1470 memcpy(buffer, calc_buffer, calc_buffer_size);
1474 void sc_div(const void *value1, const void *value2, void *buffer)
1476 /* temp buffer holding unused result of divmod */
1477 char *unused_res = (char*) alloca(calc_buffer_size);
1479 CLEAR_BUFFER(calc_buffer);
1482 DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1483 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1485 do_divmod((const char*) value1, (const char*) value2, calc_buffer, unused_res);
1487 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1489 if ((buffer != NULL) && (buffer != calc_buffer)) {
1490 memcpy(buffer, calc_buffer, calc_buffer_size);
1494 void sc_mod(const void *value1, const void *value2, void *buffer)
1496 /* temp buffer holding unused result of divmod */
1497 char *unused_res = (char*) alloca(calc_buffer_size);
1499 CLEAR_BUFFER(calc_buffer);
1502 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1503 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1505 do_divmod((const char*) value1, (const char*) value2, unused_res, calc_buffer);
1507 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1509 if ((buffer != NULL) && (buffer != calc_buffer)) {
1510 memcpy(buffer, calc_buffer, calc_buffer_size);
1514 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer)
1516 CLEAR_BUFFER(calc_buffer);
1519 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1520 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1522 do_divmod((const char*) value1, (const char*) value2, (char*) div_buffer, (char*) mod_buffer);
1524 DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1528 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1532 DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1533 do_shl((const char*) val1, calc_buffer, shift_cnt, bitsize, sign);
1535 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1537 if ((buffer != NULL) && (buffer != calc_buffer)) {
1538 memmove(buffer, calc_buffer, calc_buffer_size);
1542 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1544 long offset = sc_val_to_long(val2);
1546 sc_shlI(val1, offset, bitsize, sign, buffer);
1549 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1553 DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1554 do_shr((const char*) val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1556 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1558 if ((buffer != NULL) && (buffer != calc_buffer)) {
1559 memmove(buffer, calc_buffer, calc_buffer_size);
1563 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1565 long shift_cnt = sc_val_to_long(val2);
1567 sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1570 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1572 long offset = sc_val_to_long(val2);
1576 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1577 do_shr((const char*) val1, calc_buffer, offset, bitsize, sign, 1);
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_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1588 long offset = sc_val_to_long(val2);
1592 DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1593 do_rotl((const char*) val1, calc_buffer, offset, bitsize, sign);
1595 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1597 if ((buffer != NULL) && (buffer != calc_buffer)) {
1598 memmove(buffer, calc_buffer, calc_buffer_size);
1602 void sc_zero(void *buffer)
1605 buffer = calc_buffer;
1606 CLEAR_BUFFER(buffer);