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 shrs_table[16][4][2] = {
90 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
91 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
92 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
93 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
94 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
95 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
96 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
97 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
98 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
99 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
100 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
101 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
102 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
103 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
104 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
105 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
108 /** converting a digit to a binary string */
109 static char const *const binary_table[] = {
110 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
111 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
114 /*****************************************************************************
116 *****************************************************************************/
119 * implements the bitwise NOT operation
121 static void do_bitnot(const char *val, char *buffer)
125 for (counter = 0; counter<calc_buffer_size; counter++)
126 buffer[counter] = val[counter] ^ SC_F;
130 * implements the bitwise OR operation
132 static void do_bitor(const char *val1, const char *val2, char *buffer)
136 for (counter = 0; counter<calc_buffer_size; counter++)
137 buffer[counter] = val1[counter] | val2[counter];
141 * implements the bitwise eXclusive OR operation
143 static void do_bitxor(const char *val1, const char *val2, char *buffer)
147 for (counter = 0; counter<calc_buffer_size; counter++)
148 buffer[counter] = val1[counter] ^ val2[counter];
152 * implements the bitwise AND operation
154 static void do_bitand(const char *val1, const char *val2, char *buffer)
158 for (counter = 0; counter<calc_buffer_size; counter++)
159 buffer[counter] = val1[counter] & val2[counter];
163 * implements the bitwise AND not operation
165 static void do_bitandnot(const char *val1, const char *val2, char *buffer)
169 for (counter = 0; counter < calc_buffer_size; ++counter)
170 buffer[counter] = val1[counter] & (SC_F ^ val2[counter]);
174 * returns the sign bit.
176 * @todo This implementation is wrong, as it returns the highest bit of the buffer
177 * NOT the highest bit depending on the real mode
179 static int do_sign(const char *val)
181 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
185 * returns non-zero if bit at position pos is set
187 static int do_bit(const char *val, int pos)
190 int nibble = pos >> 2;
192 return _bitisset(val[nibble], bit);
196 * Implements a fast ADD + 1
198 static void do_inc(const char *val, char *buffer)
202 while (counter++ < calc_buffer_size) {
207 /* No carry here, *val != SC_F */
208 *buffer = *val + SC_1;
212 /* here a carry could be lost, this is intended because this should
213 * happen only when a value changes sign. */
217 * Implements a unary MINUS
219 static void do_negate(const char *val, char *buffer)
221 do_bitnot(val, buffer);
222 do_inc(buffer, buffer);
226 * Implements a binary ADD
228 * @todo The implementation of carry is wrong, as it is the
229 * calc_buffer_size carry, not the mode depending
231 static void do_add(const char *val1, const char *val2, char *buffer)
233 unsigned carry = SC_0;
234 for (int counter = 0; counter < calc_buffer_size; ++counter) {
235 unsigned const sum = val1[counter] + val2[counter] + carry;
236 buffer[counter] = SC_RESULT(sum);
237 carry = SC_CARRY(sum);
239 carry_flag = carry != SC_0;
243 * Implements a binary SUB
245 static void do_sub(const char *val1, const char *val2, char *buffer)
247 char *temp_buffer = (char*) alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
249 do_negate(val2, temp_buffer);
250 do_add(val1, temp_buffer, buffer);
254 * Implements a binary MUL
256 static void do_mul(const char *val1, const char *val2, char *buffer)
258 char *temp_buffer; /* result buffer */
259 char *neg_val1; /* abs of val1 */
260 char *neg_val2; /* abs of val2 */
262 char sign = 0; /* marks result sign */
263 int c_inner, c_outer; /* loop counters */
265 temp_buffer = (char*) alloca(calc_buffer_size);
266 neg_val1 = (char*) alloca(calc_buffer_size);
267 neg_val2 = (char*) alloca(calc_buffer_size);
269 /* init result buffer to zeros */
270 memset(temp_buffer, SC_0, calc_buffer_size);
272 /* the multiplication works only for positive values, for negative values *
273 * it is necessary to negate them and adjust the result accordingly */
274 if (do_sign(val1) == -1) {
275 do_negate(val1, neg_val1);
279 if (do_sign(val2) == -1) {
280 do_negate(val2, neg_val2);
285 for (c_outer = 0; c_outer < max_value_size; c_outer++) {
286 if (val2[c_outer] != SC_0) {
287 unsigned carry = SC_0; /* container for carries */
288 for (c_inner = 0; c_inner < max_value_size; c_inner++) {
289 /* do the following calculation: *
290 * Add the current carry, the value at position c_outer+c_inner *
291 * and the result of the multiplication of val1[c_inner] and *
292 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
294 /* multiplicate the two digits */
295 unsigned const mul = val1[c_inner] * val2[c_outer];
296 /* add old value to result of multiplication and the carry */
297 unsigned const sum = temp_buffer[c_inner + c_outer] + mul + carry;
299 /* all carries together result in new carry. This is always smaller *
301 * Both multiplicands, the carry and the value already in the temp *
302 * buffer are single digits and their value is therefore at most *
305 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
306 * The tables list all operations rem b, so the carry is at most *
307 * (b*b-1)rem b = -1rem b = b-1 */
308 temp_buffer[c_inner + c_outer] = SC_RESULT(sum);
309 carry = SC_CARRY(sum);
312 /* A carry may hang over */
313 /* c_outer is always smaller than max_value_size! */
314 temp_buffer[max_value_size + c_outer] = carry;
319 do_negate(temp_buffer, buffer);
321 memcpy(buffer, temp_buffer, calc_buffer_size);
325 * Shift the buffer to left and add a 4 bit digit
327 static void do_push(const char digit, char *buffer)
331 for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
332 buffer[counter+1] = buffer[counter];
338 * Implements truncating integer division and remainder.
340 * Note: This is MOST slow
342 static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
344 const char *dividend = rDividend;
345 const char *minus_divisor;
349 char div_sign = 0; /* remember division result sign */
350 char rem_sign = 0; /* remember remainder result sign */
352 int c_dividend; /* loop counters */
354 neg_val1 = (char*) alloca(calc_buffer_size);
355 neg_val2 = (char*) alloca(calc_buffer_size);
357 /* clear result buffer */
358 memset(quot, SC_0, calc_buffer_size);
359 memset(rem, SC_0, calc_buffer_size);
361 /* if the divisor is zero this won't work (quot is zero) */
362 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
364 /* if the dividend is zero result is zero (quot is zero) */
365 if (sc_comp(dividend, quot) == 0)
368 if (do_sign(dividend) == -1) {
369 do_negate(dividend, neg_val1);
375 do_negate(divisor, neg_val2);
376 if (do_sign(divisor) == -1) {
378 minus_divisor = divisor;
381 minus_divisor = neg_val2;
383 /* if divisor >= dividend division is easy
384 * (remember these are absolute values) */
385 switch (sc_comp(dividend, divisor)) {
386 case 0: /* dividend == divisor */
390 case -1: /* dividend < divisor */
391 memcpy(rem, dividend, calc_buffer_size);
394 default: /* unluckily division is necessary :( */
398 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
399 do_push(dividend[c_dividend], rem);
402 if (sc_comp(rem, divisor) != -1) { /* remainder >= divisor */
403 /* subtract until the remainder becomes negative, this should
404 * be faster than comparing remainder with divisor */
405 do_add(rem, minus_divisor, rem);
407 while (do_sign(rem) == 1) {
408 quot[0] = SC_RESULT(quot[0] + SC_1); /* TODO can this generate carry or is masking redundant? */
409 do_add(rem, minus_divisor, rem);
412 /* subtracted one too much */
413 do_add(rem, divisor, rem);
417 /* sets carry if remainder is non-zero ??? */
418 carry_flag = !sc_is_zero(rem);
421 do_negate(quot, quot);
428 * Implements a Shift Left, which can either preserve the sign bit
431 * @todo Assertions seems to be wrong
433 static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed)
438 assert((shift_cnt >= 0) || (0 && "negative leftshift"));
439 assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
440 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
441 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
443 /* if shifting far enough the result is zero */
444 if (shift_cnt >= bitsize) {
445 memset(buffer, SC_0, calc_buffer_size);
449 unsigned const shift = shift_cnt % SC_BITS;
450 shift_cnt = shift_cnt / 4;
452 /* shift the single digits some bytes (offset) and some bits (table)
454 unsigned carry = SC_0;
455 for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
456 unsigned const shl = val1[counter] << shift | carry;
457 buffer[counter + shift_cnt] = SC_RESULT(shl);
458 carry = SC_CARRY(shl);
461 unsigned const shl = val1[counter] << shift | carry;
462 buffer[counter + shift_cnt] = SC_RESULT(shl);
465 bitoffset = counter - 1;
468 /* fill with zeroes */
469 for (counter = 0; counter < shift_cnt; counter++)
470 buffer[counter] = SC_0;
472 /* if the mode was signed, change sign when the mode's msb is now 1 */
473 shift_cnt = bitoffset + shift_cnt;
474 bitoffset = (bitsize-1) % 4;
475 if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
476 /* this sets the upper bits of the leftmost digit */
477 buffer[shift_cnt] |= min_digit[bitoffset];
478 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
479 buffer[counter] = SC_F;
481 } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
482 /* this clears the upper bits of the leftmost digit */
483 buffer[shift_cnt] &= max_digit[bitoffset];
484 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
485 buffer[counter] = SC_0;
491 * Implements a Shift Right, which can either preserve the sign bit
494 * @param bitsize bitsize of the value to be shifted
496 * @todo Assertions seems to be wrong
498 static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift)
504 int shift_mod, shift_nib;
509 assert((shift_cnt >= 0) || (0 && "negative rightshift"));
510 assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
511 assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
513 sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
515 /* if shifting far enough the result is either 0 or -1 */
516 if (shift_cnt >= bitsize) {
517 if (!sc_is_zero(val1)) {
520 memset(buffer, sign, calc_buffer_size);
524 shift_mod = shift_cnt & 3;
525 shift_nib = shift_cnt >> 2;
527 /* check if any bits are lost, and set carry_flag if so */
528 for (counter = 0; counter < shift_nib; ++counter) {
529 if (val1[counter] != 0) {
534 if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
537 /* shift digits to the right with offset, carry and all */
538 buffer[0] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
539 for (counter = 1; counter < ((bitsize + 3) >> 2) - shift_nib; counter++) {
540 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
541 buffer[counter] = shrs[0];
542 buffer[counter - 1] |= shrs[1];
545 /* the last digit is special in regard of signed/unsigned shift */
546 bitoffset = bitsize & 3;
547 msd = sign; /* most significant digit */
549 /* remove sign bits if mode was signed and this is an unsigned shift */
550 if (!signed_shift && is_signed) {
551 msd &= max_digit[bitoffset];
554 shrs = shrs_table[_val(msd)][shift_mod];
556 /* signed shift and signed mode and negative value means all bits to the left are set */
557 if (signed_shift && sign == SC_F) {
558 buffer[counter] = shrs[0] | min_digit[bitoffset];
560 buffer[counter] = shrs[0];
564 buffer[counter - 1] |= shrs[1];
566 /* fill with SC_F or SC_0 depending on sign */
567 for (counter++; counter < calc_buffer_size; counter++) {
568 buffer[counter] = sign;
573 * Implements a Rotate Left.
574 * positive: low-order -> high order, negative other direction
576 static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
579 temp1 = (char*) alloca(calc_buffer_size);
580 temp2 = (char*) alloca(calc_buffer_size);
582 offset = offset % radius;
584 /* rotation by multiples of the type length is identity */
586 memmove(buffer, val1, calc_buffer_size);
590 do_shl(val1, temp1, offset, radius, is_signed);
591 do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
592 do_bitor(temp1, temp2, buffer);
593 carry_flag = 0; /* set by shr, but due to rot this is false */
596 /*****************************************************************************
597 * public functions, declared in strcalc.h
598 *****************************************************************************/
599 const void *sc_get_buffer(void)
601 return (void*)calc_buffer;
604 int sc_get_buffer_length(void)
606 return calc_buffer_size;
610 * Do sign extension if the mode is signed, otherwise to zero extension.
612 void sign_extend(void *buffer, ir_mode *mode)
614 char *calc_buffer = (char*) buffer;
615 int bits = get_mode_size_bits(mode) - 1;
616 int nibble = bits >> 2;
617 int max = max_digit[bits & 3];
620 if (mode_is_signed(mode)) {
621 if (calc_buffer[nibble] > max) {
622 /* sign bit is set, we need sign expansion */
624 for (i = nibble + 1; i < calc_buffer_size; ++i)
625 calc_buffer[i] = SC_F;
626 calc_buffer[nibble] |= sex_digit[bits & 3];
628 /* set all bits to zero */
629 for (i = nibble + 1; i < calc_buffer_size; ++i)
630 calc_buffer[i] = SC_0;
631 calc_buffer[nibble] &= zex_digit[bits & 3];
634 /* do zero extension */
635 for (i = nibble + 1; i < calc_buffer_size; ++i)
636 calc_buffer[i] = SC_0;
637 calc_buffer[nibble] &= zex_digit[bits & 3];
641 /* we assume that '0'-'9', 'a'-'z' and 'A'-'Z' are a range.
642 * The C-standard does theoretically allow otherwise. */
643 static inline void check_ascii(void)
645 /* C standard guarantees that '0'-'9' is a range */
658 int sc_val_from_str(char sign, unsigned base, const char *str,
659 size_t len, void *buffer)
663 assert(sign == -1 || sign == 1);
668 assert(base > 1 && base <= 16);
669 sc_base = (char*) alloca(calc_buffer_size);
670 sc_val_from_ulong(base, sc_base);
672 val = (char*) alloca(calc_buffer_size);
674 buffer = calc_buffer;
676 CLEAR_BUFFER(buffer);
679 /* BEGIN string evaluation, from left to right */
683 if (c >= '0' && c <= '9')
685 else if (c >= 'A' && c <= 'F')
687 else if (c >= 'a' && c <= 'f')
696 /* Radix conversion from base b to base B:
697 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
698 /* multiply current value with base */
699 do_mul(sc_base, (const char*) buffer, (char*) buffer);
700 /* add next digit to current value */
701 do_add(val, (const char*) buffer, (char*) buffer);
703 /* get ready for the next letter */
709 do_negate((const char*) buffer, (char*) buffer);
714 void sc_val_from_long(long value, void *buffer)
717 char sign, is_minlong;
719 if (buffer == NULL) buffer = calc_buffer;
720 pos = (char*) buffer;
723 is_minlong = value == LONG_MIN;
725 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
733 CLEAR_BUFFER(buffer);
735 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
736 *pos++ = _digit(value & 0xf);
742 do_inc((const char*) buffer, (char*) buffer);
744 do_negate((const char*) buffer, (char*) buffer);
748 void sc_val_from_ulong(unsigned long value, void *buffer)
752 if (buffer == NULL) buffer = calc_buffer;
753 pos = (unsigned char*) buffer;
755 while (pos < (unsigned char *)buffer + calc_buffer_size) {
756 *pos++ = (unsigned char)_digit(value & 0xf);
761 long sc_val_to_long(const void *val)
766 for (i = calc_buffer_size - 1; i >= 0; i--) {
767 l = (l << 4) + _val(((char *)val)[i]);
772 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
777 if (buffer == NULL) buffer = calc_buffer;
778 CLEAR_BUFFER(buffer);
780 if (!sign) return; /* unsigned means minimum is 0(zero) */
782 pos = (char*) buffer;
785 for (i = 0; i < bits/4; i++)
788 *pos++ = min_digit[bits%4];
790 for (i++; i <= calc_buffer_size - 1; i++)
794 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
799 if (buffer == NULL) buffer = calc_buffer;
800 CLEAR_BUFFER(buffer);
801 pos = (char*) buffer;
803 bits = num_bits - sign;
804 for (i = 0; i < bits/4; i++)
807 *pos++ = max_digit[bits%4];
809 for (i++; i <= calc_buffer_size - 1; i++)
813 void sc_truncate(unsigned int num_bits, void *buffer)
815 char *cbuffer = (char*) buffer;
816 char *pos = cbuffer + (num_bits / 4);
817 char *end = cbuffer + calc_buffer_size;
821 switch (num_bits % 4) {
822 case 0: /* nothing to do */ break;
823 case 1: *pos++ &= SC_1; break;
824 case 2: *pos++ &= SC_3; break;
825 case 3: *pos++ &= SC_7; break;
828 for ( ; pos < end; ++pos)
832 int sc_comp(const void* value1, const void* value2)
834 int counter = calc_buffer_size - 1;
835 const char *val1 = (const char *)value1;
836 const char *val2 = (const char *)value2;
838 /* compare signs first:
839 * the loop below can only compare values of the same sign! */
840 if (do_sign(val1) != do_sign(val2))
841 return (do_sign(val1) == 1)?(1):(-1);
843 /* loop until two digits differ, the values are equal if there
844 * are no such two digits */
845 while (val1[counter] == val2[counter]) {
847 if (counter < 0) return 0;
850 /* the leftmost digit is the most significant, so this returns
851 * the correct result.
852 * This implies the digit enum is ordered */
853 return (val1[counter] > val2[counter]) ? (1) : (-1);
856 int sc_get_highest_set_bit(const void *value)
858 const char *val = (const char*)value;
861 high = calc_buffer_size * 4 - 1;
863 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
864 if (val[counter] == SC_0)
867 if (val[counter] > SC_7) return high;
868 else if (val[counter] > SC_3) return high - 1;
869 else if (val[counter] > SC_1) return high - 2;
870 else return high - 3;
876 int sc_get_lowest_set_bit(const void *value)
878 const char *val = (const char*)value;
882 for (counter = 0; counter < calc_buffer_size; counter++) {
883 switch (val[counter]) {
910 int sc_get_bit_at(const void *value, unsigned pos)
912 const char *val = (const char*) value;
913 unsigned nibble = pos >> 2;
915 return (val[nibble] & SHIFT(pos & 3)) != SC_0;
918 void sc_set_bit_at(void *value, unsigned pos)
920 char *val = (char*) value;
921 unsigned nibble = pos >> 2;
923 val[nibble] |= SHIFT(pos & 3);
926 int sc_is_zero(const void *value)
928 const char* val = (const char *)value;
931 for (counter = 0; counter < calc_buffer_size; ++counter) {
932 if (val[counter] != SC_0)
938 int sc_is_negative(const void *value)
940 return do_sign((const char*) value) == -1;
943 int sc_had_carry(void)
948 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
950 const char *val = (const char *)value;
951 int nibble_ofs = 2 * byte_ofs;
954 /* the current scheme uses one byte to store a nibble */
955 if (4 * nibble_ofs >= len)
958 res = _val(val[nibble_ofs]);
959 if (len > 4 * (nibble_ofs + 1))
960 res |= _val(val[nibble_ofs + 1]) << 4;
962 /* kick bits outsize */
963 if (len - 8 * byte_ofs < 8) {
964 res &= (1 << (len - 8 * byte_ofs)) - 1;
970 * convert to a string
971 * FIXME: Doesn't check buffer bounds
973 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode)
975 static const char big_digits[] = "0123456789ABCDEF";
976 static const char small_digits[] = "0123456789abcdef";
978 char *base_val, *div1_res, *div2_res, *rem_res;
979 int counter, nibbles, i, sign, mask;
982 const char *val = (const char *)value;
986 const char *digits = small_digits;
988 base_val = (char*) alloca(calc_buffer_size);
989 div1_res = (char*) alloca(calc_buffer_size);
990 div2_res = (char*) alloca(calc_buffer_size);
991 rem_res = (char*) alloca(calc_buffer_size);
993 pos = output_buffer + bit_pattern_size;
998 bits = bit_pattern_size;
999 #ifdef STRCALC_DEBUG_FULLPRINT
1003 nibbles = bits >> 2;
1007 digits = big_digits;
1009 for (counter = 0; counter < nibbles; ++counter) {
1010 *(--pos) = digits[_val(val[counter])];
1011 #ifdef STRCALC_DEBUG_GROUPPRINT
1012 if ((counter+1)%8 == 0)
1017 /* last nibble must be masked */
1019 mask = zex_digit[(bits & 3) - 1];
1020 x = val[counter++] & mask;
1021 *(--pos) = digits[_val(x)];
1024 /* now kill zeros */
1025 for (; counter > 1; --counter, ++pos) {
1026 #ifdef STRCALC_DEBUG_GROUPPRINT
1027 if (pos[0] == ' ') ++pos;
1035 for (counter = 0; counter < nibbles; ++counter) {
1037 p = binary_table[_val(val[counter])];
1044 /* last nibble must be masked */
1046 mask = zex_digit[(bits & 3) - 1];
1047 x = val[counter++] & mask;
1050 p = binary_table[_val(x)];
1057 /* now kill zeros */
1058 for (counter <<= 2; counter > 1; --counter, ++pos)
1065 memset(base_val, SC_0, calc_buffer_size);
1066 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1070 if (signed_mode && base == SC_DEC) {
1071 /* check for negative values */
1072 if (do_bit(val, bits - 1)) {
1073 do_negate(val, div2_res);
1079 /* transfer data into oscillating buffers */
1080 memset(div1_res, SC_0, calc_buffer_size);
1081 for (counter = 0; counter < nibbles; ++counter)
1082 div1_res[counter] = p[counter];
1084 /* last nibble must be masked */
1086 mask = zex_digit[(bits & 3) - 1];
1087 div1_res[counter] = p[counter] & mask;
1094 do_divmod(m, base_val, n, rem_res);
1098 *(--pos) = digits[_val(rem_res[0])];
1101 for (i = 0; i < calc_buffer_size; ++i)
1112 panic("Unsupported base %d", base);
1117 void init_strcalc(int precision)
1119 if (calc_buffer == NULL) {
1120 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1122 /* round up to multiple of 4 */
1123 precision = (precision + 3) & ~3;
1125 bit_pattern_size = (precision);
1126 calc_buffer_size = (precision / 2);
1127 max_value_size = (precision / 4);
1129 calc_buffer = XMALLOCN(char, calc_buffer_size + 1);
1130 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1132 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));
1137 void finish_strcalc(void)
1139 free(calc_buffer); calc_buffer = NULL;
1140 free(output_buffer); output_buffer = NULL;
1143 int sc_get_precision(void)
1145 return bit_pattern_size;
1149 void sc_add(const void *value1, const void *value2, void *buffer)
1151 CLEAR_BUFFER(calc_buffer);
1154 DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1155 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1157 do_add((const char*) value1, (const char*) value2, (char*) calc_buffer);
1159 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1161 if ((buffer != NULL) && (buffer != calc_buffer)) {
1162 memcpy(buffer, calc_buffer, calc_buffer_size);
1166 void sc_sub(const void *value1, const void *value2, void *buffer)
1168 CLEAR_BUFFER(calc_buffer);
1171 DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1172 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1174 do_sub((const char*) value1, (const char*) value2, calc_buffer);
1176 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1178 if ((buffer != NULL) && (buffer != calc_buffer)) {
1179 memcpy(buffer, calc_buffer, calc_buffer_size);
1183 void sc_neg(const void *value1, void *buffer)
1187 DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1189 do_negate((const char*) value1, calc_buffer);
1191 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1193 if ((buffer != NULL) && (buffer != calc_buffer)) {
1194 memcpy(buffer, calc_buffer, calc_buffer_size);
1198 void sc_and(const void *value1, const void *value2, void *buffer)
1200 CLEAR_BUFFER(calc_buffer);
1203 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1204 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1206 do_bitand((const char*) value1, (const char*) value2, calc_buffer);
1208 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1210 if ((buffer != NULL) && (buffer != calc_buffer)) {
1211 memcpy(buffer, calc_buffer, calc_buffer_size);
1215 void sc_andnot(const void *value1, const void *value2, void *buffer)
1217 CLEAR_BUFFER(calc_buffer);
1220 DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1221 DEBUGPRINTF_COMPUTATION(("~%s -> ", sc_print_hex(value2)));
1223 do_bitandnot((const char*) value1, (const char*) value2, calc_buffer);
1225 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1227 if (buffer != NULL && buffer != calc_buffer) {
1228 memcpy(buffer, calc_buffer, calc_buffer_size);
1232 void sc_or(const void *value1, const void *value2, void *buffer)
1234 CLEAR_BUFFER(calc_buffer);
1237 DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1238 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1240 do_bitor((const char*) value1, (const char*) value2, calc_buffer);
1242 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1244 if ((buffer != NULL) && (buffer != calc_buffer)) {
1245 memcpy(buffer, calc_buffer, calc_buffer_size);
1249 void sc_xor(const void *value1, const void *value2, void *buffer)
1251 CLEAR_BUFFER(calc_buffer);
1254 DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1255 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1257 do_bitxor((const char*) value1, (const char*) value2, calc_buffer);
1259 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1261 if ((buffer != NULL) && (buffer != calc_buffer)) {
1262 memcpy(buffer, calc_buffer, calc_buffer_size);
1266 void sc_not(const void *value1, void *buffer)
1268 CLEAR_BUFFER(calc_buffer);
1271 DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1273 do_bitnot((const char*) value1, calc_buffer);
1275 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1277 if ((buffer != NULL) && (buffer != calc_buffer)) {
1278 memcpy(buffer, calc_buffer, calc_buffer_size);
1282 void sc_mul(const void *value1, const void *value2, void *buffer)
1284 CLEAR_BUFFER(calc_buffer);
1287 DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1288 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1290 do_mul((const char*) value1, (const char*) value2, calc_buffer);
1292 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1294 if ((buffer != NULL) && (buffer != calc_buffer)) {
1295 memcpy(buffer, calc_buffer, calc_buffer_size);
1299 void sc_div(const void *value1, const void *value2, void *buffer)
1301 /* temp buffer holding unused result of divmod */
1302 char *unused_res = (char*) alloca(calc_buffer_size);
1304 CLEAR_BUFFER(calc_buffer);
1307 DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1308 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1310 do_divmod((const char*) value1, (const char*) value2, calc_buffer, unused_res);
1312 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1314 if ((buffer != NULL) && (buffer != calc_buffer)) {
1315 memcpy(buffer, calc_buffer, calc_buffer_size);
1319 void sc_mod(const void *value1, const void *value2, void *buffer)
1321 /* temp buffer holding unused result of divmod */
1322 char *unused_res = (char*) alloca(calc_buffer_size);
1324 CLEAR_BUFFER(calc_buffer);
1327 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1328 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1330 do_divmod((const char*) value1, (const char*) value2, unused_res, calc_buffer);
1332 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1334 if ((buffer != NULL) && (buffer != calc_buffer)) {
1335 memcpy(buffer, calc_buffer, calc_buffer_size);
1339 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer)
1341 CLEAR_BUFFER(calc_buffer);
1344 DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1345 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1347 do_divmod((const char*) value1, (const char*) value2, (char*) div_buffer, (char*) mod_buffer);
1349 DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1353 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1357 DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1358 do_shl((const char*) val1, calc_buffer, shift_cnt, bitsize, sign);
1360 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1362 if ((buffer != NULL) && (buffer != calc_buffer)) {
1363 memmove(buffer, calc_buffer, calc_buffer_size);
1367 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1369 long offset = sc_val_to_long(val2);
1371 sc_shlI(val1, offset, bitsize, sign, buffer);
1374 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1378 DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1379 do_shr((const char*) val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1381 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1383 if ((buffer != NULL) && (buffer != calc_buffer)) {
1384 memmove(buffer, calc_buffer, calc_buffer_size);
1388 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1390 long shift_cnt = sc_val_to_long(val2);
1392 sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1395 void sc_shrsI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1399 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), shift_cnt));
1400 do_shr((const char*) val1, calc_buffer, shift_cnt, bitsize, sign, 1);
1402 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1404 if ((buffer != NULL) && (buffer != calc_buffer)) {
1405 memmove(buffer, calc_buffer, calc_buffer_size);
1409 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1411 long offset = sc_val_to_long(val2);
1415 DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1416 do_shr((const char*) val1, calc_buffer, offset, bitsize, sign, 1);
1418 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1420 if ((buffer != NULL) && (buffer != calc_buffer)) {
1421 memmove(buffer, calc_buffer, calc_buffer_size);
1425 void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1427 long offset = sc_val_to_long(val2);
1431 DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1432 do_rotl((const char*) val1, calc_buffer, offset, bitsize, sign);
1434 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1436 if ((buffer != NULL) && (buffer != calc_buffer)) {
1437 memmove(buffer, calc_buffer, calc_buffer_size);
1441 void sc_zero(void *buffer)
1444 buffer = calc_buffer;
1445 CLEAR_BUFFER(buffer);