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 | For the deconstructed floating-point with fraction FRAC, return true
91 | if the fraction represents a signalling NaN; otherwise false.
92 *----------------------------------------------------------------------------*/
94 static bool parts_is_snan_frac(uint64_t frac
, float_status
*status
)
96 #ifdef NO_SIGNALING_NANS
99 flag msb
= extract64(frac
, DECOMPOSED_BINARY_POINT
- 1, 1);
100 return msb
== status
->snan_bit_is_one
;
104 /*----------------------------------------------------------------------------
105 | The pattern for a default generated deconstructed floating-point NaN.
106 *----------------------------------------------------------------------------*/
108 static FloatParts
parts_default_nan(float_status
*status
)
113 #if defined(TARGET_SPARC) || defined(TARGET_M68K)
114 frac
= (1ULL << DECOMPOSED_BINARY_POINT
) - 1;
115 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA) || \
116 defined(TARGET_S390X) || defined(TARGET_RISCV)
117 frac
= 1ULL << (DECOMPOSED_BINARY_POINT
- 1);
118 #elif defined(TARGET_HPPA)
119 frac
= 1ULL << (DECOMPOSED_BINARY_POINT
- 2);
121 if (status
->snan_bit_is_one
) {
122 frac
= (1ULL << (DECOMPOSED_BINARY_POINT
- 1)) - 1;
124 #if defined(TARGET_MIPS)
125 frac
= 1ULL << (DECOMPOSED_BINARY_POINT
- 1);
127 frac
= 1ULL << (DECOMPOSED_BINARY_POINT
- 1);
133 return (FloatParts
) {
134 .cls
= float_class_qnan
,
141 /*----------------------------------------------------------------------------
142 | Returns a quiet NaN from a signalling NaN for the deconstructed
143 | floating-point parts.
144 *----------------------------------------------------------------------------*/
146 static FloatParts
parts_silence_nan(FloatParts a
, float_status
*status
)
148 #ifdef NO_SIGNALING_NANS
149 g_assert_not_reached();
150 #elif defined(TARGET_HPPA)
151 a
.frac
&= ~(1ULL << (DECOMPOSED_BINARY_POINT
- 1));
152 a
.frac
|= 1ULL << (DECOMPOSED_BINARY_POINT
- 2);
154 if (status
->snan_bit_is_one
) {
155 return parts_default_nan(status
);
157 a
.frac
|= 1ULL << (DECOMPOSED_BINARY_POINT
- 1);
160 a
.cls
= float_class_qnan
;
164 /*----------------------------------------------------------------------------
165 | The pattern for a default generated half-precision NaN.
166 *----------------------------------------------------------------------------*/
167 float16
float16_default_nan(float_status
*status
)
169 #if defined(TARGET_ARM)
170 return const_float16(0x7E00);
172 if (status
->snan_bit_is_one
) {
173 return const_float16(0x7DFF);
175 #if defined(TARGET_MIPS)
176 return const_float16(0x7E00);
178 return const_float16(0xFE00);
184 /*----------------------------------------------------------------------------
185 | The pattern for a default generated single-precision NaN.
186 *----------------------------------------------------------------------------*/
187 float32
float32_default_nan(float_status
*status
)
189 #if defined(TARGET_SPARC) || defined(TARGET_M68K)
190 return const_float32(0x7FFFFFFF);
191 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA) || \
192 defined(TARGET_XTENSA) || defined(TARGET_S390X) || \
193 defined(TARGET_TRICORE) || defined(TARGET_RISCV)
194 return const_float32(0x7FC00000);
195 #elif defined(TARGET_HPPA)
196 return const_float32(0x7FA00000);
198 if (status
->snan_bit_is_one
) {
199 return const_float32(0x7FBFFFFF);
201 #if defined(TARGET_MIPS)
202 return const_float32(0x7FC00000);
204 return const_float32(0xFFC00000);
210 /*----------------------------------------------------------------------------
211 | The pattern for a default generated double-precision NaN.
212 *----------------------------------------------------------------------------*/
213 float64
float64_default_nan(float_status
*status
)
215 #if defined(TARGET_SPARC) || defined(TARGET_M68K)
216 return const_float64(LIT64(0x7FFFFFFFFFFFFFFF));
217 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA) || \
218 defined(TARGET_S390X) || defined(TARGET_RISCV)
219 return const_float64(LIT64(0x7FF8000000000000));
220 #elif defined(TARGET_HPPA)
221 return const_float64(LIT64(0x7FF4000000000000));
223 if (status
->snan_bit_is_one
) {
224 return const_float64(LIT64(0x7FF7FFFFFFFFFFFF));
226 #if defined(TARGET_MIPS)
227 return const_float64(LIT64(0x7FF8000000000000));
229 return const_float64(LIT64(0xFFF8000000000000));
235 /*----------------------------------------------------------------------------
236 | The pattern for a default generated extended double-precision NaN.
237 *----------------------------------------------------------------------------*/
238 floatx80
floatx80_default_nan(float_status
*status
)
241 #if defined(TARGET_M68K)
242 r
.low
= LIT64(0xFFFFFFFFFFFFFFFF);
245 if (status
->snan_bit_is_one
) {
246 r
.low
= LIT64(0xBFFFFFFFFFFFFFFF);
249 r
.low
= LIT64(0xC000000000000000);
256 /*----------------------------------------------------------------------------
257 | The pattern for a default generated extended double-precision inf.
258 *----------------------------------------------------------------------------*/
260 #define floatx80_infinity_high 0x7FFF
261 #if defined(TARGET_M68K)
262 #define floatx80_infinity_low LIT64(0x0000000000000000)
264 #define floatx80_infinity_low LIT64(0x8000000000000000)
267 const floatx80 floatx80_infinity
268 = make_floatx80_init(floatx80_infinity_high
, floatx80_infinity_low
);
270 /*----------------------------------------------------------------------------
271 | The pattern for a default generated quadruple-precision NaN.
272 *----------------------------------------------------------------------------*/
273 float128
float128_default_nan(float_status
*status
)
277 if (status
->snan_bit_is_one
) {
278 r
.low
= LIT64(0xFFFFFFFFFFFFFFFF);
279 r
.high
= LIT64(0x7FFF7FFFFFFFFFFF);
281 r
.low
= LIT64(0x0000000000000000);
282 #if defined(TARGET_S390X) || defined(TARGET_PPC) || defined(TARGET_RISCV)
283 r
.high
= LIT64(0x7FFF800000000000);
285 r
.high
= LIT64(0xFFFF800000000000);
291 /*----------------------------------------------------------------------------
292 | Raises the exceptions specified by `flags'. Floating-point traps can be
293 | defined here if desired. It is currently not possible for such a trap
294 | to substitute a result value. If traps are not implemented, this routine
295 | should be simply `float_exception_flags |= flags;'.
296 *----------------------------------------------------------------------------*/
298 void float_raise(uint8_t flags
, float_status
*status
)
300 status
->float_exception_flags
|= flags
;
303 /*----------------------------------------------------------------------------
304 | Internal canonical NaN format.
305 *----------------------------------------------------------------------------*/
311 /*----------------------------------------------------------------------------
312 | Returns 1 if the half-precision floating-point value `a' is a quiet
313 | NaN; otherwise returns 0.
314 *----------------------------------------------------------------------------*/
316 int float16_is_quiet_nan(float16 a_
, float_status
*status
)
318 #ifdef NO_SIGNALING_NANS
319 return float16_is_any_nan(a_
);
321 uint16_t a
= float16_val(a_
);
322 if (status
->snan_bit_is_one
) {
323 return (((a
>> 9) & 0x3F) == 0x3E) && (a
& 0x1FF);
325 return ((a
& ~0x8000) >= 0x7C80);
330 /*----------------------------------------------------------------------------
331 | Returns 1 if the half-precision floating-point value `a' is a signaling
332 | NaN; otherwise returns 0.
333 *----------------------------------------------------------------------------*/
335 int float16_is_signaling_nan(float16 a_
, float_status
*status
)
337 #ifdef NO_SIGNALING_NANS
340 uint16_t a
= float16_val(a_
);
341 if (status
->snan_bit_is_one
) {
342 return ((a
& ~0x8000) >= 0x7C80);
344 return (((a
>> 9) & 0x3F) == 0x3E) && (a
& 0x1FF);
349 /*----------------------------------------------------------------------------
350 | Returns a quiet NaN from a signalling NaN for the half-precision
351 | floating point value `a'.
352 *----------------------------------------------------------------------------*/
354 float16
float16_silence_nan(float16 a
, float_status
*status
)
356 #ifdef NO_SIGNALING_NANS
357 g_assert_not_reached();
359 if (status
->snan_bit_is_one
) {
360 return float16_default_nan(status
);
367 /*----------------------------------------------------------------------------
368 | Returns a quiet NaN if the half-precision floating point value `a' is a
369 | signaling NaN; otherwise returns `a'.
370 *----------------------------------------------------------------------------*/
372 float16
float16_maybe_silence_nan(float16 a
, float_status
*status
)
374 if (float16_is_signaling_nan(a
, status
)) {
375 return float16_silence_nan(a
, status
);
380 /*----------------------------------------------------------------------------
381 | Returns 1 if the single-precision floating-point value `a' is a quiet
382 | NaN; otherwise returns 0.
383 *----------------------------------------------------------------------------*/
385 int float32_is_quiet_nan(float32 a_
, float_status
*status
)
387 #ifdef NO_SIGNALING_NANS
388 return float32_is_any_nan(a_
);
390 uint32_t a
= float32_val(a_
);
391 if (status
->snan_bit_is_one
) {
392 return (((a
>> 22) & 0x1FF) == 0x1FE) && (a
& 0x003FFFFF);
394 return ((uint32_t)(a
<< 1) >= 0xFF800000);
399 /*----------------------------------------------------------------------------
400 | Returns 1 if the single-precision floating-point value `a' is a signaling
401 | NaN; otherwise returns 0.
402 *----------------------------------------------------------------------------*/
404 int float32_is_signaling_nan(float32 a_
, float_status
*status
)
406 #ifdef NO_SIGNALING_NANS
409 uint32_t a
= float32_val(a_
);
410 if (status
->snan_bit_is_one
) {
411 return ((uint32_t)(a
<< 1) >= 0xFF800000);
413 return (((a
>> 22) & 0x1FF) == 0x1FE) && (a
& 0x003FFFFF);
418 /*----------------------------------------------------------------------------
419 | Returns a quiet NaN from a signalling NaN for the single-precision
420 | floating point value `a'.
421 *----------------------------------------------------------------------------*/
423 float32
float32_silence_nan(float32 a
, float_status
*status
)
425 #ifdef NO_SIGNALING_NANS
426 g_assert_not_reached();
428 if (status
->snan_bit_is_one
) {
434 return float32_default_nan(status
);
437 return a
| (1 << 22);
441 /*----------------------------------------------------------------------------
442 | Returns a quiet NaN if the single-precision floating point value `a' is a
443 | signaling NaN; otherwise returns `a'.
444 *----------------------------------------------------------------------------*/
446 float32
float32_maybe_silence_nan(float32 a
, float_status
*status
)
448 if (float32_is_signaling_nan(a
, status
)) {
449 return float32_silence_nan(a
, status
);
454 /*----------------------------------------------------------------------------
455 | Returns the result of converting the single-precision floating-point NaN
456 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
457 | exception is raised.
458 *----------------------------------------------------------------------------*/
460 static commonNaNT
float32ToCommonNaN(float32 a
, float_status
*status
)
464 if (float32_is_signaling_nan(a
, status
)) {
465 float_raise(float_flag_invalid
, status
);
467 z
.sign
= float32_val(a
) >> 31;
469 z
.high
= ((uint64_t)float32_val(a
)) << 41;
473 /*----------------------------------------------------------------------------
474 | Returns the result of converting the canonical NaN `a' to the single-
475 | precision floating-point format.
476 *----------------------------------------------------------------------------*/
478 static float32
commonNaNToFloat32(commonNaNT a
, float_status
*status
)
480 uint32_t mantissa
= a
.high
>> 41;
482 if (status
->default_nan_mode
) {
483 return float32_default_nan(status
);
488 (((uint32_t)a
.sign
) << 31) | 0x7F800000 | (a
.high
>> 41));
490 return float32_default_nan(status
);
494 /*----------------------------------------------------------------------------
495 | Select which NaN to propagate for a two-input operation.
496 | IEEE754 doesn't specify all the details of this, so the
497 | algorithm is target-specific.
498 | The routine is passed various bits of information about the
499 | two NaNs and should return 0 to select NaN a and 1 for NaN b.
500 | Note that signalling NaNs are always squashed to quiet NaNs
501 | by the caller, by calling floatXX_maybe_silence_nan() before
504 | aIsLargerSignificand is only valid if both a and b are NaNs
505 | of some kind, and is true if a has the larger significand,
506 | or if both a and b have the same significand but a is
507 | positive but b is negative. It is only needed for the x87
509 *----------------------------------------------------------------------------*/
511 #if defined(TARGET_ARM)
512 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
513 flag aIsLargerSignificand
)
515 /* ARM mandated NaN propagation rules (see FPProcessNaNs()), take
517 * 1. A if it is signaling
518 * 2. B if it is signaling
521 * A signaling NaN is always quietened before returning it.
525 } else if (bIsSNaN
) {
527 } else if (aIsQNaN
) {
533 #elif defined(TARGET_MIPS) || defined(TARGET_HPPA)
534 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
535 flag aIsLargerSignificand
)
537 /* According to MIPS specifications, if one of the two operands is
538 * a sNaN, a new qNaN has to be generated. This is done in
539 * floatXX_maybe_silence_nan(). For qNaN inputs the specifications
540 * says: "When possible, this QNaN result is one of the operand QNaN
541 * values." In practice it seems that most implementations choose
542 * the first operand if both operands are qNaN. In short this gives
543 * the following rules:
544 * 1. A if it is signaling
545 * 2. B if it is signaling
548 * A signaling NaN is always silenced before returning it.
552 } else if (bIsSNaN
) {
554 } else if (aIsQNaN
) {
560 #elif defined(TARGET_PPC) || defined(TARGET_XTENSA)
561 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
562 flag aIsLargerSignificand
)
564 /* PowerPC propagation rules:
565 * 1. A if it sNaN or qNaN
566 * 2. B if it sNaN or qNaN
567 * A signaling NaN is always silenced before returning it.
569 if (aIsSNaN
|| aIsQNaN
) {
575 #elif defined(TARGET_M68K)
576 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
577 flag aIsLargerSignificand
)
579 /* M68000 FAMILY PROGRAMMER'S REFERENCE MANUAL
580 * 3.4 FLOATING-POINT INSTRUCTION DETAILS
581 * If either operand, but not both operands, of an operation is a
582 * nonsignaling NaN, then that NaN is returned as the result. If both
583 * operands are nonsignaling NaNs, then the destination operand
584 * nonsignaling NaN is returned as the result.
585 * If either operand to an operation is a signaling NaN (SNaN), then the
586 * SNaN bit is set in the FPSR EXC byte. If the SNaN exception enable bit
587 * is set in the FPCR ENABLE byte, then the exception is taken and the
588 * destination is not modified. If the SNaN exception enable bit is not
589 * set, setting the SNaN bit in the operand to a one converts the SNaN to
590 * a nonsignaling NaN. The operation then continues as described in the
591 * preceding paragraph for nonsignaling NaNs.
593 if (aIsQNaN
|| aIsSNaN
) { /* a is the destination operand */
594 return 0; /* return the destination operand */
596 return 1; /* return b */
600 static int pickNaN(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
601 flag aIsLargerSignificand
)
603 /* This implements x87 NaN propagation rules:
604 * SNaN + QNaN => return the QNaN
605 * two SNaNs => return the one with the larger significand, silenced
606 * two QNaNs => return the one with the larger significand
607 * SNaN and a non-NaN => return the SNaN, silenced
608 * QNaN and a non-NaN => return the QNaN
610 * If we get down to comparing significands and they are the same,
611 * return the NaN with the positive sign bit (if any).
615 return aIsLargerSignificand
? 0 : 1;
617 return bIsQNaN
? 1 : 0;
618 } else if (aIsQNaN
) {
619 if (bIsSNaN
|| !bIsQNaN
) {
622 return aIsLargerSignificand
? 0 : 1;
630 /*----------------------------------------------------------------------------
631 | Select which NaN to propagate for a three-input operation.
632 | For the moment we assume that no CPU needs the 'larger significand'
634 | Return values : 0 : a; 1 : b; 2 : c; 3 : default-NaN
635 *----------------------------------------------------------------------------*/
636 #if defined(TARGET_ARM)
637 static int pickNaNMulAdd(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
638 flag cIsQNaN
, flag cIsSNaN
, flag infzero
,
639 float_status
*status
)
641 /* For ARM, the (inf,zero,qnan) case sets InvalidOp and returns
644 if (infzero
&& cIsQNaN
) {
645 float_raise(float_flag_invalid
, status
);
649 /* This looks different from the ARM ARM pseudocode, because the ARM ARM
650 * puts the operands to a fused mac operation (a*b)+c in the order c,a,b.
654 } else if (aIsSNaN
) {
656 } else if (bIsSNaN
) {
658 } else if (cIsQNaN
) {
660 } else if (aIsQNaN
) {
666 #elif defined(TARGET_MIPS)
667 static int pickNaNMulAdd(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
668 flag cIsQNaN
, flag cIsSNaN
, flag infzero
,
669 float_status
*status
)
671 /* For MIPS, the (inf,zero,qnan) case sets InvalidOp and returns
675 float_raise(float_flag_invalid
, status
);
679 if (status
->snan_bit_is_one
) {
680 /* Prefer sNaN over qNaN, in the a, b, c order. */
683 } else if (bIsSNaN
) {
685 } else if (cIsSNaN
) {
687 } else if (aIsQNaN
) {
689 } else if (bIsQNaN
) {
695 /* Prefer sNaN over qNaN, in the c, a, b order. */
698 } else if (aIsSNaN
) {
700 } else if (bIsSNaN
) {
702 } else if (cIsQNaN
) {
704 } else if (aIsQNaN
) {
711 #elif defined(TARGET_PPC)
712 static int pickNaNMulAdd(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
713 flag cIsQNaN
, flag cIsSNaN
, flag infzero
,
714 float_status
*status
)
716 /* For PPC, the (inf,zero,qnan) case sets InvalidOp, but we prefer
717 * to return an input NaN if we have one (ie c) rather than generating
721 float_raise(float_flag_invalid
, status
);
725 /* If fRA is a NaN return it; otherwise if fRB is a NaN return it;
726 * otherwise return fRC. Note that muladd on PPC is (fRA * fRC) + frB
728 if (aIsSNaN
|| aIsQNaN
) {
730 } else if (cIsSNaN
|| cIsQNaN
) {
737 /* A default implementation: prefer a to b to c.
738 * This is unlikely to actually match any real implementation.
740 static int pickNaNMulAdd(flag aIsQNaN
, flag aIsSNaN
, flag bIsQNaN
, flag bIsSNaN
,
741 flag cIsQNaN
, flag cIsSNaN
, flag infzero
,
742 float_status
*status
)
744 if (aIsSNaN
|| aIsQNaN
) {
746 } else if (bIsSNaN
|| bIsQNaN
) {
754 /*----------------------------------------------------------------------------
755 | Takes two single-precision floating-point values `a' and `b', one of which
756 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
757 | signaling NaN, the invalid exception is raised.
758 *----------------------------------------------------------------------------*/
760 static float32
propagateFloat32NaN(float32 a
, float32 b
, float_status
*status
)
762 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
763 flag aIsLargerSignificand
;
766 aIsQuietNaN
= float32_is_quiet_nan(a
, status
);
767 aIsSignalingNaN
= float32_is_signaling_nan(a
, status
);
768 bIsQuietNaN
= float32_is_quiet_nan(b
, status
);
769 bIsSignalingNaN
= float32_is_signaling_nan(b
, status
);
773 if (aIsSignalingNaN
| bIsSignalingNaN
) {
774 float_raise(float_flag_invalid
, status
);
777 if (status
->default_nan_mode
) {
778 return float32_default_nan(status
);
781 if ((uint32_t)(av
<< 1) < (uint32_t)(bv
<< 1)) {
782 aIsLargerSignificand
= 0;
783 } else if ((uint32_t)(bv
<< 1) < (uint32_t)(av
<< 1)) {
784 aIsLargerSignificand
= 1;
786 aIsLargerSignificand
= (av
< bv
) ? 1 : 0;
789 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
790 aIsLargerSignificand
)) {
791 return float32_maybe_silence_nan(b
, status
);
793 return float32_maybe_silence_nan(a
, status
);
797 /*----------------------------------------------------------------------------
798 | Returns 1 if the double-precision floating-point value `a' is a quiet
799 | NaN; otherwise returns 0.
800 *----------------------------------------------------------------------------*/
802 int float64_is_quiet_nan(float64 a_
, float_status
*status
)
804 #ifdef NO_SIGNALING_NANS
805 return float64_is_any_nan(a_
);
807 uint64_t a
= float64_val(a_
);
808 if (status
->snan_bit_is_one
) {
809 return (((a
>> 51) & 0xFFF) == 0xFFE)
810 && (a
& 0x0007FFFFFFFFFFFFULL
);
812 return ((a
<< 1) >= 0xFFF0000000000000ULL
);
817 /*----------------------------------------------------------------------------
818 | Returns 1 if the double-precision floating-point value `a' is a signaling
819 | NaN; otherwise returns 0.
820 *----------------------------------------------------------------------------*/
822 int float64_is_signaling_nan(float64 a_
, float_status
*status
)
824 #ifdef NO_SIGNALING_NANS
827 uint64_t a
= float64_val(a_
);
828 if (status
->snan_bit_is_one
) {
829 return ((a
<< 1) >= 0xFFF0000000000000ULL
);
831 return (((a
>> 51) & 0xFFF) == 0xFFE)
832 && (a
& LIT64(0x0007FFFFFFFFFFFF));
837 /*----------------------------------------------------------------------------
838 | Returns a quiet NaN from a signalling NaN for the double-precision
839 | floating point value `a'.
840 *----------------------------------------------------------------------------*/
842 float64
float64_silence_nan(float64 a
, float_status
*status
)
844 #ifdef NO_SIGNALING_NANS
845 g_assert_not_reached();
847 if (status
->snan_bit_is_one
) {
849 a
&= ~0x0008000000000000ULL
;
850 a
|= 0x0004000000000000ULL
;
853 return float64_default_nan(status
);
856 return a
| LIT64(0x0008000000000000);
861 /*----------------------------------------------------------------------------
862 | Returns a quiet NaN if the double-precision floating point value `a' is a
863 | signaling NaN; otherwise returns `a'.
864 *----------------------------------------------------------------------------*/
866 float64
float64_maybe_silence_nan(float64 a
, float_status
*status
)
868 if (float64_is_signaling_nan(a
, status
)) {
869 return float64_silence_nan(a
, status
);
874 /*----------------------------------------------------------------------------
875 | Returns the result of converting the double-precision floating-point NaN
876 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
877 | exception is raised.
878 *----------------------------------------------------------------------------*/
880 static commonNaNT
float64ToCommonNaN(float64 a
, float_status
*status
)
884 if (float64_is_signaling_nan(a
, status
)) {
885 float_raise(float_flag_invalid
, status
);
887 z
.sign
= float64_val(a
) >> 63;
889 z
.high
= float64_val(a
) << 12;
893 /*----------------------------------------------------------------------------
894 | Returns the result of converting the canonical NaN `a' to the double-
895 | precision floating-point format.
896 *----------------------------------------------------------------------------*/
898 static float64
commonNaNToFloat64(commonNaNT a
, float_status
*status
)
900 uint64_t mantissa
= a
.high
>> 12;
902 if (status
->default_nan_mode
) {
903 return float64_default_nan(status
);
908 (((uint64_t) a
.sign
) << 63)
909 | LIT64(0x7FF0000000000000)
912 return float64_default_nan(status
);
916 /*----------------------------------------------------------------------------
917 | Takes two double-precision floating-point values `a' and `b', one of which
918 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
919 | signaling NaN, the invalid exception is raised.
920 *----------------------------------------------------------------------------*/
922 static float64
propagateFloat64NaN(float64 a
, float64 b
, float_status
*status
)
924 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
925 flag aIsLargerSignificand
;
928 aIsQuietNaN
= float64_is_quiet_nan(a
, status
);
929 aIsSignalingNaN
= float64_is_signaling_nan(a
, status
);
930 bIsQuietNaN
= float64_is_quiet_nan(b
, status
);
931 bIsSignalingNaN
= float64_is_signaling_nan(b
, status
);
935 if (aIsSignalingNaN
| bIsSignalingNaN
) {
936 float_raise(float_flag_invalid
, status
);
939 if (status
->default_nan_mode
) {
940 return float64_default_nan(status
);
943 if ((uint64_t)(av
<< 1) < (uint64_t)(bv
<< 1)) {
944 aIsLargerSignificand
= 0;
945 } else if ((uint64_t)(bv
<< 1) < (uint64_t)(av
<< 1)) {
946 aIsLargerSignificand
= 1;
948 aIsLargerSignificand
= (av
< bv
) ? 1 : 0;
951 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
952 aIsLargerSignificand
)) {
953 return float64_maybe_silence_nan(b
, status
);
955 return float64_maybe_silence_nan(a
, status
);
959 /*----------------------------------------------------------------------------
960 | Returns 1 if the extended double-precision floating-point value `a' is a
961 | quiet NaN; otherwise returns 0. This slightly differs from the same
962 | function for other types as floatx80 has an explicit bit.
963 *----------------------------------------------------------------------------*/
965 int floatx80_is_quiet_nan(floatx80 a
, float_status
*status
)
967 #ifdef NO_SIGNALING_NANS
968 return floatx80_is_any_nan(a
);
970 if (status
->snan_bit_is_one
) {
973 aLow
= a
.low
& ~0x4000000000000000ULL
;
974 return ((a
.high
& 0x7FFF) == 0x7FFF)
978 return ((a
.high
& 0x7FFF) == 0x7FFF)
979 && (LIT64(0x8000000000000000) <= ((uint64_t)(a
.low
<< 1)));
984 /*----------------------------------------------------------------------------
985 | Returns 1 if the extended double-precision floating-point value `a' is a
986 | signaling NaN; otherwise returns 0. This slightly differs from the same
987 | function for other types as floatx80 has an explicit bit.
988 *----------------------------------------------------------------------------*/
990 int floatx80_is_signaling_nan(floatx80 a
, float_status
*status
)
992 #ifdef NO_SIGNALING_NANS
995 if (status
->snan_bit_is_one
) {
996 return ((a
.high
& 0x7FFF) == 0x7FFF)
997 && ((a
.low
<< 1) >= 0x8000000000000000ULL
);
1001 aLow
= a
.low
& ~LIT64(0x4000000000000000);
1002 return ((a
.high
& 0x7FFF) == 0x7FFF)
1003 && (uint64_t)(aLow
<< 1)
1009 /*----------------------------------------------------------------------------
1010 | Returns a quiet NaN from a signalling NaN for the extended double-precision
1011 | floating point value `a'.
1012 *----------------------------------------------------------------------------*/
1014 floatx80
floatx80_silence_nan(floatx80 a
, float_status
*status
)
1016 #ifdef NO_SIGNALING_NANS
1017 g_assert_not_reached();
1019 if (status
->snan_bit_is_one
) {
1020 return floatx80_default_nan(status
);
1022 a
.low
|= LIT64(0xC000000000000000);
1028 /*----------------------------------------------------------------------------
1029 | Returns a quiet NaN if the extended double-precision floating point value
1030 | `a' is a signaling NaN; otherwise returns `a'.
1031 *----------------------------------------------------------------------------*/
1033 floatx80
floatx80_maybe_silence_nan(floatx80 a
, float_status
*status
)
1035 if (floatx80_is_signaling_nan(a
, status
)) {
1036 return floatx80_silence_nan(a
, status
);
1041 /*----------------------------------------------------------------------------
1042 | Returns the result of converting the extended double-precision floating-
1043 | point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
1044 | invalid exception is raised.
1045 *----------------------------------------------------------------------------*/
1047 static commonNaNT
floatx80ToCommonNaN(floatx80 a
, float_status
*status
)
1052 if (floatx80_is_signaling_nan(a
, status
)) {
1053 float_raise(float_flag_invalid
, status
);
1056 z
.sign
= a
.high
>> 15;
1058 z
.high
= a
.low
<< 1;
1060 dflt
= floatx80_default_nan(status
);
1061 z
.sign
= dflt
.high
>> 15;
1063 z
.high
= dflt
.low
<< 1;
1068 /*----------------------------------------------------------------------------
1069 | Returns the result of converting the canonical NaN `a' to the extended
1070 | double-precision floating-point format.
1071 *----------------------------------------------------------------------------*/
1073 static floatx80
commonNaNToFloatx80(commonNaNT a
, float_status
*status
)
1077 if (status
->default_nan_mode
) {
1078 return floatx80_default_nan(status
);
1082 z
.low
= LIT64(0x8000000000000000) | a
.high
>> 1;
1083 z
.high
= (((uint16_t)a
.sign
) << 15) | 0x7FFF;
1085 z
= floatx80_default_nan(status
);
1090 /*----------------------------------------------------------------------------
1091 | Takes two extended double-precision floating-point values `a' and `b', one
1092 | of which is a NaN, and returns the appropriate NaN result. If either `a' or
1093 | `b' is a signaling NaN, the invalid exception is raised.
1094 *----------------------------------------------------------------------------*/
1096 floatx80
propagateFloatx80NaN(floatx80 a
, floatx80 b
, float_status
*status
)
1098 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
1099 flag aIsLargerSignificand
;
1101 aIsQuietNaN
= floatx80_is_quiet_nan(a
, status
);
1102 aIsSignalingNaN
= floatx80_is_signaling_nan(a
, status
);
1103 bIsQuietNaN
= floatx80_is_quiet_nan(b
, status
);
1104 bIsSignalingNaN
= floatx80_is_signaling_nan(b
, status
);
1106 if (aIsSignalingNaN
| bIsSignalingNaN
) {
1107 float_raise(float_flag_invalid
, status
);
1110 if (status
->default_nan_mode
) {
1111 return floatx80_default_nan(status
);
1114 if (a
.low
< b
.low
) {
1115 aIsLargerSignificand
= 0;
1116 } else if (b
.low
< a
.low
) {
1117 aIsLargerSignificand
= 1;
1119 aIsLargerSignificand
= (a
.high
< b
.high
) ? 1 : 0;
1122 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
1123 aIsLargerSignificand
)) {
1124 return floatx80_maybe_silence_nan(b
, status
);
1126 return floatx80_maybe_silence_nan(a
, status
);
1130 /*----------------------------------------------------------------------------
1131 | Returns 1 if the quadruple-precision floating-point value `a' is a quiet
1132 | NaN; otherwise returns 0.
1133 *----------------------------------------------------------------------------*/
1135 int float128_is_quiet_nan(float128 a
, float_status
*status
)
1137 #ifdef NO_SIGNALING_NANS
1138 return float128_is_any_nan(a
);
1140 if (status
->snan_bit_is_one
) {
1141 return (((a
.high
>> 47) & 0xFFFF) == 0xFFFE)
1142 && (a
.low
|| (a
.high
& 0x00007FFFFFFFFFFFULL
));
1144 return ((a
.high
<< 1) >= 0xFFFF000000000000ULL
)
1145 && (a
.low
|| (a
.high
& 0x0000FFFFFFFFFFFFULL
));
1150 /*----------------------------------------------------------------------------
1151 | Returns 1 if the quadruple-precision floating-point value `a' is a
1152 | signaling NaN; otherwise returns 0.
1153 *----------------------------------------------------------------------------*/
1155 int float128_is_signaling_nan(float128 a
, float_status
*status
)
1157 #ifdef NO_SIGNALING_NANS
1160 if (status
->snan_bit_is_one
) {
1161 return ((a
.high
<< 1) >= 0xFFFF000000000000ULL
)
1162 && (a
.low
|| (a
.high
& 0x0000FFFFFFFFFFFFULL
));
1164 return (((a
.high
>> 47) & 0xFFFF) == 0xFFFE)
1165 && (a
.low
|| (a
.high
& LIT64(0x00007FFFFFFFFFFF)));
1170 /*----------------------------------------------------------------------------
1171 | Returns a quiet NaN from a signalling NaN for the quadruple-precision
1172 | floating point value `a'.
1173 *----------------------------------------------------------------------------*/
1175 float128
float128_silence_nan(float128 a
, float_status
*status
)
1177 #ifdef NO_SIGNALING_NANS
1178 g_assert_not_reached();
1180 if (status
->snan_bit_is_one
) {
1181 return float128_default_nan(status
);
1183 a
.high
|= LIT64(0x0000800000000000);
1189 /*----------------------------------------------------------------------------
1190 | Returns a quiet NaN if the quadruple-precision floating point value `a' is
1191 | a signaling NaN; otherwise returns `a'.
1192 *----------------------------------------------------------------------------*/
1194 float128
float128_maybe_silence_nan(float128 a
, float_status
*status
)
1196 if (float128_is_signaling_nan(a
, status
)) {
1197 return float128_silence_nan(a
, status
);
1202 /*----------------------------------------------------------------------------
1203 | Returns the result of converting the quadruple-precision floating-point NaN
1204 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
1205 | exception is raised.
1206 *----------------------------------------------------------------------------*/
1208 static commonNaNT
float128ToCommonNaN(float128 a
, float_status
*status
)
1212 if (float128_is_signaling_nan(a
, status
)) {
1213 float_raise(float_flag_invalid
, status
);
1215 z
.sign
= a
.high
>> 63;
1216 shortShift128Left(a
.high
, a
.low
, 16, &z
.high
, &z
.low
);
1220 /*----------------------------------------------------------------------------
1221 | Returns the result of converting the canonical NaN `a' to the quadruple-
1222 | precision floating-point format.
1223 *----------------------------------------------------------------------------*/
1225 static float128
commonNaNToFloat128(commonNaNT a
, float_status
*status
)
1229 if (status
->default_nan_mode
) {
1230 return float128_default_nan(status
);
1233 shift128Right(a
.high
, a
.low
, 16, &z
.high
, &z
.low
);
1234 z
.high
|= (((uint64_t)a
.sign
) << 63) | LIT64(0x7FFF000000000000);
1238 /*----------------------------------------------------------------------------
1239 | Takes two quadruple-precision floating-point values `a' and `b', one of
1240 | which is a NaN, and returns the appropriate NaN result. If either `a' or
1241 | `b' is a signaling NaN, the invalid exception is raised.
1242 *----------------------------------------------------------------------------*/
1244 static float128
propagateFloat128NaN(float128 a
, float128 b
,
1245 float_status
*status
)
1247 flag aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
;
1248 flag aIsLargerSignificand
;
1250 aIsQuietNaN
= float128_is_quiet_nan(a
, status
);
1251 aIsSignalingNaN
= float128_is_signaling_nan(a
, status
);
1252 bIsQuietNaN
= float128_is_quiet_nan(b
, status
);
1253 bIsSignalingNaN
= float128_is_signaling_nan(b
, status
);
1255 if (aIsSignalingNaN
| bIsSignalingNaN
) {
1256 float_raise(float_flag_invalid
, status
);
1259 if (status
->default_nan_mode
) {
1260 return float128_default_nan(status
);
1263 if (lt128(a
.high
<< 1, a
.low
, b
.high
<< 1, b
.low
)) {
1264 aIsLargerSignificand
= 0;
1265 } else if (lt128(b
.high
<< 1, b
.low
, a
.high
<< 1, a
.low
)) {
1266 aIsLargerSignificand
= 1;
1268 aIsLargerSignificand
= (a
.high
< b
.high
) ? 1 : 0;
1271 if (pickNaN(aIsQuietNaN
, aIsSignalingNaN
, bIsQuietNaN
, bIsSignalingNaN
,
1272 aIsLargerSignificand
)) {
1273 return float128_maybe_silence_nan(b
, status
);
1275 return float128_maybe_silence_nan(a
, status
);