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 18static void partsN(return_nan)(FloatPartsN *a, float_status *s) 19{ 20 switch (a->cls) { 21 case float_class_snan: 22 float_raise(float_flag_invalid, s); 23 if (s->default_nan_mode) { 24 parts_default_nan(a, s); 25 } else { 26 parts_silence_nan(a, s); 27 } 28 break; 29 case float_class_qnan: 30 if (s->default_nan_mode) { 31 parts_default_nan(a, s); 32 } 33 break; 34 default: 35 g_assert_not_reached(); 36 } 37} 38 39static FloatPartsN *partsN(pick_nan)(FloatPartsN *a, FloatPartsN *b, 40 float_status *s) 41{ 42 if (is_snan(a->cls) || is_snan(b->cls)) { 43 float_raise(float_flag_invalid, s); 44 } 45 46 if (s->default_nan_mode) { 47 parts_default_nan(a, s); 48 } else { 49 int cmp = frac_cmp(a, b); 50 if (cmp == 0) { 51 cmp = a->sign < b->sign; 52 } 53 54 if (pickNaN(a->cls, b->cls, cmp > 0, s)) { 55 a = b; 56 } 57 if (is_snan(a->cls)) { 58 parts_silence_nan(a, s); 59 } 60 } 61 return a; 62} 63 64static FloatPartsN *partsN(pick_nan_muladd)(FloatPartsN *a, FloatPartsN *b, 65 FloatPartsN *c, float_status *s, 66 int ab_mask, int abc_mask) 67{ 68 int which; 69 70 if (unlikely(abc_mask & float_cmask_snan)) { 71 float_raise(float_flag_invalid, s); 72 } 73 74 which = pickNaNMulAdd(a->cls, b->cls, c->cls, 75 ab_mask == float_cmask_infzero, s); 76 77 if (s->default_nan_mode || which == 3) { 78 /* 79 * Note that this check is after pickNaNMulAdd so that function 80 * has an opportunity to set the Invalid flag for infzero. 81 */ 82 parts_default_nan(a, s); 83 return a; 84 } 85 86 switch (which) { 87 case 0: 88 break; 89 case 1: 90 a = b; 91 break; 92 case 2: 93 a = c; 94 break; 95 default: 96 g_assert_not_reached(); 97 } 98 if (is_snan(a->cls)) { 99 parts_silence_nan(a, s); 100 } 101 return a; 102} 103 104/* 105 * Canonicalize the FloatParts structure. Determine the class, 106 * unbias the exponent, and normalize the fraction. 107 */ 108static void partsN(canonicalize)(FloatPartsN *p, float_status *status, 109 const FloatFmt *fmt) 110{ 111 if (unlikely(p->exp == 0)) { 112 if (likely(frac_eqz(p))) { 113 p->cls = float_class_zero; 114 } else if (status->flush_inputs_to_zero) { 115 float_raise(float_flag_input_denormal, status); 116 p->cls = float_class_zero; 117 frac_clear(p); 118 } else { 119 int shift = frac_normalize(p); 120 p->cls = float_class_normal; 121 p->exp = fmt->frac_shift - fmt->exp_bias - shift + 1; 122 } 123 } else if (likely(p->exp < fmt->exp_max) || fmt->arm_althp) { 124 p->cls = float_class_normal; 125 p->exp -= fmt->exp_bias; 126 frac_shl(p, fmt->frac_shift); 127 p->frac_hi |= DECOMPOSED_IMPLICIT_BIT; 128 } else if (likely(frac_eqz(p))) { 129 p->cls = float_class_inf; 130 } else { 131 frac_shl(p, fmt->frac_shift); 132 p->cls = (parts_is_snan_frac(p->frac_hi, status) 133 ? float_class_snan : float_class_qnan); 134 } 135} 136 137/* 138 * Round and uncanonicalize a floating-point number by parts. There 139 * are FRAC_SHIFT bits that may require rounding at the bottom of the 140 * fraction; these bits will be removed. The exponent will be biased 141 * by EXP_BIAS and must be bounded by [EXP_MAX-1, 0]. 142 */ 143static void partsN(uncanon_normal)(FloatPartsN *p, float_status *s, 144 const FloatFmt *fmt) 145{ 146 const int exp_max = fmt->exp_max; 147 const int frac_shift = fmt->frac_shift; 148 const uint64_t round_mask = fmt->round_mask; 149 const uint64_t frac_lsb = round_mask + 1; 150 const uint64_t frac_lsbm1 = round_mask ^ (round_mask >> 1); 151 const uint64_t roundeven_mask = round_mask | frac_lsb; 152 uint64_t inc; 153 bool overflow_norm = false; 154 int exp, flags = 0; 155 156 switch (s->float_rounding_mode) { 157 case float_round_nearest_even: 158 if (N > 64 && frac_lsb == 0) { 159 inc = ((p->frac_hi & 1) || (p->frac_lo & round_mask) != frac_lsbm1 160 ? frac_lsbm1 : 0); 161 } else { 162 inc = ((p->frac_lo & roundeven_mask) != frac_lsbm1 163 ? frac_lsbm1 : 0); 164 } 165 break; 166 case float_round_ties_away: 167 inc = frac_lsbm1; 168 break; 169 case float_round_to_zero: 170 overflow_norm = true; 171 inc = 0; 172 break; 173 case float_round_up: 174 inc = p->sign ? 0 : round_mask; 175 overflow_norm = p->sign; 176 break; 177 case float_round_down: 178 inc = p->sign ? round_mask : 0; 179 overflow_norm = !p->sign; 180 break; 181 case float_round_to_odd: 182 overflow_norm = true; 183 /* fall through */ 184 case float_round_to_odd_inf: 185 if (N > 64 && frac_lsb == 0) { 186 inc = p->frac_hi & 1 ? 0 : round_mask; 187 } else { 188 inc = p->frac_lo & frac_lsb ? 0 : round_mask; 189 } 190 break; 191 default: 192 g_assert_not_reached(); 193 } 194 195 exp = p->exp + fmt->exp_bias; 196 if (likely(exp > 0)) { 197 if (p->frac_lo & round_mask) { 198 flags |= float_flag_inexact; 199 if (frac_addi(p, p, inc)) { 200 frac_shr(p, 1); 201 p->frac_hi |= DECOMPOSED_IMPLICIT_BIT; 202 exp++; 203 } 204 p->frac_lo &= ~round_mask; 205 } 206 207 if (fmt->arm_althp) { 208 /* ARM Alt HP eschews Inf and NaN for a wider exponent. */ 209 if (unlikely(exp > exp_max)) { 210 /* Overflow. Return the maximum normal. */ 211 flags = float_flag_invalid; 212 exp = exp_max; 213 frac_allones(p); 214 p->frac_lo &= ~round_mask; 215 } 216 } else if (unlikely(exp >= exp_max)) { 217 flags |= float_flag_overflow | float_flag_inexact; 218 if (overflow_norm) { 219 exp = exp_max - 1; 220 frac_allones(p); 221 p->frac_lo &= ~round_mask; 222 } else { 223 p->cls = float_class_inf; 224 exp = exp_max; 225 frac_clear(p); 226 } 227 } 228 frac_shr(p, frac_shift); 229 } else if (s->flush_to_zero) { 230 flags |= float_flag_output_denormal; 231 p->cls = float_class_zero; 232 exp = 0; 233 frac_clear(p); 234 } else { 235 bool is_tiny = s->tininess_before_rounding || exp < 0; 236 237 if (!is_tiny) { 238 FloatPartsN discard; 239 is_tiny = !frac_addi(&discard, p, inc); 240 } 241 242 frac_shrjam(p, 1 - exp); 243 244 if (p->frac_lo & round_mask) { 245 /* Need to recompute round-to-even/round-to-odd. */ 246 switch (s->float_rounding_mode) { 247 case float_round_nearest_even: 248 if (N > 64 && frac_lsb == 0) { 249 inc = ((p->frac_hi & 1) || 250 (p->frac_lo & round_mask) != frac_lsbm1 251 ? frac_lsbm1 : 0); 252 } else { 253 inc = ((p->frac_lo & roundeven_mask) != frac_lsbm1 254 ? frac_lsbm1 : 0); 255 } 256 break; 257 case float_round_to_odd: 258 case float_round_to_odd_inf: 259 if (N > 64 && frac_lsb == 0) { 260 inc = p->frac_hi & 1 ? 0 : round_mask; 261 } else { 262 inc = p->frac_lo & frac_lsb ? 0 : round_mask; 263 } 264 break; 265 default: 266 break; 267 } 268 flags |= float_flag_inexact; 269 frac_addi(p, p, inc); 270 p->frac_lo &= ~round_mask; 271 } 272 273 exp = (p->frac_hi & DECOMPOSED_IMPLICIT_BIT) != 0; 274 frac_shr(p, frac_shift); 275 276 if (is_tiny && (flags & float_flag_inexact)) { 277 flags |= float_flag_underflow; 278 } 279 if (exp == 0 && frac_eqz(p)) { 280 p->cls = float_class_zero; 281 } 282 } 283 p->exp = exp; 284 float_raise(flags, s); 285} 286 287static void partsN(uncanon)(FloatPartsN *p, float_status *s, 288 const FloatFmt *fmt) 289{ 290 if (likely(p->cls == float_class_normal)) { 291 parts_uncanon_normal(p, s, fmt); 292 } else { 293 switch (p->cls) { 294 case float_class_zero: 295 p->exp = 0; 296 frac_clear(p); 297 return; 298 case float_class_inf: 299 g_assert(!fmt->arm_althp); 300 p->exp = fmt->exp_max; 301 frac_clear(p); 302 return; 303 case float_class_qnan: 304 case float_class_snan: 305 g_assert(!fmt->arm_althp); 306 p->exp = fmt->exp_max; 307 frac_shr(p, fmt->frac_shift); 308 return; 309 default: 310 break; 311 } 312 g_assert_not_reached(); 313 } 314} 315 316/* 317 * Returns the result of adding or subtracting the values of the 318 * floating-point values `a' and `b'. The operation is performed 319 * according to the IEC/IEEE Standard for Binary Floating-Point 320 * Arithmetic. 321 */ 322static FloatPartsN *partsN(addsub)(FloatPartsN *a, FloatPartsN *b, 323 float_status *s, bool subtract) 324{ 325 bool b_sign = b->sign ^ subtract; 326 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls); 327 328 if (a->sign != b_sign) { 329 /* Subtraction */ 330 if (likely(ab_mask == float_cmask_normal)) { 331 if (parts_sub_normal(a, b)) { 332 return a; 333 } 334 /* Subtract was exact, fall through to set sign. */ 335 ab_mask = float_cmask_zero; 336 } 337 338 if (ab_mask == float_cmask_zero) { 339 a->sign = s->float_rounding_mode == float_round_down; 340 return a; 341 } 342 343 if (unlikely(ab_mask & float_cmask_anynan)) { 344 goto p_nan; 345 } 346 347 if (ab_mask & float_cmask_inf) { 348 if (a->cls != float_class_inf) { 349 /* N - Inf */ 350 goto return_b; 351 } 352 if (b->cls != float_class_inf) { 353 /* Inf - N */ 354 return a; 355 } 356 /* Inf - Inf */ 357 float_raise(float_flag_invalid | float_flag_invalid_isi, s); 358 parts_default_nan(a, s); 359 return a; 360 } 361 } else { 362 /* Addition */ 363 if (likely(ab_mask == float_cmask_normal)) { 364 parts_add_normal(a, b); 365 return a; 366 } 367 368 if (ab_mask == float_cmask_zero) { 369 return a; 370 } 371 372 if (unlikely(ab_mask & float_cmask_anynan)) { 373 goto p_nan; 374 } 375 376 if (ab_mask & float_cmask_inf) { 377 a->cls = float_class_inf; 378 return a; 379 } 380 } 381 382 if (b->cls == float_class_zero) { 383 g_assert(a->cls == float_class_normal); 384 return a; 385 } 386 387 g_assert(a->cls == float_class_zero); 388 g_assert(b->cls == float_class_normal); 389 return_b: 390 b->sign = b_sign; 391 return b; 392 393 p_nan: 394 return parts_pick_nan(a, b, s); 395} 396 397/* 398 * Returns the result of multiplying the floating-point values `a' and 399 * `b'. The operation is performed according to the IEC/IEEE Standard 400 * for Binary Floating-Point Arithmetic. 401 */ 402static FloatPartsN *partsN(mul)(FloatPartsN *a, FloatPartsN *b, 403 float_status *s) 404{ 405 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls); 406 bool sign = a->sign ^ b->sign; 407 408 if (likely(ab_mask == float_cmask_normal)) { 409 FloatPartsW tmp; 410 411 frac_mulw(&tmp, a, b); 412 frac_truncjam(a, &tmp); 413 414 a->exp += b->exp + 1; 415 if (!(a->frac_hi & DECOMPOSED_IMPLICIT_BIT)) { 416 frac_add(a, a, a); 417 a->exp -= 1; 418 } 419 420 a->sign = sign; 421 return a; 422 } 423 424 /* Inf * Zero == NaN */ 425 if (unlikely(ab_mask == float_cmask_infzero)) { 426 float_raise(float_flag_invalid | float_flag_invalid_imz, s); 427 parts_default_nan(a, s); 428 return a; 429 } 430 431 if (unlikely(ab_mask & float_cmask_anynan)) { 432 return parts_pick_nan(a, b, s); 433 } 434 435 /* Multiply by 0 or Inf */ 436 if (ab_mask & float_cmask_inf) { 437 a->cls = float_class_inf; 438 a->sign = sign; 439 return a; 440 } 441 442 g_assert(ab_mask & float_cmask_zero); 443 a->cls = float_class_zero; 444 a->sign = sign; 445 return a; 446} 447 448/* 449 * Returns the result of multiplying the floating-point values `a' and 450 * `b' then adding 'c', with no intermediate rounding step after the 451 * multiplication. The operation is performed according to the 452 * IEC/IEEE Standard for Binary Floating-Point Arithmetic 754-2008. 453 * The flags argument allows the caller to select negation of the 454 * addend, the intermediate product, or the final result. (The 455 * difference between this and having the caller do a separate 456 * negation is that negating externally will flip the sign bit on NaNs.) 457 * 458 * Requires A and C extracted into a double-sized structure to provide the 459 * extra space for the widening multiply. 460 */ 461static FloatPartsN *partsN(muladd)(FloatPartsN *a, FloatPartsN *b, 462 FloatPartsN *c, int flags, float_status *s) 463{ 464 int ab_mask, abc_mask; 465 FloatPartsW p_widen, c_widen; 466 467 ab_mask = float_cmask(a->cls) | float_cmask(b->cls); 468 abc_mask = float_cmask(c->cls) | ab_mask; 469 470 /* 471 * It is implementation-defined whether the cases of (0,inf,qnan) 472 * and (inf,0,qnan) raise InvalidOperation or not (and what QNaN 473 * they return if they do), so we have to hand this information 474 * off to the target-specific pick-a-NaN routine. 475 */ 476 if (unlikely(abc_mask & float_cmask_anynan)) { 477 return parts_pick_nan_muladd(a, b, c, s, ab_mask, abc_mask); 478 } 479 480 if (flags & float_muladd_negate_c) { 481 c->sign ^= 1; 482 } 483 484 /* Compute the sign of the product into A. */ 485 a->sign ^= b->sign; 486 if (flags & float_muladd_negate_product) { 487 a->sign ^= 1; 488 } 489 490 if (unlikely(ab_mask != float_cmask_normal)) { 491 if (unlikely(ab_mask == float_cmask_infzero)) { 492 float_raise(float_flag_invalid | float_flag_invalid_imz, s); 493 goto d_nan; 494 } 495 496 if (ab_mask & float_cmask_inf) { 497 if (c->cls == float_class_inf && a->sign != c->sign) { 498 float_raise(float_flag_invalid | float_flag_invalid_isi, s); 499 goto d_nan; 500 } 501 goto return_inf; 502 } 503 504 g_assert(ab_mask & float_cmask_zero); 505 if (c->cls == float_class_normal) { 506 *a = *c; 507 goto return_normal; 508 } 509 if (c->cls == float_class_zero) { 510 if (a->sign != c->sign) { 511 goto return_sub_zero; 512 } 513 goto return_zero; 514 } 515 g_assert(c->cls == float_class_inf); 516 } 517 518 if (unlikely(c->cls == float_class_inf)) { 519 a->sign = c->sign; 520 goto return_inf; 521 } 522 523 /* Perform the multiplication step. */ 524 p_widen.sign = a->sign; 525 p_widen.exp = a->exp + b->exp + 1; 526 frac_mulw(&p_widen, a, b); 527 if (!(p_widen.frac_hi & DECOMPOSED_IMPLICIT_BIT)) { 528 frac_add(&p_widen, &p_widen, &p_widen); 529 p_widen.exp -= 1; 530 } 531 532 /* Perform the addition step. */ 533 if (c->cls != float_class_zero) { 534 /* Zero-extend C to less significant bits. */ 535 frac_widen(&c_widen, c); 536 c_widen.exp = c->exp; 537 538 if (a->sign == c->sign) { 539 parts_add_normal(&p_widen, &c_widen); 540 } else if (!parts_sub_normal(&p_widen, &c_widen)) { 541 goto return_sub_zero; 542 } 543 } 544 545 /* Narrow with sticky bit, for proper rounding later. */ 546 frac_truncjam(a, &p_widen); 547 a->sign = p_widen.sign; 548 a->exp = p_widen.exp; 549 550 return_normal: 551 if (flags & float_muladd_halve_result) { 552 a->exp -= 1; 553 } 554 finish_sign: 555 if (flags & float_muladd_negate_result) { 556 a->sign ^= 1; 557 } 558 return a; 559 560 return_sub_zero: 561 a->sign = s->float_rounding_mode == float_round_down; 562 return_zero: 563 a->cls = float_class_zero; 564 goto finish_sign; 565 566 return_inf: 567 a->cls = float_class_inf; 568 goto finish_sign; 569 570 d_nan: 571 parts_default_nan(a, s); 572 return a; 573} 574 575/* 576 * Returns the result of dividing the floating-point value `a' by the 577 * corresponding value `b'. The operation is performed according to 578 * the IEC/IEEE Standard for Binary Floating-Point Arithmetic. 579 */ 580static FloatPartsN *partsN(div)(FloatPartsN *a, FloatPartsN *b, 581 float_status *s) 582{ 583 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls); 584 bool sign = a->sign ^ b->sign; 585 586 if (likely(ab_mask == float_cmask_normal)) { 587 a->sign = sign; 588 a->exp -= b->exp + frac_div(a, b); 589 return a; 590 } 591 592 /* 0/0 or Inf/Inf => NaN */ 593 if (unlikely(ab_mask == float_cmask_zero) || 594 unlikely(ab_mask == float_cmask_inf)) { 595 float_raise(float_flag_invalid, s); 596 parts_default_nan(a, s); 597 return a; 598 } 599 600 /* All the NaN cases */ 601 if (unlikely(ab_mask & float_cmask_anynan)) { 602 return parts_pick_nan(a, b, s); 603 } 604 605 a->sign = sign; 606 607 /* Inf / X */ 608 if (a->cls == float_class_inf) { 609 return a; 610 } 611 612 /* 0 / X */ 613 if (a->cls == float_class_zero) { 614 return a; 615 } 616 617 /* X / Inf */ 618 if (b->cls == float_class_inf) { 619 a->cls = float_class_zero; 620 return a; 621 } 622 623 /* X / 0 => Inf */ 624 g_assert(b->cls == float_class_zero); 625 float_raise(float_flag_divbyzero, s); 626 a->cls = float_class_inf; 627 return a; 628} 629 630/* 631 * Floating point remainder, per IEC/IEEE, or modulus. 632 */ 633static FloatPartsN *partsN(modrem)(FloatPartsN *a, FloatPartsN *b, 634 uint64_t *mod_quot, float_status *s) 635{ 636 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls); 637 638 if (likely(ab_mask == float_cmask_normal)) { 639 frac_modrem(a, b, mod_quot); 640 return a; 641 } 642 643 if (mod_quot) { 644 *mod_quot = 0; 645 } 646 647 /* All the NaN cases */ 648 if (unlikely(ab_mask & float_cmask_anynan)) { 649 return parts_pick_nan(a, b, s); 650 } 651 652 /* Inf % N; N % 0 */ 653 if (a->cls == float_class_inf || b->cls == float_class_zero) { 654 float_raise(float_flag_invalid, s); 655 parts_default_nan(a, s); 656 return a; 657 } 658 659 /* N % Inf; 0 % N */ 660 g_assert(b->cls == float_class_inf || a->cls == float_class_zero); 661 return a; 662} 663 664/* 665 * Square Root 666 * 667 * The base algorithm is lifted from 668 * https://git.musl-libc.org/cgit/musl/tree/src/math/sqrtf.c 669 * https://git.musl-libc.org/cgit/musl/tree/src/math/sqrt.c 670 * https://git.musl-libc.org/cgit/musl/tree/src/math/sqrtl.c 671 * and is thus MIT licenced. 672 */ 673static void partsN(sqrt)(FloatPartsN *a, float_status *status, 674 const FloatFmt *fmt) 675{ 676 const uint32_t three32 = 3u << 30; 677 const uint64_t three64 = 3ull << 62; 678 uint32_t d32, m32, r32, s32, u32; /* 32-bit computation */ 679 uint64_t d64, m64, r64, s64, u64; /* 64-bit computation */ 680 uint64_t dh, dl, rh, rl, sh, sl, uh, ul; /* 128-bit computation */ 681 uint64_t d0h, d0l, d1h, d1l, d2h, d2l; 682 uint64_t discard; 683 bool exp_odd; 684 size_t index; 685 686 if (unlikely(a->cls != float_class_normal)) { 687 switch (a->cls) { 688 case float_class_snan: 689 case float_class_qnan: 690 parts_return_nan(a, status); 691 return; 692 case float_class_zero: 693 return; 694 case float_class_inf: 695 if (unlikely(a->sign)) { 696 goto d_nan; 697 } 698 return; 699 default: 700 g_assert_not_reached(); 701 } 702 } 703 704 if (unlikely(a->sign)) { 705 goto d_nan; 706 } 707 708 /* 709 * Argument reduction. 710 * x = 4^e frac; with integer e, and frac in [1, 4) 711 * m = frac fixed point at bit 62, since we're in base 4. 712 * If base-2 exponent is odd, exchange that for multiply by 2, 713 * which results in no shift. 714 */ 715 exp_odd = a->exp & 1; 716 index = extract64(a->frac_hi, 57, 6) | (!exp_odd << 6); 717 if (!exp_odd) { 718 frac_shr(a, 1); 719 } 720 721 /* 722 * Approximate r ~= 1/sqrt(m) and s ~= sqrt(m) when m in [1, 4). 723 * 724 * Initial estimate: 725 * 7-bit lookup table (1-bit exponent and 6-bit significand). 726 * 727 * The relative error (e = r0*sqrt(m)-1) of a linear estimate 728 * (r0 = a*m + b) is |e| < 0.085955 ~ 0x1.6p-4 at best; 729 * a table lookup is faster and needs one less iteration. 730 * The 7-bit table gives |e| < 0x1.fdp-9. 731 * 732 * A Newton-Raphson iteration for r is 733 * s = m*r 734 * d = s*r 735 * u = 3 - d 736 * r = r*u/2 737 * 738 * Fixed point representations: 739 * m, s, d, u, three are all 2.30; r is 0.32 740 */ 741 m64 = a->frac_hi; 742 m32 = m64 >> 32; 743 744 r32 = rsqrt_tab[index] << 16; 745 /* |r*sqrt(m) - 1| < 0x1.FDp-9 */ 746 747 s32 = ((uint64_t)m32 * r32) >> 32; 748 d32 = ((uint64_t)s32 * r32) >> 32; 749 u32 = three32 - d32; 750 751 if (N == 64) { 752 /* float64 or smaller */ 753 754 r32 = ((uint64_t)r32 * u32) >> 31; 755 /* |r*sqrt(m) - 1| < 0x1.7Bp-16 */ 756 757 s32 = ((uint64_t)m32 * r32) >> 32; 758 d32 = ((uint64_t)s32 * r32) >> 32; 759 u32 = three32 - d32; 760 761 if (fmt->frac_size <= 23) { 762 /* float32 or smaller */ 763 764 s32 = ((uint64_t)s32 * u32) >> 32; /* 3.29 */ 765 s32 = (s32 - 1) >> 6; /* 9.23 */ 766 /* s < sqrt(m) < s + 0x1.08p-23 */ 767 768 /* compute nearest rounded result to 2.23 bits */ 769 uint32_t d0 = (m32 << 16) - s32 * s32; 770 uint32_t d1 = s32 - d0; 771 uint32_t d2 = d1 + s32 + 1; 772 s32 += d1 >> 31; 773 a->frac_hi = (uint64_t)s32 << (64 - 25); 774 775 /* increment or decrement for inexact */ 776 if (d2 != 0) { 777 a->frac_hi += ((int32_t)(d1 ^ d2) < 0 ? -1 : 1); 778 } 779 goto done; 780 } 781 782 /* float64 */ 783 784 r64 = (uint64_t)r32 * u32 * 2; 785 /* |r*sqrt(m) - 1| < 0x1.37-p29; convert to 64-bit arithmetic */ 786 mul64To128(m64, r64, &s64, &discard); 787 mul64To128(s64, r64, &d64, &discard); 788 u64 = three64 - d64; 789 790 mul64To128(s64, u64, &s64, &discard); /* 3.61 */ 791 s64 = (s64 - 2) >> 9; /* 12.52 */ 792 793 /* Compute nearest rounded result */ 794 uint64_t d0 = (m64 << 42) - s64 * s64; 795 uint64_t d1 = s64 - d0; 796 uint64_t d2 = d1 + s64 + 1; 797 s64 += d1 >> 63; 798 a->frac_hi = s64 << (64 - 54); 799 800 /* increment or decrement for inexact */ 801 if (d2 != 0) { 802 a->frac_hi += ((int64_t)(d1 ^ d2) < 0 ? -1 : 1); 803 } 804 goto done; 805 } 806 807 r64 = (uint64_t)r32 * u32 * 2; 808 /* |r*sqrt(m) - 1| < 0x1.7Bp-16; convert to 64-bit arithmetic */ 809 810 mul64To128(m64, r64, &s64, &discard); 811 mul64To128(s64, r64, &d64, &discard); 812 u64 = three64 - d64; 813 mul64To128(u64, r64, &r64, &discard); 814 r64 <<= 1; 815 /* |r*sqrt(m) - 1| < 0x1.a5p-31 */ 816 817 mul64To128(m64, r64, &s64, &discard); 818 mul64To128(s64, r64, &d64, &discard); 819 u64 = three64 - d64; 820 mul64To128(u64, r64, &rh, &rl); 821 add128(rh, rl, rh, rl, &rh, &rl); 822 /* |r*sqrt(m) - 1| < 0x1.c001p-59; change to 128-bit arithmetic */ 823 824 mul128To256(a->frac_hi, a->frac_lo, rh, rl, &sh, &sl, &discard, &discard); 825 mul128To256(sh, sl, rh, rl, &dh, &dl, &discard, &discard); 826 sub128(three64, 0, dh, dl, &uh, &ul); 827 mul128To256(uh, ul, sh, sl, &sh, &sl, &discard, &discard); /* 3.125 */ 828 /* -0x1p-116 < s - sqrt(m) < 0x3.8001p-125 */ 829 830 sub128(sh, sl, 0, 4, &sh, &sl); 831 shift128Right(sh, sl, 13, &sh, &sl); /* 16.112 */ 832 /* s < sqrt(m) < s + 1ulp */ 833 834 /* Compute nearest rounded result */ 835 mul64To128(sl, sl, &d0h, &d0l); 836 d0h += 2 * sh * sl; 837 sub128(a->frac_lo << 34, 0, d0h, d0l, &d0h, &d0l); 838 sub128(sh, sl, d0h, d0l, &d1h, &d1l); 839 add128(sh, sl, 0, 1, &d2h, &d2l); 840 add128(d2h, d2l, d1h, d1l, &d2h, &d2l); 841 add128(sh, sl, 0, d1h >> 63, &sh, &sl); 842 shift128Left(sh, sl, 128 - 114, &sh, &sl); 843 844 /* increment or decrement for inexact */ 845 if (d2h | d2l) { 846 if ((int64_t)(d1h ^ d2h) < 0) { 847 sub128(sh, sl, 0, 1, &sh, &sl); 848 } else { 849 add128(sh, sl, 0, 1, &sh, &sl); 850 } 851 } 852 a->frac_lo = sl; 853 a->frac_hi = sh; 854 855 done: 856 /* Convert back from base 4 to base 2. */ 857 a->exp >>= 1; 858 if (!(a->frac_hi & DECOMPOSED_IMPLICIT_BIT)) { 859 frac_add(a, a, a); 860 } else { 861 a->exp += 1; 862 } 863 return; 864 865 d_nan: 866 float_raise(float_flag_invalid, status); 867 parts_default_nan(a, status); 868} 869 870/* 871 * Rounds the floating-point value `a' to an integer, and returns the 872 * result as a floating-point value. The operation is performed 873 * according to the IEC/IEEE Standard for Binary Floating-Point 874 * Arithmetic. 875 * 876 * parts_round_to_int_normal is an internal helper function for 877 * normal numbers only, returning true for inexact but not directly 878 * raising float_flag_inexact. 879 */ 880static bool partsN(round_to_int_normal)(FloatPartsN *a, FloatRoundMode rmode, 881 int scale, int frac_size) 882{ 883 uint64_t frac_lsb, frac_lsbm1, rnd_even_mask, rnd_mask, inc; 884 int shift_adj; 885 886 scale = MIN(MAX(scale, -0x10000), 0x10000); 887 a->exp += scale; 888 889 if (a->exp < 0) { 890 bool one; 891 892 /* All fractional */ 893 switch (rmode) { 894 case float_round_nearest_even: 895 one = false; 896 if (a->exp == -1) { 897 FloatPartsN tmp; 898 /* Shift left one, discarding DECOMPOSED_IMPLICIT_BIT */ 899 frac_add(&tmp, a, a); 900 /* Anything remaining means frac > 0.5. */ 901 one = !frac_eqz(&tmp); 902 } 903 break; 904 case float_round_ties_away: 905 one = a->exp == -1; 906 break; 907 case float_round_to_zero: 908 one = false; 909 break; 910 case float_round_up: 911 one = !a->sign; 912 break; 913 case float_round_down: 914 one = a->sign; 915 break; 916 case float_round_to_odd: 917 one = true; 918 break; 919 default: 920 g_assert_not_reached(); 921 } 922 923 frac_clear(a); 924 a->exp = 0; 925 if (one) { 926 a->frac_hi = DECOMPOSED_IMPLICIT_BIT; 927 } else { 928 a->cls = float_class_zero; 929 } 930 return true; 931 } 932 933 if (a->exp >= frac_size) { 934 /* All integral */ 935 return false; 936 } 937 938 if (N > 64 && a->exp < N - 64) { 939 /* 940 * Rounding is not in the low word -- shift lsb to bit 2, 941 * which leaves room for sticky and rounding bit. 942 */ 943 shift_adj = (N - 1) - (a->exp + 2); 944 frac_shrjam(a, shift_adj); 945 frac_lsb = 1 << 2; 946 } else { 947 shift_adj = 0; 948 frac_lsb = DECOMPOSED_IMPLICIT_BIT >> (a->exp & 63); 949 } 950 951 frac_lsbm1 = frac_lsb >> 1; 952 rnd_mask = frac_lsb - 1; 953 rnd_even_mask = rnd_mask | frac_lsb; 954 955 if (!(a->frac_lo & rnd_mask)) { 956 /* Fractional bits already clear, undo the shift above. */ 957 frac_shl(a, shift_adj); 958 return false; 959 } 960 961 switch (rmode) { 962 case float_round_nearest_even: 963 inc = ((a->frac_lo & rnd_even_mask) != frac_lsbm1 ? frac_lsbm1 : 0); 964 break; 965 case float_round_ties_away: 966 inc = frac_lsbm1; 967 break; 968 case float_round_to_zero: 969 inc = 0; 970 break; 971 case float_round_up: 972 inc = a->sign ? 0 : rnd_mask; 973 break; 974 case float_round_down: 975 inc = a->sign ? rnd_mask : 0; 976 break; 977 case float_round_to_odd: 978 inc = a->frac_lo & frac_lsb ? 0 : rnd_mask; 979 break; 980 default: 981 g_assert_not_reached(); 982 } 983 984 if (shift_adj == 0) { 985 if (frac_addi(a, a, inc)) { 986 frac_shr(a, 1); 987 a->frac_hi |= DECOMPOSED_IMPLICIT_BIT; 988 a->exp++; 989 } 990 a->frac_lo &= ~rnd_mask; 991 } else { 992 frac_addi(a, a, inc); 993 a->frac_lo &= ~rnd_mask; 994 /* Be careful shifting back, not to overflow */ 995 frac_shl(a, shift_adj - 1); 996 if (a->frac_hi & DECOMPOSED_IMPLICIT_BIT) { 997 a->exp++; 998 } else { 999 frac_add(a, a, a); 1000 } 1001 } 1002 return true; 1003} 1004 1005static void partsN(round_to_int)(FloatPartsN *a, FloatRoundMode rmode, 1006 int scale, float_status *s, 1007 const FloatFmt *fmt) 1008{ 1009 switch (a->cls) { 1010 case float_class_qnan: 1011 case float_class_snan: 1012 parts_return_nan(a, s); 1013 break; 1014 case float_class_zero: 1015 case float_class_inf: 1016 break; 1017 case float_class_normal: 1018 if (parts_round_to_int_normal(a, rmode, scale, fmt->frac_size)) { 1019 float_raise(float_flag_inexact, s); 1020 } 1021 break; 1022 default: 1023 g_assert_not_reached(); 1024 } 1025} 1026 1027/* 1028 * Returns the result of converting the floating-point value `a' to 1029 * the two's complement integer format. The conversion is performed 1030 * according to the IEC/IEEE Standard for Binary Floating-Point 1031 * Arithmetic---which means in particular that the conversion is 1032 * rounded according to the current rounding mode. If `a' is a NaN, 1033 * the largest positive integer is returned. Otherwise, if the 1034 * conversion overflows, the largest integer with the same sign as `a' 1035 * is returned. 1036 */ 1037static int64_t partsN(float_to_sint)(FloatPartsN *p, FloatRoundMode rmode, 1038 int scale, int64_t min, int64_t max, 1039 float_status *s) 1040{ 1041 int flags = 0; 1042 uint64_t r; 1043 1044 switch (p->cls) { 1045 case float_class_snan: 1046 case float_class_qnan: 1047 flags = float_flag_invalid; 1048 r = max; 1049 break; 1050 1051 case float_class_inf: 1052 flags = float_flag_invalid; 1053 r = p->sign ? min : max; 1054 break; 1055 1056 case float_class_zero: 1057 return 0; 1058 1059 case float_class_normal: 1060 /* TODO: N - 2 is frac_size for rounding; could use input fmt. */ 1061 if (parts_round_to_int_normal(p, rmode, scale, N - 2)) { 1062 flags = float_flag_inexact; 1063 } 1064 1065 if (p->exp <= DECOMPOSED_BINARY_POINT) { 1066 r = p->frac_hi >> (DECOMPOSED_BINARY_POINT - p->exp); 1067 } else { 1068 r = UINT64_MAX; 1069 } 1070 if (p->sign) { 1071 if (r <= -(uint64_t)min) { 1072 r = -r; 1073 } else { 1074 flags = float_flag_invalid; 1075 r = min; 1076 } 1077 } else if (r > max) { 1078 flags = float_flag_invalid; 1079 r = max; 1080 } 1081 break; 1082 1083 default: 1084 g_assert_not_reached(); 1085 } 1086 1087 float_raise(flags, s); 1088 return r; 1089} 1090 1091/* 1092 * Returns the result of converting the floating-point value `a' to 1093 * the unsigned integer format. The conversion is performed according 1094 * to the IEC/IEEE Standard for Binary Floating-Point 1095 * Arithmetic---which means in particular that the conversion is 1096 * rounded according to the current rounding mode. If `a' is a NaN, 1097 * the largest unsigned integer is returned. Otherwise, if the 1098 * conversion overflows, the largest unsigned integer is returned. If 1099 * the 'a' is negative, the result is rounded and zero is returned; 1100 * values that do not round to zero will raise the inexact exception 1101 * flag. 1102 */ 1103static uint64_t partsN(float_to_uint)(FloatPartsN *p, FloatRoundMode rmode, 1104 int scale, uint64_t max, float_status *s) 1105{ 1106 int flags = 0; 1107 uint64_t r; 1108 1109 switch (p->cls) { 1110 case float_class_snan: 1111 case float_class_qnan: 1112 flags = float_flag_invalid; 1113 r = max; 1114 break; 1115 1116 case float_class_inf: 1117 flags = float_flag_invalid; 1118 r = p->sign ? 0 : max; 1119 break; 1120 1121 case float_class_zero: 1122 return 0; 1123 1124 case float_class_normal: 1125 /* TODO: N - 2 is frac_size for rounding; could use input fmt. */ 1126 if (parts_round_to_int_normal(p, rmode, scale, N - 2)) { 1127 flags = float_flag_inexact; 1128 if (p->cls == float_class_zero) { 1129 r = 0; 1130 break; 1131 } 1132 } 1133 1134 if (p->sign) { 1135 flags = float_flag_invalid; 1136 r = 0; 1137 } else if (p->exp > DECOMPOSED_BINARY_POINT) { 1138 flags = float_flag_invalid; 1139 r = max; 1140 } else { 1141 r = p->frac_hi >> (DECOMPOSED_BINARY_POINT - p->exp); 1142 if (r > max) { 1143 flags = float_flag_invalid; 1144 r = max; 1145 } 1146 } 1147 break; 1148 1149 default: 1150 g_assert_not_reached(); 1151 } 1152 1153 float_raise(flags, s); 1154 return r; 1155} 1156 1157/* 1158 * Integer to float conversions 1159 * 1160 * Returns the result of converting the two's complement integer `a' 1161 * to the floating-point format. The conversion is performed according 1162 * to the IEC/IEEE Standard for Binary Floating-Point Arithmetic. 1163 */ 1164static void partsN(sint_to_float)(FloatPartsN *p, int64_t a, 1165 int scale, float_status *s) 1166{ 1167 uint64_t f = a; 1168 int shift; 1169 1170 memset(p, 0, sizeof(*p)); 1171 1172 if (a == 0) { 1173 p->cls = float_class_zero; 1174 return; 1175 } 1176 1177 p->cls = float_class_normal; 1178 if (a < 0) { 1179 f = -f; 1180 p->sign = true; 1181 } 1182 shift = clz64(f); 1183 scale = MIN(MAX(scale, -0x10000), 0x10000); 1184 1185 p->exp = DECOMPOSED_BINARY_POINT - shift + scale; 1186 p->frac_hi = f << shift; 1187} 1188 1189/* 1190 * Unsigned Integer to float conversions 1191 * 1192 * Returns the result of converting the unsigned integer `a' to the 1193 * floating-point format. The conversion is performed according to the 1194 * IEC/IEEE Standard for Binary Floating-Point Arithmetic. 1195 */ 1196static void partsN(uint_to_float)(FloatPartsN *p, uint64_t a, 1197 int scale, float_status *status) 1198{ 1199 memset(p, 0, sizeof(*p)); 1200 1201 if (a == 0) { 1202 p->cls = float_class_zero; 1203 } else { 1204 int shift = clz64(a); 1205 scale = MIN(MAX(scale, -0x10000), 0x10000); 1206 p->cls = float_class_normal; 1207 p->exp = DECOMPOSED_BINARY_POINT - shift + scale; 1208 p->frac_hi = a << shift; 1209 } 1210} 1211 1212/* 1213 * Float min/max. 1214 */ 1215static FloatPartsN *partsN(minmax)(FloatPartsN *a, FloatPartsN *b, 1216 float_status *s, int flags) 1217{ 1218 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls); 1219 int a_exp, b_exp, cmp; 1220 1221 if (unlikely(ab_mask & float_cmask_anynan)) { 1222 /* 1223 * For minNum/maxNum (IEEE 754-2008) 1224 * or minimumNumber/maximumNumber (IEEE 754-2019), 1225 * if one operand is a QNaN, and the other 1226 * operand is numerical, then return numerical argument. 1227 */ 1228 if ((flags & (minmax_isnum | minmax_isnumber)) 1229 && !(ab_mask & float_cmask_snan) 1230 && (ab_mask & ~float_cmask_qnan)) { 1231 return is_nan(a->cls) ? b : a; 1232 } 1233 1234 /* 1235 * In IEEE 754-2019, minNum, maxNum, minNumMag and maxNumMag 1236 * are removed and replaced with minimum, minimumNumber, maximum 1237 * and maximumNumber. 1238 * minimumNumber/maximumNumber behavior for SNaN is changed to: 1239 * If both operands are NaNs, a QNaN is returned. 1240 * If either operand is a SNaN, 1241 * an invalid operation exception is signaled, 1242 * but unless both operands are NaNs, 1243 * the SNaN is otherwise ignored and not converted to a QNaN. 1244 */ 1245 if ((flags & minmax_isnumber) 1246 && (ab_mask & float_cmask_snan) 1247 && (ab_mask & ~float_cmask_anynan)) { 1248 float_raise(float_flag_invalid, s); 1249 return is_nan(a->cls) ? b : a; 1250 } 1251 1252 return parts_pick_nan(a, b, s); 1253 } 1254 1255 a_exp = a->exp; 1256 b_exp = b->exp; 1257 1258 if (unlikely(ab_mask != float_cmask_normal)) { 1259 switch (a->cls) { 1260 case float_class_normal: 1261 break; 1262 case float_class_inf: 1263 a_exp = INT16_MAX; 1264 break; 1265 case float_class_zero: 1266 a_exp = INT16_MIN; 1267 break; 1268 default: 1269 g_assert_not_reached(); 1270 break; 1271 } 1272 switch (b->cls) { 1273 case float_class_normal: 1274 break; 1275 case float_class_inf: 1276 b_exp = INT16_MAX; 1277 break; 1278 case float_class_zero: 1279 b_exp = INT16_MIN; 1280 break; 1281 default: 1282 g_assert_not_reached(); 1283 break; 1284 } 1285 } 1286 1287 /* Compare magnitudes. */ 1288 cmp = a_exp - b_exp; 1289 if (cmp == 0) { 1290 cmp = frac_cmp(a, b); 1291 } 1292 1293 /* 1294 * Take the sign into account. 1295 * For ismag, only do this if the magnitudes are equal. 1296 */ 1297 if (!(flags & minmax_ismag) || cmp == 0) { 1298 if (a->sign != b->sign) { 1299 /* For differing signs, the negative operand is less. */ 1300 cmp = a->sign ? -1 : 1; 1301 } else if (a->sign) { 1302 /* For two negative operands, invert the magnitude comparison. */ 1303 cmp = -cmp; 1304 } 1305 } 1306 1307 if (flags & minmax_ismin) { 1308 cmp = -cmp; 1309 } 1310 return cmp < 0 ? b : a; 1311} 1312 1313/* 1314 * Floating point compare 1315 */ 1316static FloatRelation partsN(compare)(FloatPartsN *a, FloatPartsN *b, 1317 float_status *s, bool is_quiet) 1318{ 1319 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls); 1320 int cmp; 1321 1322 if (likely(ab_mask == float_cmask_normal)) { 1323 if (a->sign != b->sign) { 1324 goto a_sign; 1325 } 1326 if (a->exp != b->exp) { 1327 cmp = a->exp < b->exp ? -1 : 1; 1328 } else { 1329 cmp = frac_cmp(a, b); 1330 } 1331 if (a->sign) { 1332 cmp = -cmp; 1333 } 1334 return cmp; 1335 } 1336 1337 if (unlikely(ab_mask & float_cmask_anynan)) { 1338 if (!is_quiet || (ab_mask & float_cmask_snan)) { 1339 float_raise(float_flag_invalid, s); 1340 } 1341 return float_relation_unordered; 1342 } 1343 1344 if (ab_mask & float_cmask_zero) { 1345 if (ab_mask == float_cmask_zero) { 1346 return float_relation_equal; 1347 } else if (a->cls == float_class_zero) { 1348 goto b_sign; 1349 } else { 1350 goto a_sign; 1351 } 1352 } 1353 1354 if (ab_mask == float_cmask_inf) { 1355 if (a->sign == b->sign) { 1356 return float_relation_equal; 1357 } 1358 } else if (b->cls == float_class_inf) { 1359 goto b_sign; 1360 } else { 1361 g_assert(a->cls == float_class_inf); 1362 } 1363 1364 a_sign: 1365 return a->sign ? float_relation_less : float_relation_greater; 1366 b_sign: 1367 return b->sign ? float_relation_greater : float_relation_less; 1368} 1369 1370/* 1371 * Multiply A by 2 raised to the power N. 1372 */ 1373static void partsN(scalbn)(FloatPartsN *a, int n, float_status *s) 1374{ 1375 switch (a->cls) { 1376 case float_class_snan: 1377 case float_class_qnan: 1378 parts_return_nan(a, s); 1379 break; 1380 case float_class_zero: 1381 case float_class_inf: 1382 break; 1383 case float_class_normal: 1384 a->exp += MIN(MAX(n, -0x10000), 0x10000); 1385 break; 1386 default: 1387 g_assert_not_reached(); 1388 } 1389} 1390 1391/* 1392 * Return log2(A) 1393 */ 1394static void partsN(log2)(FloatPartsN *a, float_status *s, const FloatFmt *fmt) 1395{ 1396 uint64_t a0, a1, r, t, ign; 1397 FloatPartsN f; 1398 int i, n, a_exp, f_exp; 1399 1400 if (unlikely(a->cls != float_class_normal)) { 1401 switch (a->cls) { 1402 case float_class_snan: 1403 case float_class_qnan: 1404 parts_return_nan(a, s); 1405 return; 1406 case float_class_zero: 1407 /* log2(0) = -inf */ 1408 a->cls = float_class_inf; 1409 a->sign = 1; 1410 return; 1411 case float_class_inf: 1412 if (unlikely(a->sign)) { 1413 goto d_nan; 1414 } 1415 return; 1416 default: 1417 break; 1418 } 1419 g_assert_not_reached(); 1420 } 1421 if (unlikely(a->sign)) { 1422 goto d_nan; 1423 } 1424 1425 /* TODO: This algorithm looses bits too quickly for float128. */ 1426 g_assert(N == 64); 1427 1428 a_exp = a->exp; 1429 f_exp = -1; 1430 1431 r = 0; 1432 t = DECOMPOSED_IMPLICIT_BIT; 1433 a0 = a->frac_hi; 1434 a1 = 0; 1435 1436 n = fmt->frac_size + 2; 1437 if (unlikely(a_exp == -1)) { 1438 /* 1439 * When a_exp == -1, we're computing the log2 of a value [0.5,1.0). 1440 * When the value is very close to 1.0, there are lots of 1's in 1441 * the msb parts of the fraction. At the end, when we subtract 1442 * this value from -1.0, we can see a catastrophic loss of precision, 1443 * as 0x800..000 - 0x7ff..ffx becomes 0x000..00y, leaving only the 1444 * bits of y in the final result. To minimize this, compute as many 1445 * digits as we can. 1446 * ??? This case needs another algorithm to avoid this. 1447 */ 1448 n = fmt->frac_size * 2 + 2; 1449 /* Don't compute a value overlapping the sticky bit */ 1450 n = MIN(n, 62); 1451 } 1452 1453 for (i = 0; i < n; i++) { 1454 if (a1) { 1455 mul128To256(a0, a1, a0, a1, &a0, &a1, &ign, &ign); 1456 } else if (a0 & 0xffffffffull) { 1457 mul64To128(a0, a0, &a0, &a1); 1458 } else if (a0 & ~DECOMPOSED_IMPLICIT_BIT) { 1459 a0 >>= 32; 1460 a0 *= a0; 1461 } else { 1462 goto exact; 1463 } 1464 1465 if (a0 & DECOMPOSED_IMPLICIT_BIT) { 1466 if (unlikely(a_exp == 0 && r == 0)) { 1467 /* 1468 * When a_exp == 0, we're computing the log2 of a value 1469 * [1.0,2.0). When the value is very close to 1.0, there 1470 * are lots of 0's in the msb parts of the fraction. 1471 * We need to compute more digits to produce a correct 1472 * result -- restart at the top of the fraction. 1473 * ??? This is likely to lose precision quickly, as for 1474 * float128; we may need another method. 1475 */ 1476 f_exp -= i; 1477 t = r = DECOMPOSED_IMPLICIT_BIT; 1478 i = 0; 1479 } else { 1480 r |= t; 1481 } 1482 } else { 1483 add128(a0, a1, a0, a1, &a0, &a1); 1484 } 1485 t >>= 1; 1486 } 1487 1488 /* Set sticky for inexact. */ 1489 r |= (a1 || a0 & ~DECOMPOSED_IMPLICIT_BIT); 1490 1491 exact: 1492 parts_sint_to_float(a, a_exp, 0, s); 1493 if (r == 0) { 1494 return; 1495 } 1496 1497 memset(&f, 0, sizeof(f)); 1498 f.cls = float_class_normal; 1499 f.frac_hi = r; 1500 f.exp = f_exp - frac_normalize(&f); 1501 1502 if (a_exp < 0) { 1503 parts_sub_normal(a, &f); 1504 } else if (a_exp > 0) { 1505 parts_add_normal(a, &f); 1506 } else { 1507 *a = f; 1508 } 1509 return; 1510 1511 d_nan: 1512 float_raise(float_flag_invalid, s); 1513 parts_default_nan(a, s); 1514} 1515