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 | Internal canonical NaN format.
232 *----------------------------------------------------------------------------*/
238 /*----------------------------------------------------------------------------
239 | Returns 1 if the half-precision floating-point value `a' is a quiet
240 | NaN; otherwise returns 0.
241 *----------------------------------------------------------------------------*/
243 bool float16_is_quiet_nan(float16 a_, float_status *status)
245 if (no_signaling_nans(status)) {
246 return float16_is_any_nan(a_);
248 uint16_t a = float16_val(a_);
249 if (snan_bit_is_one(status)) {
250 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF);
253 return ((a >> 9) & 0x3F) == 0x3F;
258 /*----------------------------------------------------------------------------
259 | Returns 1 if the bfloat16 value `a' is a quiet
260 | NaN; otherwise returns 0.
261 *----------------------------------------------------------------------------*/
263 bool bfloat16_is_quiet_nan(bfloat16 a_, float_status *status)
265 if (no_signaling_nans(status)) {
266 return bfloat16_is_any_nan(a_);
269 if (snan_bit_is_one(status)) {
270 return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F);
272 return ((a >> 6) & 0x1FF) == 0x1FF;
277 /*----------------------------------------------------------------------------
278 | Returns 1 if the half-precision floating-point value `a' is a signaling
279 | NaN; otherwise returns 0.
280 *----------------------------------------------------------------------------*/
282 bool float16_is_signaling_nan(float16 a_, float_status *status)
284 if (no_signaling_nans(status)) {
287 uint16_t a = float16_val(a_);
288 if (snan_bit_is_one(status)) {
289 return ((a >> 9) & 0x3F) == 0x3F;
291 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF);
296 /*----------------------------------------------------------------------------
297 | Returns 1 if the bfloat16 value `a' is a signaling
298 | NaN; otherwise returns 0.
299 *----------------------------------------------------------------------------*/
301 bool bfloat16_is_signaling_nan(bfloat16 a_, float_status *status)
303 if (no_signaling_nans(status)) {
307 if (snan_bit_is_one(status)) {
308 return ((a >> 6) & 0x1FF) == 0x1FF;
310 return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F);
315 /*----------------------------------------------------------------------------
316 | Returns 1 if the single-precision floating-point value `a' is a quiet
317 | NaN; otherwise returns 0.
318 *----------------------------------------------------------------------------*/
320 bool float32_is_quiet_nan(float32 a_, float_status *status)
322 if (no_signaling_nans(status)) {
323 return float32_is_any_nan(a_);
325 uint32_t a = float32_val(a_);
326 if (snan_bit_is_one(status)) {
327 return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF);
329 return ((uint32_t)(a << 1) >= 0xFF800000);
334 /*----------------------------------------------------------------------------
335 | Returns 1 if the single-precision floating-point value `a' is a signaling
336 | NaN; otherwise returns 0.
337 *----------------------------------------------------------------------------*/
339 bool float32_is_signaling_nan(float32 a_, float_status *status)
341 if (no_signaling_nans(status)) {
344 uint32_t a = float32_val(a_);
345 if (snan_bit_is_one(status)) {
346 return ((uint32_t)(a << 1) >= 0xFF800000);
348 return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF);
353 /*----------------------------------------------------------------------------
354 | Returns the result of converting the single-precision floating-point NaN
355 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
356 | exception is raised.
357 *----------------------------------------------------------------------------*/
359 static commonNaNT float32ToCommonNaN(float32 a, float_status *status)
363 if (float32_is_signaling_nan(a, status)) {
364 float_raise(float_flag_invalid, status);
366 z.sign = float32_val(a) >> 31;
368 z.high = ((uint64_t)float32_val(a)) << 41;
372 /*----------------------------------------------------------------------------
373 | Returns the result of converting the canonical NaN `a' to the single-
374 | precision floating-point format.
375 *----------------------------------------------------------------------------*/
377 static float32 commonNaNToFloat32(commonNaNT a, float_status *status)
379 uint32_t mantissa = a.high >> 41;
381 if (status->default_nan_mode) {
382 return float32_default_nan(status);
387 (((uint32_t)a.sign) << 31) | 0x7F800000 | (a.high >> 41));
389 return float32_default_nan(status);
393 /*----------------------------------------------------------------------------
394 | Select which NaN to propagate for a two-input operation.
395 | IEEE754 doesn't specify all the details of this, so the
396 | algorithm is target-specific.
397 | The routine is passed various bits of information about the
398 | two NaNs and should return 0 to select NaN a and 1 for NaN b.
399 | Note that signalling NaNs are always squashed to quiet NaNs
400 | by the caller, by calling floatXX_silence_nan() before
403 | aIsLargerSignificand is only valid if both a and b are NaNs
404 | of some kind, and is true if a has the larger significand,
405 | or if both a and b have the same significand but a is
406 | positive but b is negative. It is only needed for the x87
408 *----------------------------------------------------------------------------*/
410 static int pickNaN(FloatClass a_cls, FloatClass b_cls,
411 bool aIsLargerSignificand, float_status *status)
413 #if defined(TARGET_ARM) || defined(TARGET_MIPS) || defined(TARGET_HPPA)
414 /* ARM mandated NaN propagation rules (see FPProcessNaNs()), take
416 * 1. A if it is signaling
417 * 2. B if it is signaling
420 * A signaling NaN is always quietened before returning it.
422 /* According to MIPS specifications, if one of the two operands is
423 * a sNaN, a new qNaN has to be generated. This is done in
424 * floatXX_silence_nan(). For qNaN inputs the specifications
425 * says: "When possible, this QNaN result is one of the operand QNaN
426 * values." In practice it seems that most implementations choose
427 * the first operand if both operands are qNaN. In short this gives
428 * the following rules:
429 * 1. A if it is signaling
430 * 2. B if it is signaling
433 * A signaling NaN is always silenced before returning it.
435 if (is_snan(a_cls)) {
437 } else if (is_snan(b_cls)) {
439 } else if (is_qnan(a_cls)) {
444 #elif defined(TARGET_PPC) || defined(TARGET_M68K)
445 /* PowerPC propagation rules:
446 * 1. A if it sNaN or qNaN
447 * 2. B if it sNaN or qNaN
448 * A signaling NaN is always silenced before returning it.
450 /* M68000 FAMILY PROGRAMMER'S REFERENCE MANUAL
451 * 3.4 FLOATING-POINT INSTRUCTION DETAILS
452 * If either operand, but not both operands, of an operation is a
453 * nonsignaling NaN, then that NaN is returned as the result. If both
454 * operands are nonsignaling NaNs, then the destination operand
455 * nonsignaling NaN is returned as the result.
456 * If either operand to an operation is a signaling NaN (SNaN), then the
457 * SNaN bit is set in the FPSR EXC byte. If the SNaN exception enable bit
458 * is set in the FPCR ENABLE byte, then the exception is taken and the
459 * destination is not modified. If the SNaN exception enable bit is not
460 * set, setting the SNaN bit in the operand to a one converts the SNaN to
461 * a nonsignaling NaN. The operation then continues as described in the
462 * preceding paragraph for nonsignaling NaNs.
469 #elif defined(TARGET_XTENSA)
471 * Xtensa has two NaN propagation modes.
472 * Which one is active is controlled by float_status::use_first_nan.
474 if (status->use_first_nan) {
488 /* This implements x87 NaN propagation rules:
489 * SNaN + QNaN => return the QNaN
490 * two SNaNs => return the one with the larger significand, silenced
491 * two QNaNs => return the one with the larger significand
492 * SNaN and a non-NaN => return the SNaN, silenced
493 * QNaN and a non-NaN => return the QNaN
495 * If we get down to comparing significands and they are the same,
496 * return the NaN with the positive sign bit (if any).
498 if (is_snan(a_cls)) {
499 if (is_snan(b_cls)) {
500 return aIsLargerSignificand ? 0 : 1;
502 return is_qnan(b_cls) ? 1 : 0;
503 } else if (is_qnan(a_cls)) {
504 if (is_snan(b_cls) || !is_qnan(b_cls)) {
507 return aIsLargerSignificand ? 0 : 1;
515 /*----------------------------------------------------------------------------
516 | Select which NaN to propagate for a three-input operation.
517 | For the moment we assume that no CPU needs the 'larger significand'
519 | Return values : 0 : a; 1 : b; 2 : c; 3 : default-NaN
520 *----------------------------------------------------------------------------*/
521 static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls,
522 bool infzero, float_status *status)
524 #if defined(TARGET_ARM)
525 /* For ARM, the (inf,zero,qnan) case sets InvalidOp and returns
528 if (infzero && is_qnan(c_cls)) {
529 float_raise(float_flag_invalid, status);
533 /* This looks different from the ARM ARM pseudocode, because the ARM ARM
534 * puts the operands to a fused mac operation (a*b)+c in the order c,a,b.
536 if (is_snan(c_cls)) {
538 } else if (is_snan(a_cls)) {
540 } else if (is_snan(b_cls)) {
542 } else if (is_qnan(c_cls)) {
544 } else if (is_qnan(a_cls)) {
549 #elif defined(TARGET_MIPS)
550 if (snan_bit_is_one(status)) {
552 * For MIPS systems that conform to IEEE754-1985, the (inf,zero,nan)
553 * case sets InvalidOp and returns the default NaN
556 float_raise(float_flag_invalid, status);
559 /* Prefer sNaN over qNaN, in the a, b, c order. */
560 if (is_snan(a_cls)) {
562 } else if (is_snan(b_cls)) {
564 } else if (is_snan(c_cls)) {
566 } else if (is_qnan(a_cls)) {
568 } else if (is_qnan(b_cls)) {
575 * For MIPS systems that conform to IEEE754-2008, the (inf,zero,nan)
576 * case sets InvalidOp and returns the input value 'c'
579 float_raise(float_flag_invalid, status);
582 /* Prefer sNaN over qNaN, in the c, a, b order. */
583 if (is_snan(c_cls)) {
585 } else if (is_snan(a_cls)) {
587 } else if (is_snan(b_cls)) {
589 } else if (is_qnan(c_cls)) {
591 } else if (is_qnan(a_cls)) {
597 #elif defined(TARGET_PPC)
598 /* For PPC, the (inf,zero,qnan) case sets InvalidOp, but we prefer
599 * to return an input NaN if we have one (ie c) rather than generating
603 float_raise(float_flag_invalid, status);
607 /* If fRA is a NaN return it; otherwise if fRB is a NaN return it;
608 * otherwise return fRC. Note that muladd on PPC is (fRA * fRC) + frB
612 } else if (is_nan(c_cls)) {
617 #elif defined(TARGET_RISCV)
618 /* For RISC-V, InvalidOp is set when multiplicands are Inf and zero */
620 float_raise(float_flag_invalid, status);
622 return 3; /* default NaN */
623 #elif defined(TARGET_XTENSA)
625 * For Xtensa, the (inf,zero,nan) case sets InvalidOp and returns
626 * an input NaN if we have one (ie c).
629 float_raise(float_flag_invalid, status);
632 if (status->use_first_nan) {
635 } else if (is_nan(b_cls)) {
643 } else if (is_nan(b_cls)) {
650 /* A default implementation: prefer a to b to c.
651 * This is unlikely to actually match any real implementation.
655 } else if (is_nan(b_cls)) {
663 /*----------------------------------------------------------------------------
664 | Takes two single-precision floating-point values `a' and `b', one of which
665 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
666 | signaling NaN, the invalid exception is raised.
667 *----------------------------------------------------------------------------*/
669 static float32 propagateFloat32NaN(float32 a, float32 b, float_status *status)
671 bool aIsLargerSignificand;
673 FloatClass a_cls, b_cls;
675 /* This is not complete, but is good enough for pickNaN. */
676 a_cls = (!float32_is_any_nan(a)
678 : float32_is_signaling_nan(a, status)
681 b_cls = (!float32_is_any_nan(b)
683 : float32_is_signaling_nan(b, status)
690 if (is_snan(a_cls) || is_snan(b_cls)) {
691 float_raise(float_flag_invalid, status);
694 if (status->default_nan_mode) {
695 return float32_default_nan(status);
698 if ((uint32_t)(av << 1) < (uint32_t)(bv << 1)) {
699 aIsLargerSignificand = 0;
700 } else if ((uint32_t)(bv << 1) < (uint32_t)(av << 1)) {
701 aIsLargerSignificand = 1;
703 aIsLargerSignificand = (av < bv) ? 1 : 0;
706 if (pickNaN(a_cls, b_cls, aIsLargerSignificand, status)) {
707 if (is_snan(b_cls)) {
708 return float32_silence_nan(b, status);
712 if (is_snan(a_cls)) {
713 return float32_silence_nan(a, status);
719 /*----------------------------------------------------------------------------
720 | Returns 1 if the double-precision floating-point value `a' is a quiet
721 | NaN; otherwise returns 0.
722 *----------------------------------------------------------------------------*/
724 bool float64_is_quiet_nan(float64 a_, float_status *status)
726 if (no_signaling_nans(status)) {
727 return float64_is_any_nan(a_);
729 uint64_t a = float64_val(a_);
730 if (snan_bit_is_one(status)) {
731 return (((a >> 51) & 0xFFF) == 0xFFE)
732 && (a & 0x0007FFFFFFFFFFFFULL);
734 return ((a << 1) >= 0xFFF0000000000000ULL);
739 /*----------------------------------------------------------------------------
740 | Returns 1 if the double-precision floating-point value `a' is a signaling
741 | NaN; otherwise returns 0.
742 *----------------------------------------------------------------------------*/
744 bool float64_is_signaling_nan(float64 a_, float_status *status)
746 if (no_signaling_nans(status)) {
749 uint64_t a = float64_val(a_);
750 if (snan_bit_is_one(status)) {
751 return ((a << 1) >= 0xFFF0000000000000ULL);
753 return (((a >> 51) & 0xFFF) == 0xFFE)
754 && (a & UINT64_C(0x0007FFFFFFFFFFFF));
759 /*----------------------------------------------------------------------------
760 | Returns the result of converting the double-precision floating-point NaN
761 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
762 | exception is raised.
763 *----------------------------------------------------------------------------*/
765 static commonNaNT float64ToCommonNaN(float64 a, float_status *status)
769 if (float64_is_signaling_nan(a, status)) {
770 float_raise(float_flag_invalid, status);
772 z.sign = float64_val(a) >> 63;
774 z.high = float64_val(a) << 12;
778 /*----------------------------------------------------------------------------
779 | Returns the result of converting the canonical NaN `a' to the double-
780 | precision floating-point format.
781 *----------------------------------------------------------------------------*/
783 static float64 commonNaNToFloat64(commonNaNT a, float_status *status)
785 uint64_t mantissa = a.high >> 12;
787 if (status->default_nan_mode) {
788 return float64_default_nan(status);
793 (((uint64_t) a.sign) << 63)
794 | UINT64_C(0x7FF0000000000000)
797 return float64_default_nan(status);
801 /*----------------------------------------------------------------------------
802 | Takes two double-precision floating-point values `a' and `b', one of which
803 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
804 | signaling NaN, the invalid exception is raised.
805 *----------------------------------------------------------------------------*/
807 static float64 propagateFloat64NaN(float64 a, float64 b, float_status *status)
809 bool aIsLargerSignificand;
811 FloatClass a_cls, b_cls;
813 /* This is not complete, but is good enough for pickNaN. */
814 a_cls = (!float64_is_any_nan(a)
816 : float64_is_signaling_nan(a, status)
819 b_cls = (!float64_is_any_nan(b)
821 : float64_is_signaling_nan(b, status)
828 if (is_snan(a_cls) || is_snan(b_cls)) {
829 float_raise(float_flag_invalid, status);
832 if (status->default_nan_mode) {
833 return float64_default_nan(status);
836 if ((uint64_t)(av << 1) < (uint64_t)(bv << 1)) {
837 aIsLargerSignificand = 0;
838 } else if ((uint64_t)(bv << 1) < (uint64_t)(av << 1)) {
839 aIsLargerSignificand = 1;
841 aIsLargerSignificand = (av < bv) ? 1 : 0;
844 if (pickNaN(a_cls, b_cls, aIsLargerSignificand, status)) {
845 if (is_snan(b_cls)) {
846 return float64_silence_nan(b, status);
850 if (is_snan(a_cls)) {
851 return float64_silence_nan(a, status);
857 /*----------------------------------------------------------------------------
858 | Returns 1 if the extended double-precision floating-point value `a' is a
859 | quiet NaN; otherwise returns 0. This slightly differs from the same
860 | function for other types as floatx80 has an explicit bit.
861 *----------------------------------------------------------------------------*/
863 int floatx80_is_quiet_nan(floatx80 a, float_status *status)
865 if (no_signaling_nans(status)) {
866 return floatx80_is_any_nan(a);
868 if (snan_bit_is_one(status)) {
871 aLow = a.low & ~0x4000000000000000ULL;
872 return ((a.high & 0x7FFF) == 0x7FFF)
876 return ((a.high & 0x7FFF) == 0x7FFF)
877 && (UINT64_C(0x8000000000000000) <= ((uint64_t)(a.low << 1)));
882 /*----------------------------------------------------------------------------
883 | Returns 1 if the extended double-precision floating-point value `a' is a
884 | signaling NaN; otherwise returns 0. This slightly differs from the same
885 | function for other types as floatx80 has an explicit bit.
886 *----------------------------------------------------------------------------*/
888 int floatx80_is_signaling_nan(floatx80 a, float_status *status)
890 if (no_signaling_nans(status)) {
893 if (snan_bit_is_one(status)) {
894 return ((a.high & 0x7FFF) == 0x7FFF)
895 && ((a.low << 1) >= 0x8000000000000000ULL);
899 aLow = a.low & ~UINT64_C(0x4000000000000000);
900 return ((a.high & 0x7FFF) == 0x7FFF)
901 && (uint64_t)(aLow << 1)
907 /*----------------------------------------------------------------------------
908 | Returns a quiet NaN from a signalling NaN for the extended double-precision
909 | floating point value `a'.
910 *----------------------------------------------------------------------------*/
912 floatx80 floatx80_silence_nan(floatx80 a, float_status *status)
914 /* None of the targets that have snan_bit_is_one use floatx80. */
915 assert(!snan_bit_is_one(status));
916 a.low |= UINT64_C(0xC000000000000000);
920 /*----------------------------------------------------------------------------
921 | Returns the result of converting the extended double-precision floating-
922 | point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
923 | invalid exception is raised.
924 *----------------------------------------------------------------------------*/
926 static commonNaNT floatx80ToCommonNaN(floatx80 a, float_status *status)
931 if (floatx80_is_signaling_nan(a, status)) {
932 float_raise(float_flag_invalid, status);
935 z.sign = a.high >> 15;
939 dflt = floatx80_default_nan(status);
940 z.sign = dflt.high >> 15;
942 z.high = dflt.low << 1;
947 /*----------------------------------------------------------------------------
948 | Returns the result of converting the canonical NaN `a' to the extended
949 | double-precision floating-point format.
950 *----------------------------------------------------------------------------*/
952 static floatx80 commonNaNToFloatx80(commonNaNT a, float_status *status)
956 if (status->default_nan_mode) {
957 return floatx80_default_nan(status);
961 z.low = UINT64_C(0x8000000000000000) | a.high >> 1;
962 z.high = (((uint16_t)a.sign) << 15) | 0x7FFF;
964 z = floatx80_default_nan(status);
969 /*----------------------------------------------------------------------------
970 | Takes two extended double-precision floating-point values `a' and `b', one
971 | of which is a NaN, and returns the appropriate NaN result. If either `a' or
972 | `b' is a signaling NaN, the invalid exception is raised.
973 *----------------------------------------------------------------------------*/
975 floatx80 propagateFloatx80NaN(floatx80 a, floatx80 b, float_status *status)
977 bool aIsLargerSignificand;
978 FloatClass a_cls, b_cls;
980 /* This is not complete, but is good enough for pickNaN. */
981 a_cls = (!floatx80_is_any_nan(a)
983 : floatx80_is_signaling_nan(a, status)
986 b_cls = (!floatx80_is_any_nan(b)
988 : floatx80_is_signaling_nan(b, status)
992 if (is_snan(a_cls) || is_snan(b_cls)) {
993 float_raise(float_flag_invalid, status);
996 if (status->default_nan_mode) {
997 return floatx80_default_nan(status);
1000 if (a.low < b.low) {
1001 aIsLargerSignificand = 0;
1002 } else if (b.low < a.low) {
1003 aIsLargerSignificand = 1;
1005 aIsLargerSignificand = (a.high < b.high) ? 1 : 0;
1008 if (pickNaN(a_cls, b_cls, aIsLargerSignificand, status)) {
1009 if (is_snan(b_cls)) {
1010 return floatx80_silence_nan(b, status);
1014 if (is_snan(a_cls)) {
1015 return floatx80_silence_nan(a, status);
1021 /*----------------------------------------------------------------------------
1022 | Returns 1 if the quadruple-precision floating-point value `a' is a quiet
1023 | NaN; otherwise returns 0.
1024 *----------------------------------------------------------------------------*/
1026 bool float128_is_quiet_nan(float128 a, float_status *status)
1028 if (no_signaling_nans(status)) {
1029 return float128_is_any_nan(a);
1031 if (snan_bit_is_one(status)) {
1032 return (((a.high >> 47) & 0xFFFF) == 0xFFFE)
1033 && (a.low || (a.high & 0x00007FFFFFFFFFFFULL));
1035 return ((a.high << 1) >= 0xFFFF000000000000ULL)
1036 && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL));
1041 /*----------------------------------------------------------------------------
1042 | Returns 1 if the quadruple-precision floating-point value `a' is a
1043 | signaling NaN; otherwise returns 0.
1044 *----------------------------------------------------------------------------*/
1046 bool float128_is_signaling_nan(float128 a, float_status *status)
1048 if (no_signaling_nans(status)) {
1051 if (snan_bit_is_one(status)) {
1052 return ((a.high << 1) >= 0xFFFF000000000000ULL)
1053 && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL));
1055 return (((a.high >> 47) & 0xFFFF) == 0xFFFE)
1056 && (a.low || (a.high & UINT64_C(0x00007FFFFFFFFFFF)));
1061 /*----------------------------------------------------------------------------
1062 | Returns a quiet NaN from a signalling NaN for the quadruple-precision
1063 | floating point value `a'.
1064 *----------------------------------------------------------------------------*/
1066 float128 float128_silence_nan(float128 a, float_status *status)
1068 if (no_signaling_nans(status)) {
1069 g_assert_not_reached();
1071 if (snan_bit_is_one(status)) {
1072 return float128_default_nan(status);
1074 a.high |= UINT64_C(0x0000800000000000);
1080 /*----------------------------------------------------------------------------
1081 | Returns the result of converting the quadruple-precision floating-point NaN
1082 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
1083 | exception is raised.
1084 *----------------------------------------------------------------------------*/
1086 static commonNaNT float128ToCommonNaN(float128 a, float_status *status)
1090 if (float128_is_signaling_nan(a, status)) {
1091 float_raise(float_flag_invalid, status);
1093 z.sign = a.high >> 63;
1094 shortShift128Left(a.high, a.low, 16, &z.high, &z.low);
1098 /*----------------------------------------------------------------------------
1099 | Returns the result of converting the canonical NaN `a' to the quadruple-
1100 | precision floating-point format.
1101 *----------------------------------------------------------------------------*/
1103 static float128 commonNaNToFloat128(commonNaNT a, float_status *status)
1107 if (status->default_nan_mode) {
1108 return float128_default_nan(status);
1111 shift128Right(a.high, a.low, 16, &z.high, &z.low);
1112 z.high |= (((uint64_t)a.sign) << 63) | UINT64_C(0x7FFF000000000000);
1116 /*----------------------------------------------------------------------------
1117 | Takes two quadruple-precision floating-point values `a' and `b', one of
1118 | which is a NaN, and returns the appropriate NaN result. If either `a' or
1119 | `b' is a signaling NaN, the invalid exception is raised.
1120 *----------------------------------------------------------------------------*/
1122 static float128 propagateFloat128NaN(float128 a, float128 b,
1123 float_status *status)
1125 bool aIsLargerSignificand;
1126 FloatClass a_cls, b_cls;
1128 /* This is not complete, but is good enough for pickNaN. */
1129 a_cls = (!float128_is_any_nan(a)
1130 ? float_class_normal
1131 : float128_is_signaling_nan(a, status)
1133 : float_class_qnan);
1134 b_cls = (!float128_is_any_nan(b)
1135 ? float_class_normal
1136 : float128_is_signaling_nan(b, status)
1138 : float_class_qnan);
1140 if (is_snan(a_cls) || is_snan(b_cls)) {
1141 float_raise(float_flag_invalid, status);
1144 if (status->default_nan_mode) {
1145 return float128_default_nan(status);
1148 if (lt128(a.high << 1, a.low, b.high << 1, b.low)) {
1149 aIsLargerSignificand = 0;
1150 } else if (lt128(b.high << 1, b.low, a.high << 1, a.low)) {
1151 aIsLargerSignificand = 1;
1153 aIsLargerSignificand = (a.high < b.high) ? 1 : 0;
1156 if (pickNaN(a_cls, b_cls, aIsLargerSignificand, status)) {
1157 if (is_snan(b_cls)) {
1158 return float128_silence_nan(b, status);
1162 if (is_snan(a_cls)) {
1163 return float128_silence_nan(a, status);