2 /*============================================================================
4 This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
5 Arithmetic Package, Release 2b.
7 Written by John R. Hauser. This work was made possible in part by the
8 International Computer Science Institute, located at Suite 600, 1947 Center
9 Street, Berkeley, California 94704. Funding was partially provided by the
10 National Science Foundation under grant MIP-9311980. The original version
11 of this code was written as part of a project to build a fixed-point vector
12 processor in collaboration with the University of California at Berkeley,
13 overseen by Profs. Nelson Morgan and John Wawrzynek. More information
14 is available through the Web page `http://www.cs.berkeley.edu/~jhauser/
15 arithmetic/SoftFloat.html'.
17 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort has
18 been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES
19 RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS
20 AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES,
21 COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE
22 EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE
23 INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR
24 OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE.
26 Derivative works are acceptable, even for commercial purposes, so long as
27 (1) the source code for the derivative work includes prominent notice that
28 the work is derivative, and (2) the source code includes prominent notice with
29 these four paragraphs for those parts of this code that are retained.
31 =============================================================================*/
33 #if defined(TARGET_MIPS) || defined(TARGET_SH4)
34 #define SNAN_BIT_IS_ONE 1
36 #define SNAN_BIT_IS_ONE 0
39 /*----------------------------------------------------------------------------
40 | Raises the exceptions specified by `flags'. Floating-point traps can be
41 | defined here if desired. It is currently not possible for such a trap
42 | to substitute a result value. If traps are not implemented, this routine
43 | should be simply `float_exception_flags |= flags;'.
44 *----------------------------------------------------------------------------*/
46 void float_raise( int8 flags STATUS_PARAM
)
48 STATUS(float_exception_flags
) |= flags
;
51 /*----------------------------------------------------------------------------
52 | Internal canonical NaN format.
53 *----------------------------------------------------------------------------*/
59 /*----------------------------------------------------------------------------
60 | The pattern for a default generated half-precision NaN.
61 *----------------------------------------------------------------------------*/
62 #if defined(TARGET_ARM)
63 #define float16_default_nan make_float16(0x7E00)
65 #define float16_default_nan make_float16(0x7DFF)
67 #define float16_default_nan make_float16(0xFE00)
70 /*----------------------------------------------------------------------------
71 | Returns 1 if the half-precision floating-point value `a' is a quiet
72 | NaN; otherwise returns 0.
73 *----------------------------------------------------------------------------*/
75 int float16_is_quiet_nan(float16 a_
)
77 uint16_t a
= float16_val(a_
);
79 return (((a
>> 9) & 0x3F) == 0x3E) && (a
& 0x1FF);
81 return ((a
& ~0x8000) >= 0x7c80);
85 /*----------------------------------------------------------------------------
86 | Returns 1 if the half-precision floating-point value `a' is a signaling
87 | NaN; otherwise returns 0.
88 *----------------------------------------------------------------------------*/
90 int float16_is_signaling_nan(float16 a_
)
92 uint16_t a
= float16_val(a_
);
94 return ((a
& ~0x8000) >= 0x7c80);
96 return (((a
>> 9) & 0x3F) == 0x3E) && (a
& 0x1FF);
100 /*----------------------------------------------------------------------------
101 | Returns a quiet NaN if the half-precision floating point value `a' is a
102 | signaling NaN; otherwise returns `a'.
103 *----------------------------------------------------------------------------*/
104 float16
float16_maybe_silence_nan(float16 a_
)
106 if (float16_is_signaling_nan(a_
)) {
108 # if defined(TARGET_MIPS) || defined(TARGET_SH4)
109 return float16_default_nan
;
111 # error Rules for silencing a signaling NaN are target-specific
114 uint16_t a
= float16_val(a_
);
116 return make_float16(a
);
122 /*----------------------------------------------------------------------------
123 | Returns the result of converting the half-precision floating-point NaN
124 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
125 | exception is raised.
126 *----------------------------------------------------------------------------*/
128 static commonNaNT
float16ToCommonNaN( float16 a STATUS_PARAM
)
132 if ( float16_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
133 z
.sign
= float16_val(a
) >> 15;
135 z
.high
= ((bits64
) float16_val(a
))<<54;
139 /*----------------------------------------------------------------------------
140 | Returns the result of converting the canonical NaN `a' to the half-
141 | precision floating-point format.
142 *----------------------------------------------------------------------------*/
144 static float16
commonNaNToFloat16(commonNaNT a STATUS_PARAM
)
146 uint16_t mantissa
= a
.high
>>54;
148 if (STATUS(default_nan_mode
)) {
149 return float16_default_nan
;
153 return make_float16(((((uint16_t) a
.sign
) << 15)
154 | (0x1F << 10) | mantissa
));
156 return float16_default_nan
;
160 /*----------------------------------------------------------------------------
161 | The pattern for a default generated single-precision NaN.
162 *----------------------------------------------------------------------------*/
163 #if defined(TARGET_SPARC)
164 #define float32_default_nan make_float32(0x7FFFFFFF)
165 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA)
166 #define float32_default_nan make_float32(0x7FC00000)
167 #elif SNAN_BIT_IS_ONE
168 #define float32_default_nan make_float32(0x7FBFFFFF)
170 #define float32_default_nan make_float32(0xFFC00000)
173 /*----------------------------------------------------------------------------
174 | Returns 1 if the single-precision floating-point value `a' is a quiet
175 | NaN; otherwise returns 0.
176 *----------------------------------------------------------------------------*/
178 int float32_is_quiet_nan( float32 a_
)
180 uint32_t a
= float32_val(a_
);
182 return ( ( ( a
>>22 ) & 0x1FF ) == 0x1FE ) && ( a
& 0x003FFFFF );
184 return ( 0xFF800000 <= (bits32
) ( a
<<1 ) );
188 /*----------------------------------------------------------------------------
189 | Returns 1 if the single-precision floating-point value `a' is a signaling
190 | NaN; otherwise returns 0.
191 *----------------------------------------------------------------------------*/
193 int float32_is_signaling_nan( float32 a_
)
195 uint32_t a
= float32_val(a_
);
197 return ( 0xFF800000 <= (bits32
) ( a
<<1 ) );
199 return ( ( ( a
>>22 ) & 0x1FF ) == 0x1FE ) && ( a
& 0x003FFFFF );
203 /*----------------------------------------------------------------------------
204 | Returns a quiet NaN if the single-precision floating point value `a' is a
205 | signaling NaN; otherwise returns `a'.
206 *----------------------------------------------------------------------------*/
208 float32
float32_maybe_silence_nan( float32 a_
)
210 if (float32_is_signaling_nan(a_
)) {
212 # if defined(TARGET_MIPS) || defined(TARGET_SH4)
213 return float32_default_nan
;
215 # error Rules for silencing a signaling NaN are target-specific
218 bits32 a
= float32_val(a_
);
220 return make_float32(a
);
226 /*----------------------------------------------------------------------------
227 | Returns the result of converting the single-precision floating-point NaN
228 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
229 | exception is raised.
230 *----------------------------------------------------------------------------*/
232 static commonNaNT
float32ToCommonNaN( float32 a STATUS_PARAM
)
236 if ( float32_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
237 z
.sign
= float32_val(a
)>>31;
239 z
.high
= ( (bits64
) float32_val(a
) )<<41;
243 /*----------------------------------------------------------------------------
244 | Returns the result of converting the canonical NaN `a' to the single-
245 | precision floating-point format.
246 *----------------------------------------------------------------------------*/
248 static float32
commonNaNToFloat32( commonNaNT a STATUS_PARAM
)
250 bits32 mantissa
= a
.high
>>41;
252 if ( STATUS(default_nan_mode
) ) {
253 return float32_default_nan
;
258 ( ( (bits32
) a
.sign
)<<31 ) | 0x7F800000 | ( a
.high
>>41 ) );
260 return float32_default_nan
;
263 /*----------------------------------------------------------------------------
264 | Select which NaN to propagate for a two-input operation.
265 | IEEE754 doesn't specify all the details of this, so the
266 | algorithm is target-specific.
267 | The routine is passed various bits of information about the
268 | two NaNs and should return 0 to select NaN a and 1 for NaN b.
269 | Note that signalling NaNs are always squashed to quiet NaNs
270 | by the caller, by calling floatXX_maybe_silence_nan() before
273 | aIsLargerSignificand is only valid if both a and b are NaNs
274 | of some kind, and is true if a has the larger significand,
275 | or if both a and b have the same significand but a is
276 | positive but b is negative. It is only needed for the x87
278 *----------------------------------------------------------------------------*/
280 #if defined(TARGET_ARM)
281 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
282 flag aIsLargerSignificand
)
284 /* ARM mandated NaN propagation rules: take the first of:
285 * 1. A if it is signaling
286 * 2. B if it is signaling
289 * A signaling NaN is always quietened before returning it.
293 } else if (bIsSNaN
) {
295 } else if (aIsQNaN
) {
301 #elif defined(TARGET_MIPS)
302 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
303 flag aIsLargerSignificand
)
305 /* According to MIPS specifications, if one of the two operands is
306 * a sNaN, a new qNaN has to be generated. This is done in
307 * floatXX_maybe_silence_nan(). For qNaN inputs the specifications
308 * says: "When possible, this QNaN result is one of the operand QNaN
309 * values." In practice it seems that most implementations choose
310 * the first operand if both operands are qNaN. In short this gives
311 * the following rules:
312 * 1. A if it is signaling
313 * 2. B if it is signaling
316 * A signaling NaN is always silenced before returning it.
320 } else if (bIsSNaN
) {
322 } else if (aIsQNaN
) {
328 #elif defined(TARGET_PPC)
329 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
330 flag aIsLargerSignificand
)
332 /* PowerPC propagation rules:
333 * 1. A if it sNaN or qNaN
334 * 2. B if it sNaN or qNaN
335 * A signaling NaN is always silenced before returning it.
337 if (aIsSNaN
|| aIsQNaN
) {
344 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
345 flag aIsLargerSignificand
)
347 /* This implements x87 NaN propagation rules:
348 * SNaN + QNaN => return the QNaN
349 * two SNaNs => return the one with the larger significand, silenced
350 * two QNaNs => return the one with the larger significand
351 * SNaN and a non-NaN => return the SNaN, silenced
352 * QNaN and a non-NaN => return the QNaN
354 * If we get down to comparing significands and they are the same,
355 * return the NaN with the positive sign bit (if any).
359 return aIsLargerSignificand
? 0 : 1;
361 return bIsQNaN
? 1 : 0;
364 if (bIsSNaN
|| !bIsQNaN
)
367 return aIsLargerSignificand
? 0 : 1;
375 /*----------------------------------------------------------------------------
376 | Takes two single-precision floating-point values `a' and `b', one of which
377 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
378 | signaling NaN, the invalid exception is raised.
379 *----------------------------------------------------------------------------*/
381 static float32
propagateFloat32NaN( float32 a
, float32 b STATUS_PARAM
)
383 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
384 flag aIsLargerSignificand
;
387 aIsQuietNaN
= float32_is_quiet_nan( a
);
388 aIsSignalingNaN
= float32_is_signaling_nan( a
);
389 bIsQuietNaN
= float32_is_quiet_nan( b
);
390 bIsSignalingNaN
= float32_is_signaling_nan( b
);
394 if ( aIsSignalingNaN
| bIsSignalingNaN
) float_raise( float_flag_invalid STATUS_VAR
);
396 if ( STATUS(default_nan_mode
) )
397 return float32_default_nan
;
399 if ((bits32
)(av
<<1) < (bits32
)(bv
<<1)) {
400 aIsLargerSignificand
= 0;
401 } else if ((bits32
)(bv
<<1) < (bits32
)(av
<<1)) {
402 aIsLargerSignificand
= 1;
404 aIsLargerSignificand
= (av
< bv
) ? 1 : 0;
407 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
408 aIsLargerSignificand
)) {
409 return float32_maybe_silence_nan(b
);
411 return float32_maybe_silence_nan(a
);
415 /*----------------------------------------------------------------------------
416 | The pattern for a default generated double-precision NaN.
417 *----------------------------------------------------------------------------*/
418 #if defined(TARGET_SPARC)
419 #define float64_default_nan make_float64(LIT64( 0x7FFFFFFFFFFFFFFF ))
420 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA)
421 #define float64_default_nan make_float64(LIT64( 0x7FF8000000000000 ))
422 #elif SNAN_BIT_IS_ONE
423 #define float64_default_nan make_float64(LIT64( 0x7FF7FFFFFFFFFFFF ))
425 #define float64_default_nan make_float64(LIT64( 0xFFF8000000000000 ))
428 /*----------------------------------------------------------------------------
429 | Returns 1 if the double-precision floating-point value `a' is a quiet
430 | NaN; otherwise returns 0.
431 *----------------------------------------------------------------------------*/
433 int float64_is_quiet_nan( float64 a_
)
435 bits64 a
= float64_val(a_
);
438 ( ( ( a
>>51 ) & 0xFFF ) == 0xFFE )
439 && ( a
& LIT64( 0x0007FFFFFFFFFFFF ) );
441 return ( LIT64( 0xFFF0000000000000 ) <= (bits64
) ( a
<<1 ) );
445 /*----------------------------------------------------------------------------
446 | Returns 1 if the double-precision floating-point value `a' is a signaling
447 | NaN; otherwise returns 0.
448 *----------------------------------------------------------------------------*/
450 int float64_is_signaling_nan( float64 a_
)
452 bits64 a
= float64_val(a_
);
454 return ( LIT64( 0xFFF0000000000000 ) <= (bits64
) ( a
<<1 ) );
457 ( ( ( a
>>51 ) & 0xFFF ) == 0xFFE )
458 && ( a
& LIT64( 0x0007FFFFFFFFFFFF ) );
462 /*----------------------------------------------------------------------------
463 | Returns a quiet NaN if the double-precision floating point value `a' is a
464 | signaling NaN; otherwise returns `a'.
465 *----------------------------------------------------------------------------*/
467 float64
float64_maybe_silence_nan( float64 a_
)
469 if (float64_is_signaling_nan(a_
)) {
471 # if defined(TARGET_MIPS) || defined(TARGET_SH4)
472 return float64_default_nan
;
474 # error Rules for silencing a signaling NaN are target-specific
477 bits64 a
= float64_val(a_
);
478 a
|= LIT64( 0x0008000000000000 );
479 return make_float64(a
);
485 /*----------------------------------------------------------------------------
486 | Returns the result of converting the double-precision floating-point NaN
487 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
488 | exception is raised.
489 *----------------------------------------------------------------------------*/
491 static commonNaNT
float64ToCommonNaN( float64 a STATUS_PARAM
)
495 if ( float64_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
496 z
.sign
= float64_val(a
)>>63;
498 z
.high
= float64_val(a
)<<12;
502 /*----------------------------------------------------------------------------
503 | Returns the result of converting the canonical NaN `a' to the double-
504 | precision floating-point format.
505 *----------------------------------------------------------------------------*/
507 static float64
commonNaNToFloat64( commonNaNT a STATUS_PARAM
)
509 bits64 mantissa
= a
.high
>>12;
511 if ( STATUS(default_nan_mode
) ) {
512 return float64_default_nan
;
517 ( ( (bits64
) a
.sign
)<<63 )
518 | LIT64( 0x7FF0000000000000 )
521 return float64_default_nan
;
524 /*----------------------------------------------------------------------------
525 | Takes two double-precision floating-point values `a' and `b', one of which
526 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
527 | signaling NaN, the invalid exception is raised.
528 *----------------------------------------------------------------------------*/
530 static float64
propagateFloat64NaN( float64 a
, float64 b STATUS_PARAM
)
532 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
533 flag aIsLargerSignificand
;
536 aIsQuietNaN
= float64_is_quiet_nan( a
);
537 aIsSignalingNaN
= float64_is_signaling_nan( a
);
538 bIsQuietNaN
= float64_is_quiet_nan( b
);
539 bIsSignalingNaN
= float64_is_signaling_nan( b
);
543 if ( aIsSignalingNaN
| bIsSignalingNaN
) float_raise( float_flag_invalid STATUS_VAR
);
545 if ( STATUS(default_nan_mode
) )
546 return float64_default_nan
;
548 if ((bits64
)(av
<<1) < (bits64
)(bv
<<1)) {
549 aIsLargerSignificand
= 0;
550 } else if ((bits64
)(bv
<<1) < (bits64
)(av
<<1)) {
551 aIsLargerSignificand
= 1;
553 aIsLargerSignificand
= (av
< bv
) ? 1 : 0;
556 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
557 aIsLargerSignificand
)) {
558 return float64_maybe_silence_nan(b
);
560 return float64_maybe_silence_nan(a
);
566 /*----------------------------------------------------------------------------
567 | The pattern for a default generated extended double-precision NaN. The
568 | `high' and `low' values hold the most- and least-significant bits,
570 *----------------------------------------------------------------------------*/
572 #define floatx80_default_nan_high 0x7FFF
573 #define floatx80_default_nan_low LIT64( 0xBFFFFFFFFFFFFFFF )
575 #define floatx80_default_nan_high 0xFFFF
576 #define floatx80_default_nan_low LIT64( 0xC000000000000000 )
579 /*----------------------------------------------------------------------------
580 | Returns 1 if the extended double-precision floating-point value `a' is a
581 | quiet NaN; otherwise returns 0. This slightly differs from the same
582 | function for other types as floatx80 has an explicit bit.
583 *----------------------------------------------------------------------------*/
585 int floatx80_is_quiet_nan( floatx80 a
)
590 aLow
= a
.low
& ~ LIT64( 0x4000000000000000 );
592 ( ( a
.high
& 0x7FFF ) == 0x7FFF )
593 && (bits64
) ( aLow
<<1 )
594 && ( a
.low
== aLow
);
596 return ( ( a
.high
& 0x7FFF ) == 0x7FFF )
597 && (LIT64( 0x8000000000000000 ) <= ((bits64
) ( a
.low
<<1 )));
601 /*----------------------------------------------------------------------------
602 | Returns 1 if the extended double-precision floating-point value `a' is a
603 | signaling NaN; otherwise returns 0. This slightly differs from the same
604 | function for other types as floatx80 has an explicit bit.
605 *----------------------------------------------------------------------------*/
607 int floatx80_is_signaling_nan( floatx80 a
)
610 return ( ( a
.high
& 0x7FFF ) == 0x7FFF )
611 && (LIT64( 0x8000000000000000 ) <= ((bits64
) ( a
.low
<<1 )));
615 aLow
= a
.low
& ~ LIT64( 0x4000000000000000 );
617 ( ( a
.high
& 0x7FFF ) == 0x7FFF )
618 && (bits64
) ( aLow
<<1 )
619 && ( a
.low
== aLow
);
623 /*----------------------------------------------------------------------------
624 | Returns a quiet NaN if the extended double-precision floating point value
625 | `a' is a signaling NaN; otherwise returns `a'.
626 *----------------------------------------------------------------------------*/
628 floatx80
floatx80_maybe_silence_nan( floatx80 a
)
630 if (floatx80_is_signaling_nan(a
)) {
632 # if defined(TARGET_MIPS) || defined(TARGET_SH4)
633 a
.low
= floatx80_default_nan_low
;
634 a
.high
= floatx80_default_nan_high
;
636 # error Rules for silencing a signaling NaN are target-specific
639 a
.low
|= LIT64( 0xC000000000000000 );
646 /*----------------------------------------------------------------------------
647 | Returns the result of converting the extended double-precision floating-
648 | point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
649 | invalid exception is raised.
650 *----------------------------------------------------------------------------*/
652 static commonNaNT
floatx80ToCommonNaN( floatx80 a STATUS_PARAM
)
656 if ( floatx80_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
663 /*----------------------------------------------------------------------------
664 | Returns the result of converting the canonical NaN `a' to the extended
665 | double-precision floating-point format.
666 *----------------------------------------------------------------------------*/
668 static floatx80
commonNaNToFloatx80( commonNaNT a STATUS_PARAM
)
672 if ( STATUS(default_nan_mode
) ) {
673 z
.low
= floatx80_default_nan_low
;
674 z
.high
= floatx80_default_nan_high
;
681 z
.low
= floatx80_default_nan_low
;
682 z
.high
= ( ( (bits16
) a
.sign
)<<15 ) | 0x7FFF;
686 /*----------------------------------------------------------------------------
687 | Takes two extended double-precision floating-point values `a' and `b', one
688 | of which is a NaN, and returns the appropriate NaN result. If either `a' or
689 | `b' is a signaling NaN, the invalid exception is raised.
690 *----------------------------------------------------------------------------*/
692 static floatx80
propagateFloatx80NaN( floatx80 a
, floatx80 b STATUS_PARAM
)
694 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
695 flag aIsLargerSignificand
;
697 aIsQuietNaN
= floatx80_is_quiet_nan( a
);
698 aIsSignalingNaN
= floatx80_is_signaling_nan( a
);
699 bIsQuietNaN
= floatx80_is_quiet_nan( b
);
700 bIsSignalingNaN
= floatx80_is_signaling_nan( b
);
702 if ( aIsSignalingNaN
| bIsSignalingNaN
) float_raise( float_flag_invalid STATUS_VAR
);
704 if ( STATUS(default_nan_mode
) ) {
705 a
.low
= floatx80_default_nan_low
;
706 a
.high
= floatx80_default_nan_high
;
711 aIsLargerSignificand
= 0;
712 } else if (b
.low
< a
.low
) {
713 aIsLargerSignificand
= 1;
715 aIsLargerSignificand
= (a
.high
< b
.high
) ? 1 : 0;
718 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
719 aIsLargerSignificand
)) {
720 return floatx80_maybe_silence_nan(b
);
722 return floatx80_maybe_silence_nan(a
);
730 /*----------------------------------------------------------------------------
731 | The pattern for a default generated quadruple-precision NaN. The `high' and
732 | `low' values hold the most- and least-significant bits, respectively.
733 *----------------------------------------------------------------------------*/
735 #define float128_default_nan_high LIT64( 0x7FFF7FFFFFFFFFFF )
736 #define float128_default_nan_low LIT64( 0xFFFFFFFFFFFFFFFF )
738 #define float128_default_nan_high LIT64( 0xFFFF800000000000 )
739 #define float128_default_nan_low LIT64( 0x0000000000000000 )
742 /*----------------------------------------------------------------------------
743 | Returns 1 if the quadruple-precision floating-point value `a' is a quiet
744 | NaN; otherwise returns 0.
745 *----------------------------------------------------------------------------*/
747 int float128_is_quiet_nan( float128 a
)
751 ( ( ( a
.high
>>47 ) & 0xFFFF ) == 0xFFFE )
752 && ( a
.low
|| ( a
.high
& LIT64( 0x00007FFFFFFFFFFF ) ) );
755 ( LIT64( 0xFFFE000000000000 ) <= (bits64
) ( a
.high
<<1 ) )
756 && ( a
.low
|| ( a
.high
& LIT64( 0x0000FFFFFFFFFFFF ) ) );
760 /*----------------------------------------------------------------------------
761 | Returns 1 if the quadruple-precision floating-point value `a' is a
762 | signaling NaN; otherwise returns 0.
763 *----------------------------------------------------------------------------*/
765 int float128_is_signaling_nan( float128 a
)
769 ( LIT64( 0xFFFE000000000000 ) <= (bits64
) ( a
.high
<<1 ) )
770 && ( a
.low
|| ( a
.high
& LIT64( 0x0000FFFFFFFFFFFF ) ) );
773 ( ( ( a
.high
>>47 ) & 0xFFFF ) == 0xFFFE )
774 && ( a
.low
|| ( a
.high
& LIT64( 0x00007FFFFFFFFFFF ) ) );
778 /*----------------------------------------------------------------------------
779 | Returns a quiet NaN if the quadruple-precision floating point value `a' is
780 | a signaling NaN; otherwise returns `a'.
781 *----------------------------------------------------------------------------*/
783 float128
float128_maybe_silence_nan( float128 a
)
785 if (float128_is_signaling_nan(a
)) {
787 # if defined(TARGET_MIPS) || defined(TARGET_SH4)
788 a
.low
= float128_default_nan_low
;
789 a
.high
= float128_default_nan_high
;
791 # error Rules for silencing a signaling NaN are target-specific
794 a
.high
|= LIT64( 0x0000800000000000 );
801 /*----------------------------------------------------------------------------
802 | Returns the result of converting the quadruple-precision floating-point NaN
803 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
804 | exception is raised.
805 *----------------------------------------------------------------------------*/
807 static commonNaNT
float128ToCommonNaN( float128 a STATUS_PARAM
)
811 if ( float128_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
813 shortShift128Left( a
.high
, a
.low
, 16, &z
.high
, &z
.low
);
817 /*----------------------------------------------------------------------------
818 | Returns the result of converting the canonical NaN `a' to the quadruple-
819 | precision floating-point format.
820 *----------------------------------------------------------------------------*/
822 static float128
commonNaNToFloat128( commonNaNT a STATUS_PARAM
)
826 if ( STATUS(default_nan_mode
) ) {
827 z
.low
= float128_default_nan_low
;
828 z
.high
= float128_default_nan_high
;
832 shift128Right( a
.high
, a
.low
, 16, &z
.high
, &z
.low
);
833 z
.high
|= ( ( (bits64
) a
.sign
)<<63 ) | LIT64( 0x7FFF000000000000 );
837 /*----------------------------------------------------------------------------
838 | Takes two quadruple-precision floating-point values `a' and `b', one of
839 | which is a NaN, and returns the appropriate NaN result. If either `a' or
840 | `b' is a signaling NaN, the invalid exception is raised.
841 *----------------------------------------------------------------------------*/
843 static float128
propagateFloat128NaN( float128 a
, float128 b STATUS_PARAM
)
845 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
846 flag aIsLargerSignificand
;
848 aIsQuietNaN
= float128_is_quiet_nan( a
);
849 aIsSignalingNaN
= float128_is_signaling_nan( a
);
850 bIsQuietNaN
= float128_is_quiet_nan( b
);
851 bIsSignalingNaN
= float128_is_signaling_nan( b
);
853 if ( aIsSignalingNaN
| bIsSignalingNaN
) float_raise( float_flag_invalid STATUS_VAR
);
855 if ( STATUS(default_nan_mode
) ) {
856 a
.low
= float128_default_nan_low
;
857 a
.high
= float128_default_nan_high
;
861 if (lt128(a
.high
<<1, a
.low
, b
.high
<<1, b
.low
)) {
862 aIsLargerSignificand
= 0;
863 } else if (lt128(b
.high
<<1, b
.low
, a
.high
<<1, a
.low
)) {
864 aIsLargerSignificand
= 1;
866 aIsLargerSignificand
= (a
.high
< b
.high
) ? 1 : 0;
869 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
870 aIsLargerSignificand
)) {
871 return float128_maybe_silence_nan(b
);
873 return float128_maybe_silence_nan(a
);