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
|
---|
8 |
|
---|
9 | void set_float_rounding_mode(int val STATUS_PARAM)
|
---|
10 | {
|
---|
11 | STATUS(float_rounding_mode) = val;
|
---|
12 | #if (defined(CONFIG_BSD) && !defined(__APPLE__) && !defined(__GLIBC__)) || \
|
---|
13 | (defined(CONFIG_SOLARIS) && (CONFIG_SOLARIS_VERSION < 10 || CONFIG_SOLARIS_VERSION == 11)) /* VBOX adds sol 11 */
|
---|
14 | fpsetround(val);
|
---|
15 | #elif defined(__arm__)
|
---|
16 | /* nothing to do */
|
---|
17 | #else
|
---|
18 | fesetround(val);
|
---|
19 | #endif
|
---|
20 | }
|
---|
21 |
|
---|
22 | #ifdef FLOATX80
|
---|
23 | void set_floatx80_rounding_precision(int val STATUS_PARAM)
|
---|
24 | {
|
---|
25 | STATUS(floatx80_rounding_precision) = val;
|
---|
26 | }
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | #if defined(CONFIG_BSD) || \
|
---|
30 | (defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10)
|
---|
31 | #define lrint(d) ((int32_t)rint(d))
|
---|
32 | #define llrint(d) ((int64_t)rint(d))
|
---|
33 | #define lrintf(f) ((int32_t)rint(f))
|
---|
34 | #define llrintf(f) ((int64_t)rint(f))
|
---|
35 | #define sqrtf(f) ((float)sqrt(f))
|
---|
36 | #define remainderf(fa, fb) ((float)remainder(fa, fb))
|
---|
37 | #define rintf(f) ((float)rint(f))
|
---|
38 | /* Some defines which only apply to *BSD */
|
---|
39 | # if defined(VBOX) && defined(HOST_BSD)
|
---|
40 | # define lrintl(f) ((int32_t)rint(f))
|
---|
41 | # define llrintl(f) ((int64_t)rint(f))
|
---|
42 | # define rintl(d) ((int32_t)rint(d))
|
---|
43 | # define sqrtl(f) (sqrt(f))
|
---|
44 | # define remainderl(fa, fb) (remainder(fa, fb))
|
---|
45 | # endif /* VBOX && _BSD */
|
---|
46 | #if !defined(__sparc__) && \
|
---|
47 | (defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10)
|
---|
48 | extern long double rintl(long double);
|
---|
49 | extern long double scalbnl(long double, int);
|
---|
50 |
|
---|
51 | long long
|
---|
52 | llrintl(long double x) {
|
---|
53 | return ((long long) rintl(x));
|
---|
54 | }
|
---|
55 |
|
---|
56 | long
|
---|
57 | lrintl(long double x) {
|
---|
58 | return ((long) rintl(x));
|
---|
59 | }
|
---|
60 |
|
---|
61 | long double
|
---|
62 | ldexpl(long double x, int n) {
|
---|
63 | return (scalbnl(x, n));
|
---|
64 | }
|
---|
65 | #endif
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | #if defined(_ARCH_PPC)
|
---|
69 |
|
---|
70 | /* correct (but slow) PowerPC rint() (glibc version is incorrect) */
|
---|
71 | static double qemu_rint(double x)
|
---|
72 | {
|
---|
73 | double y = 4503599627370496.0;
|
---|
74 | if (fabs(x) >= y)
|
---|
75 | return x;
|
---|
76 | if (x < 0)
|
---|
77 | y = -y;
|
---|
78 | y = (x + y) - y;
|
---|
79 | if (y == 0.0)
|
---|
80 | y = copysign(y, x);
|
---|
81 | return y;
|
---|
82 | }
|
---|
83 |
|
---|
84 | #define rint qemu_rint
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | /*----------------------------------------------------------------------------
|
---|
88 | | Software IEC/IEEE integer-to-floating-point conversion routines.
|
---|
89 | *----------------------------------------------------------------------------*/
|
---|
90 | float32 int32_to_float32(int v STATUS_PARAM)
|
---|
91 | {
|
---|
92 | return (float32)v;
|
---|
93 | }
|
---|
94 |
|
---|
95 | float32 uint32_to_float32(unsigned int v STATUS_PARAM)
|
---|
96 | {
|
---|
97 | return (float32)v;
|
---|
98 | }
|
---|
99 |
|
---|
100 | float64 int32_to_float64(int v STATUS_PARAM)
|
---|
101 | {
|
---|
102 | return (float64)v;
|
---|
103 | }
|
---|
104 |
|
---|
105 | float64 uint32_to_float64(unsigned int v STATUS_PARAM)
|
---|
106 | {
|
---|
107 | return (float64)v;
|
---|
108 | }
|
---|
109 |
|
---|
110 | #ifdef FLOATX80
|
---|
111 | floatx80 int32_to_floatx80(int v STATUS_PARAM)
|
---|
112 | {
|
---|
113 | return (floatx80)v;
|
---|
114 | }
|
---|
115 | #endif
|
---|
116 | float32 int64_to_float32( int64_t v STATUS_PARAM)
|
---|
117 | {
|
---|
118 | return (float32)v;
|
---|
119 | }
|
---|
120 | float32 uint64_to_float32( uint64_t v STATUS_PARAM)
|
---|
121 | {
|
---|
122 | return (float32)v;
|
---|
123 | }
|
---|
124 | float64 int64_to_float64( int64_t v STATUS_PARAM)
|
---|
125 | {
|
---|
126 | return (float64)v;
|
---|
127 | }
|
---|
128 | float64 uint64_to_float64( uint64_t v STATUS_PARAM)
|
---|
129 | {
|
---|
130 | return (float64)v;
|
---|
131 | }
|
---|
132 | #ifdef FLOATX80
|
---|
133 | floatx80 int64_to_floatx80( int64_t v STATUS_PARAM)
|
---|
134 | {
|
---|
135 | return (floatx80)v;
|
---|
136 | }
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | /* XXX: this code implements the x86 behaviour, not the IEEE one. */
|
---|
140 | #if HOST_LONG_BITS == 32
|
---|
141 | static inline int long_to_int32(long a)
|
---|
142 | {
|
---|
143 | return a;
|
---|
144 | }
|
---|
145 | #else
|
---|
146 | static inline int long_to_int32(long a)
|
---|
147 | {
|
---|
148 | if (a != (int32_t)a)
|
---|
149 | a = 0x80000000;
|
---|
150 | return a;
|
---|
151 | }
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | /*----------------------------------------------------------------------------
|
---|
155 | | Software IEC/IEEE single-precision conversion routines.
|
---|
156 | *----------------------------------------------------------------------------*/
|
---|
157 | int float32_to_int32( float32 a STATUS_PARAM)
|
---|
158 | {
|
---|
159 | return long_to_int32(lrintf(a));
|
---|
160 | }
|
---|
161 | int float32_to_int32_round_to_zero( float32 a STATUS_PARAM)
|
---|
162 | {
|
---|
163 | return (int)a;
|
---|
164 | }
|
---|
165 | int64_t float32_to_int64( float32 a STATUS_PARAM)
|
---|
166 | {
|
---|
167 | return llrintf(a);
|
---|
168 | }
|
---|
169 |
|
---|
170 | int64_t float32_to_int64_round_to_zero( float32 a STATUS_PARAM)
|
---|
171 | {
|
---|
172 | return (int64_t)a;
|
---|
173 | }
|
---|
174 |
|
---|
175 | float64 float32_to_float64( float32 a STATUS_PARAM)
|
---|
176 | {
|
---|
177 | return a;
|
---|
178 | }
|
---|
179 | #ifdef FLOATX80
|
---|
180 | floatx80 float32_to_floatx80( float32 a STATUS_PARAM)
|
---|
181 | {
|
---|
182 | return a;
|
---|
183 | }
|
---|
184 | #endif
|
---|
185 |
|
---|
186 | unsigned int float32_to_uint32( float32 a STATUS_PARAM)
|
---|
187 | {
|
---|
188 | int64_t v;
|
---|
189 | unsigned int res;
|
---|
190 |
|
---|
191 | v = llrintf(a);
|
---|
192 | if (v < 0) {
|
---|
193 | res = 0;
|
---|
194 | } else if (v > 0xffffffff) {
|
---|
195 | res = 0xffffffff;
|
---|
196 | } else {
|
---|
197 | res = v;
|
---|
198 | }
|
---|
199 | return res;
|
---|
200 | }
|
---|
201 | unsigned int float32_to_uint32_round_to_zero( float32 a STATUS_PARAM)
|
---|
202 | {
|
---|
203 | int64_t v;
|
---|
204 | unsigned int res;
|
---|
205 |
|
---|
206 | v = (int64_t)a;
|
---|
207 | if (v < 0) {
|
---|
208 | res = 0;
|
---|
209 | } else if (v > 0xffffffff) {
|
---|
210 | res = 0xffffffff;
|
---|
211 | } else {
|
---|
212 | res = v;
|
---|
213 | }
|
---|
214 | return res;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /*----------------------------------------------------------------------------
|
---|
218 | | Software IEC/IEEE single-precision operations.
|
---|
219 | *----------------------------------------------------------------------------*/
|
---|
220 | float32 float32_round_to_int( float32 a STATUS_PARAM)
|
---|
221 | {
|
---|
222 | return rintf(a);
|
---|
223 | }
|
---|
224 |
|
---|
225 | float32 float32_rem( float32 a, float32 b STATUS_PARAM)
|
---|
226 | {
|
---|
227 | return remainderf(a, b);
|
---|
228 | }
|
---|
229 |
|
---|
230 | float32 float32_sqrt( float32 a STATUS_PARAM)
|
---|
231 | {
|
---|
232 | return sqrtf(a);
|
---|
233 | }
|
---|
234 | int float32_compare( float32 a, float32 b STATUS_PARAM )
|
---|
235 | {
|
---|
236 | if (a < b) {
|
---|
237 | return float_relation_less;
|
---|
238 | } else if (a == b) {
|
---|
239 | return float_relation_equal;
|
---|
240 | } else if (a > b) {
|
---|
241 | return float_relation_greater;
|
---|
242 | } else {
|
---|
243 | return float_relation_unordered;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | int float32_compare_quiet( float32 a, float32 b STATUS_PARAM )
|
---|
247 | {
|
---|
248 | if (isless(a, b)) {
|
---|
249 | return float_relation_less;
|
---|
250 | } else if (a == b) {
|
---|
251 | return float_relation_equal;
|
---|
252 | } else if (isgreater(a, b)) {
|
---|
253 | return float_relation_greater;
|
---|
254 | } else {
|
---|
255 | return float_relation_unordered;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | int float32_is_signaling_nan( float32 a1)
|
---|
259 | {
|
---|
260 | float32u u;
|
---|
261 | uint32_t a;
|
---|
262 | u.f = a1;
|
---|
263 | a = u.i;
|
---|
264 | return ( ( ( a>>22 ) & 0x1FF ) == 0x1FE ) && ( a & 0x003FFFFF );
|
---|
265 | }
|
---|
266 |
|
---|
267 | int float32_is_nan( float32 a1 )
|
---|
268 | {
|
---|
269 | float32u u;
|
---|
270 | uint64_t a;
|
---|
271 | u.f = a1;
|
---|
272 | a = u.i;
|
---|
273 | return ( 0xFF800000 < ( a<<1 ) );
|
---|
274 | }
|
---|
275 |
|
---|
276 | /*----------------------------------------------------------------------------
|
---|
277 | | Software IEC/IEEE double-precision conversion routines.
|
---|
278 | *----------------------------------------------------------------------------*/
|
---|
279 | int float64_to_int32( float64 a STATUS_PARAM)
|
---|
280 | {
|
---|
281 | return long_to_int32(lrint(a));
|
---|
282 | }
|
---|
283 | int float64_to_int32_round_to_zero( float64 a STATUS_PARAM)
|
---|
284 | {
|
---|
285 | return (int)a;
|
---|
286 | }
|
---|
287 | int64_t float64_to_int64( float64 a STATUS_PARAM)
|
---|
288 | {
|
---|
289 | return llrint(a);
|
---|
290 | }
|
---|
291 | int64_t float64_to_int64_round_to_zero( float64 a STATUS_PARAM)
|
---|
292 | {
|
---|
293 | return (int64_t)a;
|
---|
294 | }
|
---|
295 | float32 float64_to_float32( float64 a STATUS_PARAM)
|
---|
296 | {
|
---|
297 | return a;
|
---|
298 | }
|
---|
299 | #ifdef FLOATX80
|
---|
300 | floatx80 float64_to_floatx80( float64 a STATUS_PARAM)
|
---|
301 | {
|
---|
302 | return a;
|
---|
303 | }
|
---|
304 | #endif
|
---|
305 | #ifdef FLOAT128
|
---|
306 | float128 float64_to_float128( float64 a STATUS_PARAM)
|
---|
307 | {
|
---|
308 | return a;
|
---|
309 | }
|
---|
310 | #endif
|
---|
311 |
|
---|
312 | unsigned int float64_to_uint32( float64 a STATUS_PARAM)
|
---|
313 | {
|
---|
314 | int64_t v;
|
---|
315 | unsigned int res;
|
---|
316 |
|
---|
317 | v = llrint(a);
|
---|
318 | if (v < 0) {
|
---|
319 | res = 0;
|
---|
320 | } else if (v > 0xffffffff) {
|
---|
321 | res = 0xffffffff;
|
---|
322 | } else {
|
---|
323 | res = v;
|
---|
324 | }
|
---|
325 | return res;
|
---|
326 | }
|
---|
327 | unsigned int float64_to_uint32_round_to_zero( float64 a STATUS_PARAM)
|
---|
328 | {
|
---|
329 | int64_t v;
|
---|
330 | unsigned int res;
|
---|
331 |
|
---|
332 | v = (int64_t)a;
|
---|
333 | if (v < 0) {
|
---|
334 | res = 0;
|
---|
335 | } else if (v > 0xffffffff) {
|
---|
336 | res = 0xffffffff;
|
---|
337 | } else {
|
---|
338 | res = v;
|
---|
339 | }
|
---|
340 | return res;
|
---|
341 | }
|
---|
342 | uint64_t float64_to_uint64 (float64 a STATUS_PARAM)
|
---|
343 | {
|
---|
344 | int64_t v;
|
---|
345 |
|
---|
346 | v = llrint(a + (float64)INT64_MIN);
|
---|
347 |
|
---|
348 | return v - INT64_MIN;
|
---|
349 | }
|
---|
350 | uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM)
|
---|
351 | {
|
---|
352 | int64_t v;
|
---|
353 |
|
---|
354 | v = (int64_t)(a + (float64)INT64_MIN);
|
---|
355 |
|
---|
356 | return v - INT64_MIN;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /*----------------------------------------------------------------------------
|
---|
360 | | Software IEC/IEEE double-precision operations.
|
---|
361 | *----------------------------------------------------------------------------*/
|
---|
362 | #if defined(__sun__) && \
|
---|
363 | (defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10)
|
---|
364 | static inline float64 trunc(float64 x)
|
---|
365 | {
|
---|
366 | return x < 0 ? -floor(-x) : floor(x);
|
---|
367 | }
|
---|
368 | #endif
|
---|
369 | float64 float64_trunc_to_int( float64 a STATUS_PARAM )
|
---|
370 | {
|
---|
371 | return trunc(a);
|
---|
372 | }
|
---|
373 |
|
---|
374 | float64 float64_round_to_int( float64 a STATUS_PARAM )
|
---|
375 | {
|
---|
376 | #if defined(__arm__)
|
---|
377 | switch(STATUS(float_rounding_mode)) {
|
---|
378 | default:
|
---|
379 | case float_round_nearest_even:
|
---|
380 | asm("rndd %0, %1" : "=f" (a) : "f"(a));
|
---|
381 | break;
|
---|
382 | case float_round_down:
|
---|
383 | asm("rnddm %0, %1" : "=f" (a) : "f"(a));
|
---|
384 | break;
|
---|
385 | case float_round_up:
|
---|
386 | asm("rnddp %0, %1" : "=f" (a) : "f"(a));
|
---|
387 | break;
|
---|
388 | case float_round_to_zero:
|
---|
389 | asm("rnddz %0, %1" : "=f" (a) : "f"(a));
|
---|
390 | break;
|
---|
391 | }
|
---|
392 | #else
|
---|
393 | return rint(a);
|
---|
394 | #endif
|
---|
395 | }
|
---|
396 |
|
---|
397 | float64 float64_rem( float64 a, float64 b STATUS_PARAM)
|
---|
398 | {
|
---|
399 | return remainder(a, b);
|
---|
400 | }
|
---|
401 |
|
---|
402 | float64 float64_sqrt( float64 a STATUS_PARAM)
|
---|
403 | {
|
---|
404 | return sqrt(a);
|
---|
405 | }
|
---|
406 | int float64_compare( float64 a, float64 b STATUS_PARAM )
|
---|
407 | {
|
---|
408 | if (a < b) {
|
---|
409 | return float_relation_less;
|
---|
410 | } else if (a == b) {
|
---|
411 | return float_relation_equal;
|
---|
412 | } else if (a > b) {
|
---|
413 | return float_relation_greater;
|
---|
414 | } else {
|
---|
415 | return float_relation_unordered;
|
---|
416 | }
|
---|
417 | }
|
---|
418 | int float64_compare_quiet( float64 a, float64 b STATUS_PARAM )
|
---|
419 | {
|
---|
420 | if (isless(a, b)) {
|
---|
421 | return float_relation_less;
|
---|
422 | } else if (a == b) {
|
---|
423 | return float_relation_equal;
|
---|
424 | } else if (isgreater(a, b)) {
|
---|
425 | return float_relation_greater;
|
---|
426 | } else {
|
---|
427 | return float_relation_unordered;
|
---|
428 | }
|
---|
429 | }
|
---|
430 | int float64_is_signaling_nan( float64 a1)
|
---|
431 | {
|
---|
432 | float64u u;
|
---|
433 | uint64_t a;
|
---|
434 | u.f = a1;
|
---|
435 | a = u.i;
|
---|
436 | return
|
---|
437 | ( ( ( a>>51 ) & 0xFFF ) == 0xFFE )
|
---|
438 | && ( a & LIT64( 0x0007FFFFFFFFFFFF ) );
|
---|
439 |
|
---|
440 | }
|
---|
441 |
|
---|
442 | int float64_is_nan( float64 a1 )
|
---|
443 | {
|
---|
444 | float64u u;
|
---|
445 | uint64_t a;
|
---|
446 | u.f = a1;
|
---|
447 | a = u.i;
|
---|
448 |
|
---|
449 | return ( LIT64( 0xFFF0000000000000 ) < (bits64) ( a<<1 ) );
|
---|
450 |
|
---|
451 | }
|
---|
452 |
|
---|
453 | #ifdef FLOATX80
|
---|
454 |
|
---|
455 | /*----------------------------------------------------------------------------
|
---|
456 | | Software IEC/IEEE extended double-precision conversion routines.
|
---|
457 | *----------------------------------------------------------------------------*/
|
---|
458 | int floatx80_to_int32( floatx80 a STATUS_PARAM)
|
---|
459 | {
|
---|
460 | return long_to_int32(lrintl(a));
|
---|
461 | }
|
---|
462 | int floatx80_to_int32_round_to_zero( floatx80 a STATUS_PARAM)
|
---|
463 | {
|
---|
464 | return (int)a;
|
---|
465 | }
|
---|
466 | int64_t floatx80_to_int64( floatx80 a STATUS_PARAM)
|
---|
467 | {
|
---|
468 | return llrintl(a);
|
---|
469 | }
|
---|
470 | int64_t floatx80_to_int64_round_to_zero( floatx80 a STATUS_PARAM)
|
---|
471 | {
|
---|
472 | return (int64_t)a;
|
---|
473 | }
|
---|
474 | float32 floatx80_to_float32( floatx80 a STATUS_PARAM)
|
---|
475 | {
|
---|
476 | return a;
|
---|
477 | }
|
---|
478 | float64 floatx80_to_float64( floatx80 a STATUS_PARAM)
|
---|
479 | {
|
---|
480 | return a;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /*----------------------------------------------------------------------------
|
---|
484 | | Software IEC/IEEE extended double-precision operations.
|
---|
485 | *----------------------------------------------------------------------------*/
|
---|
486 | floatx80 floatx80_round_to_int( floatx80 a STATUS_PARAM)
|
---|
487 | {
|
---|
488 | return rintl(a);
|
---|
489 | }
|
---|
490 | floatx80 floatx80_rem( floatx80 a, floatx80 b STATUS_PARAM)
|
---|
491 | {
|
---|
492 | return remainderl(a, b);
|
---|
493 | }
|
---|
494 | floatx80 floatx80_sqrt( floatx80 a STATUS_PARAM)
|
---|
495 | {
|
---|
496 | return sqrtl(a);
|
---|
497 | }
|
---|
498 | int floatx80_compare( floatx80 a, floatx80 b STATUS_PARAM )
|
---|
499 | {
|
---|
500 | if (a < b) {
|
---|
501 | return float_relation_less;
|
---|
502 | } else if (a == b) {
|
---|
503 | return float_relation_equal;
|
---|
504 | } else if (a > b) {
|
---|
505 | return float_relation_greater;
|
---|
506 | } else {
|
---|
507 | return float_relation_unordered;
|
---|
508 | }
|
---|
509 | }
|
---|
510 | int floatx80_compare_quiet( floatx80 a, floatx80 b STATUS_PARAM )
|
---|
511 | {
|
---|
512 | if (isless(a, b)) {
|
---|
513 | return float_relation_less;
|
---|
514 | } else if (a == b) {
|
---|
515 | return float_relation_equal;
|
---|
516 | } else if (isgreater(a, b)) {
|
---|
517 | return float_relation_greater;
|
---|
518 | } else {
|
---|
519 | return float_relation_unordered;
|
---|
520 | }
|
---|
521 | }
|
---|
522 | int floatx80_is_signaling_nan( floatx80 a1)
|
---|
523 | {
|
---|
524 | floatx80u u;
|
---|
525 | uint64_t aLow;
|
---|
526 | u.f = a1;
|
---|
527 |
|
---|
528 | aLow = u.i.low & ~ LIT64( 0x4000000000000000 );
|
---|
529 | return
|
---|
530 | ( ( u.i.high & 0x7FFF ) == 0x7FFF )
|
---|
531 | && (bits64) ( aLow<<1 )
|
---|
532 | && ( u.i.low == aLow );
|
---|
533 | }
|
---|
534 |
|
---|
535 | int floatx80_is_nan( floatx80 a1 )
|
---|
536 | {
|
---|
537 | floatx80u u;
|
---|
538 | u.f = a1;
|
---|
539 | return ( ( u.i.high & 0x7FFF ) == 0x7FFF ) && (bits64) ( u.i.low<<1 );
|
---|
540 | }
|
---|
541 |
|
---|
542 | #endif
|
---|