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