tv: Let sc_comp() return an ir_relation instead of magic numbers.
authorChristoph Mallon <christoph.mallon@gmx.de>
Sat, 8 Dec 2012 21:50:21 +0000 (22:50 +0100)
committerChristoph Mallon <christoph.mallon@gmx.de>
Mon, 10 Dec 2012 09:52:06 +0000 (10:52 +0100)
ir/tv/fltcalc.c
ir/tv/strcalc.c
ir/tv/strcalc.h
ir/tv/tv.c

index 2f53fcf..17c3585 100644 (file)
@@ -331,7 +331,7 @@ static int normalize(const fp_value *in_val, fp_value *out_val, int sticky)
 
        /* check for exponent overflow */
        sc_val_from_ulong((1 << out_val->desc.exponent_size) - 1, temp);
-       if (sc_comp(_exp(out_val), temp) != -1) {
+       if (sc_comp(_exp(out_val), temp) != ir_relation_less) {
                /* exponent overflow, reaction depends on rounding method:
                 *
                 * mode        | sign of value |  result
@@ -437,19 +437,17 @@ static void _fadd(const fp_value *a, const fp_value *b, fp_value *result)
         * (+/- 0 ?) */
        if (sign && sc_val_to_long(exp_diff) == 0) {
                switch (sc_comp(_mant(a), _mant(b))) {
-               case 1:  /* a > b */
+               case ir_relation_greater:  /* a > b */
                        res_sign = a->sign;  /* abs(a) is bigger and a is negative */
                        break;
-               case 0:  /* a == b */
+               case ir_relation_equal:  /* a == b */
                        res_sign = (rounding_mode == FC_TONEGATIVE);
                        break;
-               case -1: /* a < b */
+               case ir_relation_less: /* a < b */
                        res_sign = b->sign; /* abs(b) is bigger and b is negative */
                        break;
                default:
-                       /* can't be reached */
-                       res_sign = 0;
-                       break;
+                       panic("invalid comparison result");
                }
        }
        else
@@ -494,7 +492,7 @@ static void _fadd(const fp_value *a, const fp_value *b, fp_value *result)
        }
 
        if (sign) {
-               if (sc_comp(_mant(a), temp) == -1)
+               if (sc_comp(_mant(a), temp) == ir_relation_less)
                        sc_sub(temp, _mant(a), _mant(result));
                else
                        sc_sub(_mant(a), temp, _mant(result));
@@ -1153,16 +1151,12 @@ ir_relation fc_comp(fp_value const *const val_a, fp_value const *const val_b)
                return ir_relation_less ^ mul;
 
        /* check first exponent, that mantissa if equal */
-       int rel = sc_comp(_exp(val_a), _exp(val_b));
-       if (rel == 0)
+       ir_relation rel = sc_comp(_exp(val_a), _exp(val_b));
+       if (rel == ir_relation_equal)
                rel = sc_comp(_mant(val_a), _mant(val_b));
-
-       switch (rel) {
-       case -1: return ir_relation_less    ^ mul;
-       case  0: return ir_relation_equal;
-       case  1: return ir_relation_greater ^ mul;
-       default: panic("invalid comparison result");
-       }
+       if (rel != ir_relation_equal)
+               rel ^= mul;
+       return rel;
 }
 
 int fc_is_zero(const fp_value *a)
@@ -1343,7 +1337,7 @@ fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result)
        if (result == NULL) result = calc_buffer;
 
        /* make the value with the bigger exponent the first one */
-       if (sc_comp(_exp(a), _exp(b)) == -1)
+       if (sc_comp(_exp(a), _exp(b)) == ir_relation_less)
                _fadd(b, a, result);
        else
                _fadd(a, b, result);
@@ -1360,7 +1354,7 @@ fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result)
        temp = (fp_value*) alloca(calc_buffer_size);
        memcpy(temp, b, calc_buffer_size);
        temp->sign = !b->sign;
-       if (sc_comp(_exp(a), _exp(temp)) == -1)
+       if (sc_comp(_exp(a), _exp(temp)) == ir_relation_less)
                _fadd(temp, a, result);
        else
                _fadd(a, temp, result);
