VirtualBox

source: vbox/trunk/src/recompiler/fpu/softfloat-native.c@ 65902

最後變更 在這個檔案從65902是 37689,由 vboxsync 提交於 13 年 前

recompiler: Merged in changes from 0.13.0.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette