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