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
42 #define SC_RESULT(x) ((x) & ((1U << SC_BITS) - 1U))
43 #define SC_CARRY(x) ((unsigned)(x) >> SC_BITS)
45 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, calc_buffer_size)
46 #define SHIFT(count) (SC_1 << (count))
47 #define _val(a) ((a)-SC_0)
48 #define _digit(a) ((a)+SC_0)
49 #define _bitisset(digit, pos) (((digit) & SHIFT(pos)) != SC_0)
51 /* shortcut output for debugging */
52 # define sc_print_hex(a) sc_print((a), 0, SC_HEX, 0)
53 # define sc_print_dec(a) sc_print((a), 0, SC_DEC, 1)
54 # define sc_print_oct(a) sc_print((a), 0, SC_OCT, 0)
55 # define sc_print_bin(a) sc_print((a), 0, SC_BIN, 0)
57 #ifdef STRCALC_DEBUG_PRINTCOMP
58 # define DEBUGPRINTF_COMPUTATION(x) printf x
60 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
63 # define DEBUGPRINTF(x) printf x
65 # define DEBUGPRINTF(x) ((void)0)
72 static char *calc_buffer = NULL; /* buffer holding all results */
73 static char *output_buffer = NULL; /* buffer for output */
74 static int bit_pattern_size; /* maximum number of bits */
75 static int calc_buffer_size; /* size of internally stored values */
76 static int max_value_size; /* maximum size of values */
78 static int carry_flag; /**< some computation set the carry_flag:
79 - right shift if bits were lost due to shifting
80 - division if there was a remainder
81 However, the meaning of carry is machine dependent
82 and often defined in other ways! */
84 static const char sex_digit[4] = { SC_E, SC_C, SC_8, SC_0 };
85 static const char zex_digit[4] = { SC_1, SC_3, SC_7, SC_F };
86 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
87 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
89 static char const mul_table[16][16][2] = {
90 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
91 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
92 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
93 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
95 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
96 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
97 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
98 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
100 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
101 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
102 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
103 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
105 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
106 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
107 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
108 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
110 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
111 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
112 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
113 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
115 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
116 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
117 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
118 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
120 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
121 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
122 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
123 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
125 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
126 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
127 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
128 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
130 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
131 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
132 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
133 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
135 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
136 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
137 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
138 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
140 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
141 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
142 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
143 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
145 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
146 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
147 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
148 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
150 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
151 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
152 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
153 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
155 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
156 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
157 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
158 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
160 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
161 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
162 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
163 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
165 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
166 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
167 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
168 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
171 static char const shrs_table[16][4][2] = {
172 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
173 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
174 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
175 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
176 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
177 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
178 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
179 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
180 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
181 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
182 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
183 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
184 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
185 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
186 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
187 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
190 /** converting a digit to a binary string */
191 static char const *const binary_table[] = {
192 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
193 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
196 /*****************************************************************************
198 *****************************************************************************/
201 * implements the bitwise NOT operation
203 static void do_bitnot(const char *val, char *buffer)
207 for (counter = 0; counter<calc_buffer_size; counter++)
208 buffer[counter] = val[counter] ^ SC_F;
212 * implements the bitwise OR operation
214 static void do_bitor(const char *val1, const char *val2, char *buffer)
218 for (counter = 0; counter<calc_buffer_size; counter++)
219 buffer[counter] = val1[counter] | val2[counter];
223 * implements the bitwise eXclusive OR operation
225 static void do_bitxor(const char *val1, const char *val2, char *buffer)
229 for (counter = 0; counter<calc_buffer_size; counter++)
230 buffer[counter] = val1[counter] ^ val2[counter];
234 * implements the bitwise AND operation
236 static void do_bitand(const char *val1, const char *val2, char *buffer)
240 for (counter = 0; counter<calc_buffer_size; counter++)
241 buffer[counter] = val1[counter] & val2[counter];
245 * implements the bitwise AND not operation
247 static void do_bitandnot(const char *val1, const char *val2, char *buffer)
251 for (counter = 0; counter < calc_buffer_size; ++counter)
252 buffer[counter] = val1[counter] & (SC_F ^ val2[counter]);
256 * returns the sign bit.
258 * @todo This implementation is wrong, as it returns the highest bit of the buffer
259 * NOT the highest bit depending on the real mode
261 static int do_sign(const char *val)
263 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
267 * returns non-zero if bit at position pos is set
269 static int do_bit(const char *val, int pos)
272 int nibble = pos >> 2;
274 return _bitisset(val[nibble], bit);
278 * Implements a fast ADD + 1
280 static void do_inc(const char *val, char *buffer)
284 while (counter++ < calc_buffer_size) {
289 /* No carry here, *val != SC_F */
290 *buffer = *val + SC_1;
294 /* here a carry could be lost, this is intended because this should
295 * happen only when a value changes sign. */
299 * Implements a unary MINUS
301 static void do_negate(const char *val, char *buffer)
303 do_bitnot(val, buffer);
304 do_inc(buffer, buffer);
308 * Implements a binary ADD
310 * @todo The implementation of carry is wrong, as it is the
311 * calc_buffer_size carry, not the mode depending
313 static void do_add(const char *val1, const char *val2, char *buffer)
315 unsigned carry = SC_0;
316 for (int counter = 0; counter < calc_buffer_size; ++counter) {
317 unsigned const sum = val1[counter] + val2[counter] + carry;
318 buffer[counter] = SC_RESULT(sum);
319 carry = SC_CARRY(sum);
321 carry_flag = carry != SC_0;
325 * Implements a binary SUB
327 static void do_sub(const char *val1, const char *val2, char *buffer)
329 char *temp_buffer = (char*) alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
331 do_negate(val2, temp_buffer);
332 do_add(val1, temp_buffer, buffer);
336 * Implements a binary MUL
338 static void do_mul(const char *val1, const char *val2, char *buffer)
340 char *temp_buffer; /* result buffer */
341 char *neg_val1; /* abs of val1 */
342 char *neg_val2; /* abs of val2 */
344 char sign = 0; /* marks result sign */
345 int c_inner, c_outer; /* loop counters */
347 temp_buffer = (char*) alloca(calc_buffer_size);
348 neg_val1 = (char*) alloca(calc_buffer_size);
349 neg_val2 = (char*) alloca(calc_buffer_size);
351 /* init result buffer to zeros */
352 memset(temp_buffer, SC_0, calc_buffer_size);
354 /* the multiplication works only for positive values, for negative values *
355 * it is necessary to negate them and adjust the result accordingly */
356 if (do_sign(val1) == -1) {
357 do_negate(val1, neg_val1);
361 if (do_sign(val2) == -1) {
362 do_negate(val2, neg_val2);
367 for (c_outer = 0; c_outer < max_value_size; c_outer++) {
368 if (val2[c_outer] != SC_0) {
369 unsigned carry = SC_0; /* container for carries */
370 for (c_inner = 0; c_inner < max_value_size; c_inner++) {
371 /* do the following calculation: *
372 * Add the current carry, the value at position c_outer+c_inner *
373 * and the result of the multiplication of val1[c_inner] and *
374 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
376 /* multiplicate the two digits */
377 char const *const mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
378 /* add old value to result of multiplication and the carry */
379 unsigned const sum = temp_buffer[c_inner + c_outer] + (mul[1] << SC_BITS) + mul[0] + carry;
381 /* all carries together result in new carry. This is always smaller *
383 * Both multiplicands, the carry and the value already in the temp *
384 * buffer are single digits and their value is therefore at most *
387 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
388 * The tables list all operations rem b, so the carry is at most *
389 * (b*b-1)rem b = -1rem b = b-1 */
390 temp_buffer[c_inner + c_outer] = SC_RESULT(sum);
391 carry = SC_CARRY(sum);
394 /* A carry may hang over */
395 /* c_outer is always smaller than max_value_size! */
396 temp_buffer[max_value_size + c_outer] = carry;
401 do_negate(temp_buffer, buffer);
403 memcpy(buffer, temp_buffer, calc_buffer_size);
407 * Shift the buffer to left and add a 4 bit digit
409 static void do_push(const char digit, char *buffer)
413 for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
414 buffer[counter+1] = buffer[counter];
420 * Implements truncating integer division and remainder.
422 * Note: This is MOST slow
424 static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
426 const char *dividend = rDividend;
427 const char *minus_divisor;
431 char div_sign = 0; /* remember division result sign */
432 char rem_sign = 0; /* remember remainder result sign */
434 int c_dividend; /* loop counters */
436 neg_val1 = (char*) alloca(calc_buffer_size);
437 neg_val2 = (char*) alloca(calc_buffer_size);
439 /* clear result buffer */
440 memset(quot, SC_0, calc_buffer_size);
441 memset(rem, SC_0, calc_buffer_size);
443 /* if the divisor is zero this won't work (quot is zero) */
444 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
446 /* if the dividend is zero result is zero (quot is zero) */
447 if (sc_comp(dividend, quot) == 0)
450 if (do_sign(dividend) == -1) {
451 do_negate(dividend, neg_val1);
457 do_negate(divisor, neg_val2);
458 if (do_sign(divisor) == -1) {
460 minus_divisor = divisor;
463 minus_divisor = neg_val2;
465 /* if divisor >= dividend division is easy
466 * (remember these are absolute values) */
467 switch (sc_comp(dividend, divisor)) {
468 case 0: /* dividend == divisor */
472 case -1: /* dividend < divisor */
473 memcpy(rem, dividend, calc_buffer_size);
476 default: /* unluckily division is necessary :( */
480 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
481 do_push(dividend[c_dividend], rem);
484 if (sc_comp(rem, divisor) != -1) { /* remainder >= divisor */
485 /* subtract until the remainder becomes negative, this should
486 * be faster than comparing remainder with divisor */
487 do_add(rem, minus_divisor, rem);
489 while (do_sign(rem) == 1) {
490 quot[0] = SC_RESULT(quot[0] + SC_1); /* TODO can this generate carry or is masking redundant? */
491 do_add(rem, minus_divisor, rem);
494 /* subtracted one too much */
495 do_add(rem, divisor, rem);
499 /* sets carry if remainder is non-zero ??? */
500 carry_flag = !sc_is_zero(rem);
503 do_negate(quot, quot);
510 * Implements a Shift Left, which can either preserve the sign bit
513 * @todo Assertions seems to be wrong
515 static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed)
524 assert((shift_cnt >= 0) || (0 && "negative leftshift"));
525 assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
526 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
527 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
529 /* if shifting far enough the result is zero */
530 if (shift_cnt >= bitsize) {
531 memset(buffer, SC_0, calc_buffer_size);
535 shift = SHIFT(shift_cnt % 4); /* this is 2 ** (offset % 4) */
536 shift_cnt = shift_cnt / 4;
538 /* shift the single digits some bytes (offset) and some bits (table)
540 for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
541 shl = mul_table[_val(val1[counter])][_val(shift)];
542 buffer[counter + shift_cnt] = shl[0] | carry;
546 shl = mul_table[_val(val1[counter])][_val(shift)];
547 buffer[counter + shift_cnt] = shl[0] | carry;
550 bitoffset = counter - 1;
553 /* fill with zeroes */
554 for (counter = 0; counter < shift_cnt; counter++)
555 buffer[counter] = SC_0;
557 /* if the mode was signed, change sign when the mode's msb is now 1 */
558 shift_cnt = bitoffset + shift_cnt;
559 bitoffset = (bitsize-1) % 4;
560 if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
561 /* this sets the upper bits of the leftmost digit */
562 buffer[shift_cnt] |= min_digit[bitoffset];
563 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
564 buffer[counter] = SC_F;
566 } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
567 /* this clears the upper bits of the leftmost digit */
568 buffer[shift_cnt] &= max_digit[bitoffset];
569 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
570 buffer[counter] = SC_0;
576 * Implements a Shift Right, which can either preserve the sign bit
579 * @param bitsize bitsize of the value to be shifted
581 * @todo Assertions seems to be wrong
583 static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift)
589 int shift_mod, shift_nib;
594 assert((shift_cnt >= 0) || (0 && "negative rightshift"));
595 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
596 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
598 sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
600 /* if shifting far enough the result is either 0 or -1 */
601 if (shift_cnt >= bitsize) {
602 if (!sc_is_zero(val1)) {
605 memset(buffer, sign, calc_buffer_size);
609 shift_mod = shift_cnt & 3;
610 shift_nib = shift_cnt >> 2;
612 /* check if any bits are lost, and set carry_flag if so */
613 for (counter = 0; counter < shift_nib; ++counter) {
614 if (val1[counter] != 0) {
619 if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
622 /* shift digits to the right with offset, carry and all */
623 buffer[0] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
624 for (counter = 1; counter < ((bitsize + 3) >> 2) - shift_nib; counter++) {
625 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
626 buffer[counter] = shrs[0];
627 buffer[counter - 1] |= shrs[1];
630 /* the last digit is special in regard of signed/unsigned shift */
631 bitoffset = bitsize & 3;
632 msd = sign; /* most significant digit */
634 /* remove sign bits if mode was signed and this is an unsigned shift */
635 if (!signed_shift && is_signed) {
636 msd &= max_digit[bitoffset];
639 shrs = shrs_table[_val(msd)][shift_mod];
641 /* signed shift and signed mode and negative value means all bits to the left are set */
642 if (signed_shift && sign == SC_F) {
643 buffer[counter] = shrs[0] | min_digit[bitoffset];
645 buffer[counter] = shrs[0];
649 buffer[counter - 1] |= shrs[1];
651 /* fill with SC_F or SC_0 depending on sign */
652 for (counter++; counter < calc_buffer_size; counter++) {
653 buffer[counter] = sign;
658 * Implements a Rotate Left.
659 * positive: low-order -> high order, negative other direction
661 static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
664 temp1 = (char*) alloca(calc_buffer_size);
665 temp2 = (char*) alloca(calc_buffer_size);
667 offset = offset % radius;
669 /* rotation by multiples of the type length is identity */
671 memmove(buffer, val1, calc_buffer_size);
675 do_shl(val1, temp1, offset, radius, is_signed);
676 do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
677 do_bitor(temp1, temp2, buffer);
678 carry_flag = 0; /* set by shr, but due to rot this is false */
681 /*****************************************************************************
682 * public functions, declared in strcalc.h
683 *****************************************************************************/
684 const void *sc_get_buffer(void)
686 return (void*)calc_buffer;
689 int sc_get_buffer_length(void)
691 return calc_buffer_size;
695 * Do sign extension if the mode is signed, otherwise to zero extension.
697 void sign_extend(void *buffer, ir_mode *mode)
699 char *calc_buffer = (char*) buffer;
700 int bits = get_mode_size_bits(mode) - 1;
701 int nibble = bits >> 2;
702 int max = max_digit[bits & 3];
705 if (mode_is_signed(mode)) {
706 if (calc_buffer[nibble] > max) {
707 /* sign bit is set, we need sign expansion */
709 for (i = nibble + 1; i < calc_buffer_size; ++i)
710 calc_buffer[i] = SC_F;
711 calc_buffer[nibble] |= sex_digit[bits & 3];
713 /* set all bits to zero */
714 for (i = nibble + 1; i < calc_buffer_size; ++i)
715 calc_buffer[i] = SC_0;
716 calc_buffer[nibble] &= zex_digit[bits & 3];
719 /* do zero extension */
720 for (i = nibble + 1; i < calc_buffer_size; ++i)
721 calc_buffer[i] = SC_0;
722 calc_buffer[nibble] &= zex_digit[bits & 3];
726 /* we assume that '0'-'9', 'a'-'z' and 'A'-'Z' are a range.
727 * The C-standard does theoretically allow otherwise. */
728 static inline void check_ascii(void)
730 /* C standard guarantees that '0'-'9' is a range */
743 int sc_val_from_str(char sign, unsigned base, const char *str,
744 size_t len, void *buffer)
748 assert(sign == -1 || sign == 1);
753 assert(base > 1 && base <= 16);
754 sc_base = (char*) alloca(calc_buffer_size);
755 sc_val_from_ulong(base, sc_base);
757 val = (char*) alloca(calc_buffer_size);
759 buffer = calc_buffer;
761 CLEAR_BUFFER(buffer);
764 /* BEGIN string evaluation, from left to right */
768 if (c >= '0' && c <= '9')
770 else if (c >= 'A' && c <= 'F')
772 else if (c >= 'a' && c <= 'f')
781 /* Radix conversion from base b to base B:
782 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
783 /* multiply current value with base */
784 do_mul(sc_base, (const char*) buffer, (char*) buffer);
785 /* add next digit to current value */
786 do_add(val, (const char*) buffer, (char*) buffer);
788 /* get ready for the next letter */
794 do_negate((const char*) buffer, (char*) buffer);
799 void sc_val_from_long(long value, void *buffer)
802 char sign, is_minlong;
804 if (buffer == NULL) buffer = calc_buffer;
805 pos = (char*) buffer;
808 is_minlong = value == LONG_MIN;
810 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
818 CLEAR_BUFFER(buffer);
820 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
821 *pos++ = _digit(value & 0xf);
827 do_inc((const char*) buffer, (char*) buffer);
829 do_negate((const char*) buffer, (char*) buffer);
833 void sc_val_from_ulong(unsigned long value, void *buffer)
837 if (buffer == NULL) buffer = calc_buffer;
838 pos = (unsigned char*) buffer;
840 while (pos < (unsigned char *)buffer + calc_buffer_size) {
841 *pos++ = (unsigned char)_digit(value & 0xf);
846 long sc_val_to_long(const void *val)
851 for (i = calc_buffer_size - 1; i >= 0; i--) {
852 l = (l << 4) + _val(((char *)val)[i]);
857 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
862 if (buffer == NULL) buffer = calc_buffer;
863 CLEAR_BUFFER(buffer);
865 if (!sign) return; /* unsigned means minimum is 0(zero) */
867 pos = (char*) buffer;
870 for (i = 0; i < bits/4; i++)
873 *pos++ = min_digit[bits%4];
875 for (i++; i <= calc_buffer_size - 1; i++)
879 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
884 if (buffer == NULL) buffer = calc_buffer;
885 CLEAR_BUFFER(buffer);
886 pos = (char*) buffer;
888 bits = num_bits - sign;
889 for (i = 0; i < bits/4; i++)
892 *pos++ = max_digit[bits%4];
894 for (i++; i <= calc_buffer_size - 1; i++)
898 void sc_truncate(unsigned int num_bits, void *buffer)
900 char *cbuffer = (char*) buffer;
901 char *pos = cbuffer + (num_bits / 4);
902 char *end = cbuffer + calc_buffer_size;
906 switch (num_bits % 4) {
907 case 0: /* nothing to do */ break;
908 case 1: *pos++ &= SC_1; break;
909 case 2: *pos++ &= SC_3; break;
910 case 3: *pos++ &= SC_7; break;
913 for ( ; pos < end; ++pos)
917 int sc_comp(const void* value1, const void* value2)
919 int counter = calc_buffer_size - 1;
920 const char *val1 = (const char *)value1;
921 const char *val2 = (const char *)value2;
923 /* compare signs first:
924 * the loop below can only compare values of the same sign! */
925 if (do_sign(val1) != do_sign(val2))
926 return (do_sign(val1) == 1)?(1):(-1);
928 /* loop until two digits differ, the values are equal if there
929 * are no such two digits */
930 while (val1[counter] == val2[counter]) {
932 if (counter < 0) return 0;
935 /* the leftmost digit is the most significant, so this returns
936 * the correct result.
937 * This implies the digit enum is ordered */
938 return (val1[counter] > val2[counter]) ? (1) : (-1);
941 int sc_get_highest_set_bit(const void *value)
943 const char *val = (const char*)value;
946 high = calc_buffer_size * 4 - 1;
948 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
949 if (val[counter] == SC_0)
952 if (val[counter] > SC_7) return high;
953 else if (val[counter] > SC_3) return high - 1;
954 else if (val[counter] > SC_1) return high - 2;
955 else return high - 3;
961 int sc_get_lowest_set_bit(const void *value)
963 const char *val = (const char*)value;
967 for (counter = 0; counter < calc_buffer_size; counter++) {
968 switch (val[counter]) {
995 int sc_get_bit_at(const void *value, unsigned pos)
997 const char *val = (const char*) value;
998 unsigned nibble = pos >> 2;
1000 return (val[nibble] & SHIFT(pos & 3)) != SC_0;
1003 void sc_set_bit_at(void *value, unsigned pos)
1005 char *val = (char*) value;
1006 unsigned nibble = pos >> 2;
1008 val[nibble] |= SHIFT(pos & 3);
1011 int sc_is_zero(const void *value)
1013 const char* val = (const char *)value;
1016 for (counter = 0; counter < calc_buffer_size; ++counter) {
1017 if (val[counter] != SC_0)
1023 int sc_is_negative(const void *value)
1025 return do_sign((const char*) value) == -1;
1028 int sc_had_carry(void)
1033 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1035 const char *val = (const char *)value;
1036 int nibble_ofs = 2 * byte_ofs;
1039 /* the current scheme uses one byte to store a nibble */
1040 if (4 * nibble_ofs >= len)
1043 res = _val(val[nibble_ofs]);
1044 if (len > 4 * (nibble_ofs + 1))
1045 res |= _val(val[nibble_ofs + 1]) << 4;
1047 /* kick bits outsize */
1048 if (len - 8 * byte_ofs < 8) {
1049 res &= (1 << (len - 8 * byte_ofs)) - 1;
1055 * convert to a string
1056 * FIXME: Doesn't check buffer bounds
1058 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode)
1060 static const char big_digits[] = "0123456789ABCDEF";
1061 static const char small_digits[] = "0123456789abcdef";
1063 char *base_val, *div1_res, *div2_res, *rem_res;
1064 int counter, nibbles, i, sign, mask;
1067 const char *val = (const char *)value;
1071 const char *digits = small_digits;
1073 base_val = (char*) alloca(calc_buffer_size);
1074 div1_res = (char*) alloca(calc_buffer_size);
1075 div2_res = (char*) alloca(calc_buffer_size);
1076 rem_res = (char*) alloca(calc_buffer_size);
1078 pos = output_buffer + bit_pattern_size;
1083 bits = bit_pattern_size;
1084 #ifdef STRCALC_DEBUG_FULLPRINT
1088 nibbles = bits >> 2;
1092 digits = big_digits;
1094 for (counter = 0; counter < nibbles; ++counter) {
1095 *(--pos) = digits[_val(val[counter])];
1096 #ifdef STRCALC_DEBUG_GROUPPRINT
1097 if ((counter+1)%8 == 0)
1102 /* last nibble must be masked */
1104 mask = zex_digit[(bits & 3) - 1];
1105 x = val[counter++] & mask;
1106 *(--pos) = digits[_val(x)];
1109 /* now kill zeros */
1110 for (; counter > 1; --counter, ++pos) {
1111 #ifdef STRCALC_DEBUG_GROUPPRINT
1112 if (pos[0] == ' ') ++pos;
1120 for (counter = 0; counter < nibbles; ++counter) {
1122 p = binary_table[_val(val[counter])];
1129 /* last nibble must be masked */
1131 mask = zex_digit[(bits & 3) - 1];
1132 x = val[counter++] & mask;
1135 p = binary_table[_val(x)];
1142 /* now kill zeros */
1143 for (counter <<= 2; counter > 1; --counter, ++pos)
1150 memset(base_val, SC_0, calc_buffer_size);
1151 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1155 if (signed_mode && base == SC_DEC) {
1156 /* check for negative values */
1157 if (do_bit(val, bits - 1)) {
1158 do_negate(val, div2_res);
1164 /* transfer data into oscillating buffers */
1165 memset(div1_res, SC_0, calc_buffer_size);
1166 for (counter = 0; counter < nibbles; ++counter)
1167 div1_res[counter] = p[counter];
1169 /* last nibble must be masked */
1171 mask = zex_digit[(bits & 3) - 1];
1172 div1_res[counter] = p[counter] & mask;
1179 do_divmod(m, base_val, n, rem_res);
1183 *(--pos) = digits[_val(rem_res[0])];
1186 for (i = 0; i < calc_buffer_size; ++i)
1197 panic("Unsupported base %d", base);
1202 void init_strcalc(int precision)
1204 if (calc_buffer == NULL) {
1205 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1207 /* round up to multiple of 4 */
1208 precision = (precision + 3) & ~3;
1210 bit_pattern_size = (precision);
1211 calc_buffer_size = (precision / 2);
1212 max_value_size = (precision / 4);
1214 calc_buffer = XMALLOCN(char, calc_buffer_size + 1);
1215 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1217 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));
1222 void finish_strcalc(void)
1224 free(calc_buffer); calc_buffer = NULL;
1225 free(output_buffer); output_buffer = NULL;
1228 int sc_get_precision(void)
1230 return bit_pattern_size;
1234 void sc_add(const void *value1, const void *value2, void *buffer)
1236 CLEAR_BUFFER(calc_buffer);
1239 DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1240 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1242 do_add((const char*) value1, (const char*) value2, (char*) calc_buffer);
1244 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1246 if ((buffer != NULL) && (buffer != calc_buffer)) {
1247 memcpy(buffer, calc_buffer, calc_buffer_size);
1251 void sc_sub(const void *value1, const void *value2, void *buffer)
1253 CLEAR_BUFFER(calc_buffer);
1256 DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1257 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1259 do_sub((const char*) value1, (const char*) value2, calc_buffer);
1261 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1263 if ((buffer != NULL) && (buffer != calc_buffer)) {
1264 memcpy(buffer, calc_buffer, calc_buffer_size);
1268 void sc_neg(const void *value1, void *buffer)
1272 DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1274 do_negate((const char*) value1, calc_buffer);
1276 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1278 if ((buffer != NULL) && (buffer != calc_buffer)) {
1279 memcpy(buffer, calc_buffer, calc_buffer_size);
1283 void sc_and(const void *value1, const void *value2, void *buffer)
1285 CLEAR_BUFFER(calc_buffer);
1288 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1289 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1291 do_bitand((const char*) value1, (const char*) value2, calc_buffer);
1293 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1295 if ((buffer != NULL) && (buffer != calc_buffer)) {
1296 memcpy(buffer, calc_buffer, calc_buffer_size);
1300 void sc_andnot(const void *value1, const void *value2, void *buffer)
1302 CLEAR_BUFFER(calc_buffer);
1305 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1306 DEBUGPRINTF_COMPUTATION(("~%s -> ", sc_print_hex(value2)));
1308 do_bitandnot((const char*) value1, (const char*) value2, calc_buffer);
1310 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1312 if (buffer != NULL && buffer != calc_buffer) {
1313 memcpy(buffer, calc_buffer, calc_buffer_size);
1317 void sc_or(const void *value1, const void *value2, void *buffer)
1319 CLEAR_BUFFER(calc_buffer);
1322 DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1323 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1325 do_bitor((const char*) value1, (const char*) value2, calc_buffer);
1327 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1329 if ((buffer != NULL) && (buffer != calc_buffer)) {
1330 memcpy(buffer, calc_buffer, calc_buffer_size);
1334 void sc_xor(const void *value1, const void *value2, void *buffer)
1336 CLEAR_BUFFER(calc_buffer);
1339 DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1340 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1342 do_bitxor((const char*) value1, (const char*) value2, calc_buffer);
1344 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1346 if ((buffer != NULL) && (buffer != calc_buffer)) {
1347 memcpy(buffer, calc_buffer, calc_buffer_size);
1351 void sc_not(const void *value1, void *buffer)
1353 CLEAR_BUFFER(calc_buffer);
1356 DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1358 do_bitnot((const char*) value1, calc_buffer);
1360 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1362 if ((buffer != NULL) && (buffer != calc_buffer)) {
1363 memcpy(buffer, calc_buffer, calc_buffer_size);
1367 void sc_mul(const void *value1, const void *value2, void *buffer)
1369 CLEAR_BUFFER(calc_buffer);
1372 DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1373 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1375 do_mul((const char*) value1, (const char*) value2, calc_buffer);
1377 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1379 if ((buffer != NULL) && (buffer != calc_buffer)) {
1380 memcpy(buffer, calc_buffer, calc_buffer_size);
1384 void sc_div(const void *value1, const void *value2, void *buffer)
1386 /* temp buffer holding unused result of divmod */
1387 char *unused_res = (char*) alloca(calc_buffer_size);
1389 CLEAR_BUFFER(calc_buffer);
1392 DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1393 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1395 do_divmod((const char*) value1, (const char*) value2, calc_buffer, unused_res);
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_mod(const void *value1, const void *value2, void *buffer)
1406 /* temp buffer holding unused result of divmod */
1407 char *unused_res = (char*) alloca(calc_buffer_size);
1409 CLEAR_BUFFER(calc_buffer);
1412 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1413 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1415 do_divmod((const char*) value1, (const char*) value2, unused_res, calc_buffer);
1417 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1419 if ((buffer != NULL) && (buffer != calc_buffer)) {
1420 memcpy(buffer, calc_buffer, calc_buffer_size);
1424 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer)
1426 CLEAR_BUFFER(calc_buffer);
1429 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1430 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1432 do_divmod((const char*) value1, (const char*) value2, (char*) div_buffer, (char*) mod_buffer);
1434 DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1438 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1442 DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1443 do_shl((const char*) val1, calc_buffer, shift_cnt, bitsize, sign);
1445 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1447 if ((buffer != NULL) && (buffer != calc_buffer)) {
1448 memmove(buffer, calc_buffer, calc_buffer_size);
1452 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1454 long offset = sc_val_to_long(val2);
1456 sc_shlI(val1, offset, bitsize, sign, buffer);
1459 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1463 DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1464 do_shr((const char*) val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1466 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1468 if ((buffer != NULL) && (buffer != calc_buffer)) {
1469 memmove(buffer, calc_buffer, calc_buffer_size);
1473 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1475 long shift_cnt = sc_val_to_long(val2);
1477 sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1480 void sc_shrsI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1484 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), shift_cnt));
1485 do_shr((const char*) val1, calc_buffer, shift_cnt, bitsize, sign, 1);
1487 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1489 if ((buffer != NULL) && (buffer != calc_buffer)) {
1490 memmove(buffer, calc_buffer, calc_buffer_size);
1494 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1496 long offset = sc_val_to_long(val2);
1500 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1501 do_shr((const char*) val1, calc_buffer, offset, bitsize, sign, 1);
1503 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1505 if ((buffer != NULL) && (buffer != calc_buffer)) {
1506 memmove(buffer, calc_buffer, calc_buffer_size);
1510 void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1512 long offset = sc_val_to_long(val2);
1516 DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1517 do_rotl((const char*) val1, calc_buffer, offset, bitsize, sign);
1519 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1521 if ((buffer != NULL) && (buffer != calc_buffer)) {
1522 memmove(buffer, calc_buffer, calc_buffer_size);
1526 void sc_zero(void *buffer)
1529 buffer = calc_buffer;
1530 CLEAR_BUFFER(buffer);