4 * The code in this source file is derived from release 2a of the SoftFloat
5 * IEC/IEEE Floating-point Arithmetic Package. Those parts of the code (and
6 * some later contributions) are provided under that license, as detailed below.
7 * It has subsequently been modified by contributors to the QEMU Project,
8 * so some portions are provided under:
9 * the SoftFloat-2a license
13 * Any future contributions to this file after December 1st 2014 will be
14 * taken to be licensed under the Softfloat-2a license unless specifically
15 * indicated otherwise.
19 ===============================================================================
20 This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
21 Arithmetic Package, Release 2a.
23 Written by John R. Hauser. This work was made possible in part by the
24 International Computer Science Institute, located at Suite 600, 1947 Center
25 Street, Berkeley, California 94704. Funding was partially provided by the
26 National Science Foundation under grant MIP-9311980. The original version
27 of this code was written as part of a project to build a fixed-point vector
28 processor in collaboration with the University of California at Berkeley,
29 overseen by Profs. Nelson Morgan and John Wawrzynek. More information
30 is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
31 arithmetic/SoftFloat.html'.
33 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
34 has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
35 TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
36 PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
37 AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
39 Derivative works are acceptable, even for commercial purposes, so long as
40 (1) they include prominent notice that the work is derivative, and (2) they
41 include prominent notice akin to these four paragraphs for those parts of
42 this code that are retained.
44 ===============================================================================
48 * Copyright (c) 2006, Fabrice Bellard
49 * All rights reserved.
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions are met:
54 * 1. Redistributions of source code must retain the above copyright notice,
55 * this list of conditions and the following disclaimer.
57 * 2. Redistributions in binary form must reproduce the above copyright notice,
58 * this list of conditions and the following disclaimer in the documentation
59 * and/or other materials provided with the distribution.
61 * 3. Neither the name of the copyright holder nor the names of its contributors
62 * may be used to endorse or promote products derived from this software without
63 * specific prior written permission.
65 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
66 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
69 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
70 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
71 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
72 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
73 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
74 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
75 * THE POSSIBILITY OF SUCH DAMAGE.
78 /* Portions of this work are licensed under the terms of the GNU GPL,
79 * version 2 or later. See the COPYING file in the top-level directory.
82 #if defined(TARGET_XTENSA)
83 /* Define for architectures which deviate from IEEE in not supporting
84 * signaling NaNs (so all NaNs are treated as quiet).
86 #define NO_SIGNALING_NANS 1
89 /*----------------------------------------------------------------------------
90 | The pattern for a default generated half-precision NaN.
91 *----------------------------------------------------------------------------*/
92 float16
float16_default_nan(float_status
*status
)
94 #if defined(TARGET_ARM)
95 return const_float16(0x7E00);
97 if (status
->snan_bit_is_one
) {
98 return const_float16(0x7DFF);
100 #if defined(TARGET_MIPS)
101 return const_float16(0x7E00);
103 return const_float16(0xFE00);
109 /*----------------------------------------------------------------------------
110 | The pattern for a default generated single-precision NaN.
111 *----------------------------------------------------------------------------*/
112 float32
float32_default_nan(float_status
*status
)
114 #if defined(TARGET_SPARC)
115 return const_float32(0x7FFFFFFF);
116 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA) || \
117 defined(TARGET_XTENSA) || defined(TARGET_S390X) || defined(TARGET_TRICORE)
118 return const_float32(0x7FC00000);
119 #elif defined(TARGET_HPPA)
120 return const_float32(0x7FA00000);
122 if (status
->snan_bit_is_one
) {
123 return const_float32(0x7FBFFFFF);
125 #if defined(TARGET_MIPS)
126 return const_float32(0x7FC00000);
128 return const_float32(0xFFC00000);
134 /*----------------------------------------------------------------------------
135 | The pattern for a default generated double-precision NaN.
136 *----------------------------------------------------------------------------*/
137 float64
float64_default_nan(float_status
*status
)
139 #if defined(TARGET_SPARC)
140 return const_float64(LIT64(0x7FFFFFFFFFFFFFFF));
141 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA) || \
142 defined(TARGET_S390X)
143 return const_float64(LIT64(0x7FF8000000000000));
144 #elif defined(TARGET_HPPA)
145 return const_float64(LIT64(0x7FF4000000000000));
147 if (status
->snan_bit_is_one
) {
148 return const_float64(LIT64(0x7FF7FFFFFFFFFFFF));
150 #if defined(TARGET_MIPS)
151 return const_float64(LIT64(0x7FF8000000000000));
153 return const_float64(LIT64(0xFFF8000000000000));
159 /*----------------------------------------------------------------------------
160 | The pattern for a default generated extended double-precision NaN.
161 *----------------------------------------------------------------------------*/
162 floatx80
floatx80_default_nan(float_status
*status
)
166 if (status
->snan_bit_is_one
) {
167 r
.low
= LIT64(0xBFFFFFFFFFFFFFFF);
170 r
.low
= LIT64(0xC000000000000000);
176 /*----------------------------------------------------------------------------
177 | The pattern for a default generated quadruple-precision NaN.
178 *----------------------------------------------------------------------------*/
179 float128
float128_default_nan(float_status
*status
)
183 if (status
->snan_bit_is_one
) {
184 r
.low
= LIT64(0xFFFFFFFFFFFFFFFF);
185 r
.high
= LIT64(0x7FFF7FFFFFFFFFFF);
187 r
.low
= LIT64(0x0000000000000000);
188 #if defined(TARGET_S390X) || defined(TARGET_PPC)
189 r
.high
= LIT64(0x7FFF800000000000);
191 r
.high
= LIT64(0xFFFF800000000000);
197 /*----------------------------------------------------------------------------
198 | Raises the exceptions specified by `flags'. Floating-point traps can be
199 | defined here if desired. It is currently not possible for such a trap
200 | to substitute a result value. If traps are not implemented, this routine
201 | should be simply `float_exception_flags |= flags;'.
202 *----------------------------------------------------------------------------*/
204 void float_raise(uint8_t flags
, float_status
*status
)
206 status
->float_exception_flags
|= flags
;
209 /*----------------------------------------------------------------------------
210 | Internal canonical NaN format.
211 *----------------------------------------------------------------------------*/
217 #ifdef NO_SIGNALING_NANS
218 int float16_is_quiet_nan(float16 a_
, float_status
*status
)
220 return float16_is_any_nan(a_
);
223 int float16_is_signaling_nan(float16 a_
, float_status
*status
)
228 /*----------------------------------------------------------------------------
229 | Returns 1 if the half-precision floating-point value `a' is a quiet
230 | NaN; otherwise returns 0.
231 *----------------------------------------------------------------------------*/
233 int float16_is_quiet_nan(float16 a_
, float_status
*status
)
235 uint16_t a
= float16_val(a_
);
236 if (status
->snan_bit_is_one
) {
237 return (((a
>> 9) & 0x3F) == 0x3E) && (a
& 0x1FF);
239 return ((a
& ~0x8000) >= 0x7C80);
243 /*----------------------------------------------------------------------------
244 | Returns 1 if the half-precision floating-point value `a' is a signaling
245 | NaN; otherwise returns 0.
246 *----------------------------------------------------------------------------*/
248 int float16_is_signaling_nan(float16 a_
, float_status
*status
)
250 uint16_t a
= float16_val(a_
);
251 if (status
->snan_bit_is_one
) {
252 return ((a
& ~0x8000) >= 0x7C80);
254 return (((a
>> 9) & 0x3F) == 0x3E) && (a
& 0x1FF);
259 /*----------------------------------------------------------------------------
260 | Returns a quiet NaN if the half-precision floating point value `a' is a
261 | signaling NaN; otherwise returns `a'.
262 *----------------------------------------------------------------------------*/
263 float16
float16_maybe_silence_nan(float16 a_
, float_status
*status
)
265 if (float16_is_signaling_nan(a_
, status
)) {
266 if (status
->snan_bit_is_one
) {
267 return float16_default_nan(status
);
269 uint16_t a
= float16_val(a_
);
271 return make_float16(a
);
277 /*----------------------------------------------------------------------------
278 | Returns the result of converting the half-precision floating-point NaN
279 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
280 | exception is raised.
281 *----------------------------------------------------------------------------*/
283 static commonNaNT
float16ToCommonNaN(float16 a
, float_status
*status
)
287 if (float16_is_signaling_nan(a
, status
)) {
288 float_raise(float_flag_invalid
, status
);
290 z
.sign
= float16_val(a
) >> 15;
292 z
.high
= ((uint64_t) float16_val(a
)) << 54;
296 /*----------------------------------------------------------------------------
297 | Returns the result of converting the canonical NaN `a' to the half-
298 | precision floating-point format.
299 *----------------------------------------------------------------------------*/
301 static float16
commonNaNToFloat16(commonNaNT a
, float_status
*status
)
303 uint16_t mantissa
= a
.high
>> 54;
305 if (status
->default_nan_mode
) {
306 return float16_default_nan(status
);
310 return make_float16(((((uint16_t) a
.sign
) << 15)
311 | (0x1F << 10) | mantissa
));
313 return float16_default_nan(status
);
317 #ifdef NO_SIGNALING_NANS
318 int float32_is_quiet_nan(float32 a_
, float_status
*status
)
320 return float32_is_any_nan(a_
);
323 int float32_is_signaling_nan(float32 a_
, float_status
*status
)
328 /*----------------------------------------------------------------------------
329 | Returns 1 if the single-precision floating-point value `a' is a quiet
330 | NaN; otherwise returns 0.
331 *----------------------------------------------------------------------------*/
333 int float32_is_quiet_nan(float32 a_
, float_status
*status
)
335 uint32_t a
= float32_val(a_
);
336 if (status
->snan_bit_is_one
) {
337 return (((a
>> 22) & 0x1FF) == 0x1FE) && (a
& 0x003FFFFF);
339 return ((uint32_t)(a
<< 1) >= 0xFF800000);
343 /*----------------------------------------------------------------------------
344 | Returns 1 if the single-precision floating-point value `a' is a signaling
345 | NaN; otherwise returns 0.
346 *----------------------------------------------------------------------------*/
348 int float32_is_signaling_nan(float32 a_
, float_status
*status
)
350 uint32_t a
= float32_val(a_
);
351 if (status
->snan_bit_is_one
) {
352 return ((uint32_t)(a
<< 1) >= 0xFF800000);
354 return (((a
>> 22) & 0x1FF) == 0x1FE) && (a
& 0x003FFFFF);
359 /*----------------------------------------------------------------------------
360 | Returns a quiet NaN if the single-precision floating point value `a' is a
361 | signaling NaN; otherwise returns `a'.
362 *----------------------------------------------------------------------------*/
364 float32
float32_maybe_silence_nan(float32 a_
, float_status
*status
)
366 if (float32_is_signaling_nan(a_
, status
)) {
367 if (status
->snan_bit_is_one
) {
369 uint32_t a
= float32_val(a_
);
372 return make_float32(a
);
374 return float32_default_nan(status
);
377 uint32_t a
= float32_val(a_
);
379 return make_float32(a
);
385 /*----------------------------------------------------------------------------
386 | Returns the result of converting the single-precision floating-point NaN
387 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
388 | exception is raised.
389 *----------------------------------------------------------------------------*/
391 static commonNaNT
float32ToCommonNaN(float32 a
, float_status
*status
)
395 if (float32_is_signaling_nan(a
, status
)) {
396 float_raise(float_flag_invalid
, status
);
398 z
.sign
= float32_val(a
) >> 31;
400 z
.high
= ((uint64_t)float32_val(a
)) << 41;
404 /*----------------------------------------------------------------------------
405 | Returns the result of converting the canonical NaN `a' to the single-
406 | precision floating-point format.
407 *----------------------------------------------------------------------------*/
409 static float32
commonNaNToFloat32(commonNaNT a
, float_status
*status
)
411 uint32_t mantissa
= a
.high
>> 41;
413 if (status
->default_nan_mode
) {
414 return float32_default_nan(status
);
419 (((uint32_t)a
.sign
) << 31) | 0x7F800000 | (a
.high
>> 41));
421 return float32_default_nan(status
);
425 /*----------------------------------------------------------------------------
426 | Select which NaN to propagate for a two-input operation.
427 | IEEE754 doesn't specify all the details of this, so the
428 | algorithm is target-specific.
429 | The routine is passed various bits of information about the
430 | two NaNs and should return 0 to select NaN a and 1 for NaN b.
431 | Note that signalling NaNs are always squashed to quiet NaNs
432 | by the caller, by calling floatXX_maybe_silence_nan() before
435 | aIsLargerSignificand is only valid if both a and b are NaNs
436 | of some kind, and is true if a has the larger significand,
437 | or if both a and b have the same significand but a is
438 | positive but b is negative. It is only needed for the x87
440 *----------------------------------------------------------------------------*/
442 #if defined(TARGET_ARM)
443 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
444 flag aIsLargerSignificand
)
446 /* ARM mandated NaN propagation rules: take the first of:
447 * 1. A if it is signaling
448 * 2. B if it is signaling
451 * A signaling NaN is always quietened before returning it.
455 } else if (bIsSNaN
) {
457 } else if (aIsQNaN
) {
463 #elif defined(TARGET_MIPS) || defined(TARGET_HPPA)
464 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
465 flag aIsLargerSignificand
)
467 /* According to MIPS specifications, if one of the two operands is
468 * a sNaN, a new qNaN has to be generated. This is done in
469 * floatXX_maybe_silence_nan(). For qNaN inputs the specifications
470 * says: "When possible, this QNaN result is one of the operand QNaN
471 * values." In practice it seems that most implementations choose
472 * the first operand if both operands are qNaN. In short this gives
473 * the following rules:
474 * 1. A if it is signaling
475 * 2. B if it is signaling
478 * A signaling NaN is always silenced before returning it.
482 } else if (bIsSNaN
) {
484 } else if (aIsQNaN
) {
490 #elif defined(TARGET_PPC) || defined(TARGET_XTENSA)
491 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
492 flag aIsLargerSignificand
)
494 /* PowerPC propagation rules:
495 * 1. A if it sNaN or qNaN
496 * 2. B if it sNaN or qNaN
497 * A signaling NaN is always silenced before returning it.
499 if (aIsSNaN
|| aIsQNaN
) {
506 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
507 flag aIsLargerSignificand
)
509 /* This implements x87 NaN propagation rules:
510 * SNaN + QNaN => return the QNaN
511 * two SNaNs => return the one with the larger significand, silenced
512 * two QNaNs => return the one with the larger significand
513 * SNaN and a non-NaN => return the SNaN, silenced
514 * QNaN and a non-NaN => return the QNaN
516 * If we get down to comparing significands and they are the same,
517 * return the NaN with the positive sign bit (if any).
521 return aIsLargerSignificand
? 0 : 1;
523 return bIsQNaN
? 1 : 0;
524 } else if (aIsQNaN
) {
525 if (bIsSNaN
|| !bIsQNaN
) {
528 return aIsLargerSignificand
? 0 : 1;
536 /*----------------------------------------------------------------------------
537 | Select which NaN to propagate for a three-input operation.
538 | For the moment we assume that no CPU needs the 'larger significand'
540 | Return values : 0 : a; 1 : b; 2 : c; 3 : default-NaN
541 *----------------------------------------------------------------------------*/
542 #if defined(TARGET_ARM)
543 static int pickNaNMulAdd(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
544 flag cIsQNaN
, flag cIsSNaN
, flag infzero
,
545 float_status
*status
)
547 /* For ARM, the (inf,zero,qnan) case sets InvalidOp and returns
550 if (infzero
&& cIsQNaN
) {
551 float_raise(float_flag_invalid
, status
);
555 /* This looks different from the ARM ARM pseudocode, because the ARM ARM
556 * puts the operands to a fused mac operation (a*b)+c in the order c,a,b.
560 } else if (aIsSNaN
) {
562 } else if (bIsSNaN
) {
564 } else if (cIsQNaN
) {
566 } else if (aIsQNaN
) {
572 #elif defined(TARGET_MIPS)
573 static int pickNaNMulAdd(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
574 flag cIsQNaN
, flag cIsSNaN
, flag infzero
,
575 float_status
*status
)
577 /* For MIPS, the (inf,zero,qnan) case sets InvalidOp and returns
581 float_raise(float_flag_invalid
, status
);
585 if (status
->snan_bit_is_one
) {
586 /* Prefer sNaN over qNaN, in the a, b, c order. */
589 } else if (bIsSNaN
) {
591 } else if (cIsSNaN
) {
593 } else if (aIsQNaN
) {
595 } else if (bIsQNaN
) {
601 /* Prefer sNaN over qNaN, in the c, a, b order. */
604 } else if (aIsSNaN
) {
606 } else if (bIsSNaN
) {
608 } else if (cIsQNaN
) {
610 } else if (aIsQNaN
) {
617 #elif defined(TARGET_PPC)
618 static int pickNaNMulAdd(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
619 flag cIsQNaN
, flag cIsSNaN
, flag infzero
,
620 float_status
*status
)
622 /* For PPC, the (inf,zero,qnan) case sets InvalidOp, but we prefer
623 * to return an input NaN if we have one (ie c) rather than generating
627 float_raise(float_flag_invalid
, status
);
631 /* If fRA is a NaN return it; otherwise if fRB is a NaN return it;
632 * otherwise return fRC. Note that muladd on PPC is (fRA * fRC) + frB
634 if (aIsSNaN
|| aIsQNaN
) {
636 } else if (cIsSNaN
|| cIsQNaN
) {
643 /* A default implementation: prefer a to b to c.
644 * This is unlikely to actually match any real implementation.
646 static int pickNaNMulAdd(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
647 flag cIsQNaN
, flag cIsSNaN
, flag infzero
,
648 float_status
*status
)
650 if (aIsSNaN
|| aIsQNaN
) {
652 } else if (bIsSNaN
|| bIsQNaN
) {
660 /*----------------------------------------------------------------------------
661 | Takes two single-precision floating-point values `a' and `b', one of which
662 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
663 | signaling NaN, the invalid exception is raised.
664 *----------------------------------------------------------------------------*/
666 static float32
propagateFloat32NaN(float32 a
, float32 b
, float_status
*status
)
668 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
669 flag aIsLargerSignificand
;
672 aIsQuietNaN
= float32_is_quiet_nan(a
, status
);
673 aIsSignalingNaN
= float32_is_signaling_nan(a
, status
);
674 bIsQuietNaN
= float32_is_quiet_nan(b
, status
);
675 bIsSignalingNaN
= float32_is_signaling_nan(b
, status
);
679 if (aIsSignalingNaN
| bIsSignalingNaN
) {
680 float_raise(float_flag_invalid
, status
);
683 if (status
->default_nan_mode
) {
684 return float32_default_nan(status
);
687 if ((uint32_t)(av
<< 1) < (uint32_t)(bv
<< 1)) {
688 aIsLargerSignificand
= 0;
689 } else if ((uint32_t)(bv
<< 1) < (uint32_t)(av
<< 1)) {
690 aIsLargerSignificand
= 1;
692 aIsLargerSignificand
= (av
< bv
) ? 1 : 0;
695 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
696 aIsLargerSignificand
)) {
697 return float32_maybe_silence_nan(b
, status
);
699 return float32_maybe_silence_nan(a
, status
);
703 /*----------------------------------------------------------------------------
704 | Takes three single-precision floating-point values `a', `b' and `c', one of
705 | which is a NaN, and returns the appropriate NaN result. If any of `a',
706 | `b' or `c' is a signaling NaN, the invalid exception is raised.
707 | The input infzero indicates whether a*b was 0*inf or inf*0 (in which case
708 | obviously c is a NaN, and whether to propagate c or some other NaN is
709 | implementation defined).
710 *----------------------------------------------------------------------------*/
712 static float32
propagateFloat32MulAddNaN(float32 a
, float32 b
,
713 float32 c
, flag infzero
,
714 float_status
*status
)
716 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
717 cIsQuietNaN
, cIsSignalingNaN
;
720 aIsQuietNaN
= float32_is_quiet_nan(a
, status
);
721 aIsSignalingNaN
= float32_is_signaling_nan(a
, status
);
722 bIsQuietNaN
= float32_is_quiet_nan(b
, status
);
723 bIsSignalingNaN
= float32_is_signaling_nan(b
, status
);
724 cIsQuietNaN
= float32_is_quiet_nan(c
, status
);
725 cIsSignalingNaN
= float32_is_signaling_nan(c
, status
);
727 if (aIsSignalingNaN
| bIsSignalingNaN
| cIsSignalingNaN
) {
728 float_raise(float_flag_invalid
, status
);
731 which
= pickNaNMulAdd(aIsQuietNaN
, aIsSignalingNaN
,
732 bIsQuietNaN
, bIsSignalingNaN
,
733 cIsQuietNaN
, cIsSignalingNaN
, infzero
, status
);
735 if (status
->default_nan_mode
) {
736 /* Note that this check is after pickNaNMulAdd so that function
737 * has an opportunity to set the Invalid flag.
739 return float32_default_nan(status
);
744 return float32_maybe_silence_nan(a
, status
);
746 return float32_maybe_silence_nan(b
, status
);
748 return float32_maybe_silence_nan(c
, status
);
751 return float32_default_nan(status
);
755 #ifdef NO_SIGNALING_NANS
756 int float64_is_quiet_nan(float64 a_
, float_status
*status
)
758 return float64_is_any_nan(a_
);
761 int float64_is_signaling_nan(float64 a_
, float_status
*status
)
766 /*----------------------------------------------------------------------------
767 | Returns 1 if the double-precision floating-point value `a' is a quiet
768 | NaN; otherwise returns 0.
769 *----------------------------------------------------------------------------*/
771 int float64_is_quiet_nan(float64 a_
, float_status
*status
)
773 uint64_t a
= float64_val(a_
);
774 if (status
->snan_bit_is_one
) {
775 return (((a
>> 51) & 0xFFF) == 0xFFE)
776 && (a
& 0x0007FFFFFFFFFFFFULL
);
778 return ((a
<< 1) >= 0xFFF0000000000000ULL
);
782 /*----------------------------------------------------------------------------
783 | Returns 1 if the double-precision floating-point value `a' is a signaling
784 | NaN; otherwise returns 0.
785 *----------------------------------------------------------------------------*/
787 int float64_is_signaling_nan(float64 a_
, float_status
*status
)
789 uint64_t a
= float64_val(a_
);
790 if (status
->snan_bit_is_one
) {
791 return ((a
<< 1) >= 0xFFF0000000000000ULL
);
793 return (((a
>> 51) & 0xFFF) == 0xFFE)
794 && (a
& LIT64(0x0007FFFFFFFFFFFF));
799 /*----------------------------------------------------------------------------
800 | Returns a quiet NaN if the double-precision floating point value `a' is a
801 | signaling NaN; otherwise returns `a'.
802 *----------------------------------------------------------------------------*/
804 float64
float64_maybe_silence_nan(float64 a_
, float_status
*status
)
806 if (float64_is_signaling_nan(a_
, status
)) {
807 if (status
->snan_bit_is_one
) {
809 uint64_t a
= float64_val(a_
);
810 a
&= ~0x0008000000000000ULL
;
811 a
|= 0x0004000000000000ULL
;
812 return make_float64(a
);
814 return float64_default_nan(status
);
817 uint64_t a
= float64_val(a_
);
818 a
|= LIT64(0x0008000000000000);
819 return make_float64(a
);
825 /*----------------------------------------------------------------------------
826 | Returns the result of converting the double-precision floating-point NaN
827 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
828 | exception is raised.
829 *----------------------------------------------------------------------------*/
831 static commonNaNT
float64ToCommonNaN(float64 a
, float_status
*status
)
835 if (float64_is_signaling_nan(a
, status
)) {
836 float_raise(float_flag_invalid
, status
);
838 z
.sign
= float64_val(a
) >> 63;
840 z
.high
= float64_val(a
) << 12;
844 /*----------------------------------------------------------------------------
845 | Returns the result of converting the canonical NaN `a' to the double-
846 | precision floating-point format.
847 *----------------------------------------------------------------------------*/
849 static float64
commonNaNToFloat64(commonNaNT a
, float_status
*status
)
851 uint64_t mantissa
= a
.high
>> 12;
853 if (status
->default_nan_mode
) {
854 return float64_default_nan(status
);
859 (((uint64_t) a
.sign
) << 63)
860 | LIT64(0x7FF0000000000000)
863 return float64_default_nan(status
);
867 /*----------------------------------------------------------------------------
868 | Takes two double-precision floating-point values `a' and `b', one of which
869 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
870 | signaling NaN, the invalid exception is raised.
871 *----------------------------------------------------------------------------*/
873 static float64
propagateFloat64NaN(float64 a
, float64 b
, float_status
*status
)
875 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
876 flag aIsLargerSignificand
;
879 aIsQuietNaN
= float64_is_quiet_nan(a
, status
);
880 aIsSignalingNaN
= float64_is_signaling_nan(a
, status
);
881 bIsQuietNaN
= float64_is_quiet_nan(b
, status
);
882 bIsSignalingNaN
= float64_is_signaling_nan(b
, status
);
886 if (aIsSignalingNaN
| bIsSignalingNaN
) {
887 float_raise(float_flag_invalid
, status
);
890 if (status
->default_nan_mode
) {
891 return float64_default_nan(status
);
894 if ((uint64_t)(av
<< 1) < (uint64_t)(bv
<< 1)) {
895 aIsLargerSignificand
= 0;
896 } else if ((uint64_t)(bv
<< 1) < (uint64_t)(av
<< 1)) {
897 aIsLargerSignificand
= 1;
899 aIsLargerSignificand
= (av
< bv
) ? 1 : 0;
902 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
903 aIsLargerSignificand
)) {
904 return float64_maybe_silence_nan(b
, status
);
906 return float64_maybe_silence_nan(a
, status
);
910 /*----------------------------------------------------------------------------
911 | Takes three double-precision floating-point values `a', `b' and `c', one of
912 | which is a NaN, and returns the appropriate NaN result. If any of `a',
913 | `b' or `c' is a signaling NaN, the invalid exception is raised.
914 | The input infzero indicates whether a*b was 0*inf or inf*0 (in which case
915 | obviously c is a NaN, and whether to propagate c or some other NaN is
916 | implementation defined).
917 *----------------------------------------------------------------------------*/
919 static float64
propagateFloat64MulAddNaN(float64 a
, float64 b
,
920 float64 c
, flag infzero
,
921 float_status
*status
)
923 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
924 cIsQuietNaN
, cIsSignalingNaN
;
927 aIsQuietNaN
= float64_is_quiet_nan(a
, status
);
928 aIsSignalingNaN
= float64_is_signaling_nan(a
, status
);
929 bIsQuietNaN
= float64_is_quiet_nan(b
, status
);
930 bIsSignalingNaN
= float64_is_signaling_nan(b
, status
);
931 cIsQuietNaN
= float64_is_quiet_nan(c
, status
);
932 cIsSignalingNaN
= float64_is_signaling_nan(c
, status
);
934 if (aIsSignalingNaN
| bIsSignalingNaN
| cIsSignalingNaN
) {
935 float_raise(float_flag_invalid
, status
);
938 which
= pickNaNMulAdd(aIsQuietNaN
, aIsSignalingNaN
,
939 bIsQuietNaN
, bIsSignalingNaN
,
940 cIsQuietNaN
, cIsSignalingNaN
, infzero
, status
);
942 if (status
->default_nan_mode
) {
943 /* Note that this check is after pickNaNMulAdd so that function
944 * has an opportunity to set the Invalid flag.
946 return float64_default_nan(status
);
951 return float64_maybe_silence_nan(a
, status
);
953 return float64_maybe_silence_nan(b
, status
);
955 return float64_maybe_silence_nan(c
, status
);
958 return float64_default_nan(status
);
962 #ifdef NO_SIGNALING_NANS
963 int floatx80_is_quiet_nan(floatx80 a_
, float_status
*status
)
965 return floatx80_is_any_nan(a_
);
968 int floatx80_is_signaling_nan(floatx80 a_
, float_status
*status
)
973 /*----------------------------------------------------------------------------
974 | Returns 1 if the extended double-precision floating-point value `a' is a
975 | quiet NaN; otherwise returns 0. This slightly differs from the same
976 | function for other types as floatx80 has an explicit bit.
977 *----------------------------------------------------------------------------*/
979 int floatx80_is_quiet_nan(floatx80 a
, float_status
*status
)
981 if (status
->snan_bit_is_one
) {
984 aLow
= a
.low
& ~0x4000000000000000ULL
;
985 return ((a
.high
& 0x7FFF) == 0x7FFF)
989 return ((a
.high
& 0x7FFF) == 0x7FFF)
990 && (LIT64(0x8000000000000000) <= ((uint64_t)(a
.low
<< 1)));
994 /*----------------------------------------------------------------------------
995 | Returns 1 if the extended double-precision floating-point value `a' is a
996 | signaling NaN; otherwise returns 0. This slightly differs from the same
997 | function for other types as floatx80 has an explicit bit.
998 *----------------------------------------------------------------------------*/
1000 int floatx80_is_signaling_nan(floatx80 a
, float_status
*status
)
1002 if (status
->snan_bit_is_one
) {
1003 return ((a
.high
& 0x7FFF) == 0x7FFF)
1004 && ((a
.low
<< 1) >= 0x8000000000000000ULL
);
1008 aLow
= a
.low
& ~LIT64(0x4000000000000000);
1009 return ((a
.high
& 0x7FFF) == 0x7FFF)
1010 && (uint64_t)(aLow
<< 1)
1016 /*----------------------------------------------------------------------------
1017 | Returns a quiet NaN if the extended double-precision floating point value
1018 | `a' is a signaling NaN; otherwise returns `a'.
1019 *----------------------------------------------------------------------------*/
1021 floatx80
floatx80_maybe_silence_nan(floatx80 a
, float_status
*status
)
1023 if (floatx80_is_signaling_nan(a
, status
)) {
1024 if (status
->snan_bit_is_one
) {
1025 a
= floatx80_default_nan(status
);
1027 a
.low
|= LIT64(0xC000000000000000);
1034 /*----------------------------------------------------------------------------
1035 | Returns the result of converting the extended double-precision floating-
1036 | point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
1037 | invalid exception is raised.
1038 *----------------------------------------------------------------------------*/
1040 static commonNaNT
floatx80ToCommonNaN(floatx80 a
, float_status
*status
)
1045 if (floatx80_is_signaling_nan(a
, status
)) {
1046 float_raise(float_flag_invalid
, status
);
1049 z
.sign
= a
.high
>> 15;
1051 z
.high
= a
.low
<< 1;
1053 dflt
= floatx80_default_nan(status
);
1054 z
.sign
= dflt
.high
>> 15;
1056 z
.high
= dflt
.low
<< 1;
1061 /*----------------------------------------------------------------------------
1062 | Returns the result of converting the canonical NaN `a' to the extended
1063 | double-precision floating-point format.
1064 *----------------------------------------------------------------------------*/
1066 static floatx80
commonNaNToFloatx80(commonNaNT a
, float_status
*status
)
1070 if (status
->default_nan_mode
) {
1071 return floatx80_default_nan(status
);
1075 z
.low
= LIT64(0x8000000000000000) | a
.high
>> 1;
1076 z
.high
= (((uint16_t)a
.sign
) << 15) | 0x7FFF;
1078 z
= floatx80_default_nan(status
);
1083 /*----------------------------------------------------------------------------
1084 | Takes two extended double-precision floating-point values `a' and `b', one
1085 | of which is a NaN, and returns the appropriate NaN result. If either `a' or
1086 | `b' is a signaling NaN, the invalid exception is raised.
1087 *----------------------------------------------------------------------------*/
1089 static floatx80
propagateFloatx80NaN(floatx80 a
, floatx80 b
,
1090 float_status
*status
)
1092 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
1093 flag aIsLargerSignificand
;
1095 aIsQuietNaN
= floatx80_is_quiet_nan(a
, status
);
1096 aIsSignalingNaN
= floatx80_is_signaling_nan(a
, status
);
1097 bIsQuietNaN
= floatx80_is_quiet_nan(b
, status
);
1098 bIsSignalingNaN
= floatx80_is_signaling_nan(b
, status
);
1100 if (aIsSignalingNaN
| bIsSignalingNaN
) {
1101 float_raise(float_flag_invalid
, status
);
1104 if (status
->default_nan_mode
) {
1105 return floatx80_default_nan(status
);
1108 if (a
.low
< b
.low
) {
1109 aIsLargerSignificand
= 0;
1110 } else if (b
.low
< a
.low
) {
1111 aIsLargerSignificand
= 1;
1113 aIsLargerSignificand
= (a
.high
< b
.high
) ? 1 : 0;
1116 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
1117 aIsLargerSignificand
)) {
1118 return floatx80_maybe_silence_nan(b
, status
);
1120 return floatx80_maybe_silence_nan(a
, status
);
1124 #ifdef NO_SIGNALING_NANS
1125 int float128_is_quiet_nan(float128 a_
, float_status
*status
)
1127 return float128_is_any_nan(a_
);
1130 int float128_is_signaling_nan(float128 a_
, float_status
*status
)
1135 /*----------------------------------------------------------------------------
1136 | Returns 1 if the quadruple-precision floating-point value `a' is a quiet
1137 | NaN; otherwise returns 0.
1138 *----------------------------------------------------------------------------*/
1140 int float128_is_quiet_nan(float128 a
, float_status
*status
)
1142 if (status
->snan_bit_is_one
) {
1143 return (((a
.high
>> 47) & 0xFFFF) == 0xFFFE)
1144 && (a
.low
|| (a
.high
& 0x00007FFFFFFFFFFFULL
));
1146 return ((a
.high
<< 1) >= 0xFFFF000000000000ULL
)
1147 && (a
.low
|| (a
.high
& 0x0000FFFFFFFFFFFFULL
));
1151 /*----------------------------------------------------------------------------
1152 | Returns 1 if the quadruple-precision floating-point value `a' is a
1153 | signaling NaN; otherwise returns 0.
1154 *----------------------------------------------------------------------------*/
1156 int float128_is_signaling_nan(float128 a
, float_status
*status
)
1158 if (status
->snan_bit_is_one
) {
1159 return ((a
.high
<< 1) >= 0xFFFF000000000000ULL
)
1160 && (a
.low
|| (a
.high
& 0x0000FFFFFFFFFFFFULL
));
1162 return (((a
.high
>> 47) & 0xFFFF) == 0xFFFE)
1163 && (a
.low
|| (a
.high
& LIT64(0x00007FFFFFFFFFFF)));
1168 /*----------------------------------------------------------------------------
1169 | Returns a quiet NaN if the quadruple-precision floating point value `a' is
1170 | a signaling NaN; otherwise returns `a'.
1171 *----------------------------------------------------------------------------*/
1173 float128
float128_maybe_silence_nan(float128 a
, float_status
*status
)
1175 if (float128_is_signaling_nan(a
, status
)) {
1176 if (status
->snan_bit_is_one
) {
1177 a
= float128_default_nan(status
);
1179 a
.high
|= LIT64(0x0000800000000000);
1186 /*----------------------------------------------------------------------------
1187 | Returns the result of converting the quadruple-precision floating-point NaN
1188 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
1189 | exception is raised.
1190 *----------------------------------------------------------------------------*/
1192 static commonNaNT
float128ToCommonNaN(float128 a
, float_status
*status
)
1196 if (float128_is_signaling_nan(a
, status
)) {
1197 float_raise(float_flag_invalid
, status
);
1199 z
.sign
= a
.high
>> 63;
1200 shortShift128Left(a
.high
, a
.low
, 16, &z
.high
, &z
.low
);
1204 /*----------------------------------------------------------------------------
1205 | Returns the result of converting the canonical NaN `a' to the quadruple-
1206 | precision floating-point format.
1207 *----------------------------------------------------------------------------*/
1209 static float128
commonNaNToFloat128(commonNaNT a
, float_status
*status
)
1213 if (status
->default_nan_mode
) {
1214 return float128_default_nan(status
);
1217 shift128Right(a
.high
, a
.low
, 16, &z
.high
, &z
.low
);
1218 z
.high
|= (((uint64_t)a
.sign
) << 63) | LIT64(0x7FFF000000000000);
1222 /*----------------------------------------------------------------------------
1223 | Takes two quadruple-precision floating-point values `a' and `b', one of
1224 | which is a NaN, and returns the appropriate NaN result. If either `a' or
1225 | `b' is a signaling NaN, the invalid exception is raised.
1226 *----------------------------------------------------------------------------*/
1228 static float128
propagateFloat128NaN(float128 a
, float128 b
,
1229 float_status
*status
)
1231 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
1232 flag aIsLargerSignificand
;
1234 aIsQuietNaN
= float128_is_quiet_nan(a
, status
);
1235 aIsSignalingNaN
= float128_is_signaling_nan(a
, status
);
1236 bIsQuietNaN
= float128_is_quiet_nan(b
, status
);
1237 bIsSignalingNaN
= float128_is_signaling_nan(b
, status
);
1239 if (aIsSignalingNaN
| bIsSignalingNaN
) {
1240 float_raise(float_flag_invalid
, status
);
1243 if (status
->default_nan_mode
) {
1244 return float128_default_nan(status
);
1247 if (lt128(a
.high
<< 1, a
.low
, b
.high
<< 1, b
.low
)) {
1248 aIsLargerSignificand
= 0;
1249 } else if (lt128(b
.high
<< 1, b
.low
, a
.high
<< 1, a
.low
)) {
1250 aIsLargerSignificand
= 1;
1252 aIsLargerSignificand
= (a
.high
< b
.high
) ? 1 : 0;
1255 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
1256 aIsLargerSignificand
)) {
1257 return float128_maybe_silence_nan(b
, status
);
1259 return float128_maybe_silence_nan(a
, status
);