index ef875fc..08f38a5 100644 (file)
@@ -341,10 +341,10 @@ static void do_divmod(const char *rDividend, const char *divisor, char *quot, ch
        memset(rem, SC_0, calc_buffer_size);
 
        /* if the divisor is zero this won't work (quot is zero) */
-       if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
+       assert(sc_comp(divisor, quot) != ir_relation_equal && "division by zero!");
 
        /* if the dividend is zero result is zero (quot is zero) */
-       if (sc_comp(dividend, quot) == 0)
+       if (sc_comp(dividend, quot) == ir_relation_equal)
                return;
 
        if (do_sign(dividend) == -1) {
@@ -365,11 +365,11 @@ static void do_divmod(const char *rDividend, const char *divisor, char *quot, ch
        /* if divisor >= dividend division is easy
         * (remember these are absolute values) */
        switch (sc_comp(dividend, divisor)) {
-       case 0: /* dividend == divisor */
+       case ir_relation_equal: /* dividend == divisor */
                quot[0] = SC_1;
                goto end;
 
-       case -1: /* dividend < divisor */
+       case ir_relation_less: /* dividend < divisor */
                memcpy(rem, dividend, calc_buffer_size);
                goto end;
 
@@ -381,7 +381,7 @@ static void do_divmod(const char *rDividend, const char *divisor, char *quot, ch
                do_push(dividend[c_dividend], rem);
                do_push(SC_0, quot);
 
-               if (sc_comp(rem, divisor) != -1) {  /* remainder >= divisor */
+               if (sc_comp(rem, divisor) != ir_relation_less) {  /* remainder >= divisor */
                        /* subtract until the remainder becomes negative, this should
                         * be faster than comparing remainder with divisor  */
                        do_add(rem, minus_divisor, rem);
@@ -811,7 +811,7 @@ void sc_truncate(unsigned int num_bits, void *buffer)
                *pos = SC_0;
 }
 
-int sc_comp(const void* value1, const void* value2)
+ir_relation sc_comp(void const* const value1, void const* const value2)
 {
        int counter = calc_buffer_size - 1;
        const char *val1 = (const char *)value1;
@@ -820,19 +820,19 @@ int sc_comp(const void* value1, const void* value2)
        /* compare signs first:
         * the loop below can only compare values of the same sign! */
        if (do_sign(val1) != do_sign(val2))
-               return (do_sign(val1) == 1)?(1):(-1);
+               return do_sign(val1) == 1 ? ir_relation_greater : ir_relation_less;
 
        /* loop until two digits differ, the values are equal if there
         * are no such two digits */
        while (val1[counter] == val2[counter]) {
                counter--;
-               if (counter < 0) return 0;
+               if (counter < 0) return ir_relation_equal;
        }
 
        /* the leftmost digit is the most significant, so this returns
         * the correct result.
         * This implies the digit enum is ordered */
-       return (val1[counter] > val2[counter]) ? (1) : (-1);
+       return val1[counter] > val2[counter] ? ir_relation_greater : ir_relation_less;
 }
 
 int sc_get_highest_set_bit(const void *value)
index 8d74b81..e470c92 100644 (file)
@@ -219,7 +219,7 @@ void sc_truncate(unsigned num_bits, void *buffer);
 /**
  * Compares val1 and val2
  */
-int  sc_comp(const void *val1, const void *val2);
+ir_relation sc_comp(void const *val1, void const *val2);
 
 int sc_get_highest_set_bit(const void *value);
 int sc_get_lowest_set_bit(const void *value);
index b20d893..4245b99 100644 (file)
@@ -201,7 +201,7 @@ static ir_tarval *get_tarval_overflow(const void *value, size_t length, ir_mode
                return get_tarval(temp, length, mode);
 
        case irms_int_number:
-               if (sc_comp(value, get_mode_max(mode)->value) == 1) {
+               if (sc_comp(value, get_mode_max(mode)->value) == ir_relation_greater) {
                        switch (tarval_get_integer_overflow_mode()) {
                        case TV_OVERFLOW_SATURATE:
                                return get_mode_max(mode);
@@ -218,7 +218,7 @@ static ir_tarval *get_tarval_overflow(const void *value, size_t length, ir_mode
                                return get_tarval(value, length, mode);
                        }
                }
-               if (sc_comp(value, get_mode_min(mode)->value) == -1) {
+               if (sc_comp(value, get_mode_min(mode)->value) == ir_relation_less) {
                        switch (tarval_get_integer_overflow_mode()) {
                        case TV_OVERFLOW_SATURATE:
                                return get_mode_min(mode);
@@ -399,7 +399,7 @@ int tarval_is_long(ir_tarval *tv)
        if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
                /* the value might be too big to fit in a long */
                sc_max_from_bits(sizeof(long) << 3, 0, NULL);
-               if (sc_comp(sc_get_buffer(), tv->value) == -1) {
+               if (sc_comp(sc_get_buffer(), tv->value) == ir_relation_less) {
                        /* really doesn't fit */
                        return 0;
                }
@@ -655,7 +655,7 @@ int tarval_is_negative(ir_tarval *a)
        case irms_int_number:
                if (!mode_is_signed(a->mode)) return 0;
                else
-                       return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
+                       return sc_comp(a->value, get_mode_null(a->mode)->value) == ir_relation_less ? 1 : 0;
 
        case irms_float_number:
                return fc_is_negative((const fp_value*) a->value);
@@ -718,7 +718,7 @@ ir_relation tarval_cmp(ir_tarval *a, ir_tarval *b)
        case irms_int_number:
                if (a == b)
                        return ir_relation_equal;
-               return sc_comp(a->value, b->value) == 1 ? ir_relation_greater : ir_relation_less;
+               return sc_comp(a->value, b->value);
 
        case irms_internal_boolean:
                if (a == b)
@@ -1001,7 +1001,7 @@ ir_tarval *tarval_abs(ir_tarval *a)
 
        switch (get_mode_sort(a->mode)) {
        case irms_int_number:
-               if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
+               if (sc_comp(a->value, get_mode_null(a->mode)->value) == ir_relation_less) {
                        buffer = (char*) alloca(sc_get_buffer_length());
                        sc_neg(a->value, buffer);
                        return get_tarval_overflow(buffer, a->length, a->mode);