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

index 3e410dc..2f53fcf 100644 (file)
@@ -1119,51 +1119,49 @@ fp_value *fc_get_minusinf(const float_descriptor_t *desc, fp_value *result)
        return result;
 }
 
-int fc_comp(const fp_value *val_a, const fp_value *val_b)
+ir_relation fc_comp(fp_value const *const val_a, fp_value const *const val_b)
 {
-       int mul = 1;
-
        /*
         * shortcut: if both values are identical, they are either
         * Unordered if NaN or equal
         */
        if (val_a == val_b)
-               return val_a->clss == FC_NAN ? 2 : 0;
+               return val_a->clss == FC_NAN ? ir_relation_unordered : ir_relation_equal;
 
        /* unordered if one is a NaN */
        if (val_a->clss == FC_NAN || val_b->clss == FC_NAN)
-               return 2;
+               return ir_relation_unordered;
 
        /* zero is equal independent of sign */
        if ((val_a->clss == FC_ZERO) && (val_b->clss == FC_ZERO))
-               return 0;
+               return ir_relation_equal;
 
        /* different signs make compare easy */
        if (val_a->sign != val_b->sign)
-               return (val_a->sign == 0) ? (1) : (-1);
+               return val_a->sign == 0 ? ir_relation_greater : ir_relation_less;
 
-       mul = val_a->sign ? -1 : 1;
+       ir_relation const mul = val_a->sign ? ir_relation_less_greater : ir_relation_false;
 
        /* both infinity means equality */
        if ((val_a->clss == FC_INF) && (val_b->clss == FC_INF))
-               return 0;
+               return ir_relation_equal;
 
        /* infinity is bigger than the rest */
        if (val_a->clss == FC_INF)
-               return  1 * mul;
+               return ir_relation_greater ^ mul;
        if (val_b->clss == FC_INF)
-               return -1 * mul;
+               return ir_relation_less ^ mul;
 
        /* check first exponent, that mantissa if equal */
-       switch (sc_comp(_exp(val_a), _exp(val_b))) {
-       case -1:
-               return -1 * mul;
-       case  1:
-               return  1 * mul;
-       case  0:
-               return sc_comp(_mant(val_a), _mant(val_b)) * mul;
-       default:
-               return 2;
+       int rel = sc_comp(_exp(val_a), _exp(val_b));
+       if (rel == 0)
+               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");
        }
 }
 
index c41257e..e0fb9fd 100644 (file)
@@ -165,13 +165,10 @@ char *fc_print(const fp_value *a, char *buf, int buflen, unsigned base);
  *
  * @param a Value No. 1
  * @param b Value No. 2
- * @result The returned value will be one of
- *          -1  if a < b
- *           0  if a == b
- *           1  if a > b
- *           2  if either value is NaN
+ * @result The relation between a and b; either less, equal, greater or
+ *         unordered.
  */
-int fc_comp(const fp_value *a, const fp_value *b);
+ir_relation fc_comp(const fp_value *a, const fp_value *b);
 
 /**
  * Converts an floating point value into an integer value.
index 7bb4a87..b20d893 100644 (file)
@@ -712,13 +712,8 @@ ir_relation tarval_cmp(ir_tarval *a, ir_tarval *b)
                 * BEWARE: we cannot compare a == b here, because
                 * a NaN is always Unordered to any other value, even to itself!
                 */
-               switch (fc_comp((const fp_value*) a->value, (const fp_value*) b->value)) {
-               case -1: return ir_relation_less;
-               case  0: return ir_relation_equal;
-               case  1: return ir_relation_greater;
-               case  2: return ir_relation_unordered;
-               default: return ir_relation_false;
-               }
+               return fc_comp((fp_value const*)a->value, (fp_value const*)b->value);
+
        case irms_reference:
        case irms_int_number:
                if (a == b)