Typo fixed.
[libfirm] / ir / tv / fltcalc.h
1 #ifndef _FLTCALC_H_
2 #define _FLTCALC_H_
3
4 #include <config.h>
5
6 #ifdef HAVE_LONG_DOUBLE
7 /* XXX Set this via autoconf */
8 #define HAVE_EXPLICIT_ONE
9 typedef long double LLDBL;
10 #else
11 typedef double LLDBL;
12 #endif
13
14 typedef enum {
15   FC_add,   /**< addition */
16   FC_sub,   /**< subtraction */
17   FC_mul,   /**< multiplication */
18   FC_div,   /**< divide */
19   FC_neg,   /**< negate */
20   FC_int,   /**< truncate to integer */
21   FC_rnd,   /**< round to integer */
22 } fc_op_t;
23
24 enum {
25   FC_DEC,
26   FC_HEX,
27   FC_BIN,
28   FC_PACKED,
29 };
30
31 /* rounding modes */
32 typedef enum {
33   FC_TONEAREST,
34   FC_TOPOSITIVE,
35   FC_TONEGATIVE,
36   FC_TOZERO,
37 } fc_rounding_mode_t;
38
39 #define FC_DEFAULT_PRECISION 64
40
41 #define FC_DECLARE1(code) char* fc_##code(const void *a, void *result)
42 #define FC_DECLARE2(code) char* fc_##code(const void *a, const void *b, void *result)
43
44 /*@{*/
45 /** internal buffer access
46  * All functions that accept NULL as return buffer put their result into an
47  * internal buffer.
48  * @return fc_get_buffer() returns the pointer to the buffer, fc_get_buffer_length()
49  * returns the size of this buffer
50  */
51 const void *fc_get_buffer(void);
52 const int fc_get_buffer_length(void);
53 /*}@*/
54
55 char* fc_val_from_str(const char *str, unsigned int len, char exp_size, char mant_size, char *result);
56
57 /** get the representation of a floating point value
58  * This function tries to builds a representation having the same value as the
59  * float number passed.
60  * If the wished precision is less than the precicion of LLDBL the value built
61  * will be rounded. Therefore only an approximation of the passed float can be
62  * expected in this case.
63  *
64  * @param l The floating point number to build a representation for
65  * @param exp_size The number of bits of the new exponent
66  * @param mant_size The number of bits of the new mantissa
67  * @param result A buffer to hold the value built. If this is NULL, the internal
68  *               accumulator buffer is used. Note that the buffer must be big
69  *               enough to hold the value. Use fc_get_buffer_length() to find out
70  *               the size needed
71  * @return The result pointer passed to the function. If this was NULL this returns
72  *               a pointer to the internal accumulator buffer
73  */
74 char* fc_val_from_float(LLDBL l, char exp_size, char mant_size, char *result);
75
76 /** retrieve the float value of an internal value
77  * This function casts the internal value to LLDBL and returns a LLDBL with
78  * that value.
79  * This implies that values of higher precision than LLDBL are subject to
80  * rounding, so the returned value might not the same than the actually
81  * represented value.
82  *
83  * @param val The representation of a float value
84  * @return a float value approximating the represented value
85  */
86 LLDBL fc_val_to_float(const void *val);
87
88 /** cast a value to another precision
89  * This function changes the precision of a float representation.
90  * If the new precision is less than the original precision the returned
91  * value might not be the same as the original value.
92  *
93  * @param val The value to be casted
94  * @param exp_size The number of bits of the new exponent
95  * @param mant_size The number of bits of the new mantissa
96  * @param result A buffer to hold the value built. If this is NULL, the internal
97  *               accumulator buffer is used. Note that the buffer must be big
98  *               enough to hold the value. Use fc_get_buffer_length() to find out
99  *               the size needed
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 char* fc_cast(const void *val, char exp_size, char mant_size, char *result);
104
105 /*@{*/
106 /** build a special float value
107  * This function builds a representation for a special float value, as indicated by the
108  * function's suffix.
109  *
110  * @param exponent_size The number of bits of exponent of the float type the value
111  *               is created for
112  * @param mantissa_size The number of bits of mantissa of the float type the value
113  *               is created for
114  * @param result A buffer to hold the value built. If this is NULL, the internal
115  *               accumulator buffer is used. Note that the buffer must be big
116  *               enough to hold the value. Use fc_get_buffer_length() to find out
117  *               the size needed
118  * @return The result pointer passed to the function. If this was NULL this returns
119  *               a pointer to the internal accumulator buffer
120  */
121 char* fc_get_min(unsigned int exponent_size, unsigned int mantissa_size, char* result);
122 char* fc_get_max(unsigned int exponent_size, unsigned int mantissa_size, char* result);
123 char* fc_get_snan(unsigned int exponent_size, unsigned int mantissa_size, char* result);
124 char* fc_get_qnan(unsigned int exponent_size, unsigned int mantissa_size, char* result);
125 char* fc_get_plusinf(unsigned int exponent_size, unsigned int mantissa_size, char* result);
126 char* fc_get_minusinf(unsigned int exponent_size, unsigned int mantissa_size, char* result);
127 /*}@*/
128
129 int fc_is_zero(const void *a);
130 int fc_is_negative(const void *a);
131 int fc_is_inf(const void *a);
132 int fc_is_nan(const void *a);
133 int fc_is_subnormal(const void *a);
134
135 FC_DECLARE2(add);
136 FC_DECLARE2(sub);
137 FC_DECLARE2(mul);
138 FC_DECLARE2(div);
139 FC_DECLARE1(neg);
140 FC_DECLARE1(int);
141 FC_DECLARE1(rnd);
142
143 char *fc_print(const void *a, char *buf, int buflen, unsigned base);
144
145 /** Compare two values
146  * This function compares two values
147  *
148  * @param a Value No. 1
149  * @param b Value No. 2
150  * @result The returned value will be one of
151  *          -1  if a < b
152  *           0  if a == b
153  *           1  if a > b
154  *           2  if either value is NaN
155  */
156 int fc_comp(const void *a, const void *b);
157
158 /** Set new rounding mode
159  * This function sets the rounding mode to one of the following, returning
160  * the previously set rounding mode.
161  * FC_TONEAREST (default):
162  *    Any unrepresentable value is rounded to the nearest representable
163  *    value. If it lies in the middle the value with the least significant
164  *    bit of zero is chosen.
165  *    Values too big to represent will round to +-infinity.
166  * FC_TONEGATIVE
167  *    Any unrepresentable value is rounded towards negative infinity.
168  *    Positive values too big to represent will round to the biggest
169  *    representable value, negative values too small to represent will
170  *    round to -infinity.
171  * FC_TOPOSITIVE
172  *    Any unrepresentable value is rounded towards positive infinity
173  *    Negative values too small to represent will round to the biggest
174  *    representable value, positive values too big to represent will
175  *    round to +infinity.
176  * FC_TOZERO
177  *    Any unrepresentable value is rounded towards zero, effectively
178  *    chopping off any bits beyond the mantissa size.
179  *    Values too big to represent will round to the biggest/smallest
180  *    representable value.
181  *
182  * These modes correspond to the modes required by the ieee standard.
183  *
184  * @param mode The new rounding mode. Any value other than the four
185  *        defined values will have no effect.
186  * @return The previous rounding mode.
187  *
188  * @see fc_get_rounding_mode()
189  * @see IEEE754, IEEE854 Floating Point Standard
190  */
191 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode);
192
193 /** Get the rounding mode
194  * This function retrieves the currently used rounding mode
195  *
196  * @return The current rounding mode
197  * @see fc_set_rounding_mode()
198  */
199 fc_rounding_mode_t fc_get_rounding_mode(void);
200
201 /** Get bit representation of a value
202  * This function allows to read a value in encoded form, bytewise.
203  * The value will be packed corresponding to the way used by the ieee
204  * encoding formats, i.e.
205  *        One bit   sign
206  *   exp_size bits  exponent + bias
207  *  mant_size bits  mantissa, without leading 1
208  *
209  * As in ieee, an exponent of 0 indicates a denormalized number, which
210  * implies a most significant bit of zero instead of one; an exponent
211  * of all ones (2**exp_size - 1) encodes infinity if the mantissa is
212  * all zeroes, else Not A Number.
213  *
214  * @param val A pointer to the value. If NULL is passed a copy of the
215  *        most recent value passed to this function is used, saving the
216  *        packing step. This behaviour may be changed in the future.
217  * @param num_bit The maximum number of bits to return. Any bit beyond
218  *        num_bit will be returned as zero.
219  * @param byte_ofs The byte index to read, 0 is the least significant
220  *        byte.
221  * @return 8 bits of encoded data
222  */
223 unsigned char fc_sub_bits(const void *val, unsigned num_bit, unsigned byte_ofs);
224
225 void init_fltcalc(int precision);
226 #endif /* _FLTCALC_H_ */