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)
1238 if (a->signalling != b->signalling)
1246 for (i = 0; i < SIGSZ; ++i)
1247 if (a->sig[i] != b->sig[i])
1253 /* Try to change R into its exact multiplicative inverse in machine
1254 mode MODE. Return true if successful. */
1257 exact_real_inverse (mode, r)
1258 enum machine_mode mode;
1261 const REAL_VALUE_TYPE *one = real_digit (1);
1265 if (r->class != rvc_normal)
1268 /* Check for a power of two: all significand bits zero except the MSB. */
1269 for (i = 0; i < SIGSZ-1; ++i)
1272 if (r->sig[SIGSZ-1] != SIG_MSB)
1275 /* Find the inverse and truncate to the required mode. */
1276 do_divide (&u, one, r);
1277 real_convert (&u, mode, &u);
1279 /* The rounding may have overflowed. */
1280 if (u.class != rvc_normal)
1282 for (i = 0; i < SIGSZ-1; ++i)
1285 if (u.sig[SIGSZ-1] != SIG_MSB)
1292 /* Render R as an integer. */
1296 const REAL_VALUE_TYPE *r;
1298 unsigned HOST_WIDE_INT i;
1309 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1317 /* Only force overflow for unsigned overflow. Signed overflow is
1318 undefined, so it doesn't matter what we return, and some callers
1319 expect to be able to use this routine for both signed and
1320 unsigned conversions. */
1321 if (r->exp > HOST_BITS_PER_WIDE_INT)
1324 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1325 i = r->sig[SIGSZ-1];
1326 else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1328 i = r->sig[SIGSZ-1];
1329 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1330 i |= r->sig[SIGSZ-2];
1335 i >>= HOST_BITS_PER_WIDE_INT - r->exp;
1346 /* Likewise, but to an integer pair, HI+LOW. */
1349 real_to_integer2 (plow, phigh, r)
1350 HOST_WIDE_INT *plow, *phigh;
1351 const REAL_VALUE_TYPE *r;
1354 HOST_WIDE_INT low, high;
1367 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1381 /* Only force overflow for unsigned overflow. Signed overflow is
1382 undefined, so it doesn't matter what we return, and some callers
1383 expect to be able to use this routine for both signed and
1384 unsigned conversions. */
1385 if (exp > 2*HOST_BITS_PER_WIDE_INT)
1388 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1389 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1391 high = t.sig[SIGSZ-1];
1392 low = t.sig[SIGSZ-2];
1394 else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1396 high = t.sig[SIGSZ-1];
1397 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1398 high |= t.sig[SIGSZ-2];
1400 low = t.sig[SIGSZ-3];
1401 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1402 low |= t.sig[SIGSZ-4];
1412 low = -low, high = ~high;
1424 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1425 of NUM / DEN. Return the quotient and place the remainder in NUM.
1426 It is expected that NUM / DEN are close enough that the quotient is
1429 static unsigned long
1430 rtd_divmod (num, den)
1431 REAL_VALUE_TYPE *num, *den;
1433 unsigned long q, msb;
1434 int expn = num->exp, expd = den->exp;
1443 msb = num->sig[SIGSZ-1] & SIG_MSB;
1445 lshift_significand_1 (num, num);
1447 if (msb || cmp_significands (num, den) >= 0)
1449 sub_significands (num, num, den, 0);
1453 while (--expn >= expd);
1461 /* Render R as a decimal floating point constant. Emit DIGITS significant
1462 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1463 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1466 #define M_LOG10_2 0.30102999566398119521
1469 real_to_decimal (str, r_orig, buf_size, digits, crop_trailing_zeros)
1471 const REAL_VALUE_TYPE *r_orig;
1472 size_t buf_size, digits;
1473 int crop_trailing_zeros;
1475 const REAL_VALUE_TYPE *one, *ten;
1476 REAL_VALUE_TYPE r, pten, u, v;
1477 int dec_exp, cmp_one, digit;
1479 char *p, *first, *last;
1486 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1491 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1494 /* ??? Print the significand as well, if not canonical? */
1495 strcpy (str, (r.sign ? "-NaN" : "+NaN"));
1501 /* Bound the number of digits printed by the size of the representation. */
1502 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1503 if (digits == 0 || digits > max_digits)
1504 digits = max_digits;
1506 /* Estimate the decimal exponent, and compute the length of the string it
1507 will print as. Be conservative and add one to account for possible
1508 overflow or rounding error. */
1509 dec_exp = r.exp * M_LOG10_2;
1510 for (max_digits = 1; dec_exp ; max_digits++)
1513 /* Bound the number of digits printed by the size of the output buffer. */
1514 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1515 if (max_digits > buf_size)
1517 if (digits > max_digits)
1518 digits = max_digits;
1520 one = real_digit (1);
1521 ten = ten_to_ptwo (0);
1529 cmp_one = do_compare (&r, one, 0);
1534 /* Number is greater than one. Convert significand to an integer
1535 and strip trailing decimal zeros. */
1538 u.exp = SIGNIFICAND_BITS - 1;
1540 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1541 m = floor_log2 (max_digits);
1543 /* Iterate over the bits of the possible powers of 10 that might
1544 be present in U and eliminate them. That is, if we find that
1545 10**2**M divides U evenly, keep the division and increase
1551 do_divide (&t, &u, ten_to_ptwo (m));
1552 do_fix_trunc (&v, &t);
1553 if (cmp_significands (&v, &t) == 0)
1561 /* Revert the scaling to integer that we performed earlier. */
1562 u.exp += r.exp - (SIGNIFICAND_BITS - 1);
1565 /* Find power of 10. Do this by dividing out 10**2**M when
1566 this is larger than the current remainder. Fill PTEN with
1567 the power of 10 that we compute. */
1570 m = floor_log2 ((int)(r.exp * M_LOG10_2)) + 1;
1573 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1574 if (do_compare (&u, ptentwo, 0) >= 0)
1576 do_divide (&u, &u, ptentwo);
1577 do_multiply (&pten, &pten, ptentwo);
1584 /* We managed to divide off enough tens in the above reduction
1585 loop that we've now got a negative exponent. Fall into the
1586 less-than-one code to compute the proper value for PTEN. */
1593 /* Number is less than one. Pad significand with leading
1599 /* Stop if we'd shift bits off the bottom. */
1603 do_multiply (&u, &v, ten);
1605 /* Stop if we're now >= 1. */
1614 /* Find power of 10. Do this by multiplying in P=10**2**M when
1615 the current remainder is smaller than 1/P. Fill PTEN with the
1616 power of 10 that we compute. */
1617 m = floor_log2 ((int)(-r.exp * M_LOG10_2)) + 1;
1620 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1621 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1623 if (do_compare (&v, ptenmtwo, 0) <= 0)
1625 do_multiply (&v, &v, ptentwo);
1626 do_multiply (&pten, &pten, ptentwo);
1632 /* Invert the positive power of 10 that we've collected so far. */
1633 do_divide (&pten, one, &pten);
1641 /* At this point, PTEN should contain the nearest power of 10 smaller
1642 than R, such that this division produces the first digit.
1644 Using a divide-step primitive that returns the complete integral
1645 remainder avoids the rounding error that would be produced if
1646 we were to use do_divide here and then simply multiply by 10 for
1647 each subsequent digit. */
1649 digit = rtd_divmod (&r, &pten);
1651 /* Be prepared for error in that division via underflow ... */
1652 if (digit == 0 && cmp_significand_0 (&r))
1654 /* Multiply by 10 and try again. */
1655 do_multiply (&r, &r, ten);
1656 digit = rtd_divmod (&r, &pten);
1662 /* ... or overflow. */
1670 else if (digit > 10)
1675 /* Generate subsequent digits. */
1676 while (--digits > 0)
1678 do_multiply (&r, &r, ten);
1679 digit = rtd_divmod (&r, &pten);
1684 /* Generate one more digit with which to do rounding. */
1685 do_multiply (&r, &r, ten);
1686 digit = rtd_divmod (&r, &pten);
1688 /* Round the result. */
1691 /* Round to nearest. If R is nonzero there are additional
1692 nonzero digits to be extracted. */
1693 if (cmp_significand_0 (&r))
1695 /* Round to even. */
1696 else if ((p[-1] - '0') & 1)
1713 /* Carry out of the first digit. This means we had all 9's and
1714 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1722 /* Insert the decimal point. */
1723 first[0] = first[1];
1726 /* If requested, drop trailing zeros. Never crop past "1.0". */
1727 if (crop_trailing_zeros)
1728 while (last > first + 3 && last[-1] == '0')
1731 /* Append the exponent. */
1732 sprintf (last, "e%+d", dec_exp);
1735 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1736 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1737 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1738 strip trailing zeros. */
1741 real_to_hexadecimal (str, r, buf_size, digits, crop_trailing_zeros)
1743 const REAL_VALUE_TYPE *r;
1744 size_t buf_size, digits;
1745 int crop_trailing_zeros;
1747 int i, j, exp = r->exp;
1760 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1763 /* ??? Print the significand as well, if not canonical? */
1764 strcpy (str, (r->sign ? "-NaN" : "+NaN"));
1771 digits = SIGNIFICAND_BITS / 4;
1773 /* Bound the number of digits printed by the size of the output buffer. */
1775 sprintf (exp_buf, "p%+d", exp);
1776 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1777 if (max_digits > buf_size)
1779 if (digits > max_digits)
1780 digits = max_digits;
1791 for (i = SIGSZ - 1; i >= 0; --i)
1792 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1794 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1800 if (crop_trailing_zeros)
1801 while (p > first + 1 && p[-1] == '0')
1804 sprintf (p, "p%+d", exp);
1807 /* Initialize R from a decimal or hexadecimal string. The string is
1808 assumed to have been syntax checked already. */
1811 real_from_string (r, str)
1825 else if (*str == '+')
1828 if (str[0] == '0' && str[1] == 'x')
1830 /* Hexadecimal floating point. */
1831 int pos = SIGNIFICAND_BITS - 4, d;
1839 d = hex_value (*str);
1844 r->sig[pos / HOST_BITS_PER_LONG]
1845 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1854 if (pos == SIGNIFICAND_BITS - 4)
1861 d = hex_value (*str);
1866 r->sig[pos / HOST_BITS_PER_LONG]
1867 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1873 if (*str == 'p' || *str == 'P')
1875 bool exp_neg = false;
1883 else if (*str == '+')
1887 while (ISDIGIT (*str))
1893 /* Overflowed the exponent. */
1907 r->class = rvc_normal;
1914 /* Decimal floating point. */
1915 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1920 while (ISDIGIT (*str))
1923 do_multiply (r, r, ten);
1925 do_add (r, r, real_digit (d), 0);
1930 if (r->class == rvc_zero)
1935 while (ISDIGIT (*str))
1938 do_multiply (r, r, ten);
1940 do_add (r, r, real_digit (d), 0);
1945 if (*str == 'e' || *str == 'E')
1947 bool exp_neg = false;
1955 else if (*str == '+')
1959 while (ISDIGIT (*str))
1965 /* Overflowed the exponent. */
1979 times_pten (r, exp);
1994 /* Legacy. Similar, but return the result directly. */
1997 real_from_string2 (s, mode)
1999 enum machine_mode mode;
2003 real_from_string (&r, s);
2004 if (mode != VOIDmode)
2005 real_convert (&r, mode, &r);
2010 /* Initialize R from the integer pair HIGH+LOW. */
2013 real_from_integer (r, mode, low, high, unsigned_p)
2015 enum machine_mode mode;
2016 unsigned HOST_WIDE_INT low;
2020 if (low == 0 && high == 0)
2024 r->class = rvc_normal;
2025 r->sign = high < 0 && !unsigned_p;
2026 r->exp = 2 * HOST_BITS_PER_WIDE_INT;
2037 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2039 r->sig[SIGSZ-1] = high;
2040 r->sig[SIGSZ-2] = low;
2041 memset (r->sig, 0, sizeof(long)*(SIGSZ-2));
2043 else if (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT)
2045 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2046 r->sig[SIGSZ-2] = high;
2047 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2048 r->sig[SIGSZ-4] = low;
2050 memset (r->sig, 0, sizeof(long)*(SIGSZ-4));
2058 if (mode != VOIDmode)
2059 real_convert (r, mode, r);
2062 /* Returns 10**2**N. */
2064 static const REAL_VALUE_TYPE *
2068 static REAL_VALUE_TYPE tens[EXP_BITS];
2070 if (n < 0 || n >= EXP_BITS)
2073 if (tens[n].class == rvc_zero)
2075 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2077 HOST_WIDE_INT t = 10;
2080 for (i = 0; i < n; ++i)
2083 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2087 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2088 do_multiply (&tens[n], t, t);
2095 /* Returns 10**(-2**N). */
2097 static const REAL_VALUE_TYPE *
2101 static REAL_VALUE_TYPE tens[EXP_BITS];
2103 if (n < 0 || n >= EXP_BITS)
2106 if (tens[n].class == rvc_zero)
2107 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2114 static const REAL_VALUE_TYPE *
2118 static REAL_VALUE_TYPE num[10];
2123 if (n > 0 && num[n].class == rvc_zero)
2124 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2129 /* Multiply R by 10**EXP. */
2136 REAL_VALUE_TYPE pten, *rr;
2137 bool negative = (exp < 0);
2143 pten = *real_digit (1);
2149 for (i = 0; exp > 0; ++i, exp >>= 1)
2151 do_multiply (rr, rr, ten_to_ptwo (i));
2154 do_divide (r, r, &pten);
2157 /* Fills R with +Inf. */
2166 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2167 we force a QNaN, else we force an SNaN. The string, if not empty,
2168 is parsed as a number and placed in the significand. Return true
2169 if the string was successfully parsed. */
2172 real_nan (r, str, quiet, mode)
2176 enum machine_mode mode;
2178 const struct real_format *fmt;
2180 fmt = real_format_for_mode[mode - QFmode];
2187 get_canonical_qnan (r, 0);
2189 get_canonical_snan (r, 0);
2196 memset (r, 0, sizeof (*r));
2199 /* Parse akin to strtol into the significand of R. */
2201 while (ISSPACE (*str))
2205 else if (*str == '+')
2215 while ((d = hex_value (*str)) < base)
2222 lshift_significand (r, r, 3);
2225 lshift_significand (r, r, 4);
2228 lshift_significand_1 (&u, r);
2229 lshift_significand (r, r, 3);
2230 add_significands (r, r, &u);
2238 add_significands (r, r, &u);
2243 /* Must have consumed the entire string for success. */
2247 /* Shift the significand into place such that the bits
2248 are in the most significant bits for the format. */
2249 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->p);
2251 /* Our MSB is always unset for NaNs. */
2252 r->sig[SIGSZ-1] &= ~SIG_MSB;
2254 /* Force quiet or signalling NaN. */
2255 r->signalling = !quiet;
2261 /* Fills R with 2**N. */
2268 memset (r, 0, sizeof (*r));
2273 else if (n < -MAX_EXP)
2277 r->class = rvc_normal;
2279 r->sig[SIGSZ-1] = SIG_MSB;
2285 round_for_format (fmt, r)
2286 const struct real_format *fmt;
2290 unsigned long sticky;
2294 p2 = fmt->p * fmt->log2_b;
2295 emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2296 emax2 = fmt->emax * fmt->log2_b;
2298 np2 = SIGNIFICAND_BITS - p2;
2302 get_zero (r, r->sign);
2304 if (!fmt->has_signed_zero)
2309 get_inf (r, r->sign);
2314 clear_significand_below (r, np2);
2324 /* If we're not base2, normalize the exponent to a multiple of
2326 if (fmt->log2_b != 1)
2328 int shift = r->exp & (fmt->log2_b - 1);
2331 shift = fmt->log2_b - shift;
2332 r->sig[0] |= sticky_rshift_significand (r, r, shift);
2337 /* Check the range of the exponent. If we're out of range,
2338 either underflow or overflow. */
2341 else if (r->exp <= emin2m1)
2345 if (!fmt->has_denorm)
2347 /* Don't underflow completely until we've had a chance to round. */
2348 if (r->exp < emin2m1)
2353 diff = emin2m1 - r->exp + 1;
2357 /* De-normalize the significand. */
2358 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2363 /* There are P2 true significand bits, followed by one guard bit,
2364 followed by one sticky bit, followed by stuff. Fold nonzero
2365 stuff into the sticky bit. */
2368 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2369 sticky |= r->sig[i];
2371 r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2373 guard = test_significand_bit (r, np2 - 1);
2374 lsb = test_significand_bit (r, np2);
2376 /* Round to even. */
2377 if (guard && (sticky || lsb))
2381 set_significand_bit (&u, np2);
2383 if (add_significands (r, r, &u))
2385 /* Overflow. Means the significand had been all ones, and
2386 is now all zeros. Need to increase the exponent, and
2387 possibly re-normalize it. */
2388 if (++r->exp > emax2)
2390 r->sig[SIGSZ-1] = SIG_MSB;
2392 if (fmt->log2_b != 1)
2394 int shift = r->exp & (fmt->log2_b - 1);
2397 shift = fmt->log2_b - shift;
2398 rshift_significand (r, r, shift);
2407 /* Catch underflow that we deferred until after rounding. */
2408 if (r->exp <= emin2m1)
2411 /* Clear out trailing garbage. */
2412 clear_significand_below (r, np2);
2415 /* Extend or truncate to a new mode. */
2418 real_convert (r, mode, a)
2420 enum machine_mode mode;
2421 const REAL_VALUE_TYPE *a;
2423 const struct real_format *fmt;
2425 fmt = real_format_for_mode[mode - QFmode];
2430 round_for_format (fmt, r);
2432 /* round_for_format de-normalizes denormals. Undo just that part. */
2433 if (r->class == rvc_normal)
2437 /* Legacy. Likewise, except return the struct directly. */
2440 real_value_truncate (mode, a)
2441 enum machine_mode mode;
2445 real_convert (&r, mode, &a);
2449 /* Return true if truncating to MODE is exact. */
2452 exact_real_truncate (mode, a)
2453 enum machine_mode mode;
2454 const REAL_VALUE_TYPE *a;
2457 real_convert (&t, mode, a);
2458 return real_identical (&t, a);
2461 /* Write R to the given target format. Place the words of the result
2462 in target word order in BUF. There are always 32 bits in each
2463 long, no matter the size of the host long.
2465 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2468 real_to_target_fmt (buf, r_orig, fmt)
2470 const REAL_VALUE_TYPE *r_orig;
2471 const struct real_format *fmt;
2477 round_for_format (fmt, &r);
2481 (*fmt->encode) (fmt, buf, &r);
2486 /* Similar, but look up the format from MODE. */
2489 real_to_target (buf, r, mode)
2491 const REAL_VALUE_TYPE *r;
2492 enum machine_mode mode;
2494 const struct real_format *fmt;
2496 fmt = real_format_for_mode[mode - QFmode];
2500 return real_to_target_fmt (buf, r, fmt);
2503 /* Read R from the given target format. Read the words of the result
2504 in target word order in BUF. There are always 32 bits in each
2505 long, no matter the size of the host long. */
2508 real_from_target_fmt (r, buf, fmt)
2511 const struct real_format *fmt;
2513 (*fmt->decode) (fmt, r, buf);
2516 /* Similar, but look up the format from MODE. */
2519 real_from_target (r, buf, mode)
2522 enum machine_mode mode;
2524 const struct real_format *fmt;
2526 fmt = real_format_for_mode[mode - QFmode];
2530 (*fmt->decode) (fmt, r, buf);
2533 /* Return the number of bits in the significand for MODE. */
2534 /* ??? Legacy. Should get access to real_format directly. */
2537 significand_size (mode)
2538 enum machine_mode mode;
2540 const struct real_format *fmt;
2542 fmt = real_format_for_mode[mode - QFmode];
2546 return fmt->p * fmt->log2_b;
2549 /* Return a hash value for the given real value. */
2550 /* ??? The "unsigned int" return value is intended to be hashval_t,
2551 but I didn't want to pull hashtab.h into real.h. */
2555 const REAL_VALUE_TYPE *r;
2560 h = r->class | (r->sign << 2);
2572 if (sizeof(unsigned long) > sizeof(unsigned int))
2573 for (i = 0; i < SIGSZ; ++i)
2575 unsigned long s = r->sig[i];
2576 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2579 for (i = 0; i < SIGSZ; ++i)
2590 /* IEEE single-precision format. */
2592 static void encode_ieee_single PARAMS ((const struct real_format *fmt,
2593 long *, const REAL_VALUE_TYPE *));
2594 static void decode_ieee_single PARAMS ((const struct real_format *,
2595 REAL_VALUE_TYPE *, const long *));
2598 encode_ieee_single (fmt, buf, r)
2599 const struct real_format *fmt;
2601 const REAL_VALUE_TYPE *r;
2603 unsigned long image, sig, exp;
2604 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2606 image = r->sign << 31;
2607 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2618 image |= 0x7fffffff;
2624 if (r->signalling == fmt->qnan_msb_set)
2635 image |= 0x7fffffff;
2639 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2640 whereas the intermediate representation is 0.F x 2**exp.
2641 Which means we're off by one. */
2645 exp = r->exp + 127 - 1;
2658 decode_ieee_single (fmt, r, buf)
2659 const struct real_format *fmt;
2663 unsigned long image = buf[0] & 0xffffffff;
2664 bool sign = (image >> 31) & 1;
2665 int exp = (image >> 23) & 0xff;
2667 memset (r, 0, sizeof (*r));
2668 image <<= HOST_BITS_PER_LONG - 24;
2673 if (image && fmt->has_denorm)
2675 r->class = rvc_normal;
2678 r->sig[SIGSZ-1] = image << 1;
2681 else if (fmt->has_signed_zero)
2684 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2690 r->signalling = ((image >> 22) & 1) ^ fmt->qnan_msb_set;
2691 r->sig[SIGSZ-1] = image;
2701 r->class = rvc_normal;
2703 r->exp = exp - 127 + 1;
2704 r->sig[SIGSZ-1] = image | SIG_MSB;
2708 const struct real_format ieee_single_format =
2726 /* IEEE double-precision format. */
2728 static void encode_ieee_double PARAMS ((const struct real_format *fmt,
2729 long *, const REAL_VALUE_TYPE *));
2730 static void decode_ieee_double PARAMS ((const struct real_format *,
2731 REAL_VALUE_TYPE *, const long *));
2734 encode_ieee_double (fmt, buf, r)
2735 const struct real_format *fmt;
2737 const REAL_VALUE_TYPE *r;
2739 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2740 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2742 image_hi = r->sign << 31;
2745 if (HOST_BITS_PER_LONG == 64)
2747 sig_hi = r->sig[SIGSZ-1];
2748 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2749 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2753 sig_hi = r->sig[SIGSZ-1];
2754 sig_lo = r->sig[SIGSZ-2];
2755 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2756 sig_hi = (sig_hi >> 11) & 0xfffff;
2766 image_hi |= 2047 << 20;
2769 image_hi |= 0x7fffffff;
2770 image_lo = 0xffffffff;
2777 if (r->signalling == fmt->qnan_msb_set)
2778 sig_hi &= ~(1 << 19);
2781 if (sig_hi == 0 && sig_lo == 0)
2784 image_hi |= 2047 << 20;
2790 image_hi |= 0x7fffffff;
2791 image_lo = 0xffffffff;
2796 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2797 whereas the intermediate representation is 0.F x 2**exp.
2798 Which means we're off by one. */
2802 exp = r->exp + 1023 - 1;
2803 image_hi |= exp << 20;
2812 if (FLOAT_WORDS_BIG_ENDIAN)
2813 buf[0] = image_hi, buf[1] = image_lo;
2815 buf[0] = image_lo, buf[1] = image_hi;
2819 decode_ieee_double (fmt, r, buf)
2820 const struct real_format *fmt;
2824 unsigned long image_hi, image_lo;
2828 if (FLOAT_WORDS_BIG_ENDIAN)
2829 image_hi = buf[0], image_lo = buf[1];
2831 image_lo = buf[0], image_hi = buf[1];
2832 image_lo &= 0xffffffff;
2833 image_hi &= 0xffffffff;
2835 sign = (image_hi >> 31) & 1;
2836 exp = (image_hi >> 20) & 0x7ff;
2838 memset (r, 0, sizeof (*r));
2840 image_hi <<= 32 - 21;
2841 image_hi |= image_lo >> 21;
2842 image_hi &= 0x7fffffff;
2843 image_lo <<= 32 - 21;
2847 if ((image_hi || image_lo) && fmt->has_denorm)
2849 r->class = rvc_normal;
2852 if (HOST_BITS_PER_LONG == 32)
2854 image_hi = (image_hi << 1) | (image_lo >> 31);
2856 r->sig[SIGSZ-1] = image_hi;
2857 r->sig[SIGSZ-2] = image_lo;
2861 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2862 r->sig[SIGSZ-1] = image_hi;
2866 else if (fmt->has_signed_zero)
2869 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2871 if (image_hi || image_lo)
2875 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
2876 if (HOST_BITS_PER_LONG == 32)
2878 r->sig[SIGSZ-1] = image_hi;
2879 r->sig[SIGSZ-2] = image_lo;
2882 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
2892 r->class = rvc_normal;
2894 r->exp = exp - 1023 + 1;
2895 if (HOST_BITS_PER_LONG == 32)
2897 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
2898 r->sig[SIGSZ-2] = image_lo;
2901 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2905 const struct real_format ieee_double_format =
2923 /* IEEE extended double precision format. This comes in three
2924 flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2927 static void encode_ieee_extended PARAMS ((const struct real_format *fmt,
2928 long *, const REAL_VALUE_TYPE *));
2929 static void decode_ieee_extended PARAMS ((const struct real_format *,
2930 REAL_VALUE_TYPE *, const long *));
2932 static void encode_ieee_extended_128 PARAMS ((const struct real_format *fmt,
2934 const REAL_VALUE_TYPE *));
2935 static void decode_ieee_extended_128 PARAMS ((const struct real_format *,
2940 encode_ieee_extended (fmt, buf, r)
2941 const struct real_format *fmt;
2943 const REAL_VALUE_TYPE *r;
2945 unsigned long image_hi, sig_hi, sig_lo;
2946 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2948 image_hi = r->sign << 15;
2949 sig_hi = sig_lo = 0;
2961 /* Intel requires the explicit integer bit to be set, otherwise
2962 it considers the value a "pseudo-infinity". Motorola docs
2963 say it doesn't care. */
2964 sig_hi = 0x80000000;
2969 sig_lo = sig_hi = 0xffffffff;
2977 if (HOST_BITS_PER_LONG == 32)
2979 sig_hi = r->sig[SIGSZ-1];
2980 sig_lo = r->sig[SIGSZ-2];
2984 sig_lo = r->sig[SIGSZ-1];
2985 sig_hi = sig_lo >> 31 >> 1;
2986 sig_lo &= 0xffffffff;
2988 if (r->signalling == fmt->qnan_msb_set)
2989 sig_hi &= ~(1 << 30);
2992 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
2995 /* Intel requires the explicit integer bit to be set, otherwise
2996 it considers the value a "pseudo-nan". Motorola docs say it
2998 sig_hi |= 0x80000000;
3003 sig_lo = sig_hi = 0xffffffff;
3011 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3012 whereas the intermediate representation is 0.F x 2**exp.
3013 Which means we're off by one.
3015 Except for Motorola, which consider exp=0 and explicit
3016 integer bit set to continue to be normalized. In theory
3017 this discrepancy has been taken care of by the difference
3018 in fmt->emin in round_for_format. */
3030 if (HOST_BITS_PER_LONG == 32)
3032 sig_hi = r->sig[SIGSZ-1];
3033 sig_lo = r->sig[SIGSZ-2];
3037 sig_lo = r->sig[SIGSZ-1];
3038 sig_hi = sig_lo >> 31 >> 1;
3039 sig_lo &= 0xffffffff;
3048 if (FLOAT_WORDS_BIG_ENDIAN)
3049 buf[0] = image_hi << 16, buf[1] = sig_hi, buf[2] = sig_lo;
3051 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3055 encode_ieee_extended_128 (fmt, buf, r)
3056 const struct real_format *fmt;
3058 const REAL_VALUE_TYPE *r;
3060 buf[3 * !FLOAT_WORDS_BIG_ENDIAN] = 0;
3061 encode_ieee_extended (fmt, buf+!!FLOAT_WORDS_BIG_ENDIAN, r);
3065 decode_ieee_extended (fmt, r, buf)
3066 const struct real_format *fmt;
3070 unsigned long image_hi, sig_hi, sig_lo;
3074 if (FLOAT_WORDS_BIG_ENDIAN)
3075 image_hi = buf[0] >> 16, sig_hi = buf[1], sig_lo = buf[2];
3077 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3078 sig_lo &= 0xffffffff;
3079 sig_hi &= 0xffffffff;
3080 image_hi &= 0xffffffff;
3082 sign = (image_hi >> 15) & 1;
3083 exp = image_hi & 0x7fff;
3085 memset (r, 0, sizeof (*r));
3089 if ((sig_hi || sig_lo) && fmt->has_denorm)
3091 r->class = rvc_normal;
3094 /* When the IEEE format contains a hidden bit, we know that
3095 it's zero at this point, and so shift up the significand
3096 and decrease the exponent to match. In this case, Motorola
3097 defines the explicit integer bit to be valid, so we don't
3098 know whether the msb is set or not. */
3100 if (HOST_BITS_PER_LONG == 32)
3102 r->sig[SIGSZ-1] = sig_hi;
3103 r->sig[SIGSZ-2] = sig_lo;
3106 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3110 else if (fmt->has_signed_zero)
3113 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3115 /* See above re "pseudo-infinities" and "pseudo-nans".
3116 Short summary is that the MSB will likely always be
3117 set, and that we don't care about it. */
3118 sig_hi &= 0x7fffffff;
3120 if (sig_hi || sig_lo)
3124 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3125 if (HOST_BITS_PER_LONG == 32)
3127 r->sig[SIGSZ-1] = sig_hi;
3128 r->sig[SIGSZ-2] = sig_lo;
3131 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3141 r->class = rvc_normal;
3143 r->exp = exp - 16383 + 1;
3144 if (HOST_BITS_PER_LONG == 32)
3146 r->sig[SIGSZ-1] = sig_hi;
3147 r->sig[SIGSZ-2] = sig_lo;
3150 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3155 decode_ieee_extended_128 (fmt, r, buf)
3156 const struct real_format *fmt;
3160 decode_ieee_extended (fmt, r, buf+!!FLOAT_WORDS_BIG_ENDIAN);
3163 const struct real_format ieee_extended_motorola_format =
3165 encode_ieee_extended,
3166 decode_ieee_extended,
3180 const struct real_format ieee_extended_intel_96_format =
3182 encode_ieee_extended,
3183 decode_ieee_extended,
3197 const struct real_format ieee_extended_intel_128_format =
3199 encode_ieee_extended_128,
3200 decode_ieee_extended_128,
3215 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3216 numbers whose sum is equal to the extended precision value. The number
3217 with greater magnitude is first. This format has the same magnitude
3218 range as an IEEE double precision value, but effectively 106 bits of
3219 significand precision. Infinity and NaN are represented by their IEEE
3220 double precision value stored in the first number, the second number is
3221 ignored. Zeroes, Infinities, and NaNs are set in both doubles
3222 due to precedent. */
3224 static void encode_ibm_extended PARAMS ((const struct real_format *fmt,
3225 long *, const REAL_VALUE_TYPE *));
3226 static void decode_ibm_extended PARAMS ((const struct real_format *,
3227 REAL_VALUE_TYPE *, const long *));
3230 encode_ibm_extended (fmt, buf, r)
3231 const struct real_format *fmt ATTRIBUTE_UNUSED;
3233 const REAL_VALUE_TYPE *r;
3235 REAL_VALUE_TYPE u, v;
3240 /* Both doubles have sign bit set. */
3241 buf[0] = FLOAT_WORDS_BIG_ENDIAN ? r->sign << 31 : 0;
3242 buf[1] = FLOAT_WORDS_BIG_ENDIAN ? 0 : r->sign << 31;
3249 /* Both doubles set to Inf / NaN. */
3250 encode_ieee_double (&ieee_double_format, &buf[0], r);
3256 /* u = IEEE double precision portion of significand. */
3258 clear_significand_below (&u, SIGNIFICAND_BITS - 53);
3261 /* If the upper double is zero, we have a denormal double, so
3262 move it to the first double and leave the second as zero. */
3263 if (u.class == rvc_zero)
3271 /* v = remainder containing additional 53 bits of significand. */
3272 do_add (&v, r, &u, 1);
3273 round_for_format (&ieee_double_format, &v);
3276 round_for_format (&ieee_double_format, &u);
3278 encode_ieee_double (&ieee_double_format, &buf[0], &u);
3279 encode_ieee_double (&ieee_double_format, &buf[2], &v);
3288 decode_ibm_extended (fmt, r, buf)
3289 const struct real_format *fmt ATTRIBUTE_UNUSED;
3293 REAL_VALUE_TYPE u, v;
3295 decode_ieee_double (&ieee_double_format, &u, &buf[0]);
3297 if (u.class != rvc_zero && u.class != rvc_inf && u.class != rvc_nan)
3299 decode_ieee_double (&ieee_double_format, &v, &buf[2]);
3300 do_add (r, &u, &v, 0);
3306 const struct real_format ibm_extended_format =
3308 encode_ibm_extended,
3309 decode_ibm_extended,
3324 /* IEEE quad precision format. */
3326 static void encode_ieee_quad PARAMS ((const struct real_format *fmt,
3327 long *, const REAL_VALUE_TYPE *));
3328 static void decode_ieee_quad PARAMS ((const struct real_format *,
3329 REAL_VALUE_TYPE *, const long *));
3332 encode_ieee_quad (fmt, buf, r)
3333 const struct real_format *fmt;
3335 const REAL_VALUE_TYPE *r;
3337 unsigned long image3, image2, image1, image0, exp;
3338 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3341 image3 = r->sign << 31;
3346 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3355 image3 |= 32767 << 16;
3358 image3 |= 0x7fffffff;
3359 image2 = 0xffffffff;
3360 image1 = 0xffffffff;
3361 image0 = 0xffffffff;
3368 image3 |= 32767 << 16;
3370 if (HOST_BITS_PER_LONG == 32)
3375 image3 |= u.sig[3] & 0xffff;
3380 image1 = image0 >> 31 >> 1;
3382 image3 |= (image2 >> 31 >> 1) & 0xffff;
3383 image0 &= 0xffffffff;
3384 image2 &= 0xffffffff;
3386 if (r->signalling == fmt->qnan_msb_set)
3390 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3395 image3 |= 0x7fffffff;
3396 image2 = 0xffffffff;
3397 image1 = 0xffffffff;
3398 image0 = 0xffffffff;
3403 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3404 whereas the intermediate representation is 0.F x 2**exp.
3405 Which means we're off by one. */
3409 exp = r->exp + 16383 - 1;
3410 image3 |= exp << 16;
3412 if (HOST_BITS_PER_LONG == 32)
3417 image3 |= u.sig[3] & 0xffff;
3422 image1 = image0 >> 31 >> 1;
3424 image3 |= (image2 >> 31 >> 1) & 0xffff;
3425 image0 &= 0xffffffff;
3426 image2 &= 0xffffffff;
3434 if (FLOAT_WORDS_BIG_ENDIAN)
3451 decode_ieee_quad (fmt, r, buf)
3452 const struct real_format *fmt;
3456 unsigned long image3, image2, image1, image0;
3460 if (FLOAT_WORDS_BIG_ENDIAN)
3474 image0 &= 0xffffffff;
3475 image1 &= 0xffffffff;
3476 image2 &= 0xffffffff;
3478 sign = (image3 >> 31) & 1;
3479 exp = (image3 >> 16) & 0x7fff;
3482 memset (r, 0, sizeof (*r));
3486 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3488 r->class = rvc_normal;
3491 r->exp = -16382 + (SIGNIFICAND_BITS - 112);
3492 if (HOST_BITS_PER_LONG == 32)
3501 r->sig[0] = (image1 << 31 << 1) | image0;
3502 r->sig[1] = (image3 << 31 << 1) | image2;
3507 else if (fmt->has_signed_zero)
3510 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3512 if (image3 | image2 | image1 | image0)
3516 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
3518 if (HOST_BITS_PER_LONG == 32)
3527 r->sig[0] = (image1 << 31 << 1) | image0;
3528 r->sig[1] = (image3 << 31 << 1) | image2;
3530 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3540 r->class = rvc_normal;
3542 r->exp = exp - 16383 + 1;
3544 if (HOST_BITS_PER_LONG == 32)
3553 r->sig[0] = (image1 << 31 << 1) | image0;
3554 r->sig[1] = (image3 << 31 << 1) | image2;
3556 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3557 r->sig[SIGSZ-1] |= SIG_MSB;
3561 const struct real_format ieee_quad_format =
3578 /* Descriptions of VAX floating point formats can be found beginning at
3580 http://www.openvms.compaq.com:8000/73final/4515/4515pro_013.html#f_floating_point_format
3582 The thing to remember is that they're almost IEEE, except for word
3583 order, exponent bias, and the lack of infinities, nans, and denormals.
3585 We don't implement the H_floating format here, simply because neither
3586 the VAX or Alpha ports use it. */
3588 static void encode_vax_f PARAMS ((const struct real_format *fmt,
3589 long *, const REAL_VALUE_TYPE *));
3590 static void decode_vax_f PARAMS ((const struct real_format *,
3591 REAL_VALUE_TYPE *, const long *));
3592 static void encode_vax_d PARAMS ((const struct real_format *fmt,
3593 long *, const REAL_VALUE_TYPE *));
3594 static void decode_vax_d PARAMS ((const struct real_format *,
3595 REAL_VALUE_TYPE *, const long *));
3596 static void encode_vax_g PARAMS ((const struct real_format *fmt,
3597 long *, const REAL_VALUE_TYPE *));
3598 static void decode_vax_g PARAMS ((const struct real_format *,
3599 REAL_VALUE_TYPE *, const long *));
3602 encode_vax_f (fmt, buf, r)
3603 const struct real_format *fmt ATTRIBUTE_UNUSED;
3605 const REAL_VALUE_TYPE *r;
3607 unsigned long sign, exp, sig, image;
3609 sign = r->sign << 15;
3619 image = 0xffff7fff | sign;
3623 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3626 image = (sig << 16) & 0xffff0000;
3640 decode_vax_f (fmt, r, buf)
3641 const struct real_format *fmt ATTRIBUTE_UNUSED;
3645 unsigned long image = buf[0] & 0xffffffff;
3646 int exp = (image >> 7) & 0xff;
3648 memset (r, 0, sizeof (*r));
3652 r->class = rvc_normal;
3653 r->sign = (image >> 15) & 1;
3656 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3657 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3662 encode_vax_d (fmt, buf, r)
3663 const struct real_format *fmt ATTRIBUTE_UNUSED;
3665 const REAL_VALUE_TYPE *r;
3667 unsigned long image0, image1, sign = r->sign << 15;
3672 image0 = image1 = 0;
3677 image0 = 0xffff7fff | sign;
3678 image1 = 0xffffffff;
3682 /* Extract the significand into straight hi:lo. */
3683 if (HOST_BITS_PER_LONG == 64)
3685 image0 = r->sig[SIGSZ-1];
3686 image1 = (image0 >> (64 - 56)) & 0xffffffff;
3687 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3691 image0 = r->sig[SIGSZ-1];
3692 image1 = r->sig[SIGSZ-2];
3693 image1 = (image0 << 24) | (image1 >> 8);
3694 image0 = (image0 >> 8) & 0xffffff;
3697 /* Rearrange the half-words of the significand to match the
3699 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3700 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3702 /* Add the sign and exponent. */
3704 image0 |= (r->exp + 128) << 7;
3711 if (FLOAT_WORDS_BIG_ENDIAN)
3712 buf[0] = image1, buf[1] = image0;
3714 buf[0] = image0, buf[1] = image1;
3718 decode_vax_d (fmt, r, buf)
3719 const struct real_format *fmt ATTRIBUTE_UNUSED;
3723 unsigned long image0, image1;
3726 if (FLOAT_WORDS_BIG_ENDIAN)
3727 image1 = buf[0], image0 = buf[1];
3729 image0 = buf[0], image1 = buf[1];
3730 image0 &= 0xffffffff;
3731 image1 &= 0xffffffff;
3733 exp = (image0 >> 7) & 0x7f;
3735 memset (r, 0, sizeof (*r));
3739 r->class = rvc_normal;
3740 r->sign = (image0 >> 15) & 1;
3743 /* Rearrange the half-words of the external format into
3744 proper ascending order. */
3745 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
3746 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3748 if (HOST_BITS_PER_LONG == 64)
3750 image0 = (image0 << 31 << 1) | image1;
3753 r->sig[SIGSZ-1] = image0;
3757 r->sig[SIGSZ-1] = image0;
3758 r->sig[SIGSZ-2] = image1;
3759 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
3760 r->sig[SIGSZ-1] |= SIG_MSB;
3766 encode_vax_g (fmt, buf, r)
3767 const struct real_format *fmt ATTRIBUTE_UNUSED;
3769 const REAL_VALUE_TYPE *r;
3771 unsigned long image0, image1, sign = r->sign << 15;
3776 image0 = image1 = 0;
3781 image0 = 0xffff7fff | sign;
3782 image1 = 0xffffffff;
3786 /* Extract the significand into straight hi:lo. */
3787 if (HOST_BITS_PER_LONG == 64)
3789 image0 = r->sig[SIGSZ-1];
3790 image1 = (image0 >> (64 - 53)) & 0xffffffff;
3791 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
3795 image0 = r->sig[SIGSZ-1];
3796 image1 = r->sig[SIGSZ-2];
3797 image1 = (image0 << 21) | (image1 >> 11);
3798 image0 = (image0 >> 11) & 0xfffff;
3801 /* Rearrange the half-words of the significand to match the
3803 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
3804 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3806 /* Add the sign and exponent. */
3808 image0 |= (r->exp + 1024) << 4;
3815 if (FLOAT_WORDS_BIG_ENDIAN)
3816 buf[0] = image1, buf[1] = image0;
3818 buf[0] = image0, buf[1] = image1;
3822 decode_vax_g (fmt, r, buf)
3823 const struct real_format *fmt ATTRIBUTE_UNUSED;
3827 unsigned long image0, image1;
3830 if (FLOAT_WORDS_BIG_ENDIAN)
3831 image1 = buf[0], image0 = buf[1];
3833 image0 = buf[0], image1 = buf[1];
3834 image0 &= 0xffffffff;
3835 image1 &= 0xffffffff;
3837 exp = (image0 >> 4) & 0x7ff;
3839 memset (r, 0, sizeof (*r));
3843 r->class = rvc_normal;
3844 r->sign = (image0 >> 15) & 1;
3845 r->exp = exp - 1024;
3847 /* Rearrange the half-words of the external format into
3848 proper ascending order. */
3849 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
3850 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3852 if (HOST_BITS_PER_LONG == 64)
3854 image0 = (image0 << 31 << 1) | image1;
3857 r->sig[SIGSZ-1] = image0;
3861 r->sig[SIGSZ-1] = image0;
3862 r->sig[SIGSZ-2] = image1;
3863 lshift_significand (r, r, 64 - 53);
3864 r->sig[SIGSZ-1] |= SIG_MSB;
3869 const struct real_format vax_f_format =
3886 const struct real_format vax_d_format =
3903 const struct real_format vax_g_format =
3920 /* A good reference for these can be found in chapter 9 of
3921 "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
3922 An on-line version can be found here:
3924 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3927 static void encode_i370_single PARAMS ((const struct real_format *fmt,
3928 long *, const REAL_VALUE_TYPE *));
3929 static void decode_i370_single PARAMS ((const struct real_format *,
3930 REAL_VALUE_TYPE *, const long *));
3931 static void encode_i370_double PARAMS ((const struct real_format *fmt,
3932 long *, const REAL_VALUE_TYPE *));
3933 static void decode_i370_double PARAMS ((const struct real_format *,
3934 REAL_VALUE_TYPE *, const long *));
3937 encode_i370_single (fmt, buf, r)
3938 const struct real_format *fmt ATTRIBUTE_UNUSED;
3940 const REAL_VALUE_TYPE *r;
3942 unsigned long sign, exp, sig, image;
3944 sign = r->sign << 31;
3954 image = 0x7fffffff | sign;
3958 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
3959 exp = ((r->exp / 4) + 64) << 24;
3960 image = sign | exp | sig;
3971 decode_i370_single (fmt, r, buf)
3972 const struct real_format *fmt ATTRIBUTE_UNUSED;
3976 unsigned long sign, sig, image = buf[0];
3979 sign = (image >> 31) & 1;
3980 exp = (image >> 24) & 0x7f;
3981 sig = image & 0xffffff;
3983 memset (r, 0, sizeof (*r));
3987 r->class = rvc_normal;
3989 r->exp = (exp - 64) * 4;
3990 r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
3996 encode_i370_double (fmt, buf, r)
3997 const struct real_format *fmt ATTRIBUTE_UNUSED;
3999 const REAL_VALUE_TYPE *r;
4001 unsigned long sign, exp, image_hi, image_lo;
4003 sign = r->sign << 31;
4008 image_hi = image_lo = 0;
4013 image_hi = 0x7fffffff | sign;
4014 image_lo = 0xffffffff;
4018 if (HOST_BITS_PER_LONG == 64)
4020 image_hi = r->sig[SIGSZ-1];
4021 image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
4022 image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
4026 image_hi = r->sig[SIGSZ-1];
4027 image_lo = r->sig[SIGSZ-2];
4028 image_lo = (image_lo >> 8) | (image_hi << 24);
4032 exp = ((r->exp / 4) + 64) << 24;
4033 image_hi |= sign | exp;
4040 if (FLOAT_WORDS_BIG_ENDIAN)
4041 buf[0] = image_hi, buf[1] = image_lo;
4043 buf[0] = image_lo, buf[1] = image_hi;
4047 decode_i370_double (fmt, r, buf)
4048 const struct real_format *fmt ATTRIBUTE_UNUSED;
4052 unsigned long sign, image_hi, image_lo;
4055 if (FLOAT_WORDS_BIG_ENDIAN)
4056 image_hi = buf[0], image_lo = buf[1];
4058 image_lo = buf[0], image_hi = buf[1];
4060 sign = (image_hi >> 31) & 1;
4061 exp = (image_hi >> 24) & 0x7f;
4062 image_hi &= 0xffffff;
4063 image_lo &= 0xffffffff;
4065 memset (r, 0, sizeof (*r));
4067 if (exp || image_hi || image_lo)
4069 r->class = rvc_normal;
4071 r->exp = (exp - 64) * 4 + (SIGNIFICAND_BITS - 56);
4073 if (HOST_BITS_PER_LONG == 32)
4075 r->sig[0] = image_lo;
4076 r->sig[1] = image_hi;
4079 r->sig[0] = image_lo | (image_hi << 31 << 1);
4085 const struct real_format i370_single_format =
4097 false, /* ??? The encoding does allow for "unnormals". */
4098 false, /* ??? The encoding does allow for "unnormals". */
4102 const struct real_format i370_double_format =
4114 false, /* ??? The encoding does allow for "unnormals". */
4115 false, /* ??? The encoding does allow for "unnormals". */
4119 /* The "twos-complement" c4x format is officially defined as
4123 This is rather misleading. One must remember that F is signed.
4124 A better description would be
4126 x = -1**s * ((s + 1 + .f) * 2**e
4128 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4129 that's -1 * (1+1+(-.5)) == -1.5. I think.
4131 The constructions here are taken from Tables 5-1 and 5-2 of the
4132 TMS320C4x User's Guide wherein step-by-step instructions for
4133 conversion from IEEE are presented. That's close enough to our
4134 internal representation so as to make things easy.
4136 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
4138 static void encode_c4x_single PARAMS ((const struct real_format *fmt,
4139 long *, const REAL_VALUE_TYPE *));
4140 static void decode_c4x_single PARAMS ((const struct real_format *,
4141 REAL_VALUE_TYPE *, const long *));
4142 static void encode_c4x_extended PARAMS ((const struct real_format *fmt,
4143 long *, const REAL_VALUE_TYPE *));
4144 static void decode_c4x_extended PARAMS ((const struct real_format *,
4145 REAL_VALUE_TYPE *, const long *));
4148 encode_c4x_single (fmt, buf, r)
4149 const struct real_format *fmt ATTRIBUTE_UNUSED;
4151 const REAL_VALUE_TYPE *r;
4153 unsigned long image, exp, sig;
4165 sig = 0x800000 - r->sign;
4170 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4185 image = ((exp & 0xff) << 24) | (sig & 0xffffff);
4190 decode_c4x_single (fmt, r, buf)
4191 const struct real_format *fmt ATTRIBUTE_UNUSED;
4195 unsigned long image = buf[0];
4199 exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
4200 sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
4202 memset (r, 0, sizeof (*r));
4206 r->class = rvc_normal;
4208 sig = sf & 0x7fffff;
4217 sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4220 r->sig[SIGSZ-1] = sig;
4225 encode_c4x_extended (fmt, buf, r)
4226 const struct real_format *fmt ATTRIBUTE_UNUSED;
4228 const REAL_VALUE_TYPE *r;
4230 unsigned long exp, sig;
4242 sig = 0x80000000 - r->sign;
4248 sig = r->sig[SIGSZ-1];
4249 if (HOST_BITS_PER_LONG == 64)
4250 sig = sig >> 1 >> 31;
4267 exp = (exp & 0xff) << 24;
4270 if (FLOAT_WORDS_BIG_ENDIAN)
4271 buf[0] = exp, buf[1] = sig;
4273 buf[0] = sig, buf[0] = exp;
4277 decode_c4x_extended (fmt, r, buf)
4278 const struct real_format *fmt ATTRIBUTE_UNUSED;
4285 if (FLOAT_WORDS_BIG_ENDIAN)
4286 exp = buf[0], sf = buf[1];
4288 sf = buf[0], exp = buf[1];
4290 exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
4291 sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
4293 memset (r, 0, sizeof (*r));
4297 r->class = rvc_normal;
4299 sig = sf & 0x7fffffff;
4308 if (HOST_BITS_PER_LONG == 64)
4309 sig = sig << 1 << 31;
4313 r->sig[SIGSZ-1] = sig;
4317 const struct real_format c4x_single_format =
4334 const struct real_format c4x_extended_format =
4336 encode_c4x_extended,
4337 decode_c4x_extended,
4352 /* A synthetic "format" for internal arithmetic. It's the size of the
4353 internal significand minus the two bits needed for proper rounding.
4354 The encode and decode routines exist only to satisfy our paranoia
4357 static void encode_internal PARAMS ((const struct real_format *fmt,
4358 long *, const REAL_VALUE_TYPE *));
4359 static void decode_internal PARAMS ((const struct real_format *,
4360 REAL_VALUE_TYPE *, const long *));
4363 encode_internal (fmt, buf, r)
4364 const struct real_format *fmt ATTRIBUTE_UNUSED;
4366 const REAL_VALUE_TYPE *r;
4368 memcpy (buf, r, sizeof (*r));
4372 decode_internal (fmt, r, buf)
4373 const struct real_format *fmt ATTRIBUTE_UNUSED;
4377 memcpy (r, buf, sizeof (*r));
4380 const struct real_format real_internal_format =
4386 SIGNIFICAND_BITS - 2,
4397 /* Set up default mode to format mapping for IEEE. Everyone else has
4398 to set these values in OVERRIDE_OPTIONS. */
4400 const struct real_format *real_format_for_mode[TFmode - QFmode + 1] =
4405 &ieee_single_format, /* SFmode */
4406 &ieee_double_format, /* DFmode */
4408 /* We explicitly don't handle XFmode. There are two formats,
4409 pretty much equally common. Choose one in OVERRIDE_OPTIONS. */
4411 &ieee_quad_format /* TFmode */
4415 /* Calculate the square root of X in mode MODE, and store the result
4416 in R. Return TRUE if the operation does not raise an exception.
4417 For details see "High Precision Division and Square Root",
4418 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4419 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4422 real_sqrt (r, mode, x)
4424 enum machine_mode mode;
4425 const REAL_VALUE_TYPE *x;
4427 static REAL_VALUE_TYPE halfthree;
4428 static bool init = false;
4429 REAL_VALUE_TYPE h, t, i;
4432 /* sqrt(-0.0) is -0.0. */
4433 if (real_isnegzero (x))
4439 /* Negative arguments return NaN. */
4442 /* Mode is ignored for canonical NaN. */
4443 real_nan (r, "", 1, SFmode);
4447 /* Infinity and NaN return themselves. */
4448 if (real_isinf (x) || real_isnan (x))
4456 real_arithmetic (&halfthree, PLUS_EXPR, &dconst1, &dconsthalf);
4460 /* Initial guess for reciprocal sqrt, i. */
4461 exp = real_exponent (x);
4462 real_ldexp (&i, &dconst1, -exp/2);
4464 /* Newton's iteration for reciprocal sqrt, i. */
4465 for (iter = 0; iter < 16; iter++)
4467 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4468 real_arithmetic (&t, MULT_EXPR, x, &i);
4469 real_arithmetic (&h, MULT_EXPR, &t, &i);
4470 real_arithmetic (&t, MULT_EXPR, &h, &dconsthalf);
4471 real_arithmetic (&h, MINUS_EXPR, &halfthree, &t);
4472 real_arithmetic (&t, MULT_EXPR, &i, &h);
4474 /* Check for early convergence. */
4475 if (iter >= 6 && real_identical (&i, &t))
4478 /* ??? Unroll loop to avoid copying. */
4482 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4483 real_arithmetic (&t, MULT_EXPR, x, &i);
4484 real_arithmetic (&h, MULT_EXPR, &t, &i);
4485 real_arithmetic (&i, MINUS_EXPR, &dconst1, &h);
4486 real_arithmetic (&h, MULT_EXPR, &t, &i);
4487 real_arithmetic (&i, MULT_EXPR, &dconsthalf, &h);
4488 real_arithmetic (&h, PLUS_EXPR, &t, &i);
4490 /* ??? We need a Tuckerman test to get the last bit. */
4492 real_convert (r, mode, &h);