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) {
295 for (counter = 0; counter<calc_buffer_size; counter++)
296 buffer[counter] = val[counter] ^ SC_F;
300 * implements the bitwise OR operation
302 static void do_bitor(const char *val1, const char *val2, char *buffer) {
305 for (counter = 0; counter<calc_buffer_size; counter++)
306 buffer[counter] = val1[counter] | val2[counter];
310 * implements the bitwise eXclusive OR operation
312 static void do_bitxor(const char *val1, const char *val2, char *buffer) {
315 for (counter = 0; counter<calc_buffer_size; counter++)
316 buffer[counter] = val1[counter] ^ val2[counter];
320 * implements the bitwise AND operation
322 static void do_bitand(const char *val1, const char *val2, char *buffer) {
325 for (counter = 0; counter<calc_buffer_size; counter++)
326 buffer[counter] = val1[counter] & val2[counter];
330 * implements the bitwise AND not operation
332 static void do_bitandnot(const char *val1, const char *val2, char *buffer)
336 for (counter = 0; counter < calc_buffer_size; ++counter)
337 buffer[counter] = val1[counter] & (SC_F ^ val2[counter]);
341 * returns the sign bit.
343 * @todo This implementation is wrong, as it returns the highest bit of the buffer
344 * NOT the highest bit depending on the real mode
346 static int do_sign(const char *val) {
347 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
351 * returns non-zero if bit at position pos is set
353 static int do_bit(const char *val, int pos) {
355 int nibble = pos >> 2;
357 return _bitisset(val[nibble], bit);
361 * Implements a fast ADD + 1
363 static void do_inc(const char *val, char *buffer) {
366 while (counter++ < calc_buffer_size) {
371 /* No carry here, *val != SC_F */
372 *buffer = add_table[_val(*val)][SC_1][0];
376 /* here a carry could be lost, this is intended because this should
377 * happen only when a value changes sign. */
381 * Implements a unary MINUS
383 static void do_negate(const char *val, char *buffer) {
384 do_bitnot(val, buffer);
385 do_inc(buffer, buffer);
389 * Implements a binary ADD
391 * @todo The implementation of carry is wrong, as it is the
392 * calc_buffer_size carry, not the mode depending
394 static void do_add(const char *val1, const char *val2, char *buffer) {
396 const char *add1, *add2;
399 for (counter = 0; counter < calc_buffer_size; counter++) {
400 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
401 add2 = add_table[_val(add1[0])][_val(carry)];
402 /* carry might be zero */
403 buffer[counter] = add2[0];
404 carry = add_table[_val(add1[1])][_val(add2[1])][0];
406 carry_flag = carry != SC_0;
410 * Implements a binary SUB
412 static void do_sub(const char *val1, const char *val2, char *buffer) {
413 char *temp_buffer = 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) {
423 char *temp_buffer; /* result buffer */
424 char *neg_val1; /* abs of val1 */
425 char *neg_val2; /* abs of val2 */
427 const char *mul, *add1, *add2; /* intermediate result containers */
428 char carry = SC_0; /* container for carries */
429 char sign = 0; /* marks result sign */
430 int c_inner, c_outer; /* loop counters */
432 temp_buffer = alloca(calc_buffer_size);
433 neg_val1 = alloca(calc_buffer_size);
434 neg_val2 = alloca(calc_buffer_size);
436 /* init result buffer to zeros */
437 memset(temp_buffer, SC_0, calc_buffer_size);
439 /* the multiplication works only for positive values, for negative values *
440 * it is necessary to negate them and adjust the result accordingly */
441 if (do_sign(val1) == -1) {
442 do_negate(val1, neg_val1);
446 if (do_sign(val2) == -1) {
447 do_negate(val2, neg_val2);
452 for (c_outer = 0; c_outer < max_value_size; c_outer++) {
453 if (val2[c_outer] != SC_0) {
454 for (c_inner = 0; c_inner < max_value_size; c_inner++) {
455 /* do the following calculation: *
456 * Add the current carry, the value at position c_outer+c_inner *
457 * and the result of the multiplication of val1[c_inner] and *
458 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
460 /* multiplicate the two digits */
461 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
462 /* add old value to result of multiplication */
463 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
464 /* add carry to the sum */
465 add2 = add_table[_val(add1[0])][_val(carry)];
467 /* all carries together result in new carry. This is always smaller *
469 * Both multiplicands, the carry and the value already in the temp *
470 * buffer are single digits and their value is therefore at most *
473 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
474 * The tables list all operations rem b, so the carry is at most *
475 * (b*b-1)rem b = -1rem b = b-1 */
476 carry = add_table[_val(mul[1])][_val(add1[1])][0];
477 carry = add_table[_val(carry)][_val(add2[1])][0];
479 temp_buffer[c_inner + c_outer] = add2[0];
482 /* A carry may hang over */
483 /* c_outer is always smaller than max_value_size! */
484 temp_buffer[max_value_size + c_outer] = carry;
490 do_negate(temp_buffer, buffer);
492 memcpy(buffer, temp_buffer, calc_buffer_size);
496 * Shift the buffer to left and add a 4 bit digit
498 static void do_push(const char digit, char *buffer) {
501 for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
502 buffer[counter+1] = buffer[counter];
508 * Implements truncating integer division and remainder.
510 * Note: This is MOST slow
512 static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem) {
513 const char *dividend = rDividend;
514 const char *minus_divisor;
518 char div_sign = 0; /* remember division result sign */
519 char rem_sign = 0; /* remember remainder result sign */
521 int c_dividend; /* loop counters */
523 neg_val1 = alloca(calc_buffer_size);
524 neg_val2 = alloca(calc_buffer_size);
526 /* clear result buffer */
527 memset(quot, SC_0, calc_buffer_size);
528 memset(rem, SC_0, calc_buffer_size);
530 /* if the divisor is zero this won't work (quot is zero) */
531 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
533 /* if the dividend is zero result is zero (quot is zero) */
534 if (sc_comp(dividend, quot) == 0)
537 if (do_sign(dividend) == -1) {
538 do_negate(dividend, neg_val1);
544 do_negate(divisor, neg_val2);
545 if (do_sign(divisor) == -1) {
547 minus_divisor = divisor;
550 minus_divisor = neg_val2;
552 /* if divisor >= dividend division is easy
553 * (remember these are absolute values) */
554 switch (sc_comp(dividend, divisor)) {
555 case 0: /* dividend == divisor */
559 case -1: /* dividend < divisor */
560 memcpy(rem, dividend, calc_buffer_size);
563 default: /* unluckily division is necessary :( */
567 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
568 do_push(dividend[c_dividend], rem);
571 if (sc_comp(rem, divisor) != -1) { /* remainder >= divisor */
572 /* subtract until the remainder becomes negative, this should
573 * be faster than comparing remainder with divisor */
574 do_add(rem, minus_divisor, rem);
576 while (do_sign(rem) == 1) {
577 quot[0] = add_table[_val(quot[0])][SC_1][0];
578 do_add(rem, minus_divisor, rem);
581 /* subtracted one too much */
582 do_add(rem, divisor, rem);
586 /* sets carry if remainder is non-zero ??? */
587 carry_flag = !sc_is_zero(rem);
590 do_negate(quot, quot);
597 * Implements a Shift Left, which can either preserve the sign bit
600 * @todo Assertions seems to be wrong
602 static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed) {
610 assert((shift_cnt >= 0) || (0 && "negative leftshift"));
611 assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
612 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
613 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
615 /* if shifting far enough the result is zero */
616 if (shift_cnt >= bitsize) {
617 memset(buffer, SC_0, calc_buffer_size);
621 shift = SHIFT(shift_cnt % 4); /* this is 2 ** (offset % 4) */
622 shift_cnt = shift_cnt / 4;
624 /* shift the single digits some bytes (offset) and some bits (table)
626 for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
627 shl = mul_table[_val(val1[counter])][_val(shift)];
628 buffer[counter + shift_cnt] = shl[0] | carry;
632 shl = mul_table[_val(val1[counter])][_val(shift)];
633 buffer[counter + shift_cnt] = shl[0] | carry;
636 bitoffset = counter - 1;
639 /* fill with zeroes */
640 for (counter = 0; counter < shift_cnt; counter++)
641 buffer[counter] = SC_0;
643 /* if the mode was signed, change sign when the mode's msb is now 1 */
644 shift_cnt = bitoffset + shift_cnt;
645 bitoffset = (bitsize-1) % 4;
646 if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
647 /* this sets the upper bits of the leftmost digit */
648 buffer[shift_cnt] |= min_digit[bitoffset];
649 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
650 buffer[counter] = SC_F;
652 } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
653 /* this clears the upper bits of the leftmost digit */
654 buffer[shift_cnt] &= max_digit[bitoffset];
655 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
656 buffer[counter] = SC_0;
662 * Implements a Shift Right, which can either preserve the sign bit
665 * @param bitsize bitsize of the value to be shifted
667 * @todo Assertions seems to be wrong
669 static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift) {
674 int shift_mod, shift_nib;
679 assert((shift_cnt >= 0) || (0 && "negative rightshift"));
680 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
681 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
683 sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
685 /* if shifting far enough the result is either 0 or -1 */
686 if (shift_cnt >= bitsize) {
687 if (!sc_is_zero(val1)) {
690 memset(buffer, sign, calc_buffer_size);
694 shift_mod = shift_cnt & 3;
695 shift_nib = shift_cnt >> 2;
697 /* check if any bits are lost, and set carry_flag if so */
698 for (counter = 0; counter < shift_nib; ++counter) {
699 if (val1[counter] != 0) {
704 if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
707 /* shift digits to the right with offset, carry and all */
708 buffer[0] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
709 for (counter = 1; counter < ((bitsize + 3) >> 2) - shift_nib; counter++) {
710 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
711 buffer[counter] = shrs[0];
712 buffer[counter - 1] |= shrs[1];
715 /* the last digit is special in regard of signed/unsigned shift */
716 bitoffset = bitsize & 3;
717 msd = sign; /* most significant digit */
719 /* remove sign bits if mode was signed and this is an unsigned shift */
720 if (!signed_shift && is_signed) {
721 msd &= max_digit[bitoffset];
724 shrs = shrs_table[_val(msd)][shift_mod];
726 /* signed shift and signed mode and negative value means all bits to the left are set */
727 if (signed_shift && sign == SC_F) {
728 buffer[counter] = shrs[0] | min_digit[bitoffset];
730 buffer[counter] = shrs[0];
734 buffer[counter - 1] |= shrs[1];
736 /* fill with SC_F or SC_0 depending on sign */
737 for (counter++; counter < calc_buffer_size; counter++) {
738 buffer[counter] = sign;
743 * Implements a Rotate Left.
744 * positive: low-order -> high order, negative other direction
746 static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed) {
748 temp1 = alloca(calc_buffer_size);
749 temp2 = alloca(calc_buffer_size);
751 offset = offset % radius;
753 /* rotation by multiples of the type length is identity */
755 memmove(buffer, val1, calc_buffer_size);
759 do_shl(val1, temp1, offset, radius, is_signed);
760 do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
761 do_bitor(temp1, temp2, buffer);
762 carry_flag = 0; /* set by shr, but due to rot this is false */
765 /*****************************************************************************
766 * public functions, declared in strcalc.h
767 *****************************************************************************/
768 const void *sc_get_buffer(void) {
769 return (void*)calc_buffer;
772 int sc_get_buffer_length(void) {
773 return calc_buffer_size;
777 * Do sign extension if the mode is signed, otherwise to zero extension.
779 void sign_extend(void *buffer, ir_mode *mode) {
780 char *calc_buffer = buffer;
781 int bits = get_mode_size_bits(mode) - 1;
782 int nibble = bits >> 2;
783 int max = max_digit[bits & 3];
786 if (mode_is_signed(mode)) {
787 if (calc_buffer[nibble] > max) {
788 /* sign bit is set, we need sign expansion */
790 for (i = nibble + 1; i < calc_buffer_size; ++i)
791 calc_buffer[i] = SC_F;
792 calc_buffer[nibble] |= sex_digit[bits & 3];
794 /* set all bits to zero */
795 for (i = nibble + 1; i < calc_buffer_size; ++i)
796 calc_buffer[i] = SC_0;
797 calc_buffer[nibble] &= zex_digit[bits & 3];
800 /* do zero extension */
801 for (i = nibble + 1; i < calc_buffer_size; ++i)
802 calc_buffer[i] = SC_0;
803 calc_buffer[nibble] &= zex_digit[bits & 3];
807 /* FIXME doesn't check for overflows */
808 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode) {
809 const char *orig_str = str;
810 unsigned int orig_len = len;
815 base = alloca(calc_buffer_size);
816 val = alloca(calc_buffer_size);
818 /* verify valid pointers (not null) */
820 /* a string no characters long is an error */
823 if (buffer == NULL) buffer = calc_buffer;
825 CLEAR_BUFFER(buffer);
829 /* strip leading spaces */
830 while ((len > 0) && (*str == ' ')) { len--; str++; }
832 /* if the first two characters are 0x or 0X -> hex
833 * if the first is a 0 -> oct
834 * else dec, strip leading -/+ and remember sign
836 * only a + or - sign is no number resulting in an error */
840 if (str[1] == 'x' || str[1] == 'X') { /* hex */
843 base[1] = SC_1; base[0] = SC_0;
847 base[1] = SC_0; base[0] = SC_8;
854 base[1] = SC_0; base[0] = SC_A;
861 base[1] = SC_0; base[0] = SC_A;
864 default: /* dec, else would have begun with 0x or 0 */
865 base[1] = SC_0; base[0] = SC_A;
867 } else { /* dec, else would have begun with 0x or 0 */
868 base[1] = SC_0; base[0] = SC_A;
871 /* BEGIN string evaluation, from left to right */
880 if (base[0] > SC_A || base[1] > SC_0) { /* (base > 10) */
881 val[0] = _digit((*str)-'a'+10);
884 fail_char(orig_str, orig_len, *str, str-orig_str+1);
893 if (base[0] > SC_A || base[1] > SC_0) { /* (base > 10) */
894 val[0] = _digit((*str)-'A'+10);
897 fail_char(orig_str, orig_len, *str, str-orig_str+1);
902 if (base[0] > SC_8 || base[1] > SC_0) { /* (base > 8) */
903 val[0] = _digit((*str)-'0');
906 fail_char(orig_str, orig_len, *str, str-orig_str+1);
917 val[0] = _digit((*str)-'0');
921 fail_char(orig_str, orig_len, *str, str-orig_str+1);
924 /* Radix conversion from base b to base B:
925 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
926 do_mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
927 do_add(val, calc_buffer, calc_buffer); /* add next digit to current value */
929 /* get ready for the next letter */
932 } /* while (len > 0 ) */
935 do_negate(calc_buffer, calc_buffer);
937 /* beware: even if hex numbers have no sign, we need sign extension here */
938 sign_extend(calc_buffer, mode);
941 void sc_val_from_long(long value, void *buffer) {
943 char sign, is_minlong;
945 if (buffer == NULL) buffer = calc_buffer;
949 is_minlong = value == LONG_MIN;
951 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
959 CLEAR_BUFFER(buffer);
961 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
962 *pos++ = _digit(value & 0xf);
968 do_inc(buffer, buffer);
970 do_negate(buffer, buffer);
974 void sc_val_from_ulong(unsigned long value, void *buffer) {
977 if (buffer == NULL) buffer = calc_buffer;
980 while (pos < (unsigned char *)buffer + calc_buffer_size) {
981 *pos++ = (unsigned char)_digit(value & 0xf);
986 long sc_val_to_long(const void *val) {
990 for (i = calc_buffer_size - 1; i >= 0; i--) {
991 l = (l << 4) + _val(((char *)val)[i]);
996 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1000 if (buffer == NULL) buffer = calc_buffer;
1001 CLEAR_BUFFER(buffer);
1003 if (!sign) return; /* unsigned means minimum is 0(zero) */
1007 bits = num_bits - 1;
1008 for (i = 0; i < bits/4; i++)
1011 *pos++ = min_digit[bits%4];
1013 for (i++; i <= calc_buffer_size - 1; i++)
1017 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
1021 if (buffer == NULL) buffer = calc_buffer;
1022 CLEAR_BUFFER(buffer);
1025 bits = num_bits - sign;
1026 for (i = 0; i < bits/4; i++)
1029 *pos++ = max_digit[bits%4];
1031 for (i++; i <= calc_buffer_size - 1; i++)
1035 void sc_truncate(unsigned int num_bits, void *buffer) {
1036 char *cbuffer = buffer;
1037 char *pos = cbuffer + (num_bits / 4);
1038 char *end = cbuffer + calc_buffer_size;
1042 switch(num_bits % 4) {
1043 case 0: /* nothing to do */ break;
1044 case 1: *pos++ &= SC_1; break;
1045 case 2: *pos++ &= SC_3; break;
1046 case 3: *pos++ &= SC_7; break;
1049 for( ; pos < end; ++pos)
1053 int sc_comp(const void* value1, const void* value2) {
1054 int counter = calc_buffer_size - 1;
1055 const char *val1 = (const char *)value1;
1056 const char *val2 = (const char *)value2;
1058 /* compare signs first:
1059 * the loop below can only compare values of the same sign! */
1060 if (do_sign(val1) != do_sign(val2))
1061 return (do_sign(val1) == 1)?(1):(-1);
1063 /* loop until two digits differ, the values are equal if there
1064 * are no such two digits */
1065 while (val1[counter] == val2[counter]) {
1067 if (counter < 0) return 0;
1070 /* the leftmost digit is the most significant, so this returns
1071 * the correct result.
1072 * This implies the digit enum is ordered */
1073 return (val1[counter] > val2[counter]) ? (1) : (-1);
1076 int sc_get_highest_set_bit(const void *value) {
1077 const char *val = (const char*)value;
1080 high = calc_buffer_size * 4 - 1;
1082 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1083 if (val[counter] == SC_0)
1086 if (val[counter] > SC_7) return high;
1087 else if (val[counter] > SC_3) return high - 1;
1088 else if (val[counter] > SC_1) return high - 2;
1089 else return high - 3;
1095 int sc_get_lowest_set_bit(const void *value) {
1096 const char *val = (const char*)value;
1100 for (counter = 0; counter < calc_buffer_size; counter++) {
1101 switch (val[counter]) {
1128 int sc_get_bit_at(const void *value, unsigned pos) {
1129 const char *val = value;
1130 unsigned nibble = pos >> 2;
1132 return (val[nibble] & SHIFT(pos & 3)) != SC_0;
1135 void sc_set_bit_at(void *value, unsigned pos)
1138 unsigned nibble = pos >> 2;
1140 val[nibble] |= SHIFT(pos & 3);
1143 int sc_is_zero(const void *value) {
1144 const char* val = (const char *)value;
1147 for (counter = 0; counter < calc_buffer_size; ++counter) {
1148 if (val[counter] != SC_0)
1154 int sc_is_negative(const void *value) {
1155 return do_sign(value) == -1;
1158 int sc_had_carry(void) {
1162 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs) {
1163 const char *val = (const char *)value;
1164 int nibble_ofs = 2 * byte_ofs;
1167 /* the current scheme uses one byte to store a nibble */
1168 if (4 * nibble_ofs >= len)
1171 res = _val(val[nibble_ofs]);
1172 if (len > 4 * (nibble_ofs + 1))
1173 res |= _val(val[nibble_ofs + 1]) << 4;
1175 /* kick bits outsize */
1176 if (len - 8 * byte_ofs < 8) {
1177 res &= (1 << (len - 8 * byte_ofs)) - 1;
1183 * convert to a string
1184 * FIXME: Doesn't check buffer bounds
1186 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode) {
1187 static const char big_digits[] = "0123456789ABCDEF";
1188 static const char small_digits[] = "0123456789abcdef";
1190 char *base_val, *div1_res, *div2_res, *rem_res;
1191 int counter, nibbles, i, sign, mask;
1194 const char *val = (const char *)value;
1198 const char *digits = small_digits;
1200 base_val = alloca(calc_buffer_size);
1201 div1_res = alloca(calc_buffer_size);
1202 div2_res = alloca(calc_buffer_size);
1203 rem_res = alloca(calc_buffer_size);
1205 pos = output_buffer + bit_pattern_size;
1210 bits = bit_pattern_size;
1211 #ifdef STRCALC_DEBUG_FULLPRINT
1215 nibbles = bits >> 2;
1219 digits = big_digits;
1221 for (counter = 0; counter < nibbles; ++counter) {
1222 *(--pos) = digits[_val(val[counter])];
1223 #ifdef STRCALC_DEBUG_GROUPPRINT
1224 if ((counter+1)%8 == 0)
1229 /* last nibble must be masked */
1231 mask = zex_digit[(bits & 3) - 1];
1232 x = val[counter++] & mask;
1233 *(--pos) = digits[_val(x)];
1236 /* now kill zeros */
1237 for (; counter > 1; --counter, ++pos) {
1238 #ifdef STRCALC_DEBUG_GROUPPRINT
1239 if (pos[0] == ' ') ++pos;
1247 for (counter = 0; counter < nibbles; ++counter) {
1249 p = binary_table[_val(val[counter])];
1256 /* last nibble must be masked */
1258 mask = zex_digit[(bits & 3) - 1];
1259 x = val[counter++] & mask;
1262 p = binary_table[_val(x)];
1269 /* now kill zeros */
1270 for (counter <<= 2; counter > 1; --counter, ++pos)
1277 memset(base_val, SC_0, calc_buffer_size);
1278 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1282 if (signed_mode && base == SC_DEC) {
1283 /* check for negative values */
1284 if (do_bit(val, bits - 1)) {
1285 do_negate(val, div2_res);
1291 /* transfer data into oscillating buffers */
1292 memset(div1_res, SC_0, calc_buffer_size);
1293 for (counter = 0; counter < nibbles; ++counter)
1294 div1_res[counter] = p[counter];
1296 /* last nibble must be masked */
1298 mask = zex_digit[(bits & 3) - 1];
1299 div1_res[counter] = p[counter] & mask;
1306 do_divmod(m, base_val, n, rem_res);
1310 *(--pos) = digits[_val(rem_res[0])];
1313 for (i = 0; i < calc_buffer_size; ++i)
1324 panic("Unsupported base %d", base);
1329 void init_strcalc(int precision) {
1330 if (calc_buffer == NULL) {
1331 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1333 /* round up to multiple of 4 */
1334 precision = (precision + 3) & ~3;
1336 bit_pattern_size = (precision);
1337 calc_buffer_size = (precision / 2);
1338 max_value_size = (precision / 4);
1340 calc_buffer = XMALLOCN(char, calc_buffer_size + 1);
1341 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1343 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));
1348 void finish_strcalc(void) {
1349 free(calc_buffer); calc_buffer = NULL;
1350 free(output_buffer); output_buffer = NULL;
1353 int sc_get_precision(void) {
1354 return bit_pattern_size;
1358 void sc_add(const void *value1, const void *value2, void *buffer) {
1359 CLEAR_BUFFER(calc_buffer);
1362 DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1363 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1365 do_add(value1, value2, calc_buffer);
1367 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1369 if ((buffer != NULL) && (buffer != calc_buffer)) {
1370 memcpy(buffer, calc_buffer, calc_buffer_size);
1374 void sc_sub(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_sub(value1, 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_neg(const void *value1, void *buffer) {
1393 DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1395 do_negate(value1, calc_buffer);
1397 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1399 if ((buffer != NULL) && (buffer != calc_buffer)) {
1400 memcpy(buffer, calc_buffer, calc_buffer_size);
1404 void sc_and(const void *value1, const void *value2, void *buffer) {
1405 CLEAR_BUFFER(calc_buffer);
1408 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1409 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1411 do_bitand(value1, value2, calc_buffer);
1413 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1415 if ((buffer != NULL) && (buffer != calc_buffer)) {
1416 memcpy(buffer, calc_buffer, calc_buffer_size);
1420 void sc_andnot(const void *value1, const void *value2, void *buffer)
1422 CLEAR_BUFFER(calc_buffer);
1425 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1426 DEBUGPRINTF_COMPUTATION(("~%s -> ", sc_print_hex(value2)));
1428 do_bitandnot(value1, value2, calc_buffer);
1430 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1432 if (buffer != NULL && buffer != calc_buffer) {
1433 memcpy(buffer, calc_buffer, calc_buffer_size);
1437 void sc_or(const void *value1, const void *value2, void *buffer) {
1438 CLEAR_BUFFER(calc_buffer);
1441 DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1442 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1444 do_bitor(value1, value2, calc_buffer);
1446 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1448 if ((buffer != NULL) && (buffer != calc_buffer)) {
1449 memcpy(buffer, calc_buffer, calc_buffer_size);
1453 void sc_xor(const void *value1, const void *value2, void *buffer) {
1454 CLEAR_BUFFER(calc_buffer);
1457 DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1458 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1460 do_bitxor(value1, value2, calc_buffer);
1462 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1464 if ((buffer != NULL) && (buffer != calc_buffer)) {
1465 memcpy(buffer, calc_buffer, calc_buffer_size);
1469 void sc_not(const void *value1, void *buffer) {
1470 CLEAR_BUFFER(calc_buffer);
1473 DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1475 do_bitnot(value1, calc_buffer);
1477 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1479 if ((buffer != NULL) && (buffer != calc_buffer)) {
1480 memcpy(buffer, calc_buffer, calc_buffer_size);
1484 void sc_mul(const void *value1, const void *value2, void *buffer) {
1485 CLEAR_BUFFER(calc_buffer);
1488 DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1489 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1491 do_mul(value1, value2, calc_buffer);
1493 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1495 if ((buffer != NULL) && (buffer != calc_buffer)) {
1496 memcpy(buffer, calc_buffer, calc_buffer_size);
1500 void sc_div(const void *value1, const void *value2, void *buffer) {
1501 /* temp buffer holding unused result of divmod */
1502 char *unused_res = alloca(calc_buffer_size);
1504 CLEAR_BUFFER(calc_buffer);
1507 DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1508 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1510 do_divmod(value1, value2, calc_buffer, unused_res);
1512 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1514 if ((buffer != NULL) && (buffer != calc_buffer)) {
1515 memcpy(buffer, calc_buffer, calc_buffer_size);
1519 void sc_mod(const void *value1, const void *value2, void *buffer) {
1520 /* temp buffer holding unused result of divmod */
1521 char *unused_res = alloca(calc_buffer_size);
1523 CLEAR_BUFFER(calc_buffer);
1526 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1527 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1529 do_divmod(value1, value2, unused_res, calc_buffer);
1531 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1533 if ((buffer != NULL) && (buffer != calc_buffer)) {
1534 memcpy(buffer, calc_buffer, calc_buffer_size);
1538 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer) {
1539 CLEAR_BUFFER(calc_buffer);
1542 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1543 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1545 do_divmod(value1, value2, div_buffer, mod_buffer);
1547 DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1551 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1554 DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1555 do_shl(val1, calc_buffer, shift_cnt, bitsize, sign);
1557 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1559 if ((buffer != NULL) && (buffer != calc_buffer)) {
1560 memmove(buffer, calc_buffer, calc_buffer_size);
1564 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1565 long offset = sc_val_to_long(val2);
1567 sc_shlI(val1, offset, bitsize, sign, buffer);
1570 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
1573 DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1574 do_shr(val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1576 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1578 if ((buffer != NULL) && (buffer != calc_buffer)) {
1579 memmove(buffer, calc_buffer, calc_buffer_size);
1583 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1584 long shift_cnt = sc_val_to_long(val2);
1586 sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1589 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1590 long offset = sc_val_to_long(val2);
1594 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1595 do_shr(val1, calc_buffer, offset, bitsize, sign, 1);
1597 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1599 if ((buffer != NULL) && (buffer != calc_buffer)) {
1600 memmove(buffer, calc_buffer, calc_buffer_size);
1604 void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
1605 long offset = sc_val_to_long(val2);
1609 DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1610 do_rotl(val1, calc_buffer, offset, bitsize, sign);
1612 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1614 if ((buffer != NULL) && (buffer != calc_buffer)) {
1615 memmove(buffer, calc_buffer, calc_buffer_size);
1619 void sc_zero(void *buffer) {
1621 buffer = calc_buffer;
1622 CLEAR_BUFFER(buffer);