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 char const add_table[16][16][2] = {
90 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
91 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
92 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
93 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
95 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
96 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
97 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
98 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
100 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
101 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
102 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
103 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
105 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
106 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
107 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
108 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
110 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
111 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
112 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
113 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
115 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
116 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
117 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
118 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
120 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
121 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
122 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
123 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
125 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
126 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
127 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
128 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
130 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
131 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
132 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
133 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
135 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
136 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
137 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
138 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
140 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
141 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
142 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
143 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
145 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
146 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
147 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
148 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
150 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
151 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
152 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
153 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
155 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
156 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
157 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
158 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
160 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
161 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
162 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
163 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
165 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
166 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
167 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
168 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
171 static char const mul_table[16][16][2] = {
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},
175 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
177 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
178 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
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} },
182 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
183 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
184 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
185 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
187 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
188 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
189 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
190 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
192 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
193 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
194 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
195 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
197 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
198 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
199 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
200 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
202 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
203 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
204 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
205 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
207 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
208 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
209 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
210 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
212 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
213 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
214 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
215 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
217 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
218 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
219 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
220 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
222 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
223 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
224 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
225 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
227 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
228 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
229 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
230 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
232 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
233 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
234 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
235 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
237 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
238 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
239 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
240 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
242 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
243 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
244 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
245 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
247 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
248 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
249 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
250 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
253 static char const shrs_table[16][4][2] = {
254 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
255 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
256 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
257 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
258 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
259 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
260 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
261 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
262 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
263 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
264 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
265 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
266 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
267 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
268 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
269 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
272 /** converting a digit to a binary string */
273 static const char *binary_table[16] = {
274 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
275 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
278 /*****************************************************************************
280 *****************************************************************************/
281 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
282 const char *file, int line) {
284 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
285 while (len-- && *str) printf("%c", *str++); printf("\n");
286 while (--pos) printf(" "); printf("^\n");
291 * implements the bitwise NOT operation
293 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) {
306 for (counter = 0; counter<calc_buffer_size; counter++)
307 buffer[counter] = val1[counter] | val2[counter];
311 * implements the bitwise eXclusive OR operation
313 static void do_bitxor(const char *val1, const char *val2, char *buffer) {
316 for (counter = 0; counter<calc_buffer_size; counter++)
317 buffer[counter] = val1[counter] ^ val2[counter];
321 * implements the bitwise AND operation
323 static void do_bitand(const char *val1, const char *val2, char *buffer) {
326 for (counter = 0; counter<calc_buffer_size; counter++)
327 buffer[counter] = val1[counter] & val2[counter];
331 * returns the sign bit.
333 * @todo This implementation is wrong, as it returns the highest bit of the buffer
334 * NOT the highest bit depending on the real mode
336 static int do_sign(const char *val) {
337 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
341 * returns non-zero if bit at position pos is set
343 static int do_bit(const char *val, int pos) {
345 int nibble = pos >> 2;
347 return _bitisset(val[nibble], bit);
351 * Implements a fast ADD + 1
353 static void do_inc(const char *val, char *buffer) {
356 while (counter++ < calc_buffer_size) {
361 /* No carry here, *val != SC_F */
362 *buffer = add_table[_val(*val)][SC_1][0];
366 /* here a carry could be lost, this is intended because this should
367 * happen only when a value changes sign. */
371 * Implements a unary MINUS
373 static void do_negate(const char *val, char *buffer) {
374 do_bitnot(val, buffer);
375 do_inc(buffer, buffer);
379 * Implements a binary ADD
381 * @todo The implementation of carry is wrong, as it is the
382 * calc_buffer_size carry, not the mode depending
384 static void do_add(const char *val1, const char *val2, char *buffer) {
386 const char *add1, *add2;
389 for (counter = 0; counter < calc_buffer_size; counter++) {
390 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
391 add2 = add_table[_val(add1[0])][_val(carry)];
392 /* carry might be zero */
393 buffer[counter] = add2[0];
394 carry = add_table[_val(add1[1])][_val(add2[1])][0];
396 carry_flag = carry != SC_0;
400 * Implements a binary SUB
402 static void do_sub(const char *val1, const char *val2, char *buffer) {
403 char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
405 do_negate(val2, temp_buffer);
406 do_add(val1, temp_buffer, buffer);
410 * Implements a binary MUL
412 static void do_mul(const char *val1, const char *val2, char *buffer) {
413 char *temp_buffer; /* result buffer */
414 char *neg_val1; /* abs of val1 */
415 char *neg_val2; /* abs of val2 */
417 const char *mul, *add1, *add2; /* intermediate result containers */
418 char carry = SC_0; /* container for carries */
419 char sign = 0; /* marks result sign */
420 int c_inner, c_outer; /* loop counters */
422 temp_buffer = alloca(calc_buffer_size);
423 neg_val1 = alloca(calc_buffer_size);
424 neg_val2 = alloca(calc_buffer_size);
426 /* init result buffer to zeros */
427 memset(temp_buffer, SC_0, calc_buffer_size);
429 /* the multiplication works only for positive values, for negative values *
430 * it is necessary to negate them and adjust the result accordingly */
431 if (do_sign(val1) == -1) {
432 do_negate(val1, neg_val1);
436 if (do_sign(val2) == -1) {
437 do_negate(val2, neg_val2);
442 for (c_outer = 0; c_outer < max_value_size; c_outer++) {
443 if (val2[c_outer] != SC_0) {
444 for (c_inner = 0; c_inner < max_value_size; c_inner++) {
445 /* do the following calculation: *
446 * Add the current carry, the value at position c_outer+c_inner *
447 * and the result of the multiplication of val1[c_inner] and *
448 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
450 /* multiplicate the two digits */
451 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
452 /* add old value to result of multiplication */
453 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
454 /* add carry to the sum */
455 add2 = add_table[_val(add1[0])][_val(carry)];
457 /* all carries together result in new carry. This is always smaller *
459 * Both multiplicands, the carry and the value already in the temp *
460 * buffer are single digits and their value is therefore at most *
463 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
464 * The tables list all operations rem b, so the carry is at most *
465 * (b*b-1)rem b = -1rem b = b-1 */
466 carry = add_table[_val(mul[1])][_val(add1[1])][0];
467 carry = add_table[_val(carry)][_val(add2[1])][0];
469 temp_buffer[c_inner + c_outer] = add2[0];
472 /* A carry may hang over */
473 /* c_outer is always smaller than max_value_size! */
474 temp_buffer[max_value_size + c_outer] = carry;
480 do_negate(temp_buffer, buffer);
482 memcpy(buffer, temp_buffer, calc_buffer_size);
486 * Shift the buffer to left and add a 4 bit digit
488 static void do_push(const char digit, char *buffer) {
491 for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
492 buffer[counter+1] = buffer[counter];
498 * Implements truncating integer division and remainder.
500 * Note: This is MOST slow
502 static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem) {
503 const char *dividend = rDividend;
504 const char *minus_divisor;
508 char div_sign = 0; /* remember division result sign */
509 char rem_sign = 0; /* remember remainder result sign */
511 int c_dividend; /* loop counters */
513 neg_val1 = alloca(calc_buffer_size);
514 neg_val2 = alloca(calc_buffer_size);
516 /* clear result buffer */
517 memset(quot, SC_0, calc_buffer_size);
518 memset(rem, SC_0, calc_buffer_size);
520 /* if the divisor is zero this won't work (quot is zero) */
521 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
523 /* if the dividend is zero result is zero (quot is zero) */
524 if (sc_comp(dividend, quot) == 0)
527 if (do_sign(dividend) == -1) {
528 do_negate(dividend, neg_val1);
534 do_negate(divisor, neg_val2);
535 if (do_sign(divisor) == -1) {
537 minus_divisor = divisor;
540 minus_divisor = neg_val2;
542 /* if divisor >= dividend division is easy
543 * (remember these are absolute values) */
544 switch (sc_comp(dividend, divisor)) {
545 case 0: /* dividend == divisor */
549 case -1: /* dividend < divisor */
550 memcpy(rem, dividend, calc_buffer_size);
553 default: /* unluckily division is necessary :( */
557 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
558 do_push(dividend[c_dividend], rem);
561 if (sc_comp(rem, divisor) != -1) { /* remainder >= divisor */
562 /* subtract until the remainder becomes negative, this should
563 * be faster than comparing remainder with divisor */
564 do_add(rem, minus_divisor, rem);
566 while (do_sign(rem) == 1) {
567 quot[0] = add_table[_val(quot[0])][SC_1][0];
568 do_add(rem, minus_divisor, rem);
571 /* subtracted one too much */
572 do_add(rem, divisor, rem);
576 /* sets carry if remainder is non-zero ??? */
577 carry_flag = !sc_is_zero(rem);
580 do_negate(quot, quot);
587 * Implements a Shift Left, which can either preserve the sign bit
590 * @todo Assertions seems to be wrong
592 static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed) {
600 assert((shift_cnt >= 0) || (0 && "negative leftshift"));
601 assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
602 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
603 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
605 /* if shifting far enough the result is zero */
606 if (shift_cnt >= bitsize) {
607 memset(buffer, SC_0, calc_buffer_size);
611 shift = shift_table[_val(shift_cnt%4)]; /* this is 2 ** (offset % 4) */
612 shift_cnt = shift_cnt / 4;
614 /* shift the single digits some bytes (offset) and some bits (table)
616 for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
617 shl = mul_table[_val(val1[counter])][_val(shift)];
618 buffer[counter + shift_cnt] = shl[0] | carry;
622 shl = mul_table[_val(val1[counter])][_val(shift)];
623 buffer[counter + shift_cnt] = shl[0] | carry;
626 bitoffset = counter - 1;
629 /* fill with zeroes */
630 for (counter = 0; counter < shift_cnt; counter++)
631 buffer[counter] = SC_0;
633 /* if the mode was signed, change sign when the mode's msb is now 1 */
634 shift_cnt = bitoffset + shift_cnt;
635 bitoffset = (bitsize-1) % 4;
636 if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
637 /* this sets the upper bits of the leftmost digit */
638 buffer[shift_cnt] |= min_digit[bitoffset];
639 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
640 buffer[counter] = SC_F;
642 } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
643 /* this clears the upper bits of the leftmost digit */
644 buffer[shift_cnt] &= max_digit[bitoffset];
645 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
646 buffer[counter] = SC_0;
652 * Implements a Shift Right, which can either preserve the sign bit
655 * @param bitsize bitsize of the value to be shifted
657 * @todo Assertions seems to be wrong
659 static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift) {
664 int shift_mod, shift_nib;
669 assert((shift_cnt >= 0) || (0 && "negative rightshift"));
670 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
671 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
673 sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
675 /* if shifting far enough the result is either 0 or -1 */
676 if (shift_cnt >= bitsize) {
677 if (!sc_is_zero(val1)) {
680 memset(buffer, sign, calc_buffer_size);
684 shift_mod = shift_cnt & 3;
685 shift_nib = shift_cnt >> 2;
687 /* check if any bits are lost, and set carry_flag if so */
688 for (counter = 0; counter < shift_nib; ++counter) {
689 if (val1[counter] != 0) {
694 if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
697 /* shift digits to the right with offset, carry and all */
698 buffer[0] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
699 for (counter = 1; counter < ((bitsize + 3) >> 2) - shift_nib; counter++) {
700 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
701 buffer[counter] = shrs[0];
702 buffer[counter - 1] |= shrs[1];
705 /* the last digit is special in regard of signed/unsigned shift */
706 bitoffset = bitsize & 3;
707 msd = sign; /* most significant digit */
709 /* remove sign bits if mode was signed and this is an unsigned shift */
710 if (!signed_shift && is_signed) {
711 msd &= max_digit[bitoffset];
714 shrs = shrs_table[_val(msd)][shift_mod];
716 /* signed shift and signed mode and negative value means all bits to the left are set */
717 if (signed_shift && sign == SC_F) {
718 buffer[counter] = shrs[0] | min_digit[bitoffset];
720 buffer[counter] = shrs[0];
724 buffer[counter - 1] |= shrs[1];
726 /* fill with SC_F or SC_0 depending on sign */
727 for (counter++; counter < calc_buffer_size; counter++) {
728 buffer[counter] = sign;
733 * Implements a Rotate Left.
734 * positive: low-order -> high order, negative other direction
736 static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed) {
738 temp1 = alloca(calc_buffer_size);
739 temp2 = alloca(calc_buffer_size);
741 offset = offset % radius;
743 /* rotation by multiples of the type length is identity */
745 memmove(buffer, val1, calc_buffer_size);
749 do_shl(val1, temp1, offset, radius, is_signed);
750 do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
751 do_bitor(temp1, temp2, buffer);
752 carry_flag = 0; /* set by shr, but due to rot this is false */
755 /*****************************************************************************
756 * public functions, declared in strcalc.h
757 *****************************************************************************/
758 const void *sc_get_buffer(void) {
759 return (void*)calc_buffer;
762 int sc_get_buffer_length(void) {
763 return calc_buffer_size;
767 * Do sign extension if the mode is signed, otherwise to zero extension.
769 void sign_extend(void *buffer, ir_mode *mode) {
770 char *calc_buffer = buffer;
771 int bits = get_mode_size_bits(mode) - 1;
772 int nibble = bits >> 2;
773 int max = max_digit[bits & 3];
776 if (mode_is_signed(mode)) {
777 if (calc_buffer[nibble] > max) {
778 /* sign bit is set, we need sign expansion */
780 for (i = nibble + 1; i < calc_buffer_size; ++i)
781 calc_buffer[i] = SC_F;
782 calc_buffer[nibble] |= sex_digit[bits & 3];
784 /* set all bits to zero */
785 for (i = nibble + 1; i < calc_buffer_size; ++i)
786 calc_buffer[i] = SC_0;
787 calc_buffer[nibble] &= zex_digit[bits & 3];
790 /* do zero extension */
791 for (i = nibble + 1; i < calc_buffer_size; ++i)
792 calc_buffer[i] = SC_0;
793 calc_buffer[nibble] &= zex_digit[bits & 3];
797 /* FIXME doesn't check for overflows */
798 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode) {
799 const char *orig_str = str;
800 unsigned int orig_len = len;
805 base = alloca(calc_buffer_size);
806 val = alloca(calc_buffer_size);
808 /* verify valid pointers (not null) */
810 /* a string no characters long is an error */
813 if (buffer == NULL) buffer = calc_buffer;
815 CLEAR_BUFFER(buffer);
819 /* strip leading spaces */
820 while ((len > 0) && (*str == ' ')) { len--; str++; }
822 /* if the first two characters are 0x or 0X -> hex
823 * if the first is a 0 -> oct
824 * else dec, strip leading -/+ and remember sign
826 * only a + or - sign is no number resulting in an error */
830 if (str[1] == 'x' || str[1] == 'X') { /* hex */
833 base[1] = SC_1; base[0] = SC_0;
837 base[1] = SC_0; base[0] = SC_8;
844 base[1] = SC_0; base[0] = SC_A;
851 base[1] = SC_0; base[0] = SC_A;
854 default: /* dec, else would have begun with 0x or 0 */
855 base[1] = SC_0; base[0] = SC_A;
857 } else { /* dec, else would have begun with 0x or 0 */
858 base[1] = SC_0; base[0] = SC_A;
861 /* BEGIN string evaluation, from left to right */
870 if (base[0] > SC_A || base[1] > SC_0) { /* (base > 10) */
871 val[0] = _digit((*str)-'a'+10);
874 fail_char(orig_str, orig_len, *str, str-orig_str+1);
883 if (base[0] > SC_A || base[1] > SC_0) { /* (base > 10) */
884 val[0] = _digit((*str)-'A'+10);
887 fail_char(orig_str, orig_len, *str, str-orig_str+1);
892 if (base[0] > SC_8 || base[1] > SC_0) { /* (base > 8) */
893 val[0] = _digit((*str)-'0');
896 fail_char(orig_str, orig_len, *str, str-orig_str+1);
907 val[0] = _digit((*str)-'0');
911 fail_char(orig_str, orig_len, *str, str-orig_str+1);
914 /* Radix conversion from base b to base B:
915 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
916 do_mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
917 do_add(val, calc_buffer, calc_buffer); /* add next digit to current value */
919 /* get ready for the next letter */
922 } /* while (len > 0 ) */
925 do_negate(calc_buffer, calc_buffer);
927 /* beware: even if hex numbers have no sign, we need sign extension here */
928 sign_extend(calc_buffer, mode);
931 void sc_val_from_long(long value, void *buffer) {
933 char sign, is_minlong;
935 if (buffer == NULL) buffer = calc_buffer;
939 is_minlong = value == LONG_MIN;
941 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
949 CLEAR_BUFFER(buffer);
951 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
952 *pos++ = _digit(value & 0xf);
958 do_inc(buffer, buffer);
960 do_negate(buffer, buffer);
964 void sc_val_from_ulong(unsigned long value, void *buffer) {
967 if (buffer == NULL) buffer = calc_buffer;
970 while (pos < (unsigned char *)buffer + calc_buffer_size) {
971 *pos++ = (unsigned char)_digit(value & 0xf);
976 long sc_val_to_long(const void *val) {
980 for (i = calc_buffer_size - 1; i >= 0; i--) {
981 l = (l << 4) + _val(((char *)val)[i]);
986 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
990 if (buffer == NULL) buffer = calc_buffer;
991 CLEAR_BUFFER(buffer);
993 if (!sign) return; /* unsigned means minimum is 0(zero) */
998 for (i = 0; i < bits/4; i++)
1001 *pos++ = min_digit[bits%4];
1003 for (i++; i <= calc_buffer_size - 1; i++)
1007 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1011 if (buffer == NULL) buffer = calc_buffer;
1012 CLEAR_BUFFER(buffer);
1015 bits = num_bits - sign;
1016 for (i = 0; i < bits/4; i++)
1019 *pos++ = max_digit[bits%4];
1021 for (i++; i <= calc_buffer_size - 1; i++)
1025 void sc_truncate(unsigned int num_bits, void *buffer) {
1026 char *cbuffer = buffer;
1027 char *pos = cbuffer + (num_bits / 4);
1028 char *end = cbuffer + calc_buffer_size;
1032 switch(num_bits % 4) {
1033 case 0: /* nothing to do */ break;
1034 case 1: *pos++ &= SC_1; break;
1035 case 2: *pos++ &= SC_3; break;
1036 case 3: *pos++ &= SC_7; break;
1039 for( ; pos < end; ++pos)
1043 int sc_comp(const void* value1, const void* value2) {
1044 int counter = calc_buffer_size - 1;
1045 const char *val1 = (const char *)value1;
1046 const char *val2 = (const char *)value2;
1048 /* compare signs first:
1049 * the loop below can only compare values of the same sign! */
1050 if (do_sign(val1) != do_sign(val2))
1051 return (do_sign(val1) == 1)?(1):(-1);
1053 /* loop until two digits differ, the values are equal if there
1054 * are no such two digits */
1055 while (val1[counter] == val2[counter]) {
1057 if (counter < 0) return 0;
1060 /* the leftmost digit is the most significant, so this returns
1061 * the correct result.
1062 * This implies the digit enum is ordered */
1063 return (val1[counter] > val2[counter]) ? (1) : (-1);
1066 int sc_get_highest_set_bit(const void *value) {
1067 const char *val = (const char*)value;
1070 high = calc_buffer_size * 4 - 1;
1072 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1073 if (val[counter] == SC_0)
1076 if (val[counter] > SC_7) return high;
1077 else if (val[counter] > SC_3) return high - 1;
1078 else if (val[counter] > SC_1) return high - 2;
1079 else return high - 3;
1085 int sc_get_lowest_set_bit(const void *value) {
1086 const char *val = (const char*)value;
1090 for (counter = 0; counter < calc_buffer_size; counter++) {
1091 switch (val[counter]) {
1118 int sc_get_bit_at(const void *value, unsigned pos) {
1119 const char *val = value;
1120 unsigned nibble = pos >> 2;
1122 return (val[nibble] & shift_table[pos & 3]) != SC_0;
1125 void sc_set_bit_at(void *value, unsigned pos)
1128 unsigned nibble = pos >> 2;
1130 val[nibble] |= shift_table[pos & 3];
1133 int sc_is_zero(const void *value) {
1134 const char* val = (const char *)value;
1137 for (counter = 0; counter < calc_buffer_size; ++counter) {
1138 if (val[counter] != SC_0)
1144 int sc_is_negative(const void *value) {
1145 return do_sign(value) == -1;
1148 int sc_had_carry(void) {
1152 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs) {
1153 const char *val = (const char *)value;
1154 int nibble_ofs = 2 * byte_ofs;
1157 /* the current scheme uses one byte to store a nibble */
1158 if (4 * nibble_ofs >= len)
1161 res = _val(val[nibble_ofs]);
1162 if (len > 4 * (nibble_ofs + 1))
1163 res |= _val(val[nibble_ofs + 1]) << 4;
1165 /* kick bits outsize */
1166 if (len - 8 * byte_ofs < 8) {
1167 res &= (1 << (len - 8 * byte_ofs)) - 1;
1173 * convert to a string
1174 * FIXME: Doesn't check buffer bounds
1176 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode) {
1177 static const char big_digits[] = "0123456789ABCDEF";
1178 static const char small_digits[] = "0123456789abcdef";
1180 char *base_val, *div1_res, *div2_res, *rem_res;
1181 int counter, nibbles, i, sign, mask;
1184 const char *val = (const char *)value;
1188 const char *digits = small_digits;
1190 base_val = alloca(calc_buffer_size);
1191 div1_res = alloca(calc_buffer_size);
1192 div2_res = alloca(calc_buffer_size);
1193 rem_res = alloca(calc_buffer_size);
1195 pos = output_buffer + bit_pattern_size;
1200 bits = bit_pattern_size;
1201 #ifdef STRCALC_DEBUG_FULLPRINT
1205 nibbles = bits >> 2;
1209 digits = big_digits;
1211 for (counter = 0; counter < nibbles; ++counter) {
1212 *(--pos) = digits[_val(val[counter])];
1213 #ifdef STRCALC_DEBUG_GROUPPRINT
1214 if ((counter+1)%8 == 0)
1219 /* last nibble must be masked */
1221 mask = zex_digit[(bits & 3) - 1];
1222 x = val[counter++] & mask;
1223 *(--pos) = digits[_val(x)];
1226 /* now kill zeros */
1227 for (; counter > 1; --counter, ++pos) {
1228 #ifdef STRCALC_DEBUG_GROUPPRINT
1229 if (pos[0] == ' ') ++pos;
1237 for (counter = 0; counter < nibbles; ++counter) {
1239 p = binary_table[_val(val[counter])];
1246 /* last nibble must be masked */
1248 mask = zex_digit[(bits & 3) - 1];
1249 x = val[counter++] & mask;
1252 p = binary_table[_val(x)];
1259 /* now kill zeros */
1260 for (counter <<= 2; counter > 1; --counter, ++pos)
1267 memset(base_val, SC_0, calc_buffer_size);
1268 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1272 if (signed_mode && base == SC_DEC) {
1273 /* check for negative values */
1274 if (do_bit(val, bits - 1)) {
1275 do_negate(val, div2_res);
1281 /* transfer data into oscillating buffers */
1282 memset(div1_res, SC_0, calc_buffer_size);
1283 for (counter = 0; counter < nibbles; ++counter)
1284 div1_res[counter] = p[counter];
1286 /* last nibble must be masked */
1288 mask = zex_digit[(bits & 3) - 1];
1289 div1_res[counter] = p[counter] & mask;
1296 do_divmod(m, base_val, n, rem_res);
1300 *(--pos) = digits[_val(rem_res[0])];
1303 for (i = 0; i < calc_buffer_size; ++i)
1314 panic("Unsupported base %d", base);
1319 void init_strcalc(int precision) {
1320 if (calc_buffer == NULL) {
1321 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1323 /* round up to multiple of 4 */
1324 precision = (precision + 3) & ~3;
1326 bit_pattern_size = (precision);
1327 calc_buffer_size = (precision / 2);
1328 max_value_size = (precision / 4);
1330 calc_buffer = XMALLOCN(char, calc_buffer_size + 1);
1331 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1333 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));
1338 void finish_strcalc(void) {
1339 free(calc_buffer); calc_buffer = NULL;
1340 free(output_buffer); output_buffer = NULL;
1343 int sc_get_precision(void) {
1344 return bit_pattern_size;
1348 void sc_add(const void *value1, const void *value2, void *buffer) {
1349 CLEAR_BUFFER(calc_buffer);
1352 DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1353 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1355 do_add(value1, value2, calc_buffer);
1357 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1359 if ((buffer != NULL) && (buffer != calc_buffer)) {
1360 memcpy(buffer, calc_buffer, calc_buffer_size);
1364 void sc_sub(const void *value1, const void *value2, void *buffer) {
1365 CLEAR_BUFFER(calc_buffer);
1368 DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1369 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1371 do_sub(value1, value2, calc_buffer);
1373 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1375 if ((buffer != NULL) && (buffer != calc_buffer)) {
1376 memcpy(buffer, calc_buffer, calc_buffer_size);
1380 void sc_neg(const void *value1, void *buffer) {
1383 DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1385 do_negate(value1, calc_buffer);
1387 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1389 if ((buffer != NULL) && (buffer != calc_buffer)) {
1390 memcpy(buffer, calc_buffer, calc_buffer_size);
1394 void sc_and(const void *value1, const void *value2, void *buffer) {
1395 CLEAR_BUFFER(calc_buffer);
1398 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1399 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1401 do_bitand(value1, value2, calc_buffer);
1403 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1405 if ((buffer != NULL) && (buffer != calc_buffer)) {
1406 memcpy(buffer, calc_buffer, calc_buffer_size);
1410 void sc_or(const void *value1, const void *value2, void *buffer) {
1411 CLEAR_BUFFER(calc_buffer);
1414 DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1415 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1417 do_bitor(value1, value2, calc_buffer);
1419 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1421 if ((buffer != NULL) && (buffer != calc_buffer)) {
1422 memcpy(buffer, calc_buffer, calc_buffer_size);
1426 void sc_xor(const void *value1, const void *value2, void *buffer) {
1427 CLEAR_BUFFER(calc_buffer);
1430 DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1431 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1433 do_bitxor(value1, value2, calc_buffer);
1435 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1437 if ((buffer != NULL) && (buffer != calc_buffer)) {
1438 memcpy(buffer, calc_buffer, calc_buffer_size);
1442 void sc_not(const void *value1, void *buffer) {
1443 CLEAR_BUFFER(calc_buffer);
1446 DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1448 do_bitnot(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) {
1458 CLEAR_BUFFER(calc_buffer);
1461 DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1462 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1464 do_mul(value1, value2, calc_buffer);
1466 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1468 if ((buffer != NULL) && (buffer != calc_buffer)) {
1469 memcpy(buffer, calc_buffer, calc_buffer_size);
1473 void sc_div(const void *value1, const void *value2, void *buffer) {
1474 /* temp buffer holding unused result of divmod */
1475 char *unused_res = alloca(calc_buffer_size);
1477 CLEAR_BUFFER(calc_buffer);
1480 DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1481 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1483 do_divmod(value1, value2, calc_buffer, unused_res);
1485 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1487 if ((buffer != NULL) && (buffer != calc_buffer)) {
1488 memcpy(buffer, calc_buffer, calc_buffer_size);
1492 void sc_mod(const void *value1, const void *value2, void *buffer) {
1493 /* temp buffer holding unused result of divmod */
1494 char *unused_res = alloca(calc_buffer_size);
1496 CLEAR_BUFFER(calc_buffer);
1499 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1500 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1502 do_divmod(value1, value2, unused_res, calc_buffer);
1504 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1506 if ((buffer != NULL) && (buffer != calc_buffer)) {
1507 memcpy(buffer, calc_buffer, calc_buffer_size);
1511 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer) {
1512 CLEAR_BUFFER(calc_buffer);
1515 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1516 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1518 do_divmod(value1, value2, div_buffer, mod_buffer);
1520 DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1524 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1527 DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1528 do_shl(val1, calc_buffer, shift_cnt, bitsize, sign);
1530 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1532 if ((buffer != NULL) && (buffer != calc_buffer)) {
1533 memmove(buffer, calc_buffer, calc_buffer_size);
1537 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1538 long offset = sc_val_to_long(val2);
1540 sc_shlI(val1, offset, bitsize, sign, buffer);
1543 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1546 DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1547 do_shr(val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1549 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1551 if ((buffer != NULL) && (buffer != calc_buffer)) {
1552 memmove(buffer, calc_buffer, calc_buffer_size);
1556 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1557 long shift_cnt = sc_val_to_long(val2);
1559 sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1562 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1563 long offset = sc_val_to_long(val2);
1567 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1568 do_shr(val1, calc_buffer, offset, bitsize, sign, 1);
1570 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1572 if ((buffer != NULL) && (buffer != calc_buffer)) {
1573 memmove(buffer, calc_buffer, calc_buffer_size);
1577 void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1578 long offset = sc_val_to_long(val2);
1582 DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1583 do_rotl(val1, calc_buffer, offset, bitsize, sign);
1585 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1587 if ((buffer != NULL) && (buffer != calc_buffer)) {
1588 memmove(buffer, calc_buffer, calc_buffer_size);
1592 void sc_zero(void *buffer) {
1594 buffer = calc_buffer;
1595 CLEAR_BUFFER(buffer);