1 /* real.c - software floating point emulation. 2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 3 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc. 4 Contributed by Stephen L. Moshier (moshier@world.std.com). 5 Re-written by Richard Henderson <rth@redhat.com> 6 7 This file is part of GCC. 8 9 GCC is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free 11 Software Foundation; either version 3, or (at your option) any later 12 version. 13 14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 15 WARRANTY; without even the implied warranty of MERCHANTABILITY or 16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 17 for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with GCC; see the file COPYING3. If not see 21 <http://www.gnu.org/licenses/>. */ 22 23 #include "config.h" 24 #include "system.h" 25 #include "coretypes.h" 26 #include "tm.h" 27 #include "tree.h" 28 #include "toplev.h" 29 #include "real.h" 30 #include "tm_p.h" 31 #include "dfp.h" 32 33 /* The floating point model used internally is not exactly IEEE 754 34 compliant, and close to the description in the ISO C99 standard, 35 section 5.2.4.2.2 Characteristics of floating types. 36 37 Specifically 38 39 x = s * b^e * \sum_{k=1}^p f_k * b^{-k} 40 41 where 42 s = sign (+- 1) 43 b = base or radix, here always 2 44 e = exponent 45 p = precision (the number of base-b digits in the significand) 46 f_k = the digits of the significand. 47 48 We differ from typical IEEE 754 encodings in that the entire 49 significand is fractional. Normalized significands are in the 50 range [0.5, 1.0). 51 52 A requirement of the model is that P be larger than the largest 53 supported target floating-point type by at least 2 bits. This gives 54 us proper rounding when we truncate to the target type. In addition, 55 E must be large enough to hold the smallest supported denormal number 56 in a normalized form. 57 58 Both of these requirements are easily satisfied. The largest target 59 significand is 113 bits; we store at least 160. The smallest 60 denormal number fits in 17 exponent bits; we store 26. 61 62 Note that the decimal string conversion routines are sensitive to 63 rounding errors. Since the raw arithmetic routines do not themselves 64 have guard digits or rounding, the computation of 10**exp can 65 accumulate more than a few digits of error. The previous incarnation 66 of real.c successfully used a 144-bit fraction; given the current 67 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits. */ 68 69 70 /* Used to classify two numbers simultaneously. */ 71 #define CLASS2(A, B) ((A) << 2 | (B)) 72 73 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32 74 #error "Some constant folding done by hand to avoid shift count warnings" 75 #endif 76 77 static void get_zero (REAL_VALUE_TYPE *, int); 78 static void get_canonical_qnan (REAL_VALUE_TYPE *, int); 79 static void get_canonical_snan (REAL_VALUE_TYPE *, int); 80 static void get_inf (REAL_VALUE_TYPE *, int); 81 static bool sticky_rshift_significand (REAL_VALUE_TYPE *, 82 const REAL_VALUE_TYPE *, unsigned int); 83 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, 84 unsigned int); 85 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, 86 unsigned int); 87 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *); 88 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *, 89 const REAL_VALUE_TYPE *); 90 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, 91 const REAL_VALUE_TYPE *, int); 92 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *); 93 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *); 94 static int cmp_significand_0 (const REAL_VALUE_TYPE *); 95 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int); 96 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int); 97 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int); 98 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int); 99 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, 100 const REAL_VALUE_TYPE *); 101 static void normalize (REAL_VALUE_TYPE *); 102 103 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, 104 const REAL_VALUE_TYPE *, int); 105 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, 106 const REAL_VALUE_TYPE *); 107 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, 108 const REAL_VALUE_TYPE *); 109 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int); 110 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *); 111 112 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *); 113 static void decimal_from_integer (REAL_VALUE_TYPE *); 114 static void decimal_integer_string (char *, const REAL_VALUE_TYPE *, 115 size_t); 116 117 static const REAL_VALUE_TYPE * ten_to_ptwo (int); 118 static const REAL_VALUE_TYPE * ten_to_mptwo (int); 119 static const REAL_VALUE_TYPE * real_digit (int); 120 static void times_pten (REAL_VALUE_TYPE *, int); 121 122 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *); 123 124 /* Initialize R with a positive zero. */ 125 126 static inline void 127 get_zero (REAL_VALUE_TYPE *r, int sign) 128 { 129 memset (r, 0, sizeof (*r)); 130 r->sign = sign; 131 } 132 133 /* Initialize R with the canonical quiet NaN. */ 134 135 static inline void 136 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign) 137 { 138 memset (r, 0, sizeof (*r)); 139 r->cl = rvc_nan; 140 r->sign = sign; 141 r->canonical = 1; 142 } 143 144 static inline void 145 get_canonical_snan (REAL_VALUE_TYPE *r, int sign) 146 { 147 memset (r, 0, sizeof (*r)); 148 r->cl = rvc_nan; 149 r->sign = sign; 150 r->signalling = 1; 151 r->canonical = 1; 152 } 153 154 static inline void 155 get_inf (REAL_VALUE_TYPE *r, int sign) 156 { 157 memset (r, 0, sizeof (*r)); 158 r->cl = rvc_inf; 159 r->sign = sign; 160 } 161 162 163 /* Right-shift the significand of A by N bits; put the result in the 164 significand of R. If any one bits are shifted out, return true. */ 165 166 static bool 167 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a, 168 unsigned int n) 169 { 170 unsigned long sticky = 0; 171 unsigned int i, ofs = 0; 172 173 if (n >= HOST_BITS_PER_LONG) 174 { 175 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i) 176 sticky |= a->sig[i]; 177 n &= HOST_BITS_PER_LONG - 1; 178 } 179 180 if (n != 0) 181 { 182 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1); 183 for (i = 0; i < SIGSZ; ++i) 184 { 185 r->sig[i] 186 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n) 187 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1]) 188 << (HOST_BITS_PER_LONG - n))); 189 } 190 } 191 else 192 { 193 for (i = 0; ofs + i < SIGSZ; ++i) 194 r->sig[i] = a->sig[ofs + i]; 195 for (; i < SIGSZ; ++i) 196 r->sig[i] = 0; 197 } 198 199 return sticky != 0; 200 } 201 202 /* Right-shift the significand of A by N bits; put the result in the 203 significand of R. */ 204 205 static void 206 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a, 207 unsigned int n) 208 { 209 unsigned int i, ofs = n / HOST_BITS_PER_LONG; 210 211 n &= HOST_BITS_PER_LONG - 1; 212 if (n != 0) 213 { 214 for (i = 0; i < SIGSZ; ++i) 215 { 216 r->sig[i] 217 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n) 218 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1]) 219 << (HOST_BITS_PER_LONG - n))); 220 } 221 } 222 else 223 { 224 for (i = 0; ofs + i < SIGSZ; ++i) 225 r->sig[i] = a->sig[ofs + i]; 226 for (; i < SIGSZ; ++i) 227 r->sig[i] = 0; 228 } 229 } 230 231 /* Left-shift the significand of A by N bits; put the result in the 232 significand of R. */ 233 234 static void 235 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a, 236 unsigned int n) 237 { 238 unsigned int i, ofs = n / HOST_BITS_PER_LONG; 239 240 n &= HOST_BITS_PER_LONG - 1; 241 if (n == 0) 242 { 243 for (i = 0; ofs + i < SIGSZ; ++i) 244 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs]; 245 for (; i < SIGSZ; ++i) 246 r->sig[SIGSZ-1-i] = 0; 247 } 248 else 249 for (i = 0; i < SIGSZ; ++i) 250 { 251 r->sig[SIGSZ-1-i] 252 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n) 253 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1]) 254 >> (HOST_BITS_PER_LONG - n))); 255 } 256 } 257 258 /* Likewise, but N is specialized to 1. */ 259 260 static inline void 261 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a) 262 { 263 unsigned int i; 264 265 for (i = SIGSZ - 1; i > 0; --i) 266 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1)); 267 r->sig[0] = a->sig[0] << 1; 268 } 269 270 /* Add the significands of A and B, placing the result in R. Return 271 true if there was carry out of the most significant word. */ 272 273 static inline bool 274 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a, 275 const REAL_VALUE_TYPE *b) 276 { 277 bool carry = false; 278 int i; 279 280 for (i = 0; i < SIGSZ; ++i) 281 { 282 unsigned long ai = a->sig[i]; 283 unsigned long ri = ai + b->sig[i]; 284 285 if (carry) 286 { 287 carry = ri < ai; 288 carry |= ++ri == 0; 289 } 290 else 291 carry = ri < ai; 292 293 r->sig[i] = ri; 294 } 295 296 return carry; 297 } 298 299 /* Subtract the significands of A and B, placing the result in R. CARRY is 300 true if there's a borrow incoming to the least significant word. 301 Return true if there was borrow out of the most significant word. */ 302 303 static inline bool 304 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a, 305 const REAL_VALUE_TYPE *b, int carry) 306 { 307 int i; 308 309 for (i = 0; i < SIGSZ; ++i) 310 { 311 unsigned long ai = a->sig[i]; 312 unsigned long ri = ai - b->sig[i]; 313 314 if (carry) 315 { 316 carry = ri > ai; 317 carry |= ~--ri == 0; 318 } 319 else 320 carry = ri > ai; 321 322 r->sig[i] = ri; 323 } 324 325 return carry; 326 } 327 328 /* Negate the significand A, placing the result in R. */ 329 330 static inline void 331 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a) 332 { 333 bool carry = true; 334 int i; 335 336 for (i = 0; i < SIGSZ; ++i) 337 { 338 unsigned long ri, ai = a->sig[i]; 339 340 if (carry) 341 { 342 if (ai) 343 { 344 ri = -ai; 345 carry = false; 346 } 347 else 348 ri = ai; 349 } 350 else 351 ri = ~ai; 352 353 r->sig[i] = ri; 354 } 355 } 356 357 /* Compare significands. Return tri-state vs zero. */ 358 359 static inline int 360 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b) 361 { 362 int i; 363 364 for (i = SIGSZ - 1; i >= 0; --i) 365 { 366 unsigned long ai = a->sig[i]; 367 unsigned long bi = b->sig[i]; 368 369 if (ai > bi) 370 return 1; 371 if (ai < bi) 372 return -1; 373 } 374 375 return 0; 376 } 377 378 /* Return true if A is nonzero. */ 379 380 static inline int 381 cmp_significand_0 (const REAL_VALUE_TYPE *a) 382 { 383 int i; 384 385 for (i = SIGSZ - 1; i >= 0; --i) 386 if (a->sig[i]) 387 return 1; 388 389 return 0; 390 } 391 392 /* Set bit N of the significand of R. */ 393 394 static inline void 395 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n) 396 { 397 r->sig[n / HOST_BITS_PER_LONG] 398 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG); 399 } 400 401 /* Clear bit N of the significand of R. */ 402 403 static inline void 404 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n) 405 { 406 r->sig[n / HOST_BITS_PER_LONG] 407 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG)); 408 } 409 410 /* Test bit N of the significand of R. */ 411 412 static inline bool 413 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n) 414 { 415 /* ??? Compiler bug here if we return this expression directly. 416 The conversion to bool strips the "&1" and we wind up testing 417 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */ 418 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1; 419 return t; 420 } 421 422 /* Clear bits 0..N-1 of the significand of R. */ 423 424 static void 425 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n) 426 { 427 int i, w = n / HOST_BITS_PER_LONG; 428 429 for (i = 0; i < w; ++i) 430 r->sig[i] = 0; 431 432 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1); 433 } 434 435 /* Divide the significands of A and B, placing the result in R. Return 436 true if the division was inexact. */ 437 438 static inline bool 439 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a, 440 const REAL_VALUE_TYPE *b) 441 { 442 REAL_VALUE_TYPE u; 443 int i, bit = SIGNIFICAND_BITS - 1; 444 unsigned long msb, inexact; 445 446 u = *a; 447 memset (r->sig, 0, sizeof (r->sig)); 448 449 msb = 0; 450 goto start; 451 do 452 { 453 msb = u.sig[SIGSZ-1] & SIG_MSB; 454 lshift_significand_1 (&u, &u); 455 start: 456 if (msb || cmp_significands (&u, b) >= 0) 457 { 458 sub_significands (&u, &u, b, 0); 459 set_significand_bit (r, bit); 460 } 461 } 462 while (--bit >= 0); 463 464 for (i = 0, inexact = 0; i < SIGSZ; i++) 465 inexact |= u.sig[i]; 466 467 return inexact != 0; 468 } 469 470 /* Adjust the exponent and significand of R such that the most 471 significant bit is set. We underflow to zero and overflow to 472 infinity here, without denormals. (The intermediate representation 473 exponent is large enough to handle target denormals normalized.) */ 474 475 static void 476 normalize (REAL_VALUE_TYPE *r) 477 { 478 int shift = 0, exp; 479 int i, j; 480 481 if (r->decimal) 482 return; 483 484 /* Find the first word that is nonzero. */ 485 for (i = SIGSZ - 1; i >= 0; i--) 486 if (r->sig[i] == 0) 487 shift += HOST_BITS_PER_LONG; 488 else 489 break; 490 491 /* Zero significand flushes to zero. */ 492 if (i < 0) 493 { 494 r->cl = rvc_zero; 495 SET_REAL_EXP (r, 0); 496 return; 497 } 498 499 /* Find the first bit that is nonzero. */ 500 for (j = 0; ; j++) 501 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j))) 502 break; 503 shift += j; 504 505 if (shift > 0) 506 { 507 exp = REAL_EXP (r) - shift; 508 if (exp > MAX_EXP) 509 get_inf (r, r->sign); 510 else if (exp < -MAX_EXP) 511 get_zero (r, r->sign); 512 else 513 { 514 SET_REAL_EXP (r, exp); 515 lshift_significand (r, r, shift); 516 } 517 } 518 } 519 520 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the 521 result may be inexact due to a loss of precision. */ 522 523 static bool 524 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a, 525 const REAL_VALUE_TYPE *b, int subtract_p) 526 { 527 int dexp, sign, exp; 528 REAL_VALUE_TYPE t; 529 bool inexact = false; 530 531 /* Determine if we need to add or subtract. */ 532 sign = a->sign; 533 subtract_p = (sign ^ b->sign) ^ subtract_p; 534 535 switch (CLASS2 (a->cl, b->cl)) 536 { 537 case CLASS2 (rvc_zero, rvc_zero): 538 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */ 539 get_zero (r, sign & !subtract_p); 540 return false; 541 542 case CLASS2 (rvc_zero, rvc_normal): 543 case CLASS2 (rvc_zero, rvc_inf): 544 case CLASS2 (rvc_zero, rvc_nan): 545 /* 0 + ANY = ANY. */ 546 case CLASS2 (rvc_normal, rvc_nan): 547 case CLASS2 (rvc_inf, rvc_nan): 548 case CLASS2 (rvc_nan, rvc_nan): 549 /* ANY + NaN = NaN. */ 550 case CLASS2 (rvc_normal, rvc_inf): 551 /* R + Inf = Inf. */ 552 *r = *b; 553 r->sign = sign ^ subtract_p; 554 return false; 555 556 case CLASS2 (rvc_normal, rvc_zero): 557 case CLASS2 (rvc_inf, rvc_zero): 558 case CLASS2 (rvc_nan, rvc_zero): 559 /* ANY + 0 = ANY. */ 560 case CLASS2 (rvc_nan, rvc_normal): 561 case CLASS2 (rvc_nan, rvc_inf): 562 /* NaN + ANY = NaN. */ 563 case CLASS2 (rvc_inf, rvc_normal): 564 /* Inf + R = Inf. */ 565 *r = *a; 566 return false; 567 568 case CLASS2 (rvc_inf, rvc_inf): 569 if (subtract_p) 570 /* Inf - Inf = NaN. */ 571 get_canonical_qnan (r, 0); 572 else 573 /* Inf + Inf = Inf. */ 574 *r = *a; 575 return false; 576 577 case CLASS2 (rvc_normal, rvc_normal): 578 break; 579 580 default: 581 gcc_unreachable (); 582 } 583 584 /* Swap the arguments such that A has the larger exponent. */ 585 dexp = REAL_EXP (a) - REAL_EXP (b); 586 if (dexp < 0) 587 { 588 const REAL_VALUE_TYPE *t; 589 t = a, a = b, b = t; 590 dexp = -dexp; 591 sign ^= subtract_p; 592 } 593 exp = REAL_EXP (a); 594 595 /* If the exponents are not identical, we need to shift the 596 significand of B down. */ 597 if (dexp > 0) 598 { 599 /* If the exponents are too far apart, the significands 600 do not overlap, which makes the subtraction a noop. */ 601 if (dexp >= SIGNIFICAND_BITS) 602 { 603 *r = *a; 604 r->sign = sign; 605 return true; 606 } 607 608 inexact |= sticky_rshift_significand (&t, b, dexp); 609 b = &t; 610 } 611 612 if (subtract_p) 613 { 614 if (sub_significands (r, a, b, inexact)) 615 { 616 /* We got a borrow out of the subtraction. That means that 617 A and B had the same exponent, and B had the larger 618 significand. We need to swap the sign and negate the 619 significand. */ 620 sign ^= 1; 621 neg_significand (r, r); 622 } 623 } 624 else 625 { 626 if (add_significands (r, a, b)) 627 { 628 /* We got carry out of the addition. This means we need to 629 shift the significand back down one bit and increase the 630 exponent. */ 631 inexact |= sticky_rshift_significand (r, r, 1); 632 r->sig[SIGSZ-1] |= SIG_MSB; 633 if (++exp > MAX_EXP) 634 { 635 get_inf (r, sign); 636 return true; 637 } 638 } 639 } 640 641 r->cl = rvc_normal; 642 r->sign = sign; 643 SET_REAL_EXP (r, exp); 644 /* Zero out the remaining fields. */ 645 r->signalling = 0; 646 r->canonical = 0; 647 r->decimal = 0; 648 649 /* Re-normalize the result. */ 650 normalize (r); 651 652 /* Special case: if the subtraction results in zero, the result 653 is positive. */ 654 if (r->cl == rvc_zero) 655 r->sign = 0; 656 else 657 r->sig[0] |= inexact; 658 659 return inexact; 660 } 661 662 /* Calculate R = A * B. Return true if the result may be inexact. */ 663 664 static bool 665 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a, 666 const REAL_VALUE_TYPE *b) 667 { 668 REAL_VALUE_TYPE u, t, *rr; 669 unsigned int i, j, k; 670 int sign = a->sign ^ b->sign; 671 bool inexact = false; 672 673 switch (CLASS2 (a->cl, b->cl)) 674 { 675 case CLASS2 (rvc_zero, rvc_zero): 676 case CLASS2 (rvc_zero, rvc_normal): 677 case CLASS2 (rvc_normal, rvc_zero): 678 /* +-0 * ANY = 0 with appropriate sign. */ 679 get_zero (r, sign); 680 return false; 681 682 case CLASS2 (rvc_zero, rvc_nan): 683 case CLASS2 (rvc_normal, rvc_nan): 684 case CLASS2 (rvc_inf, rvc_nan): 685 case CLASS2 (rvc_nan, rvc_nan): 686 /* ANY * NaN = NaN. */ 687 *r = *b; 688 r->sign = sign; 689 return false; 690 691 case CLASS2 (rvc_nan, rvc_zero): 692 case CLASS2 (rvc_nan, rvc_normal): 693 case CLASS2 (rvc_nan, rvc_inf): 694 /* NaN * ANY = NaN. */ 695 *r = *a; 696 r->sign = sign; 697 return false; 698 699 case CLASS2 (rvc_zero, rvc_inf): 700 case CLASS2 (rvc_inf, rvc_zero): 701 /* 0 * Inf = NaN */ 702 get_canonical_qnan (r, sign); 703 return false; 704 705 case CLASS2 (rvc_inf, rvc_inf): 706 case CLASS2 (rvc_normal, rvc_inf): 707 case CLASS2 (rvc_inf, rvc_normal): 708 /* Inf * Inf = Inf, R * Inf = Inf */ 709 get_inf (r, sign); 710 return false; 711 712 case CLASS2 (rvc_normal, rvc_normal): 713 break; 714 715 default: 716 gcc_unreachable (); 717 } 718 719 if (r == a || r == b) 720 rr = &t; 721 else 722 rr = r; 723 get_zero (rr, 0); 724 725 /* Collect all the partial products. Since we don't have sure access 726 to a widening multiply, we split each long into two half-words. 727 728 Consider the long-hand form of a four half-word multiplication: 729 730 A B C D 731 * E F G H 732 -------------- 733 DE DF DG DH 734 CE CF CG CH 735 BE BF BG BH 736 AE AF AG AH 737 738 We construct partial products of the widened half-word products 739 that are known to not overlap, e.g. DF+DH. Each such partial 740 product is given its proper exponent, which allows us to sum them 741 and obtain the finished product. */ 742 743 for (i = 0; i < SIGSZ * 2; ++i) 744 { 745 unsigned long ai = a->sig[i / 2]; 746 if (i & 1) 747 ai >>= HOST_BITS_PER_LONG / 2; 748 else 749 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1; 750 751 if (ai == 0) 752 continue; 753 754 for (j = 0; j < 2; ++j) 755 { 756 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2) 757 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2))); 758 759 if (exp > MAX_EXP) 760 { 761 get_inf (r, sign); 762 return true; 763 } 764 if (exp < -MAX_EXP) 765 { 766 /* Would underflow to zero, which we shouldn't bother adding. */ 767 inexact = true; 768 continue; 769 } 770 771 memset (&u, 0, sizeof (u)); 772 u.cl = rvc_normal; 773 SET_REAL_EXP (&u, exp); 774 775 for (k = j; k < SIGSZ * 2; k += 2) 776 { 777 unsigned long bi = b->sig[k / 2]; 778 if (k & 1) 779 bi >>= HOST_BITS_PER_LONG / 2; 780 else 781 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1; 782 783 u.sig[k / 2] = ai * bi; 784 } 785 786 normalize (&u); 787 inexact |= do_add (rr, rr, &u, 0); 788 } 789 } 790 791 rr->sign = sign; 792 if (rr != r) 793 *r = t; 794 795 return inexact; 796 } 797 798 /* Calculate R = A / B. Return true if the result may be inexact. */ 799 800 static bool 801 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a, 802 const REAL_VALUE_TYPE *b) 803 { 804 int exp, sign = a->sign ^ b->sign; 805 REAL_VALUE_TYPE t, *rr; 806 bool inexact; 807 808 switch (CLASS2 (a->cl, b->cl)) 809 { 810 case CLASS2 (rvc_zero, rvc_zero): 811 /* 0 / 0 = NaN. */ 812 case CLASS2 (rvc_inf, rvc_inf): 813 /* Inf / Inf = NaN. */ 814 get_canonical_qnan (r, sign); 815 return false; 816 817 case CLASS2 (rvc_zero, rvc_normal): 818 case CLASS2 (rvc_zero, rvc_inf): 819 /* 0 / ANY = 0. */ 820 case CLASS2 (rvc_normal, rvc_inf): 821 /* R / Inf = 0. */ 822 get_zero (r, sign); 823 return false; 824 825 case CLASS2 (rvc_normal, rvc_zero): 826 /* R / 0 = Inf. */ 827 case CLASS2 (rvc_inf, rvc_zero): 828 /* Inf / 0 = Inf. */ 829 get_inf (r, sign); 830 return false; 831 832 case CLASS2 (rvc_zero, rvc_nan): 833 case CLASS2 (rvc_normal, rvc_nan): 834 case CLASS2 (rvc_inf, rvc_nan): 835 case CLASS2 (rvc_nan, rvc_nan): 836 /* ANY / NaN = NaN. */ 837 *r = *b; 838 r->sign = sign; 839 return false; 840 841 case CLASS2 (rvc_nan, rvc_zero): 842 case CLASS2 (rvc_nan, rvc_normal): 843 case CLASS2 (rvc_nan, rvc_inf): 844 /* NaN / ANY = NaN. */ 845 *r = *a; 846 r->sign = sign; 847 return false; 848 849 case CLASS2 (rvc_inf, rvc_normal): 850 /* Inf / R = Inf. */ 851 get_inf (r, sign); 852 return false; 853 854 case CLASS2 (rvc_normal, rvc_normal): 855 break; 856 857 default: 858 gcc_unreachable (); 859 } 860 861 if (r == a || r == b) 862 rr = &t; 863 else 864 rr = r; 865 866 /* Make sure all fields in the result are initialized. */ 867 get_zero (rr, 0); 868 rr->cl = rvc_normal; 869 rr->sign = sign; 870 871 exp = REAL_EXP (a) - REAL_EXP (b) + 1; 872 if (exp > MAX_EXP) 873 { 874 get_inf (r, sign); 875 return true; 876 } 877 if (exp < -MAX_EXP) 878 { 879 get_zero (r, sign); 880 return true; 881 } 882 SET_REAL_EXP (rr, exp); 883 884 inexact = div_significands (rr, a, b); 885 886 /* Re-normalize the result. */ 887 normalize (rr); 888 rr->sig[0] |= inexact; 889 890 if (rr != r) 891 *r = t; 892 893 return inexact; 894 } 895 896 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if 897 one of the two operands is a NaN. */ 898 899 static int 900 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b, 901 int nan_result) 902 { 903 int ret; 904 905 switch (CLASS2 (a->cl, b->cl)) 906 { 907 case CLASS2 (rvc_zero, rvc_zero): 908 /* Sign of zero doesn't matter for compares. */ 909 return 0; 910 911 case CLASS2 (rvc_normal, rvc_zero): 912 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */ 913 if (a->decimal) 914 return decimal_do_compare (a, b, nan_result); 915 /* Fall through. */ 916 case CLASS2 (rvc_inf, rvc_zero): 917 case CLASS2 (rvc_inf, rvc_normal): 918 return (a->sign ? -1 : 1); 919 920 case CLASS2 (rvc_inf, rvc_inf): 921 return -a->sign - -b->sign; 922 923 case CLASS2 (rvc_zero, rvc_normal): 924 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */ 925 if (b->decimal) 926 return decimal_do_compare (a, b, nan_result); 927 /* Fall through. */ 928 case CLASS2 (rvc_zero, rvc_inf): 929 case CLASS2 (rvc_normal, rvc_inf): 930 return (b->sign ? 1 : -1); 931 932 case CLASS2 (rvc_zero, rvc_nan): 933 case CLASS2 (rvc_normal, rvc_nan): 934 case CLASS2 (rvc_inf, rvc_nan): 935 case CLASS2 (rvc_nan, rvc_nan): 936 case CLASS2 (rvc_nan, rvc_zero): 937 case CLASS2 (rvc_nan, rvc_normal): 938 case CLASS2 (rvc_nan, rvc_inf): 939 return nan_result; 940 941 case CLASS2 (rvc_normal, rvc_normal): 942 break; 943 944 default: 945 gcc_unreachable (); 946 } 947 948 if (a->sign != b->sign) 949 return -a->sign - -b->sign; 950 951 if (a->decimal || b->decimal) 952 return decimal_do_compare (a, b, nan_result); 953 954 if (REAL_EXP (a) > REAL_EXP (b)) 955 ret = 1; 956 else if (REAL_EXP (a) < REAL_EXP (b)) 957 ret = -1; 958 else 959 ret = cmp_significands (a, b); 960 961 return (a->sign ? -ret : ret); 962 } 963 964 /* Return A truncated to an integral value toward zero. */ 965 966 static void 967 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a) 968 { 969 *r = *a; 970 971 switch (r->cl) 972 { 973 case rvc_zero: 974 case rvc_inf: 975 case rvc_nan: 976 break; 977 978 case rvc_normal: 979 if (r->decimal) 980 { 981 decimal_do_fix_trunc (r, a); 982 return; 983 } 984 if (REAL_EXP (r) <= 0) 985 get_zero (r, r->sign); 986 else if (REAL_EXP (r) < SIGNIFICAND_BITS) 987 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r)); 988 break; 989 990 default: 991 gcc_unreachable (); 992 } 993 } 994 995 /* Perform the binary or unary operation described by CODE. 996 For a unary operation, leave OP1 NULL. This function returns 997 true if the result may be inexact due to loss of precision. */ 998 999 bool 1000 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0, 1001 const REAL_VALUE_TYPE *op1) 1002 { 1003 enum tree_code code = (enum tree_code) icode; 1004 1005 if (op0->decimal || (op1 && op1->decimal)) 1006 return decimal_real_arithmetic (r, code, op0, op1); 1007 1008 switch (code) 1009 { 1010 case PLUS_EXPR: 1011 /* Clear any padding areas in *r if it isn't equal to one of the 1012 operands so that we can later do bitwise comparisons later on. */ 1013 if (r != op0 && r != op1) 1014 memset (r, '\0', sizeof (*r)); 1015 return do_add (r, op0, op1, 0); 1016 1017 case MINUS_EXPR: 1018 if (r != op0 && r != op1) 1019 memset (r, '\0', sizeof (*r)); 1020 return do_add (r, op0, op1, 1); 1021 1022 case MULT_EXPR: 1023 if (r != op0 && r != op1) 1024 memset (r, '\0', sizeof (*r)); 1025 return do_multiply (r, op0, op1); 1026 1027 case RDIV_EXPR: 1028 if (r != op0 && r != op1) 1029 memset (r, '\0', sizeof (*r)); 1030 return do_divide (r, op0, op1); 1031 1032 case MIN_EXPR: 1033 if (op1->cl == rvc_nan) 1034 *r = *op1; 1035 else if (do_compare (op0, op1, -1) < 0) 1036 *r = *op0; 1037 else 1038 *r = *op1; 1039 break; 1040 1041 case MAX_EXPR: 1042 if (op1->cl == rvc_nan) 1043 *r = *op1; 1044 else if (do_compare (op0, op1, 1) < 0) 1045 *r = *op1; 1046 else 1047 *r = *op0; 1048 break; 1049 1050 case NEGATE_EXPR: 1051 *r = *op0; 1052 r->sign ^= 1; 1053 break; 1054 1055 case ABS_EXPR: 1056 *r = *op0; 1057 r->sign = 0; 1058 break; 1059 1060 case FIX_TRUNC_EXPR: 1061 do_fix_trunc (r, op0); 1062 break; 1063 1064 default: 1065 gcc_unreachable (); 1066 } 1067 return false; 1068 } 1069 1070 /* Legacy. Similar, but return the result directly. */ 1071 1072 REAL_VALUE_TYPE 1073 real_arithmetic2 (int icode, const REAL_VALUE_TYPE *op0, 1074 const REAL_VALUE_TYPE *op1) 1075 { 1076 REAL_VALUE_TYPE r; 1077 real_arithmetic (&r, icode, op0, op1); 1078 return r; 1079 } 1080 1081 bool 1082 real_compare (int icode, const REAL_VALUE_TYPE *op0, 1083 const REAL_VALUE_TYPE *op1) 1084 { 1085 enum tree_code code = (enum tree_code) icode; 1086 1087 switch (code) 1088 { 1089 case LT_EXPR: 1090 return do_compare (op0, op1, 1) < 0; 1091 case LE_EXPR: 1092 return do_compare (op0, op1, 1) <= 0; 1093 case GT_EXPR: 1094 return do_compare (op0, op1, -1) > 0; 1095 case GE_EXPR: 1096 return do_compare (op0, op1, -1) >= 0; 1097 case EQ_EXPR: 1098 return do_compare (op0, op1, -1) == 0; 1099 case NE_EXPR: 1100 return do_compare (op0, op1, -1) != 0; 1101 case UNORDERED_EXPR: 1102 return op0->cl == rvc_nan || op1->cl == rvc_nan; 1103 case ORDERED_EXPR: 1104 return op0->cl != rvc_nan && op1->cl != rvc_nan; 1105 case UNLT_EXPR: 1106 return do_compare (op0, op1, -1) < 0; 1107 case UNLE_EXPR: 1108 return do_compare (op0, op1, -1) <= 0; 1109 case UNGT_EXPR: 1110 return do_compare (op0, op1, 1) > 0; 1111 case UNGE_EXPR: 1112 return do_compare (op0, op1, 1) >= 0; 1113 case UNEQ_EXPR: 1114 return do_compare (op0, op1, 0) == 0; 1115 case LTGT_EXPR: 1116 return do_compare (op0, op1, 0) != 0; 1117 1118 default: 1119 gcc_unreachable (); 1120 } 1121 } 1122 1123 /* Return floor log2(R). */ 1124 1125 int 1126 real_exponent (const REAL_VALUE_TYPE *r) 1127 { 1128 switch (r->cl) 1129 { 1130 case rvc_zero: 1131 return 0; 1132 case rvc_inf: 1133 case rvc_nan: 1134 return (unsigned int)-1 >> 1; 1135 case rvc_normal: 1136 return REAL_EXP (r); 1137 default: 1138 gcc_unreachable (); 1139 } 1140 } 1141 1142 /* R = OP0 * 2**EXP. */ 1143 1144 void 1145 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp) 1146 { 1147 *r = *op0; 1148 switch (r->cl) 1149 { 1150 case rvc_zero: 1151 case rvc_inf: 1152 case rvc_nan: 1153 break; 1154 1155 case rvc_normal: 1156 exp += REAL_EXP (op0); 1157 if (exp > MAX_EXP) 1158 get_inf (r, r->sign); 1159 else if (exp < -MAX_EXP) 1160 get_zero (r, r->sign); 1161 else 1162 SET_REAL_EXP (r, exp); 1163 break; 1164 1165 default: 1166 gcc_unreachable (); 1167 } 1168 } 1169 1170 /* Determine whether a floating-point value X is infinite. */ 1171 1172 bool 1173 real_isinf (const REAL_VALUE_TYPE *r) 1174 { 1175 return (r->cl == rvc_inf); 1176 } 1177 1178 /* Determine whether a floating-point value X is a NaN. */ 1179 1180 bool 1181 real_isnan (const REAL_VALUE_TYPE *r) 1182 { 1183 return (r->cl == rvc_nan); 1184 } 1185 1186 /* Determine whether a floating-point value X is finite. */ 1187 1188 bool 1189 real_isfinite (const REAL_VALUE_TYPE *r) 1190 { 1191 return (r->cl != rvc_nan) && (r->cl != rvc_inf); 1192 } 1193 1194 /* Determine whether a floating-point value X is negative. */ 1195 1196 bool 1197 real_isneg (const REAL_VALUE_TYPE *r) 1198 { 1199 return r->sign; 1200 } 1201 1202 /* Determine whether a floating-point value X is minus zero. */ 1203 1204 bool 1205 real_isnegzero (const REAL_VALUE_TYPE *r) 1206 { 1207 return r->sign && r->cl == rvc_zero; 1208 } 1209 1210 /* Compare two floating-point objects for bitwise identity. */ 1211 1212 bool 1213 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b) 1214 { 1215 int i; 1216 1217 if (a->cl != b->cl) 1218 return false; 1219 if (a->sign != b->sign) 1220 return false; 1221 1222 switch (a->cl) 1223 { 1224 case rvc_zero: 1225 case rvc_inf: 1226 return true; 1227 1228 case rvc_normal: 1229 if (a->decimal != b->decimal) 1230 return false; 1231 if (REAL_EXP (a) != REAL_EXP (b)) 1232 return false; 1233 break; 1234 1235 case rvc_nan: 1236 if (a->signalling != b->signalling) 1237 return false; 1238 /* The significand is ignored for canonical NaNs. */ 1239 if (a->canonical || b->canonical) 1240 return a->canonical == b->canonical; 1241 break; 1242 1243 default: 1244 gcc_unreachable (); 1245 } 1246 1247 for (i = 0; i < SIGSZ; ++i) 1248 if (a->sig[i] != b->sig[i]) 1249 return false; 1250 1251 return true; 1252 } 1253 1254 /* Try to change R into its exact multiplicative inverse in machine 1255 mode MODE. Return true if successful. */ 1256 1257 bool 1258 exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r) 1259 { 1260 const REAL_VALUE_TYPE *one = real_digit (1); 1261 REAL_VALUE_TYPE u; 1262 int i; 1263 1264 if (r->cl != rvc_normal) 1265 return false; 1266 1267 /* Check for a power of two: all significand bits zero except the MSB. */ 1268 for (i = 0; i < SIGSZ-1; ++i) 1269 if (r->sig[i] != 0) 1270 return false; 1271 if (r->sig[SIGSZ-1] != SIG_MSB) 1272 return false; 1273 1274 /* Find the inverse and truncate to the required mode. */ 1275 do_divide (&u, one, r); 1276 real_convert (&u, mode, &u); 1277 1278 /* The rounding may have overflowed. */ 1279 if (u.cl != rvc_normal) 1280 return false; 1281 for (i = 0; i < SIGSZ-1; ++i) 1282 if (u.sig[i] != 0) 1283 return false; 1284 if (u.sig[SIGSZ-1] != SIG_MSB) 1285 return false; 1286 1287 *r = u; 1288 return true; 1289 } 1290 1291 /* Return true if arithmetic on values in IMODE that were promoted 1292 from values in TMODE is equivalent to direct arithmetic on values 1293 in TMODE. */ 1294 1295 bool 1296 real_can_shorten_arithmetic (enum machine_mode imode, enum machine_mode tmode) 1297 { 1298 const struct real_format *tfmt, *ifmt; 1299 tfmt = REAL_MODE_FORMAT (tmode); 1300 ifmt = REAL_MODE_FORMAT (imode); 1301 /* These conditions are conservative rather than trying to catch the 1302 exact boundary conditions; the main case to allow is IEEE float 1303 and double. */ 1304 return (ifmt->b == tfmt->b 1305 && ifmt->p > 2 * tfmt->p 1306 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2 1307 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2 1308 && ifmt->emax > 2 * tfmt->emax + 2 1309 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2 1310 && ifmt->round_towards_zero == tfmt->round_towards_zero 1311 && (ifmt->has_sign_dependent_rounding 1312 == tfmt->has_sign_dependent_rounding) 1313 && ifmt->has_nans >= tfmt->has_nans 1314 && ifmt->has_inf >= tfmt->has_inf 1315 && ifmt->has_signed_zero >= tfmt->has_signed_zero 1316 && !MODE_COMPOSITE_P (tmode) 1317 && !MODE_COMPOSITE_P (imode)); 1318 } 1319 1320 /* Render R as an integer. */ 1321 1322 HOST_WIDE_INT 1323 real_to_integer (const REAL_VALUE_TYPE *r) 1324 { 1325 unsigned HOST_WIDE_INT i; 1326 1327 switch (r->cl) 1328 { 1329 case rvc_zero: 1330 underflow: 1331 return 0; 1332 1333 case rvc_inf: 1334 case rvc_nan: 1335 overflow: 1336 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1); 1337 if (!r->sign) 1338 i--; 1339 return i; 1340 1341 case rvc_normal: 1342 if (r->decimal) 1343 return decimal_real_to_integer (r); 1344 1345 if (REAL_EXP (r) <= 0) 1346 goto underflow; 1347 /* Only force overflow for unsigned overflow. Signed overflow is 1348 undefined, so it doesn't matter what we return, and some callers 1349 expect to be able to use this routine for both signed and 1350 unsigned conversions. */ 1351 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT) 1352 goto overflow; 1353 1354 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG) 1355 i = r->sig[SIGSZ-1]; 1356 else 1357 { 1358 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG); 1359 i = r->sig[SIGSZ-1]; 1360 i = i << (HOST_BITS_PER_LONG - 1) << 1; 1361 i |= r->sig[SIGSZ-2]; 1362 } 1363 1364 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r); 1365 1366 if (r->sign) 1367 i = -i; 1368 return i; 1369 1370 default: 1371 gcc_unreachable (); 1372 } 1373 } 1374 1375 /* Likewise, but to an integer pair, HI+LOW. */ 1376 1377 void 1378 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh, 1379 const REAL_VALUE_TYPE *r) 1380 { 1381 REAL_VALUE_TYPE t; 1382 HOST_WIDE_INT low, high; 1383 int exp; 1384 1385 switch (r->cl) 1386 { 1387 case rvc_zero: 1388 underflow: 1389 low = high = 0; 1390 break; 1391 1392 case rvc_inf: 1393 case rvc_nan: 1394 overflow: 1395 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1); 1396 if (r->sign) 1397 low = 0; 1398 else 1399 { 1400 high--; 1401 low = -1; 1402 } 1403 break; 1404 1405 case rvc_normal: 1406 if (r->decimal) 1407 { 1408 decimal_real_to_integer2 (plow, phigh, r); 1409 return; 1410 } 1411 1412 exp = REAL_EXP (r); 1413 if (exp <= 0) 1414 goto underflow; 1415 /* Only force overflow for unsigned overflow. Signed overflow is 1416 undefined, so it doesn't matter what we return, and some callers 1417 expect to be able to use this routine for both signed and 1418 unsigned conversions. */ 1419 if (exp > 2*HOST_BITS_PER_WIDE_INT) 1420 goto overflow; 1421 1422 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp); 1423 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG) 1424 { 1425 high = t.sig[SIGSZ-1]; 1426 low = t.sig[SIGSZ-2]; 1427 } 1428 else 1429 { 1430 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG); 1431 high = t.sig[SIGSZ-1]; 1432 high = high << (HOST_BITS_PER_LONG - 1) << 1; 1433 high |= t.sig[SIGSZ-2]; 1434 1435 low = t.sig[SIGSZ-3]; 1436 low = low << (HOST_BITS_PER_LONG - 1) << 1; 1437 low |= t.sig[SIGSZ-4]; 1438 } 1439 1440 if (r->sign) 1441 { 1442 if (low == 0) 1443 high = -high; 1444 else 1445 low = -low, high = ~high; 1446 } 1447 break; 1448 1449 default: 1450 gcc_unreachable (); 1451 } 1452 1453 *plow = low; 1454 *phigh = high; 1455 } 1456 1457 /* A subroutine of real_to_decimal. Compute the quotient and remainder 1458 of NUM / DEN. Return the quotient and place the remainder in NUM. 1459 It is expected that NUM / DEN are close enough that the quotient is 1460 small. */ 1461 1462 static unsigned long 1463 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den) 1464 { 1465 unsigned long q, msb; 1466 int expn = REAL_EXP (num), expd = REAL_EXP (den); 1467 1468 if (expn < expd) 1469 return 0; 1470 1471 q = msb = 0; 1472 goto start; 1473 do 1474 { 1475 msb = num->sig[SIGSZ-1] & SIG_MSB; 1476 q <<= 1; 1477 lshift_significand_1 (num, num); 1478 start: 1479 if (msb || cmp_significands (num, den) >= 0) 1480 { 1481 sub_significands (num, num, den, 0); 1482 q |= 1; 1483 } 1484 } 1485 while (--expn >= expd); 1486 1487 SET_REAL_EXP (num, expd); 1488 normalize (num); 1489 1490 return q; 1491 } 1492 1493 /* Render R as a decimal floating point constant. Emit DIGITS significant 1494 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the 1495 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing 1496 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round 1497 to a string that, when parsed back in mode MODE, yields the same value. */ 1498 1499 #define M_LOG10_2 0.30102999566398119521 1500 1501 void 1502 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig, 1503 size_t buf_size, size_t digits, 1504 int crop_trailing_zeros, enum machine_mode mode) 1505 { 1506 const struct real_format *fmt = NULL; 1507 const REAL_VALUE_TYPE *one, *ten; 1508 REAL_VALUE_TYPE r, pten, u, v; 1509 int dec_exp, cmp_one, digit; 1510 size_t max_digits; 1511 char *p, *first, *last; 1512 bool sign; 1513 bool round_up; 1514 1515 if (mode != VOIDmode) 1516 { 1517 fmt = REAL_MODE_FORMAT (mode); 1518 gcc_assert (fmt); 1519 } 1520 1521 r = *r_orig; 1522 switch (r.cl) 1523 { 1524 case rvc_zero: 1525 strcpy (str, (r.sign ? "-0.0" : "0.0")); 1526 return; 1527 case rvc_normal: 1528 break; 1529 case rvc_inf: 1530 strcpy (str, (r.sign ? "-Inf" : "+Inf")); 1531 return; 1532 case rvc_nan: 1533 /* ??? Print the significand as well, if not canonical? */ 1534 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'), 1535 (r_orig->signalling ? 'S' : 'Q')); 1536 return; 1537 default: 1538 gcc_unreachable (); 1539 } 1540 1541 if (r.decimal) 1542 { 1543 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros); 1544 return; 1545 } 1546 1547 /* Bound the number of digits printed by the size of the representation. */ 1548 max_digits = SIGNIFICAND_BITS * M_LOG10_2; 1549 if (digits == 0 || digits > max_digits) 1550 digits = max_digits; 1551 1552 /* Estimate the decimal exponent, and compute the length of the string it 1553 will print as. Be conservative and add one to account for possible 1554 overflow or rounding error. */ 1555 dec_exp = REAL_EXP (&r) * M_LOG10_2; 1556 for (max_digits = 1; dec_exp ; max_digits++) 1557 dec_exp /= 10; 1558 1559 /* Bound the number of digits printed by the size of the output buffer. */ 1560 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1; 1561 gcc_assert (max_digits <= buf_size); 1562 if (digits > max_digits) 1563 digits = max_digits; 1564 1565 one = real_digit (1); 1566 ten = ten_to_ptwo (0); 1567 1568 sign = r.sign; 1569 r.sign = 0; 1570 1571 dec_exp = 0; 1572 pten = *one; 1573 1574 cmp_one = do_compare (&r, one, 0); 1575 if (cmp_one > 0) 1576 { 1577 int m; 1578 1579 /* Number is greater than one. Convert significand to an integer 1580 and strip trailing decimal zeros. */ 1581 1582 u = r; 1583 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1); 1584 1585 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */ 1586 m = floor_log2 (max_digits); 1587 1588 /* Iterate over the bits of the possible powers of 10 that might 1589 be present in U and eliminate them. That is, if we find that 1590 10**2**M divides U evenly, keep the division and increase 1591 DEC_EXP by 2**M. */ 1592 do 1593 { 1594 REAL_VALUE_TYPE t; 1595 1596 do_divide (&t, &u, ten_to_ptwo (m)); 1597 do_fix_trunc (&v, &t); 1598 if (cmp_significands (&v, &t) == 0) 1599 { 1600 u = t; 1601 dec_exp += 1 << m; 1602 } 1603 } 1604 while (--m >= 0); 1605 1606 /* Revert the scaling to integer that we performed earlier. */ 1607 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r) 1608 - (SIGNIFICAND_BITS - 1)); 1609 r = u; 1610 1611 /* Find power of 10. Do this by dividing out 10**2**M when 1612 this is larger than the current remainder. Fill PTEN with 1613 the power of 10 that we compute. */ 1614 if (REAL_EXP (&r) > 0) 1615 { 1616 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1; 1617 do 1618 { 1619 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m); 1620 if (do_compare (&u, ptentwo, 0) >= 0) 1621 { 1622 do_divide (&u, &u, ptentwo); 1623 do_multiply (&pten, &pten, ptentwo); 1624 dec_exp += 1 << m; 1625 } 1626 } 1627 while (--m >= 0); 1628 } 1629 else 1630 /* We managed to divide off enough tens in the above reduction 1631 loop that we've now got a negative exponent. Fall into the 1632 less-than-one code to compute the proper value for PTEN. */ 1633 cmp_one = -1; 1634 } 1635 if (cmp_one < 0) 1636 { 1637 int m; 1638 1639 /* Number is less than one. Pad significand with leading 1640 decimal zeros. */ 1641 1642 v = r; 1643 while (1) 1644 { 1645 /* Stop if we'd shift bits off the bottom. */ 1646 if (v.sig[0] & 7) 1647 break; 1648 1649 do_multiply (&u, &v, ten); 1650 1651 /* Stop if we're now >= 1. */ 1652 if (REAL_EXP (&u) > 0) 1653 break; 1654 1655 v = u; 1656 dec_exp -= 1; 1657 } 1658 r = v; 1659 1660 /* Find power of 10. Do this by multiplying in P=10**2**M when 1661 the current remainder is smaller than 1/P. Fill PTEN with the 1662 power of 10 that we compute. */ 1663 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1; 1664 do 1665 { 1666 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m); 1667 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m); 1668 1669 if (do_compare (&v, ptenmtwo, 0) <= 0) 1670 { 1671 do_multiply (&v, &v, ptentwo); 1672 do_multiply (&pten, &pten, ptentwo); 1673 dec_exp -= 1 << m; 1674 } 1675 } 1676 while (--m >= 0); 1677 1678 /* Invert the positive power of 10 that we've collected so far. */ 1679 do_divide (&pten, one, &pten); 1680 } 1681 1682 p = str; 1683 if (sign) 1684 *p++ = '-'; 1685 first = p++; 1686 1687 /* At this point, PTEN should contain the nearest power of 10 smaller 1688 than R, such that this division produces the first digit. 1689 1690 Using a divide-step primitive that returns the complete integral 1691 remainder avoids the rounding error that would be produced if 1692 we were to use do_divide here and then simply multiply by 10 for 1693 each subsequent digit. */ 1694 1695 digit = rtd_divmod (&r, &pten); 1696 1697 /* Be prepared for error in that division via underflow ... */ 1698 if (digit == 0 && cmp_significand_0 (&r)) 1699 { 1700 /* Multiply by 10 and try again. */ 1701 do_multiply (&r, &r, ten); 1702 digit = rtd_divmod (&r, &pten); 1703 dec_exp -= 1; 1704 gcc_assert (digit != 0); 1705 } 1706 1707 /* ... or overflow. */ 1708 if (digit == 10) 1709 { 1710 *p++ = '1'; 1711 if (--digits > 0) 1712 *p++ = '0'; 1713 dec_exp += 1; 1714 } 1715 else 1716 { 1717 gcc_assert (digit <= 10); 1718 *p++ = digit + '0'; 1719 } 1720 1721 /* Generate subsequent digits. */ 1722 while (--digits > 0) 1723 { 1724 do_multiply (&r, &r, ten); 1725 digit = rtd_divmod (&r, &pten); 1726 *p++ = digit + '0'; 1727 } 1728 last = p; 1729 1730 /* Generate one more digit with which to do rounding. */ 1731 do_multiply (&r, &r, ten); 1732 digit = rtd_divmod (&r, &pten); 1733 1734 /* Round the result. */ 1735 if (fmt && fmt->round_towards_zero) 1736 { 1737 /* If the format uses round towards zero when parsing the string 1738 back in, we need to always round away from zero here. */ 1739 if (cmp_significand_0 (&r)) 1740 digit++; 1741 round_up = digit > 0; 1742 } 1743 else 1744 { 1745 if (digit == 5) 1746 { 1747 /* Round to nearest. If R is nonzero there are additional 1748 nonzero digits to be extracted. */ 1749 if (cmp_significand_0 (&r)) 1750 digit++; 1751 /* Round to even. */ 1752 else if ((p[-1] - '0') & 1) 1753 digit++; 1754 } 1755 1756 round_up = digit > 5; 1757 } 1758 1759 if (round_up) 1760 { 1761 while (p > first) 1762 { 1763 digit = *--p; 1764 if (digit == '9') 1765 *p = '0'; 1766 else 1767 { 1768 *p = digit + 1; 1769 break; 1770 } 1771 } 1772 1773 /* Carry out of the first digit. This means we had all 9's and 1774 now have all 0's. "Prepend" a 1 by overwriting the first 0. */ 1775 if (p == first) 1776 { 1777 first[1] = '1'; 1778 dec_exp++; 1779 } 1780 } 1781 1782 /* Insert the decimal point. */ 1783 first[0] = first[1]; 1784 first[1] = '.'; 1785 1786 /* If requested, drop trailing zeros. Never crop past "1.0". */ 1787 if (crop_trailing_zeros) 1788 while (last > first + 3 && last[-1] == '0') 1789 last--; 1790 1791 /* Append the exponent. */ 1792 sprintf (last, "e%+d", dec_exp); 1793 1794 #ifdef ENABLE_CHECKING 1795 /* Verify that we can read the original value back in. */ 1796 if (mode != VOIDmode) 1797 { 1798 real_from_string (&r, str); 1799 real_convert (&r, mode, &r); 1800 gcc_assert (real_identical (&r, r_orig)); 1801 } 1802 #endif 1803 } 1804 1805 /* Likewise, except always uses round-to-nearest. */ 1806 1807 void 1808 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size, 1809 size_t digits, int crop_trailing_zeros) 1810 { 1811 real_to_decimal_for_mode (str, r_orig, buf_size, 1812 digits, crop_trailing_zeros, VOIDmode); 1813 } 1814 1815 /* Render R as a hexadecimal floating point constant. Emit DIGITS 1816 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0, 1817 choose the maximum for the representation. If CROP_TRAILING_ZEROS, 1818 strip trailing zeros. */ 1819 1820 void 1821 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size, 1822 size_t digits, int crop_trailing_zeros) 1823 { 1824 int i, j, exp = REAL_EXP (r); 1825 char *p, *first; 1826 char exp_buf[16]; 1827 size_t max_digits; 1828 1829 switch (r->cl) 1830 { 1831 case rvc_zero: 1832 exp = 0; 1833 break; 1834 case rvc_normal: 1835 break; 1836 case rvc_inf: 1837 strcpy (str, (r->sign ? "-Inf" : "+Inf")); 1838 return; 1839 case rvc_nan: 1840 /* ??? Print the significand as well, if not canonical? */ 1841 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'), 1842 (r->signalling ? 'S' : 'Q')); 1843 return; 1844 default: 1845 gcc_unreachable (); 1846 } 1847 1848 if (r->decimal) 1849 { 1850 /* Hexadecimal format for decimal floats is not interesting. */ 1851 strcpy (str, "N/A"); 1852 return; 1853 } 1854 1855 if (digits == 0) 1856 digits = SIGNIFICAND_BITS / 4; 1857 1858 /* Bound the number of digits printed by the size of the output buffer. */ 1859 1860 sprintf (exp_buf, "p%+d", exp); 1861 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1; 1862 gcc_assert (max_digits <= buf_size); 1863 if (digits > max_digits) 1864 digits = max_digits; 1865 1866 p = str; 1867 if (r->sign) 1868 *p++ = '-'; 1869 *p++ = '0'; 1870 *p++ = 'x'; 1871 *p++ = '0'; 1872 *p++ = '.'; 1873 first = p; 1874 1875 for (i = SIGSZ - 1; i >= 0; --i) 1876 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4) 1877 { 1878 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15]; 1879 if (--digits == 0) 1880 goto out; 1881 } 1882 1883 out: 1884 if (crop_trailing_zeros) 1885 while (p > first + 1 && p[-1] == '0') 1886 p--; 1887 1888 sprintf (p, "p%+d", exp); 1889 } 1890 1891 /* Initialize R from a decimal or hexadecimal string. The string is 1892 assumed to have been syntax checked already. Return -1 if the 1893 value underflows, +1 if overflows, and 0 otherwise. */ 1894 1895 int 1896 real_from_string (REAL_VALUE_TYPE *r, const char *str) 1897 { 1898 int exp = 0; 1899 bool sign = false; 1900 1901 get_zero (r, 0); 1902 1903 if (*str == '-') 1904 { 1905 sign = true; 1906 str++; 1907 } 1908 else if (*str == '+') 1909 str++; 1910 1911 if (!strncmp (str, "QNaN", 4)) 1912 { 1913 get_canonical_qnan (r, sign); 1914 return 0; 1915 } 1916 else if (!strncmp (str, "SNaN", 4)) 1917 { 1918 get_canonical_snan (r, sign); 1919 return 0; 1920 } 1921 else if (!strncmp (str, "Inf", 3)) 1922 { 1923 get_inf (r, sign); 1924 return 0; 1925 } 1926 1927 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 1928 { 1929 /* Hexadecimal floating point. */ 1930 int pos = SIGNIFICAND_BITS - 4, d; 1931 1932 str += 2; 1933 1934 while (*str == '0') 1935 str++; 1936 while (1) 1937 { 1938 d = hex_value (*str); 1939 if (d == _hex_bad) 1940 break; 1941 if (pos >= 0) 1942 { 1943 r->sig[pos / HOST_BITS_PER_LONG] 1944 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG); 1945 pos -= 4; 1946 } 1947 else if (d) 1948 /* Ensure correct rounding by setting last bit if there is 1949 a subsequent nonzero digit. */ 1950 r->sig[0] |= 1; 1951 exp += 4; 1952 str++; 1953 } 1954 if (*str == '.') 1955 { 1956 str++; 1957 if (pos == SIGNIFICAND_BITS - 4) 1958 { 1959 while (*str == '0') 1960 str++, exp -= 4; 1961 } 1962 while (1) 1963 { 1964 d = hex_value (*str); 1965 if (d == _hex_bad) 1966 break; 1967 if (pos >= 0) 1968 { 1969 r->sig[pos / HOST_BITS_PER_LONG] 1970 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG); 1971 pos -= 4; 1972 } 1973 else if (d) 1974 /* Ensure correct rounding by setting last bit if there is 1975 a subsequent nonzero digit. */ 1976 r->sig[0] |= 1; 1977 str++; 1978 } 1979 } 1980 1981 /* If the mantissa is zero, ignore the exponent. */ 1982 if (!cmp_significand_0 (r)) 1983 goto is_a_zero; 1984 1985 if (*str == 'p' || *str == 'P') 1986 { 1987 bool exp_neg = false; 1988 1989 str++; 1990 if (*str == '-') 1991 { 1992 exp_neg = true; 1993 str++; 1994 } 1995 else if (*str == '+') 1996 str++; 1997 1998 d = 0; 1999 while (ISDIGIT (*str)) 2000 { 2001 d *= 10; 2002 d += *str - '0'; 2003 if (d > MAX_EXP) 2004 { 2005 /* Overflowed the exponent. */ 2006 if (exp_neg) 2007 goto underflow; 2008 else 2009 goto overflow; 2010 } 2011 str++; 2012 } 2013 if (exp_neg) 2014 d = -d; 2015 2016 exp += d; 2017 } 2018 2019 r->cl = rvc_normal; 2020 SET_REAL_EXP (r, exp); 2021 2022 normalize (r); 2023 } 2024 else 2025 { 2026 /* Decimal floating point. */ 2027 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0); 2028 int d; 2029 2030 while (*str == '0') 2031 str++; 2032 while (ISDIGIT (*str)) 2033 { 2034 d = *str++ - '0'; 2035 do_multiply (r, r, ten); 2036 if (d) 2037 do_add (r, r, real_digit (d), 0); 2038 } 2039 if (*str == '.') 2040 { 2041 str++; 2042 if (r->cl == rvc_zero) 2043 { 2044 while (*str == '0') 2045 str++, exp--; 2046 } 2047 while (ISDIGIT (*str)) 2048 { 2049 d = *str++ - '0'; 2050 do_multiply (r, r, ten); 2051 if (d) 2052 do_add (r, r, real_digit (d), 0); 2053 exp--; 2054 } 2055 } 2056 2057 /* If the mantissa is zero, ignore the exponent. */ 2058 if (r->cl == rvc_zero) 2059 goto is_a_zero; 2060 2061 if (*str == 'e' || *str == 'E') 2062 { 2063 bool exp_neg = false; 2064 2065 str++; 2066 if (*str == '-') 2067 { 2068 exp_neg = true; 2069 str++; 2070 } 2071 else if (*str == '+') 2072 str++; 2073 2074 d = 0; 2075 while (ISDIGIT (*str)) 2076 { 2077 d *= 10; 2078 d += *str - '0'; 2079 if (d > MAX_EXP) 2080 { 2081 /* Overflowed the exponent. */ 2082 if (exp_neg) 2083 goto underflow; 2084 else 2085 goto overflow; 2086 } 2087 str++; 2088 } 2089 if (exp_neg) 2090 d = -d; 2091 exp += d; 2092 } 2093 2094 if (exp) 2095 times_pten (r, exp); 2096 } 2097 2098 r->sign = sign; 2099 return 0; 2100 2101 is_a_zero: 2102 get_zero (r, sign); 2103 return 0; 2104 2105 underflow: 2106 get_zero (r, sign); 2107 return -1; 2108 2109 overflow: 2110 get_inf (r, sign); 2111 return 1; 2112 } 2113 2114 /* Legacy. Similar, but return the result directly. */ 2115 2116 REAL_VALUE_TYPE 2117 real_from_string2 (const char *s, enum machine_mode mode) 2118 { 2119 REAL_VALUE_TYPE r; 2120 2121 real_from_string (&r, s); 2122 if (mode != VOIDmode) 2123 real_convert (&r, mode, &r); 2124 2125 return r; 2126 } 2127 2128 /* Initialize R from string S and desired MODE. */ 2129 2130 void 2131 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode) 2132 { 2133 if (DECIMAL_FLOAT_MODE_P (mode)) 2134 decimal_real_from_string (r, s); 2135 else 2136 real_from_string (r, s); 2137 2138 if (mode != VOIDmode) 2139 real_convert (r, mode, r); 2140 } 2141 2142 /* Initialize R from the integer pair HIGH+LOW. */ 2143 2144 void 2145 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode, 2146 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high, 2147 int unsigned_p) 2148 { 2149 if (low == 0 && high == 0) 2150 get_zero (r, 0); 2151 else 2152 { 2153 memset (r, 0, sizeof (*r)); 2154 r->cl = rvc_normal; 2155 r->sign = high < 0 && !unsigned_p; 2156 SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT); 2157 2158 if (r->sign) 2159 { 2160 high = ~high; 2161 if (low == 0) 2162 high += 1; 2163 else 2164 low = -low; 2165 } 2166 2167 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT) 2168 { 2169 r->sig[SIGSZ-1] = high; 2170 r->sig[SIGSZ-2] = low; 2171 } 2172 else 2173 { 2174 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT); 2175 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1; 2176 r->sig[SIGSZ-2] = high; 2177 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1; 2178 r->sig[SIGSZ-4] = low; 2179 } 2180 2181 normalize (r); 2182 } 2183 2184 if (DECIMAL_FLOAT_MODE_P (mode)) 2185 decimal_from_integer (r); 2186 else if (mode != VOIDmode) 2187 real_convert (r, mode, r); 2188 } 2189 2190 /* Render R, an integral value, as a floating point constant with no 2191 specified exponent. */ 2192 2193 static void 2194 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig, 2195 size_t buf_size) 2196 { 2197 int dec_exp, digit, digits; 2198 REAL_VALUE_TYPE r, pten; 2199 char *p; 2200 bool sign; 2201 2202 r = *r_orig; 2203 2204 if (r.cl == rvc_zero) 2205 { 2206 strcpy (str, "0."); 2207 return; 2208 } 2209 2210 sign = r.sign; 2211 r.sign = 0; 2212 2213 dec_exp = REAL_EXP (&r) * M_LOG10_2; 2214 digits = dec_exp + 1; 2215 gcc_assert ((digits + 2) < (int)buf_size); 2216 2217 pten = *real_digit (1); 2218 times_pten (&pten, dec_exp); 2219 2220 p = str; 2221 if (sign) 2222 *p++ = '-'; 2223 2224 digit = rtd_divmod (&r, &pten); 2225 gcc_assert (digit >= 0 && digit <= 9); 2226 *p++ = digit + '0'; 2227 while (--digits > 0) 2228 { 2229 times_pten (&r, 1); 2230 digit = rtd_divmod (&r, &pten); 2231 *p++ = digit + '0'; 2232 } 2233 *p++ = '.'; 2234 *p++ = '\0'; 2235 } 2236 2237 /* Convert a real with an integral value to decimal float. */ 2238 2239 static void 2240 decimal_from_integer (REAL_VALUE_TYPE *r) 2241 { 2242 char str[256]; 2243 2244 decimal_integer_string (str, r, sizeof (str) - 1); 2245 decimal_real_from_string (r, str); 2246 } 2247 2248 /* Returns 10**2**N. */ 2249 2250 static const REAL_VALUE_TYPE * 2251 ten_to_ptwo (int n) 2252 { 2253 static REAL_VALUE_TYPE tens[EXP_BITS]; 2254 2255 gcc_assert (n >= 0); 2256 gcc_assert (n < EXP_BITS); 2257 2258 if (tens[n].cl == rvc_zero) 2259 { 2260 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4)) 2261 { 2262 HOST_WIDE_INT t = 10; 2263 int i; 2264 2265 for (i = 0; i < n; ++i) 2266 t *= t; 2267 2268 real_from_integer (&tens[n], VOIDmode, t, 0, 1); 2269 } 2270 else 2271 { 2272 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1); 2273 do_multiply (&tens[n], t, t); 2274 } 2275 } 2276 2277 return &tens[n]; 2278 } 2279 2280 /* Returns 10**(-2**N). */ 2281 2282 static const REAL_VALUE_TYPE * 2283 ten_to_mptwo (int n) 2284 { 2285 static REAL_VALUE_TYPE tens[EXP_BITS]; 2286 2287 gcc_assert (n >= 0); 2288 gcc_assert (n < EXP_BITS); 2289 2290 if (tens[n].cl == rvc_zero) 2291 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n)); 2292 2293 return &tens[n]; 2294 } 2295 2296 /* Returns N. */ 2297 2298 static const REAL_VALUE_TYPE * 2299 real_digit (int n) 2300 { 2301 static REAL_VALUE_TYPE num[10]; 2302 2303 gcc_assert (n >= 0); 2304 gcc_assert (n <= 9); 2305 2306 if (n > 0 && num[n].cl == rvc_zero) 2307 real_from_integer (&num[n], VOIDmode, n, 0, 1); 2308 2309 return &num[n]; 2310 } 2311 2312 /* Multiply R by 10**EXP. */ 2313 2314 static void 2315 times_pten (REAL_VALUE_TYPE *r, int exp) 2316 { 2317 REAL_VALUE_TYPE pten, *rr; 2318 bool negative = (exp < 0); 2319 int i; 2320 2321 if (negative) 2322 { 2323 exp = -exp; 2324 pten = *real_digit (1); 2325 rr = &pten; 2326 } 2327 else 2328 rr = r; 2329 2330 for (i = 0; exp > 0; ++i, exp >>= 1) 2331 if (exp & 1) 2332 do_multiply (rr, rr, ten_to_ptwo (i)); 2333 2334 if (negative) 2335 do_divide (r, r, &pten); 2336 } 2337 2338 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */ 2339 2340 const REAL_VALUE_TYPE * 2341 dconst_e_ptr (void) 2342 { 2343 static REAL_VALUE_TYPE value; 2344 2345 /* Initialize mathematical constants for constant folding builtins. 2346 These constants need to be given to at least 160 bits precision. */ 2347 if (value.cl == rvc_zero) 2348 { 2349 mpfr_t m; 2350 mpfr_init2 (m, SIGNIFICAND_BITS); 2351 mpfr_set_ui (m, 1, GMP_RNDN); 2352 mpfr_exp (m, m, GMP_RNDN); 2353 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN); 2354 mpfr_clear (m); 2355 2356 } 2357 return &value; 2358 } 2359 2360 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */ 2361 2362 const REAL_VALUE_TYPE * 2363 dconst_third_ptr (void) 2364 { 2365 static REAL_VALUE_TYPE value; 2366 2367 /* Initialize mathematical constants for constant folding builtins. 2368 These constants need to be given to at least 160 bits precision. */ 2369 if (value.cl == rvc_zero) 2370 { 2371 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3)); 2372 } 2373 return &value; 2374 } 2375 2376 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */ 2377 2378 const REAL_VALUE_TYPE * 2379 dconst_sqrt2_ptr (void) 2380 { 2381 static REAL_VALUE_TYPE value; 2382 2383 /* Initialize mathematical constants for constant folding builtins. 2384 These constants need to be given to at least 160 bits precision. */ 2385 if (value.cl == rvc_zero) 2386 { 2387 mpfr_t m; 2388 mpfr_init2 (m, SIGNIFICAND_BITS); 2389 mpfr_sqrt_ui (m, 2, GMP_RNDN); 2390 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN); 2391 mpfr_clear (m); 2392 } 2393 return &value; 2394 } 2395 2396 /* Fills R with +Inf. */ 2397 2398 void 2399 real_inf (REAL_VALUE_TYPE *r) 2400 { 2401 get_inf (r, 0); 2402 } 2403 2404 /* Fills R with a NaN whose significand is described by STR. If QUIET, 2405 we force a QNaN, else we force an SNaN. The string, if not empty, 2406 is parsed as a number and placed in the significand. Return true 2407 if the string was successfully parsed. */ 2408 2409 bool 2410 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet, 2411 enum machine_mode mode) 2412 { 2413 const struct real_format *fmt; 2414 2415 fmt = REAL_MODE_FORMAT (mode); 2416 gcc_assert (fmt); 2417 2418 if (*str == 0) 2419 { 2420 if (quiet) 2421 get_canonical_qnan (r, 0); 2422 else 2423 get_canonical_snan (r, 0); 2424 } 2425 else 2426 { 2427 int base = 10, d; 2428 2429 memset (r, 0, sizeof (*r)); 2430 r->cl = rvc_nan; 2431 2432 /* Parse akin to strtol into the significand of R. */ 2433 2434 while (ISSPACE (*str)) 2435 str++; 2436 if (*str == '-') 2437 str++; 2438 else if (*str == '+') 2439 str++; 2440 if (*str == '0') 2441 { 2442 str++; 2443 if (*str == 'x' || *str == 'X') 2444 { 2445 base = 16; 2446 str++; 2447 } 2448 else 2449 base = 8; 2450 } 2451 2452 while ((d = hex_value (*str)) < base) 2453 { 2454 REAL_VALUE_TYPE u; 2455 2456 switch (base) 2457 { 2458 case 8: 2459 lshift_significand (r, r, 3); 2460 break; 2461 case 16: 2462 lshift_significand (r, r, 4); 2463 break; 2464 case 10: 2465 lshift_significand_1 (&u, r); 2466 lshift_significand (r, r, 3); 2467 add_significands (r, r, &u); 2468 break; 2469 default: 2470 gcc_unreachable (); 2471 } 2472 2473 get_zero (&u, 0); 2474 u.sig[0] = d; 2475 add_significands (r, r, &u); 2476 2477 str++; 2478 } 2479 2480 /* Must have consumed the entire string for success. */ 2481 if (*str != 0) 2482 return false; 2483 2484 /* Shift the significand into place such that the bits 2485 are in the most significant bits for the format. */ 2486 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan); 2487 2488 /* Our MSB is always unset for NaNs. */ 2489 r->sig[SIGSZ-1] &= ~SIG_MSB; 2490 2491 /* Force quiet or signalling NaN. */ 2492 r->signalling = !quiet; 2493 } 2494 2495 return true; 2496 } 2497 2498 /* Fills R with the largest finite value representable in mode MODE. 2499 If SIGN is nonzero, R is set to the most negative finite value. */ 2500 2501 void 2502 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode) 2503 { 2504 const struct real_format *fmt; 2505 int np2; 2506 2507 fmt = REAL_MODE_FORMAT (mode); 2508 gcc_assert (fmt); 2509 memset (r, 0, sizeof (*r)); 2510 2511 if (fmt->b == 10) 2512 decimal_real_maxval (r, sign, mode); 2513 else 2514 { 2515 r->cl = rvc_normal; 2516 r->sign = sign; 2517 SET_REAL_EXP (r, fmt->emax); 2518 2519 np2 = SIGNIFICAND_BITS - fmt->p; 2520 memset (r->sig, -1, SIGSZ * sizeof (unsigned long)); 2521 clear_significand_below (r, np2); 2522 2523 if (fmt->pnan < fmt->p) 2524 /* This is an IBM extended double format made up of two IEEE 2525 doubles. The value of the long double is the sum of the 2526 values of the two parts. The most significant part is 2527 required to be the value of the long double rounded to the 2528 nearest double. Rounding means we need a slightly smaller 2529 value for LDBL_MAX. */ 2530 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1); 2531 } 2532 } 2533 2534 /* Fills R with 2**N. */ 2535 2536 void 2537 real_2expN (REAL_VALUE_TYPE *r, int n, enum machine_mode fmode) 2538 { 2539 memset (r, 0, sizeof (*r)); 2540 2541 n++; 2542 if (n > MAX_EXP) 2543 r->cl = rvc_inf; 2544 else if (n < -MAX_EXP) 2545 ; 2546 else 2547 { 2548 r->cl = rvc_normal; 2549 SET_REAL_EXP (r, n); 2550 r->sig[SIGSZ-1] = SIG_MSB; 2551 } 2552 if (DECIMAL_FLOAT_MODE_P (fmode)) 2553 decimal_real_convert (r, fmode, r); 2554 } 2555 2556 2557 static void 2558 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r) 2559 { 2560 int p2, np2, i, w; 2561 int emin2m1, emax2; 2562 bool round_up = false; 2563 2564 if (r->decimal) 2565 { 2566 if (fmt->b == 10) 2567 { 2568 decimal_round_for_format (fmt, r); 2569 return; 2570 } 2571 /* FIXME. We can come here via fp_easy_constant 2572 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not 2573 investigated whether this convert needs to be here, or 2574 something else is missing. */ 2575 decimal_real_convert (r, DFmode, r); 2576 } 2577 2578 p2 = fmt->p; 2579 emin2m1 = fmt->emin - 1; 2580 emax2 = fmt->emax; 2581 2582 np2 = SIGNIFICAND_BITS - p2; 2583 switch (r->cl) 2584 { 2585 underflow: 2586 get_zero (r, r->sign); 2587 case rvc_zero: 2588 if (!fmt->has_signed_zero) 2589 r->sign = 0; 2590 return; 2591 2592 overflow: 2593 get_inf (r, r->sign); 2594 case rvc_inf: 2595 return; 2596 2597 case rvc_nan: 2598 clear_significand_below (r, np2); 2599 return; 2600 2601 case rvc_normal: 2602 break; 2603 2604 default: 2605 gcc_unreachable (); 2606 } 2607 2608 /* Check the range of the exponent. If we're out of range, 2609 either underflow or overflow. */ 2610 if (REAL_EXP (r) > emax2) 2611 goto overflow; 2612 else if (REAL_EXP (r) <= emin2m1) 2613 { 2614 int diff; 2615 2616 if (!fmt->has_denorm) 2617 { 2618 /* Don't underflow completely until we've had a chance to round. */ 2619 if (REAL_EXP (r) < emin2m1) 2620 goto underflow; 2621 } 2622 else 2623 { 2624 diff = emin2m1 - REAL_EXP (r) + 1; 2625 if (diff > p2) 2626 goto underflow; 2627 2628 /* De-normalize the significand. */ 2629 r->sig[0] |= sticky_rshift_significand (r, r, diff); 2630 SET_REAL_EXP (r, REAL_EXP (r) + diff); 2631 } 2632 } 2633 2634 if (!fmt->round_towards_zero) 2635 { 2636 /* There are P2 true significand bits, followed by one guard bit, 2637 followed by one sticky bit, followed by stuff. Fold nonzero 2638 stuff into the sticky bit. */ 2639 unsigned long sticky; 2640 bool guard, lsb; 2641 2642 sticky = 0; 2643 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i) 2644 sticky |= r->sig[i]; 2645 sticky |= r->sig[w] 2646 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1); 2647 2648 guard = test_significand_bit (r, np2 - 1); 2649 lsb = test_significand_bit (r, np2); 2650 2651 /* Round to even. */ 2652 round_up = guard && (sticky || lsb); 2653 } 2654 2655 if (round_up) 2656 { 2657 REAL_VALUE_TYPE u; 2658 get_zero (&u, 0); 2659 set_significand_bit (&u, np2); 2660 2661 if (add_significands (r, r, &u)) 2662 { 2663 /* Overflow. Means the significand had been all ones, and 2664 is now all zeros. Need to increase the exponent, and 2665 possibly re-normalize it. */ 2666 SET_REAL_EXP (r, REAL_EXP (r) + 1); 2667 if (REAL_EXP (r) > emax2) 2668 goto overflow; 2669 r->sig[SIGSZ-1] = SIG_MSB; 2670 } 2671 } 2672 2673 /* Catch underflow that we deferred until after rounding. */ 2674 if (REAL_EXP (r) <= emin2m1) 2675 goto underflow; 2676 2677 /* Clear out trailing garbage. */ 2678 clear_significand_below (r, np2); 2679 } 2680 2681 /* Extend or truncate to a new mode. */ 2682 2683 void 2684 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode, 2685 const REAL_VALUE_TYPE *a) 2686 { 2687 const struct real_format *fmt; 2688 2689 fmt = REAL_MODE_FORMAT (mode); 2690 gcc_assert (fmt); 2691 2692 *r = *a; 2693 2694 if (a->decimal || fmt->b == 10) 2695 decimal_real_convert (r, mode, a); 2696 2697 round_for_format (fmt, r); 2698 2699 /* round_for_format de-normalizes denormals. Undo just that part. */ 2700 if (r->cl == rvc_normal) 2701 normalize (r); 2702 } 2703 2704 /* Legacy. Likewise, except return the struct directly. */ 2705 2706 REAL_VALUE_TYPE 2707 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a) 2708 { 2709 REAL_VALUE_TYPE r; 2710 real_convert (&r, mode, &a); 2711 return r; 2712 } 2713 2714 /* Return true if truncating to MODE is exact. */ 2715 2716 bool 2717 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a) 2718 { 2719 const struct real_format *fmt; 2720 REAL_VALUE_TYPE t; 2721 int emin2m1; 2722 2723 fmt = REAL_MODE_FORMAT (mode); 2724 gcc_assert (fmt); 2725 2726 /* Don't allow conversion to denormals. */ 2727 emin2m1 = fmt->emin - 1; 2728 if (REAL_EXP (a) <= emin2m1) 2729 return false; 2730 2731 /* After conversion to the new mode, the value must be identical. */ 2732 real_convert (&t, mode, a); 2733 return real_identical (&t, a); 2734 } 2735 2736 /* Write R to the given target format. Place the words of the result 2737 in target word order in BUF. There are always 32 bits in each 2738 long, no matter the size of the host long. 2739 2740 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */ 2741 2742 long 2743 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig, 2744 const struct real_format *fmt) 2745 { 2746 REAL_VALUE_TYPE r; 2747 long buf1; 2748 2749 r = *r_orig; 2750 round_for_format (fmt, &r); 2751 2752 if (!buf) 2753 buf = &buf1; 2754 (*fmt->encode) (fmt, buf, &r); 2755 2756 return *buf; 2757 } 2758 2759 /* Similar, but look up the format from MODE. */ 2760 2761 long 2762 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode) 2763 { 2764 const struct real_format *fmt; 2765 2766 fmt = REAL_MODE_FORMAT (mode); 2767 gcc_assert (fmt); 2768 2769 return real_to_target_fmt (buf, r, fmt); 2770 } 2771 2772 /* Read R from the given target format. Read the words of the result 2773 in target word order in BUF. There are always 32 bits in each 2774 long, no matter the size of the host long. */ 2775 2776 void 2777 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf, 2778 const struct real_format *fmt) 2779 { 2780 (*fmt->decode) (fmt, r, buf); 2781 } 2782 2783 /* Similar, but look up the format from MODE. */ 2784 2785 void 2786 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode) 2787 { 2788 const struct real_format *fmt; 2789 2790 fmt = REAL_MODE_FORMAT (mode); 2791 gcc_assert (fmt); 2792 2793 (*fmt->decode) (fmt, r, buf); 2794 } 2795 2796 /* Return the number of bits of the largest binary value that the 2797 significand of MODE will hold. */ 2798 /* ??? Legacy. Should get access to real_format directly. */ 2799 2800 int 2801 significand_size (enum machine_mode mode) 2802 { 2803 const struct real_format *fmt; 2804 2805 fmt = REAL_MODE_FORMAT (mode); 2806 if (fmt == NULL) 2807 return 0; 2808 2809 if (fmt->b == 10) 2810 { 2811 /* Return the size in bits of the largest binary value that can be 2812 held by the decimal coefficient for this mode. This is one more 2813 than the number of bits required to hold the largest coefficient 2814 of this mode. */ 2815 double log2_10 = 3.3219281; 2816 return fmt->p * log2_10; 2817 } 2818 return fmt->p; 2819 } 2820 2821 /* Return a hash value for the given real value. */ 2822 /* ??? The "unsigned int" return value is intended to be hashval_t, 2823 but I didn't want to pull hashtab.h into real.h. */ 2824 2825 unsigned int 2826 real_hash (const REAL_VALUE_TYPE *r) 2827 { 2828 unsigned int h; 2829 size_t i; 2830 2831 h = r->cl | (r->sign << 2); 2832 switch (r->cl) 2833 { 2834 case rvc_zero: 2835 case rvc_inf: 2836 return h; 2837 2838 case rvc_normal: 2839 h |= REAL_EXP (r) << 3; 2840 break; 2841 2842 case rvc_nan: 2843 if (r->signalling) 2844 h ^= (unsigned int)-1; 2845 if (r->canonical) 2846 return h; 2847 break; 2848 2849 default: 2850 gcc_unreachable (); 2851 } 2852 2853 if (sizeof(unsigned long) > sizeof(unsigned int)) 2854 for (i = 0; i < SIGSZ; ++i) 2855 { 2856 unsigned long s = r->sig[i]; 2857 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2)); 2858 } 2859 else 2860 for (i = 0; i < SIGSZ; ++i) 2861 h ^= r->sig[i]; 2862 2863 return h; 2864 } 2865 2866 /* IEEE single-precision format. */ 2867 2868 static void encode_ieee_single (const struct real_format *fmt, 2869 long *, const REAL_VALUE_TYPE *); 2870 static void decode_ieee_single (const struct real_format *, 2871 REAL_VALUE_TYPE *, const long *); 2872 2873 static void 2874 encode_ieee_single (const struct real_format *fmt, long *buf, 2875 const REAL_VALUE_TYPE *r) 2876 { 2877 unsigned long image, sig, exp; 2878 unsigned long sign = r->sign; 2879 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0; 2880 2881 image = sign << 31; 2882 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff; 2883 2884 switch (r->cl) 2885 { 2886 case rvc_zero: 2887 break; 2888 2889 case rvc_inf: 2890 if (fmt->has_inf) 2891 image |= 255 << 23; 2892 else 2893 image |= 0x7fffffff; 2894 break; 2895 2896 case rvc_nan: 2897 if (fmt->has_nans) 2898 { 2899 if (r->canonical) 2900 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0); 2901 if (r->signalling == fmt->qnan_msb_set) 2902 sig &= ~(1 << 22); 2903 else 2904 sig |= 1 << 22; 2905 if (sig == 0) 2906 sig = 1 << 21; 2907 2908 image |= 255 << 23; 2909 image |= sig; 2910 } 2911 else 2912 image |= 0x7fffffff; 2913 break; 2914 2915 case rvc_normal: 2916 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp, 2917 whereas the intermediate representation is 0.F x 2**exp. 2918 Which means we're off by one. */ 2919 if (denormal) 2920 exp = 0; 2921 else 2922 exp = REAL_EXP (r) + 127 - 1; 2923 image |= exp << 23; 2924 image |= sig; 2925 break; 2926 2927 default: 2928 gcc_unreachable (); 2929 } 2930 2931 buf[0] = image; 2932 } 2933 2934 static void 2935 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r, 2936 const long *buf) 2937 { 2938 unsigned long image = buf[0] & 0xffffffff; 2939 bool sign = (image >> 31) & 1; 2940 int exp = (image >> 23) & 0xff; 2941 2942 memset (r, 0, sizeof (*r)); 2943 image <<= HOST_BITS_PER_LONG - 24; 2944 image &= ~SIG_MSB; 2945 2946 if (exp == 0) 2947 { 2948 if (image && fmt->has_denorm) 2949 { 2950 r->cl = rvc_normal; 2951 r->sign = sign; 2952 SET_REAL_EXP (r, -126); 2953 r->sig[SIGSZ-1] = image << 1; 2954 normalize (r); 2955 } 2956 else if (fmt->has_signed_zero) 2957 r->sign = sign; 2958 } 2959 else if (exp == 255 && (fmt->has_nans || fmt->has_inf)) 2960 { 2961 if (image) 2962 { 2963 r->cl = rvc_nan; 2964 r->sign = sign; 2965 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1) 2966 ^ fmt->qnan_msb_set); 2967 r->sig[SIGSZ-1] = image; 2968 } 2969 else 2970 { 2971 r->cl = rvc_inf; 2972 r->sign = sign; 2973 } 2974 } 2975 else 2976 { 2977 r->cl = rvc_normal; 2978 r->sign = sign; 2979 SET_REAL_EXP (r, exp - 127 + 1); 2980 r->sig[SIGSZ-1] = image | SIG_MSB; 2981 } 2982 } 2983 2984 const struct real_format ieee_single_format = 2985 { 2986 encode_ieee_single, 2987 decode_ieee_single, 2988 2, 2989 24, 2990 24, 2991 -125, 2992 128, 2993 31, 2994 31, 2995 false, 2996 true, 2997 true, 2998 true, 2999 true, 3000 true, 3001 true, 3002 false 3003 }; 3004 3005 const struct real_format mips_single_format = 3006 { 3007 encode_ieee_single, 3008 decode_ieee_single, 3009 2, 3010 24, 3011 24, 3012 -125, 3013 128, 3014 31, 3015 31, 3016 false, 3017 true, 3018 true, 3019 true, 3020 true, 3021 true, 3022 false, 3023 true 3024 }; 3025 3026 const struct real_format motorola_single_format = 3027 { 3028 encode_ieee_single, 3029 decode_ieee_single, 3030 2, 3031 24, 3032 24, 3033 -125, 3034 128, 3035 31, 3036 31, 3037 false, 3038 true, 3039 true, 3040 true, 3041 true, 3042 true, 3043 true, 3044 true 3045 }; 3046 3047 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE 3048 single precision with the following differences: 3049 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT 3050 are generated. 3051 - NaNs are not supported. 3052 - The range of non-zero numbers in binary is 3053 (001)[1.]000...000 to (255)[1.]111...111. 3054 - Denormals can be represented, but are treated as +0.0 when 3055 used as an operand and are never generated as a result. 3056 - -0.0 can be represented, but a zero result is always +0.0. 3057 - the only supported rounding mode is trunction (towards zero). */ 3058 const struct real_format spu_single_format = 3059 { 3060 encode_ieee_single, 3061 decode_ieee_single, 3062 2, 3063 24, 3064 24, 3065 -125, 3066 129, 3067 31, 3068 31, 3069 true, 3070 false, 3071 false, 3072 false, 3073 true, 3074 true, 3075 false, 3076 false 3077 }; 3078 3079 /* IEEE double-precision format. */ 3080 3081 static void encode_ieee_double (const struct real_format *fmt, 3082 long *, const REAL_VALUE_TYPE *); 3083 static void decode_ieee_double (const struct real_format *, 3084 REAL_VALUE_TYPE *, const long *); 3085 3086 static void 3087 encode_ieee_double (const struct real_format *fmt, long *buf, 3088 const REAL_VALUE_TYPE *r) 3089 { 3090 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp; 3091 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0; 3092 3093 image_hi = r->sign << 31; 3094 image_lo = 0; 3095 3096 if (HOST_BITS_PER_LONG == 64) 3097 { 3098 sig_hi = r->sig[SIGSZ-1]; 3099 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff; 3100 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff; 3101 } 3102 else 3103 { 3104 sig_hi = r->sig[SIGSZ-1]; 3105 sig_lo = r->sig[SIGSZ-2]; 3106 sig_lo = (sig_hi << 21) | (sig_lo >> 11); 3107 sig_hi = (sig_hi >> 11) & 0xfffff; 3108 } 3109 3110 switch (r->cl) 3111 { 3112 case rvc_zero: 3113 break; 3114 3115 case rvc_inf: 3116 if (fmt->has_inf) 3117 image_hi |= 2047 << 20; 3118 else 3119 { 3120 image_hi |= 0x7fffffff; 3121 image_lo = 0xffffffff; 3122 } 3123 break; 3124 3125 case rvc_nan: 3126 if (fmt->has_nans) 3127 { 3128 if (r->canonical) 3129 { 3130 if (fmt->canonical_nan_lsbs_set) 3131 { 3132 sig_hi = (1 << 19) - 1; 3133 sig_lo = 0xffffffff; 3134 } 3135 else 3136 { 3137 sig_hi = 0; 3138 sig_lo = 0; 3139 } 3140 } 3141 if (r->signalling == fmt->qnan_msb_set) 3142 sig_hi &= ~(1 << 19); 3143 else 3144 sig_hi |= 1 << 19; 3145 if (sig_hi == 0 && sig_lo == 0) 3146 sig_hi = 1 << 18; 3147 3148 image_hi |= 2047 << 20; 3149 image_hi |= sig_hi; 3150 image_lo = sig_lo; 3151 } 3152 else 3153 { 3154 image_hi |= 0x7fffffff; 3155 image_lo = 0xffffffff; 3156 } 3157 break; 3158 3159 case rvc_normal: 3160 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp, 3161 whereas the intermediate representation is 0.F x 2**exp. 3162 Which means we're off by one. */ 3163 if (denormal) 3164 exp = 0; 3165 else 3166 exp = REAL_EXP (r) + 1023 - 1; 3167 image_hi |= exp << 20; 3168 image_hi |= sig_hi; 3169 image_lo = sig_lo; 3170 break; 3171 3172 default: 3173 gcc_unreachable (); 3174 } 3175 3176 if (FLOAT_WORDS_BIG_ENDIAN) 3177 buf[0] = image_hi, buf[1] = image_lo; 3178 else 3179 buf[0] = image_lo, buf[1] = image_hi; 3180 } 3181 3182 static void 3183 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r, 3184 const long *buf) 3185 { 3186 unsigned long image_hi, image_lo; 3187 bool sign; 3188 int exp; 3189 3190 if (FLOAT_WORDS_BIG_ENDIAN) 3191 image_hi = buf[0], image_lo = buf[1]; 3192 else 3193 image_lo = buf[0], image_hi = buf[1]; 3194 image_lo &= 0xffffffff; 3195 image_hi &= 0xffffffff; 3196 3197 sign = (image_hi >> 31) & 1; 3198 exp = (image_hi >> 20) & 0x7ff; 3199 3200 memset (r, 0, sizeof (*r)); 3201 3202 image_hi <<= 32 - 21; 3203 image_hi |= image_lo >> 21; 3204 image_hi &= 0x7fffffff; 3205 image_lo <<= 32 - 21; 3206 3207 if (exp == 0) 3208 { 3209 if ((image_hi || image_lo) && fmt->has_denorm) 3210 { 3211 r->cl = rvc_normal; 3212 r->sign = sign; 3213 SET_REAL_EXP (r, -1022); 3214 if (HOST_BITS_PER_LONG == 32) 3215 { 3216 image_hi = (image_hi << 1) | (image_lo >> 31); 3217 image_lo <<= 1; 3218 r->sig[SIGSZ-1] = image_hi; 3219 r->sig[SIGSZ-2] = image_lo; 3220 } 3221 else 3222 { 3223 image_hi = (image_hi << 31 << 2) | (image_lo << 1); 3224 r->sig[SIGSZ-1] = image_hi; 3225 } 3226 normalize (r); 3227 } 3228 else if (fmt->has_signed_zero) 3229 r->sign = sign; 3230 } 3231 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf)) 3232 { 3233 if (image_hi || image_lo) 3234 { 3235 r->cl = rvc_nan; 3236 r->sign = sign; 3237 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set; 3238 if (HOST_BITS_PER_LONG == 32) 3239 { 3240 r->sig[SIGSZ-1] = image_hi; 3241 r->sig[SIGSZ-2] = image_lo; 3242 } 3243 else 3244 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo; 3245 } 3246 else 3247 { 3248 r->cl = rvc_inf; 3249 r->sign = sign; 3250 } 3251 } 3252 else 3253 { 3254 r->cl = rvc_normal; 3255 r->sign = sign; 3256 SET_REAL_EXP (r, exp - 1023 + 1); 3257 if (HOST_BITS_PER_LONG == 32) 3258 { 3259 r->sig[SIGSZ-1] = image_hi | SIG_MSB; 3260 r->sig[SIGSZ-2] = image_lo; 3261 } 3262 else 3263 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB; 3264 } 3265 } 3266 3267 const struct real_format ieee_double_format = 3268 { 3269 encode_ieee_double, 3270 decode_ieee_double, 3271 2, 3272 53, 3273 53, 3274 -1021, 3275 1024, 3276 63, 3277 63, 3278 false, 3279 true, 3280 true, 3281 true, 3282 true, 3283 true, 3284 true, 3285 false 3286 }; 3287 3288 const struct real_format mips_double_format = 3289 { 3290 encode_ieee_double, 3291 decode_ieee_double, 3292 2, 3293 53, 3294 53, 3295 -1021, 3296 1024, 3297 63, 3298 63, 3299 false, 3300 true, 3301 true, 3302 true, 3303 true, 3304 true, 3305 false, 3306 true 3307 }; 3308 3309 const struct real_format motorola_double_format = 3310 { 3311 encode_ieee_double, 3312 decode_ieee_double, 3313 2, 3314 53, 3315 53, 3316 -1021, 3317 1024, 3318 63, 3319 63, 3320 false, 3321 true, 3322 true, 3323 true, 3324 true, 3325 true, 3326 true, 3327 true 3328 }; 3329 3330 /* IEEE extended real format. This comes in three flavors: Intel's as 3331 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel 3332 12- and 16-byte images may be big- or little endian; Motorola's is 3333 always big endian. */ 3334 3335 /* Helper subroutine which converts from the internal format to the 3336 12-byte little-endian Intel format. Functions below adjust this 3337 for the other possible formats. */ 3338 static void 3339 encode_ieee_extended (const struct real_format *fmt, long *buf, 3340 const REAL_VALUE_TYPE *r) 3341 { 3342 unsigned long image_hi, sig_hi, sig_lo; 3343 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0; 3344 3345 image_hi = r->sign << 15; 3346 sig_hi = sig_lo = 0; 3347 3348 switch (r->cl) 3349 { 3350 case rvc_zero: 3351 break; 3352 3353 case rvc_inf: 3354 if (fmt->has_inf) 3355 { 3356 image_hi |= 32767; 3357 3358 /* Intel requires the explicit integer bit to be set, otherwise 3359 it considers the value a "pseudo-infinity". Motorola docs 3360 say it doesn't care. */ 3361 sig_hi = 0x80000000; 3362 } 3363 else 3364 { 3365 image_hi |= 32767; 3366 sig_lo = sig_hi = 0xffffffff; 3367 } 3368 break; 3369 3370 case rvc_nan: 3371 if (fmt->has_nans) 3372 { 3373 image_hi |= 32767; 3374 if (r->canonical) 3375 { 3376 if (fmt->canonical_nan_lsbs_set) 3377 { 3378 sig_hi = (1 << 30) - 1; 3379 sig_lo = 0xffffffff; 3380 } 3381 } 3382 else if (HOST_BITS_PER_LONG == 32) 3383 { 3384 sig_hi = r->sig[SIGSZ-1]; 3385 sig_lo = r->sig[SIGSZ-2]; 3386 } 3387 else 3388 { 3389 sig_lo = r->sig[SIGSZ-1]; 3390 sig_hi = sig_lo >> 31 >> 1; 3391 sig_lo &= 0xffffffff; 3392 } 3393 if (r->signalling == fmt->qnan_msb_set) 3394 sig_hi &= ~(1 << 30); 3395 else 3396 sig_hi |= 1 << 30; 3397 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0) 3398 sig_hi = 1 << 29; 3399 3400 /* Intel requires the explicit integer bit to be set, otherwise 3401 it considers the value a "pseudo-nan". Motorola docs say it 3402 doesn't care. */ 3403 sig_hi |= 0x80000000; 3404 } 3405 else 3406 { 3407 image_hi |= 32767; 3408 sig_lo = sig_hi = 0xffffffff; 3409 } 3410 break; 3411 3412 case rvc_normal: 3413 { 3414 int exp = REAL_EXP (r); 3415 3416 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp, 3417 whereas the intermediate representation is 0.F x 2**exp. 3418 Which means we're off by one. 3419 3420 Except for Motorola, which consider exp=0 and explicit 3421 integer bit set to continue to be normalized. In theory 3422 this discrepancy has been taken care of by the difference 3423 in fmt->emin in round_for_format. */ 3424 3425 if (denormal) 3426 exp = 0; 3427 else 3428 { 3429 exp += 16383 - 1; 3430 gcc_assert (exp >= 0); 3431 } 3432 image_hi |= exp; 3433 3434 if (HOST_BITS_PER_LONG == 32) 3435 { 3436 sig_hi = r->sig[SIGSZ-1]; 3437 sig_lo = r->sig[SIGSZ-2]; 3438 } 3439 else 3440 { 3441 sig_lo = r->sig[SIGSZ-1]; 3442 sig_hi = sig_lo >> 31 >> 1; 3443 sig_lo &= 0xffffffff; 3444 } 3445 } 3446 break; 3447 3448 default: 3449 gcc_unreachable (); 3450 } 3451 3452 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi; 3453 } 3454 3455 /* Convert from the internal format to the 12-byte Motorola format 3456 for an IEEE extended real. */ 3457 static void 3458 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf, 3459 const REAL_VALUE_TYPE *r) 3460 { 3461 long intermed[3]; 3462 encode_ieee_extended (fmt, intermed, r); 3463 3464 /* Motorola chips are assumed always to be big-endian. Also, the 3465 padding in a Motorola extended real goes between the exponent and 3466 the mantissa. At this point the mantissa is entirely within 3467 elements 0 and 1 of intermed, and the exponent entirely within 3468 element 2, so all we have to do is swap the order around, and 3469 shift element 2 left 16 bits. */ 3470 buf[0] = intermed[2] << 16; 3471 buf[1] = intermed[1]; 3472 buf[2] = intermed[0]; 3473 } 3474 3475 /* Convert from the internal format to the 12-byte Intel format for 3476 an IEEE extended real. */ 3477 static void 3478 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf, 3479 const REAL_VALUE_TYPE *r) 3480 { 3481 if (FLOAT_WORDS_BIG_ENDIAN) 3482 { 3483 /* All the padding in an Intel-format extended real goes at the high 3484 end, which in this case is after the mantissa, not the exponent. 3485 Therefore we must shift everything down 16 bits. */ 3486 long intermed[3]; 3487 encode_ieee_extended (fmt, intermed, r); 3488 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16)); 3489 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16)); 3490 buf[2] = (intermed[0] << 16); 3491 } 3492 else 3493 /* encode_ieee_extended produces what we want directly. */ 3494 encode_ieee_extended (fmt, buf, r); 3495 } 3496 3497 /* Convert from the internal format to the 16-byte Intel format for 3498 an IEEE extended real. */ 3499 static void 3500 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf, 3501 const REAL_VALUE_TYPE *r) 3502 { 3503 /* All the padding in an Intel-format extended real goes at the high end. */ 3504 encode_ieee_extended_intel_96 (fmt, buf, r); 3505 buf[3] = 0; 3506 } 3507 3508 /* As above, we have a helper function which converts from 12-byte 3509 little-endian Intel format to internal format. Functions below 3510 adjust for the other possible formats. */ 3511 static void 3512 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r, 3513 const long *buf) 3514 { 3515 unsigned long image_hi, sig_hi, sig_lo; 3516 bool sign; 3517 int exp; 3518 3519 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2]; 3520 sig_lo &= 0xffffffff; 3521 sig_hi &= 0xffffffff; 3522 image_hi &= 0xffffffff; 3523 3524 sign = (image_hi >> 15) & 1; 3525 exp = image_hi & 0x7fff; 3526 3527 memset (r, 0, sizeof (*r)); 3528 3529 if (exp == 0) 3530 { 3531 if ((sig_hi || sig_lo) && fmt->has_denorm) 3532 { 3533 r->cl = rvc_normal; 3534 r->sign = sign; 3535 3536 /* When the IEEE format contains a hidden bit, we know that 3537 it's zero at this point, and so shift up the significand 3538 and decrease the exponent to match. In this case, Motorola 3539 defines the explicit integer bit to be valid, so we don't 3540 know whether the msb is set or not. */ 3541 SET_REAL_EXP (r, fmt->emin); 3542 if (HOST_BITS_PER_LONG == 32) 3543 { 3544 r->sig[SIGSZ-1] = sig_hi; 3545 r->sig[SIGSZ-2] = sig_lo; 3546 } 3547 else 3548 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo; 3549 3550 normalize (r); 3551 } 3552 else if (fmt->has_signed_zero) 3553 r->sign = sign; 3554 } 3555 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf)) 3556 { 3557 /* See above re "pseudo-infinities" and "pseudo-nans". 3558 Short summary is that the MSB will likely always be 3559 set, and that we don't care about it. */ 3560 sig_hi &= 0x7fffffff; 3561 3562 if (sig_hi || sig_lo) 3563 { 3564 r->cl = rvc_nan; 3565 r->sign = sign; 3566 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set; 3567 if (HOST_BITS_PER_LONG == 32) 3568 { 3569 r->sig[SIGSZ-1] = sig_hi; 3570 r->sig[SIGSZ-2] = sig_lo; 3571 } 3572 else 3573 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo; 3574 } 3575 else 3576 { 3577 r->cl = rvc_inf; 3578 r->sign = sign; 3579 } 3580 } 3581 else 3582 { 3583 r->cl = rvc_normal; 3584 r->sign = sign; 3585 SET_REAL_EXP (r, exp - 16383 + 1); 3586 if (HOST_BITS_PER_LONG == 32) 3587 { 3588 r->sig[SIGSZ-1] = sig_hi; 3589 r->sig[SIGSZ-2] = sig_lo; 3590 } 3591 else 3592 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo; 3593 } 3594 } 3595 3596 /* Convert from the internal format to the 12-byte Motorola format 3597 for an IEEE extended real. */ 3598 static void 3599 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r, 3600 const long *buf) 3601 { 3602 long intermed[3]; 3603 3604 /* Motorola chips are assumed always to be big-endian. Also, the 3605 padding in a Motorola extended real goes between the exponent and 3606 the mantissa; remove it. */ 3607 intermed[0] = buf[2]; 3608 intermed[1] = buf[1]; 3609 intermed[2] = (unsigned long)buf[0] >> 16; 3610 3611 decode_ieee_extended (fmt, r, intermed); 3612 } 3613 3614 /* Convert from the internal format to the 12-byte Intel format for 3615 an IEEE extended real. */ 3616 static void 3617 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r, 3618 const long *buf) 3619 { 3620 if (FLOAT_WORDS_BIG_ENDIAN) 3621 { 3622 /* All the padding in an Intel-format extended real goes at the high 3623 end, which in this case is after the mantissa, not the exponent. 3624 Therefore we must shift everything up 16 bits. */ 3625 long intermed[3]; 3626 3627 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16)); 3628 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16)); 3629 intermed[2] = ((unsigned long)buf[0] >> 16); 3630 3631 decode_ieee_extended (fmt, r, intermed); 3632 } 3633 else 3634 /* decode_ieee_extended produces what we want directly. */ 3635 decode_ieee_extended (fmt, r, buf); 3636 } 3637 3638 /* Convert from the internal format to the 16-byte Intel format for 3639 an IEEE extended real. */ 3640 static void 3641 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r, 3642 const long *buf) 3643 { 3644 /* All the padding in an Intel-format extended real goes at the high end. */ 3645 decode_ieee_extended_intel_96 (fmt, r, buf); 3646 } 3647 3648 const struct real_format ieee_extended_motorola_format = 3649 { 3650 encode_ieee_extended_motorola, 3651 decode_ieee_extended_motorola, 3652 2, 3653 64, 3654 64, 3655 -16382, 3656 16384, 3657 95, 3658 95, 3659 false, 3660 true, 3661 true, 3662 true, 3663 true, 3664 true, 3665 true, 3666 true 3667 }; 3668 3669 const struct real_format ieee_extended_intel_96_format = 3670 { 3671 encode_ieee_extended_intel_96, 3672 decode_ieee_extended_intel_96, 3673 2, 3674 64, 3675 64, 3676 -16381, 3677 16384, 3678 79, 3679 79, 3680 false, 3681 true, 3682 true, 3683 true, 3684 true, 3685 true, 3686 true, 3687 false 3688 }; 3689 3690 const struct real_format ieee_extended_intel_128_format = 3691 { 3692 encode_ieee_extended_intel_128, 3693 decode_ieee_extended_intel_128, 3694 2, 3695 64, 3696 64, 3697 -16381, 3698 16384, 3699 79, 3700 79, 3701 false, 3702 true, 3703 true, 3704 true, 3705 true, 3706 true, 3707 true, 3708 false 3709 }; 3710 3711 /* The following caters to i386 systems that set the rounding precision 3712 to 53 bits instead of 64, e.g. FreeBSD. */ 3713 const struct real_format ieee_extended_intel_96_round_53_format = 3714 { 3715 encode_ieee_extended_intel_96, 3716 decode_ieee_extended_intel_96, 3717 2, 3718 53, 3719 53, 3720 -16381, 3721 16384, 3722 79, 3723 79, 3724 false, 3725 true, 3726 true, 3727 true, 3728 true, 3729 true, 3730 true, 3731 false 3732 }; 3733 3734 /* IBM 128-bit extended precision format: a pair of IEEE double precision 3735 numbers whose sum is equal to the extended precision value. The number 3736 with greater magnitude is first. This format has the same magnitude 3737 range as an IEEE double precision value, but effectively 106 bits of 3738 significand precision. Infinity and NaN are represented by their IEEE 3739 double precision value stored in the first number, the second number is 3740 +0.0 or -0.0 for Infinity and don't-care for NaN. */ 3741 3742 static void encode_ibm_extended (const struct real_format *fmt, 3743 long *, const REAL_VALUE_TYPE *); 3744 static void decode_ibm_extended (const struct real_format *, 3745 REAL_VALUE_TYPE *, const long *); 3746 3747 static void 3748 encode_ibm_extended (const struct real_format *fmt, long *buf, 3749 const REAL_VALUE_TYPE *r) 3750 { 3751 REAL_VALUE_TYPE u, normr, v; 3752 const struct real_format *base_fmt; 3753 3754 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format; 3755 3756 /* Renormalize R before doing any arithmetic on it. */ 3757 normr = *r; 3758 if (normr.cl == rvc_normal) 3759 normalize (&normr); 3760 3761 /* u = IEEE double precision portion of significand. */ 3762 u = normr; 3763 round_for_format (base_fmt, &u); 3764 encode_ieee_double (base_fmt, &buf[0], &u); 3765 3766 if (u.cl == rvc_normal) 3767 { 3768 do_add (&v, &normr, &u, 1); 3769 /* Call round_for_format since we might need to denormalize. */ 3770 round_for_format (base_fmt, &v); 3771 encode_ieee_double (base_fmt, &buf[2], &v); 3772 } 3773 else 3774 { 3775 /* Inf, NaN, 0 are all representable as doubles, so the 3776 least-significant part can be 0.0. */ 3777 buf[2] = 0; 3778 buf[3] = 0; 3779 } 3780 } 3781 3782 static void 3783 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r, 3784 const long *buf) 3785 { 3786 REAL_VALUE_TYPE u, v; 3787 const struct real_format *base_fmt; 3788 3789 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format; 3790 decode_ieee_double (base_fmt, &u, &buf[0]); 3791 3792 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan) 3793 { 3794 decode_ieee_double (base_fmt, &v, &buf[2]); 3795 do_add (r, &u, &v, 0); 3796 } 3797 else 3798 *r = u; 3799 } 3800 3801 const struct real_format ibm_extended_format = 3802 { 3803 encode_ibm_extended, 3804 decode_ibm_extended, 3805 2, 3806 53 + 53, 3807 53, 3808 -1021 + 53, 3809 1024, 3810 127, 3811 -1, 3812 false, 3813 true, 3814 true, 3815 true, 3816 true, 3817 true, 3818 true, 3819 false 3820 }; 3821 3822 const struct real_format mips_extended_format = 3823 { 3824 encode_ibm_extended, 3825 decode_ibm_extended, 3826 2, 3827 53 + 53, 3828 53, 3829 -1021 + 53, 3830 1024, 3831 127, 3832 -1, 3833 false, 3834 true, 3835 true, 3836 true, 3837 true, 3838 true, 3839 false, 3840 true 3841 }; 3842 3843 3844 /* IEEE quad precision format. */ 3845 3846 static void encode_ieee_quad (const struct real_format *fmt, 3847 long *, const REAL_VALUE_TYPE *); 3848 static void decode_ieee_quad (const struct real_format *, 3849 REAL_VALUE_TYPE *, const long *); 3850 3851 static void 3852 encode_ieee_quad (const struct real_format *fmt, long *buf, 3853 const REAL_VALUE_TYPE *r) 3854 { 3855 unsigned long image3, image2, image1, image0, exp; 3856 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0; 3857 REAL_VALUE_TYPE u; 3858 3859 image3 = r->sign << 31; 3860 image2 = 0; 3861 image1 = 0; 3862 image0 = 0; 3863 3864 rshift_significand (&u, r, SIGNIFICAND_BITS - 113); 3865 3866 switch (r->cl) 3867 { 3868 case rvc_zero: 3869 break; 3870 3871 case rvc_inf: 3872 if (fmt->has_inf) 3873 image3 |= 32767 << 16; 3874 else 3875 { 3876 image3 |= 0x7fffffff; 3877 image2 = 0xffffffff; 3878 image1 = 0xffffffff; 3879 image0 = 0xffffffff; 3880 } 3881 break; 3882 3883 case rvc_nan: 3884 if (fmt->has_nans) 3885 { 3886 image3 |= 32767 << 16; 3887 3888 if (r->canonical) 3889 { 3890 if (fmt->canonical_nan_lsbs_set) 3891 { 3892 image3 |= 0x7fff; 3893 image2 = image1 = image0 = 0xffffffff; 3894 } 3895 } 3896 else if (HOST_BITS_PER_LONG == 32) 3897 { 3898 image0 = u.sig[0]; 3899 image1 = u.sig[1]; 3900 image2 = u.sig[2]; 3901 image3 |= u.sig[3] & 0xffff; 3902 } 3903 else 3904 { 3905 image0 = u.sig[0]; 3906 image1 = image0 >> 31 >> 1; 3907 image2 = u.sig[1]; 3908 image3 |= (image2 >> 31 >> 1) & 0xffff; 3909 image0 &= 0xffffffff; 3910 image2 &= 0xffffffff; 3911 } 3912 if (r->signalling == fmt->qnan_msb_set) 3913 image3 &= ~0x8000; 3914 else 3915 image3 |= 0x8000; 3916 if (((image3 & 0xffff) | image2 | image1 | image0) == 0) 3917 image3 |= 0x4000; 3918 } 3919 else 3920 { 3921 image3 |= 0x7fffffff; 3922 image2 = 0xffffffff; 3923 image1 = 0xffffffff; 3924 image0 = 0xffffffff; 3925 } 3926 break; 3927 3928 case rvc_normal: 3929 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp, 3930 whereas the intermediate representation is 0.F x 2**exp. 3931 Which means we're off by one. */ 3932 if (denormal) 3933 exp = 0; 3934 else 3935 exp = REAL_EXP (r) + 16383 - 1; 3936 image3 |= exp << 16; 3937 3938 if (HOST_BITS_PER_LONG == 32) 3939 { 3940 image0 = u.sig[0]; 3941 image1 = u.sig[1]; 3942 image2 = u.sig[2]; 3943 image3 |= u.sig[3] & 0xffff; 3944 } 3945 else 3946 { 3947 image0 = u.sig[0]; 3948 image1 = image0 >> 31 >> 1; 3949 image2 = u.sig[1]; 3950 image3 |= (image2 >> 31 >> 1) & 0xffff; 3951 image0 &= 0xffffffff; 3952 image2 &= 0xffffffff; 3953 } 3954 break; 3955 3956 default: 3957 gcc_unreachable (); 3958 } 3959 3960 if (FLOAT_WORDS_BIG_ENDIAN) 3961 { 3962 buf[0] = image3; 3963 buf[1] = image2; 3964 buf[2] = image1; 3965 buf[3] = image0; 3966 } 3967 else 3968 { 3969 buf[0] = image0; 3970 buf[1] = image1; 3971 buf[2] = image2; 3972 buf[3] = image3; 3973 } 3974 } 3975 3976 static void 3977 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r, 3978 const long *buf) 3979 { 3980 unsigned long image3, image2, image1, image0; 3981 bool sign; 3982 int exp; 3983 3984 if (FLOAT_WORDS_BIG_ENDIAN) 3985 { 3986 image3 = buf[0]; 3987 image2 = buf[1]; 3988 image1 = buf[2]; 3989 image0 = buf[3]; 3990 } 3991 else 3992 { 3993 image0 = buf[0]; 3994 image1 = buf[1]; 3995 image2 = buf[2]; 3996 image3 = buf[3]; 3997 } 3998 image0 &= 0xffffffff; 3999 image1 &= 0xffffffff; 4000 image2 &= 0xffffffff; 4001 4002 sign = (image3 >> 31) & 1; 4003 exp = (image3 >> 16) & 0x7fff; 4004 image3 &= 0xffff; 4005 4006 memset (r, 0, sizeof (*r)); 4007 4008 if (exp == 0) 4009 { 4010 if ((image3 | image2 | image1 | image0) && fmt->has_denorm) 4011 { 4012 r->cl = rvc_normal; 4013 r->sign = sign; 4014 4015 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112)); 4016 if (HOST_BITS_PER_LONG == 32) 4017 { 4018 r->sig[0] = image0; 4019 r->sig[1] = image1; 4020 r->sig[2] = image2; 4021 r->sig[3] = image3; 4022 } 4023 else 4024 { 4025 r->sig[0] = (image1 << 31 << 1) | image0; 4026 r->sig[1] = (image3 << 31 << 1) | image2; 4027 } 4028 4029 normalize (r); 4030 } 4031 else if (fmt->has_signed_zero) 4032 r->sign = sign; 4033 } 4034 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf)) 4035 { 4036 if (image3 | image2 | image1 | image0) 4037 { 4038 r->cl = rvc_nan; 4039 r->sign = sign; 4040 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set; 4041 4042 if (HOST_BITS_PER_LONG == 32) 4043 { 4044 r->sig[0] = image0; 4045 r->sig[1] = image1; 4046 r->sig[2] = image2; 4047 r->sig[3] = image3; 4048 } 4049 else 4050 { 4051 r->sig[0] = (image1 << 31 << 1) | image0; 4052 r->sig[1] = (image3 << 31 << 1) | image2; 4053 } 4054 lshift_significand (r, r, SIGNIFICAND_BITS - 113); 4055 } 4056 else 4057 { 4058 r->cl = rvc_inf; 4059 r->sign = sign; 4060 } 4061 } 4062 else 4063 { 4064 r->cl = rvc_normal; 4065 r->sign = sign; 4066 SET_REAL_EXP (r, exp - 16383 + 1); 4067 4068 if (HOST_BITS_PER_LONG == 32) 4069 { 4070 r->sig[0] = image0; 4071 r->sig[1] = image1; 4072 r->sig[2] = image2; 4073 r->sig[3] = image3; 4074 } 4075 else 4076 { 4077 r->sig[0] = (image1 << 31 << 1) | image0; 4078 r->sig[1] = (image3 << 31 << 1) | image2; 4079 } 4080 lshift_significand (r, r, SIGNIFICAND_BITS - 113); 4081 r->sig[SIGSZ-1] |= SIG_MSB; 4082 } 4083 } 4084 4085 const struct real_format ieee_quad_format = 4086 { 4087 encode_ieee_quad, 4088 decode_ieee_quad, 4089 2, 4090 113, 4091 113, 4092 -16381, 4093 16384, 4094 127, 4095 127, 4096 false, 4097 true, 4098 true, 4099 true, 4100 true, 4101 true, 4102 true, 4103 false 4104 }; 4105 4106 const struct real_format mips_quad_format = 4107 { 4108 encode_ieee_quad, 4109 decode_ieee_quad, 4110 2, 4111 113, 4112 113, 4113 -16381, 4114 16384, 4115 127, 4116 127, 4117 false, 4118 true, 4119 true, 4120 true, 4121 true, 4122 true, 4123 false, 4124 true 4125 }; 4126 4127 /* Descriptions of VAX floating point formats can be found beginning at 4128 4129 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format 4130 4131 The thing to remember is that they're almost IEEE, except for word 4132 order, exponent bias, and the lack of infinities, nans, and denormals. 4133 4134 We don't implement the H_floating format here, simply because neither 4135 the VAX or Alpha ports use it. */ 4136 4137 static void encode_vax_f (const struct real_format *fmt, 4138 long *, const REAL_VALUE_TYPE *); 4139 static void decode_vax_f (const struct real_format *, 4140 REAL_VALUE_TYPE *, const long *); 4141 static void encode_vax_d (const struct real_format *fmt, 4142 long *, const REAL_VALUE_TYPE *); 4143 static void decode_vax_d (const struct real_format *, 4144 REAL_VALUE_TYPE *, const long *); 4145 static void encode_vax_g (const struct real_format *fmt, 4146 long *, const REAL_VALUE_TYPE *); 4147 static void decode_vax_g (const struct real_format *, 4148 REAL_VALUE_TYPE *, const long *); 4149 4150 static void 4151 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf, 4152 const REAL_VALUE_TYPE *r) 4153 { 4154 unsigned long sign, exp, sig, image; 4155 4156 sign = r->sign << 15; 4157 4158 switch (r->cl) 4159 { 4160 case rvc_zero: 4161 image = 0; 4162 break; 4163 4164 case rvc_inf: 4165 case rvc_nan: 4166 image = 0xffff7fff | sign; 4167 break; 4168 4169 case rvc_normal: 4170 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff; 4171 exp = REAL_EXP (r) + 128; 4172 4173 image = (sig << 16) & 0xffff0000; 4174 image |= sign; 4175 image |= exp << 7; 4176 image |= sig >> 16; 4177 break; 4178 4179 default: 4180 gcc_unreachable (); 4181 } 4182 4183 buf[0] = image; 4184 } 4185 4186 static void 4187 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, 4188 REAL_VALUE_TYPE *r, const long *buf) 4189 { 4190 unsigned long image = buf[0] & 0xffffffff; 4191 int exp = (image >> 7) & 0xff; 4192 4193 memset (r, 0, sizeof (*r)); 4194 4195 if (exp != 0) 4196 { 4197 r->cl = rvc_normal; 4198 r->sign = (image >> 15) & 1; 4199 SET_REAL_EXP (r, exp - 128); 4200 4201 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff); 4202 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB; 4203 } 4204 } 4205 4206 static void 4207 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf, 4208 const REAL_VALUE_TYPE *r) 4209 { 4210 unsigned long image0, image1, sign = r->sign << 15; 4211 4212 switch (r->cl) 4213 { 4214 case rvc_zero: 4215 image0 = image1 = 0; 4216 break; 4217 4218 case rvc_inf: 4219 case rvc_nan: 4220 image0 = 0xffff7fff | sign; 4221 image1 = 0xffffffff; 4222 break; 4223 4224 case rvc_normal: 4225 /* Extract the significand into straight hi:lo. */ 4226 if (HOST_BITS_PER_LONG == 64) 4227 { 4228 image0 = r->sig[SIGSZ-1]; 4229 image1 = (image0 >> (64 - 56)) & 0xffffffff; 4230 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff; 4231 } 4232 else 4233 { 4234 image0 = r->sig[SIGSZ-1]; 4235 image1 = r->sig[SIGSZ-2]; 4236 image1 = (image0 << 24) | (image1 >> 8); 4237 image0 = (image0 >> 8) & 0xffffff; 4238 } 4239 4240 /* Rearrange the half-words of the significand to match the 4241 external format. */ 4242 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f; 4243 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff; 4244 4245 /* Add the sign and exponent. */ 4246 image0 |= sign; 4247 image0 |= (REAL_EXP (r) + 128) << 7; 4248 break; 4249 4250 default: 4251 gcc_unreachable (); 4252 } 4253 4254 if (FLOAT_WORDS_BIG_ENDIAN) 4255 buf[0] = image1, buf[1] = image0; 4256 else 4257 buf[0] = image0, buf[1] = image1; 4258 } 4259 4260 static void 4261 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, 4262 REAL_VALUE_TYPE *r, const long *buf) 4263 { 4264 unsigned long image0, image1; 4265 int exp; 4266 4267 if (FLOAT_WORDS_BIG_ENDIAN) 4268 image1 = buf[0], image0 = buf[1]; 4269 else 4270 image0 = buf[0], image1 = buf[1]; 4271 image0 &= 0xffffffff; 4272 image1 &= 0xffffffff; 4273 4274 exp = (image0 >> 7) & 0xff; 4275 4276 memset (r, 0, sizeof (*r)); 4277 4278 if (exp != 0) 4279 { 4280 r->cl = rvc_normal; 4281 r->sign = (image0 >> 15) & 1; 4282 SET_REAL_EXP (r, exp - 128); 4283 4284 /* Rearrange the half-words of the external format into 4285 proper ascending order. */ 4286 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff); 4287 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff); 4288 4289 if (HOST_BITS_PER_LONG == 64) 4290 { 4291 image0 = (image0 << 31 << 1) | image1; 4292 image0 <<= 64 - 56; 4293 image0 |= SIG_MSB; 4294 r->sig[SIGSZ-1] = image0; 4295 } 4296 else 4297 { 4298 r->sig[SIGSZ-1] = image0; 4299 r->sig[SIGSZ-2] = image1; 4300 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56); 4301 r->sig[SIGSZ-1] |= SIG_MSB; 4302 } 4303 } 4304 } 4305 4306 static void 4307 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf, 4308 const REAL_VALUE_TYPE *r) 4309 { 4310 unsigned long image0, image1, sign = r->sign << 15; 4311 4312 switch (r->cl) 4313 { 4314 case rvc_zero: 4315 image0 = image1 = 0; 4316 break; 4317 4318 case rvc_inf: 4319 case rvc_nan: 4320 image0 = 0xffff7fff | sign; 4321 image1 = 0xffffffff; 4322 break; 4323 4324 case rvc_normal: 4325 /* Extract the significand into straight hi:lo. */ 4326 if (HOST_BITS_PER_LONG == 64) 4327 { 4328 image0 = r->sig[SIGSZ-1]; 4329 image1 = (image0 >> (64 - 53)) & 0xffffffff; 4330 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff; 4331 } 4332 else 4333 { 4334 image0 = r->sig[SIGSZ-1]; 4335 image1 = r->sig[SIGSZ-2]; 4336 image1 = (image0 << 21) | (image1 >> 11); 4337 image0 = (image0 >> 11) & 0xfffff; 4338 } 4339 4340 /* Rearrange the half-words of the significand to match the 4341 external format. */ 4342 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f; 4343 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff; 4344 4345 /* Add the sign and exponent. */ 4346 image0 |= sign; 4347 image0 |= (REAL_EXP (r) + 1024) << 4; 4348 break; 4349 4350 default: 4351 gcc_unreachable (); 4352 } 4353 4354 if (FLOAT_WORDS_BIG_ENDIAN) 4355 buf[0] = image1, buf[1] = image0; 4356 else 4357 buf[0] = image0, buf[1] = image1; 4358 } 4359 4360 static void 4361 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, 4362 REAL_VALUE_TYPE *r, const long *buf) 4363 { 4364 unsigned long image0, image1; 4365 int exp; 4366 4367 if (FLOAT_WORDS_BIG_ENDIAN) 4368 image1 = buf[0], image0 = buf[1]; 4369 else 4370 image0 = buf[0], image1 = buf[1]; 4371 image0 &= 0xffffffff; 4372 image1 &= 0xffffffff; 4373 4374 exp = (image0 >> 4) & 0x7ff; 4375 4376 memset (r, 0, sizeof (*r)); 4377 4378 if (exp != 0) 4379 { 4380 r->cl = rvc_normal; 4381 r->sign = (image0 >> 15) & 1; 4382 SET_REAL_EXP (r, exp - 1024); 4383 4384 /* Rearrange the half-words of the external format into 4385 proper ascending order. */ 4386 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff); 4387 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff); 4388 4389 if (HOST_BITS_PER_LONG == 64) 4390 { 4391 image0 = (image0 << 31 << 1) | image1; 4392 image0 <<= 64 - 53; 4393 image0 |= SIG_MSB; 4394 r->sig[SIGSZ-1] = image0; 4395 } 4396 else 4397 { 4398 r->sig[SIGSZ-1] = image0; 4399 r->sig[SIGSZ-2] = image1; 4400 lshift_significand (r, r, 64 - 53); 4401 r->sig[SIGSZ-1] |= SIG_MSB; 4402 } 4403 } 4404 } 4405 4406 const struct real_format vax_f_format = 4407 { 4408 encode_vax_f, 4409 decode_vax_f, 4410 2, 4411 24, 4412 24, 4413 -127, 4414 127, 4415 15, 4416 15, 4417 false, 4418 false, 4419 false, 4420 false, 4421 false, 4422 false, 4423 false, 4424 false 4425 }; 4426 4427 const struct real_format vax_d_format = 4428 { 4429 encode_vax_d, 4430 decode_vax_d, 4431 2, 4432 56, 4433 56, 4434 -127, 4435 127, 4436 15, 4437 15, 4438 false, 4439 false, 4440 false, 4441 false, 4442 false, 4443 false, 4444 false, 4445 false 4446 }; 4447 4448 const struct real_format vax_g_format = 4449 { 4450 encode_vax_g, 4451 decode_vax_g, 4452 2, 4453 53, 4454 53, 4455 -1023, 4456 1023, 4457 15, 4458 15, 4459 false, 4460 false, 4461 false, 4462 false, 4463 false, 4464 false, 4465 false, 4466 false 4467 }; 4468 4469 /* Encode real R into a single precision DFP value in BUF. */ 4470 static void 4471 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED, 4472 long *buf ATTRIBUTE_UNUSED, 4473 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED) 4474 { 4475 encode_decimal32 (fmt, buf, r); 4476 } 4477 4478 /* Decode a single precision DFP value in BUF into a real R. */ 4479 static void 4480 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED, 4481 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED, 4482 const long *buf ATTRIBUTE_UNUSED) 4483 { 4484 decode_decimal32 (fmt, r, buf); 4485 } 4486 4487 /* Encode real R into a double precision DFP value in BUF. */ 4488 static void 4489 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED, 4490 long *buf ATTRIBUTE_UNUSED, 4491 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED) 4492 { 4493 encode_decimal64 (fmt, buf, r); 4494 } 4495 4496 /* Decode a double precision DFP value in BUF into a real R. */ 4497 static void 4498 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED, 4499 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED, 4500 const long *buf ATTRIBUTE_UNUSED) 4501 { 4502 decode_decimal64 (fmt, r, buf); 4503 } 4504 4505 /* Encode real R into a quad precision DFP value in BUF. */ 4506 static void 4507 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED, 4508 long *buf ATTRIBUTE_UNUSED, 4509 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED) 4510 { 4511 encode_decimal128 (fmt, buf, r); 4512 } 4513 4514 /* Decode a quad precision DFP value in BUF into a real R. */ 4515 static void 4516 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED, 4517 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED, 4518 const long *buf ATTRIBUTE_UNUSED) 4519 { 4520 decode_decimal128 (fmt, r, buf); 4521 } 4522 4523 /* Single precision decimal floating point (IEEE 754). */ 4524 const struct real_format decimal_single_format = 4525 { 4526 encode_decimal_single, 4527 decode_decimal_single, 4528 10, 4529 7, 4530 7, 4531 -94, 4532 97, 4533 31, 4534 31, 4535 false, 4536 true, 4537 true, 4538 true, 4539 true, 4540 true, 4541 true, 4542 false 4543 }; 4544 4545 /* Double precision decimal floating point (IEEE 754). */ 4546 const struct real_format decimal_double_format = 4547 { 4548 encode_decimal_double, 4549 decode_decimal_double, 4550 10, 4551 16, 4552 16, 4553 -382, 4554 385, 4555 63, 4556 63, 4557 false, 4558 true, 4559 true, 4560 true, 4561 true, 4562 true, 4563 true, 4564 false 4565 }; 4566 4567 /* Quad precision decimal floating point (IEEE 754). */ 4568 const struct real_format decimal_quad_format = 4569 { 4570 encode_decimal_quad, 4571 decode_decimal_quad, 4572 10, 4573 34, 4574 34, 4575 -6142, 4576 6145, 4577 127, 4578 127, 4579 false, 4580 true, 4581 true, 4582 true, 4583 true, 4584 true, 4585 true, 4586 false 4587 }; 4588 4589 /* Encode half-precision floats. This routine is used both for the IEEE 4590 ARM alternative encodings. */ 4591 static void 4592 encode_ieee_half (const struct real_format *fmt, long *buf, 4593 const REAL_VALUE_TYPE *r) 4594 { 4595 unsigned long image, sig, exp; 4596 unsigned long sign = r->sign; 4597 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0; 4598 4599 image = sign << 15; 4600 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff; 4601 4602 switch (r->cl) 4603 { 4604 case rvc_zero: 4605 break; 4606 4607 case rvc_inf: 4608 if (fmt->has_inf) 4609 image |= 31 << 10; 4610 else 4611 image |= 0x7fff; 4612 break; 4613 4614 case rvc_nan: 4615 if (fmt->has_nans) 4616 { 4617 if (r->canonical) 4618 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0); 4619 if (r->signalling == fmt->qnan_msb_set) 4620 sig &= ~(1 << 9); 4621 else 4622 sig |= 1 << 9; 4623 if (sig == 0) 4624 sig = 1 << 8; 4625 4626 image |= 31 << 10; 4627 image |= sig; 4628 } 4629 else 4630 image |= 0x3ff; 4631 break; 4632 4633 case rvc_normal: 4634 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp, 4635 whereas the intermediate representation is 0.F x 2**exp. 4636 Which means we're off by one. */ 4637 if (denormal) 4638 exp = 0; 4639 else 4640 exp = REAL_EXP (r) + 15 - 1; 4641 image |= exp << 10; 4642 image |= sig; 4643 break; 4644 4645 default: 4646 gcc_unreachable (); 4647 } 4648 4649 buf[0] = image; 4650 } 4651 4652 /* Decode half-precision floats. This routine is used both for the IEEE 4653 ARM alternative encodings. */ 4654 static void 4655 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r, 4656 const long *buf) 4657 { 4658 unsigned long image = buf[0] & 0xffff; 4659 bool sign = (image >> 15) & 1; 4660 int exp = (image >> 10) & 0x1f; 4661 4662 memset (r, 0, sizeof (*r)); 4663 image <<= HOST_BITS_PER_LONG - 11; 4664 image &= ~SIG_MSB; 4665 4666 if (exp == 0) 4667 { 4668 if (image && fmt->has_denorm) 4669 { 4670 r->cl = rvc_normal; 4671 r->sign = sign; 4672 SET_REAL_EXP (r, -14); 4673 r->sig[SIGSZ-1] = image << 1; 4674 normalize (r); 4675 } 4676 else if (fmt->has_signed_zero) 4677 r->sign = sign; 4678 } 4679 else if (exp == 31 && (fmt->has_nans || fmt->has_inf)) 4680 { 4681 if (image) 4682 { 4683 r->cl = rvc_nan; 4684 r->sign = sign; 4685 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1) 4686 ^ fmt->qnan_msb_set); 4687 r->sig[SIGSZ-1] = image; 4688 } 4689 else 4690 { 4691 r->cl = rvc_inf; 4692 r->sign = sign; 4693 } 4694 } 4695 else 4696 { 4697 r->cl = rvc_normal; 4698 r->sign = sign; 4699 SET_REAL_EXP (r, exp - 15 + 1); 4700 r->sig[SIGSZ-1] = image | SIG_MSB; 4701 } 4702 } 4703 4704 /* Half-precision format, as specified in IEEE 754R. */ 4705 const struct real_format ieee_half_format = 4706 { 4707 encode_ieee_half, 4708 decode_ieee_half, 4709 2, 4710 11, 4711 11, 4712 -13, 4713 16, 4714 15, 4715 15, 4716 false, 4717 true, 4718 true, 4719 true, 4720 true, 4721 true, 4722 true, 4723 false 4724 }; 4725 4726 /* ARM's alternative half-precision format, similar to IEEE but with 4727 no reserved exponent value for NaNs and infinities; rather, it just 4728 extends the range of exponents by one. */ 4729 const struct real_format arm_half_format = 4730 { 4731 encode_ieee_half, 4732 decode_ieee_half, 4733 2, 4734 11, 4735 11, 4736 -13, 4737 17, 4738 15, 4739 15, 4740 false, 4741 true, 4742 false, 4743 false, 4744 true, 4745 true, 4746 false, 4747 false 4748 }; 4749 4750 /* A synthetic "format" for internal arithmetic. It's the size of the 4751 internal significand minus the two bits needed for proper rounding. 4752 The encode and decode routines exist only to satisfy our paranoia 4753 harness. */ 4754 4755 static void encode_internal (const struct real_format *fmt, 4756 long *, const REAL_VALUE_TYPE *); 4757 static void decode_internal (const struct real_format *, 4758 REAL_VALUE_TYPE *, const long *); 4759 4760 static void 4761 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf, 4762 const REAL_VALUE_TYPE *r) 4763 { 4764 memcpy (buf, r, sizeof (*r)); 4765 } 4766 4767 static void 4768 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, 4769 REAL_VALUE_TYPE *r, const long *buf) 4770 { 4771 memcpy (r, buf, sizeof (*r)); 4772 } 4773 4774 const struct real_format real_internal_format = 4775 { 4776 encode_internal, 4777 decode_internal, 4778 2, 4779 SIGNIFICAND_BITS - 2, 4780 SIGNIFICAND_BITS - 2, 4781 -MAX_EXP, 4782 MAX_EXP, 4783 -1, 4784 -1, 4785 false, 4786 false, 4787 true, 4788 true, 4789 false, 4790 true, 4791 true, 4792 false 4793 }; 4794 4795 /* Calculate the square root of X in mode MODE, and store the result 4796 in R. Return TRUE if the operation does not raise an exception. 4797 For details see "High Precision Division and Square Root", 4798 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June 4799 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */ 4800 4801 bool 4802 real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode, 4803 const REAL_VALUE_TYPE *x) 4804 { 4805 static REAL_VALUE_TYPE halfthree; 4806 static bool init = false; 4807 REAL_VALUE_TYPE h, t, i; 4808 int iter, exp; 4809 4810 /* sqrt(-0.0) is -0.0. */ 4811 if (real_isnegzero (x)) 4812 { 4813 *r = *x; 4814 return false; 4815 } 4816 4817 /* Negative arguments return NaN. */ 4818 if (real_isneg (x)) 4819 { 4820 get_canonical_qnan (r, 0); 4821 return false; 4822 } 4823 4824 /* Infinity and NaN return themselves. */ 4825 if (!real_isfinite (x)) 4826 { 4827 *r = *x; 4828 return false; 4829 } 4830 4831 if (!init) 4832 { 4833 do_add (&halfthree, &dconst1, &dconsthalf, 0); 4834 init = true; 4835 } 4836 4837 /* Initial guess for reciprocal sqrt, i. */ 4838 exp = real_exponent (x); 4839 real_ldexp (&i, &dconst1, -exp/2); 4840 4841 /* Newton's iteration for reciprocal sqrt, i. */ 4842 for (iter = 0; iter < 16; iter++) 4843 { 4844 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */ 4845 do_multiply (&t, x, &i); 4846 do_multiply (&h, &t, &i); 4847 do_multiply (&t, &h, &dconsthalf); 4848 do_add (&h, &halfthree, &t, 1); 4849 do_multiply (&t, &i, &h); 4850 4851 /* Check for early convergence. */ 4852 if (iter >= 6 && real_identical (&i, &t)) 4853 break; 4854 4855 /* ??? Unroll loop to avoid copying. */ 4856 i = t; 4857 } 4858 4859 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */ 4860 do_multiply (&t, x, &i); 4861 do_multiply (&h, &t, &i); 4862 do_add (&i, &dconst1, &h, 1); 4863 do_multiply (&h, &t, &i); 4864 do_multiply (&i, &dconsthalf, &h); 4865 do_add (&h, &t, &i, 0); 4866 4867 /* ??? We need a Tuckerman test to get the last bit. */ 4868 4869 real_convert (r, mode, &h); 4870 return true; 4871 } 4872 4873 /* Calculate X raised to the integer exponent N in mode MODE and store 4874 the result in R. Return true if the result may be inexact due to 4875 loss of precision. The algorithm is the classic "left-to-right binary 4876 method" described in section 4.6.3 of Donald Knuth's "Seminumerical 4877 Algorithms", "The Art of Computer Programming", Volume 2. */ 4878 4879 bool 4880 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode, 4881 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n) 4882 { 4883 unsigned HOST_WIDE_INT bit; 4884 REAL_VALUE_TYPE t; 4885 bool inexact = false; 4886 bool init = false; 4887 bool neg; 4888 int i; 4889 4890 if (n == 0) 4891 { 4892 *r = dconst1; 4893 return false; 4894 } 4895 else if (n < 0) 4896 { 4897 /* Don't worry about overflow, from now on n is unsigned. */ 4898 neg = true; 4899 n = -n; 4900 } 4901 else 4902 neg = false; 4903 4904 t = *x; 4905 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1); 4906 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++) 4907 { 4908 if (init) 4909 { 4910 inexact |= do_multiply (&t, &t, &t); 4911 if (n & bit) 4912 inexact |= do_multiply (&t, &t, x); 4913 } 4914 else if (n & bit) 4915 init = true; 4916 bit >>= 1; 4917 } 4918 4919 if (neg) 4920 inexact |= do_divide (&t, &dconst1, &t); 4921 4922 real_convert (r, mode, &t); 4923 return inexact; 4924 } 4925 4926 /* Round X to the nearest integer not larger in absolute value, i.e. 4927 towards zero, placing the result in R in mode MODE. */ 4928 4929 void 4930 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode, 4931 const REAL_VALUE_TYPE *x) 4932 { 4933 do_fix_trunc (r, x); 4934 if (mode != VOIDmode) 4935 real_convert (r, mode, r); 4936 } 4937 4938 /* Round X to the largest integer not greater in value, i.e. round 4939 down, placing the result in R in mode MODE. */ 4940 4941 void 4942 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode, 4943 const REAL_VALUE_TYPE *x) 4944 { 4945 REAL_VALUE_TYPE t; 4946 4947 do_fix_trunc (&t, x); 4948 if (! real_identical (&t, x) && x->sign) 4949 do_add (&t, &t, &dconstm1, 0); 4950 if (mode != VOIDmode) 4951 real_convert (r, mode, &t); 4952 else 4953 *r = t; 4954 } 4955 4956 /* Round X to the smallest integer not less then argument, i.e. round 4957 up, placing the result in R in mode MODE. */ 4958 4959 void 4960 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode, 4961 const REAL_VALUE_TYPE *x) 4962 { 4963 REAL_VALUE_TYPE t; 4964 4965 do_fix_trunc (&t, x); 4966 if (! real_identical (&t, x) && ! x->sign) 4967 do_add (&t, &t, &dconst1, 0); 4968 if (mode != VOIDmode) 4969 real_convert (r, mode, &t); 4970 else 4971 *r = t; 4972 } 4973 4974 /* Round X to the nearest integer, but round halfway cases away from 4975 zero. */ 4976 4977 void 4978 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode, 4979 const REAL_VALUE_TYPE *x) 4980 { 4981 do_add (r, x, &dconsthalf, x->sign); 4982 do_fix_trunc (r, r); 4983 if (mode != VOIDmode) 4984 real_convert (r, mode, r); 4985 } 4986 4987 /* Set the sign of R to the sign of X. */ 4988 4989 void 4990 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x) 4991 { 4992 r->sign = x->sign; 4993 } 4994 4995 /* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible 4996 for initializing and clearing the MPFR parameter. */ 4997 4998 void 4999 mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode) 5000 { 5001 /* We use a string as an intermediate type. */ 5002 char buf[128]; 5003 int ret; 5004 5005 /* Take care of Infinity and NaN. */ 5006 if (r->cl == rvc_inf) 5007 { 5008 mpfr_set_inf (m, r->sign == 1 ? -1 : 1); 5009 return; 5010 } 5011 5012 if (r->cl == rvc_nan) 5013 { 5014 mpfr_set_nan (m); 5015 return; 5016 } 5017 5018 real_to_hexadecimal (buf, r, sizeof (buf), 0, 1); 5019 /* mpfr_set_str() parses hexadecimal floats from strings in the same 5020 format that GCC will output them. Nothing extra is needed. */ 5021 ret = mpfr_set_str (m, buf, 16, rndmode); 5022 gcc_assert (ret == 0); 5023 } 5024 5025 /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding 5026 mode RNDMODE. TYPE is only relevant if M is a NaN. */ 5027 5028 void 5029 real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode) 5030 { 5031 /* We use a string as an intermediate type. */ 5032 char buf[128], *rstr; 5033 mp_exp_t exp; 5034 5035 /* Take care of Infinity and NaN. */ 5036 if (mpfr_inf_p (m)) 5037 { 5038 real_inf (r); 5039 if (mpfr_sgn (m) < 0) 5040 *r = REAL_VALUE_NEGATE (*r); 5041 return; 5042 } 5043 5044 if (mpfr_nan_p (m)) 5045 { 5046 real_nan (r, "", 1, TYPE_MODE (type)); 5047 return; 5048 } 5049 5050 rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode); 5051 5052 /* The additional 12 chars add space for the sprintf below. This 5053 leaves 6 digits for the exponent which is supposedly enough. */ 5054 gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12); 5055 5056 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp, 5057 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust 5058 for that. */ 5059 exp *= 4; 5060 5061 if (rstr[0] == '-') 5062 sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp); 5063 else 5064 sprintf (buf, "0x.%sp%d", rstr, (int) exp); 5065 5066 mpfr_free_str (rstr); 5067 5068 real_from_string (r, buf); 5069 } 5070 5071 /* Check whether the real constant value given is an integer. */ 5072 5073 bool 5074 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode) 5075 { 5076 REAL_VALUE_TYPE cint; 5077 5078 real_trunc (&cint, mode, c); 5079 return real_identical (c, &cint); 5080 } 5081 5082 /* Write into BUF the maximum representable finite floating-point 5083 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex 5084 float string. LEN is the size of BUF, and the buffer must be large 5085 enough to contain the resulting string. */ 5086 5087 void 5088 get_max_float (const struct real_format *fmt, char *buf, size_t len) 5089 { 5090 int i, n; 5091 char *p; 5092 5093 strcpy (buf, "0x0."); 5094 n = fmt->p; 5095 for (i = 0, p = buf + 4; i + 3 < n; i += 4) 5096 *p++ = 'f'; 5097 if (i < n) 5098 *p++ = "08ce"[n - i]; 5099 sprintf (p, "p%d", fmt->emax); 5100 if (fmt->pnan < fmt->p) 5101 { 5102 /* This is an IBM extended double format made up of two IEEE 5103 doubles. The value of the long double is the sum of the 5104 values of the two parts. The most significant part is 5105 required to be the value of the long double rounded to the 5106 nearest double. Rounding means we need a slightly smaller 5107 value for LDBL_MAX. */ 5108 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4]; 5109 } 5110 5111 gcc_assert (strlen (buf) < len); 5112 } 5113