Guest debugging support for KVM (Jan Kiszka)
[sniper_test.git] / fpu / softfloat-native.c
blob99471b34cddd753d9814adf3552f2dc2f487b8b5
1 /* Native implementation of soft float functions. Only a single status
2 context is supported */
3 #include "softfloat.h"
4 #include <math.h>
6 void set_float_rounding_mode(int val STATUS_PARAM)
8 STATUS(float_rounding_mode) = val;
9 #if defined(HOST_BSD) && !defined(__APPLE__) || \
10 (defined(HOST_SOLARIS) && HOST_SOLARIS < 10)
11 fpsetround(val);
12 #elif defined(__arm__)
13 /* nothing to do */
14 #else
15 fesetround(val);
16 #endif
19 #ifdef FLOATX80
20 void set_floatx80_rounding_precision(int val STATUS_PARAM)
22 STATUS(floatx80_rounding_precision) = val;
24 #endif
26 #if defined(HOST_BSD) || (defined(HOST_SOLARIS) && HOST_SOLARIS < 10)
27 #define lrint(d) ((int32_t)rint(d))
28 #define llrint(d) ((int64_t)rint(d))
29 #define lrintf(f) ((int32_t)rint(f))
30 #define llrintf(f) ((int64_t)rint(f))
31 #define sqrtf(f) ((float)sqrt(f))
32 #define remainderf(fa, fb) ((float)remainder(fa, fb))
33 #define rintf(f) ((float)rint(f))
34 #if !defined(__sparc__) && defined(HOST_SOLARIS) && HOST_SOLARIS < 10
35 extern long double rintl(long double);
36 extern long double scalbnl(long double, int);
38 long long
39 llrintl(long double x) {
40 return ((long long) rintl(x));
43 long
44 lrintl(long double x) {
45 return ((long) rintl(x));
48 long double
49 ldexpl(long double x, int n) {
50 return (scalbnl(x, n));
52 #endif
53 #endif
55 #if defined(_ARCH_PPC)
57 /* correct (but slow) PowerPC rint() (glibc version is incorrect) */
58 static double qemu_rint(double x)
60 double y = 4503599627370496.0;
61 if (fabs(x) >= y)
62 return x;
63 if (x < 0)
64 y = -y;
65 y = (x + y) - y;
66 if (y == 0.0)
67 y = copysign(y, x);
68 return y;
71 #define rint qemu_rint
72 #endif
74 /*----------------------------------------------------------------------------
75 | Software IEC/IEEE integer-to-floating-point conversion routines.
76 *----------------------------------------------------------------------------*/
77 float32 int32_to_float32(int v STATUS_PARAM)
79 return (float32)v;
82 float32 uint32_to_float32(unsigned int v STATUS_PARAM)
84 return (float32)v;
87 float64 int32_to_float64(int v STATUS_PARAM)
89 return (float64)v;
92 float64 uint32_to_float64(unsigned int v STATUS_PARAM)
94 return (float64)v;
97 #ifdef FLOATX80
98 floatx80 int32_to_floatx80(int v STATUS_PARAM)
100 return (floatx80)v;
102 #endif
103 float32 int64_to_float32( int64_t v STATUS_PARAM)
105 return (float32)v;
107 float32 uint64_to_float32( uint64_t v STATUS_PARAM)
109 return (float32)v;
111 float64 int64_to_float64( int64_t v STATUS_PARAM)
113 return (float64)v;
115 float64 uint64_to_float64( uint64_t v STATUS_PARAM)
117 return (float64)v;
119 #ifdef FLOATX80
120 floatx80 int64_to_floatx80( int64_t v STATUS_PARAM)
122 return (floatx80)v;
124 #endif
126 /* XXX: this code implements the x86 behaviour, not the IEEE one. */
127 #if HOST_LONG_BITS == 32
128 static inline int long_to_int32(long a)
130 return a;
132 #else
133 static inline int long_to_int32(long a)
135 if (a != (int32_t)a)
136 a = 0x80000000;
137 return a;
139 #endif
141 /*----------------------------------------------------------------------------
142 | Software IEC/IEEE single-precision conversion routines.
143 *----------------------------------------------------------------------------*/
144 int float32_to_int32( float32 a STATUS_PARAM)
146 return long_to_int32(lrintf(a));
148 int float32_to_int32_round_to_zero( float32 a STATUS_PARAM)
150 return (int)a;
152 int64_t float32_to_int64( float32 a STATUS_PARAM)
154 return llrintf(a);
157 int64_t float32_to_int64_round_to_zero( float32 a STATUS_PARAM)
159 return (int64_t)a;
162 float64 float32_to_float64( float32 a STATUS_PARAM)
164 return a;
166 #ifdef FLOATX80
167 floatx80 float32_to_floatx80( float32 a STATUS_PARAM)
169 return a;
171 #endif
173 unsigned int float32_to_uint32( float32 a STATUS_PARAM)
175 int64_t v;
176 unsigned int res;
178 v = llrintf(a);
179 if (v < 0) {
180 res = 0;
181 } else if (v > 0xffffffff) {
182 res = 0xffffffff;
183 } else {
184 res = v;
186 return res;
188 unsigned int float32_to_uint32_round_to_zero( float32 a STATUS_PARAM)
190 int64_t v;
191 unsigned int res;
193 v = (int64_t)a;
194 if (v < 0) {
195 res = 0;
196 } else if (v > 0xffffffff) {
197 res = 0xffffffff;
198 } else {
199 res = v;
201 return res;
204 /*----------------------------------------------------------------------------
205 | Software IEC/IEEE single-precision operations.
206 *----------------------------------------------------------------------------*/
207 float32 float32_round_to_int( float32 a STATUS_PARAM)
209 return rintf(a);
212 float32 float32_rem( float32 a, float32 b STATUS_PARAM)
214 return remainderf(a, b);
217 float32 float32_sqrt( float32 a STATUS_PARAM)
219 return sqrtf(a);
221 int float32_compare( float32 a, float32 b STATUS_PARAM )
223 if (a < b) {
224 return float_relation_less;
225 } else if (a == b) {
226 return float_relation_equal;
227 } else if (a > b) {
228 return float_relation_greater;
229 } else {
230 return float_relation_unordered;
233 int float32_compare_quiet( float32 a, float32 b STATUS_PARAM )
235 if (isless(a, b)) {
236 return float_relation_less;
237 } else if (a == b) {
238 return float_relation_equal;
239 } else if (isgreater(a, b)) {
240 return float_relation_greater;
241 } else {
242 return float_relation_unordered;
245 int float32_is_signaling_nan( float32 a1)
247 float32u u;
248 uint32_t a;
249 u.f = a1;
250 a = u.i;
251 return ( ( ( a>>22 ) & 0x1FF ) == 0x1FE ) && ( a & 0x003FFFFF );
254 int float32_is_nan( float32 a1 )
256 float32u u;
257 uint64_t a;
258 u.f = a1;
259 a = u.i;
260 return ( 0xFF800000 < ( a<<1 ) );
263 /*----------------------------------------------------------------------------
264 | Software IEC/IEEE double-precision conversion routines.
265 *----------------------------------------------------------------------------*/
266 int float64_to_int32( float64 a STATUS_PARAM)
268 return long_to_int32(lrint(a));
270 int float64_to_int32_round_to_zero( float64 a STATUS_PARAM)
272 return (int)a;
274 int64_t float64_to_int64( float64 a STATUS_PARAM)
276 return llrint(a);
278 int64_t float64_to_int64_round_to_zero( float64 a STATUS_PARAM)
280 return (int64_t)a;
282 float32 float64_to_float32( float64 a STATUS_PARAM)
284 return a;
286 #ifdef FLOATX80
287 floatx80 float64_to_floatx80( float64 a STATUS_PARAM)
289 return a;
291 #endif
292 #ifdef FLOAT128
293 float128 float64_to_float128( float64 a STATUS_PARAM)
295 return a;
297 #endif
299 unsigned int float64_to_uint32( float64 a STATUS_PARAM)
301 int64_t v;
302 unsigned int res;
304 v = llrint(a);
305 if (v < 0) {
306 res = 0;
307 } else if (v > 0xffffffff) {
308 res = 0xffffffff;
309 } else {
310 res = v;
312 return res;
314 unsigned int float64_to_uint32_round_to_zero( float64 a STATUS_PARAM)
316 int64_t v;
317 unsigned int res;
319 v = (int64_t)a;
320 if (v < 0) {
321 res = 0;
322 } else if (v > 0xffffffff) {
323 res = 0xffffffff;
324 } else {
325 res = v;
327 return res;
329 uint64_t float64_to_uint64 (float64 a STATUS_PARAM)
331 int64_t v;
333 v = llrint(a + (float64)INT64_MIN);
335 return v - INT64_MIN;
337 uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM)
339 int64_t v;
341 v = (int64_t)(a + (float64)INT64_MIN);
343 return v - INT64_MIN;
346 /*----------------------------------------------------------------------------
347 | Software IEC/IEEE double-precision operations.
348 *----------------------------------------------------------------------------*/
349 #if defined(__sun__) && defined(HOST_SOLARIS) && HOST_SOLARIS < 10
350 static inline float64 trunc(float64 x)
352 return x < 0 ? -floor(-x) : floor(x);
354 #endif
355 float64 float64_trunc_to_int( float64 a STATUS_PARAM )
357 return trunc(a);
360 float64 float64_round_to_int( float64 a STATUS_PARAM )
362 #if defined(__arm__)
363 switch(STATUS(float_rounding_mode)) {
364 default:
365 case float_round_nearest_even:
366 asm("rndd %0, %1" : "=f" (a) : "f"(a));
367 break;
368 case float_round_down:
369 asm("rnddm %0, %1" : "=f" (a) : "f"(a));
370 break;
371 case float_round_up:
372 asm("rnddp %0, %1" : "=f" (a) : "f"(a));
373 break;
374 case float_round_to_zero:
375 asm("rnddz %0, %1" : "=f" (a) : "f"(a));
376 break;
378 #else
379 return rint(a);
380 #endif
383 float64 float64_rem( float64 a, float64 b STATUS_PARAM)
385 return remainder(a, b);
388 float64 float64_sqrt( float64 a STATUS_PARAM)
390 return sqrt(a);
392 int float64_compare( float64 a, float64 b STATUS_PARAM )
394 if (a < b) {
395 return float_relation_less;
396 } else if (a == b) {
397 return float_relation_equal;
398 } else if (a > b) {
399 return float_relation_greater;
400 } else {
401 return float_relation_unordered;
404 int float64_compare_quiet( float64 a, float64 b STATUS_PARAM )
406 if (isless(a, b)) {
407 return float_relation_less;
408 } else if (a == b) {
409 return float_relation_equal;
410 } else if (isgreater(a, b)) {
411 return float_relation_greater;
412 } else {
413 return float_relation_unordered;
416 int float64_is_signaling_nan( float64 a1)
418 float64u u;
419 uint64_t a;
420 u.f = a1;
421 a = u.i;
422 return
423 ( ( ( a>>51 ) & 0xFFF ) == 0xFFE )
424 && ( a & LIT64( 0x0007FFFFFFFFFFFF ) );
428 int float64_is_nan( float64 a1 )
430 float64u u;
431 uint64_t a;
432 u.f = a1;
433 a = u.i;
435 return ( LIT64( 0xFFF0000000000000 ) < (bits64) ( a<<1 ) );
439 #ifdef FLOATX80
441 /*----------------------------------------------------------------------------
442 | Software IEC/IEEE extended double-precision conversion routines.
443 *----------------------------------------------------------------------------*/
444 int floatx80_to_int32( floatx80 a STATUS_PARAM)
446 return long_to_int32(lrintl(a));
448 int floatx80_to_int32_round_to_zero( floatx80 a STATUS_PARAM)
450 return (int)a;
452 int64_t floatx80_to_int64( floatx80 a STATUS_PARAM)
454 return llrintl(a);
456 int64_t floatx80_to_int64_round_to_zero( floatx80 a STATUS_PARAM)
458 return (int64_t)a;
460 float32 floatx80_to_float32( floatx80 a STATUS_PARAM)
462 return a;
464 float64 floatx80_to_float64( floatx80 a STATUS_PARAM)
466 return a;
469 /*----------------------------------------------------------------------------
470 | Software IEC/IEEE extended double-precision operations.
471 *----------------------------------------------------------------------------*/
472 floatx80 floatx80_round_to_int( floatx80 a STATUS_PARAM)
474 return rintl(a);
476 floatx80 floatx80_rem( floatx80 a, floatx80 b STATUS_PARAM)
478 return remainderl(a, b);
480 floatx80 floatx80_sqrt( floatx80 a STATUS_PARAM)
482 return sqrtl(a);
484 int floatx80_compare( floatx80 a, floatx80 b STATUS_PARAM )
486 if (a < b) {
487 return float_relation_less;
488 } else if (a == b) {
489 return float_relation_equal;
490 } else if (a > b) {
491 return float_relation_greater;
492 } else {
493 return float_relation_unordered;
496 int floatx80_compare_quiet( floatx80 a, floatx80 b STATUS_PARAM )
498 if (isless(a, b)) {
499 return float_relation_less;
500 } else if (a == b) {
501 return float_relation_equal;
502 } else if (isgreater(a, b)) {
503 return float_relation_greater;
504 } else {
505 return float_relation_unordered;
508 int floatx80_is_signaling_nan( floatx80 a1)
510 floatx80u u;
511 uint64_t aLow;
512 u.f = a1;
514 aLow = u.i.low & ~ LIT64( 0x4000000000000000 );
515 return
516 ( ( u.i.high & 0x7FFF ) == 0x7FFF )
517 && (bits64) ( aLow<<1 )
518 && ( u.i.low == aLow );
521 int floatx80_is_nan( floatx80 a1 )
523 floatx80u u;
524 u.f = a1;
525 return ( ( u.i.high & 0x7FFF ) == 0x7FFF ) && (bits64) ( u.i.low<<1 );
528 #endif