assume we always have strings.h
[libfirm] / ir / tv / strcalc.c
index 47c9475..1f77823 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
  * @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"
+#include "error.h"
 
 /*
  * local definitions and macros
  */
 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, calc_buffer_size)
+#define SHIFT(count) (SC_1 << (count))
 #define _val(a) ((a)-SC_0)
 #define _digit(a) ((a)+SC_0)
-#define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
-
-#define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__,  __LINE__)
+#define _bitisset(digit, pos) (((digit) & SHIFT(pos)) != SC_0)
 
 /* shortcut output for debugging */
 #  define sc_print_hex(a) sc_print((a), 0, SC_HEX, 0)
@@ -90,159 +83,6 @@ static const char zex_digit[4] = { SC_1, SC_3, SC_7, SC_F };
 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
 
-static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
-                              SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
-
-static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
-
-static const char and_table[16][16] = {
-                            { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
-                              SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
-
-                            { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
-                              SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
-
-                            { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
-                              SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
-
-                            { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
-                              SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
-
-                            { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
-                              SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
-
-                            { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
-                              SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
-
-                            { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
-                              SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
-
-                            { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
-                              SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
-
-                            { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
-                              SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
-
-                            { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
-                              SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
-
-                            { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
-                              SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
-
-                            { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
-                              SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
-
-                            { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
-                              SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
-
-                            { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
-                              SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
-
-                            { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
-                              SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
-
-                            { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
-                              SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
-
-static const char or_table[16][16] = {
-                            { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
-                              SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
-
-                            { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
-                              SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
-
-                            { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
-                              SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
-
-                            { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
-                              SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
-
-                            { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
-                              SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
-
-                            { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
-                              SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
-
-                            { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
-                              SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
-
-                            { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
-                              SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
-
-                            { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
-                              SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
-
-                            { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
-                              SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
-
-                            { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
-                              SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
-
-                            { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
-                              SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
-
-                            { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
-                              SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
-
-                            { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
-                              SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
-
-                            { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
-                              SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
-
-                            { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
-                              SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
-
-static char const xor_table[16][16] = {
-                             { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
-                               SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
-
-                             { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
-                               SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
-
-                             { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
-                               SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
-
-                             { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
-                               SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
-
-                             { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
-                               SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
-
-                             { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
-                               SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
-
-                             { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
-                               SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
-
-                             { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
-                               SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
-
-                             { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
-                               SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
-
-                             { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
-                               SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
-
-                             { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
-                               SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
-
-                             { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
-                               SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
-
-                             { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
-                               SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
-
-                             { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
-                               SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
-
-                             { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
-                               SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
-
-                             { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
-                               SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
-                                };
-
 static char const add_table[16][16][2] = {
                        { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
                          {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
@@ -435,53 +275,60 @@ static const char *binary_table[16] = {
 /*****************************************************************************
  * private functions
  *****************************************************************************/
-static void _fail_char(const char *str, size_t len, const char fchar, int pos,
-                       const char *file, int line) {
-       printf("ERROR:\n");
-       printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
-       while (len-- && *str) printf("%c", *str++); printf("\n");
-       while (--pos) printf(" "); printf("^\n");
-       exit(-1);
-}
 
 /**
  * implements the bitwise NOT operation
  */
-static void _bitnot(const char *val, char *buffer) {
+static void do_bitnot(const char *val, char *buffer)
+{
        int counter;
 
        for (counter = 0; counter<calc_buffer_size; counter++)
-               buffer[counter] = not_table[_val(val[counter])];
+               buffer[counter] = val[counter] ^ SC_F;
 }
 
 /**
  * implements the bitwise OR operation
  */
-static void _bitor(const char *val1, const char *val2, char *buffer) {
+static void do_bitor(const char *val1, const char *val2, char *buffer)
+{
        int counter;
 
        for (counter = 0; counter<calc_buffer_size; counter++)
-               buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
+               buffer[counter] = val1[counter] | val2[counter];
 }
 
 /**
  * implements the bitwise eXclusive OR operation
  */
-static void _bitxor(const char *val1, const char *val2, char *buffer) {
+static void do_bitxor(const char *val1, const char *val2, char *buffer)
+{
        int counter;
 
        for (counter = 0; counter<calc_buffer_size; counter++)
-               buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
+               buffer[counter] = val1[counter] ^ val2[counter];
 }
 
 /**
  * implements the bitwise AND operation
  */
-static void _bitand(const char *val1, const char *val2, char *buffer) {
+static void do_bitand(const char *val1, const char *val2, char *buffer)
+{
        int counter;
 
        for (counter = 0; counter<calc_buffer_size; counter++)
-               buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
+               buffer[counter] = val1[counter] & val2[counter];
+}
+
+/**
+ * implements the bitwise AND not operation
+ */
+static void do_bitandnot(const char *val1, const char *val2, char *buffer)
+{
+       int counter;
+
+       for (counter = 0; counter < calc_buffer_size; ++counter)
+               buffer[counter] = val1[counter] & (SC_F ^ val2[counter]);
 }
 
 /**
@@ -490,14 +337,16 @@ static void _bitand(const char *val1, const char *val2, char *buffer) {
  * @todo This implementation is wrong, as it returns the highest bit of the buffer
  *       NOT the highest bit depending on the real mode
  */
-static int _sign(const char *val) {
+static int do_sign(const char *val)
+{
        return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
 }
 
 /**
  * returns non-zero if bit at position pos is set
  */
-static int _bit(const char *val, int pos) {
+static int do_bit(const char *val, int pos)
+{
        int bit    = pos & 3;
        int nibble = pos >> 2;
 
@@ -507,7 +356,8 @@ static int _bit(const char *val, int pos) {
 /**
  * Implements a fast ADD + 1
  */
-static void _inc(const char *val, char *buffer) {
+static void do_inc(const char *val, char *buffer)
+{
        int counter = 0;
 
        while (counter++ < calc_buffer_size) {
@@ -527,9 +377,10 @@ static void _inc(const char *val, char *buffer) {
 /**
  * Implements a unary MINUS
  */
-static void _negate(const char *val, char *buffer) {
-       _bitnot(val, buffer);
-       _inc(buffer, buffer);
+static void do_negate(const char *val, char *buffer)
+{
+       do_bitnot(val, buffer);
+       do_inc(buffer, buffer);
 }
 
 /**
@@ -538,7 +389,8 @@ static void _negate(const char *val, char *buffer) {
  * @todo The implementation of carry is wrong, as it is the
  *       calc_buffer_size carry, not the mode depending
  */
-static void _add(const char *val1, const char *val2, char *buffer) {
+static void do_add(const char *val1, const char *val2, char *buffer)
+{
        int counter;
        const char *add1, *add2;
        char carry = SC_0;
@@ -556,17 +408,19 @@ static void _add(const char *val1, const char *val2, char *buffer) {
 /**
  * Implements a binary SUB
  */
-static void _sub(const char *val1, const char *val2, char *buffer) {
-       char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
+static void do_sub(const char *val1, const char *val2, char *buffer)
+{
+       char *temp_buffer = (char*) alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
 
-       _negate(val2, temp_buffer);
-       _add(val1, temp_buffer, buffer);
+       do_negate(val2, temp_buffer);
+       do_add(val1, temp_buffer, buffer);
 }
 
 /**
  * Implements a binary MUL
  */
-static void _mul(const char *val1, const char *val2, char *buffer) {
+static void do_mul(const char *val1, const char *val2, char *buffer)
+{
        char *temp_buffer; /* result buffer */
        char *neg_val1;    /* abs of val1 */
        char *neg_val2;    /* abs of val2 */
@@ -576,22 +430,22 @@ static void _mul(const char *val1, const char *val2, char *buffer) {
        char sign = 0;                      /* marks result sign */
        int c_inner, c_outer;               /* loop counters */
 
-       temp_buffer = alloca(calc_buffer_size);
-       neg_val1 = alloca(calc_buffer_size);
-       neg_val2 = alloca(calc_buffer_size);
+       temp_buffer = (char*) alloca(calc_buffer_size);
+       neg_val1 = (char*) alloca(calc_buffer_size);
+       neg_val2 = (char*) alloca(calc_buffer_size);
 
-       /* init result buffer to zeroes */
+       /* init result buffer to zeros */
        memset(temp_buffer, SC_0, calc_buffer_size);
 
        /* the multiplication works only for positive values, for negative values *
         * it is necessary to negate them and adjust the result accordingly       */
-       if (_sign(val1) == -1) {
-               _negate(val1, neg_val1);
+       if (do_sign(val1) == -1) {
+               do_negate(val1, neg_val1);
                val1 = neg_val1;
                sign ^= 1;
        }
-       if (_sign(val2) == -1) {
-               _negate(val2, neg_val2);
+       if (do_sign(val2) == -1) {
+               do_negate(val2, neg_val2);
                val2 = neg_val2;
                sign ^= 1;
        }
@@ -634,7 +488,7 @@ static void _mul(const char *val1, const char *val2, char *buffer) {
        }
 
        if (sign)
-               _negate(temp_buffer, buffer);
+               do_negate(temp_buffer, buffer);
        else
                memcpy(buffer, temp_buffer, calc_buffer_size);
 }
@@ -642,7 +496,8 @@ static void _mul(const char *val1, const char *val2, char *buffer) {
 /**
  * Shift the buffer to left and add a 4 bit digit
  */
-static void _push(const char digit, char *buffer) {
+static void do_push(const char digit, char *buffer)
+{
        int counter;
 
        for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
@@ -656,7 +511,8 @@ static void _push(const char digit, char *buffer) {
  *
  * Note: This is MOST slow
  */
-static void _divmod(const char *rDividend, const char *divisor, char *quot, char *rem) {
+static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
+{
        const char *dividend = rDividend;
        const char *minus_divisor;
        char *neg_val1;
@@ -667,8 +523,8 @@ static void _divmod(const char *rDividend, const char *divisor, char *quot, char
 
        int c_dividend;      /* loop counters */
 
-       neg_val1 = alloca(calc_buffer_size);
-       neg_val2 = alloca(calc_buffer_size);
+       neg_val1 = (char*) alloca(calc_buffer_size);
+       neg_val2 = (char*) alloca(calc_buffer_size);
 
        /* clear result buffer */
        memset(quot, SC_0, calc_buffer_size);
@@ -681,15 +537,15 @@ static void _divmod(const char *rDividend, const char *divisor, char *quot, char
        if (sc_comp(dividend, quot) == 0)
                return;
 
-       if (_sign(dividend) == -1) {
-               _negate(dividend, neg_val1);
+       if (do_sign(dividend) == -1) {
+               do_negate(dividend, neg_val1);
                div_sign ^= 1;
                rem_sign ^= 1;
                dividend = neg_val1;
        }
 
-       _negate(divisor, neg_val2);
-       if (_sign(divisor) == -1) {
+       do_negate(divisor, neg_val2);
+       if (do_sign(divisor) == -1) {
                div_sign ^= 1;
                minus_divisor = divisor;
                divisor = neg_val2;
@@ -704,7 +560,7 @@ static void _divmod(const char *rDividend, const char *divisor, char *quot, char
                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 :( */
@@ -712,21 +568,21 @@ static void _divmod(const char *rDividend, const char *divisor, char *quot, char
        }
 
        for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
-               _push(dividend[c_dividend], rem);
-               _push(SC_0, quot);
+               do_push(dividend[c_dividend], rem);
+               do_push(SC_0, quot);
 
                if (sc_comp(rem, divisor) != -1) {  /* remainder >= divisor */
                        /* subtract until the remainder becomes negative, this should
                         * be faster than comparing remainder with divisor  */
-                       _add(rem, minus_divisor, rem);
+                       do_add(rem, minus_divisor, rem);
 
-                       while (_sign(rem) == 1) {
+                       while (do_sign(rem) == 1) {
                                quot[0] = add_table[_val(quot[0])][SC_1][0];
-                               _add(rem, minus_divisor, rem);
+                               do_add(rem, minus_divisor, rem);
                        }
 
                        /* subtracted one too much */
-                       _add(rem, divisor, rem);
+                       do_add(rem, divisor, rem);
                }
        }
 end:
@@ -734,10 +590,10 @@ end:
        carry_flag = !sc_is_zero(rem);
 
        if (div_sign)
-               _negate(quot, quot);
+               do_negate(quot, quot);
 
        if (rem_sign)
-               _negate(rem, rem);
+               do_negate(rem, rem);
 }
 
 /**
@@ -746,7 +602,8 @@ end:
  *
  * @todo Assertions seems to be wrong
  */
-static void _shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed) {
+static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed)
+{
        const char *shl;
        char shift;
        char carry = SC_0;
@@ -755,9 +612,9 @@ static void _shl(const char *val1, char *buffer, long shift_cnt, int bitsize, un
        int bitoffset = 0;
 
        assert((shift_cnt >= 0) || (0 && "negative leftshift"));
-       assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
-       assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
-       assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
+       assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
+       assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
+       assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
 
        /* if shifting far enough the result is zero */
        if (shift_cnt >= bitsize) {
@@ -765,19 +622,19 @@ static void _shl(const char *val1, char *buffer, long shift_cnt, int bitsize, un
                return;
        }
 
-       shift = shift_table[_val(shift_cnt%4)];      /* this is 2 ** (offset % 4) */
+       shift     = SHIFT(shift_cnt % 4); /* this is 2 ** (offset % 4) */
        shift_cnt = shift_cnt / 4;
 
        /* shift the single digits some bytes (offset) and some bits (table)
         * to the left */
        for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
                shl = mul_table[_val(val1[counter])][_val(shift)];
-               buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
+               buffer[counter + shift_cnt] = shl[0] | carry;
                carry = shl[1];
        }
        if (bitsize%4 > 0) {
                shl = mul_table[_val(val1[counter])][_val(shift)];
-               buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
+               buffer[counter + shift_cnt] = shl[0] | carry;
                bitoffset = counter;
        } else {
                bitoffset = counter - 1;
@@ -792,13 +649,13 @@ static void _shl(const char *val1, char *buffer, long shift_cnt, int bitsize, un
        bitoffset = (bitsize-1) % 4;
        if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
                /* this sets the upper bits of the leftmost digit */
-               buffer[shift_cnt] = or_table[_val(buffer[shift_cnt])][_val(min_digit[bitoffset])];
+               buffer[shift_cnt] |= min_digit[bitoffset];
                for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
                        buffer[counter] = SC_F;
                }
        } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
                /* this clears the upper bits of the leftmost digit */
-               buffer[shift_cnt] = and_table[_val(buffer[shift_cnt])][_val(max_digit[bitoffset])];
+               buffer[shift_cnt] &= max_digit[bitoffset];
                for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
                        buffer[counter] = SC_0;
                }
@@ -813,7 +670,8 @@ static void _shl(const char *val1, char *buffer, long shift_cnt, int bitsize, un
  *
  * @todo Assertions seems to be wrong
  */
-static void _shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift) {
+static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift)
+{
        const char *shrs;
        char sign;
        char msd;
@@ -824,10 +682,10 @@ static void _shr(const char *val1, char *buffer, long shift_cnt, int bitsize, un
        int bitoffset = 0;
 
        assert((shift_cnt >= 0) || (0 && "negative rightshift"));
-       assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
-       assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
+       assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
+       assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
 
-       sign = signed_shift && _bit(val1, bitsize - 1) ? SC_F : SC_0;
+       sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
 
        /* if shifting far enough the result is either 0 or -1 */
        if (shift_cnt >= bitsize) {
@@ -852,15 +710,11 @@ static void _shr(const char *val1, char *buffer, long shift_cnt, int bitsize, un
                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] |= shrs[1];
        }
 
        /* the last digit is special in regard of signed/unsigned shift */
@@ -869,20 +723,20 @@ static void _shr(const char *val1, char *buffer, long shift_cnt, int bitsize, un
 
        /* remove sign bits if mode was signed and this is an unsigned shift */
        if (!signed_shift && is_signed) {
-               msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
+               msd &= max_digit[bitoffset];
        }
 
        shrs = shrs_table[_val(msd)][shift_mod];
 
        /* signed shift and signed mode and negative value means all bits to the left are set */
        if (signed_shift && sign == SC_F) {
-               buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
+               buffer[counter] = shrs[0] | min_digit[bitoffset];
        } else {
                buffer[counter] = shrs[0];
        }
 
        if (counter > 0)
-               buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
+               buffer[counter - 1] |= shrs[1];
 
        /* fill with SC_F or SC_0 depending on sign */
        for (counter++; counter < calc_buffer_size; counter++) {
@@ -891,13 +745,14 @@ static void _shr(const char *val1, char *buffer, long shift_cnt, int bitsize, un
 }
 
 /**
- * Implements a Rotate Right.
+ * Implements a Rotate Left.
  * positive: low-order -> high order, negative other direction
  */
-static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed) {
+static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
+{
        char *temp1, *temp2;
-       temp1 = alloca(calc_buffer_size);
-       temp2 = alloca(calc_buffer_size);
+       temp1 = (char*) alloca(calc_buffer_size);
+       temp2 = (char*) alloca(calc_buffer_size);
 
        offset = offset % radius;
 
@@ -907,28 +762,31 @@ static void _rot(const char *val1, char *buffer, long offset, int radius, unsign
                return;
        }
 
-       _shl(val1, temp1, offset, radius, is_signed);
-       _shr(val1, temp2, radius - offset, radius, is_signed, 0);
-       _bitor(temp1, temp2, buffer);
+       do_shl(val1, temp1, offset, radius, is_signed);
+       do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
+       do_bitor(temp1, temp2, buffer);
        carry_flag = 0; /* set by shr, but due to rot this is false */
 }
 
 /*****************************************************************************
  * public functions, declared in strcalc.h
  *****************************************************************************/
-const void *sc_get_buffer(void) {
+const void *sc_get_buffer(void)
+{
        return (void*)calc_buffer;
 }
 
-int sc_get_buffer_length(void) {
+int sc_get_buffer_length(void)
+{
        return calc_buffer_size;
 }
 
 /**
  * Do sign extension if the mode is signed, otherwise to zero extension.
  */
-void sign_extend(void *buffer, ir_mode *mode) {
-       char *calc_buffer = buffer;
+void sign_extend(void *buffer, ir_mode *mode)
+{
+       char *calc_buffer = (char*) buffer;
        int bits          = get_mode_size_bits(mode) - 1;
        int nibble        = bits >> 2;
        int max           = max_digit[bits & 3];
@@ -940,161 +798,101 @@ void sign_extend(void *buffer, ir_mode *mode) {
 
                        for (i = nibble + 1; i < calc_buffer_size; ++i)
                                calc_buffer[i] = SC_F;
-                       calc_buffer[nibble] = or_table[(int)calc_buffer[nibble]][(int)sex_digit[bits & 3]];
+                       calc_buffer[nibble] |= sex_digit[bits & 3];
                } else {
                        /* set all bits to zero */
                        for (i = nibble + 1; i < calc_buffer_size; ++i)
                                calc_buffer[i] = SC_0;
-                       calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
+                       calc_buffer[nibble] &= zex_digit[bits & 3];
                }
        } else {
                /* do zero extension */
                for (i = nibble + 1; i < calc_buffer_size; ++i)
                        calc_buffer[i] = SC_0;
-               calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
+               calc_buffer[nibble] &= zex_digit[bits & 3];
        }
 }
 
-/* FIXME doesn't check for overflows */
-void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode) {
-       const char *orig_str = str;
-       unsigned int orig_len = len;
+/* we assume that '0'-'9', 'a'-'z' and 'A'-'Z' are a range.
+ * The C-standard does theoretically allow otherwise. */
+static inline void check_ascii(void)
+{
+       /* C standard guarantees that '0'-'9' is a range */
+       assert('b'-'a' == 1
+               && 'c'-'a' == 2
+               && 'd'-'a' == 3
+               && 'e'-'a' == 4
+               && 'f'-'a' == 5);
+       assert('B'-'A' == 1
+               && 'C'-'A' == 2
+               && 'D'-'A' == 3
+               && 'E'-'A' == 4
+               && 'F'-'A' == 5);
+}
 
-       char sign = 0;
-       char *base, *val;
+int sc_val_from_str(char sign, unsigned base, const char *str,
+                    size_t len, void *buffer)
+{
+       char *sc_base, *val;
 
-       base = alloca(calc_buffer_size);
-       val = alloca(calc_buffer_size);
+       assert(sign == -1 || sign == 1);
+       assert(str != NULL);
+       assert(len > 0);
+       check_ascii();
 
-       /* verify valid pointers (not null) */
-       assert(str);
-       /* a string no characters long is an error */
-       assert(len);
+       assert(base > 1 && base <= 16);
+       sc_base = (char*) alloca(calc_buffer_size);
+       sc_val_from_ulong(base, sc_base);
 
-       if (buffer == NULL) buffer = calc_buffer;
+       val = (char*) alloca(calc_buffer_size);
+       if (buffer == NULL)
+               buffer = calc_buffer;
 
        CLEAR_BUFFER(buffer);
-       CLEAR_BUFFER(base);
        CLEAR_BUFFER(val);
 
-       /* strip leading spaces */
-       while ((len > 0) && (*str == ' ')) { len--; str++; }
-
-       /* if the first two characters are 0x or 0X -> hex
-        * if the first is a 0 -> oct
-        * else dec, strip leading -/+ and remember sign
-        *
-        * only a + or - sign is no number resulting in an error */
-       if (len >= 2) {
-               switch (str[0]) {
-               case '0':
-                       if (str[1] == 'x' || str[1] == 'X') { /* hex */
-                               str += 2;
-                               len -= 2;
-                               base[1] = SC_1; base[0] = SC_0;
-                       } else { /* oct */
-                               str += 1;
-                               len -= 1;
-                               base[1] = SC_0; base[0] = SC_8;
-                       }
-                       break;
-
-               case '+':
-                       str += 1;
-                       len -= 1;
-                       base[1] = SC_0; base[0] = SC_A;
-                       break;
-
-               case '-':
-                       str += 1;
-                       len -= 1;
-                       sign = 1;
-                       base[1] = SC_0; base[0] = SC_A;
-                       break;
-
-               default: /* dec, else would have begun with 0x or 0 */
-                       base[1] = SC_0; base[0] = SC_A;
-               }
-       } else { /* dec, else would have begun with 0x or 0 */
-               base[1] = SC_0; base[0] = SC_A;
-       }
-
        /* BEGIN string evaluation, from left to right */
        while (len > 0) {
-               switch (*str) {
-               case 'f':
-               case 'e':
-               case 'd':
-               case 'c':
-               case 'b':
-               case 'a':
-                       if (base[0] > SC_9 || base[1] > SC_0) { /* (base > 10) */
-                               val[0] = _digit((*str)-'a'+10);
-                       }
-                       else
-                               fail_char(orig_str, orig_len, *str, str-orig_str+1);
-                       break;
-
-               case 'F':
-               case 'E':
-               case 'D':
-               case 'C':
-               case 'B':
-               case 'A':
-                       if (base[0] > SC_9 || base[1] > SC_0) { /* (base > 10) */
-                               val[0] = _digit((*str)-'A'+10);
-                       }
-                       else
-                               fail_char(orig_str, orig_len, *str, str-orig_str+1);
-                       break;
-
-               case '9':
-               case '8':
-                       if (base[0] > SC_7 || base[1] > SC_0) { /* (base > 8) */
-                               val[0] = _digit((*str)-'0');
-                       }
-                       else
-                               fail_char(orig_str, orig_len, *str, str-orig_str+1);
-                       break;
-
-               case '7':
-               case '6':
-               case '5':
-               case '4':
-               case '3':
-               case '2':
-               case '1':
-               case '0':
-                       val[0] = _digit((*str)-'0');
-                       break;
+               char c = *str;
+               unsigned v;
+               if (c >= '0' && c <= '9')
+                       v = c - '0';
+               else if (c >= 'A' && c <= 'F')
+                       v = c - 'A' + 10;
+               else if (c >= 'a' && c <= 'f')
+                       v = c - 'a' + 10;
+               else
+                       return 0;
 
-               default:
-                       fail_char(orig_str, orig_len, *str, str-orig_str+1);
-               } /* switch(*str) */
+               if (v >= base)
+                       return 0;
+               val[0] = v;
 
                /* Radix conversion from base b to base B:
                 *  (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
-               _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
-               _add(val, calc_buffer, calc_buffer);  /* add next digit to current value  */
+               /* multiply current value with base */
+               do_mul(sc_base, (const char*) buffer, (char*) buffer);
+               /* add next digit to current value  */
+               do_add(val, (const char*) buffer, (char*) buffer);
 
                /* get ready for the next letter */
                str++;
                len--;
        } /* while (len > 0 ) */
 
-       if (sign)
-               _negate(calc_buffer, calc_buffer);
+       if (sign < 0)
+               do_negate((const char*) buffer, (char*) buffer);
 
-       /* beware: even if hex numbers have no sign, we need sign extension here */
-       sign_extend(calc_buffer, mode);
+       return 1;
 }
 
-void sc_val_from_long(long value, void *buffer) {
+void sc_val_from_long(long value, void *buffer)
+{
        char *pos;
        char sign, is_minlong;
 
        if (buffer == NULL) buffer = calc_buffer;
-       pos = buffer;
+       pos = (char*) buffer;
 
        sign = (value < 0);
        is_minlong = value == LONG_MIN;
@@ -1116,17 +914,18 @@ void sc_val_from_long(long value, void *buffer) {
 
        if (sign) {
                if (is_minlong)
-                       _inc(buffer, buffer);
+                       do_inc((const char*) buffer, (char*) buffer);
 
-               _negate(buffer, buffer);
+               do_negate((const char*) buffer, (char*) buffer);
        }
 }
 
-void sc_val_from_ulong(unsigned long value, void *buffer) {
+void sc_val_from_ulong(unsigned long value, void *buffer)
+{
        unsigned char *pos;
 
        if (buffer == NULL) buffer = calc_buffer;
-       pos = buffer;
+       pos = (unsigned char*) buffer;
 
        while (pos < (unsigned char *)buffer + calc_buffer_size) {
                *pos++ = (unsigned char)_digit(value & 0xf);
@@ -1134,7 +933,8 @@ void sc_val_from_ulong(unsigned long value, void *buffer) {
        }
 }
 
-long sc_val_to_long(const void *val) {
+long sc_val_to_long(const void *val)
+{
        int i;
        long l = 0;
 
@@ -1144,7 +944,8 @@ long sc_val_to_long(const void *val) {
        return l;
 }
 
-void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
+void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
+{
        char *pos;
        int i, bits;
 
@@ -1153,7 +954,7 @@ void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
 
        if (!sign) return;  /* unsigned means minimum is 0(zero) */
 
-       pos = buffer;
+       pos = (char*) buffer;
 
        bits = num_bits - 1;
        for (i = 0; i < bits/4; i++)
@@ -1165,13 +966,14 @@ void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
                *pos++ = SC_F;
 }
 
-void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
+void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
+{
        char* pos;
        int i, bits;
 
        if (buffer == NULL) buffer = calc_buffer;
        CLEAR_BUFFER(buffer);
-       pos = buffer;
+       pos = (char*) buffer;
 
        bits = num_bits - sign;
        for (i = 0; i < bits/4; i++)
@@ -1183,33 +985,35 @@ void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
                *pos++ = SC_0;
 }
 
-void sc_truncate(unsigned int num_bits, void *buffer) {
-       char *cbuffer = buffer;
+void sc_truncate(unsigned int num_bits, void *buffer)
+{
+       char *cbuffer = (char*) buffer;
        char *pos = cbuffer + (num_bits / 4);
        char *end = cbuffer + calc_buffer_size;
 
        assert(pos < end);
 
-       switch(num_bits % 4) {
+       switch (num_bits % 4) {
        case 0: /* nothing to do */ break;
-       case 1: *pos = and_table[_val(*pos)][SC_1]; pos++; break;
-       case 2: *pos = and_table[_val(*pos)][SC_3]; pos++; break;
-       case 3: *pos = and_table[_val(*pos)][SC_7]; pos++; break;
+       case 1: *pos++ &= SC_1; break;
+       case 2: *pos++ &= SC_3; break;
+       case 3: *pos++ &= SC_7; break;
        }
 
-       for( ; pos < end; ++pos)
+       for ( ; pos < end; ++pos)
                *pos = SC_0;
 }
 
-int sc_comp(const void* value1, const void* value2) {
+int sc_comp(const void* value1, const void* value2)
+{
        int counter = calc_buffer_size - 1;
        const char *val1 = (const char *)value1;
        const char *val2 = (const char *)value2;
 
        /* compare signs first:
         * the loop below can only compare values of the same sign! */
-       if (_sign(val1) != _sign(val2))
-               return (_sign(val1) == 1)?(1):(-1);
+       if (do_sign(val1) != do_sign(val2))
+               return (do_sign(val1) == 1)?(1):(-1);
 
        /* loop until two digits differ, the values are equal if there
         * are no such two digits */
@@ -1224,7 +1028,8 @@ int sc_comp(const void* value1, const void* value2) {
        return (val1[counter] > val2[counter]) ? (1) : (-1);
 }
 
-int sc_get_highest_set_bit(const void *value) {
+int sc_get_highest_set_bit(const void *value)
+{
        const char *val = (const char*)value;
        int high, counter;
 
@@ -1243,7 +1048,8 @@ int sc_get_highest_set_bit(const void *value) {
        return high;
 }
 
-int sc_get_lowest_set_bit(const void *value) {
+int sc_get_lowest_set_bit(const void *value)
+{
        const char *val = (const char*)value;
        int low, counter;
 
@@ -1276,7 +1082,24 @@ int sc_get_lowest_set_bit(const void *value) {
        return -1;
 }
 
-int sc_is_zero(const void *value) {
+int sc_get_bit_at(const void *value, unsigned pos)
+{
+       const char *val = (const char*) value;
+       unsigned nibble = pos >> 2;
+
+       return (val[nibble] & SHIFT(pos & 3)) != SC_0;
+}
+
+void sc_set_bit_at(void *value, unsigned pos)
+{
+       char *val = (char*) value;
+       unsigned nibble = pos >> 2;
+
+       val[nibble] |= SHIFT(pos & 3);
+}
+
+int sc_is_zero(const void *value)
+{
        const char* val = (const char *)value;
        int counter;
 
@@ -1287,15 +1110,18 @@ int sc_is_zero(const void *value) {
        return 1;
 }
 
-int sc_is_negative(const void *value) {
-       return _sign(value) == -1;
+int sc_is_negative(const void *value)
+{
+       return do_sign((const char*) value) == -1;
 }
 
-int sc_had_carry(void) {
+int sc_had_carry(void)
+{
        return carry_flag;
 }
 
-unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs) {
+unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
+{
        const char *val = (const char *)value;
        int nibble_ofs  = 2 * byte_ofs;
        unsigned char res;
@@ -1309,8 +1135,8 @@ unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs) {
                res |= _val(val[nibble_ofs + 1]) << 4;
 
        /* kick bits outsize */
-       if (len < (int) (8*byte_ofs)) {
-               res &= 0xFF >> (8*byte_ofs - len);
+       if (len - 8 * byte_ofs < 8) {
+               res &= (1 << (len - 8 * byte_ofs)) - 1;
        }
        return res;
 }
@@ -1319,12 +1145,13 @@ unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs) {
  * convert to a string
  * FIXME: Doesn't check buffer bounds
  */
-const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode) {
+const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode)
+{
        static const char big_digits[]   = "0123456789ABCDEF";
        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;
@@ -1333,10 +1160,10 @@ const char *sc_print(const void *value, unsigned bits, enum base_t base, int sig
        char *pos;
        const char *digits = small_digits;
 
-       base_val = alloca(calc_buffer_size);
-       div1_res = alloca(calc_buffer_size);
-       div2_res = alloca(calc_buffer_size);
-       rem_res  = alloca(calc_buffer_size);
+       base_val = (char*) alloca(calc_buffer_size);
+       div1_res = (char*) alloca(calc_buffer_size);
+       div2_res = (char*) alloca(calc_buffer_size);
+       rem_res  = (char*) alloca(calc_buffer_size);
 
        pos = output_buffer + bit_pattern_size;
        *(--pos) = '\0';
@@ -1364,7 +1191,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    = val[counter++] & mask;
                        *(--pos) = digits[_val(x)];
                }
 
@@ -1390,7 +1218,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    = val[counter++] & mask;
 
                        pos -= 4;
                        p = binary_table[_val(x)];
@@ -1415,8 +1244,8 @@ const char *sc_print(const void *value, unsigned bits, enum base_t base, int sig
                sign = 0;
                if (signed_mode && base == SC_DEC) {
                        /* check for negative values */
-                       if (_bit(val, bits - 1)) {
-                               _negate(val, div2_res);
+                       if (do_bit(val, bits - 1)) {
+                               do_negate(val, div2_res);
                                sign = 1;
                                p = div2_res;
                        }
@@ -1429,15 +1258,15 @@ 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] = p[counter] & mask;
                        ++counter;
-
-                       div1_res[counter] = and_table[_val(p[counter])][bits & 3];
                }
 
                m = div1_res;
                n = div2_res;
                for (;;) {
-                       _divmod(m, base_val, n, rem_res);
+                       do_divmod(m, base_val, n, rem_res);
                        t = m;
                        m = n;
                        n = t;
@@ -1455,14 +1284,13 @@ 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;
 }
 
-void init_strcalc(int precision) {
+void init_strcalc(int precision)
+{
        if (calc_buffer == NULL) {
                if (precision <= 0) precision = SC_DEFAULT_PRECISION;
 
@@ -1473,32 +1301,35 @@ 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));
        }
 }
 
 
-void finish_strcalc(void) {
+void finish_strcalc(void)
+{
        free(calc_buffer);   calc_buffer   = NULL;
        free(output_buffer); output_buffer = NULL;
 }
 
-int sc_get_precision(void) {
+int sc_get_precision(void)
+{
        return bit_pattern_size;
 }
 
 
-void sc_add(const void *value1, const void *value2, void *buffer) {
+void sc_add(const void *value1, const void *value2, void *buffer)
+{
        CLEAR_BUFFER(calc_buffer);
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
        DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
 
-       _add(value1, value2, calc_buffer);
+       do_add((const char*) value1, (const char*) value2, (char*) calc_buffer);
 
        DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
 
@@ -1507,14 +1338,15 @@ void sc_add(const void *value1, const void *value2, void *buffer) {
        }
 }
 
-void sc_sub(const void *value1, const void *value2, void *buffer) {
+void sc_sub(const void *value1, const void *value2, void *buffer)
+{
        CLEAR_BUFFER(calc_buffer);
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
        DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
 
-       _sub(value1, value2, calc_buffer);
+       do_sub((const char*) value1, (const char*) value2, calc_buffer);
 
        DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
 
@@ -1523,12 +1355,13 @@ void sc_sub(const void *value1, const void *value2, void *buffer) {
        }
 }
 
-void sc_neg(const void *value1, void *buffer) {
+void sc_neg(const void *value1, void *buffer)
+{
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
 
-       _negate(value1, calc_buffer);
+       do_negate((const char*) value1, calc_buffer);
 
        DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
 
@@ -1537,14 +1370,15 @@ void sc_neg(const void *value1, void *buffer) {
        }
 }
 
-void sc_and(const void *value1, const void *value2, void *buffer) {
+void sc_and(const void *value1, const void *value2, void *buffer)
+{
        CLEAR_BUFFER(calc_buffer);
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
        DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
 
-       _bitand(value1, value2, calc_buffer);
+       do_bitand((const char*) value1, (const char*) value2, calc_buffer);
 
        DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
 
@@ -1553,14 +1387,32 @@ void sc_and(const void *value1, const void *value2, void *buffer) {
        }
 }
 
-void sc_or(const void *value1, const void *value2, void *buffer) {
+void sc_andnot(const void *value1, const void *value2, void *buffer)
+{
+       CLEAR_BUFFER(calc_buffer);
+       carry_flag = 0;
+
+       DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
+       DEBUGPRINTF_COMPUTATION(("~%s -> ", sc_print_hex(value2)));
+
+       do_bitandnot((const char*) value1, (const char*) value2, calc_buffer);
+
+       DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
+
+       if (buffer != NULL && buffer != calc_buffer) {
+               memcpy(buffer, calc_buffer, calc_buffer_size);
+       }
+}
+
+void sc_or(const void *value1, const void *value2, void *buffer)
+{
        CLEAR_BUFFER(calc_buffer);
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
        DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
 
-       _bitor(value1, value2, calc_buffer);
+       do_bitor((const char*) value1, (const char*) value2, calc_buffer);
 
        DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
 
@@ -1569,14 +1421,15 @@ void sc_or(const void *value1, const void *value2, void *buffer) {
        }
 }
 
-void sc_xor(const void *value1, const void *value2, void *buffer) {
+void sc_xor(const void *value1, const void *value2, void *buffer)
+{
        CLEAR_BUFFER(calc_buffer);
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
        DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
 
-       _bitxor(value1, value2, calc_buffer);
+       do_bitxor((const char*) value1, (const char*) value2, calc_buffer);
 
        DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
 
@@ -1585,13 +1438,14 @@ void sc_xor(const void *value1, const void *value2, void *buffer) {
        }
 }
 
-void sc_not(const void *value1, void *buffer) {
+void sc_not(const void *value1, void *buffer)
+{
        CLEAR_BUFFER(calc_buffer);
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
 
-       _bitnot(value1, calc_buffer);
+       do_bitnot((const char*) value1, calc_buffer);
 
        DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
 
@@ -1600,14 +1454,15 @@ void sc_not(const void *value1, void *buffer) {
        }
 }
 
-void sc_mul(const void *value1, const void *value2, void *buffer) {
+void sc_mul(const void *value1, const void *value2, void *buffer)
+{
        CLEAR_BUFFER(calc_buffer);
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
        DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
 
-       _mul(value1, value2, calc_buffer);
+       do_mul((const char*) value1, (const char*) value2, calc_buffer);
 
        DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
 
@@ -1616,9 +1471,10 @@ void sc_mul(const void *value1, const void *value2, void *buffer) {
        }
 }
 
-void sc_div(const void *value1, const void *value2, void *buffer) {
+void sc_div(const void *value1, const void *value2, void *buffer)
+{
        /* temp buffer holding unused result of divmod */
-       char *unused_res = alloca(calc_buffer_size);
+       char *unused_res = (char*) alloca(calc_buffer_size);
 
        CLEAR_BUFFER(calc_buffer);
        carry_flag = 0;
@@ -1626,7 +1482,7 @@ void sc_div(const void *value1, const void *value2, void *buffer) {
        DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
        DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
 
-       _divmod(value1, value2, calc_buffer, unused_res);
+       do_divmod((const char*) value1, (const char*) value2, calc_buffer, unused_res);
 
        DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
 
@@ -1635,9 +1491,10 @@ void sc_div(const void *value1, const void *value2, void *buffer) {
        }
 }
 
-void sc_mod(const void *value1, const void *value2, void *buffer) {
+void sc_mod(const void *value1, const void *value2, void *buffer)
+{
        /* temp buffer holding unused result of divmod */
-       char *unused_res = alloca(calc_buffer_size);
+       char *unused_res = (char*) alloca(calc_buffer_size);
 
        CLEAR_BUFFER(calc_buffer);
        carry_flag = 0;
@@ -1645,7 +1502,7 @@ void sc_mod(const void *value1, const void *value2, void *buffer) {
        DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
        DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
 
-       _divmod(value1, value2, unused_res, calc_buffer);
+       do_divmod((const char*) value1, (const char*) value2, unused_res, calc_buffer);
 
        DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
 
@@ -1654,24 +1511,26 @@ void sc_mod(const void *value1, const void *value2, void *buffer) {
        }
 }
 
-void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer) {
+void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer)
+{
        CLEAR_BUFFER(calc_buffer);
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
        DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
 
-       _divmod(value1, value2, div_buffer, mod_buffer);
+       do_divmod((const char*) value1, (const char*) value2, (char*) div_buffer, (char*) mod_buffer);
 
        DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
 }
 
 
-void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
+void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
+{
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
-       _shl(val1, calc_buffer, shift_cnt, bitsize, sign);
+       do_shl((const char*) val1, calc_buffer, shift_cnt, bitsize, sign);
 
        DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
 
@@ -1680,17 +1539,19 @@ void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buff
        }
 }
 
-void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
+void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
+{
        long offset = sc_val_to_long(val2);
 
        sc_shlI(val1, offset, bitsize, sign, buffer);
 }
 
-void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer) {
+void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
+{
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
-       _shr(val1, calc_buffer, shift_cnt, bitsize, sign, 0);
+       do_shr((const char*) val1, calc_buffer, shift_cnt, bitsize, sign, 0);
 
        DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
 
@@ -1699,19 +1560,21 @@ void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buff
        }
 }
 
-void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
+void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
+{
        long shift_cnt = sc_val_to_long(val2);
 
        sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
 }
 
-void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
+void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
+{
        long offset = sc_val_to_long(val2);
 
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
-       _shr(val1, calc_buffer, offset, bitsize, sign, 1);
+       do_shr((const char*) val1, calc_buffer, offset, bitsize, sign, 1);
 
        DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
 
@@ -1720,13 +1583,14 @@ void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *bu
        }
 }
 
-void sc_rot(const void *val1, const void *val2, int bitsize, int sign, void *buffer) {
+void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
+{
        long offset = sc_val_to_long(val2);
 
        carry_flag = 0;
 
        DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
-       _rot(val1, calc_buffer, offset, bitsize, sign);
+       do_rotl((const char*) val1, calc_buffer, offset, bitsize, sign);
 
        DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
 
@@ -1735,7 +1599,8 @@ void sc_rot(const void *val1, const void *val2, int bitsize, int sign, void *buf
        }
 }
 
-void sc_zero(void *buffer) {
+void sc_zero(void *buffer)
+{
        if (buffer == NULL)
                buffer = calc_buffer;
        CLEAR_BUFFER(buffer);