1 /*============================================================================
2 This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
3 Arithmetic Package, Release 2b.
4 
5 Written by John R. Hauser.  This work was made possible in part by the
6 International Computer Science Institute, located at Suite 600, 1947 Center
7 Street, Berkeley, California 94704.  Funding was partially provided by the
8 National Science Foundation under grant MIP-9311980.  The original version
9 of this code was written as part of a project to build a fixed-point vector
10 processor in collaboration with the University of California at Berkeley,
11 overseen by Profs. Nelson Morgan and John Wawrzynek.  More information
12 is available through the Web page `http://www.cs.berkeley.edu/~jhauser/
13 arithmetic/SoftFloat.html'.
14 
15 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort has
16 been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES
17 RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS
18 AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES,
19 COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE
20 EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE
21 INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR
22 OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE.
23 
24 Derivative works are acceptable, even for commercial purposes, so long as
25 (1) the source code for the derivative work includes prominent notice that
26 the work is derivative, and (2) the source code includes prominent notice with
27 these four paragraphs for those parts of this code that are retained.
28 =============================================================================*/
29 
30 #ifndef _SOFTFLOAT_SPECIALIZE_H_
31 #define _SOFTFLOAT_SPECIALIZE_H_
32 
33 #include "softfloat.h"
34 
35 /*============================================================================
36  * Adapted for Bochs (x86 achitecture simulator) by
37  *            Stanislav Shwartsman [sshwarts at sourceforge net]
38  * ==========================================================================*/
39 
40 #define int16_indefinite ((Bit16s)0x8000)
41 #define int32_indefinite ((Bit32s)0x80000000)
42 #define int64_indefinite BX_CONST64(0x8000000000000000)
43 
44 #define uint16_indefinite (0xffff)
45 #define uint32_indefinite (0xffffffff)
46 #define uint64_indefinite BX_CONST64(0xffffffffffffffff)
47 
48 /*----------------------------------------------------------------------------
49 | Internal canonical NaN format.
50 *----------------------------------------------------------------------------*/
51 
52 typedef struct {
53     int sign;
54     Bit64u hi, lo;
55 } commonNaNT;
56 
57 #ifdef FLOAT16
58 
59 /*----------------------------------------------------------------------------
60 | The pattern for a default generated half-precision NaN.
61 *----------------------------------------------------------------------------*/
62 const float16 float16_default_nan = 0xFE00;
63 
64 #define float16_fraction extractFloat16Frac
65 #define float16_exp extractFloat16Exp
66 #define float16_sign extractFloat16Sign
67 
68 /*----------------------------------------------------------------------------
69 | Returns the fraction bits of the half-precision floating-point value `a'.
70 *----------------------------------------------------------------------------*/
71 
extractFloat16Frac(float16 a)72 BX_CPP_INLINE Bit16u extractFloat16Frac(float16 a)
73 {
74     return a & 0x3FF;
75 }
76 
77 /*----------------------------------------------------------------------------
78 | Returns the exponent bits of the half-precision floating-point value `a'.
79 *----------------------------------------------------------------------------*/
80 
extractFloat16Exp(float16 a)81 BX_CPP_INLINE Bit16s extractFloat16Exp(float16 a)
82 {
83     return (a>>10) & 0x1F;
84 }
85 
86 /*----------------------------------------------------------------------------
87 | Returns the sign bit of the half-precision floating-point value `a'.
88 *----------------------------------------------------------------------------*/
89 
extractFloat16Sign(float16 a)90 BX_CPP_INLINE int extractFloat16Sign(float16 a)
91 {
92     return a>>15;
93 }
94 
95 /*----------------------------------------------------------------------------
96 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
97 | single-precision floating-point value, returning the result.  After being
98 | shifted into the proper positions, the three fields are simply added
99 | together to form the result.  This means that any integer portion of `zSig'
100 | will be added into the exponent.  Since a properly normalized significand
101 | will have an integer portion equal to 1, the `zExp' input should be 1 less
102 | than the desired result exponent whenever `zSig' is a complete, normalized
103 | significand.
104 *----------------------------------------------------------------------------*/
105 
packFloat16(int zSign,int zExp,Bit16u zSig)106 BX_CPP_INLINE float16 packFloat16(int zSign, int zExp, Bit16u zSig)
107 {
108     return (((Bit16u) zSign)<<15) + (((Bit16u) zExp)<<10) + zSig;
109 }
110 
111 /*----------------------------------------------------------------------------
112 | Returns 1 if the half-precision floating-point value `a' is a NaN;
113 | otherwise returns 0.
114 *----------------------------------------------------------------------------*/
115 
float16_is_nan(float16 a)116 BX_CPP_INLINE int float16_is_nan(float16 a)
117 {
118     return (0xF800 < (Bit16u) (a<<1));
119 }
120 
121 /*----------------------------------------------------------------------------
122 | Returns 1 if the half-precision floating-point value `a' is a signaling
123 | NaN; otherwise returns 0.
124 *----------------------------------------------------------------------------*/
125 
float16_is_signaling_nan(float16 a)126 BX_CPP_INLINE int float16_is_signaling_nan(float16 a)
127 {
128     return (((a>>9) & 0x3F) == 0x3E) && (a & 0x1FF);
129 }
130 
131 /*----------------------------------------------------------------------------
132 | Returns 1 if the half-precision floating-point value `a' is denormal;
133 | otherwise returns 0.
134 *----------------------------------------------------------------------------*/
135 
float16_is_denormal(float16 a)136 BX_CPP_INLINE int float16_is_denormal(float16 a)
137 {
138    return (extractFloat16Exp(a) == 0) && (extractFloat16Frac(a) != 0);
139 }
140 
141 /*----------------------------------------------------------------------------
142 | Convert float16 denormals to zero.
143 *----------------------------------------------------------------------------*/
144 
float16_denormal_to_zero(float16 a)145 BX_CPP_INLINE float16 float16_denormal_to_zero(float16 a)
146 {
147   if (float16_is_denormal(a)) a &= 0x8000;
148   return a;
149 }
150 
151 /*----------------------------------------------------------------------------
152 | Returns the result of converting the half-precision floating-point NaN
153 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
154 | exception is raised.
155 *----------------------------------------------------------------------------*/
156 
float16ToCommonNaN(float16 a,float_status_t & status)157 BX_CPP_INLINE commonNaNT float16ToCommonNaN(float16 a, float_status_t &status)
158 {
159     commonNaNT z;
160     if (float16_is_signaling_nan(a)) float_raise(status, float_flag_invalid);
161     z.sign = a>>15;
162     z.lo = 0;
163     z.hi = ((Bit64u) a)<<54;
164     return z;
165 }
166 
167 /*----------------------------------------------------------------------------
168 | Returns the result of converting the canonical NaN `a' to the half-
169 | precision floating-point format.
170 *----------------------------------------------------------------------------*/
171 
commonNaNToFloat16(commonNaNT a)172 BX_CPP_INLINE float16 commonNaNToFloat16(commonNaNT a)
173 {
174     return (((Bit16u) a.sign)<<15) | 0x7E00 | (Bit16u)(a.hi>>54);
175 }
176 
177 #endif
178 
179 /*----------------------------------------------------------------------------
180 | Commonly used single-precision floating point constants
181 *----------------------------------------------------------------------------*/
182 const float32 float32_negative_inf  = 0xff800000;
183 const float32 float32_positive_inf  = 0x7f800000;
184 const float32 float32_negative_zero = 0x80000000;
185 const float32 float32_positive_zero = 0x00000000;
186 const float32 float32_negative_one  = 0xbf800000;
187 const float32 float32_positive_one  = 0x3f800000;
188 const float32 float32_max_float     = 0x7f7fffff;
189 const float32 float32_min_float     = 0xff7fffff;
190 
191 /*----------------------------------------------------------------------------
192 | The pattern for a default generated single-precision NaN.
193 *----------------------------------------------------------------------------*/
194 const float32 float32_default_nan   = 0xffc00000;
195 
196 #define float32_fraction extractFloat32Frac
197 #define float32_exp extractFloat32Exp
198 #define float32_sign extractFloat32Sign
199 
200 #define FLOAT32_EXP_BIAS 0x7F
201 
202 /*----------------------------------------------------------------------------
203 | Returns the fraction bits of the single-precision floating-point value `a'.
204 *----------------------------------------------------------------------------*/
205 
extractFloat32Frac(float32 a)206 BX_CPP_INLINE Bit32u extractFloat32Frac(float32 a)
207 {
208     return a & 0x007FFFFF;
209 }
210 
211 /*----------------------------------------------------------------------------
212 | Returns the exponent bits of the single-precision floating-point value `a'.
213 *----------------------------------------------------------------------------*/
214 
extractFloat32Exp(float32 a)215 BX_CPP_INLINE Bit16s extractFloat32Exp(float32 a)
216 {
217     return (a>>23) & 0xFF;
218 }
219 
220 /*----------------------------------------------------------------------------
221 | Returns the sign bit of the single-precision floating-point value `a'.
222 *----------------------------------------------------------------------------*/
223 
extractFloat32Sign(float32 a)224 BX_CPP_INLINE int extractFloat32Sign(float32 a)
225 {
226     return a>>31;
227 }
228 
229 /*----------------------------------------------------------------------------
230 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
231 | single-precision floating-point value, returning the result.  After being
232 | shifted into the proper positions, the three fields are simply added
233 | together to form the result.  This means that any integer portion of `zSig'
234 | will be added into the exponent.  Since a properly normalized significand
235 | will have an integer portion equal to 1, the `zExp' input should be 1 less
236 | than the desired result exponent whenever `zSig' is a complete, normalized
237 | significand.
238 *----------------------------------------------------------------------------*/
239 
packFloat32(int zSign,Bit16s zExp,Bit32u zSig)240 BX_CPP_INLINE float32 packFloat32(int zSign, Bit16s zExp, Bit32u zSig)
241 {
242     return (((Bit32u) zSign)<<31) + (((Bit32u) zExp)<<23) + zSig;
243 }
244 
245 /*----------------------------------------------------------------------------
246 | Returns 1 if the single-precision floating-point value `a' is a NaN;
247 | otherwise returns 0.
248 *----------------------------------------------------------------------------*/
249 
float32_is_nan(float32 a)250 BX_CPP_INLINE int float32_is_nan(float32 a)
251 {
252     return (0xFF000000 < (Bit32u) (a<<1));
253 }
254 
255 /*----------------------------------------------------------------------------
256 | Returns 1 if the single-precision floating-point value `a' is a signaling
257 | NaN; otherwise returns 0.
258 *----------------------------------------------------------------------------*/
259 
float32_is_signaling_nan(float32 a)260 BX_CPP_INLINE int float32_is_signaling_nan(float32 a)
261 {
262     return (((a>>22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF);
263 }
264 
265 /*----------------------------------------------------------------------------
266 | Returns 1 if the single-precision floating-point value `a' is denormal;
267 | otherwise returns 0.
268 *----------------------------------------------------------------------------*/
269 
float32_is_denormal(float32 a)270 BX_CPP_INLINE int float32_is_denormal(float32 a)
271 {
272    return (extractFloat32Exp(a) == 0) && (extractFloat32Frac(a) != 0);
273 }
274 
275 /*----------------------------------------------------------------------------
276 | Convert float32 denormals to zero.
277 *----------------------------------------------------------------------------*/
278 
float32_denormal_to_zero(float32 a)279 BX_CPP_INLINE float32 float32_denormal_to_zero(float32 a)
280 {
281   if (float32_is_denormal(a)) a &= 0x80000000;
282   return a;
283 }
284 
285 /*----------------------------------------------------------------------------
286 | Returns the result of converting the single-precision floating-point NaN
287 | `a' to the canonical NaN format.  If `a' is a signaling NaN, the invalid
288 | exception is raised.
289 *----------------------------------------------------------------------------*/
290 
float32ToCommonNaN(float32 a,float_status_t & status)291 BX_CPP_INLINE commonNaNT float32ToCommonNaN(float32 a, float_status_t &status)
292 {
293     commonNaNT z;
294     if (float32_is_signaling_nan(a)) float_raise(status, float_flag_invalid);
295     z.sign = a>>31;
296     z.lo = 0;
297     z.hi = ((Bit64u) a)<<41;
298     return z;
299 }
300 
301 /*----------------------------------------------------------------------------
302 | Returns the result of converting the canonical NaN `a' to the single-
303 | precision floating-point format.
304 *----------------------------------------------------------------------------*/
305 
commonNaNToFloat32(commonNaNT a)306 BX_CPP_INLINE float32 commonNaNToFloat32(commonNaNT a)
307 {
308     return (((Bit32u) a.sign)<<31) | 0x7FC00000 | (Bit32u)(a.hi>>41);
309 }
310 
311 /*----------------------------------------------------------------------------
312 | Takes two single-precision floating-point values `a' and `b', one of which
313 | is a NaN, and returns the appropriate NaN result.  If either `a' or `b' is a
314 | signaling NaN, the invalid exception is raised.
315 *----------------------------------------------------------------------------*/
316 
317 float32 propagateFloat32NaN(float32 a, float32 b, float_status_t &status);
318 
319 /*----------------------------------------------------------------------------
320 | Takes single-precision floating-point NaN `a' and returns the appropriate
321 | NaN result.  If `a' is a signaling NaN, the invalid exception is raised.
322 *----------------------------------------------------------------------------*/
323 
propagateFloat32NaN(float32 a,float_status_t & status)324 BX_CPP_INLINE float32 propagateFloat32NaN(float32 a, float_status_t &status)
325 {
326     if (float32_is_signaling_nan(a))
327         float_raise(status, float_flag_invalid);
328 
329     return a | 0x00400000;
330 }
331 
332 /*----------------------------------------------------------------------------
333 | Commonly used single-precision floating point constants
334 *----------------------------------------------------------------------------*/
335 const float64 float64_negative_inf  = BX_CONST64(0xfff0000000000000);
336 const float64 float64_positive_inf  = BX_CONST64(0x7ff0000000000000);
337 const float64 float64_negative_zero = BX_CONST64(0x8000000000000000);
338 const float64 float64_positive_zero = BX_CONST64(0x0000000000000000);
339 const float64 float64_negative_one  = BX_CONST64(0xbff0000000000000);
340 const float64 float64_positive_one  = BX_CONST64(0x3ff0000000000000);
341 const float64 float64_max_float     = BX_CONST64(0x7fefffffffffffff);
342 const float64 float64_min_float     = BX_CONST64(0xffefffffffffffff);
343 
344 /*----------------------------------------------------------------------------
345 | The pattern for a default generated double-precision NaN.
346 *----------------------------------------------------------------------------*/
347 const float64 float64_default_nan = BX_CONST64(0xFFF8000000000000);
348 
349 #define float64_fraction extractFloat64Frac
350 #define float64_exp extractFloat64Exp
351 #define float64_sign extractFloat64Sign
352 
353 #define FLOAT64_EXP_BIAS 0x3FF
354 
355 /*----------------------------------------------------------------------------
356 | Returns the fraction bits of the double-precision floating-point value `a'.
357 *----------------------------------------------------------------------------*/
358 
extractFloat64Frac(float64 a)359 BX_CPP_INLINE Bit64u extractFloat64Frac(float64 a)
360 {
361     return a & BX_CONST64(0x000FFFFFFFFFFFFF);
362 }
363 
364 /*----------------------------------------------------------------------------
365 | Returns the exponent bits of the double-precision floating-point value `a'.
366 *----------------------------------------------------------------------------*/
367 
extractFloat64Exp(float64 a)368 BX_CPP_INLINE Bit16s extractFloat64Exp(float64 a)
369 {
370     return (Bit16s)(a>>52) & 0x7FF;
371 }
372 
373 /*----------------------------------------------------------------------------
374 | Returns the sign bit of the double-precision floating-point value `a'.
375 *----------------------------------------------------------------------------*/
376 
extractFloat64Sign(float64 a)377 BX_CPP_INLINE int extractFloat64Sign(float64 a)
378 {
379     return (int)(a>>63);
380 }
381 
382 /*----------------------------------------------------------------------------
383 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
384 | double-precision floating-point value, returning the result.  After being
385 | shifted into the proper positions, the three fields are simply added
386 | together to form the result.  This means that any integer portion of `zSig'
387 | will be added into the exponent.  Since a properly normalized significand
388 | will have an integer portion equal to 1, the `zExp' input should be 1 less
389 | than the desired result exponent whenever `zSig' is a complete, normalized
390 | significand.
391 *----------------------------------------------------------------------------*/
392 
packFloat64(int zSign,Bit16s zExp,Bit64u zSig)393 BX_CPP_INLINE float64 packFloat64(int zSign, Bit16s zExp, Bit64u zSig)
394 {
395     return (((Bit64u) zSign)<<63) + (((Bit64u) zExp)<<52) + zSig;
396 }
397 
398 /*----------------------------------------------------------------------------
399 | Returns 1 if the double-precision floating-point value `a' is a NaN;
400 | otherwise returns 0.
401 *----------------------------------------------------------------------------*/
402 
float64_is_nan(float64 a)403 BX_CPP_INLINE int float64_is_nan(float64 a)
404 {
405     return (BX_CONST64(0xFFE0000000000000) < (Bit64u) (a<<1));
406 }
407 
408 /*----------------------------------------------------------------------------
409 | Returns 1 if the double-precision floating-point value `a' is a signaling
410 | NaN; otherwise returns 0.
411 *----------------------------------------------------------------------------*/
412 
float64_is_signaling_nan(float64 a)413 BX_CPP_INLINE int float64_is_signaling_nan(float64 a)
414 {
415     return (((a>>51) & 0xFFF) == 0xFFE) && (a & BX_CONST64(0x0007FFFFFFFFFFFF));
416 }
417 
418 /*----------------------------------------------------------------------------
419 | Returns 1 if the double-precision floating-point value `a' is denormal;
420 | otherwise returns 0.
421 *----------------------------------------------------------------------------*/
422 
float64_is_denormal(float64 a)423 BX_CPP_INLINE int float64_is_denormal(float64 a)
424 {
425    return (extractFloat64Exp(a) == 0) && (extractFloat64Frac(a) != 0);
426 }
427 
428 /*----------------------------------------------------------------------------
429 | Convert float64 denormals to zero.
430 *----------------------------------------------------------------------------*/
431 
float64_denormal_to_zero(float64 a)432 BX_CPP_INLINE float64 float64_denormal_to_zero(float64 a)
433 {
434   if (float64_is_denormal(a)) a &= ((Bit64u)(1) << 63);
435   return a;
436 }
437 
438 /*----------------------------------------------------------------------------
439 | Returns the result of converting the double-precision floating-point NaN
440 | `a' to the canonical NaN format.  If `a' is a signaling NaN, the invalid
441 | exception is raised.
442 *----------------------------------------------------------------------------*/
443 
float64ToCommonNaN(float64 a,float_status_t & status)444 BX_CPP_INLINE commonNaNT float64ToCommonNaN(float64 a, float_status_t &status)
445 {
446     commonNaNT z;
447     if (float64_is_signaling_nan(a)) float_raise(status, float_flag_invalid);
448     z.sign = (int)(a>>63);
449     z.lo = 0;
450     z.hi = a<<12;
451     return z;
452 }
453 
454 /*----------------------------------------------------------------------------
455 | Returns the result of converting the canonical NaN `a' to the double-
456 | precision floating-point format.
457 *----------------------------------------------------------------------------*/
458 
commonNaNToFloat64(commonNaNT a)459 BX_CPP_INLINE float64 commonNaNToFloat64(commonNaNT a)
460 {
461     return (((Bit64u) a.sign)<<63) | BX_CONST64(0x7FF8000000000000) | (a.hi>>12);
462 }
463 
464 /*----------------------------------------------------------------------------
465 | Takes two double-precision floating-point values `a' and `b', one of which
466 | is a NaN, and returns the appropriate NaN result.  If either `a' or `b' is a
467 | signaling NaN, the invalid exception is raised.
468 *----------------------------------------------------------------------------*/
469 
470 float64 propagateFloat64NaN(float64 a, float64 b, float_status_t &status);
471 
472 /*----------------------------------------------------------------------------
473 | Takes double-precision floating-point NaN `a' and returns the appropriate
474 | NaN result.  If `a' is a signaling NaN, the invalid exception is raised.
475 *----------------------------------------------------------------------------*/
476 
propagateFloat64NaN(float64 a,float_status_t & status)477 BX_CPP_INLINE float64 propagateFloat64NaN(float64 a, float_status_t &status)
478 {
479     if (float64_is_signaling_nan(a))
480         float_raise(status, float_flag_invalid);
481 
482     return a | BX_CONST64(0x0008000000000000);
483 }
484 
485 #ifdef FLOATX80
486 
487 /*----------------------------------------------------------------------------
488 | The pattern for a default generated extended double-precision NaN.  The
489 | `high' and `low' values hold the most- and least-significant bits,
490 | respectively.
491 *----------------------------------------------------------------------------*/
492 #define floatx80_default_nan_exp 0xFFFF
493 #define floatx80_default_nan_fraction BX_CONST64(0xC000000000000000)
494 
495 #define floatx80_fraction extractFloatx80Frac
496 #define floatx80_exp extractFloatx80Exp
497 #define floatx80_sign extractFloatx80Sign
498 
499 #define FLOATX80_EXP_BIAS 0x3FFF
500 
501 /*----------------------------------------------------------------------------
502 | Returns the fraction bits of the extended double-precision floating-point
503 | value `a'.
504 *----------------------------------------------------------------------------*/
505 
extractFloatx80Frac(floatx80 a)506 BX_CPP_INLINE Bit64u extractFloatx80Frac(floatx80 a)
507 {
508     return a.fraction;
509 }
510 
511 /*----------------------------------------------------------------------------
512 | Returns the exponent bits of the extended double-precision floating-point
513 | value `a'.
514 *----------------------------------------------------------------------------*/
515 
extractFloatx80Exp(floatx80 a)516 BX_CPP_INLINE Bit32s extractFloatx80Exp(floatx80 a)
517 {
518     return a.exp & 0x7FFF;
519 }
520 
521 /*----------------------------------------------------------------------------
522 | Returns the sign bit of the extended double-precision floating-point value
523 | `a'.
524 *----------------------------------------------------------------------------*/
525 
extractFloatx80Sign(floatx80 a)526 BX_CPP_INLINE int extractFloatx80Sign(floatx80 a)
527 {
528     return a.exp>>15;
529 }
530 
531 /*----------------------------------------------------------------------------
532 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into an
533 | extended double-precision floating-point value, returning the result.
534 *----------------------------------------------------------------------------*/
535 
packFloatx80(int zSign,Bit32s zExp,Bit64u zSig)536 BX_CPP_INLINE floatx80 packFloatx80(int zSign, Bit32s zExp, Bit64u zSig)
537 {
538     floatx80 z;
539     z.fraction = zSig;
540     z.exp = (zSign << 15) + zExp;
541     return z;
542 }
543 
544 /*----------------------------------------------------------------------------
545 | Returns 1 if the extended double-precision floating-point value `a' is a
546 | NaN; otherwise returns 0.
547 *----------------------------------------------------------------------------*/
548 
floatx80_is_nan(floatx80 a)549 BX_CPP_INLINE int floatx80_is_nan(floatx80 a)
550 {
551     return ((a.exp & 0x7FFF) == 0x7FFF) && (Bit64s) (a.fraction<<1);
552 }
553 
554 /*----------------------------------------------------------------------------
555 | Returns 1 if the extended double-precision floating-point value `a' is a
556 | signaling NaN; otherwise returns 0.
557 *----------------------------------------------------------------------------*/
558 
floatx80_is_signaling_nan(floatx80 a)559 BX_CPP_INLINE int floatx80_is_signaling_nan(floatx80 a)
560 {
561     Bit64u aLow = a.fraction & ~BX_CONST64(0x4000000000000000);
562     return ((a.exp & 0x7FFF) == 0x7FFF) &&
563             ((Bit64u) (aLow<<1)) && (a.fraction == aLow);
564 }
565 
566 /*----------------------------------------------------------------------------
567 | Returns 1 if the extended double-precision floating-point value `a' is an
568 | unsupported; otherwise returns 0.
569 *----------------------------------------------------------------------------*/
570 
floatx80_is_unsupported(floatx80 a)571 BX_CPP_INLINE int floatx80_is_unsupported(floatx80 a)
572 {
573     return ((a.exp & 0x7FFF) && !(a.fraction & BX_CONST64(0x8000000000000000)));
574 }
575 
576 /*----------------------------------------------------------------------------
577 | Returns the result of converting the extended double-precision floating-
578 | point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
579 | invalid exception is raised.
580 *----------------------------------------------------------------------------*/
581 
floatx80ToCommonNaN(floatx80 a,float_status_t & status)582 BX_CPP_INLINE commonNaNT floatx80ToCommonNaN(floatx80 a, float_status_t &status)
583 {
584     commonNaNT z;
585     if (floatx80_is_signaling_nan(a)) float_raise(status, float_flag_invalid);
586     z.sign = a.exp >> 15;
587     z.lo = 0;
588     z.hi = a.fraction << 1;
589     return z;
590 }
591 
592 /*----------------------------------------------------------------------------
593 | Returns the result of converting the canonical NaN `a' to the extended
594 | double-precision floating-point format.
595 *----------------------------------------------------------------------------*/
596 
commonNaNToFloatx80(commonNaNT a)597 BX_CPP_INLINE floatx80 commonNaNToFloatx80(commonNaNT a)
598 {
599     floatx80 z;
600     z.fraction = BX_CONST64(0xC000000000000000) | (a.hi>>1);
601     z.exp = (((Bit16u) a.sign)<<15) | 0x7FFF;
602     return z;
603 }
604 
605 /*----------------------------------------------------------------------------
606 | Takes two extended double-precision floating-point values `a' and `b', one
607 | of which is a NaN, and returns the appropriate NaN result.  If either `a' or
608 | `b' is a signaling NaN, the invalid exception is raised.
609 *----------------------------------------------------------------------------*/
610 
611 floatx80 propagateFloatx80NaN(floatx80 a, floatx80 b, float_status_t &status);
612 
613 /*----------------------------------------------------------------------------
614 | Takes extended double-precision floating-point  NaN  `a' and returns the
615 | appropriate NaN result. If `a' is a signaling NaN, the invalid exception
616 | is raised.
617 *----------------------------------------------------------------------------*/
618 
propagateFloatx80NaN(floatx80 a,float_status_t & status)619 BX_CPP_INLINE floatx80 propagateFloatx80NaN(floatx80 a, float_status_t &status)
620 {
621     if (floatx80_is_signaling_nan(a))
622         float_raise(status, float_flag_invalid);
623 
624     a.fraction |= BX_CONST64(0xC000000000000000);
625 
626     return a;
627 }
628 
629 /*----------------------------------------------------------------------------
630 | The pattern for a default generated extended double-precision NaN.
631 *----------------------------------------------------------------------------*/
632 extern const floatx80 floatx80_default_nan;
633 
634 #endif /* FLOATX80 */
635 
636 #ifdef FLOAT128
637 
638 #include "softfloat-macros.h"
639 
640 /*----------------------------------------------------------------------------
641 | The pattern for a default generated quadruple-precision NaN. The `high' and
642 | `low' values hold the most- and least-significant bits, respectively.
643 *----------------------------------------------------------------------------*/
644 #define float128_default_nan_hi BX_CONST64(0xFFFF800000000000)
645 #define float128_default_nan_lo BX_CONST64(0x0000000000000000)
646 
647 #define float128_exp extractFloat128Exp
648 
649 /*----------------------------------------------------------------------------
650 | Returns the least-significant 64 fraction bits of the quadruple-precision
651 | floating-point value `a'.
652 *----------------------------------------------------------------------------*/
653 
extractFloat128Frac1(float128 a)654 BX_CPP_INLINE Bit64u extractFloat128Frac1(float128 a)
655 {
656     return a.lo;
657 }
658 
659 /*----------------------------------------------------------------------------
660 | Returns the most-significant 48 fraction bits of the quadruple-precision
661 | floating-point value `a'.
662 *----------------------------------------------------------------------------*/
663 
extractFloat128Frac0(float128 a)664 BX_CPP_INLINE Bit64u extractFloat128Frac0(float128 a)
665 {
666     return a.hi & BX_CONST64(0x0000FFFFFFFFFFFF);
667 }
668 
669 /*----------------------------------------------------------------------------
670 | Returns the exponent bits of the quadruple-precision floating-point value
671 | `a'.
672 *----------------------------------------------------------------------------*/
673 
extractFloat128Exp(float128 a)674 BX_CPP_INLINE Bit32s extractFloat128Exp(float128 a)
675 {
676     return ((Bit32s)(a.hi>>48)) & 0x7FFF;
677 }
678 
679 /*----------------------------------------------------------------------------
680 | Returns the sign bit of the quadruple-precision floating-point value `a'.
681 *----------------------------------------------------------------------------*/
682 
extractFloat128Sign(float128 a)683 BX_CPP_INLINE int extractFloat128Sign(float128 a)
684 {
685     return (int)(a.hi >> 63);
686 }
687 
688 /*----------------------------------------------------------------------------
689 | Packs the sign `zSign', the exponent `zExp', and the significand formed
690 | by the concatenation of `zSig0' and `zSig1' into a quadruple-precision
691 | floating-point value, returning the result.  After being shifted into the
692 | proper positions, the three fields `zSign', `zExp', and `zSig0' are simply
693 | added together to form the most significant 32 bits of the result.  This
694 | means that any integer portion of `zSig0' will be added into the exponent.
695 | Since a properly normalized significand will have an integer portion equal
696 | to 1, the `zExp' input should be 1 less than the desired result exponent
697 | whenever `zSig0' and `zSig1' concatenated form a complete, normalized
698 | significand.
699 *----------------------------------------------------------------------------*/
700 
packFloat128(int zSign,Bit32s zExp,Bit64u zSig0,Bit64u zSig1)701 BX_CPP_INLINE float128 packFloat128(int zSign, Bit32s zExp, Bit64u zSig0, Bit64u zSig1)
702 {
703     float128 z;
704     z.lo = zSig1;
705     z.hi = (((Bit64u) zSign)<<63) + (((Bit64u) zExp)<<48) + zSig0;
706     return z;
707 }
708 
709 /*----------------------------------------------------------------------------
710 | Packs two 64-bit precision integers into into the quadruple-precision
711 | floating-point value, returning the result.
712 *----------------------------------------------------------------------------*/
713 
packFloat128(Bit64u zHi,Bit64u zLo)714 BX_CPP_INLINE float128 packFloat128(Bit64u zHi, Bit64u zLo)
715 {
716     float128 z;
717     z.lo = zLo;
718     z.hi = zHi;
719     return z;
720 }
721 
722 #ifdef _MSC_VER
723 #define PACK_FLOAT_128(hi,lo) { lo, hi }
724 #else
725 #define PACK_FLOAT_128(hi,lo) packFloat128(BX_CONST64(hi),BX_CONST64(lo))
726 #endif
727 
728 /*----------------------------------------------------------------------------
729 | Returns 1 if the quadruple-precision floating-point value `a' is a NaN;
730 | otherwise returns 0.
731 *----------------------------------------------------------------------------*/
732 
float128_is_nan(float128 a)733 BX_CPP_INLINE int float128_is_nan(float128 a)
734 {
735     return (BX_CONST64(0xFFFE000000000000) <= (Bit64u) (a.hi<<1))
736         && (a.lo || (a.hi & BX_CONST64(0x0000FFFFFFFFFFFF)));
737 }
738 
739 /*----------------------------------------------------------------------------
740 | Returns 1 if the quadruple-precision floating-point value `a' is a
741 | signaling NaN; otherwise returns 0.
742 *----------------------------------------------------------------------------*/
743 
float128_is_signaling_nan(float128 a)744 BX_CPP_INLINE int float128_is_signaling_nan(float128 a)
745 {
746     return (((a.hi>>47) & 0xFFFF) == 0xFFFE)
747         && (a.lo || (a.hi & BX_CONST64(0x00007FFFFFFFFFFF)));
748 }
749 
750 /*----------------------------------------------------------------------------
751 | Returns the result of converting the quadruple-precision floating-point NaN
752 | `a' to the canonical NaN format.  If `a' is a signaling NaN, the invalid
753 | exception is raised.
754 *----------------------------------------------------------------------------*/
755 
float128ToCommonNaN(float128 a,float_status_t & status)756 BX_CPP_INLINE commonNaNT float128ToCommonNaN(float128 a, float_status_t &status)
757 {
758     commonNaNT z;
759     if (float128_is_signaling_nan(a)) float_raise(status, float_flag_invalid);
760     z.sign = (int)(a.hi>>63);
761     shortShift128Left(a.hi, a.lo, 16, &z.hi, &z.lo);
762     return z;
763 }
764 
765 /*----------------------------------------------------------------------------
766 | Returns the result of converting the canonical NaN `a' to the quadruple-
767 | precision floating-point format.
768 *----------------------------------------------------------------------------*/
769 
commonNaNToFloat128(commonNaNT a)770 BX_CPP_INLINE float128 commonNaNToFloat128(commonNaNT a)
771 {
772     float128 z;
773     shift128Right(a.hi, a.lo, 16, &z.hi, &z.lo);
774     z.hi |= (((Bit64u) a.sign)<<63) | BX_CONST64(0x7FFF800000000000);
775     return z;
776 }
777 
778 /*----------------------------------------------------------------------------
779 | Takes two quadruple-precision floating-point values `a' and `b', one of
780 | which is a NaN, and returns the appropriate NaN result.  If either `a' or
781 | `b' is a signaling NaN, the invalid exception is raised.
782 *----------------------------------------------------------------------------*/
783 
784 float128 propagateFloat128NaN(float128 a, float128 b, float_status_t &status);
785 
786 /*----------------------------------------------------------------------------
787 | The pattern for a default generated quadruple-precision NaN.
788 *----------------------------------------------------------------------------*/
789 extern const float128 float128_default_nan;
790 
791 #endif /* FLOAT128 */
792 
793 #endif
794