2 * Copyright (C) 1995-2007 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"
32 #ifdef HAVE_LONG_DOUBLE
33 /* XXX Set this via autoconf */
34 #define HAVE_EXPLICIT_ONE
35 typedef long double LLDBL;
47 /** IEEE-754 Rounding modes. */
49 FC_TONEAREST, /**< if unsure, to the nearest even */
50 FC_TOPOSITIVE, /**< to +oo */
51 FC_TONEGATIVE, /**< to -oo */
52 FC_TOZERO /**< to 0 */
55 #define FC_DEFAULT_PRECISION 64
57 typedef struct _fp_value fp_value;
60 /** internal buffer access
61 * All functions that accept NULL as return buffer put their result into an
63 * @return fc_get_buffer() returns the pointer to the buffer, fc_get_buffer_length()
64 * returns the size of this buffer
66 const void *fc_get_buffer(void);
67 int fc_get_buffer_length(void);
70 void *fc_val_from_str(const char *str, unsigned int len, char exp_size, char mant_size, void *result);
72 /** get the representation of a floating point value
73 * This function tries to builds a representation having the same value as the
74 * float number passed.
75 * If the wished precision is less than the precision of LLDBL the value built
76 * will be rounded. Therefore only an approximation of the passed float can be
77 * expected in this case.
79 * @param l The floating point number to build a representation for
80 * @param exp_size The number of bits of the new exponent
81 * @param mant_size The number of bits of the new mantissa
82 * @param result A buffer to hold the value built. If this is NULL, the internal
83 * accumulator buffer is used. Note that the buffer must be big
84 * enough to hold the value. Use fc_get_buffer_length() to find out
86 * @return The result pointer passed to the function. If this was NULL this returns
87 * a pointer to the internal accumulator buffer
89 fp_value *fc_val_from_ieee754(LLDBL l, char exp_size, char mant_size, fp_value *result);
91 /** retrieve the float value of an internal value
92 * This function casts the internal value to LLDBL and returns a LLDBL with
94 * This implies that values of higher precision than LLDBL are subject to
95 * rounding, so the returned value might not the same than the actually
98 * @param val The representation of a float value
99 * @return a float value approximating the represented value
101 LLDBL fc_val_to_ieee754(const fp_value *val);
103 /** cast a value to another precision
104 * This function changes the precision of a float representation.
105 * If the new precision is less than the original precision the returned
106 * value might not be the same as the original value.
108 * @param val The value to be casted
109 * @param exp_size The number of bits of the new exponent
110 * @param mant_size The number of bits of the new mantissa
111 * @param result A buffer to hold the value built. If this is NULL, the internal
112 * accumulator buffer is used. Note that the buffer must be big
113 * enough to hold the value. Use fc_get_buffer_length() to find out
115 * @return The result pointer passed to the function. If this was NULL this returns
116 * a pointer to the internal accumulator buffer
118 fp_value *fc_cast(const fp_value *val, char exp_size, char mant_size, fp_value *result);
121 /** build a special float value
122 * This function builds a representation for a special float value, as indicated by the
125 * @param exponent_size The number of bits of exponent of the float type the value
127 * @param mantissa_size The number of bits of mantissa of the float type the value
129 * @param result A buffer to hold the value built. If this is NULL, the internal
130 * accumulator buffer is used. Note that the buffer must be big
131 * enough to hold the value. Use fc_get_buffer_length() to find out
133 * @return The result pointer passed to the function. If this was NULL this returns
134 * a pointer to the internal accumulator buffer
136 fp_value *fc_get_min(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result);
137 fp_value *fc_get_max(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result);
138 fp_value *fc_get_snan(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result);
139 fp_value *fc_get_qnan(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result);
140 fp_value *fc_get_plusinf(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result);
141 fp_value *fc_get_minusinf(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result);
144 int fc_is_zero(const fp_value *a);
145 int fc_is_negative(const fp_value *a);
146 int fc_is_inf(const fp_value *a);
147 int fc_is_nan(const fp_value *a);
148 int fc_is_subnormal(const fp_value *a);
150 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result);
151 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result);
152 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result);
153 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result);
154 fp_value *fc_neg(const fp_value *a, fp_value *result);
155 fp_value *fc_int(const fp_value *a, fp_value *result);
156 fp_value *fc_rnd(const fp_value *a, fp_value *result);
158 char *fc_print(const fp_value *a, char *buf, int buflen, unsigned base);
160 /** Compare two values
161 * This function compares two values
163 * @param a Value No. 1
164 * @param b Value No. 2
165 * @result The returned value will be one of
169 * 2 if either value is NaN
171 int fc_comp(const fp_value *a, const fp_value *b);
174 * Returns non-zero if the mantissa is zero, i.e. 1.0Exxx
176 int fc_zero_mantissa(const fp_value *value);
179 * Returns the exponent of a value.
181 int fc_get_exponent(const fp_value *value);
183 /** Set new rounding mode
184 * This function sets the rounding mode to one of the following, returning
185 * the previously set rounding mode.
186 * FC_TONEAREST (default):
187 * Any unrepresentable value is rounded to the nearest representable
188 * value. If it lies in the middle the value with the least significant
189 * bit of zero is chosen (the even one).
190 * Values too big to represent will round to +/-infinity.
192 * Any unrepresentable value is rounded towards negative infinity.
193 * Positive values too big to represent will round to the biggest
194 * representable value, negative values too small to represent will
195 * round to -infinity.
197 * Any unrepresentable value is rounded towards positive infinity
198 * Negative values too small to represent will round to the biggest
199 * representable value, positive values too big to represent will
200 * round to +infinity.
202 * Any unrepresentable value is rounded towards zero, effectively
203 * chopping off any bits beyond the mantissa size.
204 * Values too big to represent will round to the biggest/smallest
205 * representable value.
207 * These modes correspond to the modes required by the IEEE-754 standard.
209 * @param mode The new rounding mode. Any value other than the four
210 * defined values will have no effect.
211 * @return The previous rounding mode.
213 * @see fc_get_rounding_mode()
214 * @see IEEE754, IEEE854 Floating Point Standard
216 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode);
218 /** Get the rounding mode
219 * This function retrieves the currently used rounding mode
221 * @return The current rounding mode
222 * @see fc_set_rounding_mode()
224 fc_rounding_mode_t fc_get_rounding_mode(void);
226 /** Get bit representation of a value
227 * This function allows to read a value in encoded form, byte wise.
228 * The value will be packed corresponding to the way used by the IEEE
229 * encoding formats, i.e.
231 * exp_size bits exponent + bias
232 * mant_size bits mantissa, without leading 1
234 * As in IEEE, an exponent of 0 indicates a denormalized number, which
235 * implies a most significant bit of zero instead of one; an exponent
236 * of all ones (2**exp_size - 1) encodes infinity if the mantissa is
237 * all zeros, else Not A Number.
239 * @param val A pointer to the value. If NULL is passed a copy of the
240 * most recent value passed to this function is used, saving the
241 * packing step. This behavior may be changed in the future.
242 * @param num_bit The maximum number of bits to return. Any bit beyond
243 * num_bit will be returned as zero.
244 * @param byte_ofs The byte index to read, 0 is the least significant
246 * @return 8 bits of encoded data
248 unsigned char fc_sub_bits(const fp_value *val, unsigned num_bit, unsigned byte_ofs);
250 void init_fltcalc(int precision);
251 void finish_fltcalc(void);
253 #endif /* FIRM_TV_FLTCALC_H */