- BugFix: value_param_tp must always exist
[libfirm] / ir / tv / strcalc.c
index 43b3ed3..0ea940a 100644 (file)
  * @version  $Id$
  */
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
 
-#ifdef HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
-#ifdef HAVE_STRING_H
-# include <string.h>  /* memset/memcmp */
-#endif
+#include <stdlib.h>
+#include <string.h>   /* memset/memcmp */
 #include <assert.h>   /* assertions */
 #include <stdio.h>    /* output for error messages */
 #include <limits.h>   /* definition of LONG_MIN, used in sc_get_val_from_long */
 
 #include "strcalc.h"
 #include "xmalloc.h"
+#include "error.h"
 
 /*
  * local definitions and macros
@@ -704,7 +699,7 @@ static void do_divmod(const char *rDividend, const char *divisor, char *quot, ch
                goto end;
 
        case -1: /* dividend < divisor */
-               memcpy(rem, rDividend, calc_buffer_size);
+               memcpy(rem, dividend, calc_buffer_size);
                goto end;
 
        default: /* unluckily division is necessary :( */
@@ -852,15 +847,11 @@ static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize,
                carry_flag = 1;
 
        /* shift digits to the right with offset, carry and all */
-       counter = 0;
-       if ((bitsize >> 2) > shift_nib) {
-               buffer[counter] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
-               counter = 1;
-       }
-       for (; counter < bitsize/4 - shift_nib; counter++) {
+       buffer[0] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
+       for (counter = 1; counter < ((bitsize + 3) >> 2) - shift_nib; counter++) {
                shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
-               buffer[counter] = shrs[0];
-               buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
+               buffer[counter]     = shrs[0];
+               buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
        }
 
        /* the last digit is special in regard of signed/unsigned shift */
@@ -1276,6 +1267,23 @@ int sc_get_lowest_set_bit(const void *value) {
        return -1;
 }
 
+int sc_get_bit_at(const void *value, unsigned pos) {
+       const char *val = value;
+       unsigned nibble = pos >> 2;
+
+       if (and_table[(int) val[nibble]][(int) shift_table[pos & 3]] != SC_0)
+               return 1;
+       return 0;
+}
+
+void sc_set_bit_at(void *value, unsigned pos)
+{
+       char *val = value;
+       unsigned nibble = pos >> 2;
+
+       val[nibble] = or_table[(int)val[nibble]][(int)shift_table[pos & 3]];
+}
+
 int sc_is_zero(const void *value) {
        const char* val = (const char *)value;
        int counter;
@@ -1324,7 +1332,7 @@ const char *sc_print(const void *value, unsigned bits, enum base_t base, int sig
        static const char small_digits[] = "0123456789abcdef";
 
        char *base_val, *div1_res, *div2_res, *rem_res;
-       int counter, nibbles, i, sign;
+       int counter, nibbles, i, sign, mask;
        char x;
 
        const char *val = (const char *)value;
@@ -1364,7 +1372,8 @@ const char *sc_print(const void *value, unsigned bits, enum base_t base, int sig
 
                /* last nibble must be masked */
                if (bits & 3) {
-                       x = and_table[_val(val[++counter])][bits & 3];
+                       mask = zex_digit[bits & 3];
+                       x = and_table[_val(val[counter++])][mask];
                        *(--pos) = digits[_val(x)];
                }
 
@@ -1390,7 +1399,8 @@ const char *sc_print(const void *value, unsigned bits, enum base_t base, int sig
 
                /* last nibble must be masked */
                if (bits & 3) {
-                       x = and_table[_val(val[++counter])][bits & 3];
+                       mask = zex_digit[bits & 3];
+                       x = and_table[_val(val[counter++])][mask];
 
                        pos -= 4;
                        p = binary_table[_val(x)];
@@ -1429,9 +1439,9 @@ const char *sc_print(const void *value, unsigned bits, enum base_t base, int sig
 
                /* last nibble must be masked */
                if (bits & 3) {
+                       mask = zex_digit[bits & 3];
+                       div1_res[counter] = and_table[_val(p[counter])][mask];
                        ++counter;
-
-                       div1_res[counter] = and_table[_val(p[counter])][bits & 3];
                }
 
                m = div1_res;
@@ -1455,9 +1465,7 @@ const char *sc_print(const void *value, unsigned bits, enum base_t base, int sig
                break;
 
        default:
-               printf("%i\n", base);
-               assert(0);
-               return NULL;
+               panic("Unsupported base %d", base);
        }
        return pos;
 }
@@ -1473,8 +1481,8 @@ void init_strcalc(int precision) {
                calc_buffer_size = (precision / 2);
                max_value_size   = (precision / 4);
 
-               calc_buffer   = xmalloc(calc_buffer_size+1 * sizeof(char));
-               output_buffer = xmalloc(bit_pattern_size+1 * sizeof(char));
+               calc_buffer   = XMALLOCN(char, calc_buffer_size + 1);
+               output_buffer = XMALLOCN(char, bit_pattern_size + 1);
 
                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));
        }