fltcalc.h 9.01 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Michael Beck's avatar
Michael Beck committed
6
7
8
9
10
/**
 * @file
 * @brief    tarval floating point calculations
 * @date     2003
 * @author   Mathias Heil
11
 */
Michael Beck's avatar
Michael Beck committed
12
13
#ifndef FIRM_TV_FLTCALC_H
#define FIRM_TV_FLTCALC_H
14

15
#include <stdlib.h>
16
#include "firm_types.h"
17
#include "irtypes.h"
18

19
enum {
Michael Beck's avatar
Michael Beck committed
20
21
22
23
	FC_DEC,
	FC_HEX,
	FC_BIN,
	FC_PACKED
24
25
};

Michael Beck's avatar
Michael Beck committed
26
/** IEEE-754 Rounding modes. */
27
typedef enum {
Michael Beck's avatar
Michael Beck committed
28
29
30
31
	FC_TONEAREST,   /**< if unsure, to the nearest even */
	FC_TOPOSITIVE,  /**< to +oo */
	FC_TONEGATIVE,  /**< to -oo */
	FC_TOZERO       /**< to 0 */
32
33
34
35
} fc_rounding_mode_t;

#define FC_DEFAULT_PRECISION 64

Michael Beck's avatar
Michael Beck committed
36
37
38
39
/**
 * possible float states
 */
typedef enum {
40
41
42
43
44
	FC_NORMAL,       /**< normal representation, implicit 1 */
	FC_ZERO,         /**< +/-0 */
	FC_SUBNORMAL,    /**< denormals, implicit 0 */
	FC_INF,          /**< +/-oo */
	FC_NAN,          /**< Not A Number */
Michael Beck's avatar
Michael Beck committed
45
46
} value_class_t;

47
48
struct fp_value;
typedef struct fp_value fp_value;
Michael Beck's avatar
Michael Beck committed
49

50
51
52
53
54
55
56
/*@{*/
/** 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
 */
57
const void *fc_get_buffer(void);
Michael Beck's avatar
Michael Beck committed
58
int fc_get_buffer_length(void);
59
/*}@*/
60

61
void *fc_val_from_str(const char *str, size_t len, void *result);
62
63
64

/** get the representation of a floating point value
 * This function tries to builds a representation having the same value as the
65
 * long double floating point number passed.
66
 *
Michael Beck's avatar
Michael Beck committed
67
68
69
70
71
72
73
74
 * @param l       The floating point number to build a representation for
 * @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
75
 */
76
fp_value *fc_val_from_ieee754(long double l, fp_value *result);
77
78

/** retrieve the float value of an internal value
79
80
81
 * This function casts the internal value to long double and returns a
 * long double with that value.
 * This implies that values of higher precision than long double are subject to
82
83
84
 * rounding, so the returned value might not the same than the actually
 * represented value.
 *
Michael Beck's avatar
Michael Beck committed
85
86
 * @param val  The representation of a float value
 *
87
88
 * @return a float value approximating the represented value
 */
89
long double fc_val_to_ieee754(const fp_value *val);
90

91
92
93
94
95
/** 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
96
97
98
99
100
101
102
103
 * @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
104
 */
105
fp_value *fc_cast(const fp_value *val, const float_descriptor_t *desc, fp_value *result);
106
107
108
109
110
111

/*@{*/
/** 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
112
113
114
115
116
117
118
 * @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
119
 */
120
121
122
123
124
125
fp_value *fc_get_min(const float_descriptor_t *desc, fp_value *result);
fp_value *fc_get_max(const float_descriptor_t *desc, fp_value *result);
fp_value *fc_get_snan(const float_descriptor_t *desc, fp_value *result);
fp_value *fc_get_qnan(const float_descriptor_t *desc, fp_value *result);
fp_value *fc_get_plusinf(const float_descriptor_t *desc, fp_value *result);
fp_value *fc_get_minusinf(const float_descriptor_t *desc, fp_value *result);
Michael Beck's avatar
Michael Beck committed
126
/*@}*/
127

Matthias Braun's avatar
Matthias Braun committed
128
129
130
131
132
bool fc_is_zero(const fp_value *a);
bool fc_is_negative(const fp_value *a);
bool fc_is_inf(const fp_value *a);
bool fc_is_nan(const fp_value *a);
bool fc_is_subnormal(const fp_value *a);
133

Michael Beck's avatar
Michael Beck committed
134
135
136
137
138
139
140
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);
141

142
int fc_print(const fp_value *a, char *buf, int buflen, unsigned base);
143
144
145
146
147
148

/** Compare two values
 * This function compares two values
 *
 * @param a Value No. 1
 * @param b Value No. 2
149
150
 * @result The relation between a and b; either less, equal, greater or
 *         unordered.
151
 */
152
ir_relation fc_comp(const fp_value *a, const fp_value *b);
Michael Beck's avatar
Michael Beck committed
153

154
155
156
157
158
159
160
typedef enum flt2int_result_t {
	FLT2INT_OK,
	FLT2INT_POSITIVE_OVERFLOW,
	FLT2INT_NEGATIVE_OVERFLOW,
	FLT2INT_UNKNOWN
} flt2int_result_t;

161
162
163
/**
 * Converts an floating point value into an integer value.
 */
164
flt2int_result_t fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode);
165

Michael Beck's avatar
Michael Beck committed
166
167
168
/**
 * Returns non-zero if the mantissa is zero, i.e. 1.0Exxx
 */
Matthias Braun's avatar
Matthias Braun committed
169
bool fc_zero_mantissa(const fp_value *value);
Michael Beck's avatar
Michael Beck committed
170

Matthias Braun's avatar
Matthias Braun committed
171
/** Returns the exponent of a value. */
Michael Beck's avatar
Michael Beck committed
172
int fc_get_exponent(const fp_value *value);
173

174
/**
Matthias Braun's avatar
Matthias Braun committed
175
176
 * Return non-zero if a given value can be converted lossless into another
 * precision.
177
 */
Matthias Braun's avatar
Matthias Braun committed
178
179
bool fc_can_lossless_conv_to(const fp_value *value,
                             const float_descriptor_t *desc);
180

181
182
183
184
185
186
/** 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
187
188
 *    bit of zero is chosen (the even one).
 *    Values too big to represent will round to +/-infinity.
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
 * 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
205
 * These modes correspond to the modes required by the IEEE-754 standard.
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
 *
 * @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
225
 * This function allows to read a value in encoded form, byte wise.
Michael Beck's avatar
Michael Beck committed
226
 * The value will be packed corresponding to the way used by the IEEE
227
228
229
230
231
 * 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
232
 * As in IEEE, an exponent of 0 indicates a denormalized number, which
233
234
 * 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
235
 * all zeros, else Not A Number.
236
237
238
 *
 * @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
239
 *        packing step. This behavior may be changed in the future.
240
241
242
243
244
245
 * @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
246
unsigned char fc_sub_bits(const fp_value *val, unsigned num_bit, unsigned byte_ofs);
247

248
249
250
251
252
/**
 * Returns non-zero if the result of the last operation was exact.
 */
int fc_is_exact(void);

253
void init_fltcalc(int precision);
Michael Beck's avatar
Michael Beck committed
254
void finish_fltcalc(void);
255

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