00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "config.h"
00029 #include "system.h"
00030 #ifdef SGI_MONGOOSE
00031
00032 #include "rtl.h"
00033 #endif
00034 #include "tree.h"
00035 #include "toplev.h"
00036 #include "real.h"
00037 #include "tm_p.h"
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 #define CLASS2(A, B) ((A) << 2 | (B))
00083
00084 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
00085 #error "Some constant folding done by hand to avoid shift count warnings"
00086 #endif
00087
00088 static void get_zero PARAMS ((REAL_VALUE_TYPE *, int));
00089 static void get_canonical_qnan PARAMS ((REAL_VALUE_TYPE *, int));
00090 static void get_canonical_snan PARAMS ((REAL_VALUE_TYPE *, int));
00091 static void get_inf PARAMS ((REAL_VALUE_TYPE *, int));
00092 static bool sticky_rshift_significand PARAMS ((REAL_VALUE_TYPE *,
00093 const REAL_VALUE_TYPE *,
00094 unsigned int));
00095 static void rshift_significand PARAMS ((REAL_VALUE_TYPE *,
00096 const REAL_VALUE_TYPE *,
00097 unsigned int));
00098 static void lshift_significand PARAMS ((REAL_VALUE_TYPE *,
00099 const REAL_VALUE_TYPE *,
00100 unsigned int));
00101 static void lshift_significand_1 PARAMS ((REAL_VALUE_TYPE *,
00102 const REAL_VALUE_TYPE *));
00103 static bool add_significands PARAMS ((REAL_VALUE_TYPE *r,
00104 const REAL_VALUE_TYPE *,
00105 const REAL_VALUE_TYPE *));
00106 static bool sub_significands PARAMS ((REAL_VALUE_TYPE *,
00107 const REAL_VALUE_TYPE *,
00108 const REAL_VALUE_TYPE *, int));
00109 static void neg_significand PARAMS ((REAL_VALUE_TYPE *,
00110 const REAL_VALUE_TYPE *));
00111 static int cmp_significands PARAMS ((const REAL_VALUE_TYPE *,
00112 const REAL_VALUE_TYPE *));
00113 static int cmp_significand_0 PARAMS ((const REAL_VALUE_TYPE *));
00114 static void set_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
00115 static void clear_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
00116 static bool test_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
00117 static void clear_significand_below PARAMS ((REAL_VALUE_TYPE *,
00118 unsigned int));
00119 static bool div_significands PARAMS ((REAL_VALUE_TYPE *,
00120 const REAL_VALUE_TYPE *,
00121 const REAL_VALUE_TYPE *));
00122 static void normalize PARAMS ((REAL_VALUE_TYPE *));
00123
00124 static void do_add PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
00125 const REAL_VALUE_TYPE *, int));
00126 static void do_multiply PARAMS ((REAL_VALUE_TYPE *,
00127 const REAL_VALUE_TYPE *,
00128 const REAL_VALUE_TYPE *));
00129 static void do_divide PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
00130 const REAL_VALUE_TYPE *));
00131 static int do_compare PARAMS ((const REAL_VALUE_TYPE *,
00132 const REAL_VALUE_TYPE *, int));
00133 static void do_fix_trunc PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *));
00134
00135 static unsigned long rtd_divmod PARAMS ((REAL_VALUE_TYPE *,
00136 REAL_VALUE_TYPE *));
00137
00138 static const REAL_VALUE_TYPE * ten_to_ptwo PARAMS ((int));
00139 static const REAL_VALUE_TYPE * ten_to_mptwo PARAMS ((int));
00140 static const REAL_VALUE_TYPE * real_digit PARAMS ((int));
00141 static void times_pten PARAMS ((REAL_VALUE_TYPE *, int));
00142
00143 static void round_for_format PARAMS ((const struct real_format *,
00144 REAL_VALUE_TYPE *));
00145
00146
00147
00148 static inline void
00149 get_zero (r, sign)
00150 REAL_VALUE_TYPE *r;
00151 int sign;
00152 {
00153 memset (r, 0, sizeof (*r));
00154 r->sign = sign;
00155 }
00156
00157
00158
00159 static inline void
00160 get_canonical_qnan (r, sign)
00161 REAL_VALUE_TYPE *r;
00162 int sign;
00163 {
00164 memset (r, 0, sizeof (*r));
00165 r->class = rvc_nan;
00166 r->sign = sign;
00167 r->sig[SIGSZ-1] = SIG_MSB >> 1;
00168 }
00169
00170 static inline void
00171 get_canonical_snan (r, sign)
00172 REAL_VALUE_TYPE *r;
00173 int sign;
00174 {
00175 memset (r, 0, sizeof (*r));
00176 r->class = rvc_nan;
00177 r->sign = sign;
00178 r->sig[SIGSZ-1] = SIG_MSB >> 2;
00179 }
00180
00181 static inline void
00182 get_inf (r, sign)
00183 REAL_VALUE_TYPE *r;
00184 int sign;
00185 {
00186 memset (r, 0, sizeof (*r));
00187 r->class = rvc_inf;
00188 r->sign = sign;
00189 }
00190
00191
00192
00193
00194
00195 static bool
00196 sticky_rshift_significand (r, a, n)
00197 REAL_VALUE_TYPE *r;
00198 const REAL_VALUE_TYPE *a;
00199 unsigned int n;
00200 {
00201 unsigned long sticky = 0;
00202 unsigned int i, ofs = 0;
00203
00204 if (n >= HOST_BITS_PER_LONG)
00205 {
00206 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
00207 sticky |= a->sig[i];
00208 n &= HOST_BITS_PER_LONG - 1;
00209 }
00210
00211 if (n != 0)
00212 {
00213 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
00214 for (i = 0; i < SIGSZ; ++i)
00215 {
00216 r->sig[i]
00217 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
00218 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
00219 << (HOST_BITS_PER_LONG - n)));
00220 }
00221 }
00222 else
00223 {
00224 for (i = 0; ofs + i < SIGSZ; ++i)
00225 r->sig[i] = a->sig[ofs + i];
00226 for (; i < SIGSZ; ++i)
00227 r->sig[i] = 0;
00228 }
00229
00230 return sticky != 0;
00231 }
00232
00233
00234
00235
00236 static void
00237 rshift_significand (r, a, n)
00238 REAL_VALUE_TYPE *r;
00239 const REAL_VALUE_TYPE *a;
00240 unsigned int n;
00241 {
00242 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
00243
00244 n &= HOST_BITS_PER_LONG - 1;
00245 if (n != 0)
00246 {
00247 for (i = 0; i < SIGSZ; ++i)
00248 {
00249 r->sig[i]
00250 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
00251 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
00252 << (HOST_BITS_PER_LONG - n)));
00253 }
00254 }
00255 else
00256 {
00257 for (i = 0; ofs + i < SIGSZ; ++i)
00258 r->sig[i] = a->sig[ofs + i];
00259 for (; i < SIGSZ; ++i)
00260 r->sig[i] = 0;
00261 }
00262 }
00263
00264
00265
00266
00267 static void
00268 lshift_significand (r, a, n)
00269 REAL_VALUE_TYPE *r;
00270 const REAL_VALUE_TYPE *a;
00271 unsigned int n;
00272 {
00273 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
00274
00275 n &= HOST_BITS_PER_LONG - 1;
00276 if (n == 0)
00277 {
00278 for (i = 0; ofs + i < SIGSZ; ++i)
00279 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
00280 for (; i < SIGSZ; ++i)
00281 r->sig[SIGSZ-1-i] = 0;
00282 }
00283 else
00284 for (i = 0; i < SIGSZ; ++i)
00285 {
00286 r->sig[SIGSZ-1-i]
00287 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
00288 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
00289 >> (HOST_BITS_PER_LONG - n)));
00290 }
00291 }
00292
00293
00294
00295 static inline void
00296 lshift_significand_1 (r, a)
00297 REAL_VALUE_TYPE *r;
00298 const REAL_VALUE_TYPE *a;
00299 {
00300 unsigned int i;
00301
00302 for (i = SIGSZ - 1; i > 0; --i)
00303 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
00304 r->sig[0] = a->sig[0] << 1;
00305 }
00306
00307
00308
00309
00310 static inline bool
00311 add_significands (r, a, b)
00312 REAL_VALUE_TYPE *r;
00313 const REAL_VALUE_TYPE *a, *b;
00314 {
00315 bool carry = false;
00316 int i;
00317
00318 for (i = 0; i < SIGSZ; ++i)
00319 {
00320 unsigned long ai = a->sig[i];
00321 unsigned long ri = ai + b->sig[i];
00322
00323 if (carry)
00324 {
00325 carry = ri < ai;
00326 carry |= ++ri == 0;
00327 }
00328 else
00329 carry = ri < ai;
00330
00331 r->sig[i] = ri;
00332 }
00333
00334 return carry;
00335 }
00336
00337
00338
00339
00340
00341 static inline bool
00342 sub_significands (r, a, b, carry)
00343 REAL_VALUE_TYPE *r;
00344 const REAL_VALUE_TYPE *a, *b;
00345 int carry;
00346 {
00347 int i;
00348
00349 for (i = 0; i < SIGSZ; ++i)
00350 {
00351 unsigned long ai = a->sig[i];
00352 unsigned long ri = ai - b->sig[i];
00353
00354 if (carry)
00355 {
00356 carry = ri > ai;
00357 carry |= ~--ri == 0;
00358 }
00359 else
00360 carry = ri > ai;
00361
00362 r->sig[i] = ri;
00363 }
00364
00365 return carry;
00366 }
00367
00368
00369
00370 static inline void
00371 neg_significand (r, a)
00372 REAL_VALUE_TYPE *r;
00373 const REAL_VALUE_TYPE *a;
00374 {
00375 bool carry = true;
00376 int i;
00377
00378 for (i = 0; i < SIGSZ; ++i)
00379 {
00380 unsigned long ri, ai = a->sig[i];
00381
00382 if (carry)
00383 {
00384 if (ai)
00385 {
00386 ri = -ai;
00387 carry = false;
00388 }
00389 else
00390 ri = ai;
00391 }
00392 else
00393 ri = ~ai;
00394
00395 r->sig[i] = ri;
00396 }
00397 }
00398
00399
00400
00401 static inline int
00402 cmp_significands (a, b)
00403 const REAL_VALUE_TYPE *a, *b;
00404 {
00405 int i;
00406
00407 for (i = SIGSZ - 1; i >= 0; --i)
00408 {
00409 unsigned long ai = a->sig[i];
00410 unsigned long bi = b->sig[i];
00411
00412 if (ai > bi)
00413 return 1;
00414 if (ai < bi)
00415 return -1;
00416 }
00417
00418 return 0;
00419 }
00420
00421
00422
00423 static inline int
00424 cmp_significand_0 (a)
00425 const REAL_VALUE_TYPE *a;
00426 {
00427 int i;
00428
00429 for (i = SIGSZ - 1; i >= 0; --i)
00430 if (a->sig[i])
00431 return 1;
00432
00433 return 0;
00434 }
00435
00436
00437
00438 static inline void
00439 set_significand_bit (r, n)
00440 REAL_VALUE_TYPE *r;
00441 unsigned int n;
00442 {
00443 r->sig[n / HOST_BITS_PER_LONG]
00444 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
00445 }
00446
00447
00448
00449 static inline void
00450 clear_significand_bit (r, n)
00451 REAL_VALUE_TYPE *r;
00452 unsigned int n;
00453 {
00454 r->sig[n / HOST_BITS_PER_LONG]
00455 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
00456 }
00457
00458
00459
00460 static inline bool
00461 test_significand_bit (r, n)
00462 REAL_VALUE_TYPE *r;
00463 unsigned int n;
00464 {
00465
00466
00467
00468 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
00469 return t;
00470 }
00471
00472
00473
00474 static void
00475 clear_significand_below (r, n)
00476 REAL_VALUE_TYPE *r;
00477 unsigned int n;
00478 {
00479 int i, w = n / HOST_BITS_PER_LONG;
00480
00481 for (i = 0; i < w; ++i)
00482 r->sig[i] = 0;
00483
00484 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
00485 }
00486
00487
00488
00489
00490 static inline bool
00491 div_significands (r, a, b)
00492 REAL_VALUE_TYPE *r;
00493 const REAL_VALUE_TYPE *a, *b;
00494 {
00495 REAL_VALUE_TYPE u;
00496 int i, bit = SIGNIFICAND_BITS - 1;
00497 unsigned long msb, inexact;
00498
00499 u = *a;
00500 memset (r->sig, 0, sizeof (r->sig));
00501
00502 msb = 0;
00503 goto start;
00504 do
00505 {
00506 msb = u.sig[SIGSZ-1] & SIG_MSB;
00507 lshift_significand_1 (&u, &u);
00508 start:
00509 if (msb || cmp_significands (&u, b) >= 0)
00510 {
00511 sub_significands (&u, &u, b, 0);
00512 set_significand_bit (r, bit);
00513 }
00514 }
00515 while (--bit >= 0);
00516
00517 for (i = 0, inexact = 0; i < SIGSZ; i++)
00518 inexact |= u.sig[i];
00519
00520 return inexact != 0;
00521 }
00522
00523
00524
00525
00526
00527
00528 static void
00529 normalize (r)
00530 REAL_VALUE_TYPE *r;
00531 {
00532 int shift = 0, exp;
00533 int i, j;
00534
00535
00536 for (i = SIGSZ - 1; i >= 0; i--)
00537 if (r->sig[i] == 0)
00538 shift += HOST_BITS_PER_LONG;
00539 else
00540 break;
00541
00542
00543 if (i < 0)
00544 {
00545 r->class = rvc_zero;
00546 r->exp = 0;
00547 return;
00548 }
00549
00550
00551 for (j = 0; ; j++)
00552 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
00553 break;
00554 shift += j;
00555
00556 if (shift > 0)
00557 {
00558 exp = r->exp - shift;
00559 if (exp > MAX_EXP)
00560 get_inf (r, r->sign);
00561 else if (exp < -MAX_EXP)
00562 get_zero (r, r->sign);
00563 else
00564 {
00565 r->exp = exp;
00566 lshift_significand (r, r, shift);
00567 }
00568 }
00569 }
00570
00571
00572
00573 static void
00574 do_add (r, a, b, subtract_p)
00575 REAL_VALUE_TYPE *r;
00576 const REAL_VALUE_TYPE *a, *b;
00577 int subtract_p;
00578 {
00579 int dexp, sign, exp;
00580 REAL_VALUE_TYPE t;
00581 bool inexact = false;
00582
00583
00584 sign = a->sign;
00585 subtract_p = (sign ^ b->sign) ^ subtract_p;
00586
00587 switch (CLASS2 (a->class, b->class))
00588 {
00589 case CLASS2 (rvc_zero, rvc_zero):
00590
00591 get_zero (r, sign & !subtract_p);
00592 return;
00593
00594 case CLASS2 (rvc_zero, rvc_normal):
00595 case CLASS2 (rvc_zero, rvc_inf):
00596 case CLASS2 (rvc_zero, rvc_nan):
00597
00598 case CLASS2 (rvc_normal, rvc_nan):
00599 case CLASS2 (rvc_inf, rvc_nan):
00600 case CLASS2 (rvc_nan, rvc_nan):
00601
00602 case CLASS2 (rvc_normal, rvc_inf):
00603
00604 *r = *b;
00605 r->sign = sign ^ subtract_p;
00606 return;
00607
00608 case CLASS2 (rvc_normal, rvc_zero):
00609 case CLASS2 (rvc_inf, rvc_zero):
00610 case CLASS2 (rvc_nan, rvc_zero):
00611
00612 case CLASS2 (rvc_nan, rvc_normal):
00613 case CLASS2 (rvc_nan, rvc_inf):
00614
00615 case CLASS2 (rvc_inf, rvc_normal):
00616
00617 *r = *a;
00618 return;
00619
00620 case CLASS2 (rvc_inf, rvc_inf):
00621 if (subtract_p)
00622
00623 get_canonical_qnan (r, 0);
00624 else
00625
00626 *r = *a;
00627 return;
00628
00629 case CLASS2 (rvc_normal, rvc_normal):
00630 break;
00631
00632 default:
00633 abort ();
00634 }
00635
00636
00637 dexp = a->exp - b->exp;
00638 if (dexp < 0)
00639 {
00640 const REAL_VALUE_TYPE *t;
00641 t = a, a = b, b = t;
00642 dexp = -dexp;
00643 sign ^= subtract_p;
00644 }
00645 exp = a->exp;
00646
00647
00648
00649 if (dexp > 0)
00650 {
00651
00652
00653 if (dexp >= SIGNIFICAND_BITS)
00654 {
00655 *r = *a;
00656 r->sign = sign;
00657 return;
00658 }
00659
00660 inexact |= sticky_rshift_significand (&t, b, dexp);
00661 b = &t;
00662 }
00663
00664 if (subtract_p)
00665 {
00666 if (sub_significands (r, a, b, inexact))
00667 {
00668
00669
00670
00671
00672 sign ^= 1;
00673 neg_significand (r, r);
00674 }
00675 }
00676 else
00677 {
00678 if (add_significands (r, a, b))
00679 {
00680
00681
00682
00683 inexact |= sticky_rshift_significand (r, r, 1);
00684 r->sig[SIGSZ-1] |= SIG_MSB;
00685 if (++exp > MAX_EXP)
00686 {
00687 get_inf (r, sign);
00688 return;
00689 }
00690 }
00691 }
00692
00693 r->class = rvc_normal;
00694 r->sign = sign;
00695 r->exp = exp;
00696
00697
00698 normalize (r);
00699
00700
00701
00702 if (r->class == rvc_zero)
00703 r->sign = 0;
00704 else
00705 r->sig[0] |= inexact;
00706 }
00707
00708
00709
00710 static void
00711 do_multiply (r, a, b)
00712 REAL_VALUE_TYPE *r;
00713 const REAL_VALUE_TYPE *a, *b;
00714 {
00715 REAL_VALUE_TYPE u, t, *rr;
00716 unsigned int i, j, k;
00717 int sign = a->sign ^ b->sign;
00718
00719 switch (CLASS2 (a->class, b->class))
00720 {
00721 case CLASS2 (rvc_zero, rvc_zero):
00722 case CLASS2 (rvc_zero, rvc_normal):
00723 case CLASS2 (rvc_normal, rvc_zero):
00724
00725 get_zero (r, sign);
00726 return;
00727
00728 case CLASS2 (rvc_zero, rvc_nan):
00729 case CLASS2 (rvc_normal, rvc_nan):
00730 case CLASS2 (rvc_inf, rvc_nan):
00731 case CLASS2 (rvc_nan, rvc_nan):
00732
00733 *r = *b;
00734 r->sign = sign;
00735 return;
00736
00737 case CLASS2 (rvc_nan, rvc_zero):
00738 case CLASS2 (rvc_nan, rvc_normal):
00739 case CLASS2 (rvc_nan, rvc_inf):
00740
00741 *r = *a;
00742 r->sign = sign;
00743 return;
00744
00745 case CLASS2 (rvc_zero, rvc_inf):
00746 case CLASS2 (rvc_inf, rvc_zero):
00747
00748 get_canonical_qnan (r, sign);
00749 return;
00750
00751 case CLASS2 (rvc_inf, rvc_inf):
00752 case CLASS2 (rvc_normal, rvc_inf):
00753 case CLASS2 (rvc_inf, rvc_normal):
00754
00755 overflow:
00756 get_inf (r, sign);
00757 return;
00758
00759 case CLASS2 (rvc_normal, rvc_normal):
00760 break;
00761
00762 default:
00763 abort ();
00764 }
00765
00766 if (r == a || r == b)
00767 rr = &t;
00768 else
00769 rr = r;
00770 get_zero (rr, 0);
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790 for (i = 0; i < SIGSZ * 2; ++i)
00791 {
00792 unsigned long ai = a->sig[i / 2];
00793 if (i & 1)
00794 ai >>= HOST_BITS_PER_LONG / 2;
00795 else
00796 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
00797
00798 if (ai == 0)
00799 continue;
00800
00801 for (j = 0; j < 2; ++j)
00802 {
00803 int exp = (a->exp - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
00804 + (b->exp - (1-j)*(HOST_BITS_PER_LONG/2)));
00805
00806 if (exp > MAX_EXP)
00807 goto overflow;
00808 if (exp < -MAX_EXP)
00809
00810 continue;
00811
00812 u.class = rvc_normal;
00813 u.sign = 0;
00814 u.exp = exp;
00815
00816 for (k = j; k < SIGSZ * 2; k += 2)
00817 {
00818 unsigned long bi = b->sig[k / 2];
00819 if (k & 1)
00820 bi >>= HOST_BITS_PER_LONG / 2;
00821 else
00822 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
00823
00824 u.sig[k / 2] = ai * bi;
00825 }
00826
00827 normalize (&u);
00828 do_add (rr, rr, &u, 0);
00829 }
00830 }
00831
00832 rr->sign = sign;
00833 if (rr != r)
00834 *r = t;
00835 }
00836
00837
00838
00839 static void
00840 do_divide (r, a, b)
00841 REAL_VALUE_TYPE *r;
00842 const REAL_VALUE_TYPE *a, *b;
00843 {
00844 int exp, sign = a->sign ^ b->sign;
00845 REAL_VALUE_TYPE t, *rr;
00846 bool inexact;
00847
00848 switch (CLASS2 (a->class, b->class))
00849 {
00850 case CLASS2 (rvc_zero, rvc_zero):
00851
00852 case CLASS2 (rvc_inf, rvc_inf):
00853
00854 get_canonical_qnan (r, sign);
00855 return;
00856
00857 case CLASS2 (rvc_zero, rvc_normal):
00858 case CLASS2 (rvc_zero, rvc_inf):
00859
00860 case CLASS2 (rvc_normal, rvc_inf):
00861
00862 underflow:
00863 get_zero (r, sign);
00864 return;
00865
00866 case CLASS2 (rvc_normal, rvc_zero):
00867
00868 case CLASS2 (rvc_inf, rvc_zero):
00869
00870 get_inf (r, sign);
00871 return;
00872
00873 case CLASS2 (rvc_zero, rvc_nan):
00874 case CLASS2 (rvc_normal, rvc_nan):
00875 case CLASS2 (rvc_inf, rvc_nan):
00876 case CLASS2 (rvc_nan, rvc_nan):
00877
00878 *r = *b;
00879 r->sign = sign;
00880 return;
00881
00882 case CLASS2 (rvc_nan, rvc_zero):
00883 case CLASS2 (rvc_nan, rvc_normal):
00884 case CLASS2 (rvc_nan, rvc_inf):
00885
00886 *r = *a;
00887 r->sign = sign;
00888 return;
00889
00890 case CLASS2 (rvc_inf, rvc_normal):
00891
00892 overflow:
00893 get_inf (r, sign);
00894 return;
00895
00896 case CLASS2 (rvc_normal, rvc_normal):
00897 break;
00898
00899 default:
00900 abort ();
00901 }
00902
00903 if (r == a || r == b)
00904 rr = &t;
00905 else
00906 rr = r;
00907
00908 rr->class = rvc_normal;
00909 rr->sign = sign;
00910
00911 exp = a->exp - b->exp + 1;
00912 if (exp > MAX_EXP)
00913 goto overflow;
00914 if (exp < -MAX_EXP)
00915 goto underflow;
00916 rr->exp = exp;
00917
00918 inexact = div_significands (rr, a, b);
00919
00920
00921 normalize (rr);
00922 rr->sig[0] |= inexact;
00923
00924 if (rr != r)
00925 *r = t;
00926 }
00927
00928
00929
00930
00931 static int
00932 do_compare (a, b, nan_result)
00933 const REAL_VALUE_TYPE *a, *b;
00934 int nan_result;
00935 {
00936 int ret;
00937
00938 switch (CLASS2 (a->class, b->class))
00939 {
00940 case CLASS2 (rvc_zero, rvc_zero):
00941
00942 return 0;
00943
00944 case CLASS2 (rvc_inf, rvc_zero):
00945 case CLASS2 (rvc_inf, rvc_normal):
00946 case CLASS2 (rvc_normal, rvc_zero):
00947 return (a->sign ? -1 : 1);
00948
00949 case CLASS2 (rvc_inf, rvc_inf):
00950 return -a->sign - -b->sign;
00951
00952 case CLASS2 (rvc_zero, rvc_normal):
00953 case CLASS2 (rvc_zero, rvc_inf):
00954 case CLASS2 (rvc_normal, rvc_inf):
00955 return (b->sign ? 1 : -1);
00956
00957 case CLASS2 (rvc_zero, rvc_nan):
00958 case CLASS2 (rvc_normal, rvc_nan):
00959 case CLASS2 (rvc_inf, rvc_nan):
00960 case CLASS2 (rvc_nan, rvc_nan):
00961 case CLASS2 (rvc_nan, rvc_zero):
00962 case CLASS2 (rvc_nan, rvc_normal):
00963 case CLASS2 (rvc_nan, rvc_inf):
00964 return nan_result;
00965
00966 case CLASS2 (rvc_normal, rvc_normal):
00967 break;
00968
00969 default:
00970 abort ();
00971 }
00972
00973 if (a->sign != b->sign)
00974 return -a->sign - -b->sign;
00975
00976 if (a->exp > b->exp)
00977 ret = 1;
00978 else if (a->exp < b->exp)
00979 ret = -1;
00980 else
00981 ret = cmp_significands (a, b);
00982
00983 return (a->sign ? -ret : ret);
00984 }
00985
00986
00987
00988 static void
00989 do_fix_trunc (r, a)
00990 REAL_VALUE_TYPE *r;
00991 const REAL_VALUE_TYPE *a;
00992 {
00993 *r = *a;
00994
00995 switch (r->class)
00996 {
00997 case rvc_zero:
00998 case rvc_inf:
00999 case rvc_nan:
01000 break;
01001
01002 case rvc_normal:
01003 if (r->exp <= 0)
01004 get_zero (r, r->sign);
01005 else if (r->exp < SIGNIFICAND_BITS)
01006 clear_significand_below (r, SIGNIFICAND_BITS - r->exp);
01007 break;
01008
01009 default:
01010 abort ();
01011 }
01012 }
01013
01014
01015
01016
01017 void
01018 real_arithmetic (r, icode, op0, op1)
01019 REAL_VALUE_TYPE *r;
01020 int icode;
01021 const REAL_VALUE_TYPE *op0, *op1;
01022 {
01023 enum tree_code code = icode;
01024
01025 switch (code)
01026 {
01027 case PLUS_EXPR:
01028 do_add (r, op0, op1, 0);
01029 break;
01030
01031 case MINUS_EXPR:
01032 do_add (r, op0, op1, 1);
01033 break;
01034
01035 case MULT_EXPR:
01036 do_multiply (r, op0, op1);
01037 break;
01038
01039 case RDIV_EXPR:
01040 do_divide (r, op0, op1);
01041 break;
01042
01043 case MIN_EXPR:
01044 if (op1->class == rvc_nan)
01045 *r = *op1;
01046 else if (do_compare (op0, op1, -1) < 0)
01047 *r = *op0;
01048 else
01049 *r = *op1;
01050 break;
01051
01052 case MAX_EXPR:
01053 if (op1->class == rvc_nan)
01054 *r = *op1;
01055 else if (do_compare (op0, op1, 1) < 0)
01056 *r = *op1;
01057 else
01058 *r = *op0;
01059 break;
01060
01061 case NEGATE_EXPR:
01062 *r = *op0;
01063 r->sign ^= 1;
01064 break;
01065
01066 case ABS_EXPR:
01067 *r = *op0;
01068 r->sign = 0;
01069 break;
01070
01071 case FIX_TRUNC_EXPR:
01072 do_fix_trunc (r, op0);
01073 break;
01074
01075 default:
01076 abort ();
01077 }
01078 }
01079
01080
01081
01082 REAL_VALUE_TYPE
01083 real_arithmetic2 (icode, op0, op1)
01084 int icode;
01085 const REAL_VALUE_TYPE *op0, *op1;
01086 {
01087 REAL_VALUE_TYPE r;
01088 real_arithmetic (&r, icode, op0, op1);
01089 return r;
01090 }
01091
01092 bool
01093 real_compare (icode, op0, op1)
01094 int icode;
01095 const REAL_VALUE_TYPE *op0, *op1;
01096 {
01097 enum tree_code code = icode;
01098
01099 switch (code)
01100 {
01101 case LT_EXPR:
01102 return do_compare (op0, op1, 1) < 0;
01103 case LE_EXPR:
01104 return do_compare (op0, op1, 1) <= 0;
01105 case GT_EXPR:
01106 return do_compare (op0, op1, -1) > 0;
01107 case GE_EXPR:
01108 return do_compare (op0, op1, -1) >= 0;
01109 case EQ_EXPR:
01110 return do_compare (op0, op1, -1) == 0;
01111 case NE_EXPR:
01112 return do_compare (op0, op1, -1) != 0;
01113 case UNORDERED_EXPR:
01114 return op0->class == rvc_nan || op1->class == rvc_nan;
01115 case ORDERED_EXPR:
01116 return op0->class != rvc_nan && op1->class != rvc_nan;
01117 case UNLT_EXPR:
01118 return do_compare (op0, op1, -1) < 0;
01119 case UNLE_EXPR:
01120 return do_compare (op0, op1, -1) <= 0;
01121 case UNGT_EXPR:
01122 return do_compare (op0, op1, 1) > 0;
01123 case UNGE_EXPR:
01124 return do_compare (op0, op1, 1) >= 0;
01125 case UNEQ_EXPR:
01126 return do_compare (op0, op1, 0) == 0;
01127
01128 default:
01129 abort ();
01130 }
01131 }
01132
01133
01134
01135 int
01136 real_exponent (r)
01137 const REAL_VALUE_TYPE *r;
01138 {
01139 switch (r->class)
01140 {
01141 case rvc_zero:
01142 return 0;
01143 case rvc_inf:
01144 case rvc_nan:
01145 return (unsigned int)-1 >> 1;
01146 case rvc_normal:
01147 return r->exp;
01148 default:
01149 abort ();
01150 }
01151 }
01152
01153
01154
01155 void
01156 real_ldexp (r, op0, exp)
01157 REAL_VALUE_TYPE *r;
01158 const REAL_VALUE_TYPE *op0;
01159 int exp;
01160 {
01161 *r = *op0;
01162 switch (r->class)
01163 {
01164 case rvc_zero:
01165 case rvc_inf:
01166 case rvc_nan:
01167 break;
01168
01169 case rvc_normal:
01170 exp += op0->exp;
01171 if (exp > MAX_EXP)
01172 get_inf (r, r->sign);
01173 else if (exp < -MAX_EXP)
01174 get_zero (r, r->sign);
01175 else
01176 r->exp = exp;
01177 break;
01178
01179 default:
01180 abort ();
01181 }
01182 }
01183
01184
01185
01186 bool
01187 real_isinf (r)
01188 const REAL_VALUE_TYPE *r;
01189 {
01190 return (r->class == rvc_inf);
01191 }
01192
01193
01194
01195 bool
01196 real_isnan (r)
01197 const REAL_VALUE_TYPE *r;
01198 {
01199 return (r->class == rvc_nan);
01200 }
01201
01202
01203
01204 bool
01205 real_isneg (r)
01206 const REAL_VALUE_TYPE *r;
01207 {
01208 return r->sign;
01209 }
01210
01211
01212
01213 bool
01214 real_isnegzero (r)
01215 const REAL_VALUE_TYPE *r;
01216 {
01217 return r->sign && r->class == rvc_zero;
01218 }
01219
01220
01221
01222 extern bool
01223 real_identical (a, b)
01224 const REAL_VALUE_TYPE *a, *b;
01225 {
01226 int i;
01227
01228 if (a->class != b->class)
01229 return false;
01230 if (a->sign != b->sign)
01231 return false;
01232
01233 switch (a->class)
01234 {
01235 case rvc_zero:
01236 case rvc_inf:
01237 break;
01238
01239 case rvc_normal:
01240 if (a->exp != b->exp)
01241 return false;
01242
01243 case rvc_nan:
01244 for (i = 0; i < SIGSZ; ++i)
01245 if (a->sig[i] != b->sig[i])
01246 return false;
01247 break;
01248
01249 default:
01250 abort ();
01251 }
01252
01253 return true;
01254 }
01255
01256
01257
01258
01259 bool
01260 exact_real_inverse (mode, r)
01261 enum machine_mode mode;
01262 REAL_VALUE_TYPE *r;
01263 {
01264 const REAL_VALUE_TYPE *one = real_digit (1);
01265 REAL_VALUE_TYPE u;
01266 int i;
01267
01268 if (r->class != rvc_normal)
01269 return false;
01270
01271
01272 for (i = 0; i < SIGSZ-1; ++i)
01273 if (r->sig[i] != 0)
01274 return false;
01275 if (r->sig[SIGSZ-1] != SIG_MSB)
01276 return false;
01277
01278
01279 do_divide (&u, one, r);
01280 real_convert (&u, mode, &u);
01281
01282
01283 if (u.class != rvc_normal)
01284 return false;
01285 for (i = 0; i < SIGSZ-1; ++i)
01286 if (u.sig[i] != 0)
01287 return false;
01288 if (u.sig[SIGSZ-1] != SIG_MSB)
01289 return false;
01290
01291 *r = u;
01292 return true;
01293 }
01294
01295
01296
01297 HOST_WIDE_INT
01298 real_to_integer (r)
01299 const REAL_VALUE_TYPE *r;
01300 {
01301 unsigned HOST_WIDE_INT i;
01302
01303 switch (r->class)
01304 {
01305 case rvc_zero:
01306 underflow:
01307 return 0;
01308
01309 case rvc_inf:
01310 case rvc_nan:
01311 overflow:
01312 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
01313 if (!r->sign)
01314 i--;
01315 return i;
01316
01317 case rvc_normal:
01318 if (r->exp <= 0)
01319 goto underflow;
01320 if (r->exp > HOST_BITS_PER_WIDE_INT)
01321 goto overflow;
01322
01323 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
01324 i = r->sig[SIGSZ-1];
01325 else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
01326 {
01327 i = r->sig[SIGSZ-1];
01328 i = i << (HOST_BITS_PER_LONG - 1) << 1;
01329 i |= r->sig[SIGSZ-2];
01330 }
01331 else
01332 abort ();
01333
01334 i >>= HOST_BITS_PER_WIDE_INT - r->exp;
01335
01336 if (r->sign)
01337 i = -i;
01338 return i;
01339
01340 default:
01341 abort ();
01342 }
01343 }
01344
01345
01346
01347 void
01348 real_to_integer2 (plow, phigh, r)
01349 HOST_WIDE_INT *plow, *phigh;
01350 const REAL_VALUE_TYPE *r;
01351 {
01352 REAL_VALUE_TYPE t;
01353 HOST_WIDE_INT low, high;
01354 int exp;
01355
01356 switch (r->class)
01357 {
01358 case rvc_zero:
01359 underflow:
01360 low = high = 0;
01361 break;
01362
01363 case rvc_inf:
01364 case rvc_nan:
01365 overflow:
01366 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
01367 if (r->sign)
01368 low = 0;
01369 else
01370 {
01371 high--;
01372 low = -1;
01373 }
01374 break;
01375
01376 case rvc_normal:
01377 exp = r->exp;
01378 if (exp <= 0)
01379 goto underflow;
01380 if (exp > 2*HOST_BITS_PER_WIDE_INT)
01381 goto overflow;
01382
01383 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
01384 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
01385 {
01386 high = t.sig[SIGSZ-1];
01387 low = t.sig[SIGSZ-2];
01388 }
01389 else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
01390 {
01391 high = t.sig[SIGSZ-1];
01392 high = high << (HOST_BITS_PER_LONG - 1) << 1;
01393 high |= t.sig[SIGSZ-2];
01394
01395 low = t.sig[SIGSZ-3];
01396 low = low << (HOST_BITS_PER_LONG - 1) << 1;
01397 low |= t.sig[SIGSZ-4];
01398 }
01399 else
01400 abort ();
01401
01402 if (r->sign)
01403 {
01404 if (low == 0)
01405 high = -high;
01406 else
01407 low = -low, high = ~high;
01408 }
01409 break;
01410
01411 default:
01412 abort ();
01413 }
01414
01415 *plow = low;
01416 *phigh = high;
01417 }
01418
01419
01420
01421
01422
01423
01424 static unsigned long
01425 rtd_divmod (num, den)
01426 REAL_VALUE_TYPE *num, *den;
01427 {
01428 unsigned long q, msb;
01429 int expn = num->exp, expd = den->exp;
01430
01431 if (expn < expd)
01432 return 0;
01433
01434 q = msb = 0;
01435 goto start;
01436 do
01437 {
01438 msb = num->sig[SIGSZ-1] & SIG_MSB;
01439 q <<= 1;
01440 lshift_significand_1 (num, num);
01441 start:
01442 if (msb || cmp_significands (num, den) >= 0)
01443 {
01444 sub_significands (num, num, den, 0);
01445 q |= 1;
01446 }
01447 }
01448 while (--expn >= expd);
01449
01450 num->exp = expd;
01451 normalize (num);
01452
01453 return q;
01454 }
01455
01456
01457
01458
01459
01460
01461 #define M_LOG10_2 0.30102999566398119521
01462
01463 void
01464 real_to_decimal (str, r_orig, buf_size, digits, crop_trailing_zeros)
01465 char *str;
01466 const REAL_VALUE_TYPE *r_orig;
01467 size_t buf_size, digits;
01468 int crop_trailing_zeros;
01469 {
01470 const REAL_VALUE_TYPE *one, *ten;
01471 REAL_VALUE_TYPE r, pten, u, v;
01472 int dec_exp, cmp_one, digit;
01473 size_t max_digits;
01474 char *p, *first, *last;
01475 bool sign;
01476
01477 r = *r_orig;
01478 switch (r.class)
01479 {
01480 case rvc_zero:
01481 strcpy (str, (r.sign ? "-0.0" : "0.0"));
01482 return;
01483 case rvc_normal:
01484 break;
01485 case rvc_inf:
01486 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
01487 return;
01488 case rvc_nan:
01489
01490 strcpy (str, (r.sign ? "-NaN" : "+NaN"));
01491 return;
01492 default:
01493 abort ();
01494 }
01495
01496
01497 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
01498 if (digits == 0 || digits > max_digits)
01499 digits = max_digits;
01500
01501
01502
01503
01504 dec_exp = r.exp * M_LOG10_2;
01505 for (max_digits = 1; dec_exp ; max_digits++)
01506 dec_exp /= 10;
01507
01508
01509 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
01510 if (max_digits > buf_size)
01511 abort ();
01512 if (digits > max_digits)
01513 digits = max_digits;
01514
01515 one = real_digit (1);
01516 ten = ten_to_ptwo (0);
01517
01518 sign = r.sign;
01519 r.sign = 0;
01520
01521 dec_exp = 0;
01522 pten = *one;
01523
01524 cmp_one = do_compare (&r, one, 0);
01525 if (cmp_one > 0)
01526 {
01527 int m;
01528
01529
01530
01531
01532 u = r;
01533 u.exp = SIGNIFICAND_BITS - 1;
01534
01535
01536 m = floor_log2 (max_digits);
01537
01538
01539
01540
01541
01542 do
01543 {
01544 REAL_VALUE_TYPE t;
01545
01546 do_divide (&t, &u, ten_to_ptwo (m));
01547 do_fix_trunc (&v, &t);
01548 if (cmp_significands (&v, &t) == 0)
01549 {
01550 u = t;
01551 dec_exp += 1 << m;
01552 }
01553 }
01554 while (--m >= 0);
01555
01556
01557 u.exp += r.exp - (SIGNIFICAND_BITS - 1);
01558 r = u;
01559
01560
01561
01562
01563 if (r.exp > 0)
01564 {
01565 m = floor_log2 ((int)(r.exp * M_LOG10_2)) + 1;
01566 do
01567 {
01568 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
01569 if (do_compare (&u, ptentwo, 0) >= 0)
01570 {
01571 do_divide (&u, &u, ptentwo);
01572 do_multiply (&pten, &pten, ptentwo);
01573 dec_exp += 1 << m;
01574 }
01575 }
01576 while (--m >= 0);
01577 }
01578 else
01579
01580
01581
01582 cmp_one = -1;
01583 }
01584 if (cmp_one < 0)
01585 {
01586 int m;
01587
01588
01589
01590
01591 v = r;
01592 while (1)
01593 {
01594
01595 if (v.sig[0] & 7)
01596 break;
01597
01598 do_multiply (&u, &v, ten);
01599
01600
01601 if (u.exp > 0)
01602 break;
01603
01604 v = u;
01605 dec_exp -= 1;
01606 }
01607 r = v;
01608
01609
01610
01611
01612 m = floor_log2 ((int)(-r.exp * M_LOG10_2)) + 1;
01613 do
01614 {
01615 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
01616 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
01617
01618 if (do_compare (&v, ptenmtwo, 0) <= 0)
01619 {
01620 do_multiply (&v, &v, ptentwo);
01621 do_multiply (&pten, &pten, ptentwo);
01622 dec_exp -= 1 << m;
01623 }
01624 }
01625 while (--m >= 0);
01626
01627
01628 do_divide (&pten, one, &pten);
01629 }
01630
01631 p = str;
01632 if (sign)
01633 *p++ = '-';
01634 first = p++;
01635
01636
01637
01638
01639
01640
01641
01642
01643
01644 digit = rtd_divmod (&r, &pten);
01645
01646
01647 if (digit == 0 && cmp_significand_0 (&r))
01648 {
01649
01650 do_multiply (&r, &r, ten);
01651 digit = rtd_divmod (&r, &pten);
01652 dec_exp -= 1;
01653 if (digit == 0)
01654 abort ();
01655 }
01656
01657
01658 if (digit == 10)
01659 {
01660 *p++ = '1';
01661 if (--digits > 0)
01662 *p++ = '0';
01663 dec_exp += 1;
01664 }
01665 else if (digit > 10)
01666 abort ();
01667 else
01668 *p++ = digit + '0';
01669
01670
01671 while (--digits > 0)
01672 {
01673 do_multiply (&r, &r, ten);
01674 digit = rtd_divmod (&r, &pten);
01675 *p++ = digit + '0';
01676 }
01677 last = p;
01678
01679
01680 do_multiply (&r, &r, ten);
01681 digit = rtd_divmod (&r, &pten);
01682
01683
01684 if (digit == 5)
01685 {
01686
01687
01688 if (cmp_significand_0 (&r))
01689 digit++;
01690
01691 else if ((p[-1] - '0') & 1)
01692 digit++;
01693 }
01694 if (digit > 5)
01695 {
01696 while (p > first)
01697 {
01698 digit = *--p;
01699 if (digit == '9')
01700 *p = '0';
01701 else
01702 {
01703 *p = digit + 1;
01704 break;
01705 }
01706 }
01707
01708
01709
01710 if (p == first)
01711 {
01712 first[1] = '1';
01713 dec_exp++;
01714 }
01715 }
01716
01717
01718 first[0] = first[1];
01719 first[1] = '.';
01720
01721
01722 if (crop_trailing_zeros)
01723 while (last > first + 3 && last[-1] == '0')
01724 last--;
01725
01726
01727 sprintf (last, "e%+d", dec_exp);
01728 }
01729
01730
01731
01732
01733
01734
01735 void
01736 real_to_hexadecimal (str, r, buf_size, digits, crop_trailing_zeros)
01737 char *str;
01738 const REAL_VALUE_TYPE *r;
01739 size_t buf_size, digits;
01740 int crop_trailing_zeros;
01741 {
01742 int i, j, exp = r->exp;
01743 char *p, *first;
01744 char exp_buf[16];
01745 size_t max_digits;
01746
01747 switch (r->class)
01748 {
01749 case rvc_zero:
01750 exp = 0;
01751 break;
01752 case rvc_normal:
01753 break;
01754 case rvc_inf:
01755 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
01756 return;
01757 case rvc_nan:
01758
01759 strcpy (str, (r->sign ? "-NaN" : "+NaN"));
01760 return;
01761 default:
01762 abort ();
01763 }
01764
01765 if (digits == 0)
01766 digits = SIGNIFICAND_BITS / 4;
01767
01768
01769
01770 sprintf (exp_buf, "p%+d", exp);
01771 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
01772 if (max_digits > buf_size)
01773 abort ();
01774 if (digits > max_digits)
01775 digits = max_digits;
01776
01777 p = str;
01778 if (r->sign)
01779 *p++ = '-';
01780 *p++ = '0';
01781 *p++ = 'x';
01782 *p++ = '0';
01783 *p++ = '.';
01784 first = p;
01785
01786 for (i = SIGSZ - 1; i >= 0; --i)
01787 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
01788 {
01789 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
01790 if (--digits == 0)
01791 goto out;
01792 }
01793
01794 out:
01795 if (crop_trailing_zeros)
01796 while (p > first + 1 && p[-1] == '0')
01797 p--;
01798
01799 sprintf (p, "p%+d", exp);
01800 }
01801
01802
01803
01804
01805 void
01806 real_from_string (r, str)
01807 REAL_VALUE_TYPE *r;
01808 const char *str;
01809 {
01810 int exp = 0;
01811 bool sign = false;
01812
01813 get_zero (r, 0);
01814
01815 if (*str == '-')
01816 {
01817 sign = true;
01818 str++;
01819 }
01820 else if (*str == '+')
01821 str++;
01822
01823 if (str[0] == '0' && str[1] == 'x')
01824 {
01825
01826 int pos = SIGNIFICAND_BITS - 4, d;
01827
01828 str += 2;
01829
01830 while (*str == '0')
01831 str++;
01832 while (1)
01833 {
01834 d = hex_value (*str);
01835 if (d == _hex_bad)
01836 break;
01837 if (pos >= 0)
01838 {
01839 r->sig[pos / HOST_BITS_PER_LONG]
01840 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
01841 pos -= 4;
01842 }
01843 exp += 4;
01844 str++;
01845 }
01846 if (*str == '.')
01847 {
01848 str++;
01849 if (pos == SIGNIFICAND_BITS - 4)
01850 {
01851 while (*str == '0')
01852 str++, exp -= 4;
01853 }
01854 while (1)
01855 {
01856 d = hex_value (*str);
01857 if (d == _hex_bad)
01858 break;
01859 if (pos >= 0)
01860 {
01861 r->sig[pos / HOST_BITS_PER_LONG]
01862 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
01863 pos -= 4;
01864 }
01865 str++;
01866 }
01867 }
01868 if (*str == 'p' || *str == 'P')
01869 {
01870 bool exp_neg = false;
01871
01872 str++;
01873 if (*str == '-')
01874 {
01875 exp_neg = true;
01876 str++;
01877 }
01878 else if (*str == '+')
01879 str++;
01880
01881 d = 0;
01882 while (ISDIGIT (*str))
01883 {
01884 d *= 10;
01885 d += *str - '0';
01886 if (d > MAX_EXP)
01887 {
01888
01889 if (exp_neg)
01890 goto underflow;
01891 else
01892 goto overflow;
01893 }
01894 str++;
01895 }
01896 if (exp_neg)
01897 d = -d;
01898
01899 exp += d;
01900 }
01901
01902 r->class = rvc_normal;
01903 r->exp = exp;
01904
01905 normalize (r);
01906 }
01907 else
01908 {
01909
01910 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
01911 int d;
01912
01913 while (*str == '0')
01914 str++;
01915 while (ISDIGIT (*str))
01916 {
01917 d = *str++ - '0';
01918 do_multiply (r, r, ten);
01919 if (d)
01920 do_add (r, r, real_digit (d), 0);
01921 }
01922 if (*str == '.')
01923 {
01924 str++;
01925 if (r->class == rvc_zero)
01926 {
01927 while (*str == '0')
01928 str++, exp--;
01929 }
01930 while (ISDIGIT (*str))
01931 {
01932 d = *str++ - '0';
01933 do_multiply (r, r, ten);
01934 if (d)
01935 do_add (r, r, real_digit (d), 0);
01936 exp--;
01937 }
01938 }
01939
01940 if (*str == 'e' || *str == 'E')
01941 {
01942 bool exp_neg = false;
01943
01944 str++;
01945 if (*str == '-')
01946 {
01947 exp_neg = true;
01948 str++;
01949 }
01950 else if (*str == '+')
01951 str++;
01952
01953 d = 0;
01954 while (ISDIGIT (*str))
01955 {
01956 d *= 10;
01957 d += *str - '0';
01958 if (d > MAX_EXP)
01959 {
01960
01961 if (exp_neg)
01962 goto underflow;
01963 else
01964 goto overflow;
01965 }
01966 str++;
01967 }
01968 if (exp_neg)
01969 d = -d;
01970 exp += d;
01971 }
01972
01973 if (exp)
01974 times_pten (r, exp);
01975 }
01976
01977 r->sign = sign;
01978 return;
01979
01980 underflow:
01981 get_zero (r, sign);
01982 return;
01983
01984 overflow:
01985 get_inf (r, sign);
01986 return;
01987 }
01988
01989
01990
01991 REAL_VALUE_TYPE
01992 real_from_string2 (s, mode)
01993 const char *s;
01994 enum machine_mode mode;
01995 {
01996 REAL_VALUE_TYPE r;
01997
01998 real_from_string (&r, s);
01999 if (mode != VOIDmode)
02000 real_convert (&r, mode, &r);
02001
02002 return r;
02003 }
02004
02005
02006
02007 void
02008 real_from_integer (r, mode, low, high, unsigned_p)
02009 REAL_VALUE_TYPE *r;
02010 enum machine_mode mode;
02011 unsigned HOST_WIDE_INT low;
02012 HOST_WIDE_INT high;
02013 int unsigned_p;
02014 {
02015 if (low == 0 && high == 0)
02016 get_zero (r, 0);
02017 else
02018 {
02019 r->class = rvc_normal;
02020 r->sign = high < 0 && !unsigned_p;
02021 r->exp = 2 * HOST_BITS_PER_WIDE_INT;
02022
02023 if (r->sign)
02024 {
02025 high = ~high;
02026 if (low == 0)
02027 high += 1;
02028 else
02029 low = -low;
02030 }
02031
02032 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
02033 {
02034 r->sig[SIGSZ-1] = high;
02035 r->sig[SIGSZ-2] = low;
02036 memset (r->sig, 0, sizeof(long)*(SIGSZ-2));
02037 }
02038 else if (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT)
02039 {
02040 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
02041 r->sig[SIGSZ-2] = high;
02042 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
02043 r->sig[SIGSZ-4] = low;
02044 if (SIGSZ > 4)
02045 memset (r->sig, 0, sizeof(long)*(SIGSZ-4));
02046 }
02047 else
02048 abort ();
02049
02050 normalize (r);
02051 }
02052
02053 if (mode != VOIDmode)
02054 real_convert (r, mode, r);
02055 }
02056
02057
02058
02059 static const REAL_VALUE_TYPE *
02060 ten_to_ptwo (n)
02061 int n;
02062 {
02063 static REAL_VALUE_TYPE tens[EXP_BITS];
02064
02065 if (n < 0 || n >= EXP_BITS)
02066 abort ();
02067
02068 if (tens[n].class == rvc_zero)
02069 {
02070 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
02071 {
02072 HOST_WIDE_INT t = 10;
02073 int i;
02074
02075 for (i = 0; i < n; ++i)
02076 t *= t;
02077
02078 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
02079 }
02080 else
02081 {
02082 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
02083 do_multiply (&tens[n], t, t);
02084 }
02085 }
02086
02087 return &tens[n];
02088 }
02089
02090
02091
02092 static const REAL_VALUE_TYPE *
02093 ten_to_mptwo (n)
02094 int n;
02095 {
02096 static REAL_VALUE_TYPE tens[EXP_BITS];
02097
02098 if (n < 0 || n >= EXP_BITS)
02099 abort ();
02100
02101 if (tens[n].class == rvc_zero)
02102 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
02103
02104 return &tens[n];
02105 }
02106
02107
02108
02109 static const REAL_VALUE_TYPE *
02110 real_digit (n)
02111 int n;
02112 {
02113 static REAL_VALUE_TYPE num[10];
02114
02115 if (n < 0 || n > 9)
02116 abort ();
02117
02118 if (n > 0 && num[n].class == rvc_zero)
02119 real_from_integer (&num[n], VOIDmode, n, 0, 1);
02120
02121 return &num[n];
02122 }
02123
02124
02125
02126 static void
02127 times_pten (r, exp)
02128 REAL_VALUE_TYPE *r;
02129 int exp;
02130 {
02131 REAL_VALUE_TYPE pten, *rr;
02132 bool negative = (exp < 0);
02133 int i;
02134
02135 if (negative)
02136 {
02137 exp = -exp;
02138 pten = *real_digit (1);
02139 rr = &pten;
02140 }
02141 else
02142 rr = r;
02143
02144 for (i = 0; exp > 0; ++i, exp >>= 1)
02145 if (exp & 1)
02146 do_multiply (rr, rr, ten_to_ptwo (i));
02147
02148 if (negative)
02149 do_divide (r, r, &pten);
02150 }
02151
02152
02153
02154 void
02155 real_inf (r)
02156 REAL_VALUE_TYPE *r;
02157 {
02158 get_inf (r, 0);
02159 }
02160
02161
02162
02163
02164
02165
02166 bool
02167 real_nan (r, str, quiet, mode)
02168 REAL_VALUE_TYPE *r;
02169 const char *str;
02170 int quiet;
02171 enum machine_mode mode;
02172 {
02173 const struct real_format *fmt;
02174
02175 fmt = real_format_for_mode[mode - QFmode];
02176 if (fmt == NULL)
02177 abort ();
02178
02179 if (*str == 0)
02180 {
02181 if (quiet)
02182 get_canonical_qnan (r, 0);
02183 else
02184 get_canonical_snan (r, 0);
02185 }
02186 else
02187 {
02188 int base = 10, d;
02189 bool neg = false;
02190
02191 memset (r, 0, sizeof (*r));
02192 r->class = rvc_nan;
02193
02194
02195
02196 while (ISSPACE (*str))
02197 str++;
02198 if (*str == '-')
02199 str++, neg = true;
02200 else if (*str == '+')
02201 str++;
02202 if (*str == '0')
02203 {
02204 if (*++str == 'x')
02205 str++, base = 16;
02206 else
02207 base = 8;
02208 }
02209
02210 while ((d = hex_value (*str)) < base)
02211 {
02212 REAL_VALUE_TYPE u;
02213
02214 switch (base)
02215 {
02216 case 8:
02217 lshift_significand (r, r, 3);
02218 break;
02219 case 16:
02220 lshift_significand (r, r, 4);
02221 break;
02222 case 10:
02223 lshift_significand_1 (&u, r);
02224 lshift_significand (r, r, 3);
02225 add_significands (r, r, &u);
02226 break;
02227 default:
02228 abort ();
02229 }
02230
02231 get_zero (&u, 0);
02232 u.sig[0] = d;
02233 add_significands (r, r, &u);
02234
02235 str++;
02236 }
02237
02238
02239 if (*str != 0)
02240 return false;
02241
02242
02243
02244 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->p);
02245
02246
02247 r->sig[SIGSZ-1] &= ~SIG_MSB;
02248
02249
02250 if (quiet)
02251 r->sig[SIGSZ-1] |= SIG_MSB >> 1;
02252 else
02253 r->sig[SIGSZ-1] &= ~(SIG_MSB >> 1);
02254
02255
02256 for (d = 0; d < SIGSZ; ++d)
02257 if (r->sig[d])
02258 break;
02259 if (d == SIGSZ)
02260 r->sig[SIGSZ-1] |= SIG_MSB >> 2;
02261
02262
02263
02264
02265 if (!fmt->qnan_msb_set)
02266 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1) | (SIG_MSB >> 2);
02267 }
02268
02269 return true;
02270 }
02271
02272
02273
02274 void
02275 real_2expN (r, n)
02276 REAL_VALUE_TYPE *r;
02277 int n;
02278 {
02279 memset (r, 0, sizeof (*r));
02280
02281 n++;
02282 if (n > MAX_EXP)
02283 r->class = rvc_inf;
02284 else if (n < -MAX_EXP)
02285 ;
02286 else
02287 {
02288 r->class = rvc_normal;
02289 r->exp = n;
02290 r->sig[SIGSZ-1] = SIG_MSB;
02291 }
02292 }
02293
02294
02295 static void
02296 round_for_format (fmt, r)
02297 const struct real_format *fmt;
02298 REAL_VALUE_TYPE *r;
02299 {
02300 int p2, np2, i, w;
02301 unsigned long sticky;
02302 bool guard, lsb;
02303 int emin2m1, emax2;
02304
02305 p2 = fmt->p * fmt->log2_b;
02306 emin2m1 = (fmt->emin - 1) * fmt->log2_b;
02307 emax2 = fmt->emax * fmt->log2_b;
02308
02309 np2 = SIGNIFICAND_BITS - p2;
02310 switch (r->class)
02311 {
02312 underflow:
02313 get_zero (r, r->sign);
02314 case rvc_zero:
02315 if (!fmt->has_signed_zero)
02316 r->sign = 0;
02317 return;
02318
02319 overflow:
02320 get_inf (r, r->sign);
02321 case rvc_inf:
02322 return;
02323
02324 case rvc_nan:
02325 clear_significand_below (r, np2);
02326
02327
02328
02329 for (i = 0; i < SIGSZ; ++i)
02330 if (r->sig[i])
02331 break;
02332 if (i == SIGSZ)
02333 r->sig[SIGSZ-1] = SIG_MSB >> 2;
02334 return;
02335
02336 case rvc_normal:
02337 break;
02338
02339 default:
02340 abort ();
02341 }
02342
02343
02344
02345 if (fmt->log2_b != 1)
02346 {
02347 int shift = r->exp & (fmt->log2_b - 1);
02348 if (shift)
02349 {
02350 shift = fmt->log2_b - shift;
02351 r->sig[0] |= sticky_rshift_significand (r, r, shift);
02352 r->exp += shift;
02353 }
02354 }
02355
02356
02357
02358 if (r->exp > emax2)
02359 goto overflow;
02360 else if (r->exp <= emin2m1)
02361 {
02362 int diff;
02363
02364 if (!fmt->has_denorm)
02365 {
02366
02367 if (r->exp < emin2m1)
02368 goto underflow;
02369 }
02370 else
02371 {
02372 diff = emin2m1 - r->exp + 1;
02373 if (diff > p2)
02374 goto underflow;
02375
02376
02377 r->sig[0] |= sticky_rshift_significand (r, r, diff);
02378 r->exp += diff;
02379 }
02380 }
02381
02382
02383
02384
02385
02386 sticky = 0;
02387 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
02388 sticky |= r->sig[i];
02389 sticky |=
02390 r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
02391
02392 guard = test_significand_bit (r, np2 - 1);
02393 lsb = test_significand_bit (r, np2);
02394
02395
02396 if (guard && (sticky || lsb))
02397 {
02398 REAL_VALUE_TYPE u;
02399 get_zero (&u, 0);
02400 set_significand_bit (&u, np2);
02401
02402 if (add_significands (r, r, &u))
02403 {
02404
02405
02406
02407 if (++r->exp > emax2)
02408 goto overflow;
02409 r->sig[SIGSZ-1] = SIG_MSB;
02410
02411 if (fmt->log2_b != 1)
02412 {
02413 int shift = r->exp & (fmt->log2_b - 1);
02414 if (shift)
02415 {
02416 shift = fmt->log2_b - shift;
02417 rshift_significand (r, r, shift);
02418 r->exp += shift;
02419 if (r->exp > emax2)
02420 goto overflow;
02421 }
02422 }
02423 }
02424 }
02425
02426
02427 if (r->exp <= emin2m1)
02428 goto underflow;
02429
02430
02431 clear_significand_below (r, np2);
02432 }
02433
02434
02435
02436 void
02437 real_convert (r, mode, a)
02438 REAL_VALUE_TYPE *r;
02439 enum machine_mode mode;
02440 const REAL_VALUE_TYPE *a;
02441 {
02442 const struct real_format *fmt;
02443
02444 fmt = real_format_for_mode[mode - QFmode];
02445 if (fmt == NULL)
02446 abort ();
02447
02448 *r = *a;
02449 round_for_format (fmt, r);
02450
02451
02452 if (r->class == rvc_normal)
02453 normalize (r);
02454 }
02455
02456
02457
02458 REAL_VALUE_TYPE
02459 real_value_truncate (mode, a)
02460 enum machine_mode mode;
02461 REAL_VALUE_TYPE a;
02462 {
02463 REAL_VALUE_TYPE r;
02464 real_convert (&r, mode, &a);
02465 return r;
02466 }
02467
02468
02469
02470 bool
02471 exact_real_truncate (mode, a)
02472 enum machine_mode mode;
02473 const REAL_VALUE_TYPE *a;
02474 {
02475 REAL_VALUE_TYPE t;
02476 real_convert (&t, mode, a);
02477 return real_identical (&t, a);
02478 }
02479
02480
02481
02482
02483
02484
02485
02486 long
02487 real_to_target_fmt (buf, r_orig, fmt)
02488 long *buf;
02489 const REAL_VALUE_TYPE *r_orig;
02490 const struct real_format *fmt;
02491 {
02492 REAL_VALUE_TYPE r;
02493 long buf1;
02494
02495 r = *r_orig;
02496 round_for_format (fmt, &r);
02497
02498 if (!buf)
02499 buf = &buf1;
02500 (*fmt->encode) (fmt, buf, &r);
02501
02502 return *buf;
02503 }
02504
02505
02506
02507 long
02508 real_to_target (buf, r, mode)
02509 long *buf;
02510 const REAL_VALUE_TYPE *r;
02511 enum machine_mode mode;
02512 {
02513 const struct real_format *fmt;
02514
02515 fmt = real_format_for_mode[mode - QFmode];
02516 if (fmt == NULL)
02517 abort ();
02518
02519 return real_to_target_fmt (buf, r, fmt);
02520 }
02521
02522
02523
02524
02525
02526 void
02527 real_from_target_fmt (r, buf, fmt)
02528 REAL_VALUE_TYPE *r;
02529 const long *buf;
02530 const struct real_format *fmt;
02531 {
02532 (*fmt->decode) (fmt, r, buf);
02533 }
02534
02535
02536
02537 void
02538 real_from_target (r, buf, mode)
02539 REAL_VALUE_TYPE *r;
02540 const long *buf;
02541 enum machine_mode mode;
02542 {
02543 const struct real_format *fmt;
02544
02545 fmt = real_format_for_mode[mode - QFmode];
02546 if (fmt == NULL)
02547 abort ();
02548
02549 (*fmt->decode) (fmt, r, buf);
02550 }
02551
02552
02553
02554
02555 int
02556 significand_size (mode)
02557 enum machine_mode mode;
02558 {
02559 const struct real_format *fmt;
02560
02561 fmt = real_format_for_mode[mode - QFmode];
02562 if (fmt == NULL)
02563 return 0;
02564
02565 return fmt->p * fmt->log2_b;
02566 }
02567
02568
02569
02570
02571
02572 unsigned int
02573 real_hash (r)
02574 const REAL_VALUE_TYPE *r;
02575 {
02576 unsigned int h;
02577 size_t i;
02578
02579 h = r->class | (r->sign << 2);
02580 switch (r->class)
02581 {
02582 case rvc_zero:
02583 case rvc_inf:
02584 break;
02585
02586 case rvc_normal:
02587 h |= r->exp << 3;
02588
02589
02590 case rvc_nan:
02591 if (sizeof(unsigned long) > sizeof(unsigned int))
02592 for (i = 0; i < SIGSZ; ++i)
02593 {
02594 unsigned long s = r->sig[i];
02595 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
02596 }
02597 else
02598 for (i = 0; i < SIGSZ; ++i)
02599 h ^= r->sig[i];
02600 break;
02601
02602 default:
02603 abort ();
02604 }
02605
02606 return h;
02607 }
02608
02609
02610
02611 static void encode_ieee_single PARAMS ((const struct real_format *fmt,
02612 long *, const REAL_VALUE_TYPE *));
02613 static void decode_ieee_single PARAMS ((const struct real_format *,
02614 REAL_VALUE_TYPE *, const long *));
02615
02616 static void
02617 encode_ieee_single (fmt, buf, r)
02618 const struct real_format *fmt;
02619 long *buf;
02620 const REAL_VALUE_TYPE *r;
02621 {
02622 unsigned long image, sig, exp;
02623 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
02624
02625 image = r->sign << 31;
02626 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
02627
02628 switch (r->class)
02629 {
02630 case rvc_zero:
02631 break;
02632
02633 case rvc_inf:
02634 if (fmt->has_inf)
02635 image |= 255 << 23;
02636 else
02637 image |= 0x7fffffff;
02638 break;
02639
02640 case rvc_nan:
02641 if (fmt->has_nans)
02642 {
02643 image |= 255 << 23;
02644 image |= sig;
02645 if (!fmt->qnan_msb_set)
02646 image ^= 1 << 23 | 1 << 22;
02647 }
02648 else
02649 image |= 0x7fffffff;
02650 break;
02651
02652 case rvc_normal:
02653
02654
02655
02656 if (denormal)
02657 exp = 0;
02658 else
02659 exp = r->exp + 127 - 1;
02660 image |= exp << 23;
02661 image |= sig;
02662 break;
02663
02664 default:
02665 abort ();
02666 }
02667
02668 buf[0] = image;
02669 }
02670
02671 static void
02672 decode_ieee_single (fmt, r, buf)
02673 const struct real_format *fmt;
02674 REAL_VALUE_TYPE *r;
02675 const long *buf;
02676 {
02677 unsigned long image = buf[0] & 0xffffffff;
02678 bool sign = (image >> 31) & 1;
02679 int exp = (image >> 23) & 0xff;
02680
02681 memset (r, 0, sizeof (*r));
02682 image <<= HOST_BITS_PER_LONG - 24;
02683 image &= ~SIG_MSB;
02684
02685 if (exp == 0)
02686 {
02687 if (image && fmt->has_denorm)
02688 {
02689 r->class = rvc_normal;
02690 r->sign = sign;
02691 r->exp = -126;
02692 r->sig[SIGSZ-1] = image << 1;
02693 normalize (r);
02694 }
02695 else if (fmt->has_signed_zero)
02696 r->sign = sign;
02697 }
02698 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
02699 {
02700 if (image)
02701 {
02702 r->class = rvc_nan;
02703 r->sign = sign;
02704 if (!fmt->qnan_msb_set)
02705 image ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
02706 r->sig[SIGSZ-1] = image;
02707 }
02708 else
02709 {
02710 r->class = rvc_inf;
02711 r->sign = sign;
02712 }
02713 }
02714 else
02715 {
02716 r->class = rvc_normal;
02717 r->sign = sign;
02718 r->exp = exp - 127 + 1;
02719 r->sig[SIGSZ-1] = image | SIG_MSB;
02720 }
02721 }
02722
02723 const struct real_format ieee_single_format =
02724 {
02725 encode_ieee_single,
02726 decode_ieee_single,
02727 2,
02728 1,
02729 24,
02730 -125,
02731 128,
02732 true,
02733 true,
02734 true,
02735 true,
02736 true
02737 };
02738
02739
02740
02741
02742 static void encode_ieee_double PARAMS ((const struct real_format *fmt,
02743 long *, const REAL_VALUE_TYPE *));
02744 static void decode_ieee_double PARAMS ((const struct real_format *,
02745 REAL_VALUE_TYPE *, const long *));
02746
02747 static void
02748 encode_ieee_double (fmt, buf, r)
02749 const struct real_format *fmt;
02750 long *buf;
02751 const REAL_VALUE_TYPE *r;
02752 {
02753 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
02754 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
02755
02756 image_hi = r->sign << 31;
02757 image_lo = 0;
02758
02759 if (HOST_BITS_PER_LONG == 64)
02760 {
02761 sig_hi = r->sig[SIGSZ-1];
02762 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
02763 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
02764 }
02765 else
02766 {
02767 sig_hi = r->sig[SIGSZ-1];
02768 sig_lo = r->sig[SIGSZ-2];
02769 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
02770 sig_hi = (sig_hi >> 11) & 0xfffff;
02771 }
02772
02773 switch (r->class)
02774 {
02775 case rvc_zero:
02776 break;
02777
02778 case rvc_inf:
02779 if (fmt->has_inf)
02780 image_hi |= 2047 << 20;
02781 else
02782 {
02783 image_hi |= 0x7fffffff;
02784 image_lo = 0xffffffff;
02785 }
02786 break;
02787
02788 case rvc_nan:
02789 if (fmt->has_nans)
02790 {
02791 image_hi |= 2047 << 20;
02792 image_hi |= sig_hi;
02793 if (!fmt->qnan_msb_set)
02794 image_hi ^= 1 << 19 | 1 << 18;
02795 image_lo = sig_lo;
02796 }
02797 else
02798 {
02799 image_hi |= 0x7fffffff;
02800 image_lo = 0xffffffff;
02801 }
02802 break;
02803
02804 case rvc_normal:
02805
02806
02807
02808 if (denormal)
02809 exp = 0;
02810 else
02811 exp = r->exp + 1023 - 1;
02812 image_hi |= exp << 20;
02813 image_hi |= sig_hi;
02814 image_lo = sig_lo;
02815 break;
02816
02817 default:
02818 abort ();
02819 }
02820
02821 if (FLOAT_WORDS_BIG_ENDIAN)
02822 buf[0] = image_hi, buf[1] = image_lo;
02823 else
02824 buf[0] = image_lo, buf[1] = image_hi;
02825 }
02826
02827 static void
02828 decode_ieee_double (fmt, r, buf)
02829 const struct real_format *fmt;
02830 REAL_VALUE_TYPE *r;
02831 const long *buf;
02832 {
02833 unsigned long image_hi, image_lo;
02834 bool sign;
02835 int exp;
02836
02837 if (FLOAT_WORDS_BIG_ENDIAN)
02838 image_hi = buf[0], image_lo = buf[1];
02839 else
02840 image_lo = buf[0], image_hi = buf[1];
02841 image_lo &= 0xffffffff;
02842 image_hi &= 0xffffffff;
02843
02844 sign = (image_hi >> 31) & 1;
02845 exp = (image_hi >> 20) & 0x7ff;
02846
02847 memset (r, 0, sizeof (*r));
02848
02849 image_hi <<= 32 - 21;
02850 image_hi |= image_lo >> 21;
02851 image_hi &= 0x7fffffff;
02852 image_lo <<= 32 - 21;
02853
02854 if (exp == 0)
02855 {
02856 if ((image_hi || image_lo) && fmt->has_denorm)
02857 {
02858 r->class = rvc_normal;
02859 r->sign = sign;
02860 r->exp = -1022;
02861 if (HOST_BITS_PER_LONG == 32)
02862 {
02863 image_hi = (image_hi << 1) | (image_lo >> 31);
02864 image_lo <<= 1;
02865 r->sig[SIGSZ-1] = image_hi;
02866 r->sig[SIGSZ-2] = image_lo;
02867 }
02868 else
02869 {
02870 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
02871 r->sig[SIGSZ-1] = image_hi;
02872 }
02873 normalize (r);
02874 }
02875 else if (fmt->has_signed_zero)
02876 r->sign = sign;
02877 }
02878 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
02879 {
02880 if (image_hi || image_lo)
02881 {
02882 r->class = rvc_nan;
02883 r->sign = sign;
02884 if (HOST_BITS_PER_LONG == 32)
02885 {
02886 r->sig[SIGSZ-1] = image_hi;
02887 r->sig[SIGSZ-2] = image_lo;
02888 }
02889 else
02890 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
02891
02892 if (!fmt->qnan_msb_set)
02893 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
02894 }
02895 else
02896 {
02897 r->class = rvc_inf;
02898 r->sign = sign;
02899 }
02900 }
02901 else
02902 {
02903 r->class = rvc_normal;
02904 r->sign = sign;
02905 r->exp = exp - 1023 + 1;
02906 if (HOST_BITS_PER_LONG == 32)
02907 {
02908 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
02909 r->sig[SIGSZ-2] = image_lo;
02910 }
02911 else
02912 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
02913 }
02914 }
02915
02916 const struct real_format ieee_double_format =
02917 {
02918 encode_ieee_double,
02919 decode_ieee_double,
02920 2,
02921 1,
02922 53,
02923 -1021,
02924 1024,
02925 true,
02926 true,
02927 true,
02928 true,
02929 true
02930 };
02931
02932
02933
02934
02935
02936
02937 static void encode_ieee_extended PARAMS ((const struct real_format *fmt,
02938 long *, const REAL_VALUE_TYPE *));
02939 static void decode_ieee_extended PARAMS ((const struct real_format *,
02940 REAL_VALUE_TYPE *, const long *));
02941
02942 static void encode_ieee_extended_128 PARAMS ((const struct real_format *fmt,
02943 long *,
02944 const REAL_VALUE_TYPE *));
02945 static void decode_ieee_extended_128 PARAMS ((const struct real_format *,
02946 REAL_VALUE_TYPE *,
02947 const long *));
02948
02949 static void
02950 encode_ieee_extended (fmt, buf, r)
02951 const struct real_format *fmt;
02952 long *buf;
02953 const REAL_VALUE_TYPE *r;
02954 {
02955 unsigned long image_hi, sig_hi, sig_lo;
02956 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
02957
02958 image_hi = r->sign << 15;
02959 sig_hi = sig_lo = 0;
02960
02961 switch (r->class)
02962 {
02963 case rvc_zero:
02964 break;
02965
02966 case rvc_inf:
02967 if (fmt->has_inf)
02968 {
02969 image_hi |= 32767;
02970
02971
02972
02973
02974 sig_hi = 0x80000000;
02975 }
02976 else
02977 {
02978 image_hi |= 32767;
02979 sig_lo = sig_hi = 0xffffffff;
02980 }
02981 break;
02982
02983 case rvc_nan:
02984 if (fmt->has_nans)
02985 {
02986 image_hi |= 32767;
02987 if (HOST_BITS_PER_LONG == 32)
02988 {
02989 sig_hi = r->sig[SIGSZ-1];
02990 sig_lo = r->sig[SIGSZ-2];
02991 }
02992 else
02993 {
02994 sig_lo = r->sig[SIGSZ-1];
02995 sig_hi = sig_lo >> 31 >> 1;
02996 sig_lo &= 0xffffffff;
02997 }
02998 if (!fmt->qnan_msb_set)
02999 sig_hi ^= 1 << 30 | 1 << 29;
03000
03001
03002
03003
03004 sig_hi |= 0x80000000;
03005 }
03006 else
03007 {
03008 image_hi |= 32767;
03009 sig_lo = sig_hi = 0xffffffff;
03010 }
03011 break;
03012
03013 case rvc_normal:
03014 {
03015 int exp = r->exp;
03016
03017
03018
03019
03020
03021
03022
03023
03024
03025
03026 if (denormal)
03027 exp = 0;
03028 else
03029 {
03030 exp += 16383 - 1;
03031 if (exp < 0)
03032 abort ();
03033 }
03034 image_hi |= exp;
03035
03036 if (HOST_BITS_PER_LONG == 32)
03037 {
03038 sig_hi = r->sig[SIGSZ-1];
03039 sig_lo = r->sig[SIGSZ-2];
03040 }
03041 else
03042 {
03043 sig_lo = r->sig[SIGSZ-1];
03044 sig_hi = sig_lo >> 31 >> 1;
03045 sig_lo &= 0xffffffff;
03046 }
03047 }
03048 break;
03049
03050 default:
03051 abort ();
03052 }
03053
03054 if (FLOAT_WORDS_BIG_ENDIAN)
03055 buf[0] = image_hi << 16, buf[1] = sig_hi, buf[2] = sig_lo;
03056 else
03057 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
03058 }
03059
03060 static void
03061 encode_ieee_extended_128 (fmt, buf, r)
03062 const struct real_format *fmt;
03063 long *buf;
03064 const REAL_VALUE_TYPE *r;
03065 {
03066 buf[3 * !FLOAT_WORDS_BIG_ENDIAN] = 0;
03067 encode_ieee_extended (fmt, buf+!!FLOAT_WORDS_BIG_ENDIAN, r);
03068 }
03069
03070 static void
03071 decode_ieee_extended (fmt, r, buf)
03072 const struct real_format *fmt;
03073 REAL_VALUE_TYPE *r;
03074 const long *buf;
03075 {
03076 unsigned long image_hi, sig_hi, sig_lo;
03077 bool sign;
03078 int exp;
03079
03080 if (FLOAT_WORDS_BIG_ENDIAN)
03081 image_hi = buf[0] >> 16, sig_hi = buf[1], sig_lo = buf[2];
03082 else
03083 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
03084 sig_lo &= 0xffffffff;
03085 sig_hi &= 0xffffffff;
03086 image_hi &= 0xffffffff;
03087
03088 sign = (image_hi >> 15) & 1;
03089 exp = image_hi & 0x7fff;
03090
03091 memset (r, 0, sizeof (*r));
03092
03093 if (exp == 0)
03094 {
03095 if ((sig_hi || sig_lo) && fmt->has_denorm)
03096 {
03097 r->class = rvc_normal;
03098 r->sign = sign;
03099
03100
03101
03102
03103
03104
03105 r->exp = fmt->emin;
03106 if (HOST_BITS_PER_LONG == 32)
03107 {
03108 r->sig[SIGSZ-1] = sig_hi;
03109 r->sig[SIGSZ-2] = sig_lo;
03110 }
03111 else
03112 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
03113
03114 normalize (r);
03115 }
03116 else if (fmt->has_signed_zero)
03117 r->sign = sign;
03118 }
03119 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
03120 {
03121
03122
03123
03124 sig_hi &= 0x7fffffff;
03125
03126 if (sig_hi || sig_lo)
03127 {
03128 r->class = rvc_nan;
03129 r->sign = sign;
03130 if (HOST_BITS_PER_LONG == 32)
03131 {
03132 r->sig[SIGSZ-1] = sig_hi;
03133 r->sig[SIGSZ-2] = sig_lo;
03134 }
03135 else
03136 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
03137
03138 if (!fmt->qnan_msb_set)
03139 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
03140 }
03141 else
03142 {
03143 r->class = rvc_inf;
03144 r->sign = sign;
03145 }
03146 }
03147 else
03148 {
03149 r->class = rvc_normal;
03150 r->sign = sign;
03151 r->exp = exp - 16383 + 1;
03152 if (HOST_BITS_PER_LONG == 32)
03153 {
03154 r->sig[SIGSZ-1] = sig_hi;
03155 r->sig[SIGSZ-2] = sig_lo;
03156 }
03157 else
03158 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
03159 }
03160 }
03161
03162 static void
03163 decode_ieee_extended_128 (fmt, r, buf)
03164 const struct real_format *fmt;
03165 REAL_VALUE_TYPE *r;
03166 const long *buf;
03167 {
03168 decode_ieee_extended (fmt, r, buf+!!FLOAT_WORDS_BIG_ENDIAN);
03169 }
03170
03171 const struct real_format ieee_extended_motorola_format =
03172 {
03173 encode_ieee_extended,
03174 decode_ieee_extended,
03175 2,
03176 1,
03177 64,
03178 -16382,
03179 16384,
03180 true,
03181 true,
03182 true,
03183 true,
03184 true
03185 };
03186
03187 const struct real_format ieee_extended_intel_96_format =
03188 {
03189 encode_ieee_extended,
03190 decode_ieee_extended,
03191 2,
03192 1,
03193 64,
03194 -16381,
03195 16384,
03196 true,
03197 true,
03198 true,
03199 true,
03200 true
03201 };
03202
03203 const struct real_format ieee_extended_intel_128_format =
03204 {
03205 encode_ieee_extended_128,
03206 decode_ieee_extended_128,
03207 2,
03208 1,
03209 64,
03210 -16381,
03211 16384,
03212 true,
03213 true,
03214 true,
03215 true,
03216 true
03217 };
03218
03219
03220
03221 const struct real_format ieee_extended_intel_96_round_53_format =
03222 {
03223 encode_ieee_extended,
03224 decode_ieee_extended,
03225 2,
03226 1,
03227 53,
03228 -16381,
03229 16384,
03230 true,
03231 true,
03232 true,
03233 true,
03234 true
03235 };
03236
03237
03238
03239
03240
03241
03242
03243
03244
03245
03246 static void encode_ibm_extended PARAMS ((const struct real_format *fmt,
03247 long *, const REAL_VALUE_TYPE *));
03248 static void decode_ibm_extended PARAMS ((const struct real_format *,
03249 REAL_VALUE_TYPE *, const long *));
03250
03251 static void
03252 encode_ibm_extended (fmt, buf, r)
03253 const struct real_format *fmt ATTRIBUTE_UNUSED;
03254 long *buf;
03255 const REAL_VALUE_TYPE *r;
03256 {
03257 REAL_VALUE_TYPE u, v;
03258
03259 switch (r->class)
03260 {
03261 case rvc_zero:
03262
03263 buf[0] = FLOAT_WORDS_BIG_ENDIAN ? r->sign << 31 : 0;
03264 buf[1] = FLOAT_WORDS_BIG_ENDIAN ? 0 : r->sign << 31;
03265 buf[2] = buf[0];
03266 buf[3] = buf[1];
03267 break;
03268
03269 case rvc_inf:
03270 case rvc_nan:
03271
03272 encode_ieee_double (&ieee_double_format, &buf[0], r);
03273 buf[2] = buf[0];
03274 buf[3] = buf[1];
03275 return;
03276
03277 case rvc_normal:
03278
03279 u = *r;
03280 clear_significand_below (&u, SIGNIFICAND_BITS - 53);
03281
03282 normalize (&u);
03283
03284
03285 if (u.class == rvc_zero)
03286 {
03287 v = u;
03288 u = *r;
03289 normalize (&u);
03290 }
03291 else
03292 {
03293
03294 do_add (&v, r, &u, 1);
03295 round_for_format (&ieee_double_format, &v);
03296 }
03297
03298 round_for_format (&ieee_double_format, &u);
03299
03300 encode_ieee_double (&ieee_double_format, &buf[0], &u);
03301 encode_ieee_double (&ieee_double_format, &buf[2], &v);
03302 break;
03303
03304 default:
03305 abort ();
03306 }
03307 }
03308
03309 static void
03310 decode_ibm_extended (fmt, r, buf)
03311 const struct real_format *fmt ATTRIBUTE_UNUSED;
03312 REAL_VALUE_TYPE *r;
03313 const long *buf;
03314 {
03315 REAL_VALUE_TYPE u, v;
03316
03317 decode_ieee_double (&ieee_double_format, &u, &buf[0]);
03318
03319 if (u.class != rvc_zero && u.class != rvc_inf && u.class != rvc_nan)
03320 {
03321 decode_ieee_double (&ieee_double_format, &v, &buf[2]);
03322 do_add (r, &u, &v, 0);
03323 }
03324 else
03325 *r = u;
03326 }
03327
03328 const struct real_format ibm_extended_format =
03329 {
03330 encode_ibm_extended,
03331 decode_ibm_extended,
03332 2,
03333 1,
03334 53 + 53,
03335 -1021 + 53,
03336 1024,
03337 true,
03338 true,
03339 true,
03340 true,
03341 true
03342 };
03343
03344
03345
03346
03347 static void encode_ieee_quad PARAMS ((const struct real_format *fmt,
03348 long *, const REAL_VALUE_TYPE *));
03349 static void decode_ieee_quad PARAMS ((const struct real_format *,
03350 REAL_VALUE_TYPE *, const long *));
03351
03352 static void
03353 encode_ieee_quad (fmt, buf, r)
03354 const struct real_format *fmt;
03355 long *buf;
03356 const REAL_VALUE_TYPE *r;
03357 {
03358 unsigned long image3, image2, image1, image0, exp;
03359 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
03360 REAL_VALUE_TYPE u;
03361
03362 image3 = r->sign << 31;
03363 image2 = 0;
03364 image1 = 0;
03365 image0 = 0;
03366
03367 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
03368
03369 switch (r->class)
03370 {
03371 case rvc_zero:
03372 break;
03373
03374 case rvc_inf:
03375 if (fmt->has_inf)
03376 image3 |= 32767 << 16;
03377 else
03378 {
03379 image3 |= 0x7fffffff;
03380 image2 = 0xffffffff;
03381 image1 = 0xffffffff;
03382 image0 = 0xffffffff;
03383 }
03384 break;
03385
03386 case rvc_nan:
03387 if (fmt->has_nans)
03388 {
03389 image3 |= 32767 << 16;
03390
03391 if (HOST_BITS_PER_LONG == 32)
03392 {
03393 image0 = u.sig[0];
03394 image1 = u.sig[1];
03395 image2 = u.sig[2];
03396 image3 |= u.sig[3] & 0xffff;
03397 }
03398 else
03399 {
03400 image0 = u.sig[0];
03401 image1 = image0 >> 31 >> 1;
03402 image2 = u.sig[1];
03403 image3 |= (image2 >> 31 >> 1) & 0xffff;
03404 image0 &= 0xffffffff;
03405 image2 &= 0xffffffff;
03406 }
03407
03408 if (!fmt->qnan_msb_set)
03409 image3 ^= 1 << 15 | 1 << 14;
03410 }
03411 else
03412 {
03413 image3 |= 0x7fffffff;
03414 image2 = 0xffffffff;
03415 image1 = 0xffffffff;
03416 image0 = 0xffffffff;
03417 }
03418 break;
03419
03420 case rvc_normal:
03421
03422
03423
03424 if (denormal)
03425 exp = 0;
03426 else
03427 exp = r->exp + 16383 - 1;
03428 image3 |= exp << 16;
03429
03430 if (HOST_BITS_PER_LONG == 32)
03431 {
03432 image0 = u.sig[0];
03433 image1 = u.sig[1];
03434 image2 = u.sig[2];
03435 image3 |= u.sig[3] & 0xffff;
03436 }
03437 else
03438 {
03439 image0 = u.sig[0];
03440 image1 = image0 >> 31 >> 1;
03441 image2 = u.sig[1];
03442 image3 |= (image2 >> 31 >> 1) & 0xffff;
03443 image0 &= 0xffffffff;
03444 image2 &= 0xffffffff;
03445 }
03446 break;
03447
03448 default:
03449 abort ();
03450 }
03451
03452 if (FLOAT_WORDS_BIG_ENDIAN)
03453 {
03454 buf[0] = image3;
03455 buf[1] = image2;
03456 buf[2] = image1;
03457 buf[3] = image0;
03458 }
03459 else
03460 {
03461 buf[0] = image0;
03462 buf[1] = image1;
03463 buf[2] = image2;
03464 buf[3] = image3;
03465 }
03466 }
03467
03468 static void
03469 decode_ieee_quad (fmt, r, buf)
03470 const struct real_format *fmt;
03471 REAL_VALUE_TYPE *r;
03472 const long *buf;
03473 {
03474 unsigned long image3, image2, image1, image0;
03475 bool sign;
03476 int exp;
03477
03478 if (FLOAT_WORDS_BIG_ENDIAN)
03479 {
03480 image3 = buf[0];
03481 image2 = buf[1];
03482 image1 = buf[2];
03483 image0 = buf[3];
03484 }
03485 else
03486 {
03487 image0 = buf[0];
03488 image1 = buf[1];
03489 image2 = buf[2];
03490 image3 = buf[3];
03491 }
03492 image0 &= 0xffffffff;
03493 image1 &= 0xffffffff;
03494 image2 &= 0xffffffff;
03495
03496 sign = (image3 >> 31) & 1;
03497 exp = (image3 >> 16) & 0x7fff;
03498 image3 &= 0xffff;
03499
03500 memset (r, 0, sizeof (*r));
03501
03502 if (exp == 0)
03503 {
03504 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
03505 {
03506 r->class = rvc_normal;
03507 r->sign = sign;
03508
03509 r->exp = -16382 + (SIGNIFICAND_BITS - 112);
03510 if (HOST_BITS_PER_LONG == 32)
03511 {
03512 r->sig[0] = image0;
03513 r->sig[1] = image1;
03514 r->sig[2] = image2;
03515 r->sig[3] = image3;
03516 }
03517 else
03518 {
03519 r->sig[0] = (image1 << 31 << 1) | image0;
03520 r->sig[1] = (image3 << 31 << 1) | image2;
03521 }
03522
03523 normalize (r);
03524 }
03525 else if (fmt->has_signed_zero)
03526 r->sign = sign;
03527 }
03528 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
03529 {
03530 if (image3 | image2 | image1 | image0)
03531 {
03532 r->class = rvc_nan;
03533 r->sign = sign;
03534
03535 if (HOST_BITS_PER_LONG == 32)
03536 {
03537 r->sig[0] = image0;
03538 r->sig[1] = image1;
03539 r->sig[2] = image2;
03540 r->sig[3] = image3;
03541 }
03542 else
03543 {
03544 r->sig[0] = (image1 << 31 << 1) | image0;
03545 r->sig[1] = (image3 << 31 << 1) | image2;
03546 }
03547 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
03548
03549 if (!fmt->qnan_msb_set)
03550 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
03551 }
03552 else
03553 {
03554 r->class = rvc_inf;
03555 r->sign = sign;
03556 }
03557 }
03558 else
03559 {
03560 r->class = rvc_normal;
03561 r->sign = sign;
03562 r->exp = exp - 16383 + 1;
03563
03564 if (HOST_BITS_PER_LONG == 32)
03565 {
03566 r->sig[0] = image0;
03567 r->sig[1] = image1;
03568 r->sig[2] = image2;
03569 r->sig[3] = image3;
03570 }
03571 else
03572 {
03573 r->sig[0] = (image1 << 31 << 1) | image0;
03574 r->sig[1] = (image3 << 31 << 1) | image2;
03575 }
03576 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
03577 r->sig[SIGSZ-1] |= SIG_MSB;
03578 }
03579 }
03580
03581 const struct real_format ieee_quad_format =
03582 {
03583 encode_ieee_quad,
03584 decode_ieee_quad,
03585 2,
03586 1,
03587 113,
03588 -16381,
03589 16384,
03590 true,
03591 true,
03592 true,
03593 true,
03594 true
03595 };
03596
03597
03598
03599
03600
03601
03602
03603
03604
03605
03606
03607 static void encode_vax_f PARAMS ((const struct real_format *fmt,
03608 long *, const REAL_VALUE_TYPE *));
03609 static void decode_vax_f PARAMS ((const struct real_format *,
03610 REAL_VALUE_TYPE *, const long *));
03611 static void encode_vax_d PARAMS ((const struct real_format *fmt,
03612 long *, const REAL_VALUE_TYPE *));
03613 static void decode_vax_d PARAMS ((const struct real_format *,
03614 REAL_VALUE_TYPE *, const long *));
03615 static void encode_vax_g PARAMS ((const struct real_format *fmt,
03616 long *, const REAL_VALUE_TYPE *));
03617 static void decode_vax_g PARAMS ((const struct real_format *,
03618 REAL_VALUE_TYPE *, const long *));
03619
03620 static void
03621 encode_vax_f (fmt, buf, r)
03622 const struct real_format *fmt ATTRIBUTE_UNUSED;
03623 long *buf;
03624 const REAL_VALUE_TYPE *r;
03625 {
03626 unsigned long sign, exp, sig, image;
03627
03628 sign = r->sign << 15;
03629
03630 switch (r->class)
03631 {
03632 case rvc_zero:
03633 image = 0;
03634 break;
03635
03636 case rvc_inf:
03637 case rvc_nan:
03638 image = 0xffff7fff | sign;
03639 break;
03640
03641 case rvc_normal:
03642 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
03643 exp = r->exp + 128;
03644
03645 image = (sig << 16) & 0xffff0000;
03646 image |= sign;
03647 image |= exp << 7;
03648 image |= sig >> 16;
03649 break;
03650
03651 default:
03652 abort ();
03653 }
03654
03655 buf[0] = image;
03656 }
03657
03658 static void
03659 decode_vax_f (fmt, r, buf)
03660 const struct real_format *fmt ATTRIBUTE_UNUSED;
03661 REAL_VALUE_TYPE *r;
03662 const long *buf;
03663 {
03664 unsigned long image = buf[0] & 0xffffffff;
03665 int exp = (image >> 7) & 0xff;
03666
03667 memset (r, 0, sizeof (*r));
03668
03669 if (exp != 0)
03670 {
03671 r->class = rvc_normal;
03672 r->sign = (image >> 15) & 1;
03673 r->exp = exp - 128;
03674
03675 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
03676 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
03677 }
03678 }
03679
03680 static void
03681 encode_vax_d (fmt, buf, r)
03682 const struct real_format *fmt ATTRIBUTE_UNUSED;
03683 long *buf;
03684 const REAL_VALUE_TYPE *r;
03685 {
03686 unsigned long image0, image1, sign = r->sign << 15;
03687
03688 switch (r->class)
03689 {
03690 case rvc_zero:
03691 image0 = image1 = 0;
03692 break;
03693
03694 case rvc_inf:
03695 case rvc_nan:
03696 image0 = 0xffff7fff | sign;
03697 image1 = 0xffffffff;
03698 break;
03699
03700 case rvc_normal:
03701
03702 if (HOST_BITS_PER_LONG == 64)
03703 {
03704 image0 = r->sig[SIGSZ-1];
03705 image1 = (image0 >> (64 - 56)) & 0xffffffff;
03706 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
03707 }
03708 else
03709 {
03710 image0 = r->sig[SIGSZ-1];
03711 image1 = r->sig[SIGSZ-2];
03712 image1 = (image0 << 24) | (image1 >> 8);
03713 image0 = (image0 >> 8) & 0xffffff;
03714 }
03715
03716
03717
03718 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
03719 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
03720
03721
03722 image0 |= sign;
03723 image0 |= (r->exp + 128) << 7;
03724 break;
03725
03726 default:
03727 abort ();
03728 }
03729
03730 if (FLOAT_WORDS_BIG_ENDIAN)
03731 buf[0] = image1, buf[1] = image0;
03732 else
03733 buf[0] = image0, buf[1] = image1;
03734 }
03735
03736 static void
03737 decode_vax_d (fmt, r, buf)
03738 const struct real_format *fmt ATTRIBUTE_UNUSED;
03739 REAL_VALUE_TYPE *r;
03740 const long *buf;
03741 {
03742 unsigned long image0, image1;
03743 int exp;
03744
03745 if (FLOAT_WORDS_BIG_ENDIAN)
03746 image1 = buf[0], image0 = buf[1];
03747 else
03748 image0 = buf[0], image1 = buf[1];
03749 image0 &= 0xffffffff;
03750 image1 &= 0xffffffff;
03751
03752 exp = (image0 >> 7) & 0x7f;
03753
03754 memset (r, 0, sizeof (*r));
03755
03756 if (exp != 0)
03757 {
03758 r->class = rvc_normal;
03759 r->sign = (image0 >> 15) & 1;
03760 r->exp = exp - 128;
03761
03762
03763
03764 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
03765 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
03766
03767 if (HOST_BITS_PER_LONG == 64)
03768 {
03769 image0 = (image0 << 31 << 1) | image1;
03770 image0 <<= 64 - 56;
03771 image0 |= SIG_MSB;
03772 r->sig[SIGSZ-1] = image0;
03773 }
03774 else
03775 {
03776 r->sig[SIGSZ-1] = image0;
03777 r->sig[SIGSZ-2] = image1;
03778 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
03779 r->sig[SIGSZ-1] |= SIG_MSB;
03780 }
03781 }
03782 }
03783
03784 static void
03785 encode_vax_g (fmt, buf, r)
03786 const struct real_format *fmt ATTRIBUTE_UNUSED;
03787 long *buf;
03788 const REAL_VALUE_TYPE *r;
03789 {
03790 unsigned long image0, image1, sign = r->sign << 15;
03791
03792 switch (r->class)
03793 {
03794 case rvc_zero:
03795 image0 = image1 = 0;
03796 break;
03797
03798 case rvc_inf:
03799 case rvc_nan:
03800 image0 = 0xffff7fff | sign;
03801 image1 = 0xffffffff;
03802 break;
03803
03804 case rvc_normal:
03805
03806 if (HOST_BITS_PER_LONG == 64)
03807 {
03808 image0 = r->sig[SIGSZ-1];
03809 image1 = (image0 >> (64 - 53)) & 0xffffffff;
03810 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
03811 }
03812 else
03813 {
03814 image0 = r->sig[SIGSZ-1];
03815 image1 = r->sig[SIGSZ-2];
03816 image1 = (image0 << 21) | (image1 >> 11);
03817 image0 = (image0 >> 11) & 0xfffff;
03818 }
03819
03820
03821
03822 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
03823 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
03824
03825
03826 image0 |= sign;
03827 image0 |= (r->exp + 1024) << 4;
03828 break;
03829
03830 default:
03831 abort ();
03832 }
03833
03834 if (FLOAT_WORDS_BIG_ENDIAN)
03835 buf[0] = image1, buf[1] = image0;
03836 else
03837 buf[0] = image0, buf[1] = image1;
03838 }
03839
03840 static void
03841 decode_vax_g (fmt, r, buf)
03842 const struct real_format *fmt ATTRIBUTE_UNUSED;
03843 REAL_VALUE_TYPE *r;
03844 const long *buf;
03845 {
03846 unsigned long image0, image1;
03847 int exp;
03848
03849 if (FLOAT_WORDS_BIG_ENDIAN)
03850 image1 = buf[0], image0 = buf[1];
03851 else
03852 image0 = buf[0], image1 = buf[1];
03853 image0 &= 0xffffffff;
03854 image1 &= 0xffffffff;
03855
03856 exp = (image0 >> 4) & 0x7ff;
03857
03858 memset (r, 0, sizeof (*r));
03859
03860 if (exp != 0)
03861 {
03862 r->class = rvc_normal;
03863 r->sign = (image0 >> 15) & 1;
03864 r->exp = exp - 1024;
03865
03866
03867
03868 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
03869 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
03870
03871 if (HOST_BITS_PER_LONG == 64)
03872 {
03873 image0 = (image0 << 31 << 1) | image1;
03874 image0 <<= 64 - 53;
03875 image0 |= SIG_MSB;
03876 r->sig[SIGSZ-1] = image0;
03877 }
03878 else
03879 {
03880 r->sig[SIGSZ-1] = image0;
03881 r->sig[SIGSZ-2] = image1;
03882 lshift_significand (r, r, 64 - 53);
03883 r->sig[SIGSZ-1] |= SIG_MSB;
03884 }
03885 }
03886 }
03887
03888 const struct real_format vax_f_format =
03889 {
03890 encode_vax_f,
03891 decode_vax_f,
03892 2,
03893 1,
03894 24,
03895 -127,
03896 127,
03897 false,
03898 false,
03899 false,
03900 false,
03901 false
03902 };
03903
03904 const struct real_format vax_d_format =
03905 {
03906 encode_vax_d,
03907 decode_vax_d,
03908 2,
03909 1,
03910 56,
03911 -127,
03912 127,
03913 false,
03914 false,
03915 false,
03916 false,
03917 false
03918 };
03919
03920 const struct real_format vax_g_format =
03921 {
03922 encode_vax_g,
03923 decode_vax_g,
03924 2,
03925 1,
03926 53,
03927 -1023,
03928 1023,
03929 false,
03930 false,
03931 false,
03932 false,
03933 false
03934 };
03935
03936
03937
03938
03939
03940
03941
03942
03943 static void encode_i370_single PARAMS ((const struct real_format *fmt,
03944 long *, const REAL_VALUE_TYPE *));
03945 static void decode_i370_single PARAMS ((const struct real_format *,
03946 REAL_VALUE_TYPE *, const long *));
03947 static void encode_i370_double PARAMS ((const struct real_format *fmt,
03948 long *, const REAL_VALUE_TYPE *));
03949 static void decode_i370_double PARAMS ((const struct real_format *,
03950 REAL_VALUE_TYPE *, const long *));
03951
03952 static void
03953 encode_i370_single (fmt, buf, r)
03954 const struct real_format *fmt ATTRIBUTE_UNUSED;
03955 long *buf;
03956 const REAL_VALUE_TYPE *r;
03957 {
03958 unsigned long sign, exp, sig, image;
03959
03960 sign = r->sign << 31;
03961
03962 switch (r->class)
03963 {
03964 case rvc_zero:
03965 image = 0;
03966 break;
03967
03968 case rvc_inf:
03969 case rvc_nan:
03970 image = 0x7fffffff | sign;
03971 break;
03972
03973 case rvc_normal:
03974 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
03975 exp = ((r->exp / 4) + 64) << 24;
03976 image = sign | exp | sig;
03977 break;
03978
03979 default:
03980 abort ();
03981 }
03982
03983 buf[0] = image;
03984 }
03985
03986 static void
03987 decode_i370_single (fmt, r, buf)
03988 const struct real_format *fmt ATTRIBUTE_UNUSED;
03989 REAL_VALUE_TYPE *r;
03990 const long *buf;
03991 {
03992 unsigned long sign, sig, image = buf[0];
03993 int exp;
03994
03995 sign = (image >> 31) & 1;
03996 exp = (image >> 24) & 0x7f;
03997 sig = image & 0xffffff;
03998
03999 memset (r, 0, sizeof (*r));
04000
04001 if (exp || sig)
04002 {
04003 r->class = rvc_normal;
04004 r->sign = sign;
04005 r->exp = (exp - 64) * 4;
04006 r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
04007 normalize (r);
04008 }
04009 }
04010
04011 static void
04012 encode_i370_double (fmt, buf, r)
04013 const struct real_format *fmt ATTRIBUTE_UNUSED;
04014 long *buf;
04015 const REAL_VALUE_TYPE *r;
04016 {
04017 unsigned long sign, exp, image_hi, image_lo;
04018
04019 sign = r->sign << 31;
04020
04021 switch (r->class)
04022 {
04023 case rvc_zero:
04024 image_hi = image_lo = 0;
04025 break;
04026
04027 case rvc_inf:
04028 case rvc_nan:
04029 image_hi = 0x7fffffff | sign;
04030 image_lo = 0xffffffff;
04031 break;
04032
04033 case rvc_normal:
04034 if (HOST_BITS_PER_LONG == 64)
04035 {
04036 image_hi = r->sig[SIGSZ-1];
04037 image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
04038 image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
04039 }
04040 else
04041 {
04042 image_hi = r->sig[SIGSZ-1];
04043 image_lo = r->sig[SIGSZ-2];
04044 image_lo = (image_lo >> 8) | (image_hi << 24);
04045 image_hi >>= 8;
04046 }
04047
04048 exp = ((r->exp / 4) + 64) << 24;
04049 image_hi |= sign | exp;
04050 break;
04051
04052 default:
04053 abort ();
04054 }
04055
04056 if (FLOAT_WORDS_BIG_ENDIAN)
04057 buf[0] = image_hi, buf[1] = image_lo;
04058 else
04059 buf[0] = image_lo, buf[1] = image_hi;
04060 }
04061
04062 static void
04063 decode_i370_double (fmt, r, buf)
04064 const struct real_format *fmt ATTRIBUTE_UNUSED;
04065 REAL_VALUE_TYPE *r;
04066 const long *buf;
04067 {
04068 unsigned long sign, image_hi, image_lo;
04069 int exp;
04070
04071 if (FLOAT_WORDS_BIG_ENDIAN)
04072 image_hi = buf[0], image_lo = buf[1];
04073 else
04074 image_lo = buf[0], image_hi = buf[1];
04075
04076 sign = (image_hi >> 31) & 1;
04077 exp = (image_hi >> 24) & 0x7f;
04078 image_hi &= 0xffffff;
04079 image_lo &= 0xffffffff;
04080
04081 memset (r, 0, sizeof (*r));
04082
04083 if (exp || image_hi || image_lo)
04084 {
04085 r->class = rvc_normal;
04086 r->sign = sign;
04087 r->exp = (exp - 64) * 4 + (SIGNIFICAND_BITS - 56);
04088
04089 if (HOST_BITS_PER_LONG == 32)
04090 {
04091 r->sig[0] = image_lo;
04092 r->sig[1] = image_hi;
04093 }
04094 else
04095 r->sig[0] = image_lo | (image_hi << 31 << 1);
04096
04097 normalize (r);
04098 }
04099 }
04100
04101 const struct real_format i370_single_format =
04102 {
04103 encode_i370_single,
04104 decode_i370_single,
04105 16,
04106 4,
04107 6,
04108 -64,
04109 63,
04110 false,
04111 false,
04112 false,
04113 false,
04114 false
04115 };
04116
04117 const struct real_format i370_double_format =
04118 {
04119 encode_i370_double,
04120 decode_i370_double,
04121 16,
04122 4,
04123 14,
04124 -64,
04125 63,
04126 false,
04127 false,
04128 false,
04129 false,
04130 false
04131 };
04132
04133
04134
04135
04136
04137
04138
04139
04140
04141
04142
04143
04144
04145
04146
04147
04148
04149
04150
04151
04152 static void encode_c4x_single PARAMS ((const struct real_format *fmt,
04153 long *, const REAL_VALUE_TYPE *));
04154 static void decode_c4x_single PARAMS ((const struct real_format *,
04155 REAL_VALUE_TYPE *, const long *));
04156 static void encode_c4x_extended PARAMS ((const struct real_format *fmt,
04157 long *, const REAL_VALUE_TYPE *));
04158 static void decode_c4x_extended PARAMS ((const struct real_format *,
04159 REAL_VALUE_TYPE *, const long *));
04160
04161 static void
04162 encode_c4x_single (fmt, buf, r)
04163 const struct real_format *fmt ATTRIBUTE_UNUSED;
04164 long *buf;
04165 const REAL_VALUE_TYPE *r;
04166 {
04167 unsigned long image, exp, sig;
04168
04169 switch (r->class)
04170 {
04171 case rvc_zero:
04172 exp = -128;
04173 sig = 0;
04174 break;
04175
04176 case rvc_inf:
04177 case rvc_nan:
04178 exp = 127;
04179 sig = 0x800000 - r->sign;
04180 break;
04181
04182 case rvc_normal:
04183 exp = r->exp - 1;
04184 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
04185 if (r->sign)
04186 {
04187 if (sig)
04188 sig = -sig;
04189 else
04190 exp--;
04191 sig |= 0x800000;
04192 }
04193 break;
04194
04195 default:
04196 abort ();
04197 }
04198
04199 image = ((exp & 0xff) << 24) | (sig & 0xffffff);
04200 buf[0] = image;
04201 }
04202
04203 static void
04204 decode_c4x_single (fmt, r, buf)
04205 const struct real_format *fmt ATTRIBUTE_UNUSED;
04206 REAL_VALUE_TYPE *r;
04207 const long *buf;
04208 {
04209 unsigned long image = buf[0];
04210 unsigned long sig;
04211 int exp, sf;
04212
04213 exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
04214 sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
04215
04216 memset (r, 0, sizeof (*r));
04217
04218 if (exp != -128)
04219 {
04220 r->class = rvc_normal;
04221
04222 sig = sf & 0x7fffff;
04223 if (sf < 0)
04224 {
04225 r->sign = 1;
04226 if (sig)
04227 sig = -sig;
04228 else
04229 exp++;
04230 }
04231 sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
04232
04233 r->exp = exp + 1;
04234 r->sig[SIGSZ-1] = sig;
04235 }
04236 }
04237
04238 static void
04239 encode_c4x_extended (fmt, buf, r)
04240 const struct real_format *fmt ATTRIBUTE_UNUSED;
04241 long *buf;
04242 const REAL_VALUE_TYPE *r;
04243 {
04244 unsigned long exp, sig;
04245
04246 switch (r->class)
04247 {
04248 case rvc_zero:
04249 exp = -128;
04250 sig = 0;
04251 break;
04252
04253 case rvc_inf:
04254 case rvc_nan:
04255 exp = 127;
04256 sig = 0x80000000 - r->sign;
04257 break;
04258
04259 case rvc_normal:
04260 exp = r->exp - 1;
04261
04262 sig = r->sig[SIGSZ-1];
04263 if (HOST_BITS_PER_LONG == 64)
04264 sig = sig >> 1 >> 31;
04265 sig &= 0x7fffffff;
04266
04267 if (r->sign)
04268 {
04269 if (sig)
04270 sig = -sig;
04271 else
04272 exp--;
04273 sig |= 0x80000000;
04274 }
04275 break;
04276
04277 default:
04278 abort ();
04279 }
04280
04281 exp = (exp & 0xff) << 24;
04282 sig &= 0xffffffff;
04283
04284 if (FLOAT_WORDS_BIG_ENDIAN)
04285 buf[0] = exp, buf[1] = sig;
04286 else
04287 buf[0] = sig, buf[0] = exp;
04288 }
04289
04290 static void
04291 decode_c4x_extended (fmt, r, buf)
04292 const struct real_format *fmt ATTRIBUTE_UNUSED;
04293 REAL_VALUE_TYPE *r;
04294 const long *buf;
04295 {
04296 unsigned long sig;
04297 int exp, sf;
04298
04299 if (FLOAT_WORDS_BIG_ENDIAN)
04300 exp = buf[0], sf = buf[1];
04301 else
04302 sf = buf[0], exp = buf[1];
04303
04304 exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
04305 sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
04306
04307 memset (r, 0, sizeof (*r));
04308
04309 if (exp != -128)
04310 {
04311 r->class = rvc_normal;
04312
04313 sig = sf & 0x7fffffff;
04314 if (sf < 0)
04315 {
04316 r->sign = 1;
04317 if (sig)
04318 sig = -sig;
04319 else
04320 exp++;
04321 }
04322 if (HOST_BITS_PER_LONG == 64)
04323 sig = sig << 1 << 31;
04324 sig |= SIG_MSB;
04325
04326 r->exp = exp + 1;
04327 r->sig[SIGSZ-1] = sig;
04328 }
04329 }
04330
04331 const struct real_format c4x_single_format =
04332 {
04333 encode_c4x_single,
04334 decode_c4x_single,
04335 2,
04336 1,
04337 24,
04338 -126,
04339 128,
04340 false,
04341 false,
04342 false,
04343 false,
04344 false
04345 };
04346
04347 const struct real_format c4x_extended_format =
04348 {
04349 encode_c4x_extended,
04350 decode_c4x_extended,
04351 2,
04352 1,
04353 32,
04354 -126,
04355 128,
04356 false,
04357 false,
04358 false,
04359 false,
04360 false
04361 };
04362
04363
04364
04365
04366
04367
04368
04369 static void encode_internal PARAMS ((const struct real_format *fmt,
04370 long *, const REAL_VALUE_TYPE *));
04371 static void decode_internal PARAMS ((const struct real_format *,
04372 REAL_VALUE_TYPE *, const long *));
04373
04374 static void
04375 encode_internal (fmt, buf, r)
04376 const struct real_format *fmt ATTRIBUTE_UNUSED;
04377 long *buf;
04378 const REAL_VALUE_TYPE *r;
04379 {
04380 memcpy (buf, r, sizeof (*r));
04381 }
04382
04383 static void
04384 decode_internal (fmt, r, buf)
04385 const struct real_format *fmt ATTRIBUTE_UNUSED;
04386 REAL_VALUE_TYPE *r;
04387 const long *buf;
04388 {
04389 memcpy (r, buf, sizeof (*r));
04390 }
04391
04392 const struct real_format real_internal_format =
04393 {
04394 encode_internal,
04395 decode_internal,
04396 2,
04397 1,
04398 SIGNIFICAND_BITS - 2,
04399 -MAX_EXP,
04400 MAX_EXP,
04401 true,
04402 true,
04403 false,
04404 true,
04405 true
04406 };
04407
04408
04409
04410
04411 const struct real_format *real_format_for_mode[TFmode - QFmode + 1] =
04412 {
04413 NULL,
04414 NULL,
04415 NULL,
04416 &ieee_single_format,
04417 &ieee_double_format,
04418
04419
04420
04421 NULL,
04422 &ieee_quad_format
04423 };