qdev: add creation function that may fail
[qemu/lumag.git] / fpu / softfloat-native.c
blob008bb53cec2f8ccca7cbeb78d91d0e19feb08047
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 /*----------------------------------------------------------------------------
267 | Software IEC/IEEE double-precision conversion routines.
268 *----------------------------------------------------------------------------*/
269 int float64_to_int32( float64 a STATUS_PARAM)
271 return long_to_int32(lrint(a));
273 int float64_to_int32_round_to_zero( float64 a STATUS_PARAM)
275 return (int)a;
277 int64_t float64_to_int64( float64 a STATUS_PARAM)
279 return llrint(a);
281 int64_t float64_to_int64_round_to_zero( float64 a STATUS_PARAM)
283 return (int64_t)a;
285 float32 float64_to_float32( float64 a STATUS_PARAM)
287 return a;
289 #ifdef FLOATX80
290 floatx80 float64_to_floatx80( float64 a STATUS_PARAM)
292 return a;
294 #endif
295 #ifdef FLOAT128
296 float128 float64_to_float128( float64 a STATUS_PARAM)
298 return a;
300 #endif
302 unsigned int float64_to_uint32( float64 a STATUS_PARAM)
304 int64_t v;
305 unsigned int res;
307 v = llrint(a);
308 if (v < 0) {
309 res = 0;
310 } else if (v > 0xffffffff) {
311 res = 0xffffffff;
312 } else {
313 res = v;
315 return res;
317 unsigned int float64_to_uint32_round_to_zero( float64 a STATUS_PARAM)
319 int64_t v;
320 unsigned int res;
322 v = (int64_t)a;
323 if (v < 0) {
324 res = 0;
325 } else if (v > 0xffffffff) {
326 res = 0xffffffff;
327 } else {
328 res = v;
330 return res;
332 uint64_t float64_to_uint64 (float64 a STATUS_PARAM)
334 int64_t v;
336 v = llrint(a + (float64)INT64_MIN);
338 return v - INT64_MIN;
340 uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM)
342 int64_t v;
344 v = (int64_t)(a + (float64)INT64_MIN);
346 return v - INT64_MIN;
349 /*----------------------------------------------------------------------------
350 | Software IEC/IEEE double-precision operations.
351 *----------------------------------------------------------------------------*/
352 #if defined(__sun__) && \
353 (defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10)
354 static inline float64 trunc(float64 x)
356 return x < 0 ? -floor(-x) : floor(x);
358 #endif
359 float64 float64_trunc_to_int( float64 a STATUS_PARAM )
361 return trunc(a);
364 float64 float64_round_to_int( float64 a STATUS_PARAM )
366 return rint(a);
369 float64 float64_rem( float64 a, float64 b STATUS_PARAM)
371 return remainder(a, b);
374 float64 float64_sqrt( float64 a STATUS_PARAM)
376 return sqrt(a);
378 int float64_compare( float64 a, float64 b STATUS_PARAM )
380 if (a < b) {
381 return float_relation_less;
382 } else if (a == b) {
383 return float_relation_equal;
384 } else if (a > b) {
385 return float_relation_greater;
386 } else {
387 return float_relation_unordered;
390 int float64_compare_quiet( float64 a, float64 b STATUS_PARAM )
392 if (isless(a, b)) {
393 return float_relation_less;
394 } else if (a == b) {
395 return float_relation_equal;
396 } else if (isgreater(a, b)) {
397 return float_relation_greater;
398 } else {
399 return float_relation_unordered;
402 int float64_is_signaling_nan( float64 a1)
404 float64u u;
405 uint64_t a;
406 u.f = a1;
407 a = u.i;
408 return
409 ( ( ( a>>51 ) & 0xFFF ) == 0xFFE )
410 && ( a & LIT64( 0x0007FFFFFFFFFFFF ) );
414 int float64_is_quiet_nan( float64 a1 )
416 float64u u;
417 uint64_t a;
418 u.f = a1;
419 a = u.i;
421 return ( LIT64( 0xFFF0000000000000 ) < (bits64) ( a<<1 ) );
425 #ifdef FLOATX80
427 /*----------------------------------------------------------------------------
428 | Software IEC/IEEE extended double-precision conversion routines.
429 *----------------------------------------------------------------------------*/
430 int floatx80_to_int32( floatx80 a STATUS_PARAM)
432 return long_to_int32(lrintl(a));
434 int floatx80_to_int32_round_to_zero( floatx80 a STATUS_PARAM)
436 return (int)a;
438 int64_t floatx80_to_int64( floatx80 a STATUS_PARAM)
440 return llrintl(a);
442 int64_t floatx80_to_int64_round_to_zero( floatx80 a STATUS_PARAM)
444 return (int64_t)a;
446 float32 floatx80_to_float32( floatx80 a STATUS_PARAM)
448 return a;
450 float64 floatx80_to_float64( floatx80 a STATUS_PARAM)
452 return a;
455 /*----------------------------------------------------------------------------
456 | Software IEC/IEEE extended double-precision operations.
457 *----------------------------------------------------------------------------*/
458 floatx80 floatx80_round_to_int( floatx80 a STATUS_PARAM)
460 return rintl(a);
462 floatx80 floatx80_rem( floatx80 a, floatx80 b STATUS_PARAM)
464 return remainderl(a, b);
466 floatx80 floatx80_sqrt( floatx80 a STATUS_PARAM)
468 return sqrtl(a);
470 int floatx80_compare( floatx80 a, floatx80 b STATUS_PARAM )
472 if (a < b) {
473 return float_relation_less;
474 } else if (a == b) {
475 return float_relation_equal;
476 } else if (a > b) {
477 return float_relation_greater;
478 } else {
479 return float_relation_unordered;
482 int floatx80_compare_quiet( floatx80 a, floatx80 b STATUS_PARAM )
484 if (isless(a, b)) {
485 return float_relation_less;
486 } else if (a == b) {
487 return float_relation_equal;
488 } else if (isgreater(a, b)) {
489 return float_relation_greater;
490 } else {
491 return float_relation_unordered;
494 int floatx80_is_signaling_nan( floatx80 a1)
496 floatx80u u;
497 uint64_t aLow;
498 u.f = a1;
500 aLow = u.i.low & ~ LIT64( 0x4000000000000000 );
501 return
502 ( ( u.i.high & 0x7FFF ) == 0x7FFF )
503 && (bits64) ( aLow<<1 )
504 && ( u.i.low == aLow );
507 int floatx80_is_quiet_nan( floatx80 a1 )
509 floatx80u u;
510 u.f = a1;
511 return ( ( u.i.high & 0x7FFF ) == 0x7FFF ) && (bits64) ( u.i.low<<1 );
514 #endif