scsi-generic: Remove bogus double complete
[qemu.git] / fpu / softfloat-native.c
blob88486511ee23c7042f93cd918edf54b5e6320e9c
1 /* Native implementation of soft float functions. Only a single status
2 context is supported */
3 #include "softfloat.h"
4 #include <math.h>
5 #if defined(CONFIG_SOLARIS)
6 #include <fenv.h>
7 #endif
9 void set_float_rounding_mode(int val STATUS_PARAM)
11 STATUS(float_rounding_mode) = val;
12 #if (defined(CONFIG_BSD) && !defined(__APPLE__) && !defined(__GLIBC__)) || \
13 (defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10)
14 fpsetround(val);
15 #else
16 fesetround(val);
17 #endif
20 #ifdef FLOATX80
21 void set_floatx80_rounding_precision(int val STATUS_PARAM)
23 STATUS(floatx80_rounding_precision) = val;
25 #endif
27 #if defined(CONFIG_BSD) || \
28 (defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10)
29 #define lrint(d) ((int32_t)rint(d))
30 #define llrint(d) ((int64_t)rint(d))
31 #define lrintf(f) ((int32_t)rint(f))
32 #define llrintf(f) ((int64_t)rint(f))
33 #define sqrtf(f) ((float)sqrt(f))
34 #define remainderf(fa, fb) ((float)remainder(fa, fb))
35 #define rintf(f) ((float)rint(f))
36 #if !defined(__sparc__) && \
37 (defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10)
38 extern long double rintl(long double);
39 extern long double scalbnl(long double, int);
41 long long
42 llrintl(long double x) {
43 return ((long long) rintl(x));
46 long
47 lrintl(long double x) {
48 return ((long) rintl(x));
51 long double
52 ldexpl(long double x, int n) {
53 return (scalbnl(x, n));
55 #endif
56 #endif
58 #if defined(_ARCH_PPC)
60 /* correct (but slow) PowerPC rint() (glibc version is incorrect) */
61 static double qemu_rint(double x)
63 double y = 4503599627370496.0;
64 if (fabs(x) >= y)
65 return x;
66 if (x < 0)
67 y = -y;
68 y = (x + y) - y;
69 if (y == 0.0)
70 y = copysign(y, x);
71 return y;
74 #define rint qemu_rint
75 #endif
77 /*----------------------------------------------------------------------------
78 | Software IEC/IEEE integer-to-floating-point conversion routines.
79 *----------------------------------------------------------------------------*/
80 float32 int32_to_float32(int v STATUS_PARAM)
82 return (float32)v;
85 float32 uint32_to_float32(unsigned int v STATUS_PARAM)
87 return (float32)v;
90 float64 int32_to_float64(int v STATUS_PARAM)
92 return (float64)v;
95 float64 uint32_to_float64(unsigned int v STATUS_PARAM)
97 return (float64)v;
100 #ifdef FLOATX80
101 floatx80 int32_to_floatx80(int v STATUS_PARAM)
103 return (floatx80)v;
105 #endif
106 float32 int64_to_float32( int64_t v STATUS_PARAM)
108 return (float32)v;
110 float32 uint64_to_float32( uint64_t v STATUS_PARAM)
112 return (float32)v;
114 float64 int64_to_float64( int64_t v STATUS_PARAM)
116 return (float64)v;
118 float64 uint64_to_float64( uint64_t v STATUS_PARAM)
120 return (float64)v;
122 #ifdef FLOATX80
123 floatx80 int64_to_floatx80( int64_t v STATUS_PARAM)
125 return (floatx80)v;
127 #endif
129 /* XXX: this code implements the x86 behaviour, not the IEEE one. */
130 #if HOST_LONG_BITS == 32
131 static inline int long_to_int32(long a)
133 return a;
135 #else
136 static inline int long_to_int32(long a)
138 if (a != (int32_t)a)
139 a = 0x80000000;
140 return a;
142 #endif
144 /*----------------------------------------------------------------------------
145 | Software IEC/IEEE single-precision conversion routines.
146 *----------------------------------------------------------------------------*/
147 int float32_to_int32( float32 a STATUS_PARAM)
149 return long_to_int32(lrintf(a));
151 int float32_to_int32_round_to_zero( float32 a STATUS_PARAM)
153 return (int)a;
155 int64_t float32_to_int64( float32 a STATUS_PARAM)
157 return llrintf(a);
160 int64_t float32_to_int64_round_to_zero( float32 a STATUS_PARAM)
162 return (int64_t)a;
165 float64 float32_to_float64( float32 a STATUS_PARAM)
167 return a;
169 #ifdef FLOATX80
170 floatx80 float32_to_floatx80( float32 a STATUS_PARAM)
172 return a;
174 #endif
176 unsigned int float32_to_uint32( float32 a STATUS_PARAM)
178 int64_t v;
179 unsigned int res;
181 v = llrintf(a);
182 if (v < 0) {
183 res = 0;
184 } else if (v > 0xffffffff) {
185 res = 0xffffffff;
186 } else {
187 res = v;
189 return res;
191 unsigned int float32_to_uint32_round_to_zero( float32 a STATUS_PARAM)
193 int64_t v;
194 unsigned int res;
196 v = (int64_t)a;
197 if (v < 0) {
198 res = 0;
199 } else if (v > 0xffffffff) {
200 res = 0xffffffff;
201 } else {
202 res = v;
204 return res;
207 /*----------------------------------------------------------------------------
208 | Software IEC/IEEE single-precision operations.
209 *----------------------------------------------------------------------------*/
210 float32 float32_round_to_int( float32 a STATUS_PARAM)
212 return rintf(a);
215 float32 float32_rem( float32 a, float32 b STATUS_PARAM)
217 return remainderf(a, b);
220 float32 float32_sqrt( float32 a STATUS_PARAM)
222 return sqrtf(a);
224 int float32_compare( float32 a, float32 b STATUS_PARAM )
226 if (a < b) {
227 return float_relation_less;
228 } else if (a == b) {
229 return float_relation_equal;
230 } else if (a > b) {
231 return float_relation_greater;
232 } else {
233 return float_relation_unordered;
236 int float32_compare_quiet( float32 a, float32 b STATUS_PARAM )
238 if (isless(a, b)) {
239 return float_relation_less;
240 } else if (a == b) {
241 return float_relation_equal;
242 } else if (isgreater(a, b)) {
243 return float_relation_greater;
244 } else {
245 return float_relation_unordered;
248 int float32_is_signaling_nan( float32 a1)
250 float32u u;
251 uint32_t a;
252 u.f = a1;
253 a = u.i;
254 return ( ( ( a>>22 ) & 0x1FF ) == 0x1FE ) && ( a & 0x003FFFFF );
257 int float32_is_quiet_nan( float32 a1 )
259 float32u u;
260 uint64_t a;
261 u.f = a1;
262 a = u.i;
263 return ( 0xFF800000 < ( a<<1 ) );
266 int float32_is_any_nan( float32 a1 )
268 float32u u;
269 uint32_t a;
270 u.f = a1;
271 a = u.i;
272 return (a & ~(1 << 31)) > 0x7f800000U;
275 /*----------------------------------------------------------------------------
276 | Software IEC/IEEE double-precision conversion routines.
277 *----------------------------------------------------------------------------*/
278 int float64_to_int32( float64 a STATUS_PARAM)
280 return long_to_int32(lrint(a));
282 int float64_to_int32_round_to_zero( float64 a STATUS_PARAM)
284 return (int)a;
286 int64_t float64_to_int64( float64 a STATUS_PARAM)
288 return llrint(a);
290 int64_t float64_to_int64_round_to_zero( float64 a STATUS_PARAM)
292 return (int64_t)a;
294 float32 float64_to_float32( float64 a STATUS_PARAM)
296 return a;
298 #ifdef FLOATX80
299 floatx80 float64_to_floatx80( float64 a STATUS_PARAM)
301 return a;
303 #endif
304 #ifdef FLOAT128
305 float128 float64_to_float128( float64 a STATUS_PARAM)
307 return a;
309 #endif
311 unsigned int float64_to_uint32( float64 a STATUS_PARAM)
313 int64_t v;
314 unsigned int res;
316 v = llrint(a);
317 if (v < 0) {
318 res = 0;
319 } else if (v > 0xffffffff) {
320 res = 0xffffffff;
321 } else {
322 res = v;
324 return res;
326 unsigned int float64_to_uint32_round_to_zero( float64 a STATUS_PARAM)
328 int64_t v;
329 unsigned int res;
331 v = (int64_t)a;
332 if (v < 0) {
333 res = 0;
334 } else if (v > 0xffffffff) {
335 res = 0xffffffff;
336 } else {
337 res = v;
339 return res;
341 uint64_t float64_to_uint64 (float64 a STATUS_PARAM)
343 int64_t v;
345 v = llrint(a + (float64)INT64_MIN);
347 return v - INT64_MIN;
349 uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM)
351 int64_t v;
353 v = (int64_t)(a + (float64)INT64_MIN);
355 return v - INT64_MIN;
358 /*----------------------------------------------------------------------------
359 | Software IEC/IEEE double-precision operations.
360 *----------------------------------------------------------------------------*/
361 #if defined(__sun__) && \
362 (defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10)
363 static inline float64 trunc(float64 x)
365 return x < 0 ? -floor(-x) : floor(x);
367 #endif
368 float64 float64_trunc_to_int( float64 a STATUS_PARAM )
370 return trunc(a);
373 float64 float64_round_to_int( float64 a STATUS_PARAM )
375 return rint(a);
378 float64 float64_rem( float64 a, float64 b STATUS_PARAM)
380 return remainder(a, b);
383 float64 float64_sqrt( float64 a STATUS_PARAM)
385 return sqrt(a);
387 int float64_compare( float64 a, float64 b STATUS_PARAM )
389 if (a < b) {
390 return float_relation_less;
391 } else if (a == b) {
392 return float_relation_equal;
393 } else if (a > b) {
394 return float_relation_greater;
395 } else {
396 return float_relation_unordered;
399 int float64_compare_quiet( float64 a, float64 b STATUS_PARAM )
401 if (isless(a, b)) {
402 return float_relation_less;
403 } else if (a == b) {
404 return float_relation_equal;
405 } else if (isgreater(a, b)) {
406 return float_relation_greater;
407 } else {
408 return float_relation_unordered;
411 int float64_is_signaling_nan( float64 a1)
413 float64u u;
414 uint64_t a;
415 u.f = a1;
416 a = u.i;
417 return
418 ( ( ( a>>51 ) & 0xFFF ) == 0xFFE )
419 && ( a & LIT64( 0x0007FFFFFFFFFFFF ) );
423 int float64_is_quiet_nan( float64 a1 )
425 float64u u;
426 uint64_t a;
427 u.f = a1;
428 a = u.i;
430 return ( LIT64( 0xFFF0000000000000 ) < (uint64_t) ( a<<1 ) );
434 int float64_is_any_nan( float64 a1 )
436 float64u u;
437 uint64_t a;
438 u.f = a1;
439 a = u.i;
441 return (a & ~(1ULL << 63)) > LIT64 (0x7FF0000000000000 );
444 #ifdef FLOATX80
446 /*----------------------------------------------------------------------------
447 | Software IEC/IEEE extended double-precision conversion routines.
448 *----------------------------------------------------------------------------*/
449 int floatx80_to_int32( floatx80 a STATUS_PARAM)
451 return long_to_int32(lrintl(a));
453 int floatx80_to_int32_round_to_zero( floatx80 a STATUS_PARAM)
455 return (int)a;
457 int64_t floatx80_to_int64( floatx80 a STATUS_PARAM)
459 return llrintl(a);
461 int64_t floatx80_to_int64_round_to_zero( floatx80 a STATUS_PARAM)
463 return (int64_t)a;
465 float32 floatx80_to_float32( floatx80 a STATUS_PARAM)
467 return a;
469 float64 floatx80_to_float64( floatx80 a STATUS_PARAM)
471 return a;
474 /*----------------------------------------------------------------------------
475 | Software IEC/IEEE extended double-precision operations.
476 *----------------------------------------------------------------------------*/
477 floatx80 floatx80_round_to_int( floatx80 a STATUS_PARAM)
479 return rintl(a);
481 floatx80 floatx80_rem( floatx80 a, floatx80 b STATUS_PARAM)
483 return remainderl(a, b);
485 floatx80 floatx80_sqrt( floatx80 a STATUS_PARAM)
487 return sqrtl(a);
489 int floatx80_compare( floatx80 a, floatx80 b STATUS_PARAM )
491 if (a < b) {
492 return float_relation_less;
493 } else if (a == b) {
494 return float_relation_equal;
495 } else if (a > b) {
496 return float_relation_greater;
497 } else {
498 return float_relation_unordered;
501 int floatx80_compare_quiet( floatx80 a, floatx80 b STATUS_PARAM )
503 if (isless(a, b)) {
504 return float_relation_less;
505 } else if (a == b) {
506 return float_relation_equal;
507 } else if (isgreater(a, b)) {
508 return float_relation_greater;
509 } else {
510 return float_relation_unordered;
513 int floatx80_is_signaling_nan( floatx80 a1)
515 floatx80u u;
516 uint64_t aLow;
517 u.f = a1;
519 aLow = u.i.low & ~ LIT64( 0x4000000000000000 );
520 return
521 ( ( u.i.high & 0x7FFF ) == 0x7FFF )
522 && (uint64_t) ( aLow<<1 )
523 && ( u.i.low == aLow );
526 int floatx80_is_quiet_nan( floatx80 a1 )
528 floatx80u u;
529 u.f = a1;
530 return ( ( u.i.high & 0x7FFF ) == 0x7FFF ) && (uint64_t) ( u.i.low<<1 );
533 int floatx80_is_any_nan( floatx80 a1 )
535 floatx80u u;
536 u.f = a1;
537 return ((u.i.high & 0x7FFF) == 0x7FFF) && ( u.i.low<<1 );
540 #endif