11cb9a9bdb5699672756135559acbdd4795f0545
[libfirm] / ir / tv / fltcalc.h
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  * @version  $Id$
26  */
27 #ifndef FIRM_TV_FLTCALC_H
28 #define FIRM_TV_FLTCALC_H
29
30 #include <stdlib.h>
31 #include "firm_types.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 /**
62  * A descriptor for an IEEE float value.
63  */
64 typedef struct ieee_descriptor_t {
65         unsigned char exponent_size;    /**< size of exponent in bits */
66         unsigned char mantissa_size;    /**< size of mantissa in bits */
67         unsigned char explicit_one;     /**< set if the leading one is explicit */
68 } ieee_descriptor_t;
69
70 struct fp_value;
71 typedef struct fp_value fp_value;
72
73 /*@{*/
74 /** internal buffer access
75  * All functions that accept NULL as return buffer put their result into an
76  * internal buffer.
77  * @return fc_get_buffer() returns the pointer to the buffer, fc_get_buffer_length()
78  * returns the size of this buffer
79  */
80 const void *fc_get_buffer(void);
81 int fc_get_buffer_length(void);
82 /*}@*/
83
84 void *fc_val_from_str(const char *str, size_t len, const ieee_descriptor_t *desc, void *result);
85
86 /** get the representation of a floating point value
87  * This function tries to builds a representation having the same value as the
88  * float number passed.
89  * If the wished precision is less than the precision of long double the value
90  * built will be rounded. Therefore only an approximation of the passed float
91  * can be expected in this case.
92  *
93  * @param l       The floating point number to build a representation for
94  * @param desc    The floating point descriptor
95  * @param result  A buffer to hold the value built. If this is NULL, the internal
96  *                accumulator buffer is used. Note that the buffer must be big
97  *                enough to hold the value. Use fc_get_buffer_length() to find out
98  *                the size needed
99  *
100  * @return  The result pointer passed to the function. If this was NULL this returns
101  *          a pointer to the internal accumulator buffer
102  */
103 fp_value *fc_val_from_ieee754(long double l, const ieee_descriptor_t *desc,
104                               fp_value *result);
105
106 /** retrieve the float value of an internal value
107  * This function casts the internal value to long double and returns a
108  * long double with that value.
109  * This implies that values of higher precision than long double are subject to
110  * rounding, so the returned value might not the same than the actually
111  * represented value.
112  *
113  * @param val  The representation of a float value
114  *
115  * @return a float value approximating the represented value
116  */
117 long double fc_val_to_ieee754(const fp_value *val);
118
119 /** cast a value to another precision
120  * This function changes the precision of a float representation.
121  * If the new precision is less than the original precision the returned
122  * value might not be the same as the original value.
123  *
124  * @param val     The value to be casted
125  * @param desc    The floating point descriptor
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
129  *                the size needed
130  * @return  The result pointer passed to the function. If this was NULL this returns
131  *          a pointer to the internal accumulator buffer
132  */
133 fp_value *fc_cast(const fp_value *val, const ieee_descriptor_t *desc, fp_value *result);
134
135 /*@{*/
136 /** build a special float value
137  * This function builds a representation for a special float value, as indicated by the
138  * function's suffix.
139  *
140  * @param desc    The floating point descriptor
141  * @param result  A buffer to hold the value built. If this is NULL, the internal
142  *                accumulator buffer is used. Note that the buffer must be big
143  *                enough to hold the value. Use fc_get_buffer_length() to find out
144  *                the size needed
145  * @return  The result pointer passed to the function. If this was NULL this returns
146  *          a pointer to the internal accumulator buffer
147  */
148 fp_value *fc_get_min(const ieee_descriptor_t *desc, fp_value *result);
149 fp_value *fc_get_max(const ieee_descriptor_t *desc, fp_value *result);
150 fp_value *fc_get_snan(const ieee_descriptor_t *desc, fp_value *result);
151 fp_value *fc_get_qnan(const ieee_descriptor_t *desc, fp_value *result);
152 fp_value *fc_get_plusinf(const ieee_descriptor_t *desc, fp_value *result);
153 fp_value *fc_get_minusinf(const ieee_descriptor_t *desc, fp_value *result);
154 /*@}*/
155
156 int fc_is_zero(const fp_value *a);
157 int fc_is_negative(const fp_value *a);
158 int fc_is_inf(const fp_value *a);
159 int fc_is_nan(const fp_value *a);
160 int fc_is_subnormal(const fp_value *a);
161
162 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result);
163 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result);
164 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result);
165 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result);
166 fp_value *fc_neg(const fp_value *a, fp_value *result);
167 fp_value *fc_int(const fp_value *a, fp_value *result);
168 fp_value *fc_rnd(const fp_value *a, fp_value *result);
169
170 char *fc_print(const fp_value *a, char *buf, int buflen, unsigned base);
171
172 /** Compare two values
173  * This function compares two values
174  *
175  * @param a Value No. 1
176  * @param b Value No. 2
177  * @result The returned value will be one of
178  *          -1  if a < b
179  *           0  if a == b
180  *           1  if a > b
181  *           2  if either value is NaN
182  */
183 int fc_comp(const fp_value *a, const fp_value *b);
184
185 /**
186  * Converts an floating point value into an integer value.
187  */
188 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode);
189
190 /**
191  * Returns non-zero if the mantissa is zero, i.e. 1.0Exxx
192  */
193 int fc_zero_mantissa(const fp_value *value);
194
195 /**
196  * Returns the exponent of a value.
197  */
198 int fc_get_exponent(const fp_value *value);
199
200 /**
201  * Return non-zero if a given value can be converted lossless into another precision.
202  */
203 int fc_can_lossless_conv_to(const fp_value *value, const ieee_descriptor_t *desc);
204
205 /** Set new rounding mode
206  * This function sets the rounding mode to one of the following, returning
207  * the previously set rounding mode.
208  * FC_TONEAREST (default):
209  *    Any unrepresentable value is rounded to the nearest representable
210  *    value. If it lies in the middle the value with the least significant
211  *    bit of zero is chosen (the even one).
212  *    Values too big to represent will round to +/-infinity.
213  * FC_TONEGATIVE
214  *    Any unrepresentable value is rounded towards negative infinity.
215  *    Positive values too big to represent will round to the biggest
216  *    representable value, negative values too small to represent will
217  *    round to -infinity.
218  * FC_TOPOSITIVE
219  *    Any unrepresentable value is rounded towards positive infinity
220  *    Negative values too small to represent will round to the biggest
221  *    representable value, positive values too big to represent will
222  *    round to +infinity.
223  * FC_TOZERO
224  *    Any unrepresentable value is rounded towards zero, effectively
225  *    chopping off any bits beyond the mantissa size.
226  *    Values too big to represent will round to the biggest/smallest
227  *    representable value.
228  *
229  * These modes correspond to the modes required by the IEEE-754 standard.
230  *
231  * @param mode The new rounding mode. Any value other than the four
232  *        defined values will have no effect.
233  * @return The previous rounding mode.
234  *
235  * @see fc_get_rounding_mode()
236  * @see IEEE754, IEEE854 Floating Point Standard
237  */
238 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode);
239
240 /** Get the rounding mode
241  * This function retrieves the currently used rounding mode
242  *
243  * @return The current rounding mode
244  * @see fc_set_rounding_mode()
245  */
246 fc_rounding_mode_t fc_get_rounding_mode(void);
247
248 /** Get bit representation of a value
249  * This function allows to read a value in encoded form, byte wise.
250  * The value will be packed corresponding to the way used by the IEEE
251  * encoding formats, i.e.
252  *        One bit   sign
253  *   exp_size bits  exponent + bias
254  *  mant_size bits  mantissa, without leading 1
255  *
256  * As in IEEE, an exponent of 0 indicates a denormalized number, which
257  * implies a most significant bit of zero instead of one; an exponent
258  * of all ones (2**exp_size - 1) encodes infinity if the mantissa is
259  * all zeros, else Not A Number.
260  *
261  * @param val A pointer to the value. If NULL is passed a copy of the
262  *        most recent value passed to this function is used, saving the
263  *        packing step. This behavior may be changed in the future.
264  * @param num_bit The maximum number of bits to return. Any bit beyond
265  *        num_bit will be returned as zero.
266  * @param byte_ofs The byte index to read, 0 is the least significant
267  *        byte.
268  * @return 8 bits of encoded data
269  */
270 unsigned char fc_sub_bits(const fp_value *val, unsigned num_bit, unsigned byte_ofs);
271
272 /**
273  * Set the immediate precision for IEEE-754 results. Set this to
274  * 0 to get the same precision as the operands.
275  * For x87 compatibility, set this to 80.
276  *
277  * @return the old setting
278  */
279 unsigned fc_set_immediate_precision(unsigned bits);
280
281 /**
282  * Returns non-zero if the result of the last operation was exact.
283  */
284 int fc_is_exact(void);
285
286 void init_fltcalc(int precision);
287 void finish_fltcalc(void);
288
289 #endif /* FIRM_TV_FLTCALC_H */