1/* 2 * QEMU float support 3 * 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 10 * the BSD license 11 * GPL-v2-or-later 12 * 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. 16 */ 17 18/* 19=============================================================================== 20This C source fragment is part of the SoftFloat IEC/IEEE Floating-point 21Arithmetic Package, Release 2a. 22 23Written by John R. Hauser. This work was made possible in part by the 24International Computer Science Institute, located at Suite 600, 1947 Center 25Street, Berkeley, California 94704. Funding was partially provided by the 26National Science Foundation under grant MIP-9311980. The original version 27of this code was written as part of a project to build a fixed-point vector 28processor in collaboration with the University of California at Berkeley, 29overseen by Profs. Nelson Morgan and John Wawrzynek. More information 30is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ 31arithmetic/SoftFloat.html'. 32 33THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort 34has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT 35TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO 36PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY 37AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. 38 39Derivative works are acceptable, even for commercial purposes, so long as 40(1) they include prominent notice that the work is derivative, and (2) they 41include prominent notice akin to these four paragraphs for those parts of 42this code that are retained. 43 44=============================================================================== 45*/ 46 47/* BSD licensing: 48 * Copyright (c) 2006, Fabrice Bellard 49 * All rights reserved. 50 * 51 * Redistribution and use in source and binary forms, with or without 52 * modification, are permitted provided that the following conditions are met: 53 * 54 * 1. Redistributions of source code must retain the above copyright notice, 55 * this list of conditions and the following disclaimer. 56 * 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. 60 * 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. 64 * 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. 76 */ 77 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. 80 */ 81 82/* 83 * Define whether architecture deviates from IEEE in not supporting 84 * signaling NaNs (so all NaNs are treated as quiet). 85 */ 86static inline bool no_signaling_nans(float_status *status) 87{ 88#if defined(TARGET_XTENSA) 89 return status->no_signaling_nans; 90#else 91 return false; 92#endif 93} 94 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. 101 */ 102static inline bool snan_bit_is_one(float_status *status) 103{ 104#if defined(TARGET_MIPS) 105 return status->snan_bit_is_one; 106#elif defined(TARGET_HPPA) || defined(TARGET_SH4) 107 return 1; 108#else 109 return 0; 110#endif 111} 112 113/*---------------------------------------------------------------------------- 114| For the deconstructed floating-point with fraction FRAC, return true 115| if the fraction represents a signalling NaN; otherwise false. 116*----------------------------------------------------------------------------*/ 117 118static bool parts_is_snan_frac(uint64_t frac, float_status *status) 119{ 120 if (no_signaling_nans(status)) { 121 return false; 122 } else { 123 bool msb = extract64(frac, DECOMPOSED_BINARY_POINT - 1, 1); 124 return msb == snan_bit_is_one(status); 125 } 126} 127 128/*---------------------------------------------------------------------------- 129| The pattern for a default generated deconstructed floating-point NaN. 130*----------------------------------------------------------------------------*/ 131 132static void parts64_default_nan(FloatParts64 *p, float_status *status) 133{ 134 bool sign = 0; 135 uint64_t frac; 136 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); 144 sign = 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) 149 sign = 1; 150 frac = ~0ULL; 151#else 152 /* 153 * This case is true for Alpha, ARM, MIPS, OpenRISC, PPC, RISC-V, 154 * S390, SH4, TriCore, and Xtensa. Our other supported targets 155 * do not have floating-point. 156 */ 157 if (snan_bit_is_one(status)) { 158 /* set all bits other than msb */ 159 frac = (1ULL << (DECOMPOSED_BINARY_POINT - 1)) - 1; 160 } else { 161 /* set msb */ 162 frac = 1ULL << (DECOMPOSED_BINARY_POINT - 1); 163 } 164#endif 165 166 *p = (FloatParts64) { 167 .cls = float_class_qnan, 168 .sign = sign, 169 .exp = INT_MAX, 170 .frac = frac 171 }; 172} 173 174static void parts128_default_nan(FloatParts128 *p, float_status *status) 175{ 176 /* 177 * Extrapolate from the choices made by parts64_default_nan to fill 178 * in the quad-floating format. If the low bit is set, assume we 179 * want to set all non-snan bits. 180 */ 181 FloatParts64 p64; 182 parts64_default_nan(&p64, status); 183 184 *p = (FloatParts128) { 185 .cls = float_class_qnan, 186 .sign = p64.sign, 187 .exp = INT_MAX, 188 .frac_hi = p64.frac, 189 .frac_lo = -(p64.frac & 1) 190 }; 191} 192 193/*---------------------------------------------------------------------------- 194| Returns a quiet NaN from a signalling NaN for the deconstructed 195| floating-point parts. 196*----------------------------------------------------------------------------*/ 197 198static uint64_t parts_silence_nan_frac(uint64_t frac, float_status *status) 199{ 200 g_assert(!no_signaling_nans(status)); 201 202 /* The only snan_bit_is_one target without default_nan_mode is HPPA. */ 203 if (snan_bit_is_one(status)) { 204 frac &= ~(1ULL << (DECOMPOSED_BINARY_POINT - 1)); 205 frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 2); 206 } else { 207 frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 1); 208 } 209 return frac; 210} 211 212static void parts64_silence_nan(FloatParts64 *p, float_status *status) 213{ 214 p->frac = parts_silence_nan_frac(p->frac, status); 215 p->cls = float_class_qnan; 216} 217 218static void parts128_silence_nan(FloatParts128 *p, float_status *status) 219{ 220 p->frac_hi = parts_silence_nan_frac(p->frac_hi, status); 221 p->cls = float_class_qnan; 222} 223 224/*---------------------------------------------------------------------------- 225| The pattern for a default generated extended double-precision NaN. 226*----------------------------------------------------------------------------*/ 227floatx80 floatx80_default_nan(float_status *status) 228{ 229 floatx80 r; 230 231 /* None of the targets that have snan_bit_is_one use floatx80. */ 232 assert(!snan_bit_is_one(status)); 233#if defined(TARGET_M68K) 234 r.low = UINT64_C(0xFFFFFFFFFFFFFFFF); 235 r.high = 0x7FFF; 236#else 237 /* X86 */ 238 r.low = UINT64_C(0xC000000000000000); 239 r.high = 0xFFFF; 240#endif 241 return r; 242} 243 244/*---------------------------------------------------------------------------- 245| The pattern for a default generated extended double-precision inf. 246*----------------------------------------------------------------------------*/ 247 248#define floatx80_infinity_high 0x7FFF 249#if defined(TARGET_M68K) 250#define floatx80_infinity_low UINT64_C(0x0000000000000000) 251#else 252#define floatx80_infinity_low UINT64_C(0x8000000000000000) 253#endif 254 255const floatx80 floatx80_infinity 256 = make_floatx80_init(floatx80_infinity_high, floatx80_infinity_low); 257 258/*---------------------------------------------------------------------------- 259| Returns 1 if the half-precision floating-point value `a' is a quiet 260| NaN; otherwise returns 0. 261*----------------------------------------------------------------------------*/ 262 263bool float16_is_quiet_nan(float16 a_, float_status *status) 264{ 265 if (no_signaling_nans(status)) { 266 return float16_is_any_nan(a_); 267 } else { 268 uint16_t a = float16_val(a_); 269 if (snan_bit_is_one(status)) { 270 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF); 271 } else { 272 273 return ((a >> 9) & 0x3F) == 0x3F; 274 } 275 } 276} 277 278/*---------------------------------------------------------------------------- 279| Returns 1 if the bfloat16 value `a' is a quiet 280| NaN; otherwise returns 0. 281*----------------------------------------------------------------------------*/ 282 283bool bfloat16_is_quiet_nan(bfloat16 a_, float_status *status) 284{ 285 if (no_signaling_nans(status)) { 286 return bfloat16_is_any_nan(a_); 287 } else { 288 uint16_t a = a_; 289 if (snan_bit_is_one(status)) { 290 return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F); 291 } else { 292 return ((a >> 6) & 0x1FF) == 0x1FF; 293 } 294 } 295} 296 297/*---------------------------------------------------------------------------- 298| Returns 1 if the half-precision floating-point value `a' is a signaling 299| NaN; otherwise returns 0. 300*----------------------------------------------------------------------------*/ 301 302bool float16_is_signaling_nan(float16 a_, float_status *status) 303{ 304 if (no_signaling_nans(status)) { 305 return 0; 306 } else { 307 uint16_t a = float16_val(a_); 308 if (snan_bit_is_one(status)) { 309 return ((a >> 9) & 0x3F) == 0x3F; 310 } else { 311 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF); 312 } 313 } 314} 315 316/*---------------------------------------------------------------------------- 317| Returns 1 if the bfloat16 value `a' is a signaling 318| NaN; otherwise returns 0. 319*----------------------------------------------------------------------------*/ 320 321bool bfloat16_is_signaling_nan(bfloat16 a_, float_status *status) 322{ 323 if (no_signaling_nans(status)) { 324 return 0; 325 } else { 326 uint16_t a = a_; 327 if (snan_bit_is_one(status)) { 328 return ((a >> 6) & 0x1FF) == 0x1FF; 329 } else { 330 return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F); 331 } 332 } 333} 334 335/*---------------------------------------------------------------------------- 336| Returns 1 if the single-precision floating-point value `a' is a quiet 337| NaN; otherwise returns 0. 338*----------------------------------------------------------------------------*/ 339 340bool float32_is_quiet_nan(float32 a_, float_status *status) 341{ 342 if (no_signaling_nans(status)) { 343 return float32_is_any_nan(a_); 344 } else { 345 uint32_t a = float32_val(a_); 346 if (snan_bit_is_one(status)) { 347 return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF); 348 } else { 349 return ((uint32_t)(a << 1) >= 0xFF800000); 350 } 351 } 352} 353 354/*---------------------------------------------------------------------------- 355| Returns 1 if the single-precision floating-point value `a' is a signaling 356| NaN; otherwise returns 0. 357*----------------------------------------------------------------------------*/ 358 359bool float32_is_signaling_nan(float32 a_, float_status *status) 360{ 361 if (no_signaling_nans(status)) { 362 return 0; 363 } else { 364 uint32_t a = float32_val(a_); 365 if (snan_bit_is_one(status)) { 366 return ((uint32_t)(a << 1) >= 0xFF800000); 367 } else { 368 return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF); 369 } 370 } 371} 372 373/*---------------------------------------------------------------------------- 374| Select which NaN to propagate for a two-input operation. 375| IEEE754 doesn't specify all the details of this, so the 376| algorithm is target-specific. 377| The routine is passed various bits of information about the 378| two NaNs and should return 0 to select NaN a and 1 for NaN b. 379| Note that signalling NaNs are always squashed to quiet NaNs 380| by the caller, by calling floatXX_silence_nan() before 381| returning them. 382| 383| aIsLargerSignificand is only valid if both a and b are NaNs 384| of some kind, and is true if a has the larger significand, 385| or if both a and b have the same significand but a is 386| positive but b is negative. It is only needed for the x87 387| tie-break rule. 388*----------------------------------------------------------------------------*/ 389 390static int pickNaN(FloatClass a_cls, FloatClass b_cls, 391 bool aIsLargerSignificand, float_status *status) 392{ 393 Float2NaNPropRule rule = status->float_2nan_prop_rule; 394 395 /* 396 * We guarantee not to require the target to tell us how to 397 * pick a NaN if we're always returning the default NaN. 398 */ 399 assert(!status->default_nan_mode); 400 401 if (rule == float_2nan_prop_none) { 402 /* target didn't set the rule: fall back to old ifdef choices */ 403#if defined(TARGET_AVR) || defined(TARGET_HEXAGON) \ 404 || defined(TARGET_RISCV) || defined(TARGET_SH4) \ 405 || defined(TARGET_TRICORE) || defined(TARGET_ARM) || defined(TARGET_MIPS) \ 406 || defined(TARGET_LOONGARCH64) || defined(TARGET_HPPA) \ 407 || defined(TARGET_S390X) || defined(TARGET_PPC) || defined(TARGET_M68K) \ 408 || defined(TARGET_SPARC) || defined(TARGET_XTENSA) \ 409 || defined(TARGET_I386) || defined(TARGET_ALPHA) \ 410 || defined(TARGET_MICROBLAZE) || defined(TARGET_OPENRISC) \ 411 || defined(TARGET_RX) 412 g_assert_not_reached(); 413#else 414 rule = float_2nan_prop_x87; 415#endif 416 } 417 418 switch (rule) { 419 case float_2nan_prop_s_ab: 420 if (is_snan(a_cls)) { 421 return 0; 422 } else if (is_snan(b_cls)) { 423 return 1; 424 } else if (is_qnan(a_cls)) { 425 return 0; 426 } else { 427 return 1; 428 } 429 break; 430 case float_2nan_prop_s_ba: 431 if (is_snan(b_cls)) { 432 return 1; 433 } else if (is_snan(a_cls)) { 434 return 0; 435 } else if (is_qnan(b_cls)) { 436 return 1; 437 } else { 438 return 0; 439 } 440 break; 441 case float_2nan_prop_ab: 442 if (is_nan(a_cls)) { 443 return 0; 444 } else { 445 return 1; 446 } 447 break; 448 case float_2nan_prop_ba: 449 if (is_nan(b_cls)) { 450 return 1; 451 } else { 452 return 0; 453 } 454 break; 455 case float_2nan_prop_x87: 456 /* 457 * This implements x87 NaN propagation rules: 458 * SNaN + QNaN => return the QNaN 459 * two SNaNs => return the one with the larger significand, silenced 460 * two QNaNs => return the one with the larger significand 461 * SNaN and a non-NaN => return the SNaN, silenced 462 * QNaN and a non-NaN => return the QNaN 463 * 464 * If we get down to comparing significands and they are the same, 465 * return the NaN with the positive sign bit (if any). 466 */ 467 if (is_snan(a_cls)) { 468 if (is_snan(b_cls)) { 469 return aIsLargerSignificand ? 0 : 1; 470 } 471 return is_qnan(b_cls) ? 1 : 0; 472 } else if (is_qnan(a_cls)) { 473 if (is_snan(b_cls) || !is_qnan(b_cls)) { 474 return 0; 475 } else { 476 return aIsLargerSignificand ? 0 : 1; 477 } 478 } else { 479 return 1; 480 } 481 default: 482 g_assert_not_reached(); 483 } 484} 485 486/*---------------------------------------------------------------------------- 487| Select which NaN to propagate for a three-input operation. 488| For the moment we assume that no CPU needs the 'larger significand' 489| information. 490| Return values : 0 : a; 1 : b; 2 : c; 3 : default-NaN 491*----------------------------------------------------------------------------*/ 492static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls, 493 bool infzero, float_status *status) 494{ 495#if defined(TARGET_ARM) 496 /* For ARM, the (inf,zero,qnan) case sets InvalidOp and returns 497 * the default NaN 498 */ 499 if (infzero && is_qnan(c_cls)) { 500 float_raise(float_flag_invalid | float_flag_invalid_imz, status); 501 return 3; 502 } 503 504 /* This looks different from the ARM ARM pseudocode, because the ARM ARM 505 * puts the operands to a fused mac operation (a*b)+c in the order c,a,b. 506 */ 507 if (is_snan(c_cls)) { 508 return 2; 509 } else if (is_snan(a_cls)) { 510 return 0; 511 } else if (is_snan(b_cls)) { 512 return 1; 513 } else if (is_qnan(c_cls)) { 514 return 2; 515 } else if (is_qnan(a_cls)) { 516 return 0; 517 } else { 518 return 1; 519 } 520#elif defined(TARGET_MIPS) 521 if (snan_bit_is_one(status)) { 522 /* 523 * For MIPS systems that conform to IEEE754-1985, the (inf,zero,nan) 524 * case sets InvalidOp and returns the default NaN 525 */ 526 if (infzero) { 527 float_raise(float_flag_invalid | float_flag_invalid_imz, status); 528 return 3; 529 } 530 /* Prefer sNaN over qNaN, in the a, b, c order. */ 531 if (is_snan(a_cls)) { 532 return 0; 533 } else if (is_snan(b_cls)) { 534 return 1; 535 } else if (is_snan(c_cls)) { 536 return 2; 537 } else if (is_qnan(a_cls)) { 538 return 0; 539 } else if (is_qnan(b_cls)) { 540 return 1; 541 } else { 542 return 2; 543 } 544 } else { 545 /* 546 * For MIPS systems that conform to IEEE754-2008, the (inf,zero,nan) 547 * case sets InvalidOp and returns the input value 'c' 548 */ 549 if (infzero) { 550 float_raise(float_flag_invalid | float_flag_invalid_imz, status); 551 return 2; 552 } 553 /* Prefer sNaN over qNaN, in the c, a, b order. */ 554 if (is_snan(c_cls)) { 555 return 2; 556 } else if (is_snan(a_cls)) { 557 return 0; 558 } else if (is_snan(b_cls)) { 559 return 1; 560 } else if (is_qnan(c_cls)) { 561 return 2; 562 } else if (is_qnan(a_cls)) { 563 return 0; 564 } else { 565 return 1; 566 } 567 } 568#elif defined(TARGET_LOONGARCH64) 569 /* 570 * For LoongArch systems that conform to IEEE754-2008, the (inf,zero,nan) 571 * case sets InvalidOp and returns the input value 'c' 572 */ 573 if (infzero) { 574 float_raise(float_flag_invalid | float_flag_invalid_imz, status); 575 return 2; 576 } 577 /* Prefer sNaN over qNaN, in the c, a, b order. */ 578 if (is_snan(c_cls)) { 579 return 2; 580 } else if (is_snan(a_cls)) { 581 return 0; 582 } else if (is_snan(b_cls)) { 583 return 1; 584 } else if (is_qnan(c_cls)) { 585 return 2; 586 } else if (is_qnan(a_cls)) { 587 return 0; 588 } else { 589 return 1; 590 } 591#elif defined(TARGET_PPC) 592 /* For PPC, the (inf,zero,qnan) case sets InvalidOp, but we prefer 593 * to return an input NaN if we have one (ie c) rather than generating 594 * a default NaN 595 */ 596 if (infzero) { 597 float_raise(float_flag_invalid | float_flag_invalid_imz, status); 598 return 2; 599 } 600 601 /* If fRA is a NaN return it; otherwise if fRB is a NaN return it; 602 * otherwise return fRC. Note that muladd on PPC is (fRA * fRC) + frB 603 */ 604 if (is_nan(a_cls)) { 605 return 0; 606 } else if (is_nan(c_cls)) { 607 return 2; 608 } else { 609 return 1; 610 } 611#elif defined(TARGET_RISCV) 612 /* For RISC-V, InvalidOp is set when multiplicands are Inf and zero */ 613 if (infzero) { 614 float_raise(float_flag_invalid | float_flag_invalid_imz, status); 615 } 616 return 3; /* default NaN */ 617#elif defined(TARGET_SPARC) 618 /* For (inf,0,nan) return c. */ 619 if (infzero) { 620 float_raise(float_flag_invalid | float_flag_invalid_imz, status); 621 return 2; 622 } 623 /* Prefer SNaN over QNaN, order C, B, A. */ 624 if (is_snan(c_cls)) { 625 return 2; 626 } else if (is_snan(b_cls)) { 627 return 1; 628 } else if (is_snan(a_cls)) { 629 return 0; 630 } else if (is_qnan(c_cls)) { 631 return 2; 632 } else if (is_qnan(b_cls)) { 633 return 1; 634 } else { 635 return 0; 636 } 637#elif defined(TARGET_XTENSA) 638 /* 639 * For Xtensa, the (inf,zero,nan) case sets InvalidOp and returns 640 * an input NaN if we have one (ie c). 641 */ 642 if (infzero) { 643 float_raise(float_flag_invalid | float_flag_invalid_imz, status); 644 return 2; 645 } 646 if (status->use_first_nan) { 647 if (is_nan(a_cls)) { 648 return 0; 649 } else if (is_nan(b_cls)) { 650 return 1; 651 } else { 652 return 2; 653 } 654 } else { 655 if (is_nan(c_cls)) { 656 return 2; 657 } else if (is_nan(b_cls)) { 658 return 1; 659 } else { 660 return 0; 661 } 662 } 663#else 664 /* A default implementation: prefer a to b to c. 665 * This is unlikely to actually match any real implementation. 666 */ 667 if (is_nan(a_cls)) { 668 return 0; 669 } else if (is_nan(b_cls)) { 670 return 1; 671 } else { 672 return 2; 673 } 674#endif 675} 676 677/*---------------------------------------------------------------------------- 678| Returns 1 if the double-precision floating-point value `a' is a quiet 679| NaN; otherwise returns 0. 680*----------------------------------------------------------------------------*/ 681 682bool float64_is_quiet_nan(float64 a_, float_status *status) 683{ 684 if (no_signaling_nans(status)) { 685 return float64_is_any_nan(a_); 686 } else { 687 uint64_t a = float64_val(a_); 688 if (snan_bit_is_one(status)) { 689 return (((a >> 51) & 0xFFF) == 0xFFE) 690 && (a & 0x0007FFFFFFFFFFFFULL); 691 } else { 692 return ((a << 1) >= 0xFFF0000000000000ULL); 693 } 694 } 695} 696 697/*---------------------------------------------------------------------------- 698| Returns 1 if the double-precision floating-point value `a' is a signaling 699| NaN; otherwise returns 0. 700*----------------------------------------------------------------------------*/ 701 702bool float64_is_signaling_nan(float64 a_, float_status *status) 703{ 704 if (no_signaling_nans(status)) { 705 return 0; 706 } else { 707 uint64_t a = float64_val(a_); 708 if (snan_bit_is_one(status)) { 709 return ((a << 1) >= 0xFFF0000000000000ULL); 710 } else { 711 return (((a >> 51) & 0xFFF) == 0xFFE) 712 && (a & UINT64_C(0x0007FFFFFFFFFFFF)); 713 } 714 } 715} 716 717/*---------------------------------------------------------------------------- 718| Returns 1 if the extended double-precision floating-point value `a' is a 719| quiet NaN; otherwise returns 0. This slightly differs from the same 720| function for other types as floatx80 has an explicit bit. 721*----------------------------------------------------------------------------*/ 722 723int floatx80_is_quiet_nan(floatx80 a, float_status *status) 724{ 725 if (no_signaling_nans(status)) { 726 return floatx80_is_any_nan(a); 727 } else { 728 if (snan_bit_is_one(status)) { 729 uint64_t aLow; 730 731 aLow = a.low & ~0x4000000000000000ULL; 732 return ((a.high & 0x7FFF) == 0x7FFF) 733 && (aLow << 1) 734 && (a.low == aLow); 735 } else { 736 return ((a.high & 0x7FFF) == 0x7FFF) 737 && (UINT64_C(0x8000000000000000) <= ((uint64_t)(a.low << 1))); 738 } 739 } 740} 741 742/*---------------------------------------------------------------------------- 743| Returns 1 if the extended double-precision floating-point value `a' is a 744| signaling NaN; otherwise returns 0. This slightly differs from the same 745| function for other types as floatx80 has an explicit bit. 746*----------------------------------------------------------------------------*/ 747 748int floatx80_is_signaling_nan(floatx80 a, float_status *status) 749{ 750 if (no_signaling_nans(status)) { 751 return 0; 752 } else { 753 if (snan_bit_is_one(status)) { 754 return ((a.high & 0x7FFF) == 0x7FFF) 755 && ((a.low << 1) >= 0x8000000000000000ULL); 756 } else { 757 uint64_t aLow; 758 759 aLow = a.low & ~UINT64_C(0x4000000000000000); 760 return ((a.high & 0x7FFF) == 0x7FFF) 761 && (uint64_t)(aLow << 1) 762 && (a.low == aLow); 763 } 764 } 765} 766 767/*---------------------------------------------------------------------------- 768| Returns a quiet NaN from a signalling NaN for the extended double-precision 769| floating point value `a'. 770*----------------------------------------------------------------------------*/ 771 772floatx80 floatx80_silence_nan(floatx80 a, float_status *status) 773{ 774 /* None of the targets that have snan_bit_is_one use floatx80. */ 775 assert(!snan_bit_is_one(status)); 776 a.low |= UINT64_C(0xC000000000000000); 777 return a; 778} 779 780/*---------------------------------------------------------------------------- 781| Takes two extended double-precision floating-point values `a' and `b', one 782| of which is a NaN, and returns the appropriate NaN result. If either `a' or 783| `b' is a signaling NaN, the invalid exception is raised. 784*----------------------------------------------------------------------------*/ 785 786floatx80 propagateFloatx80NaN(floatx80 a, floatx80 b, float_status *status) 787{ 788 bool aIsLargerSignificand; 789 FloatClass a_cls, b_cls; 790 791 /* This is not complete, but is good enough for pickNaN. */ 792 a_cls = (!floatx80_is_any_nan(a) 793 ? float_class_normal 794 : floatx80_is_signaling_nan(a, status) 795 ? float_class_snan 796 : float_class_qnan); 797 b_cls = (!floatx80_is_any_nan(b) 798 ? float_class_normal 799 : floatx80_is_signaling_nan(b, status) 800 ? float_class_snan 801 : float_class_qnan); 802 803 if (is_snan(a_cls) || is_snan(b_cls)) { 804 float_raise(float_flag_invalid, status); 805 } 806 807 if (status->default_nan_mode) { 808 return floatx80_default_nan(status); 809 } 810 811 if (a.low < b.low) { 812 aIsLargerSignificand = 0; 813 } else if (b.low < a.low) { 814 aIsLargerSignificand = 1; 815 } else { 816 aIsLargerSignificand = (a.high < b.high) ? 1 : 0; 817 } 818 819 if (pickNaN(a_cls, b_cls, aIsLargerSignificand, status)) { 820 if (is_snan(b_cls)) { 821 return floatx80_silence_nan(b, status); 822 } 823 return b; 824 } else { 825 if (is_snan(a_cls)) { 826 return floatx80_silence_nan(a, status); 827 } 828 return a; 829 } 830} 831 832/*---------------------------------------------------------------------------- 833| Returns 1 if the quadruple-precision floating-point value `a' is a quiet 834| NaN; otherwise returns 0. 835*----------------------------------------------------------------------------*/ 836 837bool float128_is_quiet_nan(float128 a, float_status *status) 838{ 839 if (no_signaling_nans(status)) { 840 return float128_is_any_nan(a); 841 } else { 842 if (snan_bit_is_one(status)) { 843 return (((a.high >> 47) & 0xFFFF) == 0xFFFE) 844 && (a.low || (a.high & 0x00007FFFFFFFFFFFULL)); 845 } else { 846 return ((a.high << 1) >= 0xFFFF000000000000ULL) 847 && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL)); 848 } 849 } 850} 851 852/*---------------------------------------------------------------------------- 853| Returns 1 if the quadruple-precision floating-point value `a' is a 854| signaling NaN; otherwise returns 0. 855*----------------------------------------------------------------------------*/ 856 857bool float128_is_signaling_nan(float128 a, float_status *status) 858{ 859 if (no_signaling_nans(status)) { 860 return 0; 861 } else { 862 if (snan_bit_is_one(status)) { 863 return ((a.high << 1) >= 0xFFFF000000000000ULL) 864 && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL)); 865 } else { 866 return (((a.high >> 47) & 0xFFFF) == 0xFFFE) 867 && (a.low || (a.high & UINT64_C(0x00007FFFFFFFFFFF))); 868 } 869 } 870} 871