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.
83 * Define whether architecture deviates from IEEE in not supporting
84 * signaling NaNs (so all NaNs are treated as quiet).
86 static inline bool no_signaling_nans(float_status *status)
88 #if defined(TARGET_XTENSA)
89 return status->no_signaling_nans;
95 /* Define how the architecture discriminates signaling NaNs.
96 * This done with the most significant bit of the fraction.
97 * In IEEE 754-1985 this was implementation defined, but in IEEE 754-2008
98 * the msb must be zero. MIPS is (so far) unique in supporting both the
99 * 2008 revision and backward compatibility with their original choice.
100 * Thus for MIPS we must make the choice at runtime.
102 static inline bool snan_bit_is_one(float_status *status)
104 #if defined(TARGET_MIPS)
105 return status->snan_bit_is_one;
106 #elif defined(TARGET_HPPA) || defined(TARGET_SH4)
113 /*----------------------------------------------------------------------------
114 | For the deconstructed floating-point with fraction FRAC, return true
115 | if the fraction represents a signalling NaN; otherwise false.
116 *----------------------------------------------------------------------------*/
118 static bool parts_is_snan_frac(uint64_t frac, float_status *status)
120 if (no_signaling_nans(status)) {
123 bool msb = extract64(frac, DECOMPOSED_BINARY_POINT - 1, 1);
124 return msb == snan_bit_is_one(status);
128 /*----------------------------------------------------------------------------
129 | The pattern for a default generated deconstructed floating-point NaN.
130 *----------------------------------------------------------------------------*/
132 static FloatParts parts_default_nan(float_status *status)
137 #if defined(TARGET_SPARC) || defined(TARGET_M68K)
138 /* !snan_bit_is_one, set all bits */
139 frac = (1ULL << DECOMPOSED_BINARY_POINT) - 1;
140 #elif defined(TARGET_I386) || defined(TARGET_X86_64) \
141 || defined(TARGET_MICROBLAZE)
142 /* !snan_bit_is_one, set sign and msb */
143 frac = 1ULL << (DECOMPOSED_BINARY_POINT - 1);
145 #elif defined(TARGET_HPPA)
146 /* snan_bit_is_one, set msb-1. */
147 frac = 1ULL << (DECOMPOSED_BINARY_POINT - 2);
148 #elif defined(TARGET_HEXAGON)
153 * This case is true for Alpha, ARM, MIPS, OpenRISC, PPC, RISC-V,
154 * S390, SH4, TriCore, and Xtensa. Our other supported targets,
155 * CRIS, Nios2, and Tile, do not have floating-point.
157 if (snan_bit_is_one(status)) {
158 /* set all bits other than msb */
159 frac = (1ULL << (DECOMPOSED_BINARY_POINT - 1)) - 1;
162 frac = 1ULL << (DECOMPOSED_BINARY_POINT - 1);
166 return (FloatParts) {
167 .cls = float_class_qnan,
174 /*----------------------------------------------------------------------------
175 | Returns a quiet NaN from a signalling NaN for the deconstructed
176 | floating-point parts.
177 *----------------------------------------------------------------------------*/
179 static FloatParts parts_silence_nan(FloatParts a, float_status *status)
181 g_assert(!no_signaling_nans(status));
182 #if defined(TARGET_HPPA)
183 a.frac &= ~(1ULL << (DECOMPOSED_BINARY_POINT - 1));
184 a.frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 2);
186 if (snan_bit_is_one(status)) {
187 return parts_default_nan(status);
189 a.frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 1);
192 a.cls = float_class_qnan;
196 /*----------------------------------------------------------------------------
197 | The pattern for a default generated extended double-precision NaN.
198 *----------------------------------------------------------------------------*/
199 floatx80 floatx80_default_nan(float_status *status)
203 /* None of the targets that have snan_bit_is_one use floatx80. */
204 assert(!snan_bit_is_one(status));
205 #if defined(TARGET_M68K)
206 r.low = UINT64_C(0xFFFFFFFFFFFFFFFF);
210 r.low = UINT64_C(0xC000000000000000);
216 /*----------------------------------------------------------------------------
217 | The pattern for a default generated extended double-precision inf.
218 *----------------------------------------------------------------------------*/
220 #define floatx80_infinity_high 0x7FFF
221 #if defined(TARGET_M68K)
222 #define floatx80_infinity_low UINT64_C(0x0000000000000000)
224 #define floatx80_infinity_low UINT64_C(0x8000000000000000)
227 const floatx80 floatx80_infinity
228 = make_floatx80_init(floatx80_infinity_high, floatx80_infinity_low);
230 /*----------------------------------------------------------------------------
231 | Raises the exceptions specified by `flags'. Floating-point traps can be
232 | defined here if desired. It is currently not possible for such a trap
233 | to substitute a result value. If traps are not implemented, this routine
234 | should be simply `float_exception_flags |= flags;'.
235 *----------------------------------------------------------------------------*/
237 void float_raise(uint8_t flags, float_status *status)
239 status->float_exception_flags |= flags;
242 /*----------------------------------------------------------------------------
243 | Internal canonical NaN format.
244 *----------------------------------------------------------------------------*/
250 /*----------------------------------------------------------------------------
251 | Returns 1 if the half-precision floating-point value `a' is a quiet
252 | NaN; otherwise returns 0.
253 *----------------------------------------------------------------------------*/
255 bool float16_is_quiet_nan(float16 a_, float_status *status)
257 if (no_signaling_nans(status)) {
258 return float16_is_any_nan(a_);
260 uint16_t a = float16_val(a_);
261 if (snan_bit_is_one(status)) {
262 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF);
265 return ((a >> 9) & 0x3F) == 0x3F;
270 /*----------------------------------------------------------------------------
271 | Returns 1 if the bfloat16 value `a' is a quiet
272 | NaN; otherwise returns 0.
273 *----------------------------------------------------------------------------*/
275 bool bfloat16_is_quiet_nan(bfloat16 a_, float_status *status)
277 if (no_signaling_nans(status)) {
278 return bfloat16_is_any_nan(a_);
281 if (snan_bit_is_one(status)) {
282 return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F);
284 return ((a >> 6) & 0x1FF) == 0x1FF;
289 /*----------------------------------------------------------------------------
290 | Returns 1 if the half-precision floating-point value `a' is a signaling
291 | NaN; otherwise returns 0.
292 *----------------------------------------------------------------------------*/
294 bool float16_is_signaling_nan(float16 a_, float_status *status)
296 if (no_signaling_nans(status)) {
299 uint16_t a = float16_val(a_);
300 if (snan_bit_is_one(status)) {
301 return ((a >> 9) & 0x3F) == 0x3F;
303 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF);
308 /*----------------------------------------------------------------------------
309 | Returns 1 if the bfloat16 value `a' is a signaling
310 | NaN; otherwise returns 0.
311 *----------------------------------------------------------------------------*/
313 bool bfloat16_is_signaling_nan(bfloat16 a_, float_status *status)
315 if (no_signaling_nans(status)) {
319 if (snan_bit_is_one(status)) {
320 return ((a >> 6) & 0x1FF) == 0x1FF;
322 return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F);
327 /*----------------------------------------------------------------------------
328 | Returns 1 if the single-precision floating-point value `a' is a quiet
329 | NaN; otherwise returns 0.
330 *----------------------------------------------------------------------------*/
332 bool float32_is_quiet_nan(float32 a_, float_status *status)
334 if (no_signaling_nans(status)) {
335 return float32_is_any_nan(a_);
337 uint32_t a = float32_val(a_);
338 if (snan_bit_is_one(status)) {
339 return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF);
341 return ((uint32_t)(a << 1) >= 0xFF800000);
346 /*----------------------------------------------------------------------------
347 | Returns 1 if the single-precision floating-point value `a' is a signaling
348 | NaN; otherwise returns 0.
349 *----------------------------------------------------------------------------*/
351 bool float32_is_signaling_nan(float32 a_, float_status *status)
353 if (no_signaling_nans(status)) {
356 uint32_t a = float32_val(a_);
357 if (snan_bit_is_one(status)) {
358 return ((uint32_t)(a << 1) >= 0xFF800000);
360 return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF);
365 /*----------------------------------------------------------------------------
366 | Returns the result of converting the single-precision floating-point NaN
367 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
368 | exception is raised.
369 *----------------------------------------------------------------------------*/
371 static commonNaNT float32ToCommonNaN(float32 a, float_status *status)
375 if (float32_is_signaling_nan(a, status)) {
376 float_raise(float_flag_invalid, status);
378 z.sign = float32_val(a) >> 31;
380 z.high = ((uint64_t)float32_val(a)) << 41;
384 /*----------------------------------------------------------------------------
385 | Returns the result of converting the canonical NaN `a' to the single-
386 | precision floating-point format.
387 *----------------------------------------------------------------------------*/
389 static float32 commonNaNToFloat32(commonNaNT a, float_status *status)
391 uint32_t mantissa = a.high >> 41;
393 if (status->default_nan_mode) {
394 return float32_default_nan(status);
399 (((uint32_t)a.sign) << 31) | 0x7F800000 | (a.high >> 41));
401 return float32_default_nan(status);
405 /*----------------------------------------------------------------------------
406 | Select which NaN to propagate for a two-input operation.
407 | IEEE754 doesn't specify all the details of this, so the
408 | algorithm is target-specific.
409 | The routine is passed various bits of information about the
410 | two NaNs and should return 0 to select NaN a and 1 for NaN b.
411 | Note that signalling NaNs are always squashed to quiet NaNs
412 | by the caller, by calling floatXX_silence_nan() before
415 | aIsLargerSignificand is only valid if both a and b are NaNs
416 | of some kind, and is true if a has the larger significand,
417 | or if both a and b have the same significand but a is
418 | positive but b is negative. It is only needed for the x87
420 *----------------------------------------------------------------------------*/
422 static int pickNaN(FloatClass a_cls, FloatClass b_cls,
423 bool aIsLargerSignificand, float_status *status)
425 #if defined(TARGET_ARM) || defined(TARGET_MIPS) || defined(TARGET_HPPA)
426 /* ARM mandated NaN propagation rules (see FPProcessNaNs()), take
428 * 1. A if it is signaling
429 * 2. B if it is signaling
432 * A signaling NaN is always quietened before returning it.
434 /* According to MIPS specifications, if one of the two operands is
435 * a sNaN, a new qNaN has to be generated. This is done in
436 * floatXX_silence_nan(). For qNaN inputs the specifications
437 * says: "When possible, this QNaN result is one of the operand QNaN
438 * values." In practice it seems that most implementations choose
439 * the first operand if both operands are qNaN. In short this gives
440 * the following rules:
441 * 1. A if it is signaling
442 * 2. B if it is signaling
445 * A signaling NaN is always silenced before returning it.
447 if (is_snan(a_cls)) {
449 } else if (is_snan(b_cls)) {
451 } else if (is_qnan(a_cls)) {
456 #elif defined(TARGET_PPC) || defined(TARGET_M68K)
457 /* PowerPC propagation rules:
458 * 1. A if it sNaN or qNaN
459 * 2. B if it sNaN or qNaN
460 * A signaling NaN is always silenced before returning it.
462 /* M68000 FAMILY PROGRAMMER'S REFERENCE MANUAL
463 * 3.4 FLOATING-POINT INSTRUCTION DETAILS
464 * If either operand, but not both operands, of an operation is a
465 * nonsignaling NaN, then that NaN is returned as the result. If both
466 * operands are nonsignaling NaNs, then the destination operand
467 * nonsignaling NaN is returned as the result.
468 * If either operand to an operation is a signaling NaN (SNaN), then the
469 * SNaN bit is set in the FPSR EXC byte. If the SNaN exception enable bit
470 * is set in the FPCR ENABLE byte, then the exception is taken and the
471 * destination is not modified. If the SNaN exception enable bit is not
472 * set, setting the SNaN bit in the operand to a one converts the SNaN to
473 * a nonsignaling NaN. The operation then continues as described in the
474 * preceding paragraph for nonsignaling NaNs.
481 #elif defined(TARGET_XTENSA)
483 * Xtensa has two NaN propagation modes.
484 * Which one is active is controlled by float_status::use_first_nan.
486 if (status->use_first_nan) {
500 /* This implements x87 NaN propagation rules:
501 * SNaN + QNaN => return the QNaN
502 * two SNaNs => return the one with the larger significand, silenced
503 * two QNaNs => return the one with the larger significand
504 * SNaN and a non-NaN => return the SNaN, silenced
505 * QNaN and a non-NaN => return the QNaN
507 * If we get down to comparing significands and they are the same,
508 * return the NaN with the positive sign bit (if any).
510 if (is_snan(a_cls)) {
511 if (is_snan(b_cls)) {
512 return aIsLargerSignificand ? 0 : 1;
514 return is_qnan(b_cls) ? 1 : 0;
515 } else if (is_qnan(a_cls)) {
516 if (is_snan(b_cls) || !is_qnan(b_cls)) {
519 return aIsLargerSignificand ? 0 : 1;
527 /*----------------------------------------------------------------------------
528 | Select which NaN to propagate for a three-input operation.
529 | For the moment we assume that no CPU needs the 'larger significand'
531 | Return values : 0 : a; 1 : b; 2 : c; 3 : default-NaN
532 *----------------------------------------------------------------------------*/
533 static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls,
534 bool infzero, float_status *status)
536 #if defined(TARGET_ARM)
537 /* For ARM, the (inf,zero,qnan) case sets InvalidOp and returns
540 if (infzero && is_qnan(c_cls)) {
541 float_raise(float_flag_invalid, status);
545 /* This looks different from the ARM ARM pseudocode, because the ARM ARM
546 * puts the operands to a fused mac operation (a*b)+c in the order c,a,b.
548 if (is_snan(c_cls)) {
550 } else if (is_snan(a_cls)) {
552 } else if (is_snan(b_cls)) {
554 } else if (is_qnan(c_cls)) {
556 } else if (is_qnan(a_cls)) {
561 #elif defined(TARGET_MIPS)
562 if (snan_bit_is_one(status)) {
564 * For MIPS systems that conform to IEEE754-1985, the (inf,zero,nan)
565 * case sets InvalidOp and returns the default NaN
568 float_raise(float_flag_invalid, status);
571 /* Prefer sNaN over qNaN, in the a, b, c order. */
572 if (is_snan(a_cls)) {
574 } else if (is_snan(b_cls)) {
576 } else if (is_snan(c_cls)) {
578 } else if (is_qnan(a_cls)) {
580 } else if (is_qnan(b_cls)) {
587 * For MIPS systems that conform to IEEE754-2008, the (inf,zero,nan)
588 * case sets InvalidOp and returns the input value 'c'
591 float_raise(float_flag_invalid, status);
594 /* Prefer sNaN over qNaN, in the c, a, b order. */
595 if (is_snan(c_cls)) {
597 } else if (is_snan(a_cls)) {
599 } else if (is_snan(b_cls)) {
601 } else if (is_qnan(c_cls)) {
603 } else if (is_qnan(a_cls)) {
609 #elif defined(TARGET_PPC)
610 /* For PPC, the (inf,zero,qnan) case sets InvalidOp, but we prefer
611 * to return an input NaN if we have one (ie c) rather than generating
615 float_raise(float_flag_invalid, status);
619 /* If fRA is a NaN return it; otherwise if fRB is a NaN return it;
620 * otherwise return fRC. Note that muladd on PPC is (fRA * fRC) + frB
624 } else if (is_nan(c_cls)) {
629 #elif defined(TARGET_RISCV)
630 /* For RISC-V, InvalidOp is set when multiplicands are Inf and zero */
632 float_raise(float_flag_invalid, status);
634 return 3; /* default NaN */
635 #elif defined(TARGET_XTENSA)
637 * For Xtensa, the (inf,zero,nan) case sets InvalidOp and returns
638 * an input NaN if we have one (ie c).
641 float_raise(float_flag_invalid, status);
644 if (status->use_first_nan) {
647 } else if (is_nan(b_cls)) {
655 } else if (is_nan(b_cls)) {
662 /* A default implementation: prefer a to b to c.
663 * This is unlikely to actually match any real implementation.
667 } else if (is_nan(b_cls)) {
675 /*----------------------------------------------------------------------------
676 | Takes two single-precision floating-point values `a' and `b', one of which
677 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
678 | signaling NaN, the invalid exception is raised.
679 *----------------------------------------------------------------------------*/
681 static float32 propagateFloat32NaN(float32 a, float32 b, float_status *status)
683 bool aIsLargerSignificand;
685 FloatClass a_cls, b_cls;
687 /* This is not complete, but is good enough for pickNaN. */
688 a_cls = (!float32_is_any_nan(a)
690 : float32_is_signaling_nan(a, status)
693 b_cls = (!float32_is_any_nan(b)
695 : float32_is_signaling_nan(b, status)
702 if (is_snan(a_cls) || is_snan(b_cls)) {
703 float_raise(float_flag_invalid, status);
706 if (status->default_nan_mode) {
707 return float32_default_nan(status);
710 if ((uint32_t)(av << 1) < (uint32_t)(bv << 1)) {
711 aIsLargerSignificand = 0;
712 } else if ((uint32_t)(bv << 1) < (uint32_t)(av << 1)) {
713 aIsLargerSignificand = 1;
715 aIsLargerSignificand = (av < bv) ? 1 : 0;
718 if (pickNaN(a_cls, b_cls, aIsLargerSignificand, status)) {
719 if (is_snan(b_cls)) {
720 return float32_silence_nan(b, status);
724 if (is_snan(a_cls)) {
725 return float32_silence_nan(a, status);
731 /*----------------------------------------------------------------------------
732 | Returns 1 if the double-precision floating-point value `a' is a quiet
733 | NaN; otherwise returns 0.
734 *----------------------------------------------------------------------------*/
736 bool float64_is_quiet_nan(float64 a_, float_status *status)
738 if (no_signaling_nans(status)) {
739 return float64_is_any_nan(a_);
741 uint64_t a = float64_val(a_);
742 if (snan_bit_is_one(status)) {
743 return (((a >> 51) & 0xFFF) == 0xFFE)
744 && (a & 0x0007FFFFFFFFFFFFULL);
746 return ((a << 1) >= 0xFFF0000000000000ULL);
751 /*----------------------------------------------------------------------------
752 | Returns 1 if the double-precision floating-point value `a' is a signaling
753 | NaN; otherwise returns 0.
754 *----------------------------------------------------------------------------*/
756 bool float64_is_signaling_nan(float64 a_, float_status *status)
758 if (no_signaling_nans(status)) {
761 uint64_t a = float64_val(a_);
762 if (snan_bit_is_one(status)) {
763 return ((a << 1) >= 0xFFF0000000000000ULL);
765 return (((a >> 51) & 0xFFF) == 0xFFE)
766 && (a & UINT64_C(0x0007FFFFFFFFFFFF));
771 /*----------------------------------------------------------------------------
772 | Returns the result of converting the double-precision floating-point NaN
773 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
774 | exception is raised.
775 *----------------------------------------------------------------------------*/
777 static commonNaNT float64ToCommonNaN(float64 a, float_status *status)
781 if (float64_is_signaling_nan(a, status)) {
782 float_raise(float_flag_invalid, status);
784 z.sign = float64_val(a) >> 63;
786 z.high = float64_val(a) << 12;
790 /*----------------------------------------------------------------------------
791 | Returns the result of converting the canonical NaN `a' to the double-
792 | precision floating-point format.
793 *----------------------------------------------------------------------------*/
795 static float64 commonNaNToFloat64(commonNaNT a, float_status *status)
797 uint64_t mantissa = a.high >> 12;
799 if (status->default_nan_mode) {
800 return float64_default_nan(status);
805 (((uint64_t) a.sign) << 63)
806 | UINT64_C(0x7FF0000000000000)
809 return float64_default_nan(status);
813 /*----------------------------------------------------------------------------
814 | Takes two double-precision floating-point values `a' and `b', one of which
815 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
816 | signaling NaN, the invalid exception is raised.
817 *----------------------------------------------------------------------------*/
819 static float64 propagateFloat64NaN(float64 a, float64 b, float_status *status)
821 bool aIsLargerSignificand;
823 FloatClass a_cls, b_cls;
825 /* This is not complete, but is good enough for pickNaN. */
826 a_cls = (!float64_is_any_nan(a)
828 : float64_is_signaling_nan(a, status)
831 b_cls = (!float64_is_any_nan(b)
833 : float64_is_signaling_nan(b, status)
840 if (is_snan(a_cls) || is_snan(b_cls)) {
841 float_raise(float_flag_invalid, status);
844 if (status->default_nan_mode) {
845 return float64_default_nan(status);
848 if ((uint64_t)(av << 1) < (uint64_t)(bv << 1)) {
849 aIsLargerSignificand = 0;
850 } else if ((uint64_t)(bv << 1) < (uint64_t)(av << 1)) {
851 aIsLargerSignificand = 1;
853 aIsLargerSignificand = (av < bv) ? 1 : 0;
856 if (pickNaN(a_cls, b_cls, aIsLargerSignificand, status)) {
857 if (is_snan(b_cls)) {
858 return float64_silence_nan(b, status);
862 if (is_snan(a_cls)) {
863 return float64_silence_nan(a, status);
869 /*----------------------------------------------------------------------------
870 | Returns 1 if the extended double-precision floating-point value `a' is a
871 | quiet NaN; otherwise returns 0. This slightly differs from the same
872 | function for other types as floatx80 has an explicit bit.
873 *----------------------------------------------------------------------------*/
875 int floatx80_is_quiet_nan(floatx80 a, float_status *status)
877 if (no_signaling_nans(status)) {
878 return floatx80_is_any_nan(a);
880 if (snan_bit_is_one(status)) {
883 aLow = a.low & ~0x4000000000000000ULL;
884 return ((a.high & 0x7FFF) == 0x7FFF)
888 return ((a.high & 0x7FFF) == 0x7FFF)
889 && (UINT64_C(0x8000000000000000) <= ((uint64_t)(a.low << 1)));
894 /*----------------------------------------------------------------------------
895 | Returns 1 if the extended double-precision floating-point value `a' is a
896 | signaling NaN; otherwise returns 0. This slightly differs from the same
897 | function for other types as floatx80 has an explicit bit.
898 *----------------------------------------------------------------------------*/
900 int floatx80_is_signaling_nan(floatx80 a, float_status *status)
902 if (no_signaling_nans(status)) {
905 if (snan_bit_is_one(status)) {
906 return ((a.high & 0x7FFF) == 0x7FFF)
907 && ((a.low << 1) >= 0x8000000000000000ULL);
911 aLow = a.low & ~UINT64_C(0x4000000000000000);
912 return ((a.high & 0x7FFF) == 0x7FFF)
913 && (uint64_t)(aLow << 1)
919 /*----------------------------------------------------------------------------
920 | Returns a quiet NaN from a signalling NaN for the extended double-precision
921 | floating point value `a'.
922 *----------------------------------------------------------------------------*/
924 floatx80 floatx80_silence_nan(floatx80 a, float_status *status)
926 /* None of the targets that have snan_bit_is_one use floatx80. */
927 assert(!snan_bit_is_one(status));
928 a.low |= UINT64_C(0xC000000000000000);
932 /*----------------------------------------------------------------------------
933 | Returns the result of converting the extended double-precision floating-
934 | point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
935 | invalid exception is raised.
936 *----------------------------------------------------------------------------*/
938 static commonNaNT floatx80ToCommonNaN(floatx80 a, float_status *status)
943 if (floatx80_is_signaling_nan(a, status)) {
944 float_raise(float_flag_invalid, status);
947 z.sign = a.high >> 15;
951 dflt = floatx80_default_nan(status);
952 z.sign = dflt.high >> 15;
954 z.high = dflt.low << 1;
959 /*----------------------------------------------------------------------------
960 | Returns the result of converting the canonical NaN `a' to the extended
961 | double-precision floating-point format.
962 *----------------------------------------------------------------------------*/
964 static floatx80 commonNaNToFloatx80(commonNaNT a, float_status *status)
968 if (status->default_nan_mode) {
969 return floatx80_default_nan(status);
973 z.low = UINT64_C(0x8000000000000000) | a.high >> 1;
974 z.high = (((uint16_t)a.sign) << 15) | 0x7FFF;
976 z = floatx80_default_nan(status);
981 /*----------------------------------------------------------------------------
982 | Takes two extended double-precision floating-point values `a' and `b', one
983 | of which is a NaN, and returns the appropriate NaN result. If either `a' or
984 | `b' is a signaling NaN, the invalid exception is raised.
985 *----------------------------------------------------------------------------*/
987 floatx80 propagateFloatx80NaN(floatx80 a, floatx80 b, float_status *status)
989 bool aIsLargerSignificand;
990 FloatClass a_cls, b_cls;
992 /* This is not complete, but is good enough for pickNaN. */
993 a_cls = (!floatx80_is_any_nan(a)
995 : floatx80_is_signaling_nan(a, status)
998 b_cls = (!floatx80_is_any_nan(b)
1000 : floatx80_is_signaling_nan(b, status)
1002 : float_class_qnan);
1004 if (is_snan(a_cls) || is_snan(b_cls)) {
1005 float_raise(float_flag_invalid, status);
1008 if (status->default_nan_mode) {
1009 return floatx80_default_nan(status);
1012 if (a.low < b.low) {
1013 aIsLargerSignificand = 0;
1014 } else if (b.low < a.low) {
1015 aIsLargerSignificand = 1;
1017 aIsLargerSignificand = (a.high < b.high) ? 1 : 0;
1020 if (pickNaN(a_cls, b_cls, aIsLargerSignificand, status)) {
1021 if (is_snan(b_cls)) {
1022 return floatx80_silence_nan(b, status);
1026 if (is_snan(a_cls)) {
1027 return floatx80_silence_nan(a, status);
1033 /*----------------------------------------------------------------------------
1034 | Returns 1 if the quadruple-precision floating-point value `a' is a quiet
1035 | NaN; otherwise returns 0.
1036 *----------------------------------------------------------------------------*/
1038 bool float128_is_quiet_nan(float128 a, float_status *status)
1040 if (no_signaling_nans(status)) {
1041 return float128_is_any_nan(a);
1043 if (snan_bit_is_one(status)) {
1044 return (((a.high >> 47) & 0xFFFF) == 0xFFFE)
1045 && (a.low || (a.high & 0x00007FFFFFFFFFFFULL));
1047 return ((a.high << 1) >= 0xFFFF000000000000ULL)
1048 && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL));
1053 /*----------------------------------------------------------------------------
1054 | Returns 1 if the quadruple-precision floating-point value `a' is a
1055 | signaling NaN; otherwise returns 0.
1056 *----------------------------------------------------------------------------*/
1058 bool float128_is_signaling_nan(float128 a, float_status *status)
1060 if (no_signaling_nans(status)) {
1063 if (snan_bit_is_one(status)) {
1064 return ((a.high << 1) >= 0xFFFF000000000000ULL)
1065 && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL));
1067 return (((a.high >> 47) & 0xFFFF) == 0xFFFE)
1068 && (a.low || (a.high & UINT64_C(0x00007FFFFFFFFFFF)));
1073 /*----------------------------------------------------------------------------
1074 | Returns a quiet NaN from a signalling NaN for the quadruple-precision
1075 | floating point value `a'.
1076 *----------------------------------------------------------------------------*/
1078 float128 float128_silence_nan(float128 a, float_status *status)
1080 if (no_signaling_nans(status)) {
1081 g_assert_not_reached();
1083 if (snan_bit_is_one(status)) {
1084 return float128_default_nan(status);
1086 a.high |= UINT64_C(0x0000800000000000);
1092 /*----------------------------------------------------------------------------
1093 | Returns the result of converting the quadruple-precision floating-point NaN
1094 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
1095 | exception is raised.
1096 *----------------------------------------------------------------------------*/
1098 static commonNaNT float128ToCommonNaN(float128 a, float_status *status)
1102 if (float128_is_signaling_nan(a, status)) {
1103 float_raise(float_flag_invalid, status);
1105 z.sign = a.high >> 63;
1106 shortShift128Left(a.high, a.low, 16, &z.high, &z.low);
1110 /*----------------------------------------------------------------------------
1111 | Returns the result of converting the canonical NaN `a' to the quadruple-
1112 | precision floating-point format.
1113 *----------------------------------------------------------------------------*/
1115 static float128 commonNaNToFloat128(commonNaNT a, float_status *status)
1119 if (status->default_nan_mode) {
1120 return float128_default_nan(status);
1123 shift128Right(a.high, a.low, 16, &z.high, &z.low);
1124 z.high |= (((uint64_t)a.sign) << 63) | UINT64_C(0x7FFF000000000000);
1128 /*----------------------------------------------------------------------------
1129 | Takes two quadruple-precision floating-point values `a' and `b', one of
1130 | which is a NaN, and returns the appropriate NaN result. If either `a' or
1131 | `b' is a signaling NaN, the invalid exception is raised.
1132 *----------------------------------------------------------------------------*/
1134 static float128 propagateFloat128NaN(float128 a, float128 b,
1135 float_status *status)
1137 bool aIsLargerSignificand;
1138 FloatClass a_cls, b_cls;
1140 /* This is not complete, but is good enough for pickNaN. */
1141 a_cls = (!float128_is_any_nan(a)
1142 ? float_class_normal
1143 : float128_is_signaling_nan(a, status)
1145 : float_class_qnan);
1146 b_cls = (!float128_is_any_nan(b)
1147 ? float_class_normal
1148 : float128_is_signaling_nan(b, status)
1150 : float_class_qnan);
1152 if (is_snan(a_cls) || is_snan(b_cls)) {
1153 float_raise(float_flag_invalid, status);
1156 if (status->default_nan_mode) {
1157 return float128_default_nan(status);
1160 if (lt128(a.high << 1, a.low, b.high << 1, b.low)) {
1161 aIsLargerSignificand = 0;
1162 } else if (lt128(b.high << 1, b.low, a.high << 1, a.low)) {
1163 aIsLargerSignificand = 1;
1165 aIsLargerSignificand = (a.high < b.high) ? 1 : 0;
1168 if (pickNaN(a_cls, b_cls, aIsLargerSignificand, status)) {
1169 if (is_snan(b_cls)) {
1170 return float128_silence_nan(b, status);
1174 if (is_snan(a_cls)) {
1175 return float128_silence_nan(a, status);