Statistics
| Branch: | Revision:

root / fpu / softfloat-native.h @ 36bb244b

History | View | Annotate | Download (10.6 kB)

1 158142c2 bellard
/* Native implementation of soft float functions */
2 158142c2 bellard
#include <math.h>
3 38cfa06c bellard
4 38cfa06c bellard
#if (defined(_BSD) && !defined(__APPLE__)) || defined(HOST_SOLARIS)
5 158142c2 bellard
#include <ieeefp.h>
6 38cfa06c bellard
#define fabsf(f) ((float)fabs(f))
7 158142c2 bellard
#else
8 158142c2 bellard
#include <fenv.h>
9 158142c2 bellard
#endif
10 38cfa06c bellard
11 38cfa06c bellard
/*
12 38cfa06c bellard
 * Define some C99-7.12.3 classification macros and
13 38cfa06c bellard
 *        some C99-.12.4 for Solaris systems OS less than 10,
14 38cfa06c bellard
 *        or Solaris 10 systems running GCC 3.x or less.
15 38cfa06c bellard
 *   Solaris 10 with GCC4 does not need these macros as they
16 38cfa06c bellard
 *   are defined in <iso/math_c99.h> with a compiler directive
17 38cfa06c bellard
 */
18 38cfa06c bellard
#if defined(HOST_SOLARIS) && (( HOST_SOLARIS <= 9 ) || ( ( HOST_SOLARIS >= 10 ) && ( __GNUC__ <= 4) ))
19 38cfa06c bellard
/*
20 38cfa06c bellard
 * C99 7.12.3 classification macros
21 38cfa06c bellard
 * and
22 38cfa06c bellard
 * C99 7.12.14 comparison macros
23 38cfa06c bellard
 *
24 38cfa06c bellard
 * ... do not work on Solaris 10 using GNU CC 3.4.x.
25 38cfa06c bellard
 * Try to workaround the missing / broken C99 math macros.
26 38cfa06c bellard
 */
27 38cfa06c bellard
28 38cfa06c bellard
#define isnormal(x)             (fpclass(x) >= FP_NZERO)
29 38cfa06c bellard
#define isgreater(x, y)         ((!unordered(x, y)) && ((x) > (y)))
30 38cfa06c bellard
#define isgreaterequal(x, y)    ((!unordered(x, y)) && ((x) >= (y)))
31 38cfa06c bellard
#define isless(x, y)            ((!unordered(x, y)) && ((x) < (y)))
32 38cfa06c bellard
#define islessequal(x, y)       ((!unordered(x, y)) && ((x) <= (y)))
33 38cfa06c bellard
#define isunordered(x,y)        unordered(x, y)
34 ec530c81 bellard
#endif
35 158142c2 bellard
36 158142c2 bellard
typedef float float32;
37 158142c2 bellard
typedef double float64;
38 158142c2 bellard
#ifdef FLOATX80
39 158142c2 bellard
typedef long double floatx80;
40 158142c2 bellard
#endif
41 158142c2 bellard
42 158142c2 bellard
typedef union {
43 158142c2 bellard
    float32 f;
44 158142c2 bellard
    uint32_t i;
45 158142c2 bellard
} float32u;
46 158142c2 bellard
typedef union {
47 158142c2 bellard
    float64 f;
48 158142c2 bellard
    uint64_t i;
49 158142c2 bellard
} float64u;
50 158142c2 bellard
#ifdef FLOATX80
51 158142c2 bellard
typedef union {
52 158142c2 bellard
    floatx80 f;
53 158142c2 bellard
    struct {
54 158142c2 bellard
        uint64_t low;
55 158142c2 bellard
        uint16_t high;
56 158142c2 bellard
    } i;
57 158142c2 bellard
} floatx80u;
58 158142c2 bellard
#endif
59 158142c2 bellard
60 158142c2 bellard
/*----------------------------------------------------------------------------
61 158142c2 bellard
| Software IEC/IEEE floating-point rounding mode.
62 158142c2 bellard
*----------------------------------------------------------------------------*/
63 38cfa06c bellard
#if (defined(_BSD) && !defined(__APPLE__)) || defined(HOST_SOLARIS)
64 158142c2 bellard
enum {
65 158142c2 bellard
    float_round_nearest_even = FP_RN,
66 7918bf47 pbrook
    float_round_down         = FP_RM,
67 7918bf47 pbrook
    float_round_up           = FP_RP,
68 7918bf47 pbrook
    float_round_to_zero      = FP_RZ
69 158142c2 bellard
};
70 158142c2 bellard
#elif defined(__arm__)
71 158142c2 bellard
enum {
72 158142c2 bellard
    float_round_nearest_even = 0,
73 158142c2 bellard
    float_round_down         = 1,
74 158142c2 bellard
    float_round_up           = 2,
75 158142c2 bellard
    float_round_to_zero      = 3
76 158142c2 bellard
};
77 158142c2 bellard
#else
78 158142c2 bellard
enum {
79 158142c2 bellard
    float_round_nearest_even = FE_TONEAREST,
80 158142c2 bellard
    float_round_down         = FE_DOWNWARD,
81 158142c2 bellard
    float_round_up           = FE_UPWARD,
82 158142c2 bellard
    float_round_to_zero      = FE_TOWARDZERO
83 158142c2 bellard
};
84 158142c2 bellard
#endif
85 158142c2 bellard
86 158142c2 bellard
typedef struct float_status {
87 158142c2 bellard
    signed char float_rounding_mode;
88 158142c2 bellard
#ifdef FLOATX80
89 158142c2 bellard
    signed char floatx80_rounding_precision;
90 158142c2 bellard
#endif
91 158142c2 bellard
} float_status;
92 158142c2 bellard
93 158142c2 bellard
void set_float_rounding_mode(int val STATUS_PARAM);
94 158142c2 bellard
#ifdef FLOATX80
95 158142c2 bellard
void set_floatx80_rounding_precision(int val STATUS_PARAM);
96 158142c2 bellard
#endif
97 158142c2 bellard
98 158142c2 bellard
/*----------------------------------------------------------------------------
99 158142c2 bellard
| Software IEC/IEEE integer-to-floating-point conversion routines.
100 158142c2 bellard
*----------------------------------------------------------------------------*/
101 158142c2 bellard
float32 int32_to_float32( int STATUS_PARAM);
102 75d62a58 j_mayer
float32 uint32_to_float32( unsigned int STATUS_PARAM);
103 158142c2 bellard
float64 int32_to_float64( int STATUS_PARAM);
104 75d62a58 j_mayer
float64 uint32_to_float64( unsigned int STATUS_PARAM);
105 158142c2 bellard
#ifdef FLOATX80
106 158142c2 bellard
floatx80 int32_to_floatx80( int STATUS_PARAM);
107 158142c2 bellard
#endif
108 158142c2 bellard
#ifdef FLOAT128
109 158142c2 bellard
float128 int32_to_float128( int STATUS_PARAM);
110 158142c2 bellard
#endif
111 158142c2 bellard
float32 int64_to_float32( int64_t STATUS_PARAM);
112 75d62a58 j_mayer
float32 uint64_to_float32( uint64_t STATUS_PARAM);
113 158142c2 bellard
float64 int64_to_float64( int64_t STATUS_PARAM);
114 75d62a58 j_mayer
float64 uint64_to_float64( uint64_t v STATUS_PARAM);
115 158142c2 bellard
#ifdef FLOATX80
116 158142c2 bellard
floatx80 int64_to_floatx80( int64_t STATUS_PARAM);
117 158142c2 bellard
#endif
118 158142c2 bellard
#ifdef FLOAT128
119 158142c2 bellard
float128 int64_to_float128( int64_t STATUS_PARAM);
120 158142c2 bellard
#endif
121 158142c2 bellard
122 158142c2 bellard
/*----------------------------------------------------------------------------
123 158142c2 bellard
| Software IEC/IEEE single-precision conversion routines.
124 158142c2 bellard
*----------------------------------------------------------------------------*/
125 158142c2 bellard
int float32_to_int32( float32  STATUS_PARAM);
126 158142c2 bellard
int float32_to_int32_round_to_zero( float32  STATUS_PARAM);
127 75d62a58 j_mayer
unsigned int float32_to_uint32( float32 a STATUS_PARAM);
128 75d62a58 j_mayer
unsigned int float32_to_uint32_round_to_zero( float32 a STATUS_PARAM);
129 158142c2 bellard
int64_t float32_to_int64( float32  STATUS_PARAM);
130 158142c2 bellard
int64_t float32_to_int64_round_to_zero( float32  STATUS_PARAM);
131 158142c2 bellard
float64 float32_to_float64( float32  STATUS_PARAM);
132 158142c2 bellard
#ifdef FLOATX80
133 158142c2 bellard
floatx80 float32_to_floatx80( float32  STATUS_PARAM);
134 158142c2 bellard
#endif
135 158142c2 bellard
#ifdef FLOAT128
136 158142c2 bellard
float128 float32_to_float128( float32  STATUS_PARAM);
137 158142c2 bellard
#endif
138 158142c2 bellard
139 158142c2 bellard
/*----------------------------------------------------------------------------
140 158142c2 bellard
| Software IEC/IEEE single-precision operations.
141 158142c2 bellard
*----------------------------------------------------------------------------*/
142 158142c2 bellard
float32 float32_round_to_int( float32  STATUS_PARAM);
143 158142c2 bellard
INLINE float32 float32_add( float32 a, float32 b STATUS_PARAM)
144 158142c2 bellard
{
145 158142c2 bellard
    return a + b;
146 158142c2 bellard
}
147 158142c2 bellard
INLINE float32 float32_sub( float32 a, float32 b STATUS_PARAM)
148 158142c2 bellard
{
149 158142c2 bellard
    return a - b;
150 158142c2 bellard
}
151 158142c2 bellard
INLINE float32 float32_mul( float32 a, float32 b STATUS_PARAM)
152 158142c2 bellard
{
153 158142c2 bellard
    return a * b;
154 158142c2 bellard
}
155 158142c2 bellard
INLINE float32 float32_div( float32 a, float32 b STATUS_PARAM)
156 158142c2 bellard
{
157 158142c2 bellard
    return a / b;
158 158142c2 bellard
}
159 158142c2 bellard
float32 float32_rem( float32, float32  STATUS_PARAM);
160 158142c2 bellard
float32 float32_sqrt( float32  STATUS_PARAM);
161 750afe93 bellard
INLINE int float32_eq( float32 a, float32 b STATUS_PARAM)
162 158142c2 bellard
{
163 158142c2 bellard
    return a == b;
164 158142c2 bellard
}
165 750afe93 bellard
INLINE int float32_le( float32 a, float32 b STATUS_PARAM)
166 158142c2 bellard
{
167 158142c2 bellard
    return a <= b;
168 158142c2 bellard
}
169 750afe93 bellard
INLINE int float32_lt( float32 a, float32 b STATUS_PARAM)
170 158142c2 bellard
{
171 158142c2 bellard
    return a < b;
172 158142c2 bellard
}
173 750afe93 bellard
INLINE int float32_eq_signaling( float32 a, float32 b STATUS_PARAM)
174 158142c2 bellard
{
175 b109f9f8 bellard
    return a <= b && a >= b;
176 158142c2 bellard
}
177 750afe93 bellard
INLINE int float32_le_quiet( float32 a, float32 b STATUS_PARAM)
178 158142c2 bellard
{
179 158142c2 bellard
    return islessequal(a, b);
180 158142c2 bellard
}
181 750afe93 bellard
INLINE int float32_lt_quiet( float32 a, float32 b STATUS_PARAM)
182 158142c2 bellard
{
183 158142c2 bellard
    return isless(a, b);
184 158142c2 bellard
}
185 750afe93 bellard
INLINE int float32_unordered( float32 a, float32 b STATUS_PARAM)
186 b109f9f8 bellard
{
187 b109f9f8 bellard
    return isunordered(a, b);
188 b109f9f8 bellard
189 b109f9f8 bellard
}
190 750afe93 bellard
int float32_compare( float32, float32 STATUS_PARAM );
191 750afe93 bellard
int float32_compare_quiet( float32, float32 STATUS_PARAM );
192 750afe93 bellard
int float32_is_signaling_nan( float32 );
193 158142c2 bellard
194 158142c2 bellard
INLINE float32 float32_abs(float32 a)
195 158142c2 bellard
{
196 158142c2 bellard
    return fabsf(a);
197 158142c2 bellard
}
198 158142c2 bellard
199 158142c2 bellard
INLINE float32 float32_chs(float32 a)
200 158142c2 bellard
{
201 158142c2 bellard
    return -a;
202 158142c2 bellard
}
203 158142c2 bellard
204 158142c2 bellard
/*----------------------------------------------------------------------------
205 158142c2 bellard
| Software IEC/IEEE double-precision conversion routines.
206 158142c2 bellard
*----------------------------------------------------------------------------*/
207 158142c2 bellard
int float64_to_int32( float64 STATUS_PARAM );
208 158142c2 bellard
int float64_to_int32_round_to_zero( float64 STATUS_PARAM );
209 75d62a58 j_mayer
unsigned int float64_to_uint32( float64 STATUS_PARAM );
210 75d62a58 j_mayer
unsigned int float64_to_uint32_round_to_zero( float64 STATUS_PARAM );
211 158142c2 bellard
int64_t float64_to_int64( float64 STATUS_PARAM );
212 158142c2 bellard
int64_t float64_to_int64_round_to_zero( float64 STATUS_PARAM );
213 75d62a58 j_mayer
uint64_t float64_to_uint64( float64 STATUS_PARAM );
214 75d62a58 j_mayer
uint64_t float64_to_uint64_round_to_zero( float64 STATUS_PARAM );
215 158142c2 bellard
float32 float64_to_float32( float64 STATUS_PARAM );
216 158142c2 bellard
#ifdef FLOATX80
217 158142c2 bellard
floatx80 float64_to_floatx80( float64 STATUS_PARAM );
218 158142c2 bellard
#endif
219 158142c2 bellard
#ifdef FLOAT128
220 158142c2 bellard
float128 float64_to_float128( float64 STATUS_PARAM );
221 158142c2 bellard
#endif
222 158142c2 bellard
223 158142c2 bellard
/*----------------------------------------------------------------------------
224 158142c2 bellard
| Software IEC/IEEE double-precision operations.
225 158142c2 bellard
*----------------------------------------------------------------------------*/
226 158142c2 bellard
float64 float64_round_to_int( float64 STATUS_PARAM );
227 e6e5906b pbrook
float64 float64_trunc_to_int( float64 STATUS_PARAM );
228 158142c2 bellard
INLINE float64 float64_add( float64 a, float64 b STATUS_PARAM)
229 158142c2 bellard
{
230 158142c2 bellard
    return a + b;
231 158142c2 bellard
}
232 158142c2 bellard
INLINE float64 float64_sub( float64 a, float64 b STATUS_PARAM)
233 158142c2 bellard
{
234 158142c2 bellard
    return a - b;
235 158142c2 bellard
}
236 158142c2 bellard
INLINE float64 float64_mul( float64 a, float64 b STATUS_PARAM)
237 158142c2 bellard
{
238 158142c2 bellard
    return a * b;
239 158142c2 bellard
}
240 158142c2 bellard
INLINE float64 float64_div( float64 a, float64 b STATUS_PARAM)
241 158142c2 bellard
{
242 158142c2 bellard
    return a / b;
243 158142c2 bellard
}
244 158142c2 bellard
float64 float64_rem( float64, float64 STATUS_PARAM );
245 158142c2 bellard
float64 float64_sqrt( float64 STATUS_PARAM );
246 750afe93 bellard
INLINE int float64_eq( float64 a, float64 b STATUS_PARAM)
247 158142c2 bellard
{
248 158142c2 bellard
    return a == b;
249 158142c2 bellard
}
250 750afe93 bellard
INLINE int float64_le( float64 a, float64 b STATUS_PARAM)
251 158142c2 bellard
{
252 158142c2 bellard
    return a <= b;
253 158142c2 bellard
}
254 750afe93 bellard
INLINE int float64_lt( float64 a, float64 b STATUS_PARAM)
255 158142c2 bellard
{
256 158142c2 bellard
    return a < b;
257 158142c2 bellard
}
258 750afe93 bellard
INLINE int float64_eq_signaling( float64 a, float64 b STATUS_PARAM)
259 158142c2 bellard
{
260 b109f9f8 bellard
    return a <= b && a >= b;
261 158142c2 bellard
}
262 750afe93 bellard
INLINE int float64_le_quiet( float64 a, float64 b STATUS_PARAM)
263 158142c2 bellard
{
264 158142c2 bellard
    return islessequal(a, b);
265 158142c2 bellard
}
266 750afe93 bellard
INLINE int float64_lt_quiet( float64 a, float64 b STATUS_PARAM)
267 158142c2 bellard
{
268 158142c2 bellard
    return isless(a, b);
269 158142c2 bellard
270 158142c2 bellard
}
271 750afe93 bellard
INLINE int float64_unordered( float64 a, float64 b STATUS_PARAM)
272 b109f9f8 bellard
{
273 b109f9f8 bellard
    return isunordered(a, b);
274 b109f9f8 bellard
275 b109f9f8 bellard
}
276 750afe93 bellard
int float64_compare( float64, float64 STATUS_PARAM );
277 750afe93 bellard
int float64_compare_quiet( float64, float64 STATUS_PARAM );
278 750afe93 bellard
int float64_is_signaling_nan( float64 );
279 750afe93 bellard
int float64_is_nan( float64 );
280 158142c2 bellard
281 158142c2 bellard
INLINE float64 float64_abs(float64 a)
282 158142c2 bellard
{
283 158142c2 bellard
    return fabs(a);
284 158142c2 bellard
}
285 158142c2 bellard
286 158142c2 bellard
INLINE float64 float64_chs(float64 a)
287 158142c2 bellard
{
288 158142c2 bellard
    return -a;
289 158142c2 bellard
}
290 158142c2 bellard
291 158142c2 bellard
#ifdef FLOATX80
292 158142c2 bellard
293 158142c2 bellard
/*----------------------------------------------------------------------------
294 158142c2 bellard
| Software IEC/IEEE extended double-precision conversion routines.
295 158142c2 bellard
*----------------------------------------------------------------------------*/
296 158142c2 bellard
int floatx80_to_int32( floatx80 STATUS_PARAM );
297 158142c2 bellard
int floatx80_to_int32_round_to_zero( floatx80 STATUS_PARAM );
298 158142c2 bellard
int64_t floatx80_to_int64( floatx80 STATUS_PARAM);
299 158142c2 bellard
int64_t floatx80_to_int64_round_to_zero( floatx80 STATUS_PARAM);
300 158142c2 bellard
float32 floatx80_to_float32( floatx80 STATUS_PARAM );
301 158142c2 bellard
float64 floatx80_to_float64( floatx80 STATUS_PARAM );
302 158142c2 bellard
#ifdef FLOAT128
303 158142c2 bellard
float128 floatx80_to_float128( floatx80 STATUS_PARAM );
304 158142c2 bellard
#endif
305 158142c2 bellard
306 158142c2 bellard
/*----------------------------------------------------------------------------
307 158142c2 bellard
| Software IEC/IEEE extended double-precision operations.
308 158142c2 bellard
*----------------------------------------------------------------------------*/
309 158142c2 bellard
floatx80 floatx80_round_to_int( floatx80 STATUS_PARAM );
310 158142c2 bellard
INLINE floatx80 floatx80_add( floatx80 a, floatx80 b STATUS_PARAM)
311 158142c2 bellard
{
312 158142c2 bellard
    return a + b;
313 158142c2 bellard
}
314 158142c2 bellard
INLINE floatx80 floatx80_sub( floatx80 a, floatx80 b STATUS_PARAM)
315 158142c2 bellard
{
316 158142c2 bellard
    return a - b;
317 158142c2 bellard
}
318 158142c2 bellard
INLINE floatx80 floatx80_mul( floatx80 a, floatx80 b STATUS_PARAM)
319 158142c2 bellard
{
320 158142c2 bellard
    return a * b;
321 158142c2 bellard
}
322 158142c2 bellard
INLINE floatx80 floatx80_div( floatx80 a, floatx80 b STATUS_PARAM)
323 158142c2 bellard
{
324 158142c2 bellard
    return a / b;
325 158142c2 bellard
}
326 158142c2 bellard
floatx80 floatx80_rem( floatx80, floatx80 STATUS_PARAM );
327 158142c2 bellard
floatx80 floatx80_sqrt( floatx80 STATUS_PARAM );
328 750afe93 bellard
INLINE int floatx80_eq( floatx80 a, floatx80 b STATUS_PARAM)
329 158142c2 bellard
{
330 158142c2 bellard
    return a == b;
331 158142c2 bellard
}
332 750afe93 bellard
INLINE int floatx80_le( floatx80 a, floatx80 b STATUS_PARAM)
333 158142c2 bellard
{
334 158142c2 bellard
    return a <= b;
335 158142c2 bellard
}
336 750afe93 bellard
INLINE int floatx80_lt( floatx80 a, floatx80 b STATUS_PARAM)
337 158142c2 bellard
{
338 158142c2 bellard
    return a < b;
339 158142c2 bellard
}
340 750afe93 bellard
INLINE int floatx80_eq_signaling( floatx80 a, floatx80 b STATUS_PARAM)
341 158142c2 bellard
{
342 b109f9f8 bellard
    return a <= b && a >= b;
343 158142c2 bellard
}
344 750afe93 bellard
INLINE int floatx80_le_quiet( floatx80 a, floatx80 b STATUS_PARAM)
345 158142c2 bellard
{
346 158142c2 bellard
    return islessequal(a, b);
347 158142c2 bellard
}
348 750afe93 bellard
INLINE int floatx80_lt_quiet( floatx80 a, floatx80 b STATUS_PARAM)
349 158142c2 bellard
{
350 158142c2 bellard
    return isless(a, b);
351 158142c2 bellard
352 158142c2 bellard
}
353 750afe93 bellard
INLINE int floatx80_unordered( floatx80 a, floatx80 b STATUS_PARAM)
354 b109f9f8 bellard
{
355 b109f9f8 bellard
    return isunordered(a, b);
356 b109f9f8 bellard
357 b109f9f8 bellard
}
358 750afe93 bellard
int floatx80_compare( floatx80, floatx80 STATUS_PARAM );
359 750afe93 bellard
int floatx80_compare_quiet( floatx80, floatx80 STATUS_PARAM );
360 750afe93 bellard
int floatx80_is_signaling_nan( floatx80 );
361 158142c2 bellard
362 158142c2 bellard
INLINE floatx80 floatx80_abs(floatx80 a)
363 158142c2 bellard
{
364 158142c2 bellard
    return fabsl(a);
365 158142c2 bellard
}
366 158142c2 bellard
367 158142c2 bellard
INLINE floatx80 floatx80_chs(floatx80 a)
368 158142c2 bellard
{
369 158142c2 bellard
    return -a;
370 158142c2 bellard
}
371 158142c2 bellard
#endif