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 /* Describes the properties of the specific target format in use. */
76 /* Move to and from the target bytes. */
77 void (*encode) (const struct real_format *, long *,
78 const REAL_VALUE_TYPE *);
79 void (*decode) (const struct real_format *, REAL_VALUE_TYPE *,
82 /* The radix of the exponent and digits of the significand. */
88 /* Size of the significand in digits of radix B. */
91 /* The minimum negative integer, x, such that b**(x-1) is normalized. */
94 /* The maximum integer, x, such that b**(x-1) is representable. */
97 /* Properties of the format. */
101 bool has_signed_zero;
106 static const struct real_format *fmt_for_mode[TFmode - QFmode + 1];
109 static void get_zero PARAMS ((REAL_VALUE_TYPE *, int));
110 static void get_canonical_qnan PARAMS ((REAL_VALUE_TYPE *, int));
111 static void get_canonical_snan PARAMS ((REAL_VALUE_TYPE *, int));
112 static void get_inf PARAMS ((REAL_VALUE_TYPE *, int));
113 static void sticky_rshift_significand PARAMS ((REAL_VALUE_TYPE *,
114 const REAL_VALUE_TYPE *,
116 static void rshift_significand PARAMS ((REAL_VALUE_TYPE *,
117 const REAL_VALUE_TYPE *,
119 static void lshift_significand PARAMS ((REAL_VALUE_TYPE *,
120 const REAL_VALUE_TYPE *,
122 static void lshift_significand_1 PARAMS ((REAL_VALUE_TYPE *,
123 const REAL_VALUE_TYPE *));
124 static bool add_significands PARAMS ((REAL_VALUE_TYPE *r,
125 const REAL_VALUE_TYPE *,
126 const REAL_VALUE_TYPE *));
127 static bool sub_significands PARAMS ((REAL_VALUE_TYPE *,
128 const REAL_VALUE_TYPE *,
129 const REAL_VALUE_TYPE *));
130 static void neg_significand PARAMS ((REAL_VALUE_TYPE *,
131 const REAL_VALUE_TYPE *));
132 static int cmp_significands PARAMS ((const REAL_VALUE_TYPE *,
133 const REAL_VALUE_TYPE *));
134 static void set_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
135 static void clear_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
136 static bool test_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
137 static void clear_significand_below PARAMS ((REAL_VALUE_TYPE *,
139 static bool div_significands PARAMS ((REAL_VALUE_TYPE *,
140 const REAL_VALUE_TYPE *,
141 const REAL_VALUE_TYPE *));
142 static void normalize PARAMS ((REAL_VALUE_TYPE *));
144 static void do_add PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
145 const REAL_VALUE_TYPE *, int));
146 static void do_multiply PARAMS ((REAL_VALUE_TYPE *,
147 const REAL_VALUE_TYPE *,
148 const REAL_VALUE_TYPE *));
149 static void do_divide PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
150 const REAL_VALUE_TYPE *));
151 static int do_compare PARAMS ((const REAL_VALUE_TYPE *,
152 const REAL_VALUE_TYPE *, int));
153 static void do_fix_trunc PARAMS ((REAL_VALUE_TYPE *,
154 const REAL_VALUE_TYPE *));
156 static const REAL_VALUE_TYPE * ten_to_ptwo PARAMS ((int));
157 static const REAL_VALUE_TYPE * real_digit PARAMS ((int));
159 static void round_for_format PARAMS ((const struct real_format *,
162 /* Initialize R with a positive zero. */
169 memset (r, 0, sizeof (*r));
173 /* Initialize R with the canonical quiet NaN. */
176 get_canonical_qnan (r, sign)
180 memset (r, 0, sizeof (*r));
183 r->sig[SIGSZ-1] = SIG_MSB >> 1;
187 get_canonical_snan (r, sign)
191 memset (r, 0, sizeof (*r));
194 r->sig[SIGSZ-1] = SIG_MSB >> 2;
202 memset (r, 0, sizeof (*r));
208 /* Right-shift the significand of A by N bits; put the result in the
209 significand of R. If any one bits are shifted out, set the least
210 significant bit of R. */
213 sticky_rshift_significand (r, a, n)
215 const REAL_VALUE_TYPE *a;
218 unsigned long sticky = 0;
219 unsigned int i, ofs = 0;
221 if (n >= HOST_BITS_PER_LONG)
223 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
225 n -= ofs * HOST_BITS_PER_LONG;
230 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
231 for (i = 0; i < SIGSZ; ++i)
234 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
235 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
236 << (HOST_BITS_PER_LONG - n)));
241 for (i = 0; ofs + i < SIGSZ; ++i)
242 r->sig[i] = a->sig[ofs + i];
243 for (; i < SIGSZ; ++i)
247 r->sig[0] |= (sticky != 0);
250 /* Right-shift the significand of A by N bits; put the result in the
254 rshift_significand (r, a, n)
256 const REAL_VALUE_TYPE *a;
259 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
261 n -= ofs * HOST_BITS_PER_LONG;
264 for (i = 0; i < SIGSZ; ++i)
267 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
268 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
269 << (HOST_BITS_PER_LONG - n)));
274 for (i = 0; ofs + i < SIGSZ; ++i)
275 r->sig[i] = a->sig[ofs + i];
276 for (; i < SIGSZ; ++i)
281 /* Left-shift the significand of A by N bits; put the result in the
285 lshift_significand (r, a, n)
287 const REAL_VALUE_TYPE *a;
290 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
292 n -= ofs * HOST_BITS_PER_LONG;
295 for (i = 0; ofs + i < SIGSZ; ++i)
296 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
297 for (; i < SIGSZ; ++i)
298 r->sig[SIGSZ-1-i] = 0;
301 for (i = 0; i < SIGSZ; ++i)
304 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
305 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
306 >> (HOST_BITS_PER_LONG - n)));
310 /* Likewise, but N is specialized to 1. */
313 lshift_significand_1 (r, a)
315 const REAL_VALUE_TYPE *a;
319 for (i = SIGSZ - 1; i > 0; --i)
320 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
321 r->sig[0] = a->sig[0] << 1;
324 /* Add the significands of A and B, placing the result in R. Return
325 true if there was carry out of the most significant word. */
328 add_significands (r, a, b)
330 const REAL_VALUE_TYPE *a, *b;
335 for (i = 0; i < SIGSZ; ++i)
337 unsigned long ai = a->sig[i];
338 unsigned long ri = ai + b->sig[i];
354 /* Subtract the significands of A and B, placing the result in R.
355 Return true if there was carry out of the most significant word. */
358 sub_significands (r, a, b)
360 const REAL_VALUE_TYPE *a, *b;
365 for (i = 0; i < SIGSZ; ++i)
367 unsigned long ai = a->sig[i];
368 unsigned long ri = ai - b->sig[i];
384 /* Negate the significand A, placing the result in R. */
387 neg_significand (r, a)
389 const REAL_VALUE_TYPE *a;
394 for (i = 0; i < SIGSZ; ++i)
396 unsigned long ri, ai = a->sig[i];
415 /* Compare significands. Return tri-state vs zero. */
418 cmp_significands (a, b)
419 const REAL_VALUE_TYPE *a, *b;
423 for (i = SIGSZ - 1; i >= 0; --i)
425 unsigned long ai = a->sig[i];
426 unsigned long bi = b->sig[i];
437 /* Set bit N of the significand of R. */
440 set_significand_bit (r, n)
444 r->sig[n / HOST_BITS_PER_LONG]
445 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
448 /* Clear bit N of the significand of R. */
451 clear_significand_bit (r, n)
455 r->sig[n / HOST_BITS_PER_LONG]
456 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
459 /* Test bit N of the significand of R. */
462 test_significand_bit (r, n)
466 /* ??? Compiler bug here if we return this expression directly.
467 The conversion to bool strips the "&1" and we wind up testing
468 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
469 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
473 /* Clear bits 0..N-1 of the significand of R. */
476 clear_significand_below (r, n)
480 int i, w = n / HOST_BITS_PER_LONG;
482 for (i = 0; i < w; ++i)
485 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
488 /* Divide the significands of A and B, placing the result in R. Return
489 true if the division was inexact. */
492 div_significands (r, a, b)
494 const REAL_VALUE_TYPE *a, *b;
497 int bit = SIGNIFICAND_BITS - 1;
502 memset (r->sig, 0, sizeof (r->sig));
507 if ((u.sig[SIGSZ-1] & SIG_MSB) == 0)
509 lshift_significand_1 (&u, &u);
511 if (cmp_significands (&u, b) >= 0)
513 sub_significands (&u, &u, b);
514 set_significand_bit (r, bit);
519 /* We lose a bit here, and thus know the next quotient bit
521 lshift_significand_1 (&u, &u);
522 sub_significands (&u, &u, b);
523 set_significand_bit (r, bit);
528 for (i = 0, inexact = 0; i < SIGSZ; i++)
534 /* Adjust the exponent and significand of R such that the most
535 significant bit is set. We underflow to zero and overflow to
536 infinity here, without denormals. (The intermediate representation
537 exponent is large enough to handle target denormals normalized.) */
546 /* Find the first word that is non-zero. */
547 for (i = SIGSZ - 1; i >= 0; i--)
549 shift += HOST_BITS_PER_LONG;
553 /* Zero significand flushes to zero. */
561 /* Find the first bit that is non-zero. */
563 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
569 exp = r->exp - shift;
571 get_inf (r, r->sign);
572 else if (exp < -MAX_EXP)
573 get_zero (r, r->sign);
577 lshift_significand (r, r, shift);
582 /* Return R = A + (SUBTRACT_P ? -B : B). */
585 do_add (r, a, b, subtract_p)
587 const REAL_VALUE_TYPE *a, *b;
593 /* Determine if we need to add or subtract. */
595 subtract_p = (sign ^ b->sign) ^ subtract_p;
597 switch (CLASS2 (a->class, b->class))
599 case CLASS2 (rvc_zero, rvc_zero):
600 /* +-0 +/- +-0 = +0. */
604 case CLASS2 (rvc_zero, rvc_normal):
605 case CLASS2 (rvc_zero, rvc_inf):
606 case CLASS2 (rvc_zero, rvc_nan):
608 case CLASS2 (rvc_normal, rvc_nan):
609 case CLASS2 (rvc_inf, rvc_nan):
610 case CLASS2 (rvc_nan, rvc_nan):
611 /* ANY + NaN = NaN. */
612 case CLASS2 (rvc_normal, rvc_inf):
615 r->sign = sign ^ subtract_p;
618 case CLASS2 (rvc_normal, rvc_zero):
619 case CLASS2 (rvc_inf, rvc_zero):
620 case CLASS2 (rvc_nan, rvc_zero):
622 case CLASS2 (rvc_nan, rvc_normal):
623 case CLASS2 (rvc_nan, rvc_inf):
624 /* NaN + ANY = NaN. */
625 case CLASS2 (rvc_inf, rvc_normal):
630 case CLASS2 (rvc_inf, rvc_inf):
632 /* Inf - Inf = NaN. */
633 get_canonical_qnan (r, 0);
635 /* Inf + Inf = Inf. */
639 case CLASS2 (rvc_normal, rvc_normal):
646 /* Swap the arguments such that A has the larger exponent. */
647 dexp = a->exp - b->exp;
650 const REAL_VALUE_TYPE *t;
657 /* If the exponents are not identical, we need to shift the
658 significand of B down. */
661 /* If the exponents are too far apart, the significands
662 do not overlap, which makes the subtraction a noop. */
663 if (dexp >= SIGNIFICAND_BITS)
670 sticky_rshift_significand (&t, b, dexp);
676 if (sub_significands (r, a, b))
678 /* We got a borrow out of the subtraction. That means that
679 A and B had the same exponent, and B had the larger
680 significand. We need to swap the sign and negate the
683 neg_significand (r, r);
688 if (add_significands (r, a, b))
690 /* We got carry out of the addition. This means we need to
691 shift the significand back down one bit and increase the
693 sticky_rshift_significand (r, r, 1);
694 r->sig[SIGSZ-1] |= SIG_MSB;
703 r->class = rvc_normal;
707 /* Re-normalize the result. */
710 /* Special case: if the subtraction results in zero, the result
712 if (r->class == rvc_zero)
716 /* Return R = A * B. */
719 do_multiply (r, a, b)
721 const REAL_VALUE_TYPE *a, *b;
723 REAL_VALUE_TYPE u, t, *rr;
724 unsigned int i, j, k;
725 int sign = a->sign ^ b->sign;
727 switch (CLASS2 (a->class, b->class))
729 case CLASS2 (rvc_zero, rvc_zero):
730 case CLASS2 (rvc_zero, rvc_normal):
731 case CLASS2 (rvc_normal, rvc_zero):
732 /* +-0 * ANY = 0 with appropriate sign. */
736 case CLASS2 (rvc_zero, rvc_nan):
737 case CLASS2 (rvc_normal, rvc_nan):
738 case CLASS2 (rvc_inf, rvc_nan):
739 case CLASS2 (rvc_nan, rvc_nan):
740 /* ANY * NaN = NaN. */
745 case CLASS2 (rvc_nan, rvc_zero):
746 case CLASS2 (rvc_nan, rvc_normal):
747 case CLASS2 (rvc_nan, rvc_inf):
748 /* NaN * ANY = NaN. */
753 case CLASS2 (rvc_zero, rvc_inf):
754 case CLASS2 (rvc_inf, rvc_zero):
756 get_canonical_qnan (r, sign);
759 case CLASS2 (rvc_inf, rvc_inf):
760 case CLASS2 (rvc_normal, rvc_inf):
761 case CLASS2 (rvc_inf, rvc_normal):
762 /* Inf * Inf = Inf, R * Inf = Inf */
767 case CLASS2 (rvc_normal, rvc_normal):
774 if (r == a || r == b)
780 u.class = rvc_normal;
783 /* Collect all the partial products. Since we don't have sure access
784 to a widening multiply, we split each long into two half-words.
786 Consider the long-hand form of a four half-word multiplication:
796 We construct partial products of the widened half-word products
797 that are known to not overlap, e.g. DF+DH. Each such partial
798 product is given its proper exponent, which allows us to sum them
799 and obtain the finished product. */
801 for (i = 0; i < SIGSZ * 2; ++i)
803 unsigned long ai = a->sig[i / 2];
805 ai >>= HOST_BITS_PER_LONG / 2;
807 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
812 for (j = 0; j < 2; ++j)
814 int exp = (a->exp - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
815 + (b->exp - (1-j)*(HOST_BITS_PER_LONG/2)));
820 /* Would underflow to zero, which we shouldn't bother adding. */
825 for (k = j; k < SIGSZ * 2; k += 2)
827 unsigned long bi = b->sig[k / 2];
829 bi >>= HOST_BITS_PER_LONG / 2;
831 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
833 u.sig[k / 2] = ai * bi;
836 do_add (rr, rr, &u, 0);
845 /* Return R = A / B. */
850 const REAL_VALUE_TYPE *a, *b;
852 int exp, sign = a->sign ^ b->sign;
853 REAL_VALUE_TYPE t, *rr;
856 switch (CLASS2 (a->class, b->class))
858 case CLASS2 (rvc_zero, rvc_zero):
860 case CLASS2 (rvc_inf, rvc_zero):
862 case CLASS2 (rvc_inf, rvc_inf):
863 /* Inf / Inf = NaN. */
864 get_canonical_qnan (r, sign);
867 case CLASS2 (rvc_zero, rvc_normal):
868 case CLASS2 (rvc_zero, rvc_inf):
870 case CLASS2 (rvc_normal, rvc_inf):
876 case CLASS2 (rvc_normal, rvc_zero):
881 case CLASS2 (rvc_zero, rvc_nan):
882 case CLASS2 (rvc_normal, rvc_nan):
883 case CLASS2 (rvc_inf, rvc_nan):
884 case CLASS2 (rvc_nan, rvc_nan):
885 /* ANY / NaN = NaN. */
890 case CLASS2 (rvc_nan, rvc_zero):
891 case CLASS2 (rvc_nan, rvc_normal):
892 case CLASS2 (rvc_nan, rvc_inf):
893 /* NaN / ANY = NaN. */
898 case CLASS2 (rvc_inf, rvc_normal):
904 case CLASS2 (rvc_normal, rvc_normal):
911 if (r == a || r == b)
916 rr->class = rvc_normal;
919 exp = a->exp - b->exp + 1;
926 inexact = div_significands (rr, a, b);
927 rr->sig[0] |= inexact;
929 /* Re-normalize the result. */
936 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
937 one of the two operands is a NaN. */
940 do_compare (a, b, nan_result)
941 const REAL_VALUE_TYPE *a, *b;
946 switch (CLASS2 (a->class, b->class))
948 case CLASS2 (rvc_zero, rvc_zero):
949 /* Sign of zero doesn't matter for compares. */
952 case CLASS2 (rvc_inf, rvc_zero):
953 case CLASS2 (rvc_inf, rvc_normal):
954 case CLASS2 (rvc_normal, rvc_zero):
955 return (a->sign ? -1 : 1);
957 case CLASS2 (rvc_inf, rvc_inf):
958 return -a->sign - -b->sign;
960 case CLASS2 (rvc_zero, rvc_normal):
961 case CLASS2 (rvc_zero, rvc_inf):
962 case CLASS2 (rvc_normal, rvc_inf):
963 return (b->sign ? 1 : -1);
965 case CLASS2 (rvc_zero, rvc_nan):
966 case CLASS2 (rvc_normal, rvc_nan):
967 case CLASS2 (rvc_inf, rvc_nan):
968 case CLASS2 (rvc_nan, rvc_nan):
969 case CLASS2 (rvc_nan, rvc_zero):
970 case CLASS2 (rvc_nan, rvc_normal):
971 case CLASS2 (rvc_nan, rvc_inf):
974 case CLASS2 (rvc_normal, rvc_normal):
981 if (a->sign != b->sign)
982 return -a->sign - -b->sign;
986 else if (a->exp < b->exp)
989 ret = cmp_significands (a, b);
991 return (a->sign ? -ret : ret);
994 /* Return A truncated to an integral value toward zero. */
999 const REAL_VALUE_TYPE *a;
1012 get_zero (r, r->sign);
1013 else if (r->exp < SIGNIFICAND_BITS)
1014 clear_significand_below (r, SIGNIFICAND_BITS - r->exp);
1022 /* Perform the binary or unary operation described by CODE.
1023 For a unary operation, leave OP1 NULL. */
1026 real_arithmetic (r, icode, op0, op1)
1029 const REAL_VALUE_TYPE *op0, *op1;
1031 enum tree_code code = icode;
1036 do_add (r, op0, op1, 0);
1040 do_add (r, op0, op1, 1);
1044 do_multiply (r, op0, op1);
1048 do_divide (r, op0, op1);
1052 if (op1->class == rvc_nan)
1054 else if (do_compare (op0, op1, -1) < 0)
1061 if (op1->class == rvc_nan)
1063 else if (do_compare (op0, op1, 1) < 0)
1079 case FIX_TRUNC_EXPR:
1080 do_fix_trunc (r, op0);
1088 /* Legacy. Similar, but return the result directly. */
1091 real_arithmetic2 (icode, op0, op1)
1093 const REAL_VALUE_TYPE *op0, *op1;
1096 real_arithmetic (&r, icode, op0, op1);
1101 real_compare (icode, op0, op1)
1103 const REAL_VALUE_TYPE *op0, *op1;
1105 enum tree_code code = icode;
1110 return do_compare (op0, op1, 1) < 0;
1112 return do_compare (op0, op1, 1) <= 0;
1114 return do_compare (op0, op1, -1) > 0;
1116 return do_compare (op0, op1, -1) >= 0;
1118 return do_compare (op0, op1, -1) == 0;
1120 return do_compare (op0, op1, -1) != 0;
1121 case UNORDERED_EXPR:
1122 return op0->class == rvc_nan || op1->class == rvc_nan;
1124 return op0->class != rvc_nan && op1->class != rvc_nan;
1126 return do_compare (op0, op1, -1) < 0;
1128 return do_compare (op0, op1, -1) <= 0;
1130 return do_compare (op0, op1, 1) > 0;
1132 return do_compare (op0, op1, 1) >= 0;
1134 return do_compare (op0, op1, 0) == 0;
1141 /* Return floor log2(R). */
1145 const REAL_VALUE_TYPE *r;
1153 return (unsigned int)-1 >> 1;
1161 /* R = OP0 * 2**EXP. */
1164 real_ldexp (r, op0, exp)
1166 const REAL_VALUE_TYPE *op0;
1180 get_inf (r, r->sign);
1181 else if (exp < -MAX_EXP)
1182 get_zero (r, r->sign);
1192 /* Determine whether a floating-point value X is infinite. */
1196 const REAL_VALUE_TYPE *r;
1198 return (r->class == rvc_inf);
1201 /* Determine whether a floating-point value X is a NaN. */
1205 const REAL_VALUE_TYPE *r;
1207 return (r->class == rvc_nan);
1210 /* Determine whether a floating-point value X is negative. */
1214 const REAL_VALUE_TYPE *r;
1219 /* Determine whether a floating-point value X is minus zero. */
1223 const REAL_VALUE_TYPE *r;
1225 return r->sign && r->class == rvc_zero;
1228 /* Compare two floating-point objects for bitwise identity. */
1231 real_identical (a, b)
1232 const REAL_VALUE_TYPE *a, *b;
1236 if (a->class != b->class)
1238 if (a->sign != b->sign)
1248 if (a->exp != b->exp)
1252 for (i = 0; i < SIGSZ; ++i)
1253 if (a->sig[i] != b->sig[i])
1264 /* Try to change R into its exact multiplicative inverse in machine
1265 mode MODE. Return true if successful. */
1268 exact_real_inverse (mode, r)
1269 enum machine_mode mode;
1272 const REAL_VALUE_TYPE *one = real_digit (1);
1276 if (r->class != rvc_normal)
1279 /* Check for a power of two: all significand bits zero except the MSB. */
1280 for (i = 0; i < SIGSZ-1; ++i)
1283 if (r->sig[SIGSZ-1] != SIG_MSB)
1286 /* Find the inverse and truncate to the required mode. */
1287 do_divide (&u, one, r);
1288 real_convert (&u, mode, &u);
1290 /* The rounding may have overflowed. */
1291 if (u.class != rvc_normal)
1293 for (i = 0; i < SIGSZ-1; ++i)
1296 if (u.sig[SIGSZ-1] != SIG_MSB)
1303 /* Render R as an integer. */
1307 const REAL_VALUE_TYPE *r;
1309 unsigned HOST_WIDE_INT i;
1320 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1328 if (r->exp > HOST_BITS_PER_WIDE_INT)
1331 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1332 i = r->sig[SIGSZ-1];
1333 else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1335 i = r->sig[SIGSZ-1];
1336 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1337 i |= r->sig[SIGSZ-2];
1342 i >>= HOST_BITS_PER_WIDE_INT - r->exp;
1353 /* Likewise, but to an integer pair, HI+LOW. */
1356 real_to_integer2 (plow, phigh, r)
1357 HOST_WIDE_INT *plow, *phigh;
1358 const REAL_VALUE_TYPE *r;
1361 HOST_WIDE_INT low, high;
1374 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1388 if (exp >= 2*HOST_BITS_PER_WIDE_INT)
1391 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1392 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1394 high = t.sig[SIGSZ-1];
1395 low = t.sig[SIGSZ-2];
1397 else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1399 high = t.sig[SIGSZ-1];
1400 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1401 high |= t.sig[SIGSZ-2];
1403 low = t.sig[SIGSZ-3];
1404 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1405 low |= t.sig[SIGSZ-4];
1415 low = -low, high = ~high;
1427 /* Render R as a decimal floating point constant. Emit DIGITS
1428 significant digits in the result. If DIGITS <= 0, choose the
1429 maximum for the representation. */
1431 #define M_LOG10_2 0.30102999566398119521
1434 real_to_decimal (str, r_orig, digits)
1436 const REAL_VALUE_TYPE *r_orig;
1440 const REAL_VALUE_TYPE *one, *ten;
1441 int dec_exp, max_digits, d, cmp_half;
1442 char *p, *first, *last;
1449 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1454 strcpy (str, (r.sign ? "+Inf" : "-Inf"));
1457 /* ??? Print the significand as well, if not canonical? */
1458 strcpy (str, (r.sign ? "+NaN" : "-NaN"));
1464 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1465 if (digits <= 0 || digits > max_digits)
1466 digits = max_digits;
1468 one = real_digit (1);
1469 ten = ten_to_ptwo (0);
1474 /* Estimate the decimal exponent. */
1475 dec_exp = r.exp * M_LOG10_2;
1477 /* Scale the number such that it is in [1, 10). */
1481 for (i = EXP_BITS - 1; i >= 0; --i)
1482 if (dec_exp & (1 << i))
1483 do_divide (&r, &r, ten_to_ptwo (i));
1485 else if (dec_exp < 0)
1487 int i, pos_exp = -(--dec_exp);
1489 for (i = EXP_BITS - 1; i >= 0; --i)
1490 if (pos_exp & (1 << i))
1491 do_multiply (&r, &r, ten_to_ptwo (i));
1494 /* Assert that the number is in the proper range. Round-off can
1495 prevent the above from working exactly. */
1496 if (do_compare (&r, one, -1) < 0)
1498 do_multiply (&r, &r, ten);
1501 else if (do_compare (&r, ten, 1) >= 0)
1503 do_divide (&r, &r, ten);
1513 d = real_to_integer ((const REAL_VALUE_TYPE *) &r);
1514 do_add (&r, &r, real_digit (d), 1);
1519 do_multiply (&r, &r, ten);
1523 /* Round the result. Compare R vs 0.5 by doing R*2 vs 1.0. */
1525 cmp_half = do_compare (&r, one, -1);
1527 /* Round to even. */
1550 first[0] = first[1];
1553 sprintf (last, "e%+d", dec_exp);
1556 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1557 significant digits in the result. If DIGITS <= 0, choose the maximum
1558 for the representation. */
1561 real_to_hexadecimal (str, r, digits)
1563 const REAL_VALUE_TYPE *r;
1566 int i, j, exp = r->exp;
1577 strcpy (str, (r->sign ? "+Inf" : "-Inf"));
1580 /* ??? Print the significand as well, if not canonical? */
1581 strcpy (str, (r->sign ? "+NaN" : "-NaN"));
1588 digits = SIGNIFICAND_BITS / 4;
1598 for (i = SIGSZ - 1; i >= 0; --i)
1599 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1601 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1606 sprintf (p, "p%+d", exp);
1609 /* Initialize R from a decimal or hexadecimal string. The string is
1610 assumed to have been syntax checked already. */
1613 real_from_string (r, str)
1626 else if (*str == '+')
1629 if (str[0] == '0' && str[1] == 'x')
1631 /* Hexadecimal floating point. */
1632 int pos = SIGNIFICAND_BITS - 4, d;
1640 d = hex_value (*str);
1645 r->sig[pos / HOST_BITS_PER_LONG]
1646 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1657 d = hex_value (*str);
1662 r->sig[pos / HOST_BITS_PER_LONG]
1663 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1669 if (*str == 'p' || *str == 'P')
1679 else if (*str == '+')
1683 while (ISDIGIT (*str))
1690 /* Overflowed the exponent. */
1704 r->class = rvc_normal;
1718 /* Decimal floating point. */
1719 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1724 while (ISDIGIT (*str))
1727 do_multiply (r, r, ten);
1729 do_add (r, r, real_digit (d), 0);
1734 while (ISDIGIT (*str))
1737 do_multiply (r, r, ten);
1739 do_add (r, r, real_digit (d), 0);
1744 if (*str == 'e' || *str == 'E')
1754 else if (*str == '+')
1758 while (ISDIGIT (*str))
1765 /* Overflowed the exponent. */
1781 for (d = 0; d < EXP_BITS; ++d)
1783 do_divide (r, r, ten_to_ptwo (d));
1787 for (d = 0; d < EXP_BITS; ++d)
1789 do_multiply (r, r, ten_to_ptwo (d));
1796 get_zero (r, r->sign);
1800 get_inf (r, r->sign);
1804 /* Legacy. Similar, but return the result directly. */
1807 real_from_string2 (s, mode)
1809 enum machine_mode mode;
1813 real_from_string (&r, s);
1814 if (mode != VOIDmode)
1815 real_convert (&r, mode, &r);
1820 /* Initialize R from the integer pair HIGH+LOW. */
1823 real_from_integer (r, mode, low, high, unsigned_p)
1825 enum machine_mode mode;
1826 unsigned HOST_WIDE_INT low;
1830 if (low == 0 && high == 0)
1834 r->class = rvc_normal;
1835 r->sign = high < 0 && !unsigned_p;
1836 r->exp = 2 * HOST_BITS_PER_WIDE_INT;
1847 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
1849 r->sig[SIGSZ-1] = high;
1850 r->sig[SIGSZ-2] = low;
1851 memset (r->sig, 0, sizeof(long)*(SIGSZ-2));
1853 else if (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT)
1855 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
1856 r->sig[SIGSZ-2] = high;
1857 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
1858 r->sig[SIGSZ-4] = low;
1860 memset (r->sig, 0, sizeof(long)*(SIGSZ-4));
1868 if (mode != VOIDmode)
1869 real_convert (r, mode, r);
1872 /* Returns 10**2**n. */
1874 static const REAL_VALUE_TYPE *
1878 static REAL_VALUE_TYPE tens[EXP_BITS];
1880 if (n < 0 || n >= EXP_BITS)
1883 if (tens[n].class == rvc_zero)
1885 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
1887 HOST_WIDE_INT t = 10;
1890 for (i = 0; i < n; ++i)
1893 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
1897 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
1898 do_multiply (&tens[n], t, t);
1907 static const REAL_VALUE_TYPE *
1911 static REAL_VALUE_TYPE num[10];
1916 if (n > 0 && num[n].class == rvc_zero)
1917 real_from_integer (&num[n], VOIDmode, n, 0, 1);
1922 /* Fills R with +Inf. */
1931 /* Fills R with a NaN whose significand is described by STR. If QUIET,
1932 we force a QNaN, else we force an SNaN. The string, if not empty,
1933 is parsed as a number and placed in the significand. Return true
1934 if the string was successfully parsed. */
1937 real_nan (r, str, quiet, mode)
1941 enum machine_mode mode;
1943 const struct real_format *fmt;
1945 fmt = fmt_for_mode[mode - QFmode];
1952 get_canonical_qnan (r, 0);
1954 get_canonical_snan (r, 0);
1961 memset (r, 0, sizeof (*r));
1964 /* Parse akin to strtol into the significand of R. */
1966 while (ISSPACE (*str))
1970 else if (*str == '+')
1980 while ((d = hex_value (*str)) < base)
1987 lshift_significand (r, r, 3);
1990 lshift_significand (r, r, 4);
1993 lshift_significand_1 (&u, r);
1994 lshift_significand (r, r, 3);
1995 add_significands (r, r, &u);
2003 add_significands (r, r, &u);
2008 /* Must have consumed the entire string for success. */
2012 /* Shift the significand into place such that the bits
2013 are in the most significant bits for the format. */
2014 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->p);
2016 /* Our MSB is always unset for NaNs. */
2017 r->sig[SIGSZ-1] &= ~SIG_MSB;
2019 /* Force quiet or signalling NaN. */
2021 r->sig[SIGSZ-1] |= SIG_MSB >> 1;
2023 r->sig[SIGSZ-1] &= ~(SIG_MSB >> 1);
2025 /* Force at least one bit of the significand set. */
2026 for (d = 0; d < SIGSZ; ++d)
2030 r->sig[SIGSZ-1] |= SIG_MSB >> 2;
2032 /* Our intermediate format forces QNaNs to have MSB-1 set.
2033 If the target format has QNaNs with the top bit unset,
2034 mirror the output routines and invert the top two bits. */
2035 if (!fmt->qnan_msb_set)
2036 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1) | (SIG_MSB >> 2);
2042 /* Fills R with 2**N. */
2049 memset (r, 0, sizeof (*r));
2054 else if (n < -MAX_EXP)
2058 r->class = rvc_normal;
2060 r->sig[SIGSZ-1] = SIG_MSB;
2066 round_for_format (fmt, r)
2067 const struct real_format *fmt;
2071 unsigned long sticky;
2075 p2 = fmt->p * fmt->log2_b;
2076 emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2077 emax2 = fmt->emax * fmt->log2_b;
2079 np2 = SIGNIFICAND_BITS - p2;
2083 get_zero (r, r->sign);
2085 if (!fmt->has_signed_zero)
2090 get_inf (r, r->sign);
2095 clear_significand_below (r, np2);
2097 /* If we've cleared the entire significand, we need one bit
2098 set for this to continue to be a NaN. */
2099 for (i = 0; i < SIGSZ; ++i)
2103 r->sig[SIGSZ-1] = SIG_MSB >> 2;
2113 /* If we're not base2, normalize the exponent to a multiple of
2115 if (fmt->log2_b != 1)
2117 int shift = r->exp & (fmt->log2_b - 1);
2120 shift = fmt->log2_b - shift;
2121 sticky_rshift_significand (r, r, shift);
2126 /* Check the range of the exponent. If we're out of range,
2127 either underflow or overflow. */
2130 else if (r->exp <= emin2m1)
2134 if (!fmt->has_denorm)
2136 /* Don't underflow completely until we've had a chance to round. */
2137 if (r->exp < emin2m1)
2142 diff = emin2m1 - r->exp + 1;
2146 /* De-normalize the significand. */
2147 sticky_rshift_significand (r, r, diff);
2152 /* There are P2 true significand bits, followed by one guard bit,
2153 followed by one sticky bit, followed by stuff. Fold non-zero
2154 stuff into the sticky bit. */
2157 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2158 sticky |= r->sig[i];
2160 r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2162 guard = test_significand_bit (r, np2 - 1);
2163 lsb = test_significand_bit (r, np2);
2165 /* Round to even. */
2166 if (guard && (sticky || lsb))
2170 set_significand_bit (&u, np2);
2172 if (add_significands (r, r, &u))
2174 /* Overflow. Means the significand had been all ones, and
2175 is now all zeros. Need to increase the exponent, and
2176 possibly re-normalize it. */
2177 if (++r->exp > emax2)
2179 r->sig[SIGSZ-1] = SIG_MSB;
2181 if (fmt->log2_b != 1)
2183 int shift = r->exp & (fmt->log2_b - 1);
2186 shift = fmt->log2_b - shift;
2187 sticky_rshift_significand (r, r, shift);
2196 /* Catch underflow that we deferred until after rounding. */
2197 if (r->exp <= emin2m1)
2200 /* Clear out trailing garbage. */
2201 clear_significand_below (r, np2);
2204 /* Extend or truncate to a new mode. */
2207 real_convert (r, mode, a)
2209 enum machine_mode mode;
2210 const REAL_VALUE_TYPE *a;
2212 const struct real_format *fmt;
2214 fmt = fmt_for_mode[mode - QFmode];
2219 round_for_format (fmt, r);
2221 /* round_for_format de-normalizes denormals. Undo just that part. */
2222 if (r->class == rvc_normal)
2226 /* Legacy. Likewise, except return the struct directly. */
2229 real_value_truncate (mode, a)
2230 enum machine_mode mode;
2234 real_convert (&r, mode, &a);
2238 /* Return true if truncating to MODE is exact. */
2241 exact_real_truncate (mode, a)
2242 enum machine_mode mode;
2243 const REAL_VALUE_TYPE *a;
2246 real_convert (&t, mode, a);
2247 return real_identical (&t, a);
2250 /* Write R to the target format of MODE. Place the words of the
2251 result in target word order in BUF. There are always 32 bits
2252 in each long, no matter the size of the host long.
2254 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2257 real_to_target (buf, r_orig, mode)
2259 const REAL_VALUE_TYPE *r_orig;
2260 enum machine_mode mode;
2263 const struct real_format *fmt;
2266 fmt = fmt_for_mode[mode - QFmode];
2271 round_for_format (fmt, &r);
2275 (*fmt->encode) (fmt, buf, &r);
2280 /* Read R from the target format of MODE. Read the words of the
2281 result in target word order in BUF. There are always 32 bits
2282 in each long, no matter the size of the host long. */
2285 real_from_target (r, buf, mode)
2288 enum machine_mode mode;
2290 const struct real_format *fmt;
2292 fmt = fmt_for_mode[mode - QFmode];
2296 (*fmt->decode) (fmt, r, buf);
2299 /* Return the number of bits in the significand for MODE. */
2300 /* ??? Legacy. Should get access to real_format directly. */
2303 significand_size (mode)
2304 enum machine_mode mode;
2306 const struct real_format *fmt;
2308 fmt = fmt_for_mode[mode - QFmode];
2312 return fmt->p * fmt->log2_b;
2315 /* IEEE single-precision format. */
2317 static void encode_ieee_single PARAMS ((const struct real_format *fmt,
2318 long *, const REAL_VALUE_TYPE *));
2319 static void decode_ieee_single PARAMS ((const struct real_format *,
2320 REAL_VALUE_TYPE *, const long *));
2323 encode_ieee_single (fmt, buf, r)
2324 const struct real_format *fmt;
2326 const REAL_VALUE_TYPE *r;
2328 unsigned long image, sig, exp;
2329 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2331 image = r->sign << 31;
2332 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2343 image |= 0x7fffffff;
2351 if (!fmt->qnan_msb_set)
2352 image ^= 1 << 23 | 1 << 22;
2355 image |= 0x7fffffff;
2359 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2360 whereas the intermediate representation is 0.F x 2**exp.
2361 Which means we're off by one. */
2365 exp = r->exp + 127 - 1;
2378 decode_ieee_single (fmt, r, buf)
2379 const struct real_format *fmt;
2383 unsigned long image = buf[0] & 0xffffffff;
2384 bool sign = (image >> 31) & 1;
2385 int exp = (image >> 23) & 0xff;
2387 memset (r, 0, sizeof (*r));
2388 image <<= HOST_BITS_PER_LONG - 24;
2393 if (image && fmt->has_denorm)
2395 r->class = rvc_normal;
2398 r->sig[SIGSZ-1] = image << 1;
2401 else if (fmt->has_signed_zero)
2404 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2410 if (!fmt->qnan_msb_set)
2411 image ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2412 r->sig[SIGSZ-1] = image;
2422 r->class = rvc_normal;
2424 r->exp = exp - 127 + 1;
2425 r->sig[SIGSZ-1] = image | SIG_MSB;
2429 const struct real_format ieee_single =
2446 /* IEEE double-precision format. */
2448 static void encode_ieee_double PARAMS ((const struct real_format *fmt,
2449 long *, const REAL_VALUE_TYPE *));
2450 static void decode_ieee_double PARAMS ((const struct real_format *,
2451 REAL_VALUE_TYPE *, const long *));
2454 encode_ieee_double (fmt, buf, r)
2455 const struct real_format *fmt;
2457 const REAL_VALUE_TYPE *r;
2459 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2460 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2462 image_hi = r->sign << 31;
2465 if (HOST_BITS_PER_LONG == 64)
2467 sig_hi = r->sig[SIGSZ-1];
2468 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2469 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2473 sig_hi = r->sig[SIGSZ-1];
2474 sig_lo = r->sig[SIGSZ-2];
2475 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2476 sig_hi = (sig_hi >> 11) & 0xfffff;
2486 image_hi |= 2047 << 20;
2489 image_hi |= 0x7fffffff;
2490 image_lo = 0xffffffff;
2497 image_hi |= 2047 << 20;
2499 if (!fmt->qnan_msb_set)
2500 image_hi ^= 1 << 19 | 1 << 18;
2505 image_hi |= 0x7fffffff;
2506 image_lo = 0xffffffff;
2511 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2512 whereas the intermediate representation is 0.F x 2**exp.
2513 Which means we're off by one. */
2517 exp = r->exp + 1023 - 1;
2518 image_hi |= exp << 20;
2527 if (FLOAT_WORDS_BIG_ENDIAN)
2528 buf[0] = image_hi, buf[1] = image_lo;
2530 buf[0] = image_lo, buf[1] = image_hi;
2534 decode_ieee_double (fmt, r, buf)
2535 const struct real_format *fmt;
2539 unsigned long image_hi, image_lo;
2543 if (FLOAT_WORDS_BIG_ENDIAN)
2544 image_hi = buf[0], image_lo = buf[1];
2546 image_lo = buf[0], image_hi = buf[1];
2547 image_lo &= 0xffffffff;
2548 image_hi &= 0xffffffff;
2550 sign = (image_hi >> 31) & 1;
2551 exp = (image_hi >> 20) & 0x7ff;
2553 memset (r, 0, sizeof (*r));
2555 image_hi <<= 32 - 21;
2556 image_hi |= image_lo >> 21;
2557 image_hi &= 0x7fffffff;
2558 image_lo <<= 32 - 21;
2562 if ((image_hi || image_lo) && fmt->has_denorm)
2564 r->class = rvc_normal;
2567 if (HOST_BITS_PER_LONG == 32)
2569 image_hi = (image_hi << 1) | (image_lo >> 31);
2571 r->sig[SIGSZ-1] = image_hi;
2572 r->sig[SIGSZ-2] = image_lo;
2576 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2577 r->sig[SIGSZ-1] = image_hi;
2581 else if (fmt->has_signed_zero)
2584 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2586 if (image_hi || image_lo)
2590 if (HOST_BITS_PER_LONG == 32)
2592 r->sig[SIGSZ-1] = image_hi;
2593 r->sig[SIGSZ-2] = image_lo;
2596 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
2598 if (!fmt->qnan_msb_set)
2599 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2609 r->class = rvc_normal;
2611 r->exp = exp - 1023 + 1;
2612 if (HOST_BITS_PER_LONG == 32)
2614 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
2615 r->sig[SIGSZ-2] = image_lo;
2618 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2622 const struct real_format ieee_double =
2639 /* IEEE extended double precision format. This comes in three
2640 flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2643 static void encode_ieee_extended PARAMS ((const struct real_format *fmt,
2644 long *, const REAL_VALUE_TYPE *));
2645 static void decode_ieee_extended PARAMS ((const struct real_format *,
2646 REAL_VALUE_TYPE *, const long *));
2648 static void encode_ieee_extended_128 PARAMS ((const struct real_format *fmt,
2650 const REAL_VALUE_TYPE *));
2651 static void decode_ieee_extended_128 PARAMS ((const struct real_format *,
2656 encode_ieee_extended (fmt, buf, r)
2657 const struct real_format *fmt;
2659 const REAL_VALUE_TYPE *r;
2661 unsigned long image_hi, sig_hi, sig_lo;
2662 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2664 image_hi = r->sign << 15;
2665 sig_hi = sig_lo = 0;
2677 /* Intel requires the explicit integer bit to be set, otherwise
2678 it considers the value a "pseudo-infinity". Motorola docs
2679 say it doesn't care. */
2680 sig_hi = 0x80000000;
2685 sig_lo = sig_hi = 0xffffffff;
2693 if (HOST_BITS_PER_LONG == 32)
2695 sig_hi = r->sig[SIGSZ-1];
2696 sig_lo = r->sig[SIGSZ-2];
2700 sig_lo = r->sig[SIGSZ-1];
2701 sig_hi = sig_lo >> 31 >> 1;
2702 sig_lo &= 0xffffffff;
2704 if (!fmt->qnan_msb_set)
2705 sig_hi ^= 1 << 30 | 1 << 29;
2707 /* Intel requires the explicit integer bit to be set, otherwise
2708 it considers the value a "pseudo-nan". Motorola docs say it
2710 sig_hi |= 0x80000000;
2715 sig_lo = sig_hi = 0xffffffff;
2723 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2724 whereas the intermediate representation is 0.F x 2**exp.
2725 Which means we're off by one.
2727 Except for Motorola, which consider exp=0 and explicit
2728 integer bit set to continue to be normalized. In theory
2729 this descrepency has been taken care of by the difference
2730 in fmt->emin in round_for_format. */
2742 if (HOST_BITS_PER_LONG == 32)
2744 sig_hi = r->sig[SIGSZ-1];
2745 sig_lo = r->sig[SIGSZ-2];
2749 sig_lo = r->sig[SIGSZ-1];
2750 sig_hi = sig_lo >> 31 >> 1;
2751 sig_lo &= 0xffffffff;
2760 if (FLOAT_WORDS_BIG_ENDIAN)
2761 buf[0] = image_hi << 16, buf[1] = sig_hi, buf[2] = sig_lo;
2763 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
2767 encode_ieee_extended_128 (fmt, buf, r)
2768 const struct real_format *fmt;
2770 const REAL_VALUE_TYPE *r;
2772 buf[3 * !FLOAT_WORDS_BIG_ENDIAN] = 0;
2773 encode_ieee_extended (fmt, buf+!!FLOAT_WORDS_BIG_ENDIAN, r);
2777 decode_ieee_extended (fmt, r, buf)
2778 const struct real_format *fmt;
2782 unsigned long image_hi, sig_hi, sig_lo;
2786 if (FLOAT_WORDS_BIG_ENDIAN)
2787 image_hi = buf[0] >> 16, sig_hi = buf[1], sig_lo = buf[2];
2789 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
2790 sig_lo &= 0xffffffff;
2791 sig_hi &= 0xffffffff;
2792 image_hi &= 0xffffffff;
2794 sign = (image_hi >> 15) & 1;
2795 exp = image_hi & 0x7fff;
2797 memset (r, 0, sizeof (*r));
2801 if ((sig_hi || sig_lo) && fmt->has_denorm)
2803 r->class = rvc_normal;
2806 /* When the IEEE format contains a hidden bit, we know that
2807 it's zero at this point, and so shift up the significand
2808 and decrease the exponent to match. In this case, Motorola
2809 defines the explicit integer bit to be valid, so we don't
2810 know whether the msb is set or not. */
2812 if (HOST_BITS_PER_LONG == 32)
2814 r->sig[SIGSZ-1] = sig_hi;
2815 r->sig[SIGSZ-2] = sig_lo;
2818 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2822 else if (fmt->has_signed_zero)
2825 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
2827 /* See above re "pseudo-infinities" and "pseudo-nans".
2828 Short summary is that the MSB will likely always be
2829 set, and that we don't care about it. */
2830 sig_hi &= 0x7fffffff;
2832 if (sig_hi || sig_lo)
2836 if (HOST_BITS_PER_LONG == 32)
2838 r->sig[SIGSZ-1] = sig_hi;
2839 r->sig[SIGSZ-2] = sig_lo;
2842 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2844 if (!fmt->qnan_msb_set)
2845 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2855 r->class = rvc_normal;
2857 r->exp = exp - 16383 + 1;
2858 if (HOST_BITS_PER_LONG == 32)
2860 r->sig[SIGSZ-1] = sig_hi;
2861 r->sig[SIGSZ-2] = sig_lo;
2864 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2869 decode_ieee_extended_128 (fmt, r, buf)
2870 const struct real_format *fmt;
2874 decode_ieee_extended (fmt, r, buf+!!FLOAT_WORDS_BIG_ENDIAN);
2877 const struct real_format ieee_extended_motorola =
2879 encode_ieee_extended,
2880 decode_ieee_extended,
2893 const struct real_format ieee_extended_intel_96 =
2895 encode_ieee_extended,
2896 decode_ieee_extended,
2909 const struct real_format ieee_extended_intel_128 =
2911 encode_ieee_extended_128,
2912 decode_ieee_extended_128,
2926 /* IEEE quad precision format. */
2928 static void encode_ieee_quad PARAMS ((const struct real_format *fmt,
2929 long *, const REAL_VALUE_TYPE *));
2930 static void decode_ieee_quad PARAMS ((const struct real_format *,
2931 REAL_VALUE_TYPE *, const long *));
2934 encode_ieee_quad (fmt, buf, r)
2935 const struct real_format *fmt;
2937 const REAL_VALUE_TYPE *r;
2939 unsigned long image3, image2, image1, image0, exp;
2940 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2943 image3 = r->sign << 31;
2948 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
2957 image3 |= 32767 << 16;
2960 image3 |= 0x7fffffff;
2961 image2 = 0xffffffff;
2962 image1 = 0xffffffff;
2963 image0 = 0xffffffff;
2970 image3 |= 32767 << 16;
2972 if (HOST_BITS_PER_LONG == 32)
2977 image3 |= u.sig[3] & 0xffff;
2982 image1 = image0 >> 31 >> 1;
2984 image3 |= (image2 >> 31 >> 1) & 0xffff;
2985 image0 &= 0xffffffff;
2986 image2 &= 0xffffffff;
2989 if (!fmt->qnan_msb_set)
2990 image3 ^= 1 << 15 | 1 << 14;
2994 image3 |= 0x7fffffff;
2995 image2 = 0xffffffff;
2996 image1 = 0xffffffff;
2997 image0 = 0xffffffff;
3002 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3003 whereas the intermediate representation is 0.F x 2**exp.
3004 Which means we're off by one. */
3008 exp = r->exp + 16383 - 1;
3009 image3 |= exp << 16;
3011 if (HOST_BITS_PER_LONG == 32)
3016 image3 |= u.sig[3] & 0xffff;
3021 image1 = image0 >> 31 >> 1;
3023 image3 |= (image2 >> 31 >> 1) & 0xffff;
3024 image0 &= 0xffffffff;
3025 image2 &= 0xffffffff;
3033 if (FLOAT_WORDS_BIG_ENDIAN)
3050 decode_ieee_quad (fmt, r, buf)
3051 const struct real_format *fmt;
3055 unsigned long image3, image2, image1, image0;
3059 if (FLOAT_WORDS_BIG_ENDIAN)
3073 image0 &= 0xffffffff;
3074 image1 &= 0xffffffff;
3075 image2 &= 0xffffffff;
3077 sign = (image3 >> 31) & 1;
3078 exp = (image3 >> 16) & 0x7fff;
3081 memset (r, 0, sizeof (*r));
3085 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3087 r->class = rvc_normal;
3090 r->exp = -16382 + (SIGNIFICAND_BITS - 112);
3091 if (HOST_BITS_PER_LONG == 32)
3100 r->sig[0] = (image1 << 31 << 1) | image0;
3101 r->sig[1] = (image3 << 31 << 1) | image2;
3106 else if (fmt->has_signed_zero)
3109 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3111 if (image3 | image2 | image1 | image0)
3116 if (HOST_BITS_PER_LONG == 32)
3125 r->sig[0] = (image1 << 31 << 1) | image0;
3126 r->sig[1] = (image3 << 31 << 1) | image2;
3128 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3130 if (!fmt->qnan_msb_set)
3131 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
3141 r->class = rvc_normal;
3143 r->exp = exp - 16383 + 1;
3145 if (HOST_BITS_PER_LONG == 32)
3154 r->sig[0] = (image1 << 31 << 1) | image0;
3155 r->sig[1] = (image3 << 31 << 1) | image2;
3157 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3158 r->sig[SIGSZ-1] |= SIG_MSB;
3162 const struct real_format ieee_quad =
3179 /* The VAX floating point formats. */
3181 static void encode_vax_f PARAMS ((const struct real_format *fmt,
3182 long *, const REAL_VALUE_TYPE *));
3183 static void decode_vax_f PARAMS ((const struct real_format *,
3184 REAL_VALUE_TYPE *, const long *));
3185 static void encode_vax_d PARAMS ((const struct real_format *fmt,
3186 long *, const REAL_VALUE_TYPE *));
3187 static void decode_vax_d PARAMS ((const struct real_format *,
3188 REAL_VALUE_TYPE *, const long *));
3189 static void encode_vax_g PARAMS ((const struct real_format *fmt,
3190 long *, const REAL_VALUE_TYPE *));
3191 static void decode_vax_g PARAMS ((const struct real_format *,
3192 REAL_VALUE_TYPE *, const long *));
3195 encode_vax_f (fmt, buf, r)
3196 const struct real_format *fmt ATTRIBUTE_UNUSED;
3198 const REAL_VALUE_TYPE *r;
3200 unsigned long sign, exp, sig, image;
3202 sign = r->sign << 15;
3212 image = 0xffff7fff | sign;
3216 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3219 image = (sig << 16) & 0xffff0000;
3233 decode_vax_f (fmt, r, buf)
3234 const struct real_format *fmt ATTRIBUTE_UNUSED;
3238 unsigned long image = buf[0] & 0xffffffff;
3239 int exp = (image >> 7) & 0xff;
3241 memset (r, 0, sizeof (*r));
3245 r->class = rvc_normal;
3246 r->sign = (image >> 15) & 1;
3249 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3250 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3255 encode_vax_d (fmt, buf, r)
3256 const struct real_format *fmt ATTRIBUTE_UNUSED;
3258 const REAL_VALUE_TYPE *r;
3260 unsigned long image0, image1, sign = r->sign << 15;
3265 image0 = image1 = 0;
3270 image0 = 0xffff7fff | sign;
3271 image1 = 0xffffffff;
3275 /* Extract the significand into straight hi:lo. */
3276 if (HOST_BITS_PER_LONG == 64)
3278 image0 = r->sig[SIGSZ-1];
3279 image1 = (image0 >> (64 - 56)) & 0xffffffff;
3280 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3284 image0 = r->sig[SIGSZ-1];
3285 image1 = r->sig[SIGSZ-2];
3286 image1 = (image0 << 24) | (image1 >> 8);
3287 image0 = (image0 >> 8) & 0xffffff;
3290 /* Rearrange the half-words of the significand to match the
3292 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3293 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3295 /* Add the sign and exponent. */
3297 image0 |= (r->exp + 128) << 7;
3304 if (FLOAT_WORDS_BIG_ENDIAN)
3305 buf[0] = image1, buf[1] = image0;
3307 buf[0] = image0, buf[1] = image1;
3311 decode_vax_d (fmt, r, buf)
3312 const struct real_format *fmt ATTRIBUTE_UNUSED;
3316 unsigned long image0, image1;
3319 if (FLOAT_WORDS_BIG_ENDIAN)
3320 image1 = buf[0], image0 = buf[1];
3322 image0 = buf[0], image1 = buf[1];
3323 image0 &= 0xffffffff;
3324 image1 &= 0xffffffff;
3326 exp = (image0 >> 7) & 0x7f;
3328 memset (r, 0, sizeof (*r));
3332 r->class = rvc_normal;
3333 r->sign = (image0 >> 15) & 1;
3336 /* Rearrange the half-words of the external format into
3337 proper ascending order. */
3338 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
3339 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3341 if (HOST_BITS_PER_LONG == 64)
3343 image0 = (image0 << 31 << 1) | image1;
3346 r->sig[SIGSZ-1] = image0;
3350 r->sig[SIGSZ-1] = image0;
3351 r->sig[SIGSZ-2] = image1;
3352 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
3353 r->sig[SIGSZ-1] |= SIG_MSB;
3359 encode_vax_g (fmt, buf, r)
3360 const struct real_format *fmt ATTRIBUTE_UNUSED;
3362 const REAL_VALUE_TYPE *r;
3364 unsigned long image0, image1, sign = r->sign << 15;
3369 image0 = image1 = 0;
3374 image0 = 0xffff7fff | sign;
3375 image1 = 0xffffffff;
3379 /* Extract the significand into straight hi:lo. */
3380 if (HOST_BITS_PER_LONG == 64)
3382 image0 = r->sig[SIGSZ-1];
3383 image1 = (image0 >> (64 - 53)) & 0xffffffff;
3384 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
3388 image0 = r->sig[SIGSZ-1];
3389 image1 = r->sig[SIGSZ-2];
3390 image1 = (image0 << 21) | (image1 >> 11);
3391 image0 = (image0 >> 11) & 0xfffff;
3394 /* Rearrange the half-words of the significand to match the
3396 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
3397 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3399 /* Add the sign and exponent. */
3401 image0 |= (r->exp + 1024) << 4;
3408 if (FLOAT_WORDS_BIG_ENDIAN)
3409 buf[0] = image1, buf[1] = image0;
3411 buf[0] = image0, buf[1] = image1;
3415 decode_vax_g (fmt, r, buf)
3416 const struct real_format *fmt ATTRIBUTE_UNUSED;
3420 unsigned long image0, image1;
3423 if (FLOAT_WORDS_BIG_ENDIAN)
3424 image1 = buf[0], image0 = buf[1];
3426 image0 = buf[0], image1 = buf[1];
3427 image0 &= 0xffffffff;
3428 image1 &= 0xffffffff;
3430 exp = (image0 >> 4) & 0x7ff;
3432 memset (r, 0, sizeof (*r));
3436 r->class = rvc_normal;
3437 r->sign = (image0 >> 15) & 1;
3438 r->exp = exp - 1024;
3440 /* Rearrange the half-words of the external format into
3441 proper ascending order. */
3442 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
3443 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3445 if (HOST_BITS_PER_LONG == 64)
3447 image0 = (image0 << 31 << 1) | image1;
3450 r->sig[SIGSZ-1] = image0;
3454 r->sig[SIGSZ-1] = image0;
3455 r->sig[SIGSZ-2] = image1;
3456 lshift_significand (r, r, 64 - 53);
3457 r->sig[SIGSZ-1] |= SIG_MSB;
3462 const struct real_format vax_f_format =
3478 const struct real_format vax_d_format =
3494 const struct real_format vax_g_format =
3511 /* The IBM S/390 floating point formats. A good reference for these can
3512 be found in chapter 9 of "ESA/390 Principles of Operation", IBM document
3513 number SA22-7201-01. An on-line version can be found here:
3515 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3518 static void encode_i370_single PARAMS ((const struct real_format *fmt,
3519 long *, const REAL_VALUE_TYPE *));
3520 static void decode_i370_single PARAMS ((const struct real_format *,
3521 REAL_VALUE_TYPE *, const long *));
3522 static void encode_i370_double PARAMS ((const struct real_format *fmt,
3523 long *, const REAL_VALUE_TYPE *));
3524 static void decode_i370_double PARAMS ((const struct real_format *,
3525 REAL_VALUE_TYPE *, const long *));
3528 encode_i370_single (fmt, buf, r)
3529 const struct real_format *fmt ATTRIBUTE_UNUSED;
3531 const REAL_VALUE_TYPE *r;
3533 unsigned long sign, exp, sig, image;
3535 sign = r->sign << 31;
3545 image = 0x7fffffff | sign;
3549 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
3550 exp = ((r->exp / 4) + 64) << 24;
3551 image = sign | exp | sig;
3562 decode_i370_single (fmt, r, buf)
3563 const struct real_format *fmt ATTRIBUTE_UNUSED;
3567 unsigned long sign, sig, image = buf[0];
3570 sign = (image >> 31) & 1;
3571 exp = (image >> 24) & 0x7f;
3572 sig = image & 0xffffff;
3574 memset (r, 0, sizeof (*r));
3578 r->class = rvc_normal;
3580 r->exp = (exp - 64) * 4;
3581 r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
3587 encode_i370_double (fmt, buf, r)
3588 const struct real_format *fmt ATTRIBUTE_UNUSED;
3590 const REAL_VALUE_TYPE *r;
3592 unsigned long sign, exp, image_hi, image_lo;
3594 sign = r->sign << 31;
3599 image_hi = image_lo = 0;
3604 image_hi = 0x7fffffff | sign;
3605 image_lo = 0xffffffff;
3609 if (HOST_BITS_PER_LONG == 64)
3611 image_hi = r->sig[SIGSZ-1];
3612 image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
3613 image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
3617 image_hi = r->sig[SIGSZ-1];
3618 image_lo = r->sig[SIGSZ-2];
3619 image_lo = (image_lo >> 8) | (image_hi << 24);
3623 exp = ((r->exp / 4) + 64) << 24;
3624 image_hi |= sign | exp;
3631 if (FLOAT_WORDS_BIG_ENDIAN)
3632 buf[0] = image_hi, buf[1] = image_lo;
3634 buf[0] = image_lo, buf[1] = image_hi;
3638 decode_i370_double (fmt, r, buf)
3639 const struct real_format *fmt ATTRIBUTE_UNUSED;
3643 unsigned long sign, image_hi, image_lo;
3646 if (FLOAT_WORDS_BIG_ENDIAN)
3647 image_hi = buf[0], image_lo = buf[1];
3649 image_lo = buf[0], image_hi = buf[1];
3651 sign = (image_hi >> 31) & 1;
3652 exp = (image_hi >> 24) & 0x7f;
3653 image_hi &= 0xffffff;
3654 image_lo &= 0xffffffff;
3656 memset (r, 0, sizeof (*r));
3658 if (exp || image_hi || image_lo)
3660 r->class = rvc_normal;
3662 r->exp = (exp - 64) * 4 + (SIGNIFICAND_BITS - 56);
3664 if (HOST_BITS_PER_LONG == 32)
3666 r->sig[0] = image_lo;
3667 r->sig[1] = image_hi;
3670 r->sig[0] = image_lo | (image_hi << 31 << 1);
3676 const struct real_format i370_single =
3687 false, /* ??? The encoding does allow for "unnormals". */
3688 false, /* ??? The encoding does allow for "unnormals". */
3692 const struct real_format i370_double =
3703 false, /* ??? The encoding does allow for "unnormals". */
3704 false, /* ??? The encoding does allow for "unnormals". */
3709 /* TMS320C[34]x twos complement floating point format. */
3711 static void encode_c4x_single PARAMS ((const struct real_format *fmt,
3712 long *, const REAL_VALUE_TYPE *));
3713 static void decode_c4x_single PARAMS ((const struct real_format *,
3714 REAL_VALUE_TYPE *, const long *));
3715 static void encode_c4x_extended PARAMS ((const struct real_format *fmt,
3716 long *, const REAL_VALUE_TYPE *));
3717 static void decode_c4x_extended PARAMS ((const struct real_format *,
3718 REAL_VALUE_TYPE *, const long *));
3721 encode_c4x_single (fmt, buf, r)
3722 const struct real_format *fmt ATTRIBUTE_UNUSED;
3724 const REAL_VALUE_TYPE *r;
3726 unsigned long image, exp, sig;
3738 sig = 0x800000 - r->sign;
3743 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3758 image = ((exp & 0xff) << 24) | (sig & 0xffffff);
3763 decode_c4x_single (fmt, r, buf)
3764 const struct real_format *fmt ATTRIBUTE_UNUSED;
3768 unsigned long image = buf[0];
3772 exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
3773 sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
3775 memset (r, 0, sizeof (*r));
3779 r->class = rvc_normal;
3781 sig = sf & 0x7fffff;
3790 sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3793 r->sig[SIGSZ-1] = sig;
3798 encode_c4x_extended (fmt, buf, r)
3799 const struct real_format *fmt ATTRIBUTE_UNUSED;
3801 const REAL_VALUE_TYPE *r;
3803 unsigned long exp, sig;
3815 sig = 0x80000000 - r->sign;
3821 sig = r->sig[SIGSZ-1];
3822 if (HOST_BITS_PER_LONG == 64)
3823 sig = sig >> 1 >> 31;
3840 exp = (exp & 0xff) << 24;
3843 if (FLOAT_WORDS_BIG_ENDIAN)
3844 buf[0] = exp, buf[1] = sig;
3846 buf[0] = sig, buf[0] = exp;
3850 decode_c4x_extended (fmt, r, buf)
3851 const struct real_format *fmt ATTRIBUTE_UNUSED;
3858 if (FLOAT_WORDS_BIG_ENDIAN)
3859 exp = buf[0], sf = buf[1];
3861 sf = buf[0], exp = buf[1];
3863 exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
3864 sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
3866 memset (r, 0, sizeof (*r));
3870 r->class = rvc_normal;
3872 sig = sf & 0x7fffffff;
3881 if (HOST_BITS_PER_LONG == 64)
3882 sig = sig << 1 << 31;
3886 r->sig[SIGSZ-1] = sig;
3890 const struct real_format c4x_single =
3906 const struct real_format c4x_extended =
3908 encode_c4x_extended,
3909 decode_c4x_extended,
3923 /* Initialize things at start of compilation. */
3925 static const struct real_format * format_for_size PARAMS ((int));
3927 static const struct real_format *
3928 format_for_size (size)
3931 #ifndef TARGET_G_FORMAT
3932 #define TARGET_G_FORMAT 0
3935 switch (TARGET_FLOAT_FORMAT)
3937 case IEEE_FLOAT_FORMAT:
3941 return &ieee_single;
3944 return &ieee_double;
3947 if (!INTEL_EXTENDED_IEEE_FORMAT)
3948 return &ieee_extended_motorola;
3950 return &ieee_extended_intel_96;
3953 if (!INTEL_EXTENDED_IEEE_FORMAT)
3956 return &ieee_extended_intel_128;
3960 case VAX_FLOAT_FORMAT:
3964 return &vax_f_format;
3967 if (TARGET_G_FORMAT)
3968 return &vax_g_format;
3970 return &vax_d_format;
3974 case IBM_FLOAT_FORMAT:
3978 return &i370_single;
3980 return &i370_double;
3984 case C4X_FLOAT_FORMAT:
3990 return &c4x_extended;
4003 /* Set up the mode->format table. */
4004 for (i = 0; i < 3; ++i)
4006 enum machine_mode mode;
4010 size = FLOAT_TYPE_SIZE;
4012 size = DOUBLE_TYPE_SIZE;
4014 size = LONG_DOUBLE_TYPE_SIZE;
4016 mode = mode_for_size (size, MODE_FLOAT, 0);
4017 if (mode == BLKmode)
4020 fmt_for_mode[mode - QFmode] = format_for_size (size);