2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief tarval floating point calculations
24 * @author Mathias Heil
27 #ifndef FIRM_TV_FLTCALC_H
28 #define FIRM_TV_FLTCALC_H
30 #include "firm_config.h"
31 #include "firm_types.h"
33 #ifdef HAVE_LONG_DOUBLE
34 /* XXX Set this via autoconf */
35 #define HAVE_EXPLICIT_ONE
36 typedef long double LLDBL;
48 /** IEEE-754 Rounding modes. */
50 FC_TONEAREST, /**< if unsure, to the nearest even */
51 FC_TOPOSITIVE, /**< to +oo */
52 FC_TONEGATIVE, /**< to -oo */
53 FC_TOZERO /**< to 0 */
56 #define FC_DEFAULT_PRECISION 64
59 * possible float states
62 NORMAL, /**< normal representation, implicit 1 */
64 SUBNORMAL, /**< denormals, implicit 0 */
66 NAN, /**< Not A Number */
70 * A descriptor for an IEEE float value.
72 typedef struct ieee_descriptor_t {
73 unsigned char exponent_size; /**< size of exponent in bits */
74 unsigned char mantissa_size; /**< size of mantissa in bits */
75 unsigned char explicit_one; /**< set if the leading one is explicit */
76 unsigned char clss; /**< state of this float */
80 typedef struct _fp_value fp_value;
83 /** internal buffer access
84 * All functions that accept NULL as return buffer put their result into an
86 * @return fc_get_buffer() returns the pointer to the buffer, fc_get_buffer_length()
87 * returns the size of this buffer
89 const void *fc_get_buffer(void);
90 int fc_get_buffer_length(void);
93 void *fc_val_from_str(const char *str, unsigned int len, const ieee_descriptor_t *desc, void *result);
95 /** get the representation of a floating point value
96 * This function tries to builds a representation having the same value as the
97 * float number passed.
98 * If the wished precision is less than the precision of LLDBL the value built
99 * will be rounded. Therefore only an approximation of the passed float can be
100 * expected in this case.
102 * @param l The floating point number to build a representation for
103 * @param desc The floating point descriptor
104 * @param result A buffer to hold the value built. If this is NULL, the internal
105 * accumulator buffer is used. Note that the buffer must be big
106 * enough to hold the value. Use fc_get_buffer_length() to find out
109 * @return The result pointer passed to the function. If this was NULL this returns
110 * a pointer to the internal accumulator buffer
112 fp_value *fc_val_from_ieee754(LLDBL l, const ieee_descriptor_t *desc, fp_value *result);
114 /** retrieve the float value of an internal value
115 * This function casts the internal value to LLDBL and returns a LLDBL with
117 * This implies that values of higher precision than LLDBL are subject to
118 * rounding, so the returned value might not the same than the actually
121 * @param val The representation of a float value
123 * @return a float value approximating the represented value
125 LLDBL fc_val_to_ieee754(const fp_value *val);
127 /** cast a value to another precision
128 * This function changes the precision of a float representation.
129 * If the new precision is less than the original precision the returned
130 * value might not be the same as the original value.
132 * @param val The value to be casted
133 * @param desc The floating point descriptor
134 * @param result A buffer to hold the value built. If this is NULL, the internal
135 * accumulator buffer is used. Note that the buffer must be big
136 * enough to hold the value. Use fc_get_buffer_length() to find out
138 * @return The result pointer passed to the function. If this was NULL this returns
139 * a pointer to the internal accumulator buffer
141 fp_value *fc_cast(const fp_value *val, const ieee_descriptor_t *desc, fp_value *result);
144 /** build a special float value
145 * This function builds a representation for a special float value, as indicated by the
148 * @param desc The floating point descriptor
149 * @param result A buffer to hold the value built. If this is NULL, the internal
150 * accumulator buffer is used. Note that the buffer must be big
151 * enough to hold the value. Use fc_get_buffer_length() to find out
153 * @return The result pointer passed to the function. If this was NULL this returns
154 * a pointer to the internal accumulator buffer
156 fp_value *fc_get_min(const ieee_descriptor_t *desc, fp_value *result);
157 fp_value *fc_get_max(const ieee_descriptor_t *desc, fp_value *result);
158 fp_value *fc_get_snan(const ieee_descriptor_t *desc, fp_value *result);
159 fp_value *fc_get_qnan(const ieee_descriptor_t *desc, fp_value *result);
160 fp_value *fc_get_plusinf(const ieee_descriptor_t *desc, fp_value *result);
161 fp_value *fc_get_minusinf(const ieee_descriptor_t *desc, fp_value *result);
164 int fc_is_zero(const fp_value *a);
165 int fc_is_negative(const fp_value *a);
166 int fc_is_inf(const fp_value *a);
167 int fc_is_nan(const fp_value *a);
168 int fc_is_subnormal(const fp_value *a);
170 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result);
171 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result);
172 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result);
173 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result);
174 fp_value *fc_neg(const fp_value *a, fp_value *result);
175 fp_value *fc_int(const fp_value *a, fp_value *result);
176 fp_value *fc_rnd(const fp_value *a, fp_value *result);
178 char *fc_print(const fp_value *a, char *buf, int buflen, unsigned base);
180 /** Compare two values
181 * This function compares two values
183 * @param a Value No. 1
184 * @param b Value No. 2
185 * @result The returned value will be one of
189 * 2 if either value is NaN
191 int fc_comp(const fp_value *a, const fp_value *b);
194 * Converts an floating point value into an integer value.
196 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode);
199 * Returns non-zero if the mantissa is zero, i.e. 1.0Exxx
201 int fc_zero_mantissa(const fp_value *value);
204 * Returns the exponent of a value.
206 int fc_get_exponent(const fp_value *value);
209 * Return non-zero if a given value can be converted lossless into another precision.
211 int fc_can_lossless_conv_to(const fp_value *value, const ieee_descriptor_t *desc);
213 /** Set new rounding mode
214 * This function sets the rounding mode to one of the following, returning
215 * the previously set rounding mode.
216 * FC_TONEAREST (default):
217 * Any unrepresentable value is rounded to the nearest representable
218 * value. If it lies in the middle the value with the least significant
219 * bit of zero is chosen (the even one).
220 * Values too big to represent will round to +/-infinity.
222 * Any unrepresentable value is rounded towards negative infinity.
223 * Positive values too big to represent will round to the biggest
224 * representable value, negative values too small to represent will
225 * round to -infinity.
227 * Any unrepresentable value is rounded towards positive infinity
228 * Negative values too small to represent will round to the biggest
229 * representable value, positive values too big to represent will
230 * round to +infinity.
232 * Any unrepresentable value is rounded towards zero, effectively
233 * chopping off any bits beyond the mantissa size.
234 * Values too big to represent will round to the biggest/smallest
235 * representable value.
237 * These modes correspond to the modes required by the IEEE-754 standard.
239 * @param mode The new rounding mode. Any value other than the four
240 * defined values will have no effect.
241 * @return The previous rounding mode.
243 * @see fc_get_rounding_mode()
244 * @see IEEE754, IEEE854 Floating Point Standard
246 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode);
248 /** Get the rounding mode
249 * This function retrieves the currently used rounding mode
251 * @return The current rounding mode
252 * @see fc_set_rounding_mode()
254 fc_rounding_mode_t fc_get_rounding_mode(void);
256 /** Get bit representation of a value
257 * This function allows to read a value in encoded form, byte wise.
258 * The value will be packed corresponding to the way used by the IEEE
259 * encoding formats, i.e.
261 * exp_size bits exponent + bias
262 * mant_size bits mantissa, without leading 1
264 * As in IEEE, an exponent of 0 indicates a denormalized number, which
265 * implies a most significant bit of zero instead of one; an exponent
266 * of all ones (2**exp_size - 1) encodes infinity if the mantissa is
267 * all zeros, else Not A Number.
269 * @param val A pointer to the value. If NULL is passed a copy of the
270 * most recent value passed to this function is used, saving the
271 * packing step. This behavior may be changed in the future.
272 * @param num_bit The maximum number of bits to return. Any bit beyond
273 * num_bit will be returned as zero.
274 * @param byte_ofs The byte index to read, 0 is the least significant
276 * @return 8 bits of encoded data
278 unsigned char fc_sub_bits(const fp_value *val, unsigned num_bit, unsigned byte_ofs);
281 * Set the immediate precision for IEEE-754 results. Set this to
282 * 0 to get the same precision as the operands.
283 * For x87 compatibility, set this to 80.
285 * @return the old setting
287 unsigned fc_set_immediate_precision(unsigned bits);
290 * Returns non-zero if the result of the last operation was exact.
292 int fc_is_exact(void);
294 void init_fltcalc(int precision);
295 void finish_fltcalc(void);
297 #endif /* FIRM_TV_FLTCALC_H */