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