* @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
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 :( */
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 */
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
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
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
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;
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;
/* 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)];
}
/* 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)];
/* 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;
break;
default:
- printf("%i\n", base);
- assert(0);
- return NULL;
+ panic("Unsupported base %d", base);
}
return pos;
}
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));
}