4 * Derived from SoftFloat.
8 ===============================================================================
9 This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
10 Arithmetic Package, Release 2a.
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://HTTP.CS.Berkeley.EDU/~jhauser/
20 arithmetic/SoftFloat.html'.
22 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
23 has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
24 TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
25 PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
26 AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
28 Derivative works are acceptable, even for commercial purposes, so long as
29 (1) they include prominent notice that the work is derivative, and (2) they
30 include prominent notice akin to these four paragraphs for those parts of
31 this code that are retained.
33 ===============================================================================
36 /* Does the target distinguish signaling NaNs from non-signaling NaNs
37 * by setting the most significant bit of the mantissa for a signaling NaN?
38 * (The more common choice is to have it be zero for SNaN and one for QNaN.)
40 #if defined(TARGET_MIPS) || defined(TARGET_SH4) || defined(TARGET_UNICORE32)
41 #define SNAN_BIT_IS_ONE 1
43 #define SNAN_BIT_IS_ONE 0
46 #if defined(TARGET_XTENSA)
47 /* Define for architectures which deviate from IEEE in not supporting
48 * signaling NaNs (so all NaNs are treated as quiet).
50 #define NO_SIGNALING_NANS 1
53 /*----------------------------------------------------------------------------
54 | The pattern for a default generated half-precision NaN.
55 *----------------------------------------------------------------------------*/
56 #if defined(TARGET_ARM)
57 const float16 float16_default_nan
= const_float16(0x7E00);
59 const float16 float16_default_nan
= const_float16(0x7DFF);
61 const float16 float16_default_nan
= const_float16(0xFE00);
64 /*----------------------------------------------------------------------------
65 | The pattern for a default generated single-precision NaN.
66 *----------------------------------------------------------------------------*/
67 #if defined(TARGET_SPARC)
68 const float32 float32_default_nan
= const_float32(0x7FFFFFFF);
69 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA) || \
70 defined(TARGET_XTENSA)
71 const float32 float32_default_nan
= const_float32(0x7FC00000);
73 const float32 float32_default_nan
= const_float32(0x7FBFFFFF);
75 const float32 float32_default_nan
= const_float32(0xFFC00000);
78 /*----------------------------------------------------------------------------
79 | The pattern for a default generated double-precision NaN.
80 *----------------------------------------------------------------------------*/
81 #if defined(TARGET_SPARC)
82 const float64 float64_default_nan
= const_float64(LIT64( 0x7FFFFFFFFFFFFFFF ));
83 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA)
84 const float64 float64_default_nan
= const_float64(LIT64( 0x7FF8000000000000 ));
86 const float64 float64_default_nan
= const_float64(LIT64(0x7FF7FFFFFFFFFFFF));
88 const float64 float64_default_nan
= const_float64(LIT64( 0xFFF8000000000000 ));
91 /*----------------------------------------------------------------------------
92 | The pattern for a default generated extended double-precision NaN.
93 *----------------------------------------------------------------------------*/
95 #define floatx80_default_nan_high 0x7FFF
96 #define floatx80_default_nan_low LIT64(0xBFFFFFFFFFFFFFFF)
98 #define floatx80_default_nan_high 0xFFFF
99 #define floatx80_default_nan_low LIT64( 0xC000000000000000 )
102 const floatx80 floatx80_default_nan
103 = make_floatx80_init(floatx80_default_nan_high
, floatx80_default_nan_low
);
105 /*----------------------------------------------------------------------------
106 | The pattern for a default generated quadruple-precision NaN. The `high' and
107 | `low' values hold the most- and least-significant bits, respectively.
108 *----------------------------------------------------------------------------*/
110 #define float128_default_nan_high LIT64(0x7FFF7FFFFFFFFFFF)
111 #define float128_default_nan_low LIT64(0xFFFFFFFFFFFFFFFF)
113 #define float128_default_nan_high LIT64( 0xFFFF800000000000 )
114 #define float128_default_nan_low LIT64( 0x0000000000000000 )
117 const float128 float128_default_nan
118 = make_float128_init(float128_default_nan_high
, float128_default_nan_low
);
120 /*----------------------------------------------------------------------------
121 | Raises the exceptions specified by `flags'. Floating-point traps can be
122 | defined here if desired. It is currently not possible for such a trap
123 | to substitute a result value. If traps are not implemented, this routine
124 | should be simply `float_exception_flags |= flags;'.
125 *----------------------------------------------------------------------------*/
127 void float_raise( int8 flags STATUS_PARAM
)
129 STATUS(float_exception_flags
) |= flags
;
132 /*----------------------------------------------------------------------------
133 | Internal canonical NaN format.
134 *----------------------------------------------------------------------------*/
140 #ifdef NO_SIGNALING_NANS
141 int float16_is_quiet_nan(float16 a_
)
143 return float16_is_any_nan(a_
);
146 int float16_is_signaling_nan(float16 a_
)
151 /*----------------------------------------------------------------------------
152 | Returns 1 if the half-precision floating-point value `a' is a quiet
153 | NaN; otherwise returns 0.
154 *----------------------------------------------------------------------------*/
156 int float16_is_quiet_nan(float16 a_
)
158 uint16_t a
= float16_val(a_
);
160 return (((a
>> 9) & 0x3F) == 0x3E) && (a
& 0x1FF);
162 return ((a
& ~0x8000) >= 0x7c80);
166 /*----------------------------------------------------------------------------
167 | Returns 1 if the half-precision floating-point value `a' is a signaling
168 | NaN; otherwise returns 0.
169 *----------------------------------------------------------------------------*/
171 int float16_is_signaling_nan(float16 a_
)
173 uint16_t a
= float16_val(a_
);
175 return ((a
& ~0x8000) >= 0x7c80);
177 return (((a
>> 9) & 0x3F) == 0x3E) && (a
& 0x1FF);
182 /*----------------------------------------------------------------------------
183 | Returns a quiet NaN if the half-precision floating point value `a' is a
184 | signaling NaN; otherwise returns `a'.
185 *----------------------------------------------------------------------------*/
186 float16
float16_maybe_silence_nan(float16 a_
)
188 if (float16_is_signaling_nan(a_
)) {
190 # if defined(TARGET_MIPS) || defined(TARGET_SH4) || defined(TARGET_UNICORE32)
191 return float16_default_nan
;
193 # error Rules for silencing a signaling NaN are target-specific
196 uint16_t a
= float16_val(a_
);
198 return make_float16(a
);
204 /*----------------------------------------------------------------------------
205 | Returns the result of converting the half-precision floating-point NaN
206 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
207 | exception is raised.
208 *----------------------------------------------------------------------------*/
210 static commonNaNT
float16ToCommonNaN( float16 a STATUS_PARAM
)
214 if ( float16_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
215 z
.sign
= float16_val(a
) >> 15;
217 z
.high
= ((uint64_t) float16_val(a
))<<54;
221 /*----------------------------------------------------------------------------
222 | Returns the result of converting the canonical NaN `a' to the half-
223 | precision floating-point format.
224 *----------------------------------------------------------------------------*/
226 static float16
commonNaNToFloat16(commonNaNT a STATUS_PARAM
)
228 uint16_t mantissa
= a
.high
>>54;
230 if (STATUS(default_nan_mode
)) {
231 return float16_default_nan
;
235 return make_float16(((((uint16_t) a
.sign
) << 15)
236 | (0x1F << 10) | mantissa
));
238 return float16_default_nan
;
242 #ifdef NO_SIGNALING_NANS
243 int float32_is_quiet_nan(float32 a_
)
245 return float32_is_any_nan(a_
);
248 int float32_is_signaling_nan(float32 a_
)
253 /*----------------------------------------------------------------------------
254 | Returns 1 if the single-precision floating-point value `a' is a quiet
255 | NaN; otherwise returns 0.
256 *----------------------------------------------------------------------------*/
258 int float32_is_quiet_nan( float32 a_
)
260 uint32_t a
= float32_val(a_
);
262 return (((a
>> 22) & 0x1ff) == 0x1fe) && (a
& 0x003fffff);
264 return ((uint32_t)(a
<< 1) >= 0xff800000);
268 /*----------------------------------------------------------------------------
269 | Returns 1 if the single-precision floating-point value `a' is a signaling
270 | NaN; otherwise returns 0.
271 *----------------------------------------------------------------------------*/
273 int float32_is_signaling_nan( float32 a_
)
275 uint32_t a
= float32_val(a_
);
277 return ((uint32_t)(a
<< 1) >= 0xff800000);
279 return ( ( ( a
>>22 ) & 0x1FF ) == 0x1FE ) && ( a
& 0x003FFFFF );
284 /*----------------------------------------------------------------------------
285 | Returns a quiet NaN if the single-precision floating point value `a' is a
286 | signaling NaN; otherwise returns `a'.
287 *----------------------------------------------------------------------------*/
289 float32
float32_maybe_silence_nan( float32 a_
)
291 if (float32_is_signaling_nan(a_
)) {
293 # if defined(TARGET_MIPS) || defined(TARGET_SH4) || defined(TARGET_UNICORE32)
294 return float32_default_nan
;
296 # error Rules for silencing a signaling NaN are target-specific
299 uint32_t a
= float32_val(a_
);
301 return make_float32(a
);
307 /*----------------------------------------------------------------------------
308 | Returns the result of converting the single-precision floating-point NaN
309 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
310 | exception is raised.
311 *----------------------------------------------------------------------------*/
313 static commonNaNT
float32ToCommonNaN( float32 a STATUS_PARAM
)
317 if ( float32_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
318 z
.sign
= float32_val(a
)>>31;
320 z
.high
= ( (uint64_t) float32_val(a
) )<<41;
324 /*----------------------------------------------------------------------------
325 | Returns the result of converting the canonical NaN `a' to the single-
326 | precision floating-point format.
327 *----------------------------------------------------------------------------*/
329 static float32
commonNaNToFloat32( commonNaNT a STATUS_PARAM
)
331 uint32_t mantissa
= a
.high
>>41;
333 if ( STATUS(default_nan_mode
) ) {
334 return float32_default_nan
;
339 ( ( (uint32_t) a
.sign
)<<31 ) | 0x7F800000 | ( a
.high
>>41 ) );
341 return float32_default_nan
;
344 /*----------------------------------------------------------------------------
345 | Select which NaN to propagate for a two-input operation.
346 | IEEE754 doesn't specify all the details of this, so the
347 | algorithm is target-specific.
348 | The routine is passed various bits of information about the
349 | two NaNs and should return 0 to select NaN a and 1 for NaN b.
350 | Note that signalling NaNs are always squashed to quiet NaNs
351 | by the caller, by calling floatXX_maybe_silence_nan() before
354 | aIsLargerSignificand is only valid if both a and b are NaNs
355 | of some kind, and is true if a has the larger significand,
356 | or if both a and b have the same significand but a is
357 | positive but b is negative. It is only needed for the x87
359 *----------------------------------------------------------------------------*/
361 #if defined(TARGET_ARM)
362 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
363 flag aIsLargerSignificand
)
365 /* ARM mandated NaN propagation rules: take the first of:
366 * 1. A if it is signaling
367 * 2. B if it is signaling
370 * A signaling NaN is always quietened before returning it.
374 } else if (bIsSNaN
) {
376 } else if (aIsQNaN
) {
382 #elif defined(TARGET_MIPS)
383 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
384 flag aIsLargerSignificand
)
386 /* According to MIPS specifications, if one of the two operands is
387 * a sNaN, a new qNaN has to be generated. This is done in
388 * floatXX_maybe_silence_nan(). For qNaN inputs the specifications
389 * says: "When possible, this QNaN result is one of the operand QNaN
390 * values." In practice it seems that most implementations choose
391 * the first operand if both operands are qNaN. In short this gives
392 * the following rules:
393 * 1. A if it is signaling
394 * 2. B if it is signaling
397 * A signaling NaN is always silenced before returning it.
401 } else if (bIsSNaN
) {
403 } else if (aIsQNaN
) {
409 #elif defined(TARGET_PPC) || defined(TARGET_XTENSA)
410 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
411 flag aIsLargerSignificand
)
413 /* PowerPC propagation rules:
414 * 1. A if it sNaN or qNaN
415 * 2. B if it sNaN or qNaN
416 * A signaling NaN is always silenced before returning it.
418 if (aIsSNaN
|| aIsQNaN
) {
425 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
426 flag aIsLargerSignificand
)
428 /* This implements x87 NaN propagation rules:
429 * SNaN + QNaN => return the QNaN
430 * two SNaNs => return the one with the larger significand, silenced
431 * two QNaNs => return the one with the larger significand
432 * SNaN and a non-NaN => return the SNaN, silenced
433 * QNaN and a non-NaN => return the QNaN
435 * If we get down to comparing significands and they are the same,
436 * return the NaN with the positive sign bit (if any).
440 return aIsLargerSignificand
? 0 : 1;
442 return bIsQNaN
? 1 : 0;
445 if (bIsSNaN
|| !bIsQNaN
)
448 return aIsLargerSignificand
? 0 : 1;
456 /*----------------------------------------------------------------------------
457 | Select which NaN to propagate for a three-input operation.
458 | For the moment we assume that no CPU needs the 'larger significand'
460 | Return values : 0 : a; 1 : b; 2 : c; 3 : default-NaN
461 *----------------------------------------------------------------------------*/
462 #if defined(TARGET_ARM)
463 static int pickNaNMulAdd(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
464 flag cIsQNaN
, flag cIsSNaN
, flag infzero STATUS_PARAM
)
466 /* For ARM, the (inf,zero,qnan) case sets InvalidOp and returns
469 if (infzero
&& cIsQNaN
) {
470 float_raise(float_flag_invalid STATUS_VAR
);
474 /* This looks different from the ARM ARM pseudocode, because the ARM ARM
475 * puts the operands to a fused mac operation (a*b)+c in the order c,a,b.
479 } else if (aIsSNaN
) {
481 } else if (bIsSNaN
) {
483 } else if (cIsQNaN
) {
485 } else if (aIsQNaN
) {
491 #elif defined(TARGET_MIPS)
492 static int pickNaNMulAdd(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
493 flag cIsQNaN
, flag cIsSNaN
, flag infzero STATUS_PARAM
)
495 /* For MIPS, the (inf,zero,qnan) case sets InvalidOp and returns
499 float_raise(float_flag_invalid STATUS_VAR
);
503 /* Prefer sNaN over qNaN, in the a, b, c order. */
506 } else if (bIsSNaN
) {
508 } else if (cIsSNaN
) {
510 } else if (aIsQNaN
) {
512 } else if (bIsQNaN
) {
518 #elif defined(TARGET_PPC)
519 static int pickNaNMulAdd(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
520 flag cIsQNaN
, flag cIsSNaN
, flag infzero STATUS_PARAM
)
522 /* For PPC, the (inf,zero,qnan) case sets InvalidOp, but we prefer
523 * to return an input NaN if we have one (ie c) rather than generating
527 float_raise(float_flag_invalid STATUS_VAR
);
531 /* If fRA is a NaN return it; otherwise if fRB is a NaN return it;
532 * otherwise return fRC. Note that muladd on PPC is (fRA * fRC) + frB
534 if (aIsSNaN
|| aIsQNaN
) {
536 } else if (cIsSNaN
|| cIsQNaN
) {
543 /* A default implementation: prefer a to b to c.
544 * This is unlikely to actually match any real implementation.
546 static int pickNaNMulAdd(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
547 flag cIsQNaN
, flag cIsSNaN
, flag infzero STATUS_PARAM
)
549 if (aIsSNaN
|| aIsQNaN
) {
551 } else if (bIsSNaN
|| bIsQNaN
) {
559 /*----------------------------------------------------------------------------
560 | Takes two single-precision floating-point values `a' and `b', one of which
561 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
562 | signaling NaN, the invalid exception is raised.
563 *----------------------------------------------------------------------------*/
565 static float32
propagateFloat32NaN( float32 a
, float32 b STATUS_PARAM
)
567 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
568 flag aIsLargerSignificand
;
571 aIsQuietNaN
= float32_is_quiet_nan( a
);
572 aIsSignalingNaN
= float32_is_signaling_nan( a
);
573 bIsQuietNaN
= float32_is_quiet_nan( b
);
574 bIsSignalingNaN
= float32_is_signaling_nan( b
);
578 if ( aIsSignalingNaN
| bIsSignalingNaN
) float_raise( float_flag_invalid STATUS_VAR
);
580 if ( STATUS(default_nan_mode
) )
581 return float32_default_nan
;
583 if ((uint32_t)(av
<<1) < (uint32_t)(bv
<<1)) {
584 aIsLargerSignificand
= 0;
585 } else if ((uint32_t)(bv
<<1) < (uint32_t)(av
<<1)) {
586 aIsLargerSignificand
= 1;
588 aIsLargerSignificand
= (av
< bv
) ? 1 : 0;
591 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
592 aIsLargerSignificand
)) {
593 return float32_maybe_silence_nan(b
);
595 return float32_maybe_silence_nan(a
);
599 /*----------------------------------------------------------------------------
600 | Takes three single-precision floating-point values `a', `b' and `c', one of
601 | which is a NaN, and returns the appropriate NaN result. If any of `a',
602 | `b' or `c' is a signaling NaN, the invalid exception is raised.
603 | The input infzero indicates whether a*b was 0*inf or inf*0 (in which case
604 | obviously c is a NaN, and whether to propagate c or some other NaN is
605 | implementation defined).
606 *----------------------------------------------------------------------------*/
608 static float32
propagateFloat32MulAddNaN(float32 a
, float32 b
,
609 float32 c
, flag infzero STATUS_PARAM
)
611 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
612 cIsQuietNaN
, cIsSignalingNaN
;
615 aIsQuietNaN
= float32_is_quiet_nan(a
);
616 aIsSignalingNaN
= float32_is_signaling_nan(a
);
617 bIsQuietNaN
= float32_is_quiet_nan(b
);
618 bIsSignalingNaN
= float32_is_signaling_nan(b
);
619 cIsQuietNaN
= float32_is_quiet_nan(c
);
620 cIsSignalingNaN
= float32_is_signaling_nan(c
);
622 if (aIsSignalingNaN
| bIsSignalingNaN
| cIsSignalingNaN
) {
623 float_raise(float_flag_invalid STATUS_VAR
);
626 which
= pickNaNMulAdd(aIsQuietNaN
, aIsSignalingNaN
,
627 bIsQuietNaN
, bIsSignalingNaN
,
628 cIsQuietNaN
, cIsSignalingNaN
, infzero STATUS_VAR
);
630 if (STATUS(default_nan_mode
)) {
631 /* Note that this check is after pickNaNMulAdd so that function
632 * has an opportunity to set the Invalid flag.
634 return float32_default_nan
;
639 return float32_maybe_silence_nan(a
);
641 return float32_maybe_silence_nan(b
);
643 return float32_maybe_silence_nan(c
);
646 return float32_default_nan
;
650 #ifdef NO_SIGNALING_NANS
651 int float64_is_quiet_nan(float64 a_
)
653 return float64_is_any_nan(a_
);
656 int float64_is_signaling_nan(float64 a_
)
661 /*----------------------------------------------------------------------------
662 | Returns 1 if the double-precision floating-point value `a' is a quiet
663 | NaN; otherwise returns 0.
664 *----------------------------------------------------------------------------*/
666 int float64_is_quiet_nan( float64 a_
)
668 uint64_t a
= float64_val(a_
);
670 return (((a
>> 51) & 0xfff) == 0xffe)
671 && (a
& 0x0007ffffffffffffULL
);
673 return ((a
<< 1) >= 0xfff0000000000000ULL
);
677 /*----------------------------------------------------------------------------
678 | Returns 1 if the double-precision floating-point value `a' is a signaling
679 | NaN; otherwise returns 0.
680 *----------------------------------------------------------------------------*/
682 int float64_is_signaling_nan( float64 a_
)
684 uint64_t a
= float64_val(a_
);
686 return ((a
<< 1) >= 0xfff0000000000000ULL
);
689 ( ( ( a
>>51 ) & 0xFFF ) == 0xFFE )
690 && ( a
& LIT64( 0x0007FFFFFFFFFFFF ) );
695 /*----------------------------------------------------------------------------
696 | Returns a quiet NaN if the double-precision floating point value `a' is a
697 | signaling NaN; otherwise returns `a'.
698 *----------------------------------------------------------------------------*/
700 float64
float64_maybe_silence_nan( float64 a_
)
702 if (float64_is_signaling_nan(a_
)) {
704 # if defined(TARGET_MIPS) || defined(TARGET_SH4) || defined(TARGET_UNICORE32)
705 return float64_default_nan
;
707 # error Rules for silencing a signaling NaN are target-specific
710 uint64_t a
= float64_val(a_
);
711 a
|= LIT64( 0x0008000000000000 );
712 return make_float64(a
);
718 /*----------------------------------------------------------------------------
719 | Returns the result of converting the double-precision floating-point NaN
720 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
721 | exception is raised.
722 *----------------------------------------------------------------------------*/
724 static commonNaNT
float64ToCommonNaN( float64 a STATUS_PARAM
)
728 if ( float64_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
729 z
.sign
= float64_val(a
)>>63;
731 z
.high
= float64_val(a
)<<12;
735 /*----------------------------------------------------------------------------
736 | Returns the result of converting the canonical NaN `a' to the double-
737 | precision floating-point format.
738 *----------------------------------------------------------------------------*/
740 static float64
commonNaNToFloat64( commonNaNT a STATUS_PARAM
)
742 uint64_t mantissa
= a
.high
>>12;
744 if ( STATUS(default_nan_mode
) ) {
745 return float64_default_nan
;
750 ( ( (uint64_t) a
.sign
)<<63 )
751 | LIT64( 0x7FF0000000000000 )
754 return float64_default_nan
;
757 /*----------------------------------------------------------------------------
758 | Takes two double-precision floating-point values `a' and `b', one of which
759 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
760 | signaling NaN, the invalid exception is raised.
761 *----------------------------------------------------------------------------*/
763 static float64
propagateFloat64NaN( float64 a
, float64 b STATUS_PARAM
)
765 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
766 flag aIsLargerSignificand
;
769 aIsQuietNaN
= float64_is_quiet_nan( a
);
770 aIsSignalingNaN
= float64_is_signaling_nan( a
);
771 bIsQuietNaN
= float64_is_quiet_nan( b
);
772 bIsSignalingNaN
= float64_is_signaling_nan( b
);
776 if ( aIsSignalingNaN
| bIsSignalingNaN
) float_raise( float_flag_invalid STATUS_VAR
);
778 if ( STATUS(default_nan_mode
) )
779 return float64_default_nan
;
781 if ((uint64_t)(av
<<1) < (uint64_t)(bv
<<1)) {
782 aIsLargerSignificand
= 0;
783 } else if ((uint64_t)(bv
<<1) < (uint64_t)(av
<<1)) {
784 aIsLargerSignificand
= 1;
786 aIsLargerSignificand
= (av
< bv
) ? 1 : 0;
789 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
790 aIsLargerSignificand
)) {
791 return float64_maybe_silence_nan(b
);
793 return float64_maybe_silence_nan(a
);
797 /*----------------------------------------------------------------------------
798 | Takes three double-precision floating-point values `a', `b' and `c', one of
799 | which is a NaN, and returns the appropriate NaN result. If any of `a',
800 | `b' or `c' is a signaling NaN, the invalid exception is raised.
801 | The input infzero indicates whether a*b was 0*inf or inf*0 (in which case
802 | obviously c is a NaN, and whether to propagate c or some other NaN is
803 | implementation defined).
804 *----------------------------------------------------------------------------*/
806 static float64
propagateFloat64MulAddNaN(float64 a
, float64 b
,
807 float64 c
, flag infzero STATUS_PARAM
)
809 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
810 cIsQuietNaN
, cIsSignalingNaN
;
813 aIsQuietNaN
= float64_is_quiet_nan(a
);
814 aIsSignalingNaN
= float64_is_signaling_nan(a
);
815 bIsQuietNaN
= float64_is_quiet_nan(b
);
816 bIsSignalingNaN
= float64_is_signaling_nan(b
);
817 cIsQuietNaN
= float64_is_quiet_nan(c
);
818 cIsSignalingNaN
= float64_is_signaling_nan(c
);
820 if (aIsSignalingNaN
| bIsSignalingNaN
| cIsSignalingNaN
) {
821 float_raise(float_flag_invalid STATUS_VAR
);
824 which
= pickNaNMulAdd(aIsQuietNaN
, aIsSignalingNaN
,
825 bIsQuietNaN
, bIsSignalingNaN
,
826 cIsQuietNaN
, cIsSignalingNaN
, infzero STATUS_VAR
);
828 if (STATUS(default_nan_mode
)) {
829 /* Note that this check is after pickNaNMulAdd so that function
830 * has an opportunity to set the Invalid flag.
832 return float64_default_nan
;
837 return float64_maybe_silence_nan(a
);
839 return float64_maybe_silence_nan(b
);
841 return float64_maybe_silence_nan(c
);
844 return float64_default_nan
;
848 #ifdef NO_SIGNALING_NANS
849 int floatx80_is_quiet_nan(floatx80 a_
)
851 return floatx80_is_any_nan(a_
);
854 int floatx80_is_signaling_nan(floatx80 a_
)
859 /*----------------------------------------------------------------------------
860 | Returns 1 if the extended double-precision floating-point value `a' is a
861 | quiet NaN; otherwise returns 0. This slightly differs from the same
862 | function for other types as floatx80 has an explicit bit.
863 *----------------------------------------------------------------------------*/
865 int floatx80_is_quiet_nan( floatx80 a
)
870 aLow
= a
.low
& ~0x4000000000000000ULL
;
871 return ((a
.high
& 0x7fff) == 0x7fff)
875 return ( ( a
.high
& 0x7FFF ) == 0x7FFF )
876 && (LIT64( 0x8000000000000000 ) <= ((uint64_t) ( a
.low
<<1 )));
880 /*----------------------------------------------------------------------------
881 | Returns 1 if the extended double-precision floating-point value `a' is a
882 | signaling NaN; otherwise returns 0. This slightly differs from the same
883 | function for other types as floatx80 has an explicit bit.
884 *----------------------------------------------------------------------------*/
886 int floatx80_is_signaling_nan( floatx80 a
)
889 return ((a
.high
& 0x7fff) == 0x7fff)
890 && ((a
.low
<< 1) >= 0x8000000000000000ULL
);
894 aLow
= a
.low
& ~ LIT64( 0x4000000000000000 );
896 ( ( a
.high
& 0x7FFF ) == 0x7FFF )
897 && (uint64_t) ( aLow
<<1 )
898 && ( a
.low
== aLow
);
903 /*----------------------------------------------------------------------------
904 | Returns a quiet NaN if the extended double-precision floating point value
905 | `a' is a signaling NaN; otherwise returns `a'.
906 *----------------------------------------------------------------------------*/
908 floatx80
floatx80_maybe_silence_nan( floatx80 a
)
910 if (floatx80_is_signaling_nan(a
)) {
912 # if defined(TARGET_MIPS) || defined(TARGET_SH4) || defined(TARGET_UNICORE32)
913 a
.low
= floatx80_default_nan_low
;
914 a
.high
= floatx80_default_nan_high
;
916 # error Rules for silencing a signaling NaN are target-specific
919 a
.low
|= LIT64( 0xC000000000000000 );
926 /*----------------------------------------------------------------------------
927 | Returns the result of converting the extended double-precision floating-
928 | point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
929 | invalid exception is raised.
930 *----------------------------------------------------------------------------*/
932 static commonNaNT
floatx80ToCommonNaN( floatx80 a STATUS_PARAM
)
936 if ( floatx80_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
938 z
.sign
= a
.high
>> 15;
942 z
.sign
= floatx80_default_nan_high
>> 15;
944 z
.high
= floatx80_default_nan_low
<< 1;
949 /*----------------------------------------------------------------------------
950 | Returns the result of converting the canonical NaN `a' to the extended
951 | double-precision floating-point format.
952 *----------------------------------------------------------------------------*/
954 static floatx80
commonNaNToFloatx80( commonNaNT a STATUS_PARAM
)
958 if ( STATUS(default_nan_mode
) ) {
959 z
.low
= floatx80_default_nan_low
;
960 z
.high
= floatx80_default_nan_high
;
965 z
.low
= LIT64( 0x8000000000000000 ) | a
.high
>> 1;
966 z
.high
= ( ( (uint16_t) a
.sign
)<<15 ) | 0x7FFF;
968 z
.low
= floatx80_default_nan_low
;
969 z
.high
= floatx80_default_nan_high
;
975 /*----------------------------------------------------------------------------
976 | Takes two extended double-precision floating-point values `a' and `b', one
977 | of which is a NaN, and returns the appropriate NaN result. If either `a' or
978 | `b' is a signaling NaN, the invalid exception is raised.
979 *----------------------------------------------------------------------------*/
981 static floatx80
propagateFloatx80NaN( floatx80 a
, floatx80 b STATUS_PARAM
)
983 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
984 flag aIsLargerSignificand
;
986 aIsQuietNaN
= floatx80_is_quiet_nan( a
);
987 aIsSignalingNaN
= floatx80_is_signaling_nan( a
);
988 bIsQuietNaN
= floatx80_is_quiet_nan( b
);
989 bIsSignalingNaN
= floatx80_is_signaling_nan( b
);
991 if ( aIsSignalingNaN
| bIsSignalingNaN
) float_raise( float_flag_invalid STATUS_VAR
);
993 if ( STATUS(default_nan_mode
) ) {
994 a
.low
= floatx80_default_nan_low
;
995 a
.high
= floatx80_default_nan_high
;
1000 aIsLargerSignificand
= 0;
1001 } else if (b
.low
< a
.low
) {
1002 aIsLargerSignificand
= 1;
1004 aIsLargerSignificand
= (a
.high
< b
.high
) ? 1 : 0;
1007 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
1008 aIsLargerSignificand
)) {
1009 return floatx80_maybe_silence_nan(b
);
1011 return floatx80_maybe_silence_nan(a
);
1015 #ifdef NO_SIGNALING_NANS
1016 int float128_is_quiet_nan(float128 a_
)
1018 return float128_is_any_nan(a_
);
1021 int float128_is_signaling_nan(float128 a_
)
1026 /*----------------------------------------------------------------------------
1027 | Returns 1 if the quadruple-precision floating-point value `a' is a quiet
1028 | NaN; otherwise returns 0.
1029 *----------------------------------------------------------------------------*/
1031 int float128_is_quiet_nan( float128 a
)
1034 return (((a
.high
>> 47) & 0xffff) == 0xfffe)
1035 && (a
.low
|| (a
.high
& 0x00007fffffffffffULL
));
1038 ((a
.high
<< 1) >= 0xffff000000000000ULL
)
1039 && (a
.low
|| (a
.high
& 0x0000ffffffffffffULL
));
1043 /*----------------------------------------------------------------------------
1044 | Returns 1 if the quadruple-precision floating-point value `a' is a
1045 | signaling NaN; otherwise returns 0.
1046 *----------------------------------------------------------------------------*/
1048 int float128_is_signaling_nan( float128 a
)
1052 ((a
.high
<< 1) >= 0xffff000000000000ULL
)
1053 && (a
.low
|| (a
.high
& 0x0000ffffffffffffULL
));
1056 ( ( ( a
.high
>>47 ) & 0xFFFF ) == 0xFFFE )
1057 && ( a
.low
|| ( a
.high
& LIT64( 0x00007FFFFFFFFFFF ) ) );
1062 /*----------------------------------------------------------------------------
1063 | Returns a quiet NaN if the quadruple-precision floating point value `a' is
1064 | a signaling NaN; otherwise returns `a'.
1065 *----------------------------------------------------------------------------*/
1067 float128
float128_maybe_silence_nan( float128 a
)
1069 if (float128_is_signaling_nan(a
)) {
1071 # if defined(TARGET_MIPS) || defined(TARGET_SH4) || defined(TARGET_UNICORE32)
1072 a
.low
= float128_default_nan_low
;
1073 a
.high
= float128_default_nan_high
;
1075 # error Rules for silencing a signaling NaN are target-specific
1078 a
.high
|= LIT64( 0x0000800000000000 );
1085 /*----------------------------------------------------------------------------
1086 | Returns the result of converting the quadruple-precision floating-point NaN
1087 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
1088 | exception is raised.
1089 *----------------------------------------------------------------------------*/
1091 static commonNaNT
float128ToCommonNaN( float128 a STATUS_PARAM
)
1095 if ( float128_is_signaling_nan( a
) ) float_raise( float_flag_invalid STATUS_VAR
);
1096 z
.sign
= a
.high
>>63;
1097 shortShift128Left( a
.high
, a
.low
, 16, &z
.high
, &z
.low
);
1101 /*----------------------------------------------------------------------------
1102 | Returns the result of converting the canonical NaN `a' to the quadruple-
1103 | precision floating-point format.
1104 *----------------------------------------------------------------------------*/
1106 static float128
commonNaNToFloat128( commonNaNT a STATUS_PARAM
)
1110 if ( STATUS(default_nan_mode
) ) {
1111 z
.low
= float128_default_nan_low
;
1112 z
.high
= float128_default_nan_high
;
1116 shift128Right( a
.high
, a
.low
, 16, &z
.high
, &z
.low
);
1117 z
.high
|= ( ( (uint64_t) a
.sign
)<<63 ) | LIT64( 0x7FFF000000000000 );
1121 /*----------------------------------------------------------------------------
1122 | Takes two quadruple-precision floating-point values `a' and `b', one of
1123 | which is a NaN, and returns the appropriate NaN result. If either `a' or
1124 | `b' is a signaling NaN, the invalid exception is raised.
1125 *----------------------------------------------------------------------------*/
1127 static float128
propagateFloat128NaN( float128 a
, float128 b STATUS_PARAM
)
1129 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
1130 flag aIsLargerSignificand
;
1132 aIsQuietNaN
= float128_is_quiet_nan( a
);
1133 aIsSignalingNaN
= float128_is_signaling_nan( a
);
1134 bIsQuietNaN
= float128_is_quiet_nan( b
);
1135 bIsSignalingNaN
= float128_is_signaling_nan( b
);
1137 if ( aIsSignalingNaN
| bIsSignalingNaN
) float_raise( float_flag_invalid STATUS_VAR
);
1139 if ( STATUS(default_nan_mode
) ) {
1140 a
.low
= float128_default_nan_low
;
1141 a
.high
= float128_default_nan_high
;
1145 if (lt128(a
.high
<<1, a
.low
, b
.high
<<1, b
.low
)) {
1146 aIsLargerSignificand
= 0;
1147 } else if (lt128(b
.high
<<1, b
.low
, a
.high
<<1, a
.low
)) {
1148 aIsLargerSignificand
= 1;
1150 aIsLargerSignificand
= (a
.high
< b
.high
) ? 1 : 0;
1153 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
1154 aIsLargerSignificand
)) {
1155 return float128_maybe_silence_nan(b
);
1157 return float128_maybe_silence_nan(a
);