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
39 * local definitions and macros
41 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, calc_buffer_size)
42 #define SHIFT(count) (SC_1 << (count))
43 #define _val(a) ((a)-SC_0)
44 #define _digit(a) ((a)+SC_0)
45 #define _bitisset(digit, pos) (((digit) & SHIFT(pos)) != SC_0)
47 /* shortcut output for debugging */
48 # define sc_print_hex(a) sc_print((a), 0, SC_HEX, 0)
49 # define sc_print_dec(a) sc_print((a), 0, SC_DEC, 1)
50 # define sc_print_oct(a) sc_print((a), 0, SC_OCT, 0)
51 # define sc_print_bin(a) sc_print((a), 0, SC_BIN, 0)
53 #ifdef STRCALC_DEBUG_PRINTCOMP
54 # define DEBUGPRINTF_COMPUTATION(x) printf x
56 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
59 # define DEBUGPRINTF(x) printf x
61 # define DEBUGPRINTF(x) ((void)0)
68 static char *calc_buffer = NULL; /* buffer holding all results */
69 static char *output_buffer = NULL; /* buffer for output */
70 static int bit_pattern_size; /* maximum number of bits */
71 static int calc_buffer_size; /* size of internally stored values */
72 static int max_value_size; /* maximum size of values */
74 static int carry_flag; /**< some computation set the carry_flag:
75 - right shift if bits were lost due to shifting
76 - division if there was a remainder
77 However, the meaning of carry is machine dependent
78 and often defined in other ways! */
80 static const char sex_digit[4] = { SC_E, SC_C, SC_8, SC_0 };
81 static const char zex_digit[4] = { SC_1, SC_3, SC_7, SC_F };
82 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
83 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
85 static char const add_table[16][16][2] = {
86 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
87 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
88 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
89 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
91 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
92 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
93 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
94 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
96 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
97 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
98 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
99 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
101 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
102 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
103 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
104 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
106 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
107 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
108 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
109 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
111 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
112 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
113 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
114 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
116 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
117 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
118 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
119 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
121 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
122 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
123 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
124 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
126 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
127 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
128 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
129 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
131 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
132 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
133 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
134 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
136 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
137 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
138 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
139 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
141 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
142 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
143 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
144 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
146 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
147 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
148 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
149 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
151 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
152 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
153 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
154 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
156 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
157 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
158 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
159 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
161 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
162 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
163 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
164 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
167 static char const mul_table[16][16][2] = {
168 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
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} },
173 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
174 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
175 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
176 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
178 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
179 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
180 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
181 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
183 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
184 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
185 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
186 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
188 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
189 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
190 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
191 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
193 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
194 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
195 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
196 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
198 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
199 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
200 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
201 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
203 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
204 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
205 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
206 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
208 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
209 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
210 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
211 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
213 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
214 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
215 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
216 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
218 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
219 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
220 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
221 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
223 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
224 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
225 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
226 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
228 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
229 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
230 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
231 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
233 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
234 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
235 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
236 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
238 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
239 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
240 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
241 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
243 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
244 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
245 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
246 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
249 static char const shrs_table[16][4][2] = {
250 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
251 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
252 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
253 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
254 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
255 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
256 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
257 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
258 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
259 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
260 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
261 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
262 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
263 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
264 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
265 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
268 /** converting a digit to a binary string */
269 static char const *const binary_table[] = {
270 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
271 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
274 /*****************************************************************************
276 *****************************************************************************/
279 * implements the bitwise NOT operation
281 static void do_bitnot(const char *val, char *buffer)
285 for (counter = 0; counter<calc_buffer_size; counter++)
286 buffer[counter] = val[counter] ^ SC_F;
290 * implements the bitwise OR operation
292 static void do_bitor(const char *val1, const char *val2, char *buffer)
296 for (counter = 0; counter<calc_buffer_size; counter++)
297 buffer[counter] = val1[counter] | val2[counter];
301 * implements the bitwise eXclusive OR operation
303 static void do_bitxor(const char *val1, const char *val2, char *buffer)
307 for (counter = 0; counter<calc_buffer_size; counter++)
308 buffer[counter] = val1[counter] ^ val2[counter];
312 * implements the bitwise AND operation
314 static void do_bitand(const char *val1, const char *val2, char *buffer)
318 for (counter = 0; counter<calc_buffer_size; counter++)
319 buffer[counter] = val1[counter] & val2[counter];
323 * implements the bitwise AND not operation
325 static void do_bitandnot(const char *val1, const char *val2, char *buffer)
329 for (counter = 0; counter < calc_buffer_size; ++counter)
330 buffer[counter] = val1[counter] & (SC_F ^ val2[counter]);
334 * returns the sign bit.
336 * @todo This implementation is wrong, as it returns the highest bit of the buffer
337 * NOT the highest bit depending on the real mode
339 static int do_sign(const char *val)
341 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
345 * returns non-zero if bit at position pos is set
347 static int do_bit(const char *val, int pos)
350 int nibble = pos >> 2;
352 return _bitisset(val[nibble], bit);
356 * Implements a fast ADD + 1
358 static void do_inc(const char *val, char *buffer)
362 while (counter++ < calc_buffer_size) {
367 /* No carry here, *val != SC_F */
368 *buffer = add_table[_val(*val)][SC_1][0];
372 /* here a carry could be lost, this is intended because this should
373 * happen only when a value changes sign. */
377 * Implements a unary MINUS
379 static void do_negate(const char *val, char *buffer)
381 do_bitnot(val, buffer);
382 do_inc(buffer, buffer);
386 * Implements a binary ADD
388 * @todo The implementation of carry is wrong, as it is the
389 * calc_buffer_size carry, not the mode depending
391 static void do_add(const char *val1, const char *val2, char *buffer)
394 const char *add1, *add2;
397 for (counter = 0; counter < calc_buffer_size; counter++) {
398 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
399 add2 = add_table[_val(add1[0])][_val(carry)];
400 /* carry might be zero */
401 buffer[counter] = add2[0];
402 carry = add_table[_val(add1[1])][_val(add2[1])][0];
404 carry_flag = carry != SC_0;
408 * Implements a binary SUB
410 static void do_sub(const char *val1, const char *val2, char *buffer)
412 char *temp_buffer = (char*) alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
414 do_negate(val2, temp_buffer);
415 do_add(val1, temp_buffer, buffer);
419 * Implements a binary MUL
421 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 = (char*) alloca(calc_buffer_size);
433 neg_val1 = (char*) alloca(calc_buffer_size);
434 neg_val2 = (char*) 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)
502 for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
503 buffer[counter+1] = buffer[counter];
509 * Implements truncating integer division and remainder.
511 * Note: This is MOST slow
513 static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
515 const char *dividend = rDividend;
516 const char *minus_divisor;
520 char div_sign = 0; /* remember division result sign */
521 char rem_sign = 0; /* remember remainder result sign */
523 int c_dividend; /* loop counters */
525 neg_val1 = (char*) alloca(calc_buffer_size);
526 neg_val2 = (char*) alloca(calc_buffer_size);
528 /* clear result buffer */
529 memset(quot, SC_0, calc_buffer_size);
530 memset(rem, SC_0, calc_buffer_size);
532 /* if the divisor is zero this won't work (quot is zero) */
533 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
535 /* if the dividend is zero result is zero (quot is zero) */
536 if (sc_comp(dividend, quot) == 0)
539 if (do_sign(dividend) == -1) {
540 do_negate(dividend, neg_val1);
546 do_negate(divisor, neg_val2);
547 if (do_sign(divisor) == -1) {
549 minus_divisor = divisor;
552 minus_divisor = neg_val2;
554 /* if divisor >= dividend division is easy
555 * (remember these are absolute values) */
556 switch (sc_comp(dividend, divisor)) {
557 case 0: /* dividend == divisor */
561 case -1: /* dividend < divisor */
562 memcpy(rem, dividend, calc_buffer_size);
565 default: /* unluckily division is necessary :( */
569 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
570 do_push(dividend[c_dividend], rem);
573 if (sc_comp(rem, divisor) != -1) { /* remainder >= divisor */
574 /* subtract until the remainder becomes negative, this should
575 * be faster than comparing remainder with divisor */
576 do_add(rem, minus_divisor, rem);
578 while (do_sign(rem) == 1) {
579 quot[0] = add_table[_val(quot[0])][SC_1][0];
580 do_add(rem, minus_divisor, rem);
583 /* subtracted one too much */
584 do_add(rem, divisor, rem);
588 /* sets carry if remainder is non-zero ??? */
589 carry_flag = !sc_is_zero(rem);
592 do_negate(quot, quot);
599 * Implements a Shift Left, which can either preserve the sign bit
602 * @todo Assertions seems to be wrong
604 static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed)
613 assert((shift_cnt >= 0) || (0 && "negative leftshift"));
614 assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
615 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
616 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
618 /* if shifting far enough the result is zero */
619 if (shift_cnt >= bitsize) {
620 memset(buffer, SC_0, calc_buffer_size);
624 shift = SHIFT(shift_cnt % 4); /* this is 2 ** (offset % 4) */
625 shift_cnt = shift_cnt / 4;
627 /* shift the single digits some bytes (offset) and some bits (table)
629 for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
630 shl = mul_table[_val(val1[counter])][_val(shift)];
631 buffer[counter + shift_cnt] = shl[0] | carry;
635 shl = mul_table[_val(val1[counter])][_val(shift)];
636 buffer[counter + shift_cnt] = shl[0] | carry;
639 bitoffset = counter - 1;
642 /* fill with zeroes */
643 for (counter = 0; counter < shift_cnt; counter++)
644 buffer[counter] = SC_0;
646 /* if the mode was signed, change sign when the mode's msb is now 1 */
647 shift_cnt = bitoffset + shift_cnt;
648 bitoffset = (bitsize-1) % 4;
649 if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
650 /* this sets the upper bits of the leftmost digit */
651 buffer[shift_cnt] |= min_digit[bitoffset];
652 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
653 buffer[counter] = SC_F;
655 } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
656 /* this clears the upper bits of the leftmost digit */
657 buffer[shift_cnt] &= max_digit[bitoffset];
658 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
659 buffer[counter] = SC_0;
665 * Implements a Shift Right, which can either preserve the sign bit
668 * @param bitsize bitsize of the value to be shifted
670 * @todo Assertions seems to be wrong
672 static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift)
678 int shift_mod, shift_nib;
683 assert((shift_cnt >= 0) || (0 && "negative rightshift"));
684 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
685 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
687 sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
689 /* if shifting far enough the result is either 0 or -1 */
690 if (shift_cnt >= bitsize) {
691 if (!sc_is_zero(val1)) {
694 memset(buffer, sign, calc_buffer_size);
698 shift_mod = shift_cnt & 3;
699 shift_nib = shift_cnt >> 2;
701 /* check if any bits are lost, and set carry_flag if so */
702 for (counter = 0; counter < shift_nib; ++counter) {
703 if (val1[counter] != 0) {
708 if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
711 /* shift digits to the right with offset, carry and all */
712 buffer[0] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
713 for (counter = 1; counter < ((bitsize + 3) >> 2) - shift_nib; counter++) {
714 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
715 buffer[counter] = shrs[0];
716 buffer[counter - 1] |= shrs[1];
719 /* the last digit is special in regard of signed/unsigned shift */
720 bitoffset = bitsize & 3;
721 msd = sign; /* most significant digit */
723 /* remove sign bits if mode was signed and this is an unsigned shift */
724 if (!signed_shift && is_signed) {
725 msd &= max_digit[bitoffset];
728 shrs = shrs_table[_val(msd)][shift_mod];
730 /* signed shift and signed mode and negative value means all bits to the left are set */
731 if (signed_shift && sign == SC_F) {
732 buffer[counter] = shrs[0] | min_digit[bitoffset];
734 buffer[counter] = shrs[0];
738 buffer[counter - 1] |= shrs[1];
740 /* fill with SC_F or SC_0 depending on sign */
741 for (counter++; counter < calc_buffer_size; counter++) {
742 buffer[counter] = sign;
747 * Implements a Rotate Left.
748 * positive: low-order -> high order, negative other direction
750 static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
753 temp1 = (char*) alloca(calc_buffer_size);
754 temp2 = (char*) alloca(calc_buffer_size);
756 offset = offset % radius;
758 /* rotation by multiples of the type length is identity */
760 memmove(buffer, val1, calc_buffer_size);
764 do_shl(val1, temp1, offset, radius, is_signed);
765 do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
766 do_bitor(temp1, temp2, buffer);
767 carry_flag = 0; /* set by shr, but due to rot this is false */
770 /*****************************************************************************
771 * public functions, declared in strcalc.h
772 *****************************************************************************/
773 const void *sc_get_buffer(void)
775 return (void*)calc_buffer;
778 int sc_get_buffer_length(void)
780 return calc_buffer_size;
784 * Do sign extension if the mode is signed, otherwise to zero extension.
786 void sign_extend(void *buffer, ir_mode *mode)
788 char *calc_buffer = (char*) buffer;
789 int bits = get_mode_size_bits(mode) - 1;
790 int nibble = bits >> 2;
791 int max = max_digit[bits & 3];
794 if (mode_is_signed(mode)) {
795 if (calc_buffer[nibble] > max) {
796 /* sign bit is set, we need sign expansion */
798 for (i = nibble + 1; i < calc_buffer_size; ++i)
799 calc_buffer[i] = SC_F;
800 calc_buffer[nibble] |= sex_digit[bits & 3];
802 /* set all bits to zero */
803 for (i = nibble + 1; i < calc_buffer_size; ++i)
804 calc_buffer[i] = SC_0;
805 calc_buffer[nibble] &= zex_digit[bits & 3];
808 /* do zero extension */
809 for (i = nibble + 1; i < calc_buffer_size; ++i)
810 calc_buffer[i] = SC_0;
811 calc_buffer[nibble] &= zex_digit[bits & 3];
815 /* we assume that '0'-'9', 'a'-'z' and 'A'-'Z' are a range.
816 * The C-standard does theoretically allow otherwise. */
817 static inline void check_ascii(void)
819 /* C standard guarantees that '0'-'9' is a range */
832 int sc_val_from_str(char sign, unsigned base, const char *str,
833 size_t len, void *buffer)
837 assert(sign == -1 || sign == 1);
842 assert(base > 1 && base <= 16);
843 sc_base = (char*) alloca(calc_buffer_size);
844 sc_val_from_ulong(base, sc_base);
846 val = (char*) alloca(calc_buffer_size);
848 buffer = calc_buffer;
850 CLEAR_BUFFER(buffer);
853 /* BEGIN string evaluation, from left to right */
857 if (c >= '0' && c <= '9')
859 else if (c >= 'A' && c <= 'F')
861 else if (c >= 'a' && c <= 'f')
870 /* Radix conversion from base b to base B:
871 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
872 /* multiply current value with base */
873 do_mul(sc_base, (const char*) buffer, (char*) buffer);
874 /* add next digit to current value */
875 do_add(val, (const char*) buffer, (char*) buffer);
877 /* get ready for the next letter */
883 do_negate((const char*) buffer, (char*) buffer);
888 void sc_val_from_long(long value, void *buffer)
891 char sign, is_minlong;
893 if (buffer == NULL) buffer = calc_buffer;
894 pos = (char*) buffer;
897 is_minlong = value == LONG_MIN;
899 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
907 CLEAR_BUFFER(buffer);
909 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
910 *pos++ = _digit(value & 0xf);
916 do_inc((const char*) buffer, (char*) buffer);
918 do_negate((const char*) buffer, (char*) buffer);
922 void sc_val_from_ulong(unsigned long value, void *buffer)
926 if (buffer == NULL) buffer = calc_buffer;
927 pos = (unsigned char*) buffer;
929 while (pos < (unsigned char *)buffer + calc_buffer_size) {
930 *pos++ = (unsigned char)_digit(value & 0xf);
935 long sc_val_to_long(const void *val)
940 for (i = calc_buffer_size - 1; i >= 0; i--) {
941 l = (l << 4) + _val(((char *)val)[i]);
946 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
951 if (buffer == NULL) buffer = calc_buffer;
952 CLEAR_BUFFER(buffer);
954 if (!sign) return; /* unsigned means minimum is 0(zero) */
956 pos = (char*) buffer;
959 for (i = 0; i < bits/4; i++)
962 *pos++ = min_digit[bits%4];
964 for (i++; i <= calc_buffer_size - 1; i++)
968 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
973 if (buffer == NULL) buffer = calc_buffer;
974 CLEAR_BUFFER(buffer);
975 pos = (char*) buffer;
977 bits = num_bits - sign;
978 for (i = 0; i < bits/4; i++)
981 *pos++ = max_digit[bits%4];
983 for (i++; i <= calc_buffer_size - 1; i++)
987 void sc_truncate(unsigned int num_bits, void *buffer)
989 char *cbuffer = (char*) buffer;
990 char *pos = cbuffer + (num_bits / 4);
991 char *end = cbuffer + calc_buffer_size;
995 switch (num_bits % 4) {
996 case 0: /* nothing to do */ break;
997 case 1: *pos++ &= SC_1; break;
998 case 2: *pos++ &= SC_3; break;
999 case 3: *pos++ &= SC_7; break;
1002 for ( ; pos < end; ++pos)
1006 int sc_comp(const void* value1, const void* value2)
1008 int counter = calc_buffer_size - 1;
1009 const char *val1 = (const char *)value1;
1010 const char *val2 = (const char *)value2;
1012 /* compare signs first:
1013 * the loop below can only compare values of the same sign! */
1014 if (do_sign(val1) != do_sign(val2))
1015 return (do_sign(val1) == 1)?(1):(-1);
1017 /* loop until two digits differ, the values are equal if there
1018 * are no such two digits */
1019 while (val1[counter] == val2[counter]) {
1021 if (counter < 0) return 0;
1024 /* the leftmost digit is the most significant, so this returns
1025 * the correct result.
1026 * This implies the digit enum is ordered */
1027 return (val1[counter] > val2[counter]) ? (1) : (-1);
1030 int sc_get_highest_set_bit(const void *value)
1032 const char *val = (const char*)value;
1035 high = calc_buffer_size * 4 - 1;
1037 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1038 if (val[counter] == SC_0)
1041 if (val[counter] > SC_7) return high;
1042 else if (val[counter] > SC_3) return high - 1;
1043 else if (val[counter] > SC_1) return high - 2;
1044 else return high - 3;
1050 int sc_get_lowest_set_bit(const void *value)
1052 const char *val = (const char*)value;
1056 for (counter = 0; counter < calc_buffer_size; counter++) {
1057 switch (val[counter]) {
1084 int sc_get_bit_at(const void *value, unsigned pos)
1086 const char *val = (const char*) value;
1087 unsigned nibble = pos >> 2;
1089 return (val[nibble] & SHIFT(pos & 3)) != SC_0;
1092 void sc_set_bit_at(void *value, unsigned pos)
1094 char *val = (char*) value;
1095 unsigned nibble = pos >> 2;
1097 val[nibble] |= SHIFT(pos & 3);
1100 int sc_is_zero(const void *value)
1102 const char* val = (const char *)value;
1105 for (counter = 0; counter < calc_buffer_size; ++counter) {
1106 if (val[counter] != SC_0)
1112 int sc_is_negative(const void *value)
1114 return do_sign((const char*) value) == -1;
1117 int sc_had_carry(void)
1122 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1124 const char *val = (const char *)value;
1125 int nibble_ofs = 2 * byte_ofs;
1128 /* the current scheme uses one byte to store a nibble */
1129 if (4 * nibble_ofs >= len)
1132 res = _val(val[nibble_ofs]);
1133 if (len > 4 * (nibble_ofs + 1))
1134 res |= _val(val[nibble_ofs + 1]) << 4;
1136 /* kick bits outsize */
1137 if (len - 8 * byte_ofs < 8) {
1138 res &= (1 << (len - 8 * byte_ofs)) - 1;
1144 * convert to a string
1145 * FIXME: Doesn't check buffer bounds
1147 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode)
1149 static const char big_digits[] = "0123456789ABCDEF";
1150 static const char small_digits[] = "0123456789abcdef";
1152 char *base_val, *div1_res, *div2_res, *rem_res;
1153 int counter, nibbles, i, sign, mask;
1156 const char *val = (const char *)value;
1160 const char *digits = small_digits;
1162 base_val = (char*) alloca(calc_buffer_size);
1163 div1_res = (char*) alloca(calc_buffer_size);
1164 div2_res = (char*) alloca(calc_buffer_size);
1165 rem_res = (char*) alloca(calc_buffer_size);
1167 pos = output_buffer + bit_pattern_size;
1172 bits = bit_pattern_size;
1173 #ifdef STRCALC_DEBUG_FULLPRINT
1177 nibbles = bits >> 2;
1181 digits = big_digits;
1183 for (counter = 0; counter < nibbles; ++counter) {
1184 *(--pos) = digits[_val(val[counter])];
1185 #ifdef STRCALC_DEBUG_GROUPPRINT
1186 if ((counter+1)%8 == 0)
1191 /* last nibble must be masked */
1193 mask = zex_digit[(bits & 3) - 1];
1194 x = val[counter++] & mask;
1195 *(--pos) = digits[_val(x)];
1198 /* now kill zeros */
1199 for (; counter > 1; --counter, ++pos) {
1200 #ifdef STRCALC_DEBUG_GROUPPRINT
1201 if (pos[0] == ' ') ++pos;
1209 for (counter = 0; counter < nibbles; ++counter) {
1211 p = binary_table[_val(val[counter])];
1218 /* last nibble must be masked */
1220 mask = zex_digit[(bits & 3) - 1];
1221 x = val[counter++] & mask;
1224 p = binary_table[_val(x)];
1231 /* now kill zeros */
1232 for (counter <<= 2; counter > 1; --counter, ++pos)
1239 memset(base_val, SC_0, calc_buffer_size);
1240 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1244 if (signed_mode && base == SC_DEC) {
1245 /* check for negative values */
1246 if (do_bit(val, bits - 1)) {
1247 do_negate(val, div2_res);
1253 /* transfer data into oscillating buffers */
1254 memset(div1_res, SC_0, calc_buffer_size);
1255 for (counter = 0; counter < nibbles; ++counter)
1256 div1_res[counter] = p[counter];
1258 /* last nibble must be masked */
1260 mask = zex_digit[(bits & 3) - 1];
1261 div1_res[counter] = p[counter] & mask;
1268 do_divmod(m, base_val, n, rem_res);
1272 *(--pos) = digits[_val(rem_res[0])];
1275 for (i = 0; i < calc_buffer_size; ++i)
1286 panic("Unsupported base %d", base);
1291 void init_strcalc(int precision)
1293 if (calc_buffer == NULL) {
1294 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1296 /* round up to multiple of 4 */
1297 precision = (precision + 3) & ~3;
1299 bit_pattern_size = (precision);
1300 calc_buffer_size = (precision / 2);
1301 max_value_size = (precision / 4);
1303 calc_buffer = XMALLOCN(char, calc_buffer_size + 1);
1304 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1306 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));
1311 void finish_strcalc(void)
1313 free(calc_buffer); calc_buffer = NULL;
1314 free(output_buffer); output_buffer = NULL;
1317 int sc_get_precision(void)
1319 return bit_pattern_size;
1323 void sc_add(const void *value1, const void *value2, void *buffer)
1325 CLEAR_BUFFER(calc_buffer);
1328 DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1329 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1331 do_add((const char*) value1, (const char*) value2, (char*) calc_buffer);
1333 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1335 if ((buffer != NULL) && (buffer != calc_buffer)) {
1336 memcpy(buffer, calc_buffer, calc_buffer_size);
1340 void sc_sub(const void *value1, const void *value2, void *buffer)
1342 CLEAR_BUFFER(calc_buffer);
1345 DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1346 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1348 do_sub((const char*) value1, (const char*) value2, calc_buffer);
1350 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1352 if ((buffer != NULL) && (buffer != calc_buffer)) {
1353 memcpy(buffer, calc_buffer, calc_buffer_size);
1357 void sc_neg(const void *value1, void *buffer)
1361 DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1363 do_negate((const char*) value1, calc_buffer);
1365 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1367 if ((buffer != NULL) && (buffer != calc_buffer)) {
1368 memcpy(buffer, calc_buffer, calc_buffer_size);
1372 void sc_and(const void *value1, const void *value2, void *buffer)
1374 CLEAR_BUFFER(calc_buffer);
1377 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1378 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1380 do_bitand((const char*) value1, (const char*) value2, calc_buffer);
1382 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1384 if ((buffer != NULL) && (buffer != calc_buffer)) {
1385 memcpy(buffer, calc_buffer, calc_buffer_size);
1389 void sc_andnot(const void *value1, const void *value2, void *buffer)
1391 CLEAR_BUFFER(calc_buffer);
1394 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1395 DEBUGPRINTF_COMPUTATION(("~%s -> ", sc_print_hex(value2)));
1397 do_bitandnot((const char*) value1, (const char*) value2, calc_buffer);
1399 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1401 if (buffer != NULL && buffer != calc_buffer) {
1402 memcpy(buffer, calc_buffer, calc_buffer_size);
1406 void sc_or(const void *value1, const void *value2, void *buffer)
1408 CLEAR_BUFFER(calc_buffer);
1411 DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1412 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1414 do_bitor((const char*) value1, (const char*) value2, calc_buffer);
1416 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1418 if ((buffer != NULL) && (buffer != calc_buffer)) {
1419 memcpy(buffer, calc_buffer, calc_buffer_size);
1423 void sc_xor(const void *value1, const void *value2, void *buffer)
1425 CLEAR_BUFFER(calc_buffer);
1428 DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1429 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1431 do_bitxor((const char*) value1, (const char*) value2, calc_buffer);
1433 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1435 if ((buffer != NULL) && (buffer != calc_buffer)) {
1436 memcpy(buffer, calc_buffer, calc_buffer_size);
1440 void sc_not(const void *value1, void *buffer)
1442 CLEAR_BUFFER(calc_buffer);
1445 DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1447 do_bitnot((const char*) value1, calc_buffer);
1449 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1451 if ((buffer != NULL) && (buffer != calc_buffer)) {
1452 memcpy(buffer, calc_buffer, calc_buffer_size);
1456 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((const char*) value1, (const char*) 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)
1475 /* temp buffer holding unused result of divmod */
1476 char *unused_res = (char*) alloca(calc_buffer_size);
1478 CLEAR_BUFFER(calc_buffer);
1481 DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1482 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1484 do_divmod((const char*) value1, (const char*) value2, calc_buffer, unused_res);
1486 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1488 if ((buffer != NULL) && (buffer != calc_buffer)) {
1489 memcpy(buffer, calc_buffer, calc_buffer_size);
1493 void sc_mod(const void *value1, const void *value2, void *buffer)
1495 /* temp buffer holding unused result of divmod */
1496 char *unused_res = (char*) alloca(calc_buffer_size);
1498 CLEAR_BUFFER(calc_buffer);
1501 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1502 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1504 do_divmod((const char*) value1, (const char*) value2, unused_res, calc_buffer);
1506 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1508 if ((buffer != NULL) && (buffer != calc_buffer)) {
1509 memcpy(buffer, calc_buffer, calc_buffer_size);
1513 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer)
1515 CLEAR_BUFFER(calc_buffer);
1518 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1519 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1521 do_divmod((const char*) value1, (const char*) value2, (char*) div_buffer, (char*) mod_buffer);
1523 DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1527 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1531 DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1532 do_shl((const char*) val1, calc_buffer, shift_cnt, bitsize, sign);
1534 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1536 if ((buffer != NULL) && (buffer != calc_buffer)) {
1537 memmove(buffer, calc_buffer, calc_buffer_size);
1541 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1543 long offset = sc_val_to_long(val2);
1545 sc_shlI(val1, offset, bitsize, sign, buffer);
1548 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1552 DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1553 do_shr((const char*) val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1555 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1557 if ((buffer != NULL) && (buffer != calc_buffer)) {
1558 memmove(buffer, calc_buffer, calc_buffer_size);
1562 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1564 long shift_cnt = sc_val_to_long(val2);
1566 sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1569 void sc_shrsI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1573 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), shift_cnt));
1574 do_shr((const char*) val1, calc_buffer, shift_cnt, bitsize, sign, 1);
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_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1585 long offset = sc_val_to_long(val2);
1589 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1590 do_shr((const char*) val1, calc_buffer, offset, bitsize, sign, 1);
1592 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1594 if ((buffer != NULL) && (buffer != calc_buffer)) {
1595 memmove(buffer, calc_buffer, calc_buffer_size);
1599 void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1601 long offset = sc_val_to_long(val2);
1605 DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1606 do_rotl((const char*) val1, calc_buffer, offset, bitsize, sign);
1608 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1610 if ((buffer != NULL) && (buffer != calc_buffer)) {
1611 memmove(buffer, calc_buffer, calc_buffer_size);
1615 void sc_zero(void *buffer)
1618 buffer = calc_buffer;
1619 CLEAR_BUFFER(buffer);