1 /* 2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved. 3 * 4 * This file is part of libFirm. 5 * 6 * This file may be distributed and/or modified under the terms of the 7 * GNU General Public License version 2 as published by the Free Software 8 * Foundation and appearing in the file LICENSE.GPL included in the 9 * packaging of this file. 10 * 11 * Licensees holding valid libFirm Professional Edition licenses may use 12 * this file in accordance with the libFirm Commercial License. 13 * Agreement provided with the Software. 14 * 15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE. 18 */ 19 20 /** 21 * @file 22 * @brief tarval floating point calculations 23 * @date 2003 24 * @author Mathias Heil 25 */ 26 #ifndef FIRM_TV_FLTCALC_H 27 #define FIRM_TV_FLTCALC_H 28 29 #include <stdlib.h> 30 #include "firm_types.h" 31 #include "irtypes.h" 32 33 enum { 34 FC_DEC, 35 FC_HEX, 36 FC_BIN, 37 FC_PACKED 38 }; 39 40 /** IEEE-754 Rounding modes. */ 41 typedef enum { 42 FC_TONEAREST, /**< if unsure, to the nearest even */ 43 FC_TOPOSITIVE, /**< to +oo */ 44 FC_TONEGATIVE, /**< to -oo */ 45 FC_TOZERO /**< to 0 */ 46 } fc_rounding_mode_t; 47 48 #define FC_DEFAULT_PRECISION 64 49 50 /** 51 * possible float states 52 */ 53 typedef enum { 54 FC_NORMAL, /**< normal representation, implicit 1 */ 55 FC_ZERO, /**< +/-0 */ 56 FC_SUBNORMAL, /**< denormals, implicit 0 */ 57 FC_INF, /**< +/-oo */ 58 FC_NAN, /**< Not A Number */ 59 } value_class_t; 60 61 struct fp_value; 62 typedef struct fp_value fp_value; 63 64 /*@{*/ 65 /** internal buffer access 66 * All functions that accept NULL as return buffer put their result into an 67 * internal buffer. 68 * @return fc_get_buffer() returns the pointer to the buffer, fc_get_buffer_length() 69 * returns the size of this buffer 70 */ 71 const void *fc_get_buffer(void); 72 int fc_get_buffer_length(void); 73 /*}@*/ 74 75 void *fc_val_from_str(const char *str, size_t len, const float_descriptor_t *desc, void *result); 76 77 /** get the representation of a floating point value 78 * This function tries to builds a representation having the same value as the 79 * float number passed. 80 * If the wished precision is less than the precision of long double the value 81 * built will be rounded. Therefore only an approximation of the passed float 82 * can be expected in this case. 83 * 84 * @param l The floating point number to build a representation for 85 * @param desc The floating point descriptor 86 * @param result A buffer to hold the value built. If this is NULL, the internal 87 * accumulator buffer is used. Note that the buffer must be big 88 * enough to hold the value. Use fc_get_buffer_length() to find out 89 * the size needed 90 * 91 * @return The result pointer passed to the function. If this was NULL this returns 92 * a pointer to the internal accumulator buffer 93 */ 94 fp_value *fc_val_from_ieee754(long double l, const float_descriptor_t *desc, 95 fp_value *result); 96 97 /** retrieve the float value of an internal value 98 * This function casts the internal value to long double and returns a 99 * long double with that value. 100 * This implies that values of higher precision than long double are subject to 101 * rounding, so the returned value might not the same than the actually 102 * represented value. 103 * 104 * @param val The representation of a float value 105 * 106 * @return a float value approximating the represented value 107 */ 108 long double fc_val_to_ieee754(const fp_value *val); 109 110 /** cast a value to another precision 111 * This function changes the precision of a float representation. 112 * If the new precision is less than the original precision the returned 113 * value might not be the same as the original value. 114 * 115 * @param val The value to be casted 116 * @param desc The floating point descriptor 117 * @param result A buffer to hold the value built. If this is NULL, the internal 118 * accumulator buffer is used. Note that the buffer must be big 119 * enough to hold the value. Use fc_get_buffer_length() to find out 120 * the size needed 121 * @return The result pointer passed to the function. If this was NULL this returns 122 * a pointer to the internal accumulator buffer 123 */ 124 fp_value *fc_cast(const fp_value *val, const float_descriptor_t *desc, fp_value *result); 125 126 /*@{*/ 127 /** build a special float value 128 * This function builds a representation for a special float value, as indicated by the 129 * function's suffix. 130 * 131 * @param desc The floating point descriptor 132 * @param result A buffer to hold the value built. If this is NULL, the internal 133 * accumulator buffer is used. Note that the buffer must be big 134 * enough to hold the value. Use fc_get_buffer_length() to find out 135 * the size needed 136 * @return The result pointer passed to the function. If this was NULL this returns 137 * a pointer to the internal accumulator buffer 138 */ 139 fp_value *fc_get_min(const float_descriptor_t *desc, fp_value *result); 140 fp_value *fc_get_max(const float_descriptor_t *desc, fp_value *result); 141 fp_value *fc_get_snan(const float_descriptor_t *desc, fp_value *result); 142 fp_value *fc_get_qnan(const float_descriptor_t *desc, fp_value *result); 143 fp_value *fc_get_plusinf(const float_descriptor_t *desc, fp_value *result); 144 fp_value *fc_get_minusinf(const float_descriptor_t *desc, fp_value *result); 145 /*@}*/ 146 147 int fc_is_zero(const fp_value *a); 148 int fc_is_negative(const fp_value *a); 149 int fc_is_inf(const fp_value *a); 150 int fc_is_nan(const fp_value *a); 151 int fc_is_subnormal(const fp_value *a); 152 153 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result); 154 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result); 155 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result); 156 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result); 157 fp_value *fc_neg(const fp_value *a, fp_value *result); 158 fp_value *fc_int(const fp_value *a, fp_value *result); 159 fp_value *fc_rnd(const fp_value *a, fp_value *result); 160 161 char *fc_print(const fp_value *a, char *buf, int buflen, unsigned base); 162 163 /** Compare two values 164 * This function compares two values 165 * 166 * @param a Value No. 1 167 * @param b Value No. 2 168 * @result The returned value will be one of 169 * -1 if a < b 170 * 0 if a == b 171 * 1 if a > b 172 * 2 if either value is NaN 173 */ 174 int fc_comp(const fp_value *a, const fp_value *b); 175 176 /** 177 * Converts an floating point value into an integer value. 178 */ 179 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode); 180 181 /** 182 * Returns non-zero if the mantissa is zero, i.e. 1.0Exxx 183 */ 184 int fc_zero_mantissa(const fp_value *value); 185 186 /** 187 * Returns the exponent of a value. 188 */ 189 int fc_get_exponent(const fp_value *value); 190 191 /** 192 * Return non-zero if a given value can be converted lossless into another precision. 193 */ 194 int fc_can_lossless_conv_to(const fp_value *value, const float_descriptor_t *desc); 195 196 /** Set new rounding mode 197 * This function sets the rounding mode to one of the following, returning 198 * the previously set rounding mode. 199 * FC_TONEAREST (default): 200 * Any unrepresentable value is rounded to the nearest representable 201 * value. If it lies in the middle the value with the least significant 202 * bit of zero is chosen (the even one). 203 * Values too big to represent will round to +/-infinity. 204 * FC_TONEGATIVE 205 * Any unrepresentable value is rounded towards negative infinity. 206 * Positive values too big to represent will round to the biggest 207 * representable value, negative values too small to represent will 208 * round to -infinity. 209 * FC_TOPOSITIVE 210 * Any unrepresentable value is rounded towards positive infinity 211 * Negative values too small to represent will round to the biggest 212 * representable value, positive values too big to represent will 213 * round to +infinity. 214 * FC_TOZERO 215 * Any unrepresentable value is rounded towards zero, effectively 216 * chopping off any bits beyond the mantissa size. 217 * Values too big to represent will round to the biggest/smallest 218 * representable value. 219 * 220 * These modes correspond to the modes required by the IEEE-754 standard. 221 * 222 * @param mode The new rounding mode. Any value other than the four 223 * defined values will have no effect. 224 * @return The previous rounding mode. 225 * 226 * @see fc_get_rounding_mode() 227 * @see IEEE754, IEEE854 Floating Point Standard 228 */ 229 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode); 230 231 /** Get the rounding mode 232 * This function retrieves the currently used rounding mode 233 * 234 * @return The current rounding mode 235 * @see fc_set_rounding_mode() 236 */ 237 fc_rounding_mode_t fc_get_rounding_mode(void); 238 239 /** Get bit representation of a value 240 * This function allows to read a value in encoded form, byte wise. 241 * The value will be packed corresponding to the way used by the IEEE 242 * encoding formats, i.e. 243 * One bit sign 244 * exp_size bits exponent + bias 245 * mant_size bits mantissa, without leading 1 246 * 247 * As in IEEE, an exponent of 0 indicates a denormalized number, which 248 * implies a most significant bit of zero instead of one; an exponent 249 * of all ones (2**exp_size - 1) encodes infinity if the mantissa is 250 * all zeros, else Not A Number. 251 * 252 * @param val A pointer to the value. If NULL is passed a copy of the 253 * most recent value passed to this function is used, saving the 254 * packing step. This behavior may be changed in the future. 255 * @param num_bit The maximum number of bits to return. Any bit beyond 256 * num_bit will be returned as zero. 257 * @param byte_ofs The byte index to read, 0 is the least significant 258 * byte. 259 * @return 8 bits of encoded data 260 */ 261 unsigned char fc_sub_bits(const fp_value *val, unsigned num_bit, unsigned byte_ofs); 262 263 /** 264 * Returns non-zero if the result of the last operation was exact. 265 */ 266 int fc_is_exact(void); 267 268 void init_fltcalc(int precision); 269 void finish_fltcalc(void); 270 271 #endif /* FIRM_TV_FLTCALC_H */ 272