/*
- * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
+ * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
*
* This file is part of libFirm.
*
/**
* 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++)
/**
* 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++)
/**
* 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++)
/**
* 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++)
* @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;
/**
* 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) {
/**
* 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);
}
/**
* @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;
/**
* Implements a binary SUB
*/
-static void _sub(const char *val1, const char *val2, char *buffer) {
+static void do_sub(const char *val1, const char *val2, char *buffer) {
char *temp_buffer = 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 */
neg_val1 = alloca(calc_buffer_size);
neg_val2 = 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;
}
}
if (sign)
- _negate(temp_buffer, buffer);
+ do_negate(temp_buffer, buffer);
else
memcpy(buffer, temp_buffer, calc_buffer_size);
}
/**
* 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--) {
*
* 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;
char *neg_val2;
char div_sign = 0; /* remember division result sign */
- char rem_sign = 0; /* remember remainder esult sign */
+ char rem_sign = 0; /* remember remainder result sign */
int c_dividend; /* loop counters */
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;
}
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:
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);
}
/**
*
* @todo Assertions seems to be wrong
*/
-static void _shl(const char *val1, char *buffer, long offset, int radius, 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;
int counter;
int bitoffset = 0;
- assert((offset >= 0) || (0 && "negative leftshift"));
- assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
- assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
- assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
+ assert((shift_cnt >= 0) || (0 && "negative leftshift"));
+ 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 (offset >= radius) {
+ if (shift_cnt >= bitsize) {
memset(buffer, SC_0, calc_buffer_size);
return;
}
- shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
- offset = offset / 4;
+ shift = shift_table[_val(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 < radius/4 - offset; counter++) {
+ for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
shl = mul_table[_val(val1[counter])][_val(shift)];
- buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
+ buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
carry = shl[1];
}
- if (radius%4 > 0) {
+ if (bitsize%4 > 0) {
shl = mul_table[_val(val1[counter])][_val(shift)];
- buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
+ buffer[counter + shift_cnt] = or_table[_val(shl[0])][_val(carry)];
bitoffset = counter;
} else {
bitoffset = counter - 1;
}
/* fill with zeroes */
- for (counter = 0; counter < offset; counter++)
+ for (counter = 0; counter < shift_cnt; counter++)
buffer[counter] = SC_0;
/* if the mode was signed, change sign when the mode's msb is now 1 */
- offset = bitoffset + offset;
- bitoffset = (radius-1) % 4;
- if (is_signed && _bitisset(buffer[offset], bitoffset)) {
+ shift_cnt = bitoffset + shift_cnt;
+ bitoffset = (bitsize-1) % 4;
+ if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
/* this sets the upper bits of the leftmost digit */
- buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
- for (counter = offset+1; counter < calc_buffer_size; counter++) {
+ buffer[shift_cnt] = or_table[_val(buffer[shift_cnt])][_val(min_digit[bitoffset])];
+ for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
buffer[counter] = SC_F;
}
- } else if (is_signed && !_bitisset(buffer[offset], bitoffset)) {
+ } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
/* this clears the upper bits of the leftmost digit */
- buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
- for (counter = offset+1; counter < calc_buffer_size; counter++) {
+ buffer[shift_cnt] = and_table[_val(buffer[shift_cnt])][_val(max_digit[bitoffset])];
+ for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
buffer[counter] = SC_0;
}
}
* Implements a Shift Right, which can either preserve the sign bit
* or not.
*
+ * @param bitsize bitsize of the value to be shifted
+ *
* @todo Assertions seems to be wrong
*/
-static void _shr(const char *val1, char *buffer, long offset, int radius, 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;
- int shift;
+ int shift_mod, shift_nib;
int counter;
int bitoffset = 0;
- assert((offset >= 0) || (0 && "negative rightshift"));
- assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
- assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
+ assert((shift_cnt >= 0) || (0 && "negative rightshift"));
+ 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) && (_sign(val1) == -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 (offset >= radius) {
+ if (shift_cnt >= bitsize) {
if (!sc_is_zero(val1)) {
carry_flag = 1;
}
return;
}
- shift = offset % 4;
- offset = offset / 4;
+ shift_mod = shift_cnt & 3;
+ shift_nib = shift_cnt >> 2;
/* check if any bits are lost, and set carry_flag if so */
- for (counter = 0; counter < offset; counter++) {
+ for (counter = 0; counter < shift_nib; ++counter) {
if (val1[counter] != 0) {
carry_flag = 1;
break;
}
}
- if ((_val(val1[counter]) & ((1<<shift)-1)) != 0) {
+ if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
carry_flag = 1;
- }
+
/* shift digits to the right with offset, carry and all */
counter = 0;
- if (radius/4 - offset > 0) {
- buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
+ if ((bitsize >> 2) > shift_nib) {
+ buffer[counter] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
counter = 1;
}
- for (; counter < radius/4 - offset; counter++) {
- shrs = shrs_table[_val(val1[counter + offset])][shift];
+ for (; counter < bitsize/4 - 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])];
}
/* the last digit is special in regard of signed/unsigned shift */
- bitoffset = radius%4;
- msd = (radius/4<calc_buffer_size)?(val1[radius/4]):(sign); /* most significant digit */
+ bitoffset = bitsize & 3;
+ msd = sign; /* most significant digit */
/* 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])];
}
- shrs = shrs_table[_val(msd)][shift];
+ 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 && is_signed && (_sign(val1) == -1)) {
+ if (signed_shift && sign == SC_F) {
buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
} else {
buffer[counter] = shrs[0];
}
- if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
+ if (counter > 0)
+ buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
/* fill with SC_F or SC_0 depending on sign */
for (counter++; counter < calc_buffer_size; counter++) {
}
/**
- * 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);
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 */
}
/**
* Do sign extension if the mode is signed, otherwise to zero extension.
*/
-void sign_extend(char *calc_buffer, ir_mode *mode) {
- int bits = get_mode_size_bits(mode) - 1;
- int nibble = bits >> 2;
- int max = max_digit[bits & 3];
+void sign_extend(void *buffer, ir_mode *mode) {
+ char *calc_buffer = buffer;
+ int bits = get_mode_size_bits(mode) - 1;
+ int nibble = bits >> 2;
+ int max = max_digit[bits & 3];
int i;
if (mode_is_signed(mode)) {
/* 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 */
+ do_mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
+ do_add(val, calc_buffer, calc_buffer); /* add next digit to current value */
/* get ready for the next letter */
str++;
} /* while (len > 0 ) */
if (sign)
- _negate(calc_buffer, calc_buffer);
+ do_negate(calc_buffer, calc_buffer);
/* beware: even if hex numbers have no sign, we need sign extension here */
sign_extend(calc_buffer, mode);
if (sign) {
if (is_minlong)
- _inc(buffer, buffer);
+ do_inc(buffer, buffer);
- _negate(buffer, buffer);
+ do_negate(buffer, buffer);
}
}
}
void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer) {
- char* pos;
+ char* pos;
int i, bits;
if (buffer == NULL) buffer = calc_buffer;
*pos++ = SC_0;
}
-void sc_calc(const void* value1, const void* value2, unsigned op, void *buffer) {
- char *unused_res; /* temp buffer holding unused result of divmod */
-
- const char *val1 = (const char *)value1;
- const char *val2 = (const char *)value2;
-
- unused_res = alloca(calc_buffer_size);
-
- CLEAR_BUFFER(calc_buffer);
- carry_flag = 0;
+void sc_truncate(unsigned int num_bits, void *buffer) {
+ char *cbuffer = buffer;
+ char *pos = cbuffer + (num_bits / 4);
+ char *end = cbuffer + calc_buffer_size;
- DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
+ assert(pos < end);
- switch (op) {
- case SC_NEG:
- _negate(val1, calc_buffer);
- DEBUGPRINTF_COMPUTATION(("negated: %s\n", sc_print_hex(calc_buffer)));
- break;
- case SC_OR:
- DEBUGPRINTF_COMPUTATION(("| "));
- _bitor(val1, val2, calc_buffer);
- break;
- case SC_AND:
- DEBUGPRINTF_COMPUTATION(("& "));
- _bitand(val1, val2, calc_buffer);
- break;
- case SC_XOR:
- DEBUGPRINTF_COMPUTATION(("^ "));
- _bitxor(val1, val2, calc_buffer);
- break;
- case SC_NOT:
- _bitnot(val1, calc_buffer);
- DEBUGPRINTF_COMPUTATION(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
- break;
- case SC_ADD:
- DEBUGPRINTF_COMPUTATION(("+ "));
- _add(val1, val2, calc_buffer);
- break;
- case SC_SUB:
- DEBUGPRINTF_COMPUTATION(("- "));
- _sub(val1, val2, calc_buffer);
- break;
- case SC_MUL:
- DEBUGPRINTF_COMPUTATION(("* "));
- _mul(val1, val2, calc_buffer);
- break;
- case SC_DIV:
- DEBUGPRINTF_COMPUTATION(("/ "));
- _divmod(val1, val2, calc_buffer, unused_res);
- break;
- case SC_MOD:
- DEBUGPRINTF_COMPUTATION(("%% "));
- _divmod(val1, val2, unused_res, calc_buffer);
- break;
- default:
- assert(0);
+ 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;
}
- DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
- DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
- if ((buffer != NULL) && (buffer != calc_buffer)) {
- memcpy(buffer, calc_buffer, calc_buffer_size);
- }
-}
-
-void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op, void* buffer) {
- const char *val1 = (const char *)value1;
- const char *val2 = (const char *)value2;
- long offset;
-
- carry_flag = 0;
- offset = sc_val_to_long(val2);
-
- DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
- switch (op) {
- case SC_SHL:
- DEBUGPRINTF_COMPUTATION(("<< %ld ", offset));
- _shl(val1, calc_buffer, offset, radius, sign);
- break;
- case SC_SHR:
- DEBUGPRINTF_COMPUTATION((">> %ld ", offset));
- _shr(val1, calc_buffer, offset, radius, sign, 0);
- break;
- case SC_SHRS:
- DEBUGPRINTF_COMPUTATION((">>> %ld ", offset));
- _shr(val1, calc_buffer, offset, radius, sign, 1);
- break;
- case SC_ROT:
- DEBUGPRINTF_COMPUTATION(("<<>> %ld ", offset));
- _rot(val1, calc_buffer, offset, radius, sign);
- break;
- default:
- assert(0);
- }
- DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
-
- if ((buffer != NULL) && (buffer != calc_buffer)) {
- memmove(buffer, calc_buffer, calc_buffer_size);
- }
+ for( ; pos < end; ++pos)
+ *pos = SC_0;
}
int sc_comp(const void* value1, const void* 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 */
int sc_get_lowest_set_bit(const void *value) {
const char *val = (const char*)value;
int low, counter;
- char sign;
- sign = (_sign(val)==1)?(SC_0):(SC_F);
low = 0;
-
for (counter = 0; counter < calc_buffer_size; counter++) {
- if (val[counter] == SC_0)
+ switch (val[counter]) {
+ case SC_1:
+ case SC_3:
+ case SC_5:
+ case SC_7:
+ case SC_9:
+ case SC_B:
+ case SC_D:
+ case SC_F:
+ return low;
+ case SC_2:
+ case SC_6:
+ case SC_A:
+ case SC_E:
+ return low + 1;
+ case SC_4:
+ case SC_C:
+ return low + 2;
+ case SC_8:
+ return low + 3;
+ default:
low += 4;
- else {
- if (val[counter] < SC_2) return low;
- else if (val[counter] < SC_4) return low + 1;
- else if (val[counter] < SC_8) return low + 2;
- else return low + 3;
}
}
return -1;
const char* val = (const char *)value;
int counter;
- for (counter = 0; counter < calc_buffer_size; counter++) {
- if (val[counter] != SC_0) return 0;
+ for (counter = 0; counter < calc_buffer_size; ++counter) {
+ if (val[counter] != SC_0)
+ return 0;
}
return 1;
}
int sc_is_negative(const void *value) {
- return _sign(value) == -1;
+ return do_sign(value) == -1;
}
int sc_had_carry(void) {
unsigned char res;
/* the current scheme uses one byte to store a nibble */
- if (nibble_ofs >= len)
+ if (4 * nibble_ofs >= len)
return 0;
res = _val(val[nibble_ofs]);
- if (len > nibble_ofs + 1)
+ if (len > 4 * (nibble_ofs + 1))
res |= _val(val[nibble_ofs + 1]) << 4;
+ /* kick bits outsize */
+ if (len - 8 * byte_ofs < 8) {
+ res &= (1 << (len - 8 * byte_ofs)) - 1;
+ }
return res;
}
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;
}
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;
int sc_get_precision(void) {
return bit_pattern_size;
}
+
+
+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)));
+
+ do_add(value1, 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_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)));
+
+ do_sub(value1, 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_neg(const void *value1, void *buffer) {
+ carry_flag = 0;
+
+ DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
+
+ do_negate(value1, 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_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)));
+
+ do_bitand(value1, 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)));
+
+ do_bitor(value1, 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_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)));
+
+ do_bitxor(value1, 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_not(const void *value1, void *buffer) {
+ CLEAR_BUFFER(calc_buffer);
+ carry_flag = 0;
+
+ DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
+
+ do_bitnot(value1, 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_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)));
+
+ do_mul(value1, 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_div(const void *value1, const void *value2, void *buffer) {
+ /* temp buffer holding unused result of divmod */
+ char *unused_res = alloca(calc_buffer_size);
+
+ CLEAR_BUFFER(calc_buffer);
+ carry_flag = 0;
+
+ DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
+ DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
+
+ do_divmod(value1, value2, calc_buffer, unused_res);
+
+ DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
+
+ if ((buffer != NULL) && (buffer != calc_buffer)) {
+ memcpy(buffer, calc_buffer, calc_buffer_size);
+ }
+}
+
+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);
+
+ CLEAR_BUFFER(calc_buffer);
+ carry_flag = 0;
+
+ DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
+ DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
+
+ do_divmod(value1, value2, unused_res, 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_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)));
+
+ do_divmod(value1, value2, div_buffer, 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) {
+ carry_flag = 0;
+
+ DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
+ do_shl(val1, calc_buffer, shift_cnt, bitsize, sign);
+
+ DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
+
+ if ((buffer != NULL) && (buffer != calc_buffer)) {
+ memmove(buffer, calc_buffer, calc_buffer_size);
+ }
+}
+
+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) {
+ carry_flag = 0;
+
+ DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
+ do_shr(val1, calc_buffer, shift_cnt, bitsize, sign, 0);
+
+ DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
+
+ if ((buffer != NULL) && (buffer != calc_buffer)) {
+ memmove(buffer, calc_buffer, calc_buffer_size);
+ }
+}
+
+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) {
+ long offset = sc_val_to_long(val2);
+
+ carry_flag = 0;
+
+ DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
+ do_shr(val1, calc_buffer, offset, bitsize, sign, 1);
+
+ DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
+
+ if ((buffer != NULL) && (buffer != calc_buffer)) {
+ memmove(buffer, calc_buffer, calc_buffer_size);
+ }
+}
+
+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));
+ do_rotl(val1, calc_buffer, offset, bitsize, sign);
+
+ DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
+
+ if ((buffer != NULL) && (buffer != calc_buffer)) {
+ memmove(buffer, calc_buffer, calc_buffer_size);
+ }
+}
+
+void sc_zero(void *buffer) {
+ if (buffer == NULL)
+ buffer = calc_buffer;
+ CLEAR_BUFFER(buffer);
+ carry_flag = 0;
+}