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 * File name: ir/tv/fltcalc.h
28 * Copyright: (c) 2003 Universität Karlsruhe
34 #include "firm_config.h"
36 #ifdef HAVE_LONG_DOUBLE
37 /* XXX Set this via autoconf */
38 #define HAVE_EXPLICIT_ONE
39 typedef long double LLDBL;
45 FC_add, /**< addition */
46 FC_sub, /**< subtraction */
47 FC_mul, /**< multiplication */
48 FC_div, /**< divide */
49 FC_neg, /**< negate */
50 FC_int, /**< truncate to integer */
51 FC_rnd /**< round to integer */
61 /** IEEE-754 Rounding modes. */
63 FC_TONEAREST, /**< if unsure, to the nearest even */
64 FC_TOPOSITIVE, /**< to +oo */
65 FC_TONEGATIVE, /**< to -oo */
66 FC_TOZERO /**< to 0 */
69 #define FC_DEFAULT_PRECISION 64
71 #define FC_DECLARE1(code) char* fc_##code(const void *a, void *result)
72 #define FC_DECLARE2(code) char* fc_##code(const void *a, const void *b, void *result)
75 /** internal buffer access
76 * All functions that accept NULL as return buffer put their result into an
78 * @return fc_get_buffer() returns the pointer to the buffer, fc_get_buffer_length()
79 * returns the size of this buffer
81 const void *fc_get_buffer(void);
82 int fc_get_buffer_length(void);
85 char* fc_val_from_str(const char *str, unsigned int len, char exp_size, char mant_size, char *result);
87 /** get the representation of a floating point value
88 * This function tries to builds a representation having the same value as the
89 * float number passed.
90 * If the wished precision is less than the precision of LLDBL the value built
91 * will be rounded. Therefore only an approximation of the passed float can be
92 * expected in this case.
94 * @param l The floating point number to build a representation for
95 * @param exp_size The number of bits of the new exponent
96 * @param mant_size The number of bits of the new mantissa
97 * @param result A buffer to hold the value built. If this is NULL, the internal
98 * accumulator buffer is used. Note that the buffer must be big
99 * enough to hold the value. Use fc_get_buffer_length() to find out
101 * @return The result pointer passed to the function. If this was NULL this returns
102 * a pointer to the internal accumulator buffer
104 char* fc_val_from_float(LLDBL l, char exp_size, char mant_size, char *result);
106 /** retrieve the float value of an internal value
107 * This function casts the internal value to LLDBL and returns a LLDBL with
109 * This implies that values of higher precision than LLDBL are subject to
110 * rounding, so the returned value might not the same than the actually
113 * @param val The representation of a float value
114 * @return a float value approximating the represented value
116 LLDBL fc_val_to_float(const void *val);
118 /** cast a value to another precision
119 * This function changes the precision of a float representation.
120 * If the new precision is less than the original precision the returned
121 * value might not be the same as the original value.
123 * @param val The value to be casted
124 * @param exp_size The number of bits of the new exponent
125 * @param mant_size The number of bits of the new mantissa
126 * @param result A buffer to hold the value built. If this is NULL, the internal
127 * accumulator buffer is used. Note that the buffer must be big
128 * enough to hold the value. Use fc_get_buffer_length() to find out
130 * @return The result pointer passed to the function. If this was NULL this returns
131 * a pointer to the internal accumulator buffer
133 char* fc_cast(const void *val, char exp_size, char mant_size, char *result);
136 /** build a special float value
137 * This function builds a representation for a special float value, as indicated by the
140 * @param exponent_size The number of bits of exponent of the float type the value
142 * @param mantissa_size The number of bits of mantissa of the float type the value
144 * @param result A buffer to hold the value built. If this is NULL, the internal
145 * accumulator buffer is used. Note that the buffer must be big
146 * enough to hold the value. Use fc_get_buffer_length() to find out
148 * @return The result pointer passed to the function. If this was NULL this returns
149 * a pointer to the internal accumulator buffer
151 char* fc_get_min(unsigned int exponent_size, unsigned int mantissa_size, char* result);
152 char* fc_get_max(unsigned int exponent_size, unsigned int mantissa_size, char* result);
153 char* fc_get_snan(unsigned int exponent_size, unsigned int mantissa_size, char* result);
154 char* fc_get_qnan(unsigned int exponent_size, unsigned int mantissa_size, char* result);
155 char* fc_get_plusinf(unsigned int exponent_size, unsigned int mantissa_size, char* result);
156 char* fc_get_minusinf(unsigned int exponent_size, unsigned int mantissa_size, char* result);
159 int fc_is_zero(const void *a);
160 int fc_is_negative(const void *a);
161 int fc_is_inf(const void *a);
162 int fc_is_nan(const void *a);
163 int fc_is_subnormal(const void *a);
173 char *fc_print(const void *a, char *buf, int buflen, unsigned base);
175 /** Compare two values
176 * This function compares two values
178 * @param a Value No. 1
179 * @param b Value No. 2
180 * @result The returned value will be one of
184 * 2 if either value is NaN
186 int fc_comp(const void *a, const void *b);
188 /** Set new rounding mode
189 * This function sets the rounding mode to one of the following, returning
190 * the previously set rounding mode.
191 * FC_TONEAREST (default):
192 * Any unrepresentable value is rounded to the nearest representable
193 * value. If it lies in the middle the value with the least significant
194 * bit of zero is chosen (the even one).
195 * Values too big to represent will round to +/-infinity.
197 * Any unrepresentable value is rounded towards negative infinity.
198 * Positive values too big to represent will round to the biggest
199 * representable value, negative values too small to represent will
200 * round to -infinity.
202 * Any unrepresentable value is rounded towards positive infinity
203 * Negative values too small to represent will round to the biggest
204 * representable value, positive values too big to represent will
205 * round to +infinity.
207 * Any unrepresentable value is rounded towards zero, effectively
208 * chopping off any bits beyond the mantissa size.
209 * Values too big to represent will round to the biggest/smallest
210 * representable value.
212 * These modes correspond to the modes required by the IEEE-754 standard.
214 * @param mode The new rounding mode. Any value other than the four
215 * defined values will have no effect.
216 * @return The previous rounding mode.
218 * @see fc_get_rounding_mode()
219 * @see IEEE754, IEEE854 Floating Point Standard
221 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode);
223 /** Get the rounding mode
224 * This function retrieves the currently used rounding mode
226 * @return The current rounding mode
227 * @see fc_set_rounding_mode()
229 fc_rounding_mode_t fc_get_rounding_mode(void);
231 /** Get bit representation of a value
232 * This function allows to read a value in encoded form, bytewise.
233 * The value will be packed corresponding to the way used by the IEEE
234 * encoding formats, i.e.
236 * exp_size bits exponent + bias
237 * mant_size bits mantissa, without leading 1
239 * As in IEEE, an exponent of 0 indicates a denormalized number, which
240 * implies a most significant bit of zero instead of one; an exponent
241 * of all ones (2**exp_size - 1) encodes infinity if the mantissa is
242 * all zeros, else Not A Number.
244 * @param val A pointer to the value. If NULL is passed a copy of the
245 * most recent value passed to this function is used, saving the
246 * packing step. This behavior may be changed in the future.
247 * @param num_bit The maximum number of bits to return. Any bit beyond
248 * num_bit will be returned as zero.
249 * @param byte_ofs The byte index to read, 0 is the least significant
251 * @return 8 bits of encoded data
253 unsigned char fc_sub_bits(const void *val, unsigned num_bit, unsigned byte_ofs);
255 void init_fltcalc(int precision);
256 void finish_fltcalc (void);
258 #endif /* _FLTCALC_H_ */