1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 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>
7 This file is part of GCC.
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
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
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
26 #include "coretypes.h"
33 /* The floating point model used internally is not exactly IEEE 754
34 compliant, and close to the description in the ISO C standard,
35 section 5.2.4.2.2 Characteristics of floating types.
39 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
43 b = base or radix, here always 2
45 p = precision (the number of base-b digits in the significand)
46 f_k = the digits of the significand.
48 We differ from typical IEEE 754 encodings in that the entire
49 significand is fractional. Normalized significands are in the
52 A requirement of the model is that P be larger than than the
53 largest supported target floating-point type by at least 2 bits.
54 This gives us proper rounding when we truncate to the target type.
55 In addition, E must be large enough to hold the smallest supported
56 denormal number in a normalized form.
58 Both of these requirements are easily satisfied. The largest target
59 significand is 113 bits; we store at least 160. The smallest
60 denormal number fits in 17 exponent bits; we store 29.
62 Note that the decimal string conversion routines are sensitive to
63 rounding error. Since the raw arithmetic routines do not themselves
64 have guard digits or rounding, the computation of 10**exp can
65 accumulate more than a few digits of error. The previous incarnation
66 of real.c successfully used a 144 bit fraction; given the current
67 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits.
69 Target floating point models that use base 16 instead of base 2
70 (i.e. IBM 370), are handled during round_for_format, in which we
71 canonicalize the exponent to be a multiple of 4 (log2(16)), and
72 adjust the significand to match. */
75 /* Used to classify two numbers simultaneously. */
76 #define CLASS2(A, B) ((A) << 2 | (B))
78 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
79 #error "Some constant folding done by hand to avoid shift count warnings"
82 static void get_zero PARAMS ((REAL_VALUE_TYPE *, int));
83 static void get_canonical_qnan PARAMS ((REAL_VALUE_TYPE *, int));
84 static void get_canonical_snan PARAMS ((REAL_VALUE_TYPE *, int));
85 static void get_inf PARAMS ((REAL_VALUE_TYPE *, int));
86 static bool sticky_rshift_significand PARAMS ((REAL_VALUE_TYPE *,
87 const REAL_VALUE_TYPE *,
89 static void rshift_significand PARAMS ((REAL_VALUE_TYPE *,
90 const REAL_VALUE_TYPE *,
92 static void lshift_significand PARAMS ((REAL_VALUE_TYPE *,
93 const REAL_VALUE_TYPE *,
95 static void lshift_significand_1 PARAMS ((REAL_VALUE_TYPE *,
96 const REAL_VALUE_TYPE *));
97 static bool add_significands PARAMS ((REAL_VALUE_TYPE *r,
98 const REAL_VALUE_TYPE *,
99 const REAL_VALUE_TYPE *));
100 static bool sub_significands PARAMS ((REAL_VALUE_TYPE *,
101 const REAL_VALUE_TYPE *,
102 const REAL_VALUE_TYPE *, int));
103 static void neg_significand PARAMS ((REAL_VALUE_TYPE *,
104 const REAL_VALUE_TYPE *));
105 static int cmp_significands PARAMS ((const REAL_VALUE_TYPE *,
106 const REAL_VALUE_TYPE *));
107 static int cmp_significand_0 PARAMS ((const REAL_VALUE_TYPE *));
108 static void set_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
109 static void clear_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
110 static bool test_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
111 static void clear_significand_below PARAMS ((REAL_VALUE_TYPE *,
113 static bool div_significands PARAMS ((REAL_VALUE_TYPE *,
114 const REAL_VALUE_TYPE *,
115 const REAL_VALUE_TYPE *));
116 static void normalize PARAMS ((REAL_VALUE_TYPE *));
118 static void do_add PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
119 const REAL_VALUE_TYPE *, int));
120 static void do_multiply PARAMS ((REAL_VALUE_TYPE *,
121 const REAL_VALUE_TYPE *,
122 const REAL_VALUE_TYPE *));
123 static void do_divide PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
124 const REAL_VALUE_TYPE *));
125 static int do_compare PARAMS ((const REAL_VALUE_TYPE *,
126 const REAL_VALUE_TYPE *, int));
127 static void do_fix_trunc PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *));
129 static unsigned long rtd_divmod PARAMS ((REAL_VALUE_TYPE *,
132 static const REAL_VALUE_TYPE * ten_to_ptwo PARAMS ((int));
133 static const REAL_VALUE_TYPE * ten_to_mptwo PARAMS ((int));
134 static const REAL_VALUE_TYPE * real_digit PARAMS ((int));
135 static void times_pten PARAMS ((REAL_VALUE_TYPE *, int));
137 static void round_for_format PARAMS ((const struct real_format *,
140 /* Initialize R with a positive zero. */
147 memset (r, 0, sizeof (*r));
151 /* Initialize R with the canonical quiet NaN. */
154 get_canonical_qnan (r, sign)
158 memset (r, 0, sizeof (*r));
164 get_canonical_snan (r, sign)
168 memset (r, 0, sizeof (*r));
179 memset (r, 0, sizeof (*r));
185 /* Right-shift the significand of A by N bits; put the result in the
186 significand of R. If any one bits are shifted out, return true. */
189 sticky_rshift_significand (r, a, n)
191 const REAL_VALUE_TYPE *a;
194 unsigned long sticky = 0;
195 unsigned int i, ofs = 0;
197 if (n >= HOST_BITS_PER_LONG)
199 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
201 n &= HOST_BITS_PER_LONG - 1;
206 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
207 for (i = 0; i < SIGSZ; ++i)
210 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
211 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
212 << (HOST_BITS_PER_LONG - n)));
217 for (i = 0; ofs + i < SIGSZ; ++i)
218 r->sig[i] = a->sig[ofs + i];
219 for (; i < SIGSZ; ++i)
226 /* Right-shift the significand of A by N bits; put the result in the
230 rshift_significand (r, a, n)
232 const REAL_VALUE_TYPE *a;
235 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
237 n &= HOST_BITS_PER_LONG - 1;
240 for (i = 0; i < SIGSZ; ++i)
243 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
244 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
245 << (HOST_BITS_PER_LONG - n)));
250 for (i = 0; ofs + i < SIGSZ; ++i)
251 r->sig[i] = a->sig[ofs + i];
252 for (; i < SIGSZ; ++i)
257 /* Left-shift the significand of A by N bits; put the result in the
261 lshift_significand (r, a, n)
263 const REAL_VALUE_TYPE *a;
266 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
268 n &= HOST_BITS_PER_LONG - 1;
271 for (i = 0; ofs + i < SIGSZ; ++i)
272 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
273 for (; i < SIGSZ; ++i)
274 r->sig[SIGSZ-1-i] = 0;
277 for (i = 0; i < SIGSZ; ++i)
280 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
281 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
282 >> (HOST_BITS_PER_LONG - n)));
286 /* Likewise, but N is specialized to 1. */
289 lshift_significand_1 (r, a)
291 const REAL_VALUE_TYPE *a;
295 for (i = SIGSZ - 1; i > 0; --i)
296 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
297 r->sig[0] = a->sig[0] << 1;
300 /* Add the significands of A and B, placing the result in R. Return
301 true if there was carry out of the most significant word. */
304 add_significands (r, a, b)
306 const REAL_VALUE_TYPE *a, *b;
311 for (i = 0; i < SIGSZ; ++i)
313 unsigned long ai = a->sig[i];
314 unsigned long ri = ai + b->sig[i];
330 /* Subtract the significands of A and B, placing the result in R. CARRY is
331 true if there's a borrow incoming to the least significant word.
332 Return true if there was borrow out of the most significant word. */
335 sub_significands (r, a, b, carry)
337 const REAL_VALUE_TYPE *a, *b;
342 for (i = 0; i < SIGSZ; ++i)
344 unsigned long ai = a->sig[i];
345 unsigned long ri = ai - b->sig[i];
361 /* Negate the significand A, placing the result in R. */
364 neg_significand (r, a)
366 const REAL_VALUE_TYPE *a;
371 for (i = 0; i < SIGSZ; ++i)
373 unsigned long ri, ai = a->sig[i];
392 /* Compare significands. Return tri-state vs zero. */
395 cmp_significands (a, b)
396 const REAL_VALUE_TYPE *a, *b;
400 for (i = SIGSZ - 1; i >= 0; --i)
402 unsigned long ai = a->sig[i];
403 unsigned long bi = b->sig[i];
414 /* Return true if A is nonzero. */
417 cmp_significand_0 (a)
418 const REAL_VALUE_TYPE *a;
422 for (i = SIGSZ - 1; i >= 0; --i)
429 /* Set bit N of the significand of R. */
432 set_significand_bit (r, n)
436 r->sig[n / HOST_BITS_PER_LONG]
437 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
440 /* Clear bit N of the significand of R. */
443 clear_significand_bit (r, n)
447 r->sig[n / HOST_BITS_PER_LONG]
448 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
451 /* Test bit N of the significand of R. */
454 test_significand_bit (r, n)
458 /* ??? Compiler bug here if we return this expression directly.
459 The conversion to bool strips the "&1" and we wind up testing
460 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
461 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
465 /* Clear bits 0..N-1 of the significand of R. */
468 clear_significand_below (r, n)
472 int i, w = n / HOST_BITS_PER_LONG;
474 for (i = 0; i < w; ++i)
477 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
480 /* Divide the significands of A and B, placing the result in R. Return
481 true if the division was inexact. */
484 div_significands (r, a, b)
486 const REAL_VALUE_TYPE *a, *b;
489 int i, bit = SIGNIFICAND_BITS - 1;
490 unsigned long msb, inexact;
493 memset (r->sig, 0, sizeof (r->sig));
499 msb = u.sig[SIGSZ-1] & SIG_MSB;
500 lshift_significand_1 (&u, &u);
502 if (msb || cmp_significands (&u, b) >= 0)
504 sub_significands (&u, &u, b, 0);
505 set_significand_bit (r, bit);
510 for (i = 0, inexact = 0; i < SIGSZ; i++)
516 /* Adjust the exponent and significand of R such that the most
517 significant bit is set. We underflow to zero and overflow to
518 infinity here, without denormals. (The intermediate representation
519 exponent is large enough to handle target denormals normalized.) */
528 /* Find the first word that is nonzero. */
529 for (i = SIGSZ - 1; i >= 0; i--)
531 shift += HOST_BITS_PER_LONG;
535 /* Zero significand flushes to zero. */
543 /* Find the first bit that is nonzero. */
545 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
551 exp = r->exp - shift;
553 get_inf (r, r->sign);
554 else if (exp < -MAX_EXP)
555 get_zero (r, r->sign);
559 lshift_significand (r, r, shift);
564 /* Return R = A + (SUBTRACT_P ? -B : B). */
567 do_add (r, a, b, subtract_p)
569 const REAL_VALUE_TYPE *a, *b;
574 bool inexact = false;
576 /* Determine if we need to add or subtract. */
578 subtract_p = (sign ^ b->sign) ^ subtract_p;
580 switch (CLASS2 (a->class, b->class))
582 case CLASS2 (rvc_zero, rvc_zero):
583 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
584 get_zero (r, sign & !subtract_p);
587 case CLASS2 (rvc_zero, rvc_normal):
588 case CLASS2 (rvc_zero, rvc_inf):
589 case CLASS2 (rvc_zero, rvc_nan):
591 case CLASS2 (rvc_normal, rvc_nan):
592 case CLASS2 (rvc_inf, rvc_nan):
593 case CLASS2 (rvc_nan, rvc_nan):
594 /* ANY + NaN = NaN. */
595 case CLASS2 (rvc_normal, rvc_inf):
598 r->sign = sign ^ subtract_p;
601 case CLASS2 (rvc_normal, rvc_zero):
602 case CLASS2 (rvc_inf, rvc_zero):
603 case CLASS2 (rvc_nan, rvc_zero):
605 case CLASS2 (rvc_nan, rvc_normal):
606 case CLASS2 (rvc_nan, rvc_inf):
607 /* NaN + ANY = NaN. */
608 case CLASS2 (rvc_inf, rvc_normal):
613 case CLASS2 (rvc_inf, rvc_inf):
615 /* Inf - Inf = NaN. */
616 get_canonical_qnan (r, 0);
618 /* Inf + Inf = Inf. */
622 case CLASS2 (rvc_normal, rvc_normal):
629 /* Swap the arguments such that A has the larger exponent. */
630 dexp = a->exp - b->exp;
633 const REAL_VALUE_TYPE *t;
640 /* If the exponents are not identical, we need to shift the
641 significand of B down. */
644 /* If the exponents are too far apart, the significands
645 do not overlap, which makes the subtraction a noop. */
646 if (dexp >= SIGNIFICAND_BITS)
653 inexact |= sticky_rshift_significand (&t, b, dexp);
659 if (sub_significands (r, a, b, inexact))
661 /* We got a borrow out of the subtraction. That means that
662 A and B had the same exponent, and B had the larger
663 significand. We need to swap the sign and negate the
666 neg_significand (r, r);
671 if (add_significands (r, a, b))
673 /* We got carry out of the addition. This means we need to
674 shift the significand back down one bit and increase the
676 inexact |= sticky_rshift_significand (r, r, 1);
677 r->sig[SIGSZ-1] |= SIG_MSB;
686 r->class = rvc_normal;
690 /* Re-normalize the result. */
693 /* Special case: if the subtraction results in zero, the result
695 if (r->class == rvc_zero)
698 r->sig[0] |= inexact;
701 /* Return R = A * B. */
704 do_multiply (r, a, b)
706 const REAL_VALUE_TYPE *a, *b;
708 REAL_VALUE_TYPE u, t, *rr;
709 unsigned int i, j, k;
710 int sign = a->sign ^ b->sign;
712 switch (CLASS2 (a->class, b->class))
714 case CLASS2 (rvc_zero, rvc_zero):
715 case CLASS2 (rvc_zero, rvc_normal):
716 case CLASS2 (rvc_normal, rvc_zero):
717 /* +-0 * ANY = 0 with appropriate sign. */
721 case CLASS2 (rvc_zero, rvc_nan):
722 case CLASS2 (rvc_normal, rvc_nan):
723 case CLASS2 (rvc_inf, rvc_nan):
724 case CLASS2 (rvc_nan, rvc_nan):
725 /* ANY * NaN = NaN. */
730 case CLASS2 (rvc_nan, rvc_zero):
731 case CLASS2 (rvc_nan, rvc_normal):
732 case CLASS2 (rvc_nan, rvc_inf):
733 /* NaN * ANY = NaN. */
738 case CLASS2 (rvc_zero, rvc_inf):
739 case CLASS2 (rvc_inf, rvc_zero):
741 get_canonical_qnan (r, sign);
744 case CLASS2 (rvc_inf, rvc_inf):
745 case CLASS2 (rvc_normal, rvc_inf):
746 case CLASS2 (rvc_inf, rvc_normal):
747 /* Inf * Inf = Inf, R * Inf = Inf */
752 case CLASS2 (rvc_normal, rvc_normal):
759 if (r == a || r == b)
765 /* Collect all the partial products. Since we don't have sure access
766 to a widening multiply, we split each long into two half-words.
768 Consider the long-hand form of a four half-word multiplication:
778 We construct partial products of the widened half-word products
779 that are known to not overlap, e.g. DF+DH. Each such partial
780 product is given its proper exponent, which allows us to sum them
781 and obtain the finished product. */
783 for (i = 0; i < SIGSZ * 2; ++i)
785 unsigned long ai = a->sig[i / 2];
787 ai >>= HOST_BITS_PER_LONG / 2;
789 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
794 for (j = 0; j < 2; ++j)
796 int exp = (a->exp - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
797 + (b->exp - (1-j)*(HOST_BITS_PER_LONG/2)));
802 /* Would underflow to zero, which we shouldn't bother adding. */
805 u.class = rvc_normal;
809 for (k = j; k < SIGSZ * 2; k += 2)
811 unsigned long bi = b->sig[k / 2];
813 bi >>= HOST_BITS_PER_LONG / 2;
815 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
817 u.sig[k / 2] = ai * bi;
821 do_add (rr, rr, &u, 0);
830 /* Return R = A / B. */
835 const REAL_VALUE_TYPE *a, *b;
837 int exp, sign = a->sign ^ b->sign;
838 REAL_VALUE_TYPE t, *rr;
841 switch (CLASS2 (a->class, b->class))
843 case CLASS2 (rvc_zero, rvc_zero):
845 case CLASS2 (rvc_inf, rvc_inf):
846 /* Inf / Inf = NaN. */
847 get_canonical_qnan (r, sign);
850 case CLASS2 (rvc_zero, rvc_normal):
851 case CLASS2 (rvc_zero, rvc_inf):
853 case CLASS2 (rvc_normal, rvc_inf):
859 case CLASS2 (rvc_normal, rvc_zero):
861 case CLASS2 (rvc_inf, rvc_zero):
866 case CLASS2 (rvc_zero, rvc_nan):
867 case CLASS2 (rvc_normal, rvc_nan):
868 case CLASS2 (rvc_inf, rvc_nan):
869 case CLASS2 (rvc_nan, rvc_nan):
870 /* ANY / NaN = NaN. */
875 case CLASS2 (rvc_nan, rvc_zero):
876 case CLASS2 (rvc_nan, rvc_normal):
877 case CLASS2 (rvc_nan, rvc_inf):
878 /* NaN / ANY = NaN. */
883 case CLASS2 (rvc_inf, rvc_normal):
889 case CLASS2 (rvc_normal, rvc_normal):
896 if (r == a || r == b)
901 rr->class = rvc_normal;
904 exp = a->exp - b->exp + 1;
911 inexact = div_significands (rr, a, b);
913 /* Re-normalize the result. */
915 rr->sig[0] |= inexact;
921 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
922 one of the two operands is a NaN. */
925 do_compare (a, b, nan_result)
926 const REAL_VALUE_TYPE *a, *b;
931 switch (CLASS2 (a->class, b->class))
933 case CLASS2 (rvc_zero, rvc_zero):
934 /* Sign of zero doesn't matter for compares. */
937 case CLASS2 (rvc_inf, rvc_zero):
938 case CLASS2 (rvc_inf, rvc_normal):
939 case CLASS2 (rvc_normal, rvc_zero):
940 return (a->sign ? -1 : 1);
942 case CLASS2 (rvc_inf, rvc_inf):
943 return -a->sign - -b->sign;
945 case CLASS2 (rvc_zero, rvc_normal):
946 case CLASS2 (rvc_zero, rvc_inf):
947 case CLASS2 (rvc_normal, rvc_inf):
948 return (b->sign ? 1 : -1);
950 case CLASS2 (rvc_zero, rvc_nan):
951 case CLASS2 (rvc_normal, rvc_nan):
952 case CLASS2 (rvc_inf, rvc_nan):
953 case CLASS2 (rvc_nan, rvc_nan):
954 case CLASS2 (rvc_nan, rvc_zero):
955 case CLASS2 (rvc_nan, rvc_normal):
956 case CLASS2 (rvc_nan, rvc_inf):
959 case CLASS2 (rvc_normal, rvc_normal):
966 if (a->sign != b->sign)
967 return -a->sign - -b->sign;
971 else if (a->exp < b->exp)
974 ret = cmp_significands (a, b);
976 return (a->sign ? -ret : ret);
979 /* Return A truncated to an integral value toward zero. */
984 const REAL_VALUE_TYPE *a;
997 get_zero (r, r->sign);
998 else if (r->exp < SIGNIFICAND_BITS)
999 clear_significand_below (r, SIGNIFICAND_BITS - r->exp);
1007 /* Perform the binary or unary operation described by CODE.
1008 For a unary operation, leave OP1 NULL. */
1011 real_arithmetic (r, icode, op0, op1)
1014 const REAL_VALUE_TYPE *op0, *op1;
1016 enum tree_code code = icode;
1021 do_add (r, op0, op1, 0);
1025 do_add (r, op0, op1, 1);
1029 do_multiply (r, op0, op1);
1033 do_divide (r, op0, op1);
1037 if (op1->class == rvc_nan)
1039 else if (do_compare (op0, op1, -1) < 0)
1046 if (op1->class == rvc_nan)
1048 else if (do_compare (op0, op1, 1) < 0)
1064 case FIX_TRUNC_EXPR:
1065 do_fix_trunc (r, op0);
1073 /* Legacy. Similar, but return the result directly. */
1076 real_arithmetic2 (icode, op0, op1)
1078 const REAL_VALUE_TYPE *op0, *op1;
1081 real_arithmetic (&r, icode, op0, op1);
1086 real_compare (icode, op0, op1)
1088 const REAL_VALUE_TYPE *op0, *op1;
1090 enum tree_code code = icode;
1095 return do_compare (op0, op1, 1) < 0;
1097 return do_compare (op0, op1, 1) <= 0;
1099 return do_compare (op0, op1, -1) > 0;
1101 return do_compare (op0, op1, -1) >= 0;
1103 return do_compare (op0, op1, -1) == 0;
1105 return do_compare (op0, op1, -1) != 0;
1106 case UNORDERED_EXPR:
1107 return op0->class == rvc_nan || op1->class == rvc_nan;
1109 return op0->class != rvc_nan && op1->class != rvc_nan;
1111 return do_compare (op0, op1, -1) < 0;
1113 return do_compare (op0, op1, -1) <= 0;
1115 return do_compare (op0, op1, 1) > 0;
1117 return do_compare (op0, op1, 1) >= 0;
1119 return do_compare (op0, op1, 0) == 0;
1126 /* Return floor log2(R). */
1130 const REAL_VALUE_TYPE *r;
1138 return (unsigned int)-1 >> 1;
1146 /* R = OP0 * 2**EXP. */
1149 real_ldexp (r, op0, exp)
1151 const REAL_VALUE_TYPE *op0;
1165 get_inf (r, r->sign);
1166 else if (exp < -MAX_EXP)
1167 get_zero (r, r->sign);
1177 /* Determine whether a floating-point value X is infinite. */
1181 const REAL_VALUE_TYPE *r;
1183 return (r->class == rvc_inf);
1186 /* Determine whether a floating-point value X is a NaN. */
1190 const REAL_VALUE_TYPE *r;
1192 return (r->class == rvc_nan);
1195 /* Determine whether a floating-point value X is negative. */
1199 const REAL_VALUE_TYPE *r;
1204 /* Determine whether a floating-point value X is minus zero. */
1208 const REAL_VALUE_TYPE *r;
1210 return r->sign && r->class == rvc_zero;
1213 /* Compare two floating-point objects for bitwise identity. */
1216 real_identical (a, b)
1217 const REAL_VALUE_TYPE *a, *b;
1221 if (a->class != b->class)
1223 if (a->sign != b->sign)
1233 if (a->exp != b->exp)
1237 if (a->signalling != b->signalling)
1239 for (i = 0; i < SIGSZ; ++i)
1240 if (a->sig[i] != b->sig[i])
1251 /* Try to change R into its exact multiplicative inverse in machine
1252 mode MODE. Return true if successful. */
1255 exact_real_inverse (mode, r)
1256 enum machine_mode mode;
1259 const REAL_VALUE_TYPE *one = real_digit (1);
1263 if (r->class != rvc_normal)
1266 /* Check for a power of two: all significand bits zero except the MSB. */
1267 for (i = 0; i < SIGSZ-1; ++i)
1270 if (r->sig[SIGSZ-1] != SIG_MSB)
1273 /* Find the inverse and truncate to the required mode. */
1274 do_divide (&u, one, r);
1275 real_convert (&u, mode, &u);
1277 /* The rounding may have overflowed. */
1278 if (u.class != rvc_normal)
1280 for (i = 0; i < SIGSZ-1; ++i)
1283 if (u.sig[SIGSZ-1] != SIG_MSB)
1290 /* Render R as an integer. */
1294 const REAL_VALUE_TYPE *r;
1296 unsigned HOST_WIDE_INT i;
1307 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1315 /* Only force overflow for unsigned overflow. Signed overflow is
1316 undefined, so it doesn't matter what we return, and some callers
1317 expect to be able to use this routine for both signed and
1318 unsigned conversions. */
1319 if (r->exp > HOST_BITS_PER_WIDE_INT)
1322 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1323 i = r->sig[SIGSZ-1];
1324 else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1326 i = r->sig[SIGSZ-1];
1327 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1328 i |= r->sig[SIGSZ-2];
1333 i >>= HOST_BITS_PER_WIDE_INT - r->exp;
1344 /* Likewise, but to an integer pair, HI+LOW. */
1347 real_to_integer2 (plow, phigh, r)
1348 HOST_WIDE_INT *plow, *phigh;
1349 const REAL_VALUE_TYPE *r;
1352 HOST_WIDE_INT low, high;
1365 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1379 /* Only force overflow for unsigned overflow. Signed overflow is
1380 undefined, so it doesn't matter what we return, and some callers
1381 expect to be able to use this routine for both signed and
1382 unsigned conversions. */
1383 if (exp > 2*HOST_BITS_PER_WIDE_INT)
1386 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1387 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1389 high = t.sig[SIGSZ-1];
1390 low = t.sig[SIGSZ-2];
1392 else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1394 high = t.sig[SIGSZ-1];
1395 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1396 high |= t.sig[SIGSZ-2];
1398 low = t.sig[SIGSZ-3];
1399 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1400 low |= t.sig[SIGSZ-4];
1410 low = -low, high = ~high;
1422 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1423 of NUM / DEN. Return the quotient and place the remainder in NUM.
1424 It is expected that NUM / DEN are close enough that the quotient is
1427 static unsigned long
1428 rtd_divmod (num, den)
1429 REAL_VALUE_TYPE *num, *den;
1431 unsigned long q, msb;
1432 int expn = num->exp, expd = den->exp;
1441 msb = num->sig[SIGSZ-1] & SIG_MSB;
1443 lshift_significand_1 (num, num);
1445 if (msb || cmp_significands (num, den) >= 0)
1447 sub_significands (num, num, den, 0);
1451 while (--expn >= expd);
1459 /* Render R as a decimal floating point constant. Emit DIGITS significant
1460 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1461 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1464 #define M_LOG10_2 0.30102999566398119521
1467 real_to_decimal (str, r_orig, buf_size, digits, crop_trailing_zeros)
1469 const REAL_VALUE_TYPE *r_orig;
1470 size_t buf_size, digits;
1471 int crop_trailing_zeros;
1473 const REAL_VALUE_TYPE *one, *ten;
1474 REAL_VALUE_TYPE r, pten, u, v;
1475 int dec_exp, cmp_one, digit;
1477 char *p, *first, *last;
1484 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1489 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1492 /* ??? Print the significand as well, if not canonical? */
1493 strcpy (str, (r.sign ? "-NaN" : "+NaN"));
1499 /* Bound the number of digits printed by the size of the representation. */
1500 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1501 if (digits == 0 || digits > max_digits)
1502 digits = max_digits;
1504 /* Estimate the decimal exponent, and compute the length of the string it
1505 will print as. Be conservative and add one to account for possible
1506 overflow or rounding error. */
1507 dec_exp = r.exp * M_LOG10_2;
1508 for (max_digits = 1; dec_exp ; max_digits++)
1511 /* Bound the number of digits printed by the size of the output buffer. */
1512 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1513 if (max_digits > buf_size)
1515 if (digits > max_digits)
1516 digits = max_digits;
1518 one = real_digit (1);
1519 ten = ten_to_ptwo (0);
1527 cmp_one = do_compare (&r, one, 0);
1532 /* Number is greater than one. Convert significand to an integer
1533 and strip trailing decimal zeros. */
1536 u.exp = SIGNIFICAND_BITS - 1;
1538 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1539 m = floor_log2 (max_digits);
1541 /* Iterate over the bits of the possible powers of 10 that might
1542 be present in U and eliminate them. That is, if we find that
1543 10**2**M divides U evenly, keep the division and increase
1549 do_divide (&t, &u, ten_to_ptwo (m));
1550 do_fix_trunc (&v, &t);
1551 if (cmp_significands (&v, &t) == 0)
1559 /* Revert the scaling to integer that we performed earlier. */
1560 u.exp += r.exp - (SIGNIFICAND_BITS - 1);
1563 /* Find power of 10. Do this by dividing out 10**2**M when
1564 this is larger than the current remainder. Fill PTEN with
1565 the power of 10 that we compute. */
1568 m = floor_log2 ((int)(r.exp * M_LOG10_2)) + 1;
1571 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1572 if (do_compare (&u, ptentwo, 0) >= 0)
1574 do_divide (&u, &u, ptentwo);
1575 do_multiply (&pten, &pten, ptentwo);
1582 /* We managed to divide off enough tens in the above reduction
1583 loop that we've now got a negative exponent. Fall into the
1584 less-than-one code to compute the proper value for PTEN. */
1591 /* Number is less than one. Pad significand with leading
1597 /* Stop if we'd shift bits off the bottom. */
1601 do_multiply (&u, &v, ten);
1603 /* Stop if we're now >= 1. */
1612 /* Find power of 10. Do this by multiplying in P=10**2**M when
1613 the current remainder is smaller than 1/P. Fill PTEN with the
1614 power of 10 that we compute. */
1615 m = floor_log2 ((int)(-r.exp * M_LOG10_2)) + 1;
1618 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1619 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1621 if (do_compare (&v, ptenmtwo, 0) <= 0)
1623 do_multiply (&v, &v, ptentwo);
1624 do_multiply (&pten, &pten, ptentwo);
1630 /* Invert the positive power of 10 that we've collected so far. */
1631 do_divide (&pten, one, &pten);
1639 /* At this point, PTEN should contain the nearest power of 10 smaller
1640 than R, such that this division produces the first digit.
1642 Using a divide-step primitive that returns the complete integral
1643 remainder avoids the rounding error that would be produced if
1644 we were to use do_divide here and then simply multiply by 10 for
1645 each subsequent digit. */
1647 digit = rtd_divmod (&r, &pten);
1649 /* Be prepared for error in that division via underflow ... */
1650 if (digit == 0 && cmp_significand_0 (&r))
1652 /* Multiply by 10 and try again. */
1653 do_multiply (&r, &r, ten);
1654 digit = rtd_divmod (&r, &pten);
1660 /* ... or overflow. */
1668 else if (digit > 10)
1673 /* Generate subsequent digits. */
1674 while (--digits > 0)
1676 do_multiply (&r, &r, ten);
1677 digit = rtd_divmod (&r, &pten);
1682 /* Generate one more digit with which to do rounding. */
1683 do_multiply (&r, &r, ten);
1684 digit = rtd_divmod (&r, &pten);
1686 /* Round the result. */
1689 /* Round to nearest. If R is nonzero there are additional
1690 nonzero digits to be extracted. */
1691 if (cmp_significand_0 (&r))
1693 /* Round to even. */
1694 else if ((p[-1] - '0') & 1)
1711 /* Carry out of the first digit. This means we had all 9's and
1712 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1720 /* Insert the decimal point. */
1721 first[0] = first[1];
1724 /* If requested, drop trailing zeros. Never crop past "1.0". */
1725 if (crop_trailing_zeros)
1726 while (last > first + 3 && last[-1] == '0')
1729 /* Append the exponent. */
1730 sprintf (last, "e%+d", dec_exp);
1733 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1734 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1735 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1736 strip trailing zeros. */
1739 real_to_hexadecimal (str, r, buf_size, digits, crop_trailing_zeros)
1741 const REAL_VALUE_TYPE *r;
1742 size_t buf_size, digits;
1743 int crop_trailing_zeros;
1745 int i, j, exp = r->exp;
1758 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1761 /* ??? Print the significand as well, if not canonical? */
1762 strcpy (str, (r->sign ? "-NaN" : "+NaN"));
1769 digits = SIGNIFICAND_BITS / 4;
1771 /* Bound the number of digits printed by the size of the output buffer. */
1773 sprintf (exp_buf, "p%+d", exp);
1774 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1775 if (max_digits > buf_size)
1777 if (digits > max_digits)
1778 digits = max_digits;
1789 for (i = SIGSZ - 1; i >= 0; --i)
1790 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1792 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1798 if (crop_trailing_zeros)
1799 while (p > first + 1 && p[-1] == '0')
1802 sprintf (p, "p%+d", exp);
1805 /* Initialize R from a decimal or hexadecimal string. The string is
1806 assumed to have been syntax checked already. */
1809 real_from_string (r, str)
1823 else if (*str == '+')
1826 if (str[0] == '0' && str[1] == 'x')
1828 /* Hexadecimal floating point. */
1829 int pos = SIGNIFICAND_BITS - 4, d;
1837 d = hex_value (*str);
1842 r->sig[pos / HOST_BITS_PER_LONG]
1843 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1852 if (pos == SIGNIFICAND_BITS - 4)
1859 d = hex_value (*str);
1864 r->sig[pos / HOST_BITS_PER_LONG]
1865 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1871 if (*str == 'p' || *str == 'P')
1873 bool exp_neg = false;
1881 else if (*str == '+')
1885 while (ISDIGIT (*str))
1891 /* Overflowed the exponent. */
1905 r->class = rvc_normal;
1912 /* Decimal floating point. */
1913 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1918 while (ISDIGIT (*str))
1921 do_multiply (r, r, ten);
1923 do_add (r, r, real_digit (d), 0);
1928 if (r->class == rvc_zero)
1933 while (ISDIGIT (*str))
1936 do_multiply (r, r, ten);
1938 do_add (r, r, real_digit (d), 0);
1943 if (*str == 'e' || *str == 'E')
1945 bool exp_neg = false;
1953 else if (*str == '+')
1957 while (ISDIGIT (*str))
1963 /* Overflowed the exponent. */
1977 times_pten (r, exp);
1992 /* Legacy. Similar, but return the result directly. */
1995 real_from_string2 (s, mode)
1997 enum machine_mode mode;
2001 real_from_string (&r, s);
2002 if (mode != VOIDmode)
2003 real_convert (&r, mode, &r);
2008 /* Initialize R from the integer pair HIGH+LOW. */
2011 real_from_integer (r, mode, low, high, unsigned_p)
2013 enum machine_mode mode;
2014 unsigned HOST_WIDE_INT low;
2018 if (low == 0 && high == 0)
2022 r->class = rvc_normal;
2023 r->sign = high < 0 && !unsigned_p;
2024 r->exp = 2 * HOST_BITS_PER_WIDE_INT;
2035 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2037 r->sig[SIGSZ-1] = high;
2038 r->sig[SIGSZ-2] = low;
2039 memset (r->sig, 0, sizeof(long)*(SIGSZ-2));
2041 else if (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT)
2043 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2044 r->sig[SIGSZ-2] = high;
2045 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2046 r->sig[SIGSZ-4] = low;
2048 memset (r->sig, 0, sizeof(long)*(SIGSZ-4));
2056 if (mode != VOIDmode)
2057 real_convert (r, mode, r);
2060 /* Returns 10**2**N. */
2062 static const REAL_VALUE_TYPE *
2066 static REAL_VALUE_TYPE tens[EXP_BITS];
2068 if (n < 0 || n >= EXP_BITS)
2071 if (tens[n].class == rvc_zero)
2073 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2075 HOST_WIDE_INT t = 10;
2078 for (i = 0; i < n; ++i)
2081 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2085 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2086 do_multiply (&tens[n], t, t);
2093 /* Returns 10**(-2**N). */
2095 static const REAL_VALUE_TYPE *
2099 static REAL_VALUE_TYPE tens[EXP_BITS];
2101 if (n < 0 || n >= EXP_BITS)
2104 if (tens[n].class == rvc_zero)
2105 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2112 static const REAL_VALUE_TYPE *
2116 static REAL_VALUE_TYPE num[10];
2121 if (n > 0 && num[n].class == rvc_zero)
2122 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2127 /* Multiply R by 10**EXP. */
2134 REAL_VALUE_TYPE pten, *rr;
2135 bool negative = (exp < 0);
2141 pten = *real_digit (1);
2147 for (i = 0; exp > 0; ++i, exp >>= 1)
2149 do_multiply (rr, rr, ten_to_ptwo (i));
2152 do_divide (r, r, &pten);
2155 /* Fills R with +Inf. */
2164 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2165 we force a QNaN, else we force an SNaN. The string, if not empty,
2166 is parsed as a number and placed in the significand. Return true
2167 if the string was successfully parsed. */
2170 real_nan (r, str, quiet, mode)
2174 enum machine_mode mode;
2176 const struct real_format *fmt;
2178 fmt = real_format_for_mode[mode - QFmode];
2185 get_canonical_qnan (r, 0);
2187 get_canonical_snan (r, 0);
2194 memset (r, 0, sizeof (*r));
2197 /* Parse akin to strtol into the significand of R. */
2199 while (ISSPACE (*str))
2203 else if (*str == '+')
2213 while ((d = hex_value (*str)) < base)
2220 lshift_significand (r, r, 3);
2223 lshift_significand (r, r, 4);
2226 lshift_significand_1 (&u, r);
2227 lshift_significand (r, r, 3);
2228 add_significands (r, r, &u);
2236 add_significands (r, r, &u);
2241 /* Must have consumed the entire string for success. */
2245 /* Shift the significand into place such that the bits
2246 are in the most significant bits for the format. */
2247 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->p);
2249 /* Our MSB is always unset for NaNs. */
2250 r->sig[SIGSZ-1] &= ~SIG_MSB;
2252 /* Force quiet or signalling NaN. */
2253 r->signalling = !quiet;
2259 /* Fills R with 2**N. */
2266 memset (r, 0, sizeof (*r));
2271 else if (n < -MAX_EXP)
2275 r->class = rvc_normal;
2277 r->sig[SIGSZ-1] = SIG_MSB;
2283 round_for_format (fmt, r)
2284 const struct real_format *fmt;
2288 unsigned long sticky;
2292 p2 = fmt->p * fmt->log2_b;
2293 emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2294 emax2 = fmt->emax * fmt->log2_b;
2296 np2 = SIGNIFICAND_BITS - p2;
2300 get_zero (r, r->sign);
2302 if (!fmt->has_signed_zero)
2307 get_inf (r, r->sign);
2312 clear_significand_below (r, np2);
2322 /* If we're not base2, normalize the exponent to a multiple of
2324 if (fmt->log2_b != 1)
2326 int shift = r->exp & (fmt->log2_b - 1);
2329 shift = fmt->log2_b - shift;
2330 r->sig[0] |= sticky_rshift_significand (r, r, shift);
2335 /* Check the range of the exponent. If we're out of range,
2336 either underflow or overflow. */
2339 else if (r->exp <= emin2m1)
2343 if (!fmt->has_denorm)
2345 /* Don't underflow completely until we've had a chance to round. */
2346 if (r->exp < emin2m1)
2351 diff = emin2m1 - r->exp + 1;
2355 /* De-normalize the significand. */
2356 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2361 /* There are P2 true significand bits, followed by one guard bit,
2362 followed by one sticky bit, followed by stuff. Fold nonzero
2363 stuff into the sticky bit. */
2366 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2367 sticky |= r->sig[i];
2369 r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2371 guard = test_significand_bit (r, np2 - 1);
2372 lsb = test_significand_bit (r, np2);
2374 /* Round to even. */
2375 if (guard && (sticky || lsb))
2379 set_significand_bit (&u, np2);
2381 if (add_significands (r, r, &u))
2383 /* Overflow. Means the significand had been all ones, and
2384 is now all zeros. Need to increase the exponent, and
2385 possibly re-normalize it. */
2386 if (++r->exp > emax2)
2388 r->sig[SIGSZ-1] = SIG_MSB;
2390 if (fmt->log2_b != 1)
2392 int shift = r->exp & (fmt->log2_b - 1);
2395 shift = fmt->log2_b - shift;
2396 rshift_significand (r, r, shift);
2405 /* Catch underflow that we deferred until after rounding. */
2406 if (r->exp <= emin2m1)
2409 /* Clear out trailing garbage. */
2410 clear_significand_below (r, np2);
2413 /* Extend or truncate to a new mode. */
2416 real_convert (r, mode, a)
2418 enum machine_mode mode;
2419 const REAL_VALUE_TYPE *a;
2421 const struct real_format *fmt;
2423 fmt = real_format_for_mode[mode - QFmode];
2428 round_for_format (fmt, r);
2430 /* round_for_format de-normalizes denormals. Undo just that part. */
2431 if (r->class == rvc_normal)
2435 /* Legacy. Likewise, except return the struct directly. */
2438 real_value_truncate (mode, a)
2439 enum machine_mode mode;
2443 real_convert (&r, mode, &a);
2447 /* Return true if truncating to MODE is exact. */
2450 exact_real_truncate (mode, a)
2451 enum machine_mode mode;
2452 const REAL_VALUE_TYPE *a;
2455 real_convert (&t, mode, a);
2456 return real_identical (&t, a);
2459 /* Write R to the given target format. Place the words of the result
2460 in target word order in BUF. There are always 32 bits in each
2461 long, no matter the size of the host long.
2463 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2466 real_to_target_fmt (buf, r_orig, fmt)
2468 const REAL_VALUE_TYPE *r_orig;
2469 const struct real_format *fmt;
2475 round_for_format (fmt, &r);
2479 (*fmt->encode) (fmt, buf, &r);
2484 /* Similar, but look up the format from MODE. */
2487 real_to_target (buf, r, mode)
2489 const REAL_VALUE_TYPE *r;
2490 enum machine_mode mode;
2492 const struct real_format *fmt;
2494 fmt = real_format_for_mode[mode - QFmode];
2498 return real_to_target_fmt (buf, r, fmt);
2501 /* Read R from the given target format. Read the words of the result
2502 in target word order in BUF. There are always 32 bits in each
2503 long, no matter the size of the host long. */
2506 real_from_target_fmt (r, buf, fmt)
2509 const struct real_format *fmt;
2511 (*fmt->decode) (fmt, r, buf);
2514 /* Similar, but look up the format from MODE. */
2517 real_from_target (r, buf, mode)
2520 enum machine_mode mode;
2522 const struct real_format *fmt;
2524 fmt = real_format_for_mode[mode - QFmode];
2528 (*fmt->decode) (fmt, r, buf);
2531 /* Return the number of bits in the significand for MODE. */
2532 /* ??? Legacy. Should get access to real_format directly. */
2535 significand_size (mode)
2536 enum machine_mode mode;
2538 const struct real_format *fmt;
2540 fmt = real_format_for_mode[mode - QFmode];
2544 return fmt->p * fmt->log2_b;
2547 /* Return a hash value for the given real value. */
2548 /* ??? The "unsigned int" return value is intended to be hashval_t,
2549 but I didn't want to pull hashtab.h into real.h. */
2553 const REAL_VALUE_TYPE *r;
2558 h = r->class | (r->sign << 2);
2570 if (sizeof(unsigned long) > sizeof(unsigned int))
2571 for (i = 0; i < SIGSZ; ++i)
2573 unsigned long s = r->sig[i];
2574 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2577 for (i = 0; i < SIGSZ; ++i)
2588 /* IEEE single-precision format. */
2590 static void encode_ieee_single PARAMS ((const struct real_format *fmt,
2591 long *, const REAL_VALUE_TYPE *));
2592 static void decode_ieee_single PARAMS ((const struct real_format *,
2593 REAL_VALUE_TYPE *, const long *));
2596 encode_ieee_single (fmt, buf, r)
2597 const struct real_format *fmt;
2599 const REAL_VALUE_TYPE *r;
2601 unsigned long image, sig, exp;
2602 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2604 image = r->sign << 31;
2605 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2616 image |= 0x7fffffff;
2622 if (r->signalling == fmt->qnan_msb_set)
2633 image |= 0x7fffffff;
2637 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2638 whereas the intermediate representation is 0.F x 2**exp.
2639 Which means we're off by one. */
2643 exp = r->exp + 127 - 1;
2656 decode_ieee_single (fmt, r, buf)
2657 const struct real_format *fmt;
2661 unsigned long image = buf[0] & 0xffffffff;
2662 bool sign = (image >> 31) & 1;
2663 int exp = (image >> 23) & 0xff;
2665 memset (r, 0, sizeof (*r));
2666 image <<= HOST_BITS_PER_LONG - 24;
2671 if (image && fmt->has_denorm)
2673 r->class = rvc_normal;
2676 r->sig[SIGSZ-1] = image << 1;
2679 else if (fmt->has_signed_zero)
2682 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2688 r->signalling = ((image >> 22) & 1) ^ fmt->qnan_msb_set;
2689 r->sig[SIGSZ-1] = image;
2699 r->class = rvc_normal;
2701 r->exp = exp - 127 + 1;
2702 r->sig[SIGSZ-1] = image | SIG_MSB;
2706 const struct real_format ieee_single_format =
2724 /* IEEE double-precision format. */
2726 static void encode_ieee_double PARAMS ((const struct real_format *fmt,
2727 long *, const REAL_VALUE_TYPE *));
2728 static void decode_ieee_double PARAMS ((const struct real_format *,
2729 REAL_VALUE_TYPE *, const long *));
2732 encode_ieee_double (fmt, buf, r)
2733 const struct real_format *fmt;
2735 const REAL_VALUE_TYPE *r;
2737 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2738 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2740 image_hi = r->sign << 31;
2743 if (HOST_BITS_PER_LONG == 64)
2745 sig_hi = r->sig[SIGSZ-1];
2746 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2747 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2751 sig_hi = r->sig[SIGSZ-1];
2752 sig_lo = r->sig[SIGSZ-2];
2753 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2754 sig_hi = (sig_hi >> 11) & 0xfffff;
2764 image_hi |= 2047 << 20;
2767 image_hi |= 0x7fffffff;
2768 image_lo = 0xffffffff;
2775 if (r->signalling == fmt->qnan_msb_set)
2776 sig_hi &= ~(1 << 19);
2779 if (sig_hi == 0 && sig_lo == 0)
2782 image_hi |= 2047 << 20;
2788 image_hi |= 0x7fffffff;
2789 image_lo = 0xffffffff;
2794 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2795 whereas the intermediate representation is 0.F x 2**exp.
2796 Which means we're off by one. */
2800 exp = r->exp + 1023 - 1;
2801 image_hi |= exp << 20;
2810 if (FLOAT_WORDS_BIG_ENDIAN)
2811 buf[0] = image_hi, buf[1] = image_lo;
2813 buf[0] = image_lo, buf[1] = image_hi;
2817 decode_ieee_double (fmt, r, buf)
2818 const struct real_format *fmt;
2822 unsigned long image_hi, image_lo;
2826 if (FLOAT_WORDS_BIG_ENDIAN)
2827 image_hi = buf[0], image_lo = buf[1];
2829 image_lo = buf[0], image_hi = buf[1];
2830 image_lo &= 0xffffffff;
2831 image_hi &= 0xffffffff;
2833 sign = (image_hi >> 31) & 1;
2834 exp = (image_hi >> 20) & 0x7ff;
2836 memset (r, 0, sizeof (*r));
2838 image_hi <<= 32 - 21;
2839 image_hi |= image_lo >> 21;
2840 image_hi &= 0x7fffffff;
2841 image_lo <<= 32 - 21;
2845 if ((image_hi || image_lo) && fmt->has_denorm)
2847 r->class = rvc_normal;
2850 if (HOST_BITS_PER_LONG == 32)
2852 image_hi = (image_hi << 1) | (image_lo >> 31);
2854 r->sig[SIGSZ-1] = image_hi;
2855 r->sig[SIGSZ-2] = image_lo;
2859 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2860 r->sig[SIGSZ-1] = image_hi;
2864 else if (fmt->has_signed_zero)
2867 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2869 if (image_hi || image_lo)
2873 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
2874 if (HOST_BITS_PER_LONG == 32)
2876 r->sig[SIGSZ-1] = image_hi;
2877 r->sig[SIGSZ-2] = image_lo;
2880 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
2890 r->class = rvc_normal;
2892 r->exp = exp - 1023 + 1;
2893 if (HOST_BITS_PER_LONG == 32)
2895 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
2896 r->sig[SIGSZ-2] = image_lo;
2899 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2903 const struct real_format ieee_double_format =
2921 /* IEEE extended double precision format. This comes in three
2922 flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2925 static void encode_ieee_extended PARAMS ((const struct real_format *fmt,
2926 long *, const REAL_VALUE_TYPE *));
2927 static void decode_ieee_extended PARAMS ((const struct real_format *,
2928 REAL_VALUE_TYPE *, const long *));
2930 static void encode_ieee_extended_128 PARAMS ((const struct real_format *fmt,
2932 const REAL_VALUE_TYPE *));
2933 static void decode_ieee_extended_128 PARAMS ((const struct real_format *,
2938 encode_ieee_extended (fmt, buf, r)
2939 const struct real_format *fmt;
2941 const REAL_VALUE_TYPE *r;
2943 unsigned long image_hi, sig_hi, sig_lo;
2944 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2946 image_hi = r->sign << 15;
2947 sig_hi = sig_lo = 0;
2959 /* Intel requires the explicit integer bit to be set, otherwise
2960 it considers the value a "pseudo-infinity". Motorola docs
2961 say it doesn't care. */
2962 sig_hi = 0x80000000;
2967 sig_lo = sig_hi = 0xffffffff;
2975 if (HOST_BITS_PER_LONG == 32)
2977 sig_hi = r->sig[SIGSZ-1];
2978 sig_lo = r->sig[SIGSZ-2];
2982 sig_lo = r->sig[SIGSZ-1];
2983 sig_hi = sig_lo >> 31 >> 1;
2984 sig_lo &= 0xffffffff;
2986 if (r->signalling == fmt->qnan_msb_set)
2987 sig_hi &= ~(1 << 30);
2990 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
2993 /* Intel requires the explicit integer bit to be set, otherwise
2994 it considers the value a "pseudo-nan". Motorola docs say it
2996 sig_hi |= 0x80000000;
3001 sig_lo = sig_hi = 0xffffffff;
3009 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3010 whereas the intermediate representation is 0.F x 2**exp.
3011 Which means we're off by one.
3013 Except for Motorola, which consider exp=0 and explicit
3014 integer bit set to continue to be normalized. In theory
3015 this discrepancy has been taken care of by the difference
3016 in fmt->emin in round_for_format. */
3028 if (HOST_BITS_PER_LONG == 32)
3030 sig_hi = r->sig[SIGSZ-1];
3031 sig_lo = r->sig[SIGSZ-2];
3035 sig_lo = r->sig[SIGSZ-1];
3036 sig_hi = sig_lo >> 31 >> 1;
3037 sig_lo &= 0xffffffff;
3046 if (FLOAT_WORDS_BIG_ENDIAN)
3047 buf[0] = image_hi << 16, buf[1] = sig_hi, buf[2] = sig_lo;
3049 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3053 encode_ieee_extended_128 (fmt, buf, r)
3054 const struct real_format *fmt;
3056 const REAL_VALUE_TYPE *r;
3058 buf[3 * !FLOAT_WORDS_BIG_ENDIAN] = 0;
3059 encode_ieee_extended (fmt, buf+!!FLOAT_WORDS_BIG_ENDIAN, r);
3063 decode_ieee_extended (fmt, r, buf)
3064 const struct real_format *fmt;
3068 unsigned long image_hi, sig_hi, sig_lo;
3072 if (FLOAT_WORDS_BIG_ENDIAN)
3073 image_hi = buf[0] >> 16, sig_hi = buf[1], sig_lo = buf[2];
3075 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3076 sig_lo &= 0xffffffff;
3077 sig_hi &= 0xffffffff;
3078 image_hi &= 0xffffffff;
3080 sign = (image_hi >> 15) & 1;
3081 exp = image_hi & 0x7fff;
3083 memset (r, 0, sizeof (*r));
3087 if ((sig_hi || sig_lo) && fmt->has_denorm)
3089 r->class = rvc_normal;
3092 /* When the IEEE format contains a hidden bit, we know that
3093 it's zero at this point, and so shift up the significand
3094 and decrease the exponent to match. In this case, Motorola
3095 defines the explicit integer bit to be valid, so we don't
3096 know whether the msb is set or not. */
3098 if (HOST_BITS_PER_LONG == 32)
3100 r->sig[SIGSZ-1] = sig_hi;
3101 r->sig[SIGSZ-2] = sig_lo;
3104 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3108 else if (fmt->has_signed_zero)
3111 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3113 /* See above re "pseudo-infinities" and "pseudo-nans".
3114 Short summary is that the MSB will likely always be
3115 set, and that we don't care about it. */
3116 sig_hi &= 0x7fffffff;
3118 if (sig_hi || sig_lo)
3122 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3123 if (HOST_BITS_PER_LONG == 32)
3125 r->sig[SIGSZ-1] = sig_hi;
3126 r->sig[SIGSZ-2] = sig_lo;
3129 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3139 r->class = rvc_normal;
3141 r->exp = exp - 16383 + 1;
3142 if (HOST_BITS_PER_LONG == 32)
3144 r->sig[SIGSZ-1] = sig_hi;
3145 r->sig[SIGSZ-2] = sig_lo;
3148 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3153 decode_ieee_extended_128 (fmt, r, buf)
3154 const struct real_format *fmt;
3158 decode_ieee_extended (fmt, r, buf+!!FLOAT_WORDS_BIG_ENDIAN);
3161 const struct real_format ieee_extended_motorola_format =
3163 encode_ieee_extended,
3164 decode_ieee_extended,
3178 const struct real_format ieee_extended_intel_96_format =
3180 encode_ieee_extended,
3181 decode_ieee_extended,
3195 const struct real_format ieee_extended_intel_128_format =
3197 encode_ieee_extended_128,
3198 decode_ieee_extended_128,
3213 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3214 numbers whose sum is equal to the extended precision value. The number
3215 with greater magnitude is first. This format has the same magnitude
3216 range as an IEEE double precision value, but effectively 106 bits of
3217 significand precision. Infinity and NaN are represented by their IEEE
3218 double precision value stored in the first number, the second number is
3219 ignored. Zeroes, Infinities, and NaNs are set in both doubles
3220 due to precedent. */
3222 static void encode_ibm_extended PARAMS ((const struct real_format *fmt,
3223 long *, const REAL_VALUE_TYPE *));
3224 static void decode_ibm_extended PARAMS ((const struct real_format *,
3225 REAL_VALUE_TYPE *, const long *));
3228 encode_ibm_extended (fmt, buf, r)
3229 const struct real_format *fmt ATTRIBUTE_UNUSED;
3231 const REAL_VALUE_TYPE *r;
3233 REAL_VALUE_TYPE u, v;
3238 /* Both doubles have sign bit set. */
3239 buf[0] = FLOAT_WORDS_BIG_ENDIAN ? r->sign << 31 : 0;
3240 buf[1] = FLOAT_WORDS_BIG_ENDIAN ? 0 : r->sign << 31;
3247 /* Both doubles set to Inf / NaN. */
3248 encode_ieee_double (&ieee_double_format, &buf[0], r);
3254 /* u = IEEE double precision portion of significand. */
3256 clear_significand_below (&u, SIGNIFICAND_BITS - 53);
3259 /* If the upper double is zero, we have a denormal double, so
3260 move it to the first double and leave the second as zero. */
3261 if (u.class == rvc_zero)
3269 /* v = remainder containing additional 53 bits of significand. */
3270 do_add (&v, r, &u, 1);
3271 round_for_format (&ieee_double_format, &v);
3274 round_for_format (&ieee_double_format, &u);
3276 encode_ieee_double (&ieee_double_format, &buf[0], &u);
3277 encode_ieee_double (&ieee_double_format, &buf[2], &v);
3286 decode_ibm_extended (fmt, r, buf)
3287 const struct real_format *fmt ATTRIBUTE_UNUSED;
3291 REAL_VALUE_TYPE u, v;
3293 decode_ieee_double (&ieee_double_format, &u, &buf[0]);
3295 if (u.class != rvc_zero && u.class != rvc_inf && u.class != rvc_nan)
3297 decode_ieee_double (&ieee_double_format, &v, &buf[2]);
3298 do_add (r, &u, &v, 0);
3304 const struct real_format ibm_extended_format =
3306 encode_ibm_extended,
3307 decode_ibm_extended,
3322 /* IEEE quad precision format. */
3324 static void encode_ieee_quad PARAMS ((const struct real_format *fmt,
3325 long *, const REAL_VALUE_TYPE *));
3326 static void decode_ieee_quad PARAMS ((const struct real_format *,
3327 REAL_VALUE_TYPE *, const long *));
3330 encode_ieee_quad (fmt, buf, r)
3331 const struct real_format *fmt;
3333 const REAL_VALUE_TYPE *r;
3335 unsigned long image3, image2, image1, image0, exp;
3336 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3339 image3 = r->sign << 31;
3344 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3353 image3 |= 32767 << 16;
3356 image3 |= 0x7fffffff;
3357 image2 = 0xffffffff;
3358 image1 = 0xffffffff;
3359 image0 = 0xffffffff;
3366 image3 |= 32767 << 16;
3368 if (HOST_BITS_PER_LONG == 32)
3373 image3 |= u.sig[3] & 0xffff;
3378 image1 = image0 >> 31 >> 1;
3380 image3 |= (image2 >> 31 >> 1) & 0xffff;
3381 image0 &= 0xffffffff;
3382 image2 &= 0xffffffff;
3384 if (r->signalling == fmt->qnan_msb_set)
3388 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3393 image3 |= 0x7fffffff;
3394 image2 = 0xffffffff;
3395 image1 = 0xffffffff;
3396 image0 = 0xffffffff;
3401 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3402 whereas the intermediate representation is 0.F x 2**exp.
3403 Which means we're off by one. */
3407 exp = r->exp + 16383 - 1;
3408 image3 |= exp << 16;
3410 if (HOST_BITS_PER_LONG == 32)
3415 image3 |= u.sig[3] & 0xffff;
3420 image1 = image0 >> 31 >> 1;
3422 image3 |= (image2 >> 31 >> 1) & 0xffff;
3423 image0 &= 0xffffffff;
3424 image2 &= 0xffffffff;
3432 if (FLOAT_WORDS_BIG_ENDIAN)
3449 decode_ieee_quad (fmt, r, buf)
3450 const struct real_format *fmt;
3454 unsigned long image3, image2, image1, image0;
3458 if (FLOAT_WORDS_BIG_ENDIAN)
3472 image0 &= 0xffffffff;
3473 image1 &= 0xffffffff;
3474 image2 &= 0xffffffff;
3476 sign = (image3 >> 31) & 1;
3477 exp = (image3 >> 16) & 0x7fff;
3480 memset (r, 0, sizeof (*r));
3484 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3486 r->class = rvc_normal;
3489 r->exp = -16382 + (SIGNIFICAND_BITS - 112);
3490 if (HOST_BITS_PER_LONG == 32)
3499 r->sig[0] = (image1 << 31 << 1) | image0;
3500 r->sig[1] = (image3 << 31 << 1) | image2;
3505 else if (fmt->has_signed_zero)
3508 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3510 if (image3 | image2 | image1 | image0)
3514 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
3516 if (HOST_BITS_PER_LONG == 32)
3525 r->sig[0] = (image1 << 31 << 1) | image0;
3526 r->sig[1] = (image3 << 31 << 1) | image2;
3528 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3538 r->class = rvc_normal;
3540 r->exp = exp - 16383 + 1;
3542 if (HOST_BITS_PER_LONG == 32)
3551 r->sig[0] = (image1 << 31 << 1) | image0;
3552 r->sig[1] = (image3 << 31 << 1) | image2;
3554 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3555 r->sig[SIGSZ-1] |= SIG_MSB;
3559 const struct real_format ieee_quad_format =
3576 /* Descriptions of VAX floating point formats can be found beginning at
3578 http://www.openvms.compaq.com:8000/73final/4515/4515pro_013.html#f_floating_point_format
3580 The thing to remember is that they're almost IEEE, except for word
3581 order, exponent bias, and the lack of infinities, nans, and denormals.
3583 We don't implement the H_floating format here, simply because neither
3584 the VAX or Alpha ports use it. */
3586 static void encode_vax_f PARAMS ((const struct real_format *fmt,
3587 long *, const REAL_VALUE_TYPE *));
3588 static void decode_vax_f PARAMS ((const struct real_format *,
3589 REAL_VALUE_TYPE *, const long *));
3590 static void encode_vax_d PARAMS ((const struct real_format *fmt,
3591 long *, const REAL_VALUE_TYPE *));
3592 static void decode_vax_d PARAMS ((const struct real_format *,
3593 REAL_VALUE_TYPE *, const long *));
3594 static void encode_vax_g PARAMS ((const struct real_format *fmt,
3595 long *, const REAL_VALUE_TYPE *));
3596 static void decode_vax_g PARAMS ((const struct real_format *,
3597 REAL_VALUE_TYPE *, const long *));
3600 encode_vax_f (fmt, buf, r)
3601 const struct real_format *fmt ATTRIBUTE_UNUSED;
3603 const REAL_VALUE_TYPE *r;
3605 unsigned long sign, exp, sig, image;
3607 sign = r->sign << 15;
3617 image = 0xffff7fff | sign;
3621 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3624 image = (sig << 16) & 0xffff0000;
3638 decode_vax_f (fmt, r, buf)
3639 const struct real_format *fmt ATTRIBUTE_UNUSED;
3643 unsigned long image = buf[0] & 0xffffffff;
3644 int exp = (image >> 7) & 0xff;
3646 memset (r, 0, sizeof (*r));
3650 r->class = rvc_normal;
3651 r->sign = (image >> 15) & 1;
3654 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3655 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3660 encode_vax_d (fmt, buf, r)
3661 const struct real_format *fmt ATTRIBUTE_UNUSED;
3663 const REAL_VALUE_TYPE *r;
3665 unsigned long image0, image1, sign = r->sign << 15;
3670 image0 = image1 = 0;
3675 image0 = 0xffff7fff | sign;
3676 image1 = 0xffffffff;
3680 /* Extract the significand into straight hi:lo. */
3681 if (HOST_BITS_PER_LONG == 64)
3683 image0 = r->sig[SIGSZ-1];
3684 image1 = (image0 >> (64 - 56)) & 0xffffffff;
3685 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3689 image0 = r->sig[SIGSZ-1];
3690 image1 = r->sig[SIGSZ-2];
3691 image1 = (image0 << 24) | (image1 >> 8);
3692 image0 = (image0 >> 8) & 0xffffff;
3695 /* Rearrange the half-words of the significand to match the
3697 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3698 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3700 /* Add the sign and exponent. */
3702 image0 |= (r->exp + 128) << 7;
3709 if (FLOAT_WORDS_BIG_ENDIAN)
3710 buf[0] = image1, buf[1] = image0;
3712 buf[0] = image0, buf[1] = image1;
3716 decode_vax_d (fmt, r, buf)
3717 const struct real_format *fmt ATTRIBUTE_UNUSED;
3721 unsigned long image0, image1;
3724 if (FLOAT_WORDS_BIG_ENDIAN)
3725 image1 = buf[0], image0 = buf[1];
3727 image0 = buf[0], image1 = buf[1];
3728 image0 &= 0xffffffff;
3729 image1 &= 0xffffffff;
3731 exp = (image0 >> 7) & 0x7f;
3733 memset (r, 0, sizeof (*r));
3737 r->class = rvc_normal;
3738 r->sign = (image0 >> 15) & 1;
3741 /* Rearrange the half-words of the external format into
3742 proper ascending order. */
3743 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
3744 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3746 if (HOST_BITS_PER_LONG == 64)
3748 image0 = (image0 << 31 << 1) | image1;
3751 r->sig[SIGSZ-1] = image0;
3755 r->sig[SIGSZ-1] = image0;
3756 r->sig[SIGSZ-2] = image1;
3757 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
3758 r->sig[SIGSZ-1] |= SIG_MSB;
3764 encode_vax_g (fmt, buf, r)
3765 const struct real_format *fmt ATTRIBUTE_UNUSED;
3767 const REAL_VALUE_TYPE *r;
3769 unsigned long image0, image1, sign = r->sign << 15;
3774 image0 = image1 = 0;
3779 image0 = 0xffff7fff | sign;
3780 image1 = 0xffffffff;
3784 /* Extract the significand into straight hi:lo. */
3785 if (HOST_BITS_PER_LONG == 64)
3787 image0 = r->sig[SIGSZ-1];
3788 image1 = (image0 >> (64 - 53)) & 0xffffffff;
3789 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
3793 image0 = r->sig[SIGSZ-1];
3794 image1 = r->sig[SIGSZ-2];
3795 image1 = (image0 << 21) | (image1 >> 11);
3796 image0 = (image0 >> 11) & 0xfffff;
3799 /* Rearrange the half-words of the significand to match the
3801 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
3802 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3804 /* Add the sign and exponent. */
3806 image0 |= (r->exp + 1024) << 4;
3813 if (FLOAT_WORDS_BIG_ENDIAN)
3814 buf[0] = image1, buf[1] = image0;
3816 buf[0] = image0, buf[1] = image1;
3820 decode_vax_g (fmt, r, buf)
3821 const struct real_format *fmt ATTRIBUTE_UNUSED;
3825 unsigned long image0, image1;
3828 if (FLOAT_WORDS_BIG_ENDIAN)
3829 image1 = buf[0], image0 = buf[1];
3831 image0 = buf[0], image1 = buf[1];
3832 image0 &= 0xffffffff;
3833 image1 &= 0xffffffff;
3835 exp = (image0 >> 4) & 0x7ff;
3837 memset (r, 0, sizeof (*r));
3841 r->class = rvc_normal;
3842 r->sign = (image0 >> 15) & 1;
3843 r->exp = exp - 1024;
3845 /* Rearrange the half-words of the external format into
3846 proper ascending order. */
3847 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
3848 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3850 if (HOST_BITS_PER_LONG == 64)
3852 image0 = (image0 << 31 << 1) | image1;
3855 r->sig[SIGSZ-1] = image0;
3859 r->sig[SIGSZ-1] = image0;
3860 r->sig[SIGSZ-2] = image1;
3861 lshift_significand (r, r, 64 - 53);
3862 r->sig[SIGSZ-1] |= SIG_MSB;
3867 const struct real_format vax_f_format =
3884 const struct real_format vax_d_format =
3901 const struct real_format vax_g_format =
3918 /* A good reference for these can be found in chapter 9 of
3919 "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
3920 An on-line version can be found here:
3922 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3925 static void encode_i370_single PARAMS ((const struct real_format *fmt,
3926 long *, const REAL_VALUE_TYPE *));
3927 static void decode_i370_single PARAMS ((const struct real_format *,
3928 REAL_VALUE_TYPE *, const long *));
3929 static void encode_i370_double PARAMS ((const struct real_format *fmt,
3930 long *, const REAL_VALUE_TYPE *));
3931 static void decode_i370_double PARAMS ((const struct real_format *,
3932 REAL_VALUE_TYPE *, const long *));
3935 encode_i370_single (fmt, buf, r)
3936 const struct real_format *fmt ATTRIBUTE_UNUSED;
3938 const REAL_VALUE_TYPE *r;
3940 unsigned long sign, exp, sig, image;
3942 sign = r->sign << 31;
3952 image = 0x7fffffff | sign;
3956 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
3957 exp = ((r->exp / 4) + 64) << 24;
3958 image = sign | exp | sig;
3969 decode_i370_single (fmt, r, buf)
3970 const struct real_format *fmt ATTRIBUTE_UNUSED;
3974 unsigned long sign, sig, image = buf[0];
3977 sign = (image >> 31) & 1;
3978 exp = (image >> 24) & 0x7f;
3979 sig = image & 0xffffff;
3981 memset (r, 0, sizeof (*r));
3985 r->class = rvc_normal;
3987 r->exp = (exp - 64) * 4;
3988 r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
3994 encode_i370_double (fmt, buf, r)
3995 const struct real_format *fmt ATTRIBUTE_UNUSED;
3997 const REAL_VALUE_TYPE *r;
3999 unsigned long sign, exp, image_hi, image_lo;
4001 sign = r->sign << 31;
4006 image_hi = image_lo = 0;
4011 image_hi = 0x7fffffff | sign;
4012 image_lo = 0xffffffff;
4016 if (HOST_BITS_PER_LONG == 64)
4018 image_hi = r->sig[SIGSZ-1];
4019 image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
4020 image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
4024 image_hi = r->sig[SIGSZ-1];
4025 image_lo = r->sig[SIGSZ-2];
4026 image_lo = (image_lo >> 8) | (image_hi << 24);
4030 exp = ((r->exp / 4) + 64) << 24;
4031 image_hi |= sign | exp;
4038 if (FLOAT_WORDS_BIG_ENDIAN)
4039 buf[0] = image_hi, buf[1] = image_lo;
4041 buf[0] = image_lo, buf[1] = image_hi;
4045 decode_i370_double (fmt, r, buf)
4046 const struct real_format *fmt ATTRIBUTE_UNUSED;
4050 unsigned long sign, image_hi, image_lo;
4053 if (FLOAT_WORDS_BIG_ENDIAN)
4054 image_hi = buf[0], image_lo = buf[1];
4056 image_lo = buf[0], image_hi = buf[1];
4058 sign = (image_hi >> 31) & 1;
4059 exp = (image_hi >> 24) & 0x7f;
4060 image_hi &= 0xffffff;
4061 image_lo &= 0xffffffff;
4063 memset (r, 0, sizeof (*r));
4065 if (exp || image_hi || image_lo)
4067 r->class = rvc_normal;
4069 r->exp = (exp - 64) * 4 + (SIGNIFICAND_BITS - 56);
4071 if (HOST_BITS_PER_LONG == 32)
4073 r->sig[0] = image_lo;
4074 r->sig[1] = image_hi;
4077 r->sig[0] = image_lo | (image_hi << 31 << 1);
4083 const struct real_format i370_single_format =
4095 false, /* ??? The encoding does allow for "unnormals". */
4096 false, /* ??? The encoding does allow for "unnormals". */
4100 const struct real_format i370_double_format =
4112 false, /* ??? The encoding does allow for "unnormals". */
4113 false, /* ??? The encoding does allow for "unnormals". */
4117 /* The "twos-complement" c4x format is officially defined as
4121 This is rather misleading. One must remember that F is signed.
4122 A better description would be
4124 x = -1**s * ((s + 1 + .f) * 2**e
4126 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4127 that's -1 * (1+1+(-.5)) == -1.5. I think.
4129 The constructions here are taken from Tables 5-1 and 5-2 of the
4130 TMS320C4x User's Guide wherein step-by-step instructions for
4131 conversion from IEEE are presented. That's close enough to our
4132 internal representation so as to make things easy.
4134 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
4136 static void encode_c4x_single PARAMS ((const struct real_format *fmt,
4137 long *, const REAL_VALUE_TYPE *));
4138 static void decode_c4x_single PARAMS ((const struct real_format *,
4139 REAL_VALUE_TYPE *, const long *));
4140 static void encode_c4x_extended PARAMS ((const struct real_format *fmt,
4141 long *, const REAL_VALUE_TYPE *));
4142 static void decode_c4x_extended PARAMS ((const struct real_format *,
4143 REAL_VALUE_TYPE *, const long *));
4146 encode_c4x_single (fmt, buf, r)
4147 const struct real_format *fmt ATTRIBUTE_UNUSED;
4149 const REAL_VALUE_TYPE *r;
4151 unsigned long image, exp, sig;
4163 sig = 0x800000 - r->sign;
4168 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4183 image = ((exp & 0xff) << 24) | (sig & 0xffffff);
4188 decode_c4x_single (fmt, r, buf)
4189 const struct real_format *fmt ATTRIBUTE_UNUSED;
4193 unsigned long image = buf[0];
4197 exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
4198 sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
4200 memset (r, 0, sizeof (*r));
4204 r->class = rvc_normal;
4206 sig = sf & 0x7fffff;
4215 sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4218 r->sig[SIGSZ-1] = sig;
4223 encode_c4x_extended (fmt, buf, r)
4224 const struct real_format *fmt ATTRIBUTE_UNUSED;
4226 const REAL_VALUE_TYPE *r;
4228 unsigned long exp, sig;
4240 sig = 0x80000000 - r->sign;
4246 sig = r->sig[SIGSZ-1];
4247 if (HOST_BITS_PER_LONG == 64)
4248 sig = sig >> 1 >> 31;
4265 exp = (exp & 0xff) << 24;
4268 if (FLOAT_WORDS_BIG_ENDIAN)
4269 buf[0] = exp, buf[1] = sig;
4271 buf[0] = sig, buf[0] = exp;
4275 decode_c4x_extended (fmt, r, buf)
4276 const struct real_format *fmt ATTRIBUTE_UNUSED;
4283 if (FLOAT_WORDS_BIG_ENDIAN)
4284 exp = buf[0], sf = buf[1];
4286 sf = buf[0], exp = buf[1];
4288 exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
4289 sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
4291 memset (r, 0, sizeof (*r));
4295 r->class = rvc_normal;
4297 sig = sf & 0x7fffffff;
4306 if (HOST_BITS_PER_LONG == 64)
4307 sig = sig << 1 << 31;
4311 r->sig[SIGSZ-1] = sig;
4315 const struct real_format c4x_single_format =
4332 const struct real_format c4x_extended_format =
4334 encode_c4x_extended,
4335 decode_c4x_extended,
4350 /* A synthetic "format" for internal arithmetic. It's the size of the
4351 internal significand minus the two bits needed for proper rounding.
4352 The encode and decode routines exist only to satisfy our paranoia
4355 static void encode_internal PARAMS ((const struct real_format *fmt,
4356 long *, const REAL_VALUE_TYPE *));
4357 static void decode_internal PARAMS ((const struct real_format *,
4358 REAL_VALUE_TYPE *, const long *));
4361 encode_internal (fmt, buf, r)
4362 const struct real_format *fmt ATTRIBUTE_UNUSED;
4364 const REAL_VALUE_TYPE *r;
4366 memcpy (buf, r, sizeof (*r));
4370 decode_internal (fmt, r, buf)
4371 const struct real_format *fmt ATTRIBUTE_UNUSED;
4375 memcpy (r, buf, sizeof (*r));
4378 const struct real_format real_internal_format =
4384 SIGNIFICAND_BITS - 2,
4395 /* Set up default mode to format mapping for IEEE. Everyone else has
4396 to set these values in OVERRIDE_OPTIONS. */
4398 const struct real_format *real_format_for_mode[TFmode - QFmode + 1] =
4403 &ieee_single_format, /* SFmode */
4404 &ieee_double_format, /* DFmode */
4406 /* We explicitly don't handle XFmode. There are two formats,
4407 pretty much equally common. Choose one in OVERRIDE_OPTIONS. */
4409 &ieee_quad_format /* TFmode */
4413 /* Calculate the square root of X in mode MODE, and store the result
4414 in R. Return TRUE if the operation does not raise an exception.
4415 For details see "High Precision Division and Square Root",
4416 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4417 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4420 real_sqrt (r, mode, x)
4422 enum machine_mode mode;
4423 const REAL_VALUE_TYPE *x;
4425 static REAL_VALUE_TYPE halfthree;
4426 static bool init = false;
4427 REAL_VALUE_TYPE h, t, i;
4430 /* sqrt(-0.0) is -0.0. */
4431 if (real_isnegzero (x))
4437 /* Negative arguments return NaN. */
4440 /* Mode is ignored for canonical NaN. */
4441 real_nan (r, "", 1, SFmode);
4445 /* Infinity and NaN return themselves. */
4446 if (real_isinf (x) || real_isnan (x))
4454 real_arithmetic (&halfthree, PLUS_EXPR, &dconst1, &dconsthalf);
4458 /* Initial guess for reciprocal sqrt, i. */
4459 exp = real_exponent (x);
4460 real_ldexp (&i, &dconst1, -exp/2);
4462 /* Newton's iteration for reciprocal sqrt, i. */
4463 for (iter = 0; iter < 16; iter++)
4465 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4466 real_arithmetic (&t, MULT_EXPR, x, &i);
4467 real_arithmetic (&h, MULT_EXPR, &t, &i);
4468 real_arithmetic (&t, MULT_EXPR, &h, &dconsthalf);
4469 real_arithmetic (&h, MINUS_EXPR, &halfthree, &t);
4470 real_arithmetic (&t, MULT_EXPR, &i, &h);
4472 /* Check for early convergence. */
4473 if (iter >= 6 && real_identical (&i, &t))
4476 /* ??? Unroll loop to avoid copying. */
4480 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4481 real_arithmetic (&t, MULT_EXPR, x, &i);
4482 real_arithmetic (&h, MULT_EXPR, &t, &i);
4483 real_arithmetic (&i, MINUS_EXPR, &dconst1, &h);
4484 real_arithmetic (&h, MULT_EXPR, &t, &i);
4485 real_arithmetic (&i, MULT_EXPR, &dconsthalf, &h);
4486 real_arithmetic (&h, PLUS_EXPR, &t, &i);
4488 /* ??? We need a Tuckerman test to get the last bit. */
4490 real_convert (r, mode, &h);