Commit 365ac6ee authored by Michael Beck's avatar Michael Beck
Browse files

- added fc_can_lossless_conv_to() / tarval_ieee754_can_conv_lossless()

- BugFix: the buffer REALLY must been cleared in fc_val_from_ieee754()
- replaced the constant 2 by ROUNDING_BITS
- replaced division in exponent bias calculation
- replaced assert(0) by panic()
- add panic when tarval_carry() is used (not fully implemented)
- improved doxygen docu

[r19405]
parent 75ec67cb
......@@ -147,6 +147,8 @@ long get_tarval_long(tarval *tv);
* 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);
......@@ -185,6 +187,8 @@ tarval *new_tarval_from_double(long double d, ir_mode *mode);
* 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);
......@@ -192,6 +196,8 @@ 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);
......@@ -221,8 +227,12 @@ 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*); */
......@@ -232,30 +242,30 @@ ir_mode *get_tarval_mode (const tarval *tv);
/**
* 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
......@@ -322,9 +332,9 @@ tarval *get_tarval_minus_inf(ir_mode *mode);
/* ******************** 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;
/**
......@@ -344,8 +354,8 @@ tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
* 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.
......@@ -374,7 +384,7 @@ pn_Cmp tarval_cmp(tarval *a, tarval *b);
* constructed and returned
*
* @note
* Illegal conversations will trigger an assertion
* Illegal convertions will trigger a panic
*
* @sa
* FIRM documentation for conversion rules
......@@ -407,10 +417,10 @@ tarval *tarval_convert_to(tarval *src, ir_mode *mode);
* 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. */
......@@ -422,10 +432,10 @@ tarval *tarval_sub(tarval *a, tarval *b);
/** 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. */
......@@ -434,7 +444,7 @@ tarval *tarval_mod(tarval *a, tarval *b);
/** 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. */
......@@ -458,7 +468,9 @@ tarval *tarval_shrs(tarval *a, tarval *b);
/** 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 *********** */
......@@ -472,13 +484,13 @@ int tarval_carry(void);
* 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;
/**
......@@ -486,11 +498,11 @@ typedef enum {
* 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;
/**
......@@ -571,31 +583,52 @@ unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs);
/**
* 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.
......@@ -617,21 +650,29 @@ int tarval_enable_fp_ops(int enable);
/**
* 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);
......
......@@ -52,6 +52,9 @@
#include "xmalloc.h"
/** The number of extra precesion rounding bits */
#define ROUNDING_BITS 2
typedef uint32_t UINT32;
#ifdef HAVE_LONG_DOUBLE
......@@ -210,8 +213,8 @@ static void *pack(const fp_value *int_float, void *packed) {
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) */
......@@ -235,8 +238,8 @@ static int normalize(const fp_value *in_val, fp_value *out_val, int sticky) {
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;
......@@ -246,7 +249,7 @@ static int normalize(const fp_value *in_val, fp_value *out_val, int sticky) {
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;
}
......@@ -295,7 +298,7 @@ static int normalize(const fp_value *in_val, fp_value *out_val, int sticky) {
/* 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;
......@@ -339,7 +342,7 @@ static int normalize(const fp_value *in_val, fp_value *out_val, int sticky) {
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));
......@@ -611,8 +614,8 @@ static void _fmul(const fp_value *a, const fp_value *b, fp_value *result) {
* 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();
......@@ -702,7 +705,7 @@ static void _fdiv(const fp_value *a, const fp_value *b, fp_value *result) {
* 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);
......@@ -733,7 +736,7 @@ static void _power_of_ten(int exp, descriptor_t *desc, char *result) {
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 */
......@@ -747,7 +750,7 @@ static void _power_of_ten(int exp, descriptor_t *desc, char *result) {
_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));
......@@ -991,11 +994,11 @@ done:
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);
......@@ -1034,7 +1037,7 @@ fp_value *fc_val_from_ieee754(LLDBL l, char exp_size, char mant_size, fp_value *
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;
......@@ -1063,8 +1066,8 @@ fp_value *fc_val_from_ieee754(LLDBL l, char exp_size, char mant_size, fp_value *
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;
......@@ -1100,7 +1103,7 @@ fp_value *fc_val_from_ieee754(LLDBL l, char exp_size, char mant_size, fp_value *
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
......@@ -1119,7 +1122,7 @@ fp_value *fc_val_from_ieee754(LLDBL l, char exp_size, char mant_size, fp_value *
/* 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));
......@@ -1171,7 +1174,7 @@ LLDBL fc_val_to_ieee754(const fp_value *val) {
* 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;
......@@ -1264,7 +1267,7 @@ fp_value *fc_get_max(unsigned int exponent_size, unsigned int mantissa_size, fp_
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;
......@@ -1479,15 +1482,43 @@ unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byt
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)
......@@ -1655,7 +1686,7 @@ int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode) {
}
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);
......
......@@ -186,6 +186,11 @@ int fc_zero_mantissa(const fp_value *value);
*/
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.
......
......@@ -906,7 +906,7 @@ tarval *tarval_convert_to(tarval *src, ir_mode *dst_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());
......@@ -1465,6 +1465,7 @@ tarval *tarval_rot(tarval *a, tarval *b) {
* carry flag of the last operation
*/
int tarval_carry(void) {
panic("tarval_carry() requetsed: not implemented on all operations");
return sc_had_carry();
}
......@@ -1658,6 +1659,30 @@ int tarval_ieee754_get_exponent(tarval *tv) {
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);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment