4 * Derived from SoftFloat.
7 /*============================================================================
9 This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
10 Arithmetic Package, Release 2b.
12 Written by John R. Hauser. This work was made possible in part by the
13 International Computer Science Institute, located at Suite 600, 1947 Center
14 Street, Berkeley, California 94704. Funding was partially provided by the
15 National Science Foundation under grant MIP-9311980. The original version
16 of this code was written as part of a project to build a fixed-point vector
17 processor in collaboration with the University of California at Berkeley,
18 overseen by Profs. Nelson Morgan and John Wawrzynek. More information
19 is available through the Web page `http://www.cs.berkeley.edu/~jhauser/
20 arithmetic/SoftFloat.html'.
22 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort has
23 been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES
24 RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS
25 AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES,
26 COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE
27 EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE
28 INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR
29 OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE.
31 Derivative works are acceptable, even for commercial purposes, so long as
32 (1) the source code for the derivative work includes prominent notice that
33 the work is derivative, and (2) the source code includes prominent notice with
34 these four paragraphs for those parts of this code that are retained.
36 =============================================================================*/
38 /*----------------------------------------------------------------------------
39 | Raises the exceptions specified by `flags'. Floating-point traps can be
40 | defined here if desired. It is currently not possible for such a trap
41 | to substitute a result value. If traps are not implemented, this routine
42 | should be simply `float_exception_flags |= flags;'.
43 *----------------------------------------------------------------------------*/
45 void float_raise( int8 flags STATUS_PARAM
)
47 STATUS(float_exception_flags
) |= flags
;
50 /*----------------------------------------------------------------------------
51 | Internal canonical NaN format.
52 *----------------------------------------------------------------------------*/
58 /*----------------------------------------------------------------------------
59 | Returns 1 if the half-precision floating-point value `a' is a quiet
60 | NaN; otherwise returns 0.
61 *----------------------------------------------------------------------------*/
63 int float16_is_quiet_nan(float16 a_
)
65 uint16_t a
= float16_val(a_
);
67 return (((a
>> 9) & 0x3F) == 0x3E) && (a
& 0x1FF);
69 return ((a
& ~0x8000) >= 0x7c80);
73 /*----------------------------------------------------------------------------
74 | Returns 1 if the half-precision floating-point value `a' is a signaling
75 | NaN; otherwise returns 0.
76 *----------------------------------------------------------------------------*/
78 int float16_is_signaling_nan(float16 a_
)
80 uint16_t a
= float16_val(a_
);
82 return ((a
& ~0x8000) >= 0x7c80);
84 return (((a
>> 9) & 0x3F) == 0x3E) && (a
& 0x1FF);
88 /*----------------------------------------------------------------------------
89 | Returns a quiet NaN if the half-precision floating point value `a' is a
90 | signaling NaN; otherwise returns `a'.
91 *----------------------------------------------------------------------------*/
92 float16
float16_maybe_silence_nan(float16 a_
)
94 if (float16_is_signaling_nan(a_
)) {
96 # if defined(TARGET_MIPS) || defined(TARGET_SH4) || defined(TARGET_UNICORE32)
97 return float16_default_nan
;
99 # error Rules for silencing a signaling NaN are target-specific
102 uint16_t a
= float16_val(a_
);
104 return make_float16(a
);
110 /*----------------------------------------------------------------------------
111 | Returns the result of converting the half-precision floating-point NaN
112 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
113 | exception is raised.
114 *----------------------------------------------------------------------------*/
116 static commonNaNT
float16ToCommonNaN( float16 a STATUS_PARAM
)
120 if ( float16_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
121 z
.sign
= float16_val(a
) >> 15;
123 z
.high
= ((uint64_t) float16_val(a
))<<54;
127 /*----------------------------------------------------------------------------
128 | Returns the result of converting the canonical NaN `a' to the half-
129 | precision floating-point format.
130 *----------------------------------------------------------------------------*/
132 static float16
commonNaNToFloat16(commonNaNT a STATUS_PARAM
)
134 uint16_t mantissa
= a
.high
>>54;
136 if (STATUS(default_nan_mode
)) {
137 return float16_default_nan
;
141 return make_float16(((((uint16_t) a
.sign
) << 15)
142 | (0x1F << 10) | mantissa
));
144 return float16_default_nan
;
148 /*----------------------------------------------------------------------------
149 | Returns 1 if the single-precision floating-point value `a' is a quiet
150 | NaN; otherwise returns 0.
151 *----------------------------------------------------------------------------*/
153 int float32_is_quiet_nan( float32 a_
)
155 uint32_t a
= float32_val(a_
);
157 return ( ( ( a
>>22 ) & 0x1FF ) == 0x1FE ) && ( a
& 0x003FFFFF );
159 return ( 0xFF800000 <= (uint32_t) ( a
<<1 ) );
163 /*----------------------------------------------------------------------------
164 | Returns 1 if the single-precision floating-point value `a' is a signaling
165 | NaN; otherwise returns 0.
166 *----------------------------------------------------------------------------*/
168 int float32_is_signaling_nan( float32 a_
)
170 uint32_t a
= float32_val(a_
);
172 return ( 0xFF800000 <= (uint32_t) ( a
<<1 ) );
174 return ( ( ( a
>>22 ) & 0x1FF ) == 0x1FE ) && ( a
& 0x003FFFFF );
178 /*----------------------------------------------------------------------------
179 | Returns a quiet NaN if the single-precision floating point value `a' is a
180 | signaling NaN; otherwise returns `a'.
181 *----------------------------------------------------------------------------*/
183 float32
float32_maybe_silence_nan( float32 a_
)
185 if (float32_is_signaling_nan(a_
)) {
187 # if defined(TARGET_MIPS) || defined(TARGET_SH4) || defined(TARGET_UNICORE32)
188 return float32_default_nan
;
190 # error Rules for silencing a signaling NaN are target-specific
193 uint32_t a
= float32_val(a_
);
195 return make_float32(a
);
201 /*----------------------------------------------------------------------------
202 | Returns the result of converting the single-precision floating-point NaN
203 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
204 | exception is raised.
205 *----------------------------------------------------------------------------*/
207 static commonNaNT
float32ToCommonNaN( float32 a STATUS_PARAM
)
211 if ( float32_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
212 z
.sign
= float32_val(a
)>>31;
214 z
.high
= ( (uint64_t) float32_val(a
) )<<41;
218 /*----------------------------------------------------------------------------
219 | Returns the result of converting the canonical NaN `a' to the single-
220 | precision floating-point format.
221 *----------------------------------------------------------------------------*/
223 static float32
commonNaNToFloat32( commonNaNT a STATUS_PARAM
)
225 uint32_t mantissa
= a
.high
>>41;
227 if ( STATUS(default_nan_mode
) ) {
228 return float32_default_nan
;
233 ( ( (uint32_t) a
.sign
)<<31 ) | 0x7F800000 | ( a
.high
>>41 ) );
235 return float32_default_nan
;
238 /*----------------------------------------------------------------------------
239 | Select which NaN to propagate for a two-input operation.
240 | IEEE754 doesn't specify all the details of this, so the
241 | algorithm is target-specific.
242 | The routine is passed various bits of information about the
243 | two NaNs and should return 0 to select NaN a and 1 for NaN b.
244 | Note that signalling NaNs are always squashed to quiet NaNs
245 | by the caller, by calling floatXX_maybe_silence_nan() before
248 | aIsLargerSignificand is only valid if both a and b are NaNs
249 | of some kind, and is true if a has the larger significand,
250 | or if both a and b have the same significand but a is
251 | positive but b is negative. It is only needed for the x87
253 *----------------------------------------------------------------------------*/
255 #if defined(TARGET_ARM)
256 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
257 flag aIsLargerSignificand
)
259 /* ARM mandated NaN propagation rules: take the first of:
260 * 1. A if it is signaling
261 * 2. B if it is signaling
264 * A signaling NaN is always quietened before returning it.
268 } else if (bIsSNaN
) {
270 } else if (aIsQNaN
) {
276 #elif defined(TARGET_MIPS)
277 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
278 flag aIsLargerSignificand
)
280 /* According to MIPS specifications, if one of the two operands is
281 * a sNaN, a new qNaN has to be generated. This is done in
282 * floatXX_maybe_silence_nan(). For qNaN inputs the specifications
283 * says: "When possible, this QNaN result is one of the operand QNaN
284 * values." In practice it seems that most implementations choose
285 * the first operand if both operands are qNaN. In short this gives
286 * the following rules:
287 * 1. A if it is signaling
288 * 2. B if it is signaling
291 * A signaling NaN is always silenced before returning it.
295 } else if (bIsSNaN
) {
297 } else if (aIsQNaN
) {
303 #elif defined(TARGET_PPC)
304 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
305 flag aIsLargerSignificand
)
307 /* PowerPC propagation rules:
308 * 1. A if it sNaN or qNaN
309 * 2. B if it sNaN or qNaN
310 * A signaling NaN is always silenced before returning it.
312 if (aIsSNaN
|| aIsQNaN
) {
319 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
320 flag aIsLargerSignificand
)
322 /* This implements x87 NaN propagation rules:
323 * SNaN + QNaN => return the QNaN
324 * two SNaNs => return the one with the larger significand, silenced
325 * two QNaNs => return the one with the larger significand
326 * SNaN and a non-NaN => return the SNaN, silenced
327 * QNaN and a non-NaN => return the QNaN
329 * If we get down to comparing significands and they are the same,
330 * return the NaN with the positive sign bit (if any).
334 return aIsLargerSignificand
? 0 : 1;
336 return bIsQNaN
? 1 : 0;
339 if (bIsSNaN
|| !bIsQNaN
)
342 return aIsLargerSignificand
? 0 : 1;
350 /*----------------------------------------------------------------------------
351 | Takes two single-precision floating-point values `a' and `b', one of which
352 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
353 | signaling NaN, the invalid exception is raised.
354 *----------------------------------------------------------------------------*/
356 static float32
propagateFloat32NaN( float32 a
, float32 b STATUS_PARAM
)
358 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
359 flag aIsLargerSignificand
;
362 aIsQuietNaN
= float32_is_quiet_nan( a
);
363 aIsSignalingNaN
= float32_is_signaling_nan( a
);
364 bIsQuietNaN
= float32_is_quiet_nan( b
);
365 bIsSignalingNaN
= float32_is_signaling_nan( b
);
369 if ( aIsSignalingNaN
| bIsSignalingNaN
) float_raise( float_flag_invalid STATUS_VAR
);
371 if ( STATUS(default_nan_mode
) )
372 return float32_default_nan
;
374 if ((uint32_t)(av
<<1) < (uint32_t)(bv
<<1)) {
375 aIsLargerSignificand
= 0;
376 } else if ((uint32_t)(bv
<<1) < (uint32_t)(av
<<1)) {
377 aIsLargerSignificand
= 1;
379 aIsLargerSignificand
= (av
< bv
) ? 1 : 0;
382 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
383 aIsLargerSignificand
)) {
384 return float32_maybe_silence_nan(b
);
386 return float32_maybe_silence_nan(a
);
390 /*----------------------------------------------------------------------------
391 | Returns 1 if the double-precision floating-point value `a' is a quiet
392 | NaN; otherwise returns 0.
393 *----------------------------------------------------------------------------*/
395 int float64_is_quiet_nan( float64 a_
)
397 uint64_t a
= float64_val(a_
);
400 ( ( ( a
>>51 ) & 0xFFF ) == 0xFFE )
401 && ( a
& LIT64( 0x0007FFFFFFFFFFFF ) );
403 return ( LIT64( 0xFFF0000000000000 ) <= (uint64_t) ( a
<<1 ) );
407 /*----------------------------------------------------------------------------
408 | Returns 1 if the double-precision floating-point value `a' is a signaling
409 | NaN; otherwise returns 0.
410 *----------------------------------------------------------------------------*/
412 int float64_is_signaling_nan( float64 a_
)
414 uint64_t a
= float64_val(a_
);
416 return ( LIT64( 0xFFF0000000000000 ) <= (uint64_t) ( a
<<1 ) );
419 ( ( ( a
>>51 ) & 0xFFF ) == 0xFFE )
420 && ( a
& LIT64( 0x0007FFFFFFFFFFFF ) );
424 /*----------------------------------------------------------------------------
425 | Returns a quiet NaN if the double-precision floating point value `a' is a
426 | signaling NaN; otherwise returns `a'.
427 *----------------------------------------------------------------------------*/
429 float64
float64_maybe_silence_nan( float64 a_
)
431 if (float64_is_signaling_nan(a_
)) {
433 # if defined(TARGET_MIPS) || defined(TARGET_SH4) || defined(TARGET_UNICORE32)
434 return float64_default_nan
;
436 # error Rules for silencing a signaling NaN are target-specific
439 uint64_t a
= float64_val(a_
);
440 a
|= LIT64( 0x0008000000000000 );
441 return make_float64(a
);
447 /*----------------------------------------------------------------------------
448 | Returns the result of converting the double-precision floating-point NaN
449 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
450 | exception is raised.
451 *----------------------------------------------------------------------------*/
453 static commonNaNT
float64ToCommonNaN( float64 a STATUS_PARAM
)
457 if ( float64_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
458 z
.sign
= float64_val(a
)>>63;
460 z
.high
= float64_val(a
)<<12;
464 /*----------------------------------------------------------------------------
465 | Returns the result of converting the canonical NaN `a' to the double-
466 | precision floating-point format.
467 *----------------------------------------------------------------------------*/
469 static float64
commonNaNToFloat64( commonNaNT a STATUS_PARAM
)
471 uint64_t mantissa
= a
.high
>>12;
473 if ( STATUS(default_nan_mode
) ) {
474 return float64_default_nan
;
479 ( ( (uint64_t) a
.sign
)<<63 )
480 | LIT64( 0x7FF0000000000000 )
483 return float64_default_nan
;
486 /*----------------------------------------------------------------------------
487 | Takes two double-precision floating-point values `a' and `b', one of which
488 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
489 | signaling NaN, the invalid exception is raised.
490 *----------------------------------------------------------------------------*/
492 static float64
propagateFloat64NaN( float64 a
, float64 b STATUS_PARAM
)
494 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
495 flag aIsLargerSignificand
;
498 aIsQuietNaN
= float64_is_quiet_nan( a
);
499 aIsSignalingNaN
= float64_is_signaling_nan( a
);
500 bIsQuietNaN
= float64_is_quiet_nan( b
);
501 bIsSignalingNaN
= float64_is_signaling_nan( b
);
505 if ( aIsSignalingNaN
| bIsSignalingNaN
) float_raise( float_flag_invalid STATUS_VAR
);
507 if ( STATUS(default_nan_mode
) )
508 return float64_default_nan
;
510 if ((uint64_t)(av
<<1) < (uint64_t)(bv
<<1)) {
511 aIsLargerSignificand
= 0;
512 } else if ((uint64_t)(bv
<<1) < (uint64_t)(av
<<1)) {
513 aIsLargerSignificand
= 1;
515 aIsLargerSignificand
= (av
< bv
) ? 1 : 0;
518 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
519 aIsLargerSignificand
)) {
520 return float64_maybe_silence_nan(b
);
522 return float64_maybe_silence_nan(a
);
528 /*----------------------------------------------------------------------------
529 | Returns 1 if the extended double-precision floating-point value `a' is a
530 | quiet NaN; otherwise returns 0. This slightly differs from the same
531 | function for other types as floatx80 has an explicit bit.
532 *----------------------------------------------------------------------------*/
534 int floatx80_is_quiet_nan( floatx80 a
)
539 aLow
= a
.low
& ~ LIT64( 0x4000000000000000 );
541 ( ( a
.high
& 0x7FFF ) == 0x7FFF )
542 && (uint64_t) ( aLow
<<1 )
543 && ( a
.low
== aLow
);
545 return ( ( a
.high
& 0x7FFF ) == 0x7FFF )
546 && (LIT64( 0x8000000000000000 ) <= ((uint64_t) ( a
.low
<<1 )));
550 /*----------------------------------------------------------------------------
551 | Returns 1 if the extended double-precision floating-point value `a' is a
552 | signaling NaN; otherwise returns 0. This slightly differs from the same
553 | function for other types as floatx80 has an explicit bit.
554 *----------------------------------------------------------------------------*/
556 int floatx80_is_signaling_nan( floatx80 a
)
559 return ( ( a
.high
& 0x7FFF ) == 0x7FFF )
560 && (LIT64( 0x8000000000000000 ) <= ((uint64_t) ( a
.low
<<1 )));
564 aLow
= a
.low
& ~ LIT64( 0x4000000000000000 );
566 ( ( a
.high
& 0x7FFF ) == 0x7FFF )
567 && (uint64_t) ( aLow
<<1 )
568 && ( a
.low
== aLow
);
572 /*----------------------------------------------------------------------------
573 | Returns a quiet NaN if the extended double-precision floating point value
574 | `a' is a signaling NaN; otherwise returns `a'.
575 *----------------------------------------------------------------------------*/
577 floatx80
floatx80_maybe_silence_nan( floatx80 a
)
579 if (floatx80_is_signaling_nan(a
)) {
581 # if defined(TARGET_MIPS) || defined(TARGET_SH4) || defined(TARGET_UNICORE32)
582 a
.low
= floatx80_default_nan_low
;
583 a
.high
= floatx80_default_nan_high
;
585 # error Rules for silencing a signaling NaN are target-specific
588 a
.low
|= LIT64( 0xC000000000000000 );
595 /*----------------------------------------------------------------------------
596 | Returns the result of converting the extended double-precision floating-
597 | point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
598 | invalid exception is raised.
599 *----------------------------------------------------------------------------*/
601 static commonNaNT
floatx80ToCommonNaN( floatx80 a STATUS_PARAM
)
605 if ( floatx80_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
607 z
.sign
= a
.high
>> 15;
611 z
.sign
= floatx80_default_nan_high
>> 15;
613 z
.high
= floatx80_default_nan_low
<< 1;
618 /*----------------------------------------------------------------------------
619 | Returns the result of converting the canonical NaN `a' to the extended
620 | double-precision floating-point format.
621 *----------------------------------------------------------------------------*/
623 static floatx80
commonNaNToFloatx80( commonNaNT a STATUS_PARAM
)
627 if ( STATUS(default_nan_mode
) ) {
628 z
.low
= floatx80_default_nan_low
;
629 z
.high
= floatx80_default_nan_high
;
634 z
.low
= LIT64( 0x8000000000000000 ) | a
.high
>> 1;
635 z
.high
= ( ( (uint16_t) a
.sign
)<<15 ) | 0x7FFF;
637 z
.low
= floatx80_default_nan_low
;
638 z
.high
= floatx80_default_nan_high
;
644 /*----------------------------------------------------------------------------
645 | Takes two extended double-precision floating-point values `a' and `b', one
646 | of which is a NaN, and returns the appropriate NaN result. If either `a' or
647 | `b' is a signaling NaN, the invalid exception is raised.
648 *----------------------------------------------------------------------------*/
650 static floatx80
propagateFloatx80NaN( floatx80 a
, floatx80 b STATUS_PARAM
)
652 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
653 flag aIsLargerSignificand
;
655 aIsQuietNaN
= floatx80_is_quiet_nan( a
);
656 aIsSignalingNaN
= floatx80_is_signaling_nan( a
);
657 bIsQuietNaN
= floatx80_is_quiet_nan( b
);
658 bIsSignalingNaN
= floatx80_is_signaling_nan( b
);
660 if ( aIsSignalingNaN
| bIsSignalingNaN
) float_raise( float_flag_invalid STATUS_VAR
);
662 if ( STATUS(default_nan_mode
) ) {
663 a
.low
= floatx80_default_nan_low
;
664 a
.high
= floatx80_default_nan_high
;
669 aIsLargerSignificand
= 0;
670 } else if (b
.low
< a
.low
) {
671 aIsLargerSignificand
= 1;
673 aIsLargerSignificand
= (a
.high
< b
.high
) ? 1 : 0;
676 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
677 aIsLargerSignificand
)) {
678 return floatx80_maybe_silence_nan(b
);
680 return floatx80_maybe_silence_nan(a
);
688 /*----------------------------------------------------------------------------
689 | Returns 1 if the quadruple-precision floating-point value `a' is a quiet
690 | NaN; otherwise returns 0.
691 *----------------------------------------------------------------------------*/
693 int float128_is_quiet_nan( float128 a
)
697 ( ( ( a
.high
>>47 ) & 0xFFFF ) == 0xFFFE )
698 && ( a
.low
|| ( a
.high
& LIT64( 0x00007FFFFFFFFFFF ) ) );
701 ( LIT64( 0xFFFE000000000000 ) <= (uint64_t) ( a
.high
<<1 ) )
702 && ( a
.low
|| ( a
.high
& LIT64( 0x0000FFFFFFFFFFFF ) ) );
706 /*----------------------------------------------------------------------------
707 | Returns 1 if the quadruple-precision floating-point value `a' is a
708 | signaling NaN; otherwise returns 0.
709 *----------------------------------------------------------------------------*/
711 int float128_is_signaling_nan( float128 a
)
715 ( LIT64( 0xFFFE000000000000 ) <= (uint64_t) ( a
.high
<<1 ) )
716 && ( a
.low
|| ( a
.high
& LIT64( 0x0000FFFFFFFFFFFF ) ) );
719 ( ( ( a
.high
>>47 ) & 0xFFFF ) == 0xFFFE )
720 && ( a
.low
|| ( a
.high
& LIT64( 0x00007FFFFFFFFFFF ) ) );
724 /*----------------------------------------------------------------------------
725 | Returns a quiet NaN if the quadruple-precision floating point value `a' is
726 | a signaling NaN; otherwise returns `a'.
727 *----------------------------------------------------------------------------*/
729 float128
float128_maybe_silence_nan( float128 a
)
731 if (float128_is_signaling_nan(a
)) {
733 # if defined(TARGET_MIPS) || defined(TARGET_SH4) || defined(TARGET_UNICORE32)
734 a
.low
= float128_default_nan_low
;
735 a
.high
= float128_default_nan_high
;
737 # error Rules for silencing a signaling NaN are target-specific
740 a
.high
|= LIT64( 0x0000800000000000 );
747 /*----------------------------------------------------------------------------
748 | Returns the result of converting the quadruple-precision floating-point NaN
749 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
750 | exception is raised.
751 *----------------------------------------------------------------------------*/
753 static commonNaNT
float128ToCommonNaN( float128 a STATUS_PARAM
)
757 if ( float128_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
759 shortShift128Left( a
.high
, a
.low
, 16, &z
.high
, &z
.low
);
763 /*----------------------------------------------------------------------------
764 | Returns the result of converting the canonical NaN `a' to the quadruple-
765 | precision floating-point format.
766 *----------------------------------------------------------------------------*/
768 static float128
commonNaNToFloat128( commonNaNT a STATUS_PARAM
)
772 if ( STATUS(default_nan_mode
) ) {
773 z
.low
= float128_default_nan_low
;
774 z
.high
= float128_default_nan_high
;
778 shift128Right( a
.high
, a
.low
, 16, &z
.high
, &z
.low
);
779 z
.high
|= ( ( (uint64_t) a
.sign
)<<63 ) | LIT64( 0x7FFF000000000000 );
783 /*----------------------------------------------------------------------------
784 | Takes two quadruple-precision floating-point values `a' and `b', one of
785 | which is a NaN, and returns the appropriate NaN result. If either `a' or
786 | `b' is a signaling NaN, the invalid exception is raised.
787 *----------------------------------------------------------------------------*/
789 static float128
propagateFloat128NaN( float128 a
, float128 b STATUS_PARAM
)
791 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
792 flag aIsLargerSignificand
;
794 aIsQuietNaN
= float128_is_quiet_nan( a
);
795 aIsSignalingNaN
= float128_is_signaling_nan( a
);
796 bIsQuietNaN
= float128_is_quiet_nan( b
);
797 bIsSignalingNaN
= float128_is_signaling_nan( b
);
799 if ( aIsSignalingNaN
| bIsSignalingNaN
) float_raise( float_flag_invalid STATUS_VAR
);
801 if ( STATUS(default_nan_mode
) ) {
802 a
.low
= float128_default_nan_low
;
803 a
.high
= float128_default_nan_high
;
807 if (lt128(a
.high
<<1, a
.low
, b
.high
<<1, b
.low
)) {
808 aIsLargerSignificand
= 0;
809 } else if (lt128(b
.high
<<1, b
.low
, a
.high
<<1, a
.low
)) {
810 aIsLargerSignificand
= 1;
812 aIsLargerSignificand
= (a
.high
< b
.high
) ? 1 : 0;
815 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
816 aIsLargerSignificand
)) {
817 return float128_maybe_silence_nan(b
);
819 return float128_maybe_silence_nan(a
);