6 #ifdef HAVE_LONG_DOUBLE
7 /* XXX Set this via autoconf */
8 #define HAVE_EXPLICIT_ONE
9 typedef long double LLDBL;
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 */
39 #define FC_DEFAULT_PRECISION 64
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)
45 /** internal buffer access
46 * All functions that accept NULL as return buffer put their result into an
48 * @return fc_get_buffer() returns the pointer to the buffer, fc_get_buffer_length()
49 * returns the size of this buffer
51 const void *fc_get_buffer(void);
52 const int fc_get_buffer_length(void);
55 char* fc_val_from_str(const char *str, unsigned int len, char exp_size, char mant_size, char *result);
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.
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
71 * @return The result pointer passed to the function. If this was NULL this returns
72 * a pointer to the internal accumulator buffer
74 char* fc_val_from_float(LLDBL l, char exp_size, char mant_size, char *result);
76 /** retrieve the float value of an internal value
77 * This function casts the internal value to LLDBL and returns a LLDBL with
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
83 * @param val The representation of a float value
84 * @return a float value approximating the represented value
86 LLDBL fc_val_to_float(const void *val);
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.
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
100 * @return The result pointer passed to the function. If this was NULL this returns
101 * a pointer to the internal accumulator buffer
103 char* fc_cast(const void *val, char exp_size, char mant_size, char *result);
106 /** build a special float value
107 * This function builds a representation for a special float value, as indicated by the
110 * @param exponent_size The number of bits of exponent of the float type the value
112 * @param mantissa_size The number of bits of mantissa of the float type the value
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
118 * @return The result pointer passed to the function. If this was NULL this returns
119 * a pointer to the internal accumulator buffer
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);
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);
143 char *fc_print(const void *a, char *buf, int buflen, unsigned base);
145 /** Compare two values
146 * This function compares two values
148 * @param a Value No. 1
149 * @param b Value No. 2
150 * @result The returned value will be one of
154 * 2 if either value is NaN
156 int fc_comp(const void *a, const void *b);
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.
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.
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.
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.
182 * These modes correspond to the modes required by the ieee standard.
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.
188 * @see fc_get_rounding_mode()
189 * @see IEEE754, IEEE854 Floating Point Standard
191 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode);
193 /** Get the rounding mode
194 * This function retrieves the currently used rounding mode
196 * @return The current rounding mode
197 * @see fc_set_rounding_mode()
199 fc_rounding_mode_t fc_get_rounding_mode(void);
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.
206 * exp_size bits exponent + bias
207 * mant_size bits mantissa, without leading 1
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.
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
221 * @return 8 bits of encoded data
223 unsigned char fc_sub_bits(const void *val, unsigned num_bit, unsigned byte_ofs);
225 void init_fltcalc(int precision);
226 #endif /* _FLTCALC_H_ */