1 /* Native implementation of soft float functions. Only a single status
2 context is supported */
6 void set_float_rounding_mode(int val STATUS_PARAM)
8 STATUS(float_rounding_mode) = val;
9 #if defined(_BSD) && !defined(__APPLE__) || (defined(HOST_SOLARIS) && HOST_SOLARIS < 10)
11 #elif defined(__arm__)
19 void set_floatx80_rounding_precision(int val STATUS_PARAM)
21 STATUS(floatx80_rounding_precision) = val;
25 #if defined(_BSD) || (defined(HOST_SOLARIS) && HOST_SOLARIS < 10)
26 #define lrint(d) ((int32_t)rint(d))
27 #define llrint(d) ((int64_t)rint(d))
28 #define lrintf(f) ((int32_t)rint(f))
29 #define llrintf(f) ((int64_t)rint(f))
30 #define sqrtf(f) ((float)sqrt(f))
31 #define remainderf(fa, fb) ((float)remainder(fa, fb))
32 #define rintf(f) ((float)rint(f))
33 #if !defined(__sparc__) && defined(HOST_SOLARIS) && HOST_SOLARIS < 10
34 extern long double rintl(long double);
35 extern long double scalbnl(long double, int);
38 llrintl(long double x) {
39 return ((long long) rintl(x));
43 lrintl(long double x) {
44 return ((long) rintl(x));
48 ldexpl(long double x, int n) {
49 return (scalbnl(x, n));
54 #if defined(_ARCH_PPC)
56 /* correct (but slow) PowerPC rint() (glibc version is incorrect) */
57 double qemu_rint(double x)
59 double y = 4503599627370496.0;
70 #define rint qemu_rint
73 /*----------------------------------------------------------------------------
74 | Software IEC/IEEE integer-to-floating-point conversion routines.
75 *----------------------------------------------------------------------------*/
76 float32 int32_to_float32(int v STATUS_PARAM)
81 float32 uint32_to_float32(unsigned int v STATUS_PARAM)
86 float64 int32_to_float64(int v STATUS_PARAM)
91 float64 uint32_to_float64(unsigned int v STATUS_PARAM)
97 floatx80 int32_to_floatx80(int v STATUS_PARAM)
102 float32 int64_to_float32( int64_t v STATUS_PARAM)
106 float32 uint64_to_float32( uint64_t v STATUS_PARAM)
110 float64 int64_to_float64( int64_t v STATUS_PARAM)
114 float64 uint64_to_float64( uint64_t v STATUS_PARAM)
119 floatx80 int64_to_floatx80( int64_t v STATUS_PARAM)
125 /* XXX: this code implements the x86 behaviour, not the IEEE one. */
126 #if HOST_LONG_BITS == 32
127 static inline int long_to_int32(long a)
132 static inline int long_to_int32(long a)
140 /*----------------------------------------------------------------------------
141 | Software IEC/IEEE single-precision conversion routines.
142 *----------------------------------------------------------------------------*/
143 int float32_to_int32( float32 a STATUS_PARAM)
145 return long_to_int32(lrintf(a));
147 int float32_to_int32_round_to_zero( float32 a STATUS_PARAM)
151 int64_t float32_to_int64( float32 a STATUS_PARAM)
156 int64_t float32_to_int64_round_to_zero( float32 a STATUS_PARAM)
161 float64 float32_to_float64( float32 a STATUS_PARAM)
166 floatx80 float32_to_floatx80( float32 a STATUS_PARAM)
172 unsigned int float32_to_uint32( float32 a STATUS_PARAM)
180 } else if (v > 0xffffffff) {
187 unsigned int float32_to_uint32_round_to_zero( float32 a STATUS_PARAM)
195 } else if (v > 0xffffffff) {
203 /*----------------------------------------------------------------------------
204 | Software IEC/IEEE single-precision operations.
205 *----------------------------------------------------------------------------*/
206 float32 float32_round_to_int( float32 a STATUS_PARAM)
211 float32 float32_rem( float32 a, float32 b STATUS_PARAM)
213 return remainderf(a, b);
216 float32 float32_sqrt( float32 a STATUS_PARAM)
220 int float32_compare( float32 a, float32 b STATUS_PARAM )
223 return float_relation_less;
225 return float_relation_equal;
227 return float_relation_greater;
229 return float_relation_unordered;
232 int float32_compare_quiet( float32 a, float32 b STATUS_PARAM )
235 return float_relation_less;
237 return float_relation_equal;
238 } else if (isgreater(a, b)) {
239 return float_relation_greater;
241 return float_relation_unordered;
244 int float32_is_signaling_nan( float32 a1)
250 return ( ( ( a>>22 ) & 0x1FF ) == 0x1FE ) && ( a & 0x003FFFFF );
253 int float32_is_nan( float32 a1 )
259 return ( 0xFF800000 < ( a<<1 ) );
262 /*----------------------------------------------------------------------------
263 | Software IEC/IEEE double-precision conversion routines.
264 *----------------------------------------------------------------------------*/
265 int float64_to_int32( float64 a STATUS_PARAM)
267 return long_to_int32(lrint(a));
269 int float64_to_int32_round_to_zero( float64 a STATUS_PARAM)
273 int64_t float64_to_int64( float64 a STATUS_PARAM)
277 int64_t float64_to_int64_round_to_zero( float64 a STATUS_PARAM)
281 float32 float64_to_float32( float64 a STATUS_PARAM)
286 floatx80 float64_to_floatx80( float64 a STATUS_PARAM)
292 float128 float64_to_float128( float64 a STATUS_PARAM)
298 unsigned int float64_to_uint32( float64 a STATUS_PARAM)
306 } else if (v > 0xffffffff) {
313 unsigned int float64_to_uint32_round_to_zero( float64 a STATUS_PARAM)
321 } else if (v > 0xffffffff) {
328 uint64_t float64_to_uint64 (float64 a STATUS_PARAM)
332 v = llrint(a + (float64)INT64_MIN);
334 return v - INT64_MIN;
336 uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM)
340 v = (int64_t)(a + (float64)INT64_MIN);
342 return v - INT64_MIN;
345 /*----------------------------------------------------------------------------
346 | Software IEC/IEEE double-precision operations.
347 *----------------------------------------------------------------------------*/
348 #if defined(__sun__) && defined(HOST_SOLARIS) && HOST_SOLARIS < 10
349 static inline float64 trunc(float64 x)
351 return x < 0 ? -floor(-x) : floor(x);
354 float64 float64_trunc_to_int( float64 a STATUS_PARAM )
359 float64 float64_round_to_int( float64 a STATUS_PARAM )
362 switch(STATUS(float_rounding_mode)) {
364 case float_round_nearest_even:
365 asm("rndd %0, %1" : "=f" (a) : "f"(a));
367 case float_round_down:
368 asm("rnddm %0, %1" : "=f" (a) : "f"(a));
371 asm("rnddp %0, %1" : "=f" (a) : "f"(a));
373 case float_round_to_zero:
374 asm("rnddz %0, %1" : "=f" (a) : "f"(a));
382 float64 float64_rem( float64 a, float64 b STATUS_PARAM)
384 return remainder(a, b);
387 float64 float64_sqrt( float64 a STATUS_PARAM)
391 int float64_compare( float64 a, float64 b STATUS_PARAM )
394 return float_relation_less;
396 return float_relation_equal;
398 return float_relation_greater;
400 return float_relation_unordered;
403 int float64_compare_quiet( float64 a, float64 b STATUS_PARAM )
406 return float_relation_less;
408 return float_relation_equal;
409 } else if (isgreater(a, b)) {
410 return float_relation_greater;
412 return float_relation_unordered;
415 int float64_is_signaling_nan( float64 a1)
422 ( ( ( a>>51 ) & 0xFFF ) == 0xFFE )
423 && ( a & LIT64( 0x0007FFFFFFFFFFFF ) );
427 int float64_is_nan( float64 a1 )
434 return ( LIT64( 0xFFF0000000000000 ) < (bits64) ( a<<1 ) );
440 /*----------------------------------------------------------------------------
441 | Software IEC/IEEE extended double-precision conversion routines.
442 *----------------------------------------------------------------------------*/
443 int floatx80_to_int32( floatx80 a STATUS_PARAM)
445 return long_to_int32(lrintl(a));
447 int floatx80_to_int32_round_to_zero( floatx80 a STATUS_PARAM)
451 int64_t floatx80_to_int64( floatx80 a STATUS_PARAM)
455 int64_t floatx80_to_int64_round_to_zero( floatx80 a STATUS_PARAM)
459 float32 floatx80_to_float32( floatx80 a STATUS_PARAM)
463 float64 floatx80_to_float64( floatx80 a STATUS_PARAM)
468 /*----------------------------------------------------------------------------
469 | Software IEC/IEEE extended double-precision operations.
470 *----------------------------------------------------------------------------*/
471 floatx80 floatx80_round_to_int( floatx80 a STATUS_PARAM)
475 floatx80 floatx80_rem( floatx80 a, floatx80 b STATUS_PARAM)
477 return remainderl(a, b);
479 floatx80 floatx80_sqrt( floatx80 a STATUS_PARAM)
483 int floatx80_compare( floatx80 a, floatx80 b STATUS_PARAM )
486 return float_relation_less;
488 return float_relation_equal;
490 return float_relation_greater;
492 return float_relation_unordered;
495 int floatx80_compare_quiet( floatx80 a, floatx80 b STATUS_PARAM )
498 return float_relation_less;
500 return float_relation_equal;
501 } else if (isgreater(a, b)) {
502 return float_relation_greater;
504 return float_relation_unordered;
507 int floatx80_is_signaling_nan( floatx80 a1)
513 aLow = u.i.low & ~ LIT64( 0x4000000000000000 );
515 ( ( u.i.high & 0x7FFF ) == 0x7FFF )
516 && (bits64) ( aLow<<1 )
517 && ( u.i.low == aLow );
520 int floatx80_is_nan( floatx80 a1 )
524 return ( ( u.i.high & 0x7FFF ) == 0x7FFF ) && (bits64) ( u.i.low<<1 );