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