no unnecessary comments behind #include
[libfirm] / ir / tv / strcalc.c
index 1a078eb..aed82e1 100644 (file)
  * @author   Mathias Heil
  * @version  $Id$
  */
+#include "config.h"
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-#ifdef HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
-#ifdef HAVE_STRING_H
-# include <string.h>  /* memset/memcmp */
-#endif
-#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 <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+#include <limits.h>
 
 #include "strcalc.h"
 #include "xmalloc.h"
@@ -705,7 +698,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 :( */
@@ -853,15 +846,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 */
@@ -1029,7 +1018,7 @@ void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *m
                case 'c':
                case 'b':
                case 'a':
-                       if (base[0] > SC_9 || base[1] > SC_0) { /* (base > 10) */
+                       if (base[0] > SC_A || base[1] > SC_0) { /* (base > 10) */
                                val[0] = _digit((*str)-'a'+10);
                        }
                        else
@@ -1042,7 +1031,7 @@ void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *m
                case 'C':
                case 'B':
                case 'A':
-                       if (base[0] > SC_9 || base[1] > SC_0) { /* (base > 10) */
+                       if (base[0] > SC_A || base[1] > SC_0) { /* (base > 10) */
                                val[0] = _digit((*str)-'A'+10);
                        }
                        else
@@ -1051,7 +1040,7 @@ void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *m
 
                case '9':
                case '8':
-                       if (base[0] > SC_7 || base[1] > SC_0) { /* (base > 8) */
+                       if (base[0] > SC_8 || base[1] > SC_0) { /* (base > 8) */
                                val[0] = _digit((*str)-'0');
                        }
                        else
@@ -1277,6 +1266,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;
@@ -1325,7 +1331,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;
@@ -1365,7 +1371,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) - 1];
+                       x = and_table[_val(val[counter++])][mask];
                        *(--pos) = digits[_val(x)];
                }
 
@@ -1391,7 +1398,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) - 1];
+                       x = and_table[_val(val[counter++])][mask];
 
                        pos -= 4;
                        p = binary_table[_val(x)];
@@ -1430,9 +1438,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) - 1];
+                       div1_res[counter] = and_table[_val(p[counter])][mask];
                        ++counter;
-
-                       div1_res[counter] = and_table[_val(p[counter])][bits & 3];
                }
 
                m = div1_res;
@@ -1472,8 +1480,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));
        }