1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2002 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
31 /* The floating point model used internally is not exactly IEEE 754
32 compliant, and close to the description in the ISO C standard,
33 section 5.2.4.2.2 Characteristics of floating types.
37 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
41 b = base or radix, here always 2
43 p = precision (the number of base-b digits in the significand)
44 f_k = the digits of the significand.
46 We differ from typical IEEE 754 encodings in that the entire
47 significand is fractional. Normalized significands are in the
50 A requirement of the model is that P be larger than than the
51 largest supported target floating-point type by at least 2 bits.
52 This gives us proper rounding when we truncate to the target type.
53 In addition, E must be large enough to hold the smallest supported
54 denormal number in a normalized form.
56 Both of these requirements are easily satisfied. The largest
57 target significand is 113 bits; we store 128. The smallest
58 denormal number fits in 17 exponent bits; we store 29.
60 Target floating point models that use base 16 instead of base 2
61 (i.e. IBM 370), are handled during round_for_format, in which we
62 canonicalize the exponent to be a multiple of 4 (log2(16)), and
63 adjust the significand to match. */
66 /* Used to classify two numbers simultaneously. */
67 #define CLASS2(A, B) ((A) << 2 | (B))
69 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
70 #error "Some constant folding done by hand to avoid shift count warnings"
73 static void get_zero PARAMS ((REAL_VALUE_TYPE *, int));
74 static void get_canonical_qnan PARAMS ((REAL_VALUE_TYPE *, int));
75 static void get_canonical_snan PARAMS ((REAL_VALUE_TYPE *, int));
76 static void get_inf PARAMS ((REAL_VALUE_TYPE *, int));
77 static void sticky_rshift_significand PARAMS ((REAL_VALUE_TYPE *,
78 const REAL_VALUE_TYPE *,
80 static void rshift_significand PARAMS ((REAL_VALUE_TYPE *,
81 const REAL_VALUE_TYPE *,
83 static void lshift_significand PARAMS ((REAL_VALUE_TYPE *,
84 const REAL_VALUE_TYPE *,
86 static void lshift_significand_1 PARAMS ((REAL_VALUE_TYPE *,
87 const REAL_VALUE_TYPE *));
88 static bool add_significands PARAMS ((REAL_VALUE_TYPE *r,
89 const REAL_VALUE_TYPE *,
90 const REAL_VALUE_TYPE *));
91 static bool sub_significands PARAMS ((REAL_VALUE_TYPE *,
92 const REAL_VALUE_TYPE *,
93 const REAL_VALUE_TYPE *));
94 static void neg_significand PARAMS ((REAL_VALUE_TYPE *,
95 const REAL_VALUE_TYPE *));
96 static int cmp_significands PARAMS ((const REAL_VALUE_TYPE *,
97 const REAL_VALUE_TYPE *));
98 static void set_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
99 static void clear_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
100 static bool test_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
101 static void clear_significand_below PARAMS ((REAL_VALUE_TYPE *,
103 static bool div_significands PARAMS ((REAL_VALUE_TYPE *,
104 const REAL_VALUE_TYPE *,
105 const REAL_VALUE_TYPE *));
106 static void normalize PARAMS ((REAL_VALUE_TYPE *));
108 static void do_add PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
109 const REAL_VALUE_TYPE *, int));
110 static void do_multiply PARAMS ((REAL_VALUE_TYPE *,
111 const REAL_VALUE_TYPE *,
112 const REAL_VALUE_TYPE *));
113 static void do_divide PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
114 const REAL_VALUE_TYPE *));
115 static int do_compare PARAMS ((const REAL_VALUE_TYPE *,
116 const REAL_VALUE_TYPE *, int));
117 static void do_fix_trunc PARAMS ((REAL_VALUE_TYPE *,
118 const REAL_VALUE_TYPE *));
120 static const REAL_VALUE_TYPE * ten_to_ptwo PARAMS ((int));
121 static const REAL_VALUE_TYPE * real_digit PARAMS ((int));
123 static void round_for_format PARAMS ((const struct real_format *,
126 /* Initialize R with a positive zero. */
133 memset (r, 0, sizeof (*r));
137 /* Initialize R with the canonical quiet NaN. */
140 get_canonical_qnan (r, sign)
144 memset (r, 0, sizeof (*r));
147 r->sig[SIGSZ-1] = SIG_MSB >> 1;
151 get_canonical_snan (r, sign)
155 memset (r, 0, sizeof (*r));
158 r->sig[SIGSZ-1] = SIG_MSB >> 2;
166 memset (r, 0, sizeof (*r));
172 /* Right-shift the significand of A by N bits; put the result in the
173 significand of R. If any one bits are shifted out, set the least
174 significant bit of R. */
177 sticky_rshift_significand (r, a, n)
179 const REAL_VALUE_TYPE *a;
182 unsigned long sticky = 0;
183 unsigned int i, ofs = 0;
185 if (n >= HOST_BITS_PER_LONG)
187 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
189 n -= ofs * HOST_BITS_PER_LONG;
194 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
195 for (i = 0; i < SIGSZ; ++i)
198 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
199 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
200 << (HOST_BITS_PER_LONG - n)));
205 for (i = 0; ofs + i < SIGSZ; ++i)
206 r->sig[i] = a->sig[ofs + i];
207 for (; i < SIGSZ; ++i)
211 r->sig[0] |= (sticky != 0);
214 /* Right-shift the significand of A by N bits; put the result in the
218 rshift_significand (r, a, n)
220 const REAL_VALUE_TYPE *a;
223 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
225 n -= ofs * HOST_BITS_PER_LONG;
228 for (i = 0; i < SIGSZ; ++i)
231 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
232 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
233 << (HOST_BITS_PER_LONG - n)));
238 for (i = 0; ofs + i < SIGSZ; ++i)
239 r->sig[i] = a->sig[ofs + i];
240 for (; i < SIGSZ; ++i)
245 /* Left-shift the significand of A by N bits; put the result in the
249 lshift_significand (r, a, n)
251 const REAL_VALUE_TYPE *a;
254 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
256 n -= ofs * HOST_BITS_PER_LONG;
259 for (i = 0; ofs + i < SIGSZ; ++i)
260 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
261 for (; i < SIGSZ; ++i)
262 r->sig[SIGSZ-1-i] = 0;
265 for (i = 0; i < SIGSZ; ++i)
268 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
269 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
270 >> (HOST_BITS_PER_LONG - n)));
274 /* Likewise, but N is specialized to 1. */
277 lshift_significand_1 (r, a)
279 const REAL_VALUE_TYPE *a;
283 for (i = SIGSZ - 1; i > 0; --i)
284 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
285 r->sig[0] = a->sig[0] << 1;
288 /* Add the significands of A and B, placing the result in R. Return
289 true if there was carry out of the most significant word. */
292 add_significands (r, a, b)
294 const REAL_VALUE_TYPE *a, *b;
299 for (i = 0; i < SIGSZ; ++i)
301 unsigned long ai = a->sig[i];
302 unsigned long ri = ai + b->sig[i];
318 /* Subtract the significands of A and B, placing the result in R.
319 Return true if there was carry out of the most significant word. */
322 sub_significands (r, a, b)
324 const REAL_VALUE_TYPE *a, *b;
329 for (i = 0; i < SIGSZ; ++i)
331 unsigned long ai = a->sig[i];
332 unsigned long ri = ai - b->sig[i];
348 /* Negate the significand A, placing the result in R. */
351 neg_significand (r, a)
353 const REAL_VALUE_TYPE *a;
358 for (i = 0; i < SIGSZ; ++i)
360 unsigned long ri, ai = a->sig[i];
379 /* Compare significands. Return tri-state vs zero. */
382 cmp_significands (a, b)
383 const REAL_VALUE_TYPE *a, *b;
387 for (i = SIGSZ - 1; i >= 0; --i)
389 unsigned long ai = a->sig[i];
390 unsigned long bi = b->sig[i];
401 /* Set bit N of the significand of R. */
404 set_significand_bit (r, n)
408 r->sig[n / HOST_BITS_PER_LONG]
409 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
412 /* Clear bit N of the significand of R. */
415 clear_significand_bit (r, n)
419 r->sig[n / HOST_BITS_PER_LONG]
420 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
423 /* Test bit N of the significand of R. */
426 test_significand_bit (r, n)
430 /* ??? Compiler bug here if we return this expression directly.
431 The conversion to bool strips the "&1" and we wind up testing
432 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
433 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
437 /* Clear bits 0..N-1 of the significand of R. */
440 clear_significand_below (r, n)
444 int i, w = n / HOST_BITS_PER_LONG;
446 for (i = 0; i < w; ++i)
449 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
452 /* Divide the significands of A and B, placing the result in R. Return
453 true if the division was inexact. */
456 div_significands (r, a, b)
458 const REAL_VALUE_TYPE *a, *b;
461 int bit = SIGNIFICAND_BITS - 1;
466 memset (r->sig, 0, sizeof (r->sig));
471 if ((u.sig[SIGSZ-1] & SIG_MSB) == 0)
473 lshift_significand_1 (&u, &u);
475 if (cmp_significands (&u, b) >= 0)
477 sub_significands (&u, &u, b);
478 set_significand_bit (r, bit);
483 /* We lose a bit here, and thus know the next quotient bit
485 lshift_significand_1 (&u, &u);
486 sub_significands (&u, &u, b);
487 set_significand_bit (r, bit);
492 for (i = 0, inexact = 0; i < SIGSZ; i++)
498 /* Adjust the exponent and significand of R such that the most
499 significant bit is set. We underflow to zero and overflow to
500 infinity here, without denormals. (The intermediate representation
501 exponent is large enough to handle target denormals normalized.) */
510 /* Find the first word that is nonzero. */
511 for (i = SIGSZ - 1; i >= 0; i--)
513 shift += HOST_BITS_PER_LONG;
517 /* Zero significand flushes to zero. */
525 /* Find the first bit that is nonzero. */
527 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
533 exp = r->exp - shift;
535 get_inf (r, r->sign);
536 else if (exp < -MAX_EXP)
537 get_zero (r, r->sign);
541 lshift_significand (r, r, shift);
546 /* Return R = A + (SUBTRACT_P ? -B : B). */
549 do_add (r, a, b, subtract_p)
551 const REAL_VALUE_TYPE *a, *b;
557 /* Determine if we need to add or subtract. */
559 subtract_p = (sign ^ b->sign) ^ subtract_p;
561 switch (CLASS2 (a->class, b->class))
563 case CLASS2 (rvc_zero, rvc_zero):
564 /* +-0 +/- +-0 = +0. */
568 case CLASS2 (rvc_zero, rvc_normal):
569 case CLASS2 (rvc_zero, rvc_inf):
570 case CLASS2 (rvc_zero, rvc_nan):
572 case CLASS2 (rvc_normal, rvc_nan):
573 case CLASS2 (rvc_inf, rvc_nan):
574 case CLASS2 (rvc_nan, rvc_nan):
575 /* ANY + NaN = NaN. */
576 case CLASS2 (rvc_normal, rvc_inf):
579 r->sign = sign ^ subtract_p;
582 case CLASS2 (rvc_normal, rvc_zero):
583 case CLASS2 (rvc_inf, rvc_zero):
584 case CLASS2 (rvc_nan, rvc_zero):
586 case CLASS2 (rvc_nan, rvc_normal):
587 case CLASS2 (rvc_nan, rvc_inf):
588 /* NaN + ANY = NaN. */
589 case CLASS2 (rvc_inf, rvc_normal):
594 case CLASS2 (rvc_inf, rvc_inf):
596 /* Inf - Inf = NaN. */
597 get_canonical_qnan (r, 0);
599 /* Inf + Inf = Inf. */
603 case CLASS2 (rvc_normal, rvc_normal):
610 /* Swap the arguments such that A has the larger exponent. */
611 dexp = a->exp - b->exp;
614 const REAL_VALUE_TYPE *t;
621 /* If the exponents are not identical, we need to shift the
622 significand of B down. */
625 /* If the exponents are too far apart, the significands
626 do not overlap, which makes the subtraction a noop. */
627 if (dexp >= SIGNIFICAND_BITS)
634 sticky_rshift_significand (&t, b, dexp);
640 if (sub_significands (r, a, b))
642 /* We got a borrow out of the subtraction. That means that
643 A and B had the same exponent, and B had the larger
644 significand. We need to swap the sign and negate the
647 neg_significand (r, r);
652 if (add_significands (r, a, b))
654 /* We got carry out of the addition. This means we need to
655 shift the significand back down one bit and increase the
657 sticky_rshift_significand (r, r, 1);
658 r->sig[SIGSZ-1] |= SIG_MSB;
667 r->class = rvc_normal;
671 /* Re-normalize the result. */
674 /* Special case: if the subtraction results in zero, the result
676 if (r->class == rvc_zero)
680 /* Return R = A * B. */
683 do_multiply (r, a, b)
685 const REAL_VALUE_TYPE *a, *b;
687 REAL_VALUE_TYPE u, t, *rr;
688 unsigned int i, j, k;
689 int sign = a->sign ^ b->sign;
691 switch (CLASS2 (a->class, b->class))
693 case CLASS2 (rvc_zero, rvc_zero):
694 case CLASS2 (rvc_zero, rvc_normal):
695 case CLASS2 (rvc_normal, rvc_zero):
696 /* +-0 * ANY = 0 with appropriate sign. */
700 case CLASS2 (rvc_zero, rvc_nan):
701 case CLASS2 (rvc_normal, rvc_nan):
702 case CLASS2 (rvc_inf, rvc_nan):
703 case CLASS2 (rvc_nan, rvc_nan):
704 /* ANY * NaN = NaN. */
709 case CLASS2 (rvc_nan, rvc_zero):
710 case CLASS2 (rvc_nan, rvc_normal):
711 case CLASS2 (rvc_nan, rvc_inf):
712 /* NaN * ANY = NaN. */
717 case CLASS2 (rvc_zero, rvc_inf):
718 case CLASS2 (rvc_inf, rvc_zero):
720 get_canonical_qnan (r, sign);
723 case CLASS2 (rvc_inf, rvc_inf):
724 case CLASS2 (rvc_normal, rvc_inf):
725 case CLASS2 (rvc_inf, rvc_normal):
726 /* Inf * Inf = Inf, R * Inf = Inf */
731 case CLASS2 (rvc_normal, rvc_normal):
738 if (r == a || r == b)
744 /* Collect all the partial products. Since we don't have sure access
745 to a widening multiply, we split each long into two half-words.
747 Consider the long-hand form of a four half-word multiplication:
757 We construct partial products of the widened half-word products
758 that are known to not overlap, e.g. DF+DH. Each such partial
759 product is given its proper exponent, which allows us to sum them
760 and obtain the finished product. */
762 for (i = 0; i < SIGSZ * 2; ++i)
764 unsigned long ai = a->sig[i / 2];
766 ai >>= HOST_BITS_PER_LONG / 2;
768 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
773 for (j = 0; j < 2; ++j)
775 int exp = (a->exp - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
776 + (b->exp - (1-j)*(HOST_BITS_PER_LONG/2)));
781 /* Would underflow to zero, which we shouldn't bother adding. */
784 u.class = rvc_normal;
788 for (k = j; k < SIGSZ * 2; k += 2)
790 unsigned long bi = b->sig[k / 2];
792 bi >>= HOST_BITS_PER_LONG / 2;
794 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
796 u.sig[k / 2] = ai * bi;
800 do_add (rr, rr, &u, 0);
809 /* Return R = A / B. */
814 const REAL_VALUE_TYPE *a, *b;
816 int exp, sign = a->sign ^ b->sign;
817 REAL_VALUE_TYPE t, *rr;
820 switch (CLASS2 (a->class, b->class))
822 case CLASS2 (rvc_zero, rvc_zero):
824 case CLASS2 (rvc_inf, rvc_zero):
826 case CLASS2 (rvc_inf, rvc_inf):
827 /* Inf / Inf = NaN. */
828 get_canonical_qnan (r, sign);
831 case CLASS2 (rvc_zero, rvc_normal):
832 case CLASS2 (rvc_zero, rvc_inf):
834 case CLASS2 (rvc_normal, rvc_inf):
840 case CLASS2 (rvc_normal, rvc_zero):
845 case CLASS2 (rvc_zero, rvc_nan):
846 case CLASS2 (rvc_normal, rvc_nan):
847 case CLASS2 (rvc_inf, rvc_nan):
848 case CLASS2 (rvc_nan, rvc_nan):
849 /* ANY / NaN = NaN. */
854 case CLASS2 (rvc_nan, rvc_zero):
855 case CLASS2 (rvc_nan, rvc_normal):
856 case CLASS2 (rvc_nan, rvc_inf):
857 /* NaN / ANY = NaN. */
862 case CLASS2 (rvc_inf, rvc_normal):
868 case CLASS2 (rvc_normal, rvc_normal):
875 if (r == a || r == b)
880 rr->class = rvc_normal;
883 exp = a->exp - b->exp + 1;
890 inexact = div_significands (rr, a, b);
891 rr->sig[0] |= inexact;
893 /* Re-normalize the result. */
900 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
901 one of the two operands is a NaN. */
904 do_compare (a, b, nan_result)
905 const REAL_VALUE_TYPE *a, *b;
910 switch (CLASS2 (a->class, b->class))
912 case CLASS2 (rvc_zero, rvc_zero):
913 /* Sign of zero doesn't matter for compares. */
916 case CLASS2 (rvc_inf, rvc_zero):
917 case CLASS2 (rvc_inf, rvc_normal):
918 case CLASS2 (rvc_normal, rvc_zero):
919 return (a->sign ? -1 : 1);
921 case CLASS2 (rvc_inf, rvc_inf):
922 return -a->sign - -b->sign;
924 case CLASS2 (rvc_zero, rvc_normal):
925 case CLASS2 (rvc_zero, rvc_inf):
926 case CLASS2 (rvc_normal, rvc_inf):
927 return (b->sign ? 1 : -1);
929 case CLASS2 (rvc_zero, rvc_nan):
930 case CLASS2 (rvc_normal, rvc_nan):
931 case CLASS2 (rvc_inf, rvc_nan):
932 case CLASS2 (rvc_nan, rvc_nan):
933 case CLASS2 (rvc_nan, rvc_zero):
934 case CLASS2 (rvc_nan, rvc_normal):
935 case CLASS2 (rvc_nan, rvc_inf):
938 case CLASS2 (rvc_normal, rvc_normal):
945 if (a->sign != b->sign)
946 return -a->sign - -b->sign;
950 else if (a->exp < b->exp)
953 ret = cmp_significands (a, b);
955 return (a->sign ? -ret : ret);
958 /* Return A truncated to an integral value toward zero. */
963 const REAL_VALUE_TYPE *a;
976 get_zero (r, r->sign);
977 else if (r->exp < SIGNIFICAND_BITS)
978 clear_significand_below (r, SIGNIFICAND_BITS - r->exp);
986 /* Perform the binary or unary operation described by CODE.
987 For a unary operation, leave OP1 NULL. */
990 real_arithmetic (r, icode, op0, op1)
993 const REAL_VALUE_TYPE *op0, *op1;
995 enum tree_code code = icode;
1000 do_add (r, op0, op1, 0);
1004 do_add (r, op0, op1, 1);
1008 do_multiply (r, op0, op1);
1012 do_divide (r, op0, op1);
1016 if (op1->class == rvc_nan)
1018 else if (do_compare (op0, op1, -1) < 0)
1025 if (op1->class == rvc_nan)
1027 else if (do_compare (op0, op1, 1) < 0)
1043 case FIX_TRUNC_EXPR:
1044 do_fix_trunc (r, op0);
1052 /* Legacy. Similar, but return the result directly. */
1055 real_arithmetic2 (icode, op0, op1)
1057 const REAL_VALUE_TYPE *op0, *op1;
1060 real_arithmetic (&r, icode, op0, op1);
1065 real_compare (icode, op0, op1)
1067 const REAL_VALUE_TYPE *op0, *op1;
1069 enum tree_code code = icode;
1074 return do_compare (op0, op1, 1) < 0;
1076 return do_compare (op0, op1, 1) <= 0;
1078 return do_compare (op0, op1, -1) > 0;
1080 return do_compare (op0, op1, -1) >= 0;
1082 return do_compare (op0, op1, -1) == 0;
1084 return do_compare (op0, op1, -1) != 0;
1085 case UNORDERED_EXPR:
1086 return op0->class == rvc_nan || op1->class == rvc_nan;
1088 return op0->class != rvc_nan && op1->class != rvc_nan;
1090 return do_compare (op0, op1, -1) < 0;
1092 return do_compare (op0, op1, -1) <= 0;
1094 return do_compare (op0, op1, 1) > 0;
1096 return do_compare (op0, op1, 1) >= 0;
1098 return do_compare (op0, op1, 0) == 0;
1105 /* Return floor log2(R). */
1109 const REAL_VALUE_TYPE *r;
1117 return (unsigned int)-1 >> 1;
1125 /* R = OP0 * 2**EXP. */
1128 real_ldexp (r, op0, exp)
1130 const REAL_VALUE_TYPE *op0;
1144 get_inf (r, r->sign);
1145 else if (exp < -MAX_EXP)
1146 get_zero (r, r->sign);
1156 /* Determine whether a floating-point value X is infinite. */
1160 const REAL_VALUE_TYPE *r;
1162 return (r->class == rvc_inf);
1165 /* Determine whether a floating-point value X is a NaN. */
1169 const REAL_VALUE_TYPE *r;
1171 return (r->class == rvc_nan);
1174 /* Determine whether a floating-point value X is negative. */
1178 const REAL_VALUE_TYPE *r;
1183 /* Determine whether a floating-point value X is minus zero. */
1187 const REAL_VALUE_TYPE *r;
1189 return r->sign && r->class == rvc_zero;
1192 /* Compare two floating-point objects for bitwise identity. */
1195 real_identical (a, b)
1196 const REAL_VALUE_TYPE *a, *b;
1200 if (a->class != b->class)
1202 if (a->sign != b->sign)
1212 if (a->exp != b->exp)
1216 for (i = 0; i < SIGSZ; ++i)
1217 if (a->sig[i] != b->sig[i])
1228 /* Try to change R into its exact multiplicative inverse in machine
1229 mode MODE. Return true if successful. */
1232 exact_real_inverse (mode, r)
1233 enum machine_mode mode;
1236 const REAL_VALUE_TYPE *one = real_digit (1);
1240 if (r->class != rvc_normal)
1243 /* Check for a power of two: all significand bits zero except the MSB. */
1244 for (i = 0; i < SIGSZ-1; ++i)
1247 if (r->sig[SIGSZ-1] != SIG_MSB)
1250 /* Find the inverse and truncate to the required mode. */
1251 do_divide (&u, one, r);
1252 real_convert (&u, mode, &u);
1254 /* The rounding may have overflowed. */
1255 if (u.class != rvc_normal)
1257 for (i = 0; i < SIGSZ-1; ++i)
1260 if (u.sig[SIGSZ-1] != SIG_MSB)
1267 /* Render R as an integer. */
1271 const REAL_VALUE_TYPE *r;
1273 unsigned HOST_WIDE_INT i;
1284 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1292 if (r->exp > HOST_BITS_PER_WIDE_INT)
1295 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1296 i = r->sig[SIGSZ-1];
1297 else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1299 i = r->sig[SIGSZ-1];
1300 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1301 i |= r->sig[SIGSZ-2];
1306 i >>= HOST_BITS_PER_WIDE_INT - r->exp;
1317 /* Likewise, but to an integer pair, HI+LOW. */
1320 real_to_integer2 (plow, phigh, r)
1321 HOST_WIDE_INT *plow, *phigh;
1322 const REAL_VALUE_TYPE *r;
1325 HOST_WIDE_INT low, high;
1338 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1352 if (exp >= 2*HOST_BITS_PER_WIDE_INT)
1355 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1356 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1358 high = t.sig[SIGSZ-1];
1359 low = t.sig[SIGSZ-2];
1361 else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1363 high = t.sig[SIGSZ-1];
1364 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1365 high |= t.sig[SIGSZ-2];
1367 low = t.sig[SIGSZ-3];
1368 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1369 low |= t.sig[SIGSZ-4];
1379 low = -low, high = ~high;
1391 /* Render R as a decimal floating point constant. Emit DIGITS significant
1392 digits in the result. If DIGITS <= 0, choose the maximum for the
1393 representation. If DIGITS < 0, strip trailing zeros. */
1395 #define M_LOG10_2 0.30102999566398119521
1398 real_to_decimal (str, r_orig, digits)
1400 const REAL_VALUE_TYPE *r_orig;
1404 const REAL_VALUE_TYPE *one, *ten;
1405 int dec_exp, max_digits, d, cmp_half;
1406 char *p, *first, *last;
1408 bool crop_trailing_zeros;
1414 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1419 strcpy (str, (r.sign ? "+Inf" : "-Inf"));
1422 /* ??? Print the significand as well, if not canonical? */
1423 strcpy (str, (r.sign ? "+NaN" : "-NaN"));
1429 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1430 crop_trailing_zeros = digits < 0;
1431 if (digits <= 0 || digits > max_digits)
1432 digits = max_digits;
1434 one = real_digit (1);
1435 ten = ten_to_ptwo (0);
1440 /* Estimate the decimal exponent. */
1441 dec_exp = r.exp * M_LOG10_2;
1443 /* Scale the number such that it is in [1, 10). */
1447 for (i = EXP_BITS - 1; i >= 0; --i)
1448 if (dec_exp & (1 << i))
1449 do_divide (&r, &r, ten_to_ptwo (i));
1451 else if (dec_exp < 0)
1453 int i, pos_exp = -(--dec_exp);
1455 for (i = EXP_BITS - 1; i >= 0; --i)
1456 if (pos_exp & (1 << i))
1457 do_multiply (&r, &r, ten_to_ptwo (i));
1460 /* Assert that the number is in the proper range. Round-off can
1461 prevent the above from working exactly. */
1462 if (do_compare (&r, one, -1) < 0)
1464 do_multiply (&r, &r, ten);
1467 else if (do_compare (&r, ten, 1) >= 0)
1469 do_divide (&r, &r, ten);
1479 d = real_to_integer ((const REAL_VALUE_TYPE *) &r);
1480 do_add (&r, &r, real_digit (d), 1);
1485 do_multiply (&r, &r, ten);
1489 /* Round the result. Compare R vs 0.5 by doing R*2 vs 1.0. */
1491 cmp_half = do_compare (&r, one, -1);
1493 /* Round to even. */
1516 first[0] = first[1];
1519 if (crop_trailing_zeros)
1520 while (last > first + 3 && last[-1] == '0')
1523 sprintf (last, "e%+d", dec_exp);
1526 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1527 significant digits in the result. If DIGITS <= 0, choose the maximum
1528 for the representation. If DIGITS < 0, strip trailing zeros. */
1531 real_to_hexadecimal (str, r, digits)
1533 const REAL_VALUE_TYPE *r;
1536 int i, j, exp = r->exp;
1538 bool crop_trailing_zeros;
1548 strcpy (str, (r->sign ? "+Inf" : "-Inf"));
1551 /* ??? Print the significand as well, if not canonical? */
1552 strcpy (str, (r->sign ? "+NaN" : "-NaN"));
1558 crop_trailing_zeros = digits < 0;
1560 digits = SIGNIFICAND_BITS / 4;
1571 for (i = SIGSZ - 1; i >= 0; --i)
1572 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1574 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1580 if (crop_trailing_zeros)
1581 while (p > first + 2 && p[-1] == '0')
1584 sprintf (p, "p%+d", exp);
1587 /* Initialize R from a decimal or hexadecimal string. The string is
1588 assumed to have been syntax checked already. */
1591 real_from_string (r, str)
1605 else if (*str == '+')
1608 if (str[0] == '0' && str[1] == 'x')
1610 /* Hexadecimal floating point. */
1611 int pos = SIGNIFICAND_BITS - 4, d;
1619 d = hex_value (*str);
1624 r->sig[pos / HOST_BITS_PER_LONG]
1625 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1634 if (pos == SIGNIFICAND_BITS - 4)
1641 d = hex_value (*str);
1646 r->sig[pos / HOST_BITS_PER_LONG]
1647 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1653 if (*str == 'p' || *str == 'P')
1655 bool exp_neg = false;
1663 else if (*str == '+')
1667 while (ISDIGIT (*str))
1673 /* Overflowed the exponent. */
1687 r->class = rvc_normal;
1694 /* Decimal floating point. */
1695 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1700 while (ISDIGIT (*str))
1703 do_multiply (r, r, ten);
1705 do_add (r, r, real_digit (d), 0);
1710 if (r->class == rvc_zero)
1715 while (ISDIGIT (*str))
1718 do_multiply (r, r, ten);
1720 do_add (r, r, real_digit (d), 0);
1725 if (*str == 'e' || *str == 'E')
1727 bool exp_neg = false;
1735 else if (*str == '+')
1739 while (ISDIGIT (*str))
1745 /* Overflowed the exponent. */
1761 for (d = 0; d < EXP_BITS; ++d)
1763 do_divide (r, r, ten_to_ptwo (d));
1767 for (d = 0; d < EXP_BITS; ++d)
1769 do_multiply (r, r, ten_to_ptwo (d));
1785 /* Legacy. Similar, but return the result directly. */
1788 real_from_string2 (s, mode)
1790 enum machine_mode mode;
1794 real_from_string (&r, s);
1795 if (mode != VOIDmode)
1796 real_convert (&r, mode, &r);
1801 /* Initialize R from the integer pair HIGH+LOW. */
1804 real_from_integer (r, mode, low, high, unsigned_p)
1806 enum machine_mode mode;
1807 unsigned HOST_WIDE_INT low;
1811 if (low == 0 && high == 0)
1815 r->class = rvc_normal;
1816 r->sign = high < 0 && !unsigned_p;
1817 r->exp = 2 * HOST_BITS_PER_WIDE_INT;
1828 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
1830 r->sig[SIGSZ-1] = high;
1831 r->sig[SIGSZ-2] = low;
1832 memset (r->sig, 0, sizeof(long)*(SIGSZ-2));
1834 else if (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT)
1836 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
1837 r->sig[SIGSZ-2] = high;
1838 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
1839 r->sig[SIGSZ-4] = low;
1841 memset (r->sig, 0, sizeof(long)*(SIGSZ-4));
1849 if (mode != VOIDmode)
1850 real_convert (r, mode, r);
1853 /* Returns 10**2**n. */
1855 static const REAL_VALUE_TYPE *
1859 static REAL_VALUE_TYPE tens[EXP_BITS];
1861 if (n < 0 || n >= EXP_BITS)
1864 if (tens[n].class == rvc_zero)
1866 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
1868 HOST_WIDE_INT t = 10;
1871 for (i = 0; i < n; ++i)
1874 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
1878 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
1879 do_multiply (&tens[n], t, t);
1888 static const REAL_VALUE_TYPE *
1892 static REAL_VALUE_TYPE num[10];
1897 if (n > 0 && num[n].class == rvc_zero)
1898 real_from_integer (&num[n], VOIDmode, n, 0, 1);
1903 /* Fills R with +Inf. */
1912 /* Fills R with a NaN whose significand is described by STR. If QUIET,
1913 we force a QNaN, else we force an SNaN. The string, if not empty,
1914 is parsed as a number and placed in the significand. Return true
1915 if the string was successfully parsed. */
1918 real_nan (r, str, quiet, mode)
1922 enum machine_mode mode;
1924 const struct real_format *fmt;
1926 fmt = real_format_for_mode[mode - QFmode];
1933 get_canonical_qnan (r, 0);
1935 get_canonical_snan (r, 0);
1942 memset (r, 0, sizeof (*r));
1945 /* Parse akin to strtol into the significand of R. */
1947 while (ISSPACE (*str))
1951 else if (*str == '+')
1961 while ((d = hex_value (*str)) < base)
1968 lshift_significand (r, r, 3);
1971 lshift_significand (r, r, 4);
1974 lshift_significand_1 (&u, r);
1975 lshift_significand (r, r, 3);
1976 add_significands (r, r, &u);
1984 add_significands (r, r, &u);
1989 /* Must have consumed the entire string for success. */
1993 /* Shift the significand into place such that the bits
1994 are in the most significant bits for the format. */
1995 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->p);
1997 /* Our MSB is always unset for NaNs. */
1998 r->sig[SIGSZ-1] &= ~SIG_MSB;
2000 /* Force quiet or signalling NaN. */
2002 r->sig[SIGSZ-1] |= SIG_MSB >> 1;
2004 r->sig[SIGSZ-1] &= ~(SIG_MSB >> 1);
2006 /* Force at least one bit of the significand set. */
2007 for (d = 0; d < SIGSZ; ++d)
2011 r->sig[SIGSZ-1] |= SIG_MSB >> 2;
2013 /* Our intermediate format forces QNaNs to have MSB-1 set.
2014 If the target format has QNaNs with the top bit unset,
2015 mirror the output routines and invert the top two bits. */
2016 if (!fmt->qnan_msb_set)
2017 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1) | (SIG_MSB >> 2);
2023 /* Fills R with 2**N. */
2030 memset (r, 0, sizeof (*r));
2035 else if (n < -MAX_EXP)
2039 r->class = rvc_normal;
2041 r->sig[SIGSZ-1] = SIG_MSB;
2047 round_for_format (fmt, r)
2048 const struct real_format *fmt;
2052 unsigned long sticky;
2056 p2 = fmt->p * fmt->log2_b;
2057 emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2058 emax2 = fmt->emax * fmt->log2_b;
2060 np2 = SIGNIFICAND_BITS - p2;
2064 get_zero (r, r->sign);
2066 if (!fmt->has_signed_zero)
2071 get_inf (r, r->sign);
2076 clear_significand_below (r, np2);
2078 /* If we've cleared the entire significand, we need one bit
2079 set for this to continue to be a NaN. */
2080 for (i = 0; i < SIGSZ; ++i)
2084 r->sig[SIGSZ-1] = SIG_MSB >> 2;
2094 /* If we're not base2, normalize the exponent to a multiple of
2096 if (fmt->log2_b != 1)
2098 int shift = r->exp & (fmt->log2_b - 1);
2101 shift = fmt->log2_b - shift;
2102 sticky_rshift_significand (r, r, shift);
2107 /* Check the range of the exponent. If we're out of range,
2108 either underflow or overflow. */
2111 else if (r->exp <= emin2m1)
2115 if (!fmt->has_denorm)
2117 /* Don't underflow completely until we've had a chance to round. */
2118 if (r->exp < emin2m1)
2123 diff = emin2m1 - r->exp + 1;
2127 /* De-normalize the significand. */
2128 sticky_rshift_significand (r, r, diff);
2133 /* There are P2 true significand bits, followed by one guard bit,
2134 followed by one sticky bit, followed by stuff. Fold nonzero
2135 stuff into the sticky bit. */
2138 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2139 sticky |= r->sig[i];
2141 r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2143 guard = test_significand_bit (r, np2 - 1);
2144 lsb = test_significand_bit (r, np2);
2146 /* Round to even. */
2147 if (guard && (sticky || lsb))
2151 set_significand_bit (&u, np2);
2153 if (add_significands (r, r, &u))
2155 /* Overflow. Means the significand had been all ones, and
2156 is now all zeros. Need to increase the exponent, and
2157 possibly re-normalize it. */
2158 if (++r->exp > emax2)
2160 r->sig[SIGSZ-1] = SIG_MSB;
2162 if (fmt->log2_b != 1)
2164 int shift = r->exp & (fmt->log2_b - 1);
2167 shift = fmt->log2_b - shift;
2168 sticky_rshift_significand (r, r, shift);
2177 /* Catch underflow that we deferred until after rounding. */
2178 if (r->exp <= emin2m1)
2181 /* Clear out trailing garbage. */
2182 clear_significand_below (r, np2);
2185 /* Extend or truncate to a new mode. */
2188 real_convert (r, mode, a)
2190 enum machine_mode mode;
2191 const REAL_VALUE_TYPE *a;
2193 const struct real_format *fmt;
2195 fmt = real_format_for_mode[mode - QFmode];
2200 round_for_format (fmt, r);
2202 /* round_for_format de-normalizes denormals. Undo just that part. */
2203 if (r->class == rvc_normal)
2207 /* Legacy. Likewise, except return the struct directly. */
2210 real_value_truncate (mode, a)
2211 enum machine_mode mode;
2215 real_convert (&r, mode, &a);
2219 /* Return true if truncating to MODE is exact. */
2222 exact_real_truncate (mode, a)
2223 enum machine_mode mode;
2224 const REAL_VALUE_TYPE *a;
2227 real_convert (&t, mode, a);
2228 return real_identical (&t, a);
2231 /* Write R to the given target format. Place the words of the result
2232 in target word order in BUF. There are always 32 bits in each
2233 long, no matter the size of the host long.
2235 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2238 real_to_target_fmt (buf, r_orig, fmt)
2240 const REAL_VALUE_TYPE *r_orig;
2241 const struct real_format *fmt;
2247 round_for_format (fmt, &r);
2251 (*fmt->encode) (fmt, buf, &r);
2256 /* Similar, but look up the format from MODE. */
2259 real_to_target (buf, r, mode)
2261 const REAL_VALUE_TYPE *r;
2262 enum machine_mode mode;
2264 const struct real_format *fmt;
2266 fmt = real_format_for_mode[mode - QFmode];
2270 return real_to_target_fmt (buf, r, fmt);
2273 /* Read R from the given target format. Read the words of the result
2274 in target word order in BUF. There are always 32 bits in each
2275 long, no matter the size of the host long. */
2278 real_from_target_fmt (r, buf, fmt)
2281 const struct real_format *fmt;
2283 (*fmt->decode) (fmt, r, buf);
2286 /* Similar, but look up the format from MODE. */
2289 real_from_target (r, buf, mode)
2292 enum machine_mode mode;
2294 const struct real_format *fmt;
2296 fmt = real_format_for_mode[mode - QFmode];
2300 (*fmt->decode) (fmt, r, buf);
2303 /* Return the number of bits in the significand for MODE. */
2304 /* ??? Legacy. Should get access to real_format directly. */
2307 significand_size (mode)
2308 enum machine_mode mode;
2310 const struct real_format *fmt;
2312 fmt = real_format_for_mode[mode - QFmode];
2316 return fmt->p * fmt->log2_b;
2319 /* Return a hash value for the given real value. */
2320 /* ??? The "unsigned int" return value is intended to be hashval_t,
2321 but I didn't want to pull hashtab.h into real.h. */
2325 const REAL_VALUE_TYPE *r;
2330 h = r->class | (r->sign << 2);
2342 if (sizeof(unsigned long) > sizeof(unsigned int))
2343 for (i = 0; i < SIGSZ; ++i)
2345 unsigned long s = r->sig[i];
2346 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2349 for (i = 0; i < SIGSZ; ++i)
2360 /* IEEE single-precision format. */
2362 static void encode_ieee_single PARAMS ((const struct real_format *fmt,
2363 long *, const REAL_VALUE_TYPE *));
2364 static void decode_ieee_single PARAMS ((const struct real_format *,
2365 REAL_VALUE_TYPE *, const long *));
2368 encode_ieee_single (fmt, buf, r)
2369 const struct real_format *fmt;
2371 const REAL_VALUE_TYPE *r;
2373 unsigned long image, sig, exp;
2374 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2376 image = r->sign << 31;
2377 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2388 image |= 0x7fffffff;
2396 if (!fmt->qnan_msb_set)
2397 image ^= 1 << 23 | 1 << 22;
2400 image |= 0x7fffffff;
2404 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2405 whereas the intermediate representation is 0.F x 2**exp.
2406 Which means we're off by one. */
2410 exp = r->exp + 127 - 1;
2423 decode_ieee_single (fmt, r, buf)
2424 const struct real_format *fmt;
2428 unsigned long image = buf[0] & 0xffffffff;
2429 bool sign = (image >> 31) & 1;
2430 int exp = (image >> 23) & 0xff;
2432 memset (r, 0, sizeof (*r));
2433 image <<= HOST_BITS_PER_LONG - 24;
2438 if (image && fmt->has_denorm)
2440 r->class = rvc_normal;
2443 r->sig[SIGSZ-1] = image << 1;
2446 else if (fmt->has_signed_zero)
2449 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2455 if (!fmt->qnan_msb_set)
2456 image ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2457 r->sig[SIGSZ-1] = image;
2467 r->class = rvc_normal;
2469 r->exp = exp - 127 + 1;
2470 r->sig[SIGSZ-1] = image | SIG_MSB;
2474 const struct real_format ieee_single_format =
2491 /* IEEE double-precision format. */
2493 static void encode_ieee_double PARAMS ((const struct real_format *fmt,
2494 long *, const REAL_VALUE_TYPE *));
2495 static void decode_ieee_double PARAMS ((const struct real_format *,
2496 REAL_VALUE_TYPE *, const long *));
2499 encode_ieee_double (fmt, buf, r)
2500 const struct real_format *fmt;
2502 const REAL_VALUE_TYPE *r;
2504 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2505 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2507 image_hi = r->sign << 31;
2510 if (HOST_BITS_PER_LONG == 64)
2512 sig_hi = r->sig[SIGSZ-1];
2513 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2514 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2518 sig_hi = r->sig[SIGSZ-1];
2519 sig_lo = r->sig[SIGSZ-2];
2520 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2521 sig_hi = (sig_hi >> 11) & 0xfffff;
2531 image_hi |= 2047 << 20;
2534 image_hi |= 0x7fffffff;
2535 image_lo = 0xffffffff;
2542 image_hi |= 2047 << 20;
2544 if (!fmt->qnan_msb_set)
2545 image_hi ^= 1 << 19 | 1 << 18;
2550 image_hi |= 0x7fffffff;
2551 image_lo = 0xffffffff;
2556 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2557 whereas the intermediate representation is 0.F x 2**exp.
2558 Which means we're off by one. */
2562 exp = r->exp + 1023 - 1;
2563 image_hi |= exp << 20;
2572 if (FLOAT_WORDS_BIG_ENDIAN)
2573 buf[0] = image_hi, buf[1] = image_lo;
2575 buf[0] = image_lo, buf[1] = image_hi;
2579 decode_ieee_double (fmt, r, buf)
2580 const struct real_format *fmt;
2584 unsigned long image_hi, image_lo;
2588 if (FLOAT_WORDS_BIG_ENDIAN)
2589 image_hi = buf[0], image_lo = buf[1];
2591 image_lo = buf[0], image_hi = buf[1];
2592 image_lo &= 0xffffffff;
2593 image_hi &= 0xffffffff;
2595 sign = (image_hi >> 31) & 1;
2596 exp = (image_hi >> 20) & 0x7ff;
2598 memset (r, 0, sizeof (*r));
2600 image_hi <<= 32 - 21;
2601 image_hi |= image_lo >> 21;
2602 image_hi &= 0x7fffffff;
2603 image_lo <<= 32 - 21;
2607 if ((image_hi || image_lo) && fmt->has_denorm)
2609 r->class = rvc_normal;
2612 if (HOST_BITS_PER_LONG == 32)
2614 image_hi = (image_hi << 1) | (image_lo >> 31);
2616 r->sig[SIGSZ-1] = image_hi;
2617 r->sig[SIGSZ-2] = image_lo;
2621 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2622 r->sig[SIGSZ-1] = image_hi;
2626 else if (fmt->has_signed_zero)
2629 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2631 if (image_hi || image_lo)
2635 if (HOST_BITS_PER_LONG == 32)
2637 r->sig[SIGSZ-1] = image_hi;
2638 r->sig[SIGSZ-2] = image_lo;
2641 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
2643 if (!fmt->qnan_msb_set)
2644 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2654 r->class = rvc_normal;
2656 r->exp = exp - 1023 + 1;
2657 if (HOST_BITS_PER_LONG == 32)
2659 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
2660 r->sig[SIGSZ-2] = image_lo;
2663 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2667 const struct real_format ieee_double_format =
2684 /* IEEE extended double precision format. This comes in three
2685 flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2688 static void encode_ieee_extended PARAMS ((const struct real_format *fmt,
2689 long *, const REAL_VALUE_TYPE *));
2690 static void decode_ieee_extended PARAMS ((const struct real_format *,
2691 REAL_VALUE_TYPE *, const long *));
2693 static void encode_ieee_extended_128 PARAMS ((const struct real_format *fmt,
2695 const REAL_VALUE_TYPE *));
2696 static void decode_ieee_extended_128 PARAMS ((const struct real_format *,
2701 encode_ieee_extended (fmt, buf, r)
2702 const struct real_format *fmt;
2704 const REAL_VALUE_TYPE *r;
2706 unsigned long image_hi, sig_hi, sig_lo;
2707 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2709 image_hi = r->sign << 15;
2710 sig_hi = sig_lo = 0;
2722 /* Intel requires the explicit integer bit to be set, otherwise
2723 it considers the value a "pseudo-infinity". Motorola docs
2724 say it doesn't care. */
2725 sig_hi = 0x80000000;
2730 sig_lo = sig_hi = 0xffffffff;
2738 if (HOST_BITS_PER_LONG == 32)
2740 sig_hi = r->sig[SIGSZ-1];
2741 sig_lo = r->sig[SIGSZ-2];
2745 sig_lo = r->sig[SIGSZ-1];
2746 sig_hi = sig_lo >> 31 >> 1;
2747 sig_lo &= 0xffffffff;
2749 if (!fmt->qnan_msb_set)
2750 sig_hi ^= 1 << 30 | 1 << 29;
2752 /* Intel requires the explicit integer bit to be set, otherwise
2753 it considers the value a "pseudo-nan". Motorola docs say it
2755 sig_hi |= 0x80000000;
2760 sig_lo = sig_hi = 0xffffffff;
2768 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2769 whereas the intermediate representation is 0.F x 2**exp.
2770 Which means we're off by one.
2772 Except for Motorola, which consider exp=0 and explicit
2773 integer bit set to continue to be normalized. In theory
2774 this descrepency has been taken care of by the difference
2775 in fmt->emin in round_for_format. */
2787 if (HOST_BITS_PER_LONG == 32)
2789 sig_hi = r->sig[SIGSZ-1];
2790 sig_lo = r->sig[SIGSZ-2];
2794 sig_lo = r->sig[SIGSZ-1];
2795 sig_hi = sig_lo >> 31 >> 1;
2796 sig_lo &= 0xffffffff;
2805 if (FLOAT_WORDS_BIG_ENDIAN)
2806 buf[0] = image_hi << 16, buf[1] = sig_hi, buf[2] = sig_lo;
2808 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
2812 encode_ieee_extended_128 (fmt, buf, r)
2813 const struct real_format *fmt;
2815 const REAL_VALUE_TYPE *r;
2817 buf[3 * !FLOAT_WORDS_BIG_ENDIAN] = 0;
2818 encode_ieee_extended (fmt, buf+!!FLOAT_WORDS_BIG_ENDIAN, r);
2822 decode_ieee_extended (fmt, r, buf)
2823 const struct real_format *fmt;
2827 unsigned long image_hi, sig_hi, sig_lo;
2831 if (FLOAT_WORDS_BIG_ENDIAN)
2832 image_hi = buf[0] >> 16, sig_hi = buf[1], sig_lo = buf[2];
2834 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
2835 sig_lo &= 0xffffffff;
2836 sig_hi &= 0xffffffff;
2837 image_hi &= 0xffffffff;
2839 sign = (image_hi >> 15) & 1;
2840 exp = image_hi & 0x7fff;
2842 memset (r, 0, sizeof (*r));
2846 if ((sig_hi || sig_lo) && fmt->has_denorm)
2848 r->class = rvc_normal;
2851 /* When the IEEE format contains a hidden bit, we know that
2852 it's zero at this point, and so shift up the significand
2853 and decrease the exponent to match. In this case, Motorola
2854 defines the explicit integer bit to be valid, so we don't
2855 know whether the msb is set or not. */
2857 if (HOST_BITS_PER_LONG == 32)
2859 r->sig[SIGSZ-1] = sig_hi;
2860 r->sig[SIGSZ-2] = sig_lo;
2863 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2867 else if (fmt->has_signed_zero)
2870 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
2872 /* See above re "pseudo-infinities" and "pseudo-nans".
2873 Short summary is that the MSB will likely always be
2874 set, and that we don't care about it. */
2875 sig_hi &= 0x7fffffff;
2877 if (sig_hi || sig_lo)
2881 if (HOST_BITS_PER_LONG == 32)
2883 r->sig[SIGSZ-1] = sig_hi;
2884 r->sig[SIGSZ-2] = sig_lo;
2887 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2889 if (!fmt->qnan_msb_set)
2890 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2900 r->class = rvc_normal;
2902 r->exp = exp - 16383 + 1;
2903 if (HOST_BITS_PER_LONG == 32)
2905 r->sig[SIGSZ-1] = sig_hi;
2906 r->sig[SIGSZ-2] = sig_lo;
2909 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2914 decode_ieee_extended_128 (fmt, r, buf)
2915 const struct real_format *fmt;
2919 decode_ieee_extended (fmt, r, buf+!!FLOAT_WORDS_BIG_ENDIAN);
2922 const struct real_format ieee_extended_motorola_format =
2924 encode_ieee_extended,
2925 decode_ieee_extended,
2938 const struct real_format ieee_extended_intel_96_format =
2940 encode_ieee_extended,
2941 decode_ieee_extended,
2954 const struct real_format ieee_extended_intel_128_format =
2956 encode_ieee_extended_128,
2957 decode_ieee_extended_128,
2971 /* IBM 128-bit extended precision format: a pair of IEEE double precision
2972 numbers whose sum is equal to the extended precision value. The number
2973 with greater magnitude is first. This format has the same magnitude
2974 range as an IEEE double precision value, but effectively 106 bits of
2975 significand precision. Infinity and NaN are represented by their IEEE
2976 double precision value stored in the first number, the second number is
2977 ignored. Zeroes, Infinities, and NaNs are set in both doubles
2978 due to precedent. */
2980 static void encode_ibm_extended PARAMS ((const struct real_format *fmt,
2981 long *, const REAL_VALUE_TYPE *));
2982 static void decode_ibm_extended PARAMS ((const struct real_format *,
2983 REAL_VALUE_TYPE *, const long *));
2986 encode_ibm_extended (fmt, buf, r)
2987 const struct real_format *fmt ATTRIBUTE_UNUSED;
2989 const REAL_VALUE_TYPE *r;
2991 REAL_VALUE_TYPE u, v;
2996 /* Both doubles have sign bit set. */
2997 buf[0] = FLOAT_WORDS_BIG_ENDIAN ? r->sign << 31 : 0;
2998 buf[1] = FLOAT_WORDS_BIG_ENDIAN ? 0 : r->sign << 31;
3005 /* Both doubles set to Inf / NaN. */
3006 encode_ieee_double (&ieee_double_format, &buf[0], r);
3012 /* u = IEEE double precision portion of significand. */
3014 clear_significand_below (&u, SIGNIFICAND_BITS - 53);
3016 /* v = remainder containing additional 53 bits of significand. */
3017 do_add (&v, r, &u, 1);
3019 encode_ieee_double (&ieee_double_format, &buf[0], &u);
3020 encode_ieee_double (&ieee_double_format, &buf[2], &v);
3029 decode_ibm_extended (fmt, r, buf)
3030 const struct real_format *fmt ATTRIBUTE_UNUSED;
3034 REAL_VALUE_TYPE u, v;
3036 decode_ieee_double (&ieee_double_format, &u, &buf[0]);
3038 if (u.class != rvc_zero && u.class != rvc_inf && u.class != rvc_nan)
3040 decode_ieee_double (&ieee_double_format, &v, &buf[2]);
3041 do_add (r, &u, &v, 0);
3047 const struct real_format ibm_extended_format =
3049 encode_ibm_extended,
3050 decode_ibm_extended,
3064 /* IEEE quad precision format. */
3066 static void encode_ieee_quad PARAMS ((const struct real_format *fmt,
3067 long *, const REAL_VALUE_TYPE *));
3068 static void decode_ieee_quad PARAMS ((const struct real_format *,
3069 REAL_VALUE_TYPE *, const long *));
3072 encode_ieee_quad (fmt, buf, r)
3073 const struct real_format *fmt;
3075 const REAL_VALUE_TYPE *r;
3077 unsigned long image3, image2, image1, image0, exp;
3078 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3081 image3 = r->sign << 31;
3086 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3095 image3 |= 32767 << 16;
3098 image3 |= 0x7fffffff;
3099 image2 = 0xffffffff;
3100 image1 = 0xffffffff;
3101 image0 = 0xffffffff;
3108 image3 |= 32767 << 16;
3110 if (HOST_BITS_PER_LONG == 32)
3115 image3 |= u.sig[3] & 0xffff;
3120 image1 = image0 >> 31 >> 1;
3122 image3 |= (image2 >> 31 >> 1) & 0xffff;
3123 image0 &= 0xffffffff;
3124 image2 &= 0xffffffff;
3127 if (!fmt->qnan_msb_set)
3128 image3 ^= 1 << 15 | 1 << 14;
3132 image3 |= 0x7fffffff;
3133 image2 = 0xffffffff;
3134 image1 = 0xffffffff;
3135 image0 = 0xffffffff;
3140 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3141 whereas the intermediate representation is 0.F x 2**exp.
3142 Which means we're off by one. */
3146 exp = r->exp + 16383 - 1;
3147 image3 |= exp << 16;
3149 if (HOST_BITS_PER_LONG == 32)
3154 image3 |= u.sig[3] & 0xffff;
3159 image1 = image0 >> 31 >> 1;
3161 image3 |= (image2 >> 31 >> 1) & 0xffff;
3162 image0 &= 0xffffffff;
3163 image2 &= 0xffffffff;
3171 if (FLOAT_WORDS_BIG_ENDIAN)
3188 decode_ieee_quad (fmt, r, buf)
3189 const struct real_format *fmt;
3193 unsigned long image3, image2, image1, image0;
3197 if (FLOAT_WORDS_BIG_ENDIAN)
3211 image0 &= 0xffffffff;
3212 image1 &= 0xffffffff;
3213 image2 &= 0xffffffff;
3215 sign = (image3 >> 31) & 1;
3216 exp = (image3 >> 16) & 0x7fff;
3219 memset (r, 0, sizeof (*r));
3223 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3225 r->class = rvc_normal;
3228 r->exp = -16382 + (SIGNIFICAND_BITS - 112);
3229 if (HOST_BITS_PER_LONG == 32)
3238 r->sig[0] = (image1 << 31 << 1) | image0;
3239 r->sig[1] = (image3 << 31 << 1) | image2;
3244 else if (fmt->has_signed_zero)
3247 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3249 if (image3 | image2 | image1 | image0)
3254 if (HOST_BITS_PER_LONG == 32)
3263 r->sig[0] = (image1 << 31 << 1) | image0;
3264 r->sig[1] = (image3 << 31 << 1) | image2;
3266 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3268 if (!fmt->qnan_msb_set)
3269 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
3279 r->class = rvc_normal;
3281 r->exp = exp - 16383 + 1;
3283 if (HOST_BITS_PER_LONG == 32)
3292 r->sig[0] = (image1 << 31 << 1) | image0;
3293 r->sig[1] = (image3 << 31 << 1) | image2;
3295 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3296 r->sig[SIGSZ-1] |= SIG_MSB;
3300 const struct real_format ieee_quad_format =
3316 /* Descriptions of VAX floating point formats can be found beginning at
3318 http://www.openvms.compaq.com:8000/73final/4515/4515pro_013.html#f_floating_point_format
3320 The thing to remember is that they're almost IEEE, except for word
3321 order, exponent bias, and the lack of infinities, nans, and denormals.
3323 We don't implement the H_floating format here, simply because neither
3324 the VAX or Alpha ports use it. */
3326 static void encode_vax_f PARAMS ((const struct real_format *fmt,
3327 long *, const REAL_VALUE_TYPE *));
3328 static void decode_vax_f PARAMS ((const struct real_format *,
3329 REAL_VALUE_TYPE *, const long *));
3330 static void encode_vax_d PARAMS ((const struct real_format *fmt,
3331 long *, const REAL_VALUE_TYPE *));
3332 static void decode_vax_d PARAMS ((const struct real_format *,
3333 REAL_VALUE_TYPE *, const long *));
3334 static void encode_vax_g PARAMS ((const struct real_format *fmt,
3335 long *, const REAL_VALUE_TYPE *));
3336 static void decode_vax_g PARAMS ((const struct real_format *,
3337 REAL_VALUE_TYPE *, const long *));
3340 encode_vax_f (fmt, buf, r)
3341 const struct real_format *fmt ATTRIBUTE_UNUSED;
3343 const REAL_VALUE_TYPE *r;
3345 unsigned long sign, exp, sig, image;
3347 sign = r->sign << 15;
3357 image = 0xffff7fff | sign;
3361 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3364 image = (sig << 16) & 0xffff0000;
3378 decode_vax_f (fmt, r, buf)
3379 const struct real_format *fmt ATTRIBUTE_UNUSED;
3383 unsigned long image = buf[0] & 0xffffffff;
3384 int exp = (image >> 7) & 0xff;
3386 memset (r, 0, sizeof (*r));
3390 r->class = rvc_normal;
3391 r->sign = (image >> 15) & 1;
3394 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3395 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3400 encode_vax_d (fmt, buf, r)
3401 const struct real_format *fmt ATTRIBUTE_UNUSED;
3403 const REAL_VALUE_TYPE *r;
3405 unsigned long image0, image1, sign = r->sign << 15;
3410 image0 = image1 = 0;
3415 image0 = 0xffff7fff | sign;
3416 image1 = 0xffffffff;
3420 /* Extract the significand into straight hi:lo. */
3421 if (HOST_BITS_PER_LONG == 64)
3423 image0 = r->sig[SIGSZ-1];
3424 image1 = (image0 >> (64 - 56)) & 0xffffffff;
3425 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3429 image0 = r->sig[SIGSZ-1];
3430 image1 = r->sig[SIGSZ-2];
3431 image1 = (image0 << 24) | (image1 >> 8);
3432 image0 = (image0 >> 8) & 0xffffff;
3435 /* Rearrange the half-words of the significand to match the
3437 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3438 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3440 /* Add the sign and exponent. */
3442 image0 |= (r->exp + 128) << 7;
3449 if (FLOAT_WORDS_BIG_ENDIAN)
3450 buf[0] = image1, buf[1] = image0;
3452 buf[0] = image0, buf[1] = image1;
3456 decode_vax_d (fmt, r, buf)
3457 const struct real_format *fmt ATTRIBUTE_UNUSED;
3461 unsigned long image0, image1;
3464 if (FLOAT_WORDS_BIG_ENDIAN)
3465 image1 = buf[0], image0 = buf[1];
3467 image0 = buf[0], image1 = buf[1];
3468 image0 &= 0xffffffff;
3469 image1 &= 0xffffffff;
3471 exp = (image0 >> 7) & 0x7f;
3473 memset (r, 0, sizeof (*r));
3477 r->class = rvc_normal;
3478 r->sign = (image0 >> 15) & 1;
3481 /* Rearrange the half-words of the external format into
3482 proper ascending order. */
3483 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
3484 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3486 if (HOST_BITS_PER_LONG == 64)
3488 image0 = (image0 << 31 << 1) | image1;
3491 r->sig[SIGSZ-1] = image0;
3495 r->sig[SIGSZ-1] = image0;
3496 r->sig[SIGSZ-2] = image1;
3497 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
3498 r->sig[SIGSZ-1] |= SIG_MSB;
3504 encode_vax_g (fmt, buf, r)
3505 const struct real_format *fmt ATTRIBUTE_UNUSED;
3507 const REAL_VALUE_TYPE *r;
3509 unsigned long image0, image1, sign = r->sign << 15;
3514 image0 = image1 = 0;
3519 image0 = 0xffff7fff | sign;
3520 image1 = 0xffffffff;
3524 /* Extract the significand into straight hi:lo. */
3525 if (HOST_BITS_PER_LONG == 64)
3527 image0 = r->sig[SIGSZ-1];
3528 image1 = (image0 >> (64 - 53)) & 0xffffffff;
3529 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
3533 image0 = r->sig[SIGSZ-1];
3534 image1 = r->sig[SIGSZ-2];
3535 image1 = (image0 << 21) | (image1 >> 11);
3536 image0 = (image0 >> 11) & 0xfffff;
3539 /* Rearrange the half-words of the significand to match the
3541 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
3542 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3544 /* Add the sign and exponent. */
3546 image0 |= (r->exp + 1024) << 4;
3553 if (FLOAT_WORDS_BIG_ENDIAN)
3554 buf[0] = image1, buf[1] = image0;
3556 buf[0] = image0, buf[1] = image1;
3560 decode_vax_g (fmt, r, buf)
3561 const struct real_format *fmt ATTRIBUTE_UNUSED;
3565 unsigned long image0, image1;
3568 if (FLOAT_WORDS_BIG_ENDIAN)
3569 image1 = buf[0], image0 = buf[1];
3571 image0 = buf[0], image1 = buf[1];
3572 image0 &= 0xffffffff;
3573 image1 &= 0xffffffff;
3575 exp = (image0 >> 4) & 0x7ff;
3577 memset (r, 0, sizeof (*r));
3581 r->class = rvc_normal;
3582 r->sign = (image0 >> 15) & 1;
3583 r->exp = exp - 1024;
3585 /* Rearrange the half-words of the external format into
3586 proper ascending order. */
3587 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
3588 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3590 if (HOST_BITS_PER_LONG == 64)
3592 image0 = (image0 << 31 << 1) | image1;
3595 r->sig[SIGSZ-1] = image0;
3599 r->sig[SIGSZ-1] = image0;
3600 r->sig[SIGSZ-2] = image1;
3601 lshift_significand (r, r, 64 - 53);
3602 r->sig[SIGSZ-1] |= SIG_MSB;
3607 const struct real_format vax_f_format =
3623 const struct real_format vax_d_format =
3639 const struct real_format vax_g_format =
3655 /* A good reference for these can be found in chapter 9 of
3656 "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
3657 An on-line version can be found here:
3659 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3662 static void encode_i370_single PARAMS ((const struct real_format *fmt,
3663 long *, const REAL_VALUE_TYPE *));
3664 static void decode_i370_single PARAMS ((const struct real_format *,
3665 REAL_VALUE_TYPE *, const long *));
3666 static void encode_i370_double PARAMS ((const struct real_format *fmt,
3667 long *, const REAL_VALUE_TYPE *));
3668 static void decode_i370_double PARAMS ((const struct real_format *,
3669 REAL_VALUE_TYPE *, const long *));
3672 encode_i370_single (fmt, buf, r)
3673 const struct real_format *fmt ATTRIBUTE_UNUSED;
3675 const REAL_VALUE_TYPE *r;
3677 unsigned long sign, exp, sig, image;
3679 sign = r->sign << 31;
3689 image = 0x7fffffff | sign;
3693 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
3694 exp = ((r->exp / 4) + 64) << 24;
3695 image = sign | exp | sig;
3706 decode_i370_single (fmt, r, buf)
3707 const struct real_format *fmt ATTRIBUTE_UNUSED;
3711 unsigned long sign, sig, image = buf[0];
3714 sign = (image >> 31) & 1;
3715 exp = (image >> 24) & 0x7f;
3716 sig = image & 0xffffff;
3718 memset (r, 0, sizeof (*r));
3722 r->class = rvc_normal;
3724 r->exp = (exp - 64) * 4;
3725 r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
3731 encode_i370_double (fmt, buf, r)
3732 const struct real_format *fmt ATTRIBUTE_UNUSED;
3734 const REAL_VALUE_TYPE *r;
3736 unsigned long sign, exp, image_hi, image_lo;
3738 sign = r->sign << 31;
3743 image_hi = image_lo = 0;
3748 image_hi = 0x7fffffff | sign;
3749 image_lo = 0xffffffff;
3753 if (HOST_BITS_PER_LONG == 64)
3755 image_hi = r->sig[SIGSZ-1];
3756 image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
3757 image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
3761 image_hi = r->sig[SIGSZ-1];
3762 image_lo = r->sig[SIGSZ-2];
3763 image_lo = (image_lo >> 8) | (image_hi << 24);
3767 exp = ((r->exp / 4) + 64) << 24;
3768 image_hi |= sign | exp;
3775 if (FLOAT_WORDS_BIG_ENDIAN)
3776 buf[0] = image_hi, buf[1] = image_lo;
3778 buf[0] = image_lo, buf[1] = image_hi;
3782 decode_i370_double (fmt, r, buf)
3783 const struct real_format *fmt ATTRIBUTE_UNUSED;
3787 unsigned long sign, image_hi, image_lo;
3790 if (FLOAT_WORDS_BIG_ENDIAN)
3791 image_hi = buf[0], image_lo = buf[1];
3793 image_lo = buf[0], image_hi = buf[1];
3795 sign = (image_hi >> 31) & 1;
3796 exp = (image_hi >> 24) & 0x7f;
3797 image_hi &= 0xffffff;
3798 image_lo &= 0xffffffff;
3800 memset (r, 0, sizeof (*r));
3802 if (exp || image_hi || image_lo)
3804 r->class = rvc_normal;
3806 r->exp = (exp - 64) * 4 + (SIGNIFICAND_BITS - 56);
3808 if (HOST_BITS_PER_LONG == 32)
3810 r->sig[0] = image_lo;
3811 r->sig[1] = image_hi;
3814 r->sig[0] = image_lo | (image_hi << 31 << 1);
3820 const struct real_format i370_single_format =
3831 false, /* ??? The encoding does allow for "unnormals". */
3832 false, /* ??? The encoding does allow for "unnormals". */
3836 const struct real_format i370_double_format =
3847 false, /* ??? The encoding does allow for "unnormals". */
3848 false, /* ??? The encoding does allow for "unnormals". */
3852 /* The "twos-complement" c4x format is officially defined as
3856 This is rather misleading. One must remember that F is signed.
3857 A better description would be
3859 x = -1**s * ((s + 1 + .f) * 2**e
3861 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
3862 that's -1 * (1+1+(-.5)) == -1.5. I think.
3864 The constructions here are taken from Tables 5-1 and 5-2 of the
3865 TMS320C4x User's Guide wherein step-by-step instructions for
3866 conversion from IEEE are presented. That's close enough to our
3867 internal representation so as to make things easy.
3869 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
3871 static void encode_c4x_single PARAMS ((const struct real_format *fmt,
3872 long *, const REAL_VALUE_TYPE *));
3873 static void decode_c4x_single PARAMS ((const struct real_format *,
3874 REAL_VALUE_TYPE *, const long *));
3875 static void encode_c4x_extended PARAMS ((const struct real_format *fmt,
3876 long *, const REAL_VALUE_TYPE *));
3877 static void decode_c4x_extended PARAMS ((const struct real_format *,
3878 REAL_VALUE_TYPE *, const long *));
3881 encode_c4x_single (fmt, buf, r)
3882 const struct real_format *fmt ATTRIBUTE_UNUSED;
3884 const REAL_VALUE_TYPE *r;
3886 unsigned long image, exp, sig;
3898 sig = 0x800000 - r->sign;
3903 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3918 image = ((exp & 0xff) << 24) | (sig & 0xffffff);
3923 decode_c4x_single (fmt, r, buf)
3924 const struct real_format *fmt ATTRIBUTE_UNUSED;
3928 unsigned long image = buf[0];
3932 exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
3933 sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
3935 memset (r, 0, sizeof (*r));
3939 r->class = rvc_normal;
3941 sig = sf & 0x7fffff;
3950 sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3953 r->sig[SIGSZ-1] = sig;
3958 encode_c4x_extended (fmt, buf, r)
3959 const struct real_format *fmt ATTRIBUTE_UNUSED;
3961 const REAL_VALUE_TYPE *r;
3963 unsigned long exp, sig;
3975 sig = 0x80000000 - r->sign;
3981 sig = r->sig[SIGSZ-1];
3982 if (HOST_BITS_PER_LONG == 64)
3983 sig = sig >> 1 >> 31;
4000 exp = (exp & 0xff) << 24;
4003 if (FLOAT_WORDS_BIG_ENDIAN)
4004 buf[0] = exp, buf[1] = sig;
4006 buf[0] = sig, buf[0] = exp;
4010 decode_c4x_extended (fmt, r, buf)
4011 const struct real_format *fmt ATTRIBUTE_UNUSED;
4018 if (FLOAT_WORDS_BIG_ENDIAN)
4019 exp = buf[0], sf = buf[1];
4021 sf = buf[0], exp = buf[1];
4023 exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
4024 sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
4026 memset (r, 0, sizeof (*r));
4030 r->class = rvc_normal;
4032 sig = sf & 0x7fffffff;
4041 if (HOST_BITS_PER_LONG == 64)
4042 sig = sig << 1 << 31;
4046 r->sig[SIGSZ-1] = sig;
4050 const struct real_format c4x_single_format =
4066 const struct real_format c4x_extended_format =
4068 encode_c4x_extended,
4069 decode_c4x_extended,
4082 /* Set up default mode to format mapping for IEEE. Everyone else has
4083 to set these values in OVERRIDE_OPTIONS. */
4085 const struct real_format *real_format_for_mode[TFmode - QFmode + 1] =
4090 &ieee_single_format, /* SFmode */
4091 &ieee_double_format, /* DFmode */
4093 /* We explicitly don't handle XFmode. There are two formats,
4094 pretty much equally common. Choose one in OVERRIDE_OPTIONS. */
4096 &ieee_quad_format /* TFmode */