* This validates if get_tarval_long() will return a satisfying
* result. I.e. if tv is an int_number and between min, max
* of long int (signed!)
+ *
+ * @param tv the tarval
*/
int tarval_is_long(tarval *tv);
* stored value.
* This will overflow silently, so use only if you know what
* you are doing! (better check with tarval_is_long...)
+ *
+ * @param tv the tarval
*/
long double get_tarval_double(tarval *tv);
* This validates if tarval_to_double() will return a satisfying
* result. I.e. if tv is an float_number and between min, max
* of double
+ *
+ * @param tv the tarval
*/
int tarval_is_double(tarval *tv);
* the struct tarval
*/
-/** Returns the mode of the tarval. */
-ir_mode *get_tarval_mode (const tarval *tv);
+/**
+ * Returns the mode of the tarval.
+ *
+ * @param tv the tarval
+ */
+ir_mode *get_tarval_mode(const tarval *tv);
/** Returns the contents of the 'link' field of the tarval */
/* void *get_tarval_link (tarval*); */
/**
* Returns 1 if tv is negative
*
- * @param a the tarval
+ * @param tv the tarval
*/
-int tarval_is_negative(tarval *a);
+int tarval_is_negative(tarval *tv);
/**
* Returns 1 if tv is null
*
- * @param a the tarval
+ * @param tv the tarval
*/
-int tarval_is_null(tarval *a);
+int tarval_is_null(tarval *tv);
/**
* Returns 1 if tv is the "one"
*
- * @param a the tarval
+ * @param tv the tarval
*/
-int tarval_is_one(tarval *a);
+int tarval_is_one(tarval *tv);
/**
* Returns 1 if tv is the "minus one"
*
- * @param a the tarval
+ * @param tv the tarval
*/
-int tarval_is_minus_one(tarval *a);
+int tarval_is_minus_one(tarval *tv);
/*
* returns non-zero if all bits in the tarval are set
/* ******************** Arithmetic operations on tarvals ******************** */
typedef enum _tarval_int_overflow_mode_t {
- TV_OVERFLOW_BAD, /**< tarval module will return tarval_bad if a overflow occurs */
- TV_OVERFLOW_WRAP, /**< tarval module will overflow will be ignored, wrap around occurs */
- TV_OVERFLOW_SATURATE /**< tarval module will saturate the overflow */
+ TV_OVERFLOW_BAD, /**< tarval module will return tarval_bad if a overflow occurs */
+ TV_OVERFLOW_WRAP, /**< tarval module will overflow will be ignored, wrap around occurs */
+ TV_OVERFLOW_SATURATE /**< tarval module will saturate the overflow */
} tarval_int_overflow_mode_t;
/**
* between a and b. This is either pn_Cmp_Uo, pn_Cmp_Lt, pn_Cmp_Eq, pn_Cmp_Gt,
* or pn_Cmp_False if a or b are symbolic pointers which can not be compared at all.
*
- * @param a A tarval to be compared
- * @param b A tarval to be compared
+ * @param a the first tarval to be compared
+ * @param b the second tarval to be compared
*
* @return
* The pn_Cmp best describing the relation between a and b is returned.
* constructed and returned
*
* @note
- * Illegal conversations will trigger an assertion
+ * Illegal convertions will trigger a panic
*
* @sa
* FIRM documentation for conversion rules
* The sort member of the struct mode defines which operations are valid
*/
-/** bitwise Negation of a tarval. */
+/** Bitwise Negation of a tarval. */
tarval *tarval_not(tarval *a);
-/** arithmetic Negation of a tarval. */
+/** Arithmetic Negation of a tarval. */
tarval *tarval_neg(tarval *a);
/** Addition of two tarvals. */
/** Multiplication of tarvals. */
tarval *tarval_mul(tarval *a, tarval *b);
-/** 'Exact' division. */
+/** 'Exact' division of two tarvals. */
tarval *tarval_quo(tarval *a, tarval *b);
-/** Integer division. */
+/** Integer division of two tarvals. */
tarval *tarval_div(tarval *a, tarval *b);
/** Remainder of integer division. */
/** Integer division AND remainder. */
tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod_res);
-/** Absolute value. */
+/** Absolute value of a tarval. */
tarval *tarval_abs(tarval *a);
/** Bitwise and. */
/** Rotation. */
tarval *tarval_rot(tarval *a, tarval *b);
-/** Carry flag of the last operation */
+/**
+ * Returns the carry flag of the last operation.
+ */
int tarval_carry(void);
/* *********** Output of tarvals *********** */
* However, we can do this in the tarval much simpler...
*/
typedef enum {
- TVO_NATIVE, /**< the default output mode, depends on the mode */
- TVO_HEX, /**< use hex representation, always possible */
- TVO_DECIMAL, /**< use decimal representation */
- TVO_OCTAL, /**< use octal representation */
- TVO_BINARY, /**< use binary representation */
- TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
- TVO_HEXFLOAT /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
+ TVO_NATIVE, /**< the default output mode, depends on the mode */
+ TVO_HEX, /**< use hex representation, always possible */
+ TVO_DECIMAL, /**< use decimal representation */
+ TVO_OCTAL, /**< use octal representation */
+ TVO_BINARY, /**< use binary representation */
+ TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
+ TVO_HEXFLOAT /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
} tv_output_mode;
/**
* of a tarval of a mode.
*/
typedef struct tarval_mode_info {
- tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
- const char *mode_prefix; /**< if set, this prefix will be printed
- before a value of this mode */
- const char *mode_suffix; /**< if set, this suffix will be printed
- after a value of this mode */
+ tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
+ const char *mode_prefix; /**< if set, this prefix will be printed
+ before a value of this mode */
+ const char *mode_suffix; /**< if set, this suffix will be printed
+ after a value of this mode */
} tarval_mode_info;
/**
/**
* Returns non-zero if a given (integer) tarval has only one single bit
* set.
+ *
+ * @param tv the tarval
*/
int tarval_is_single_bit(tarval *tv);
/**
- * Output of tarvals to a buffer.
+ * Output a tarval to a string buffer.
+ *
+ * @param buf the output buffer
+ * @param buflen the length of the buffer
+ * @param tv the tarval
*/
int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
/**
- * Output of tarvals to stdio.
+ * Output a tarval to stdio.
+ *
+ * @param tv the tarval
*/
int tarval_printf(tarval *tv);
/**
* Returns non-zero if the mantissa of a floating point IEEE-754
* tarval is zero (i.e. 1.0Exxx)
+ *
+ * @param tv the tarval
*/
int tarval_ieee754_zero_mantissa(tarval *tv);
/**
* Returns the exponent of a floating point IEEE-754
* tarval.
+ *
+ * @param tv the tarval
*/
int tarval_ieee754_get_exponent(tarval *tv);
+/**
+ * Check if the tarval can be converted to the given mode without
+ * precision loss.
+ *
+ * @param tv the tarval
+ * param mode the mode to convert to
+ */
+int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode);
+
/**
* Set the immediate precision for IEEE-754 results. Set this to
* 0 to get the same precision as the operands.
/**
* Check if its the a floating point NaN.
+ *
+ * @param tv the tarval
*/
int tarval_is_NaN(tarval *tv);
/**
* Check if its the a floating point +inf.
+ *
+ * @param tv the tarval
*/
int tarval_is_plus_inf(tarval *tv);
/**
* Check if its the a floating point -inf.
+ *
+ * @param tv the tarval
*/
int tarval_is_minus_inf(tarval *tv);
/**
* Check if the tarval represents a finite value, ie neither NaN nor inf.
+ *
+ * @param tv the tarval
*/
int tarval_is_finite(tarval *tv);
#include "xmalloc.h"
+/** The number of extra precesion rounding bits */
+#define ROUNDING_BITS 2
+
typedef uint32_t UINT32;
#ifdef HAVE_LONG_DOUBLE
sc_or(temp, packed, packed);
/* extract mantissa */
- /* remove 2 rounding bits */
- sc_val_from_ulong(2, shift_val);
+ /* remove rounding bits */
+ sc_val_from_ulong(ROUNDING_BITS, shift_val);
_shift_right(_mant(int_float), shift_val, temp);
/* remove leading 1 (or 0 if denormalized) */
char lsb, guard, round, round_dir = 0;
char *temp = alloca(value_size);
- /* +2: save two rounding bits at the end */
- hsb = 2 + in_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
+ /* save rounding bits at the end */
+ hsb = ROUNDING_BITS + in_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
if (in_val != out_val) {
out_val->sign = in_val->sign;
out_val->desc.clss = NORMAL;
/* mantissa all zeros, so zero exponent (because of explicit one) */
- if (hsb == 2 + in_val->desc.mantissa_size) {
+ if (hsb == ROUNDING_BITS + in_val->desc.mantissa_size) {
sc_val_from_ulong(0, _exp(out_val));
hsb = -1;
}
/* perform rounding by adding a value that clears the guard bit and the round bit
* and either causes a carry to round up or not */
/* get the last 3 bits of the value */
- lsb = sc_sub_bits(_mant(out_val), out_val->desc.mantissa_size + 2, 0) & 0x7;
+ lsb = sc_sub_bits(_mant(out_val), out_val->desc.mantissa_size + ROUNDING_BITS, 0) & 0x7;
guard = (lsb&0x2)>>1;
round = lsb&0x1;
out_val->desc.clss = ZERO;
/* check for rounding overflow */
- hsb = 2 + out_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
+ hsb = ROUNDING_BITS + out_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
if ((out_val->desc.clss != SUBNORMAL) && (hsb < -1)) {
sc_val_from_ulong(1, temp);
_shift_right(_mant(out_val), temp, _mant(out_val));
* point are the sum of the factors' digits after the radix point. As all
* values are normalized they both have the same amount of these digits,
* which has to be restored by proper shifting
- * +2 because of the two rounding bits */
- sc_val_from_ulong(2 + result->desc.mantissa_size, temp);
+ * because of the rounding bits */
+ sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
_shift_right(_mant(result), temp, _mant(result));
sticky = sc_had_carry();
* fit into the integer precision, but due to the rounding bits (which
* are always zero because the values are all normalized) the divisor
* can be shifted right instead to achieve the same result */
- sc_val_from_ulong(2 + result->desc.mantissa_size, temp);
+ sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
_shift_left(_mant(a), temp, dividend);
build = alloca(value_size);
temp = alloca(value_size);
- sc_val_from_ulong((1 << result->desc.exponent_size)/2-1, _exp(result));
+ sc_val_from_ulong((1 << (result->desc.exponent_size - 1)) - 1, _exp(result));
if (exp > 0) {
/* temp is value of ten now */
_save_result(build);
/* temp is amount of left shift needed to put the value left of the radix point */
- sc_val_from_ulong(result->desc.mantissa_size + 2, temp);
+ sc_val_from_ulong(result->desc.mantissa_size + ROUNDING_BITS, temp);
_shift_left(build, temp, _mant(result));
sc_val_from_str(mant_str, strlen(mant_str), _mant(result));
/* shift to put value left of radix point */
- sc_val_from_ulong(mant_size + 2, exp_val);
+ sc_val_from_ulong(mant_size + ROUNDING_BITS, exp_val);
_shift_left(_mant(result), exp_val, _mant(result));
- sc_val_from_ulong((1 << exp_size)/2-1, _exp(result));
+ sc_val_from_ulong((1 << (exp_size - 1)) - 1, _exp(result));
_normalize(result, result, 0);
UINT32 sign, exponent, mantissa0, mantissa1;
srcval.d = l;
- bias_res = ((1<<exp_size)/2-1);
+ bias_res = ((1 << (exp_size - 1)) - 1);
#ifdef HAVE_LONG_DOUBLE
mant_val = 64;
if (result == NULL) result = calc_buffer;
temp = alloca(value_size);
- /* CLEAR the buffer */
- //memset(result, 0, fc_get_buffer_length());
+ /* CLEAR the buffer, else some bits might be uninitialised */
+ memset(result, 0, fc_get_buffer_length());
result->desc.exponent_size = exp_size;
result->desc.mantissa_size = mant_size;
if (exponent != 0) {
/* insert the hidden bit */
sc_val_from_ulong(1, temp);
- sc_val_from_ulong(mant_val + 2, NULL);
+ sc_val_from_ulong(mant_val + ROUNDING_BITS, NULL);
_shift_left(temp, sc_get_buffer(), NULL);
}
else
/* bits from the lower word */
sc_val_from_ulong(mantissa1, temp);
- sc_val_from_ulong(2, NULL);
+ sc_val_from_ulong(ROUNDING_BITS, NULL);
_shift_left(temp, sc_get_buffer(), temp);
sc_or(_mant(result), temp, _mant(result));
* lead to wrong results */
exponent = sc_val_to_long(_exp(value)) ;
- sc_val_from_ulong(2, NULL);
+ sc_val_from_ulong(ROUNDING_BITS, NULL);
_shift_right(_mant(value), sc_get_buffer(), _mant(value));
mantissa0 = 0;
sc_val_from_ulong((1<<exponent_size) - 2, _exp(result));
sc_max_from_bits(mantissa_size + 1, 0, _mant(result));
- sc_val_from_ulong(2, NULL);
+ sc_val_from_ulong(ROUNDING_BITS, NULL);
_shift_left(_mant(result), sc_get_buffer(), _mant(result));
return result;
return sc_sub_bits(packed_value, num_bits, byte_ofs);
}
+/* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
int fc_zero_mantissa(const fp_value *value) {
- return sc_get_lowest_set_bit(_mant(value)) == 2 + value->desc.mantissa_size;
+ return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
}
+/* Returns the exponent of a value. */
int fc_get_exponent(const fp_value *value) {
int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
return sc_val_to_long(_exp(value)) - exp_bias;
}
+/* Return non-zero if a given value can be converted lossless into another precision */
+int fc_can_lossless_conv_to(const fp_value *value, char exp_size, char mant_size) {
+ int v;
+ int exp_bias;
+
+ /* handle some special cases first */
+ switch (value->desc.clss) {
+ case ZERO:
+ case INF:
+ case NAN:
+ return 1;
+ default:
+ break;
+ }
+
+ /* check if the exponent can be encoded: note, 0 and all ones are reserved for the exponent */
+ exp_bias = (1 << (exp_size - 1)) - 1;
+ v = fc_get_exponent(value) + exp_bias;
+ if (0 < v && v < (1 << exp_size) - 1) {
+ /* check the mantissa */
+ v = value->desc.mantissa_size + ROUNDING_BITS - sc_get_lowest_set_bit(_mant(value));
+ return v < mant_size;
+ }
+ return 0;
+}
+
fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode) {
if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
}
assert(exp_val >= 0 && "floating point value not integral before fc_flt2int() call");
- shift = exp_val - a->desc.mantissa_size - 2;
+ shift = exp_val - (a->desc.mantissa_size + ROUNDING_BITS);
if (shift > 0) {
sc_shlI(_mant(a), shift, 64, 0, result);
*/
int fc_get_exponent(const fp_value *value);
+/**
+ * Return non-zero if a given value can be converted lossless into another precision.
+ */
+int fc_can_lossless_conv_to(const fp_value *value, char exp_size, char mant_size);
+
/** Set new rounding mode
* This function sets the rounding mode to one of the following, returning
* the previously set rounding mode.
res = fc_rnd(src->value, NULL);
break;
default:
- assert(0);
+ panic("Unsupported float to int conversion mode in tarval_convert_to()");
break;
}
buffer = alloca(sc_get_buffer_length());
* carry flag of the last operation
*/
int tarval_carry(void) {
+ panic("tarval_carry() requetsed: not implemented on all operations");
return sc_had_carry();
}
return fc_get_exponent(tv->value);
}
+/*
+ * Check if the tarval can be converted to the given mode without
+ * precision loss.
+ */
+int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode) {
+ char exp_size, mant_size;
+ switch (get_mode_size_bits(mode)) {
+ case 32:
+ exp_size = 8; mant_size = 23;
+ break;
+ case 64:
+ exp_size = 11; mant_size = 52;
+ break;
+ case 80:
+ case 96:
+ exp_size = 15; mant_size = 64;
+ break;
+ default:
+ panic("Unsupported mode in tarval_ieee754_can_conv_lossless()");
+ return 0;
+ }
+ return fc_can_lossless_conv_to(tv->value, exp_size, mant_size);
+}
+
/* Set the immediate precision for IEEE-754 results. */
unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
return fc_set_immediate_precision(bits);