fltcalc.h 10.4 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
2
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

Michael Beck's avatar
Michael Beck committed
20
21
22
23
24
25
/**
 * @file
 * @brief    tarval floating point calculations
 * @date     2003
 * @author   Mathias Heil
 * @version  $Id$
26
 */
Michael Beck's avatar
Michael Beck committed
27
28
#ifndef FIRM_TV_FLTCALC_H
#define FIRM_TV_FLTCALC_H
29

30
#include "firm_types.h"
31

32
#ifdef HAVE_LONG_DOUBLE
33
34
/* XXX Set this via autoconf */
#define HAVE_EXPLICIT_ONE
Michael Beck's avatar
Tarval:    
Michael Beck committed
35
36
37
38
39
typedef long double LLDBL;
#else
typedef double LLDBL;
#endif

40
enum {
Michael Beck's avatar
Michael Beck committed
41
42
43
44
	FC_DEC,
	FC_HEX,
	FC_BIN,
	FC_PACKED
45
46
};

Michael Beck's avatar
Michael Beck committed
47
/** IEEE-754 Rounding modes. */
48
typedef enum {
Michael Beck's avatar
Michael Beck committed
49
50
51
52
	FC_TONEAREST,   /**< if unsure, to the nearest even */
	FC_TOPOSITIVE,  /**< to +oo */
	FC_TONEGATIVE,  /**< to -oo */
	FC_TOZERO       /**< to 0 */
53
54
55
56
} fc_rounding_mode_t;

#define FC_DEFAULT_PRECISION 64

Michael Beck's avatar
Michael Beck committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
 * possible float states
 */
typedef enum {
	NORMAL,       /**< normal representation, implicit 1 */
	ZERO,         /**< +/-0 */
	SUBNORMAL,    /**< denormals, implicit 0 */
	INF,          /**< +/-oo */
	NAN,          /**< Not A Number */
} value_class_t;

/**
 * A descriptor for an IEEE float value.
 */
typedef struct ieee_descriptor_t {
	unsigned char exponent_size;    /**< size of exponent in bits */
	unsigned char mantissa_size;    /**< size of mantissa in bits */
	unsigned char explicit_one;     /**< set if the leading one is explicit */
	unsigned char clss;             /**< state of this float */
} ieee_descriptor_t;

78
79
struct fp_value;
typedef struct fp_value fp_value;
Michael Beck's avatar
Michael Beck committed
80

81
82
83
84
85
86
87
/*@{*/
/** internal buffer access
 * All functions that accept NULL as return buffer put their result into an
 * internal buffer.
 * @return fc_get_buffer() returns the pointer to the buffer, fc_get_buffer_length()
 * returns the size of this buffer
 */
88
const void *fc_get_buffer(void);
Michael Beck's avatar
Michael Beck committed
89
int fc_get_buffer_length(void);
90
/*}@*/
91

92
void *fc_val_from_str(const char *str, size_t len, const ieee_descriptor_t *desc, void *result);
93
94
95
96

/** get the representation of a floating point value
 * This function tries to builds a representation having the same value as the
 * float number passed.
Michael Beck's avatar
Michael Beck committed
97
 * If the wished precision is less than the precision of LLDBL the value built
98
99
100
 * will be rounded. Therefore only an approximation of the passed float can be
 * expected in this case.
 *
Michael Beck's avatar
Michael Beck committed
101
102
103
104
105
106
107
108
109
 * @param l       The floating point number to build a representation for
 * @param desc    The floating point descriptor
 * @param result  A buffer to hold the value built. If this is NULL, the internal
 *                accumulator buffer is used. Note that the buffer must be big
 *                enough to hold the value. Use fc_get_buffer_length() to find out
 *                the size needed
 *
 * @return  The result pointer passed to the function. If this was NULL this returns
 *          a pointer to the internal accumulator buffer
110
 */
Michael Beck's avatar
Michael Beck committed
111
fp_value *fc_val_from_ieee754(LLDBL l, const ieee_descriptor_t *desc, fp_value *result);
112
113
114
115
116
117
118
119

/** retrieve the float value of an internal value
 * This function casts the internal value to LLDBL and returns a LLDBL with
 * that value.
 * This implies that values of higher precision than LLDBL are subject to
 * rounding, so the returned value might not the same than the actually
 * represented value.
 *
Michael Beck's avatar
Michael Beck committed
120
121
 * @param val  The representation of a float value
 *
122
123
 * @return a float value approximating the represented value
 */
Michael Beck's avatar
Michael Beck committed
124
LLDBL fc_val_to_ieee754(const fp_value *val);
125

126
127
128
129
130
/** cast a value to another precision
 * This function changes the precision of a float representation.
 * If the new precision is less than the original precision the returned
 * value might not be the same as the original value.
 *
Michael Beck's avatar
Michael Beck committed
131
132
133
134
135
136
137
138
 * @param val     The value to be casted
 * @param desc    The floating point descriptor
 * @param result  A buffer to hold the value built. If this is NULL, the internal
 *                accumulator buffer is used. Note that the buffer must be big
 *                enough to hold the value. Use fc_get_buffer_length() to find out
 *                the size needed
 * @return  The result pointer passed to the function. If this was NULL this returns
 *          a pointer to the internal accumulator buffer
139
 */
Michael Beck's avatar
Michael Beck committed
140
fp_value *fc_cast(const fp_value *val, const ieee_descriptor_t *desc, fp_value *result);
141
142
143
144
145
146

/*@{*/
/** build a special float value
 * This function builds a representation for a special float value, as indicated by the
 * function's suffix.
 *
Michael Beck's avatar
Michael Beck committed
147
148
149
150
151
152
153
 * @param desc    The floating point descriptor
 * @param result  A buffer to hold the value built. If this is NULL, the internal
 *                accumulator buffer is used. Note that the buffer must be big
 *                enough to hold the value. Use fc_get_buffer_length() to find out
 *                the size needed
 * @return  The result pointer passed to the function. If this was NULL this returns
 *          a pointer to the internal accumulator buffer
154
 */
Michael Beck's avatar
Michael Beck committed
155
156
157
158
159
160
fp_value *fc_get_min(const ieee_descriptor_t *desc, fp_value *result);
fp_value *fc_get_max(const ieee_descriptor_t *desc, fp_value *result);
fp_value *fc_get_snan(const ieee_descriptor_t *desc, fp_value *result);
fp_value *fc_get_qnan(const ieee_descriptor_t *desc, fp_value *result);
fp_value *fc_get_plusinf(const ieee_descriptor_t *desc, fp_value *result);
fp_value *fc_get_minusinf(const ieee_descriptor_t *desc, fp_value *result);
Michael Beck's avatar
Michael Beck committed
161
/*@}*/
162

Michael Beck's avatar
Michael Beck committed
163
164
165
166
167
int fc_is_zero(const fp_value *a);
int fc_is_negative(const fp_value *a);
int fc_is_inf(const fp_value *a);
int fc_is_nan(const fp_value *a);
int fc_is_subnormal(const fp_value *a);
168

Michael Beck's avatar
Michael Beck committed
169
170
171
172
173
174
175
fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result);
fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result);
fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result);
fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result);
fp_value *fc_neg(const fp_value *a, fp_value *result);
fp_value *fc_int(const fp_value *a, fp_value *result);
fp_value *fc_rnd(const fp_value *a, fp_value *result);
176

Michael Beck's avatar
Michael Beck committed
177
char *fc_print(const fp_value *a, char *buf, int buflen, unsigned base);
178
179
180
181
182
183
184
185
186
187
188
189

/** Compare two values
 * This function compares two values
 *
 * @param a Value No. 1
 * @param b Value No. 2
 * @result The returned value will be one of
 *          -1  if a < b
 *           0  if a == b
 *           1  if a > b
 *           2  if either value is NaN
 */
Michael Beck's avatar
Michael Beck committed
190
191
int fc_comp(const fp_value *a, const fp_value *b);

192
193
194
195
196
/**
 * Converts an floating point value into an integer value.
 */
int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode);

Michael Beck's avatar
Michael Beck committed
197
198
199
200
201
202
203
204
205
/**
 * Returns non-zero if the mantissa is zero, i.e. 1.0Exxx
 */
int fc_zero_mantissa(const fp_value *value);

/**
 * Returns the exponent of a value.
 */
int fc_get_exponent(const fp_value *value);
206

207
208
209
/**
 * Return non-zero if a given value can be converted lossless into another precision.
 */
Michael Beck's avatar
Michael Beck committed
210
int fc_can_lossless_conv_to(const fp_value *value, const ieee_descriptor_t *desc);
211

212
213
214
215
216
217
/** Set new rounding mode
 * This function sets the rounding mode to one of the following, returning
 * the previously set rounding mode.
 * FC_TONEAREST (default):
 *    Any unrepresentable value is rounded to the nearest representable
 *    value. If it lies in the middle the value with the least significant
Michael Beck's avatar
Michael Beck committed
218
219
 *    bit of zero is chosen (the even one).
 *    Values too big to represent will round to +/-infinity.
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
 * FC_TONEGATIVE
 *    Any unrepresentable value is rounded towards negative infinity.
 *    Positive values too big to represent will round to the biggest
 *    representable value, negative values too small to represent will
 *    round to -infinity.
 * FC_TOPOSITIVE
 *    Any unrepresentable value is rounded towards positive infinity
 *    Negative values too small to represent will round to the biggest
 *    representable value, positive values too big to represent will
 *    round to +infinity.
 * FC_TOZERO
 *    Any unrepresentable value is rounded towards zero, effectively
 *    chopping off any bits beyond the mantissa size.
 *    Values too big to represent will round to the biggest/smallest
 *    representable value.
 *
Michael Beck's avatar
Michael Beck committed
236
 * These modes correspond to the modes required by the IEEE-754 standard.
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
 *
 * @param mode The new rounding mode. Any value other than the four
 *        defined values will have no effect.
 * @return The previous rounding mode.
 *
 * @see fc_get_rounding_mode()
 * @see IEEE754, IEEE854 Floating Point Standard
 */
fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode);

/** Get the rounding mode
 * This function retrieves the currently used rounding mode
 *
 * @return The current rounding mode
 * @see fc_set_rounding_mode()
 */
fc_rounding_mode_t fc_get_rounding_mode(void);

/** Get bit representation of a value
Michael Beck's avatar
Michael Beck committed
256
 * This function allows to read a value in encoded form, byte wise.
Michael Beck's avatar
Michael Beck committed
257
 * The value will be packed corresponding to the way used by the IEEE
258
259
260
261
262
 * encoding formats, i.e.
 *        One bit   sign
 *   exp_size bits  exponent + bias
 *  mant_size bits  mantissa, without leading 1
 *
Michael Beck's avatar
Michael Beck committed
263
 * As in IEEE, an exponent of 0 indicates a denormalized number, which
264
265
 * implies a most significant bit of zero instead of one; an exponent
 * of all ones (2**exp_size - 1) encodes infinity if the mantissa is
Michael Beck's avatar
Michael Beck committed
266
 * all zeros, else Not A Number.
267
268
269
 *
 * @param val A pointer to the value. If NULL is passed a copy of the
 *        most recent value passed to this function is used, saving the
Michael Beck's avatar
Michael Beck committed
270
 *        packing step. This behavior may be changed in the future.
271
272
273
274
275
276
 * @param num_bit The maximum number of bits to return. Any bit beyond
 *        num_bit will be returned as zero.
 * @param byte_ofs The byte index to read, 0 is the least significant
 *        byte.
 * @return 8 bits of encoded data
 */
Michael Beck's avatar
Michael Beck committed
277
unsigned char fc_sub_bits(const fp_value *val, unsigned num_bit, unsigned byte_ofs);
278

279
280
281
282
283
284
285
286
287
/**
 * Set the immediate precision for IEEE-754 results. Set this to
 * 0 to get the same precision as the operands.
 * For x87 compatibility, set this to 80.
 *
 * @return the old setting
 */
unsigned fc_set_immediate_precision(unsigned bits);

288
289
290
291
292
/**
 * Returns non-zero if the result of the last operation was exact.
 */
int fc_is_exact(void);

293
void init_fltcalc(int precision);
Michael Beck's avatar
Michael Beck committed
294
void finish_fltcalc(void);
295

Michael Beck's avatar
Michael Beck committed
296
#endif /* FIRM_TV_FLTCALC_H */