added ieee754 exact flag which allows more cases to be optimized
[libfirm] / ir / tv / fltcalc.h
1 /*
2  * Copyright (C) 1995-2007 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  * @version  $Id$
26  */
27 #ifndef FIRM_TV_FLTCALC_H
28 #define FIRM_TV_FLTCALC_H
29
30 #include "firm_config.h"
31
32 #ifdef HAVE_LONG_DOUBLE
33 /* XXX Set this via autoconf */
34 #define HAVE_EXPLICIT_ONE
35 typedef long double LLDBL;
36 #else
37 typedef double LLDBL;
38 #endif
39
40 enum {
41         FC_DEC,
42         FC_HEX,
43         FC_BIN,
44         FC_PACKED
45 };
46
47 /** IEEE-754 Rounding modes. */
48 typedef enum {
49         FC_TONEAREST,   /**< if unsure, to the nearest even */
50         FC_TOPOSITIVE,  /**< to +oo */
51         FC_TONEGATIVE,  /**< to -oo */
52         FC_TOZERO       /**< to 0 */
53 } fc_rounding_mode_t;
54
55 #define FC_DEFAULT_PRECISION 64
56
57 typedef struct _fp_value fp_value;
58
59 /*@{*/
60 /** internal buffer access
61  * All functions that accept NULL as return buffer put their result into an
62  * internal buffer.
63  * @return fc_get_buffer() returns the pointer to the buffer, fc_get_buffer_length()
64  * returns the size of this buffer
65  */
66 const void *fc_get_buffer(void);
67 int fc_get_buffer_length(void);
68 /*}@*/
69
70 void *fc_val_from_str(const char *str, unsigned int len, char exp_size, char mant_size, void *result);
71
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.
78  *
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
85  *               the size needed
86  * @return The result pointer passed to the function. If this was NULL this returns
87  *               a pointer to the internal accumulator buffer
88  */
89 fp_value *fc_val_from_ieee754(LLDBL l, char exp_size, char mant_size, fp_value *result);
90
91 /** retrieve the float value of an internal value
92  * This function casts the internal value to LLDBL and returns a LLDBL with
93  * that value.
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
96  * represented value.
97  *
98  * @param val The representation of a float value
99  * @return a float value approximating the represented value
100  */
101 LLDBL fc_val_to_ieee754(const fp_value *val);
102
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.
107  *
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
114  *               the size needed
115  * @return The result pointer passed to the function. If this was NULL this returns
116  *               a pointer to the internal accumulator buffer
117  */
118 fp_value *fc_cast(const fp_value *val, char exp_size, char mant_size, fp_value *result);
119
120 /*@{*/
121 /** build a special float value
122  * This function builds a representation for a special float value, as indicated by the
123  * function's suffix.
124  *
125  * @param exponent_size The number of bits of exponent of the float type the value
126  *               is created for
127  * @param mantissa_size The number of bits of mantissa of the float type the value
128  *               is created for
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
132  *               the size needed
133  * @return The result pointer passed to the function. If this was NULL this returns
134  *               a pointer to the internal accumulator buffer
135  */
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);
142 /*@}*/
143
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);
149
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);
157
158 char *fc_print(const fp_value *a, char *buf, int buflen, unsigned base);
159
160 /** Compare two values
161  * This function compares two values
162  *
163  * @param a Value No. 1
164  * @param b Value No. 2
165  * @result The returned value will be one of
166  *          -1  if a < b
167  *           0  if a == b
168  *           1  if a > b
169  *           2  if either value is NaN
170  */
171 int fc_comp(const fp_value *a, const fp_value *b);
172
173 /**
174  * Returns non-zero if the mantissa is zero, i.e. 1.0Exxx
175  */
176 int fc_zero_mantissa(const fp_value *value);
177
178 /**
179  * Returns the exponent of a value.
180  */
181 int fc_get_exponent(const fp_value *value);
182
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.
191  * FC_TONEGATIVE
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.
196  * FC_TOPOSITIVE
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.
201  * FC_TOZERO
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.
206  *
207  * These modes correspond to the modes required by the IEEE-754 standard.
208  *
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.
212  *
213  * @see fc_get_rounding_mode()
214  * @see IEEE754, IEEE854 Floating Point Standard
215  */
216 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode);
217
218 /** Get the rounding mode
219  * This function retrieves the currently used rounding mode
220  *
221  * @return The current rounding mode
222  * @see fc_set_rounding_mode()
223  */
224 fc_rounding_mode_t fc_get_rounding_mode(void);
225
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.
230  *        One bit   sign
231  *   exp_size bits  exponent + bias
232  *  mant_size bits  mantissa, without leading 1
233  *
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.
238  *
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
245  *        byte.
246  * @return 8 bits of encoded data
247  */
248 unsigned char fc_sub_bits(const fp_value *val, unsigned num_bit, unsigned byte_ofs);
249
250 /**
251  * Set the immediate precision for IEEE-754 results. Set this to
252  * 0 to get the same precision as the operands.
253  * For x87 compatibility, set this to 80.
254  *
255  * @return the old setting
256  */
257 unsigned fc_set_immediate_precision(unsigned bits);
258
259 /**
260  * Returns non-zero if the result of the last operation was exact.
261  */
262 int fc_is_exact(void);
263
264 void init_fltcalc(int precision);
265 void finish_fltcalc(void);
266
267 #endif /* FIRM_TV_FLTCALC_H */