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 /* Return a hash value for the given real value. */
2316 /* ??? The "unsigned int" return value is intended to be hashval_t,
2317 but I didn't want to pull hashtab.h into real.h. */
2321 const REAL_VALUE_TYPE *r;
2326 h = r->class | (r->sign << 2);
2338 if (sizeof(unsigned long) > sizeof(unsigned int))
2339 for (i = 0; i < SIGSZ; ++i)
2341 unsigned long s = r->sig[i];
2342 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2345 for (i = 0; i < SIGSZ; ++i)
2356 /* IEEE single-precision format. */
2358 static void encode_ieee_single PARAMS ((const struct real_format *fmt,
2359 long *, const REAL_VALUE_TYPE *));
2360 static void decode_ieee_single PARAMS ((const struct real_format *,
2361 REAL_VALUE_TYPE *, const long *));
2364 encode_ieee_single (fmt, buf, r)
2365 const struct real_format *fmt;
2367 const REAL_VALUE_TYPE *r;
2369 unsigned long image, sig, exp;
2370 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2372 image = r->sign << 31;
2373 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2384 image |= 0x7fffffff;
2392 if (!fmt->qnan_msb_set)
2393 image ^= 1 << 23 | 1 << 22;
2396 image |= 0x7fffffff;
2400 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2401 whereas the intermediate representation is 0.F x 2**exp.
2402 Which means we're off by one. */
2406 exp = r->exp + 127 - 1;
2419 decode_ieee_single (fmt, r, buf)
2420 const struct real_format *fmt;
2424 unsigned long image = buf[0] & 0xffffffff;
2425 bool sign = (image >> 31) & 1;
2426 int exp = (image >> 23) & 0xff;
2428 memset (r, 0, sizeof (*r));
2429 image <<= HOST_BITS_PER_LONG - 24;
2434 if (image && fmt->has_denorm)
2436 r->class = rvc_normal;
2439 r->sig[SIGSZ-1] = image << 1;
2442 else if (fmt->has_signed_zero)
2445 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2451 if (!fmt->qnan_msb_set)
2452 image ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2453 r->sig[SIGSZ-1] = image;
2463 r->class = rvc_normal;
2465 r->exp = exp - 127 + 1;
2466 r->sig[SIGSZ-1] = image | SIG_MSB;
2470 const struct real_format ieee_single =
2487 /* IEEE double-precision format. */
2489 static void encode_ieee_double PARAMS ((const struct real_format *fmt,
2490 long *, const REAL_VALUE_TYPE *));
2491 static void decode_ieee_double PARAMS ((const struct real_format *,
2492 REAL_VALUE_TYPE *, const long *));
2495 encode_ieee_double (fmt, buf, r)
2496 const struct real_format *fmt;
2498 const REAL_VALUE_TYPE *r;
2500 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2501 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2503 image_hi = r->sign << 31;
2506 if (HOST_BITS_PER_LONG == 64)
2508 sig_hi = r->sig[SIGSZ-1];
2509 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2510 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2514 sig_hi = r->sig[SIGSZ-1];
2515 sig_lo = r->sig[SIGSZ-2];
2516 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2517 sig_hi = (sig_hi >> 11) & 0xfffff;
2527 image_hi |= 2047 << 20;
2530 image_hi |= 0x7fffffff;
2531 image_lo = 0xffffffff;
2538 image_hi |= 2047 << 20;
2540 if (!fmt->qnan_msb_set)
2541 image_hi ^= 1 << 19 | 1 << 18;
2546 image_hi |= 0x7fffffff;
2547 image_lo = 0xffffffff;
2552 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2553 whereas the intermediate representation is 0.F x 2**exp.
2554 Which means we're off by one. */
2558 exp = r->exp + 1023 - 1;
2559 image_hi |= exp << 20;
2568 if (FLOAT_WORDS_BIG_ENDIAN)
2569 buf[0] = image_hi, buf[1] = image_lo;
2571 buf[0] = image_lo, buf[1] = image_hi;
2575 decode_ieee_double (fmt, r, buf)
2576 const struct real_format *fmt;
2580 unsigned long image_hi, image_lo;
2584 if (FLOAT_WORDS_BIG_ENDIAN)
2585 image_hi = buf[0], image_lo = buf[1];
2587 image_lo = buf[0], image_hi = buf[1];
2588 image_lo &= 0xffffffff;
2589 image_hi &= 0xffffffff;
2591 sign = (image_hi >> 31) & 1;
2592 exp = (image_hi >> 20) & 0x7ff;
2594 memset (r, 0, sizeof (*r));
2596 image_hi <<= 32 - 21;
2597 image_hi |= image_lo >> 21;
2598 image_hi &= 0x7fffffff;
2599 image_lo <<= 32 - 21;
2603 if ((image_hi || image_lo) && fmt->has_denorm)
2605 r->class = rvc_normal;
2608 if (HOST_BITS_PER_LONG == 32)
2610 image_hi = (image_hi << 1) | (image_lo >> 31);
2612 r->sig[SIGSZ-1] = image_hi;
2613 r->sig[SIGSZ-2] = image_lo;
2617 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2618 r->sig[SIGSZ-1] = image_hi;
2622 else if (fmt->has_signed_zero)
2625 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2627 if (image_hi || image_lo)
2631 if (HOST_BITS_PER_LONG == 32)
2633 r->sig[SIGSZ-1] = image_hi;
2634 r->sig[SIGSZ-2] = image_lo;
2637 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
2639 if (!fmt->qnan_msb_set)
2640 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2650 r->class = rvc_normal;
2652 r->exp = exp - 1023 + 1;
2653 if (HOST_BITS_PER_LONG == 32)
2655 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
2656 r->sig[SIGSZ-2] = image_lo;
2659 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2663 const struct real_format ieee_double =
2680 /* IEEE extended double precision format. This comes in three
2681 flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2684 static void encode_ieee_extended PARAMS ((const struct real_format *fmt,
2685 long *, const REAL_VALUE_TYPE *));
2686 static void decode_ieee_extended PARAMS ((const struct real_format *,
2687 REAL_VALUE_TYPE *, const long *));
2689 static void encode_ieee_extended_128 PARAMS ((const struct real_format *fmt,
2691 const REAL_VALUE_TYPE *));
2692 static void decode_ieee_extended_128 PARAMS ((const struct real_format *,
2697 encode_ieee_extended (fmt, buf, r)
2698 const struct real_format *fmt;
2700 const REAL_VALUE_TYPE *r;
2702 unsigned long image_hi, sig_hi, sig_lo;
2703 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2705 image_hi = r->sign << 15;
2706 sig_hi = sig_lo = 0;
2718 /* Intel requires the explicit integer bit to be set, otherwise
2719 it considers the value a "pseudo-infinity". Motorola docs
2720 say it doesn't care. */
2721 sig_hi = 0x80000000;
2726 sig_lo = sig_hi = 0xffffffff;
2734 if (HOST_BITS_PER_LONG == 32)
2736 sig_hi = r->sig[SIGSZ-1];
2737 sig_lo = r->sig[SIGSZ-2];
2741 sig_lo = r->sig[SIGSZ-1];
2742 sig_hi = sig_lo >> 31 >> 1;
2743 sig_lo &= 0xffffffff;
2745 if (!fmt->qnan_msb_set)
2746 sig_hi ^= 1 << 30 | 1 << 29;
2748 /* Intel requires the explicit integer bit to be set, otherwise
2749 it considers the value a "pseudo-nan". Motorola docs say it
2751 sig_hi |= 0x80000000;
2756 sig_lo = sig_hi = 0xffffffff;
2764 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2765 whereas the intermediate representation is 0.F x 2**exp.
2766 Which means we're off by one.
2768 Except for Motorola, which consider exp=0 and explicit
2769 integer bit set to continue to be normalized. In theory
2770 this descrepency has been taken care of by the difference
2771 in fmt->emin in round_for_format. */
2783 if (HOST_BITS_PER_LONG == 32)
2785 sig_hi = r->sig[SIGSZ-1];
2786 sig_lo = r->sig[SIGSZ-2];
2790 sig_lo = r->sig[SIGSZ-1];
2791 sig_hi = sig_lo >> 31 >> 1;
2792 sig_lo &= 0xffffffff;
2801 if (FLOAT_WORDS_BIG_ENDIAN)
2802 buf[0] = image_hi << 16, buf[1] = sig_hi, buf[2] = sig_lo;
2804 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
2808 encode_ieee_extended_128 (fmt, buf, r)
2809 const struct real_format *fmt;
2811 const REAL_VALUE_TYPE *r;
2813 buf[3 * !FLOAT_WORDS_BIG_ENDIAN] = 0;
2814 encode_ieee_extended (fmt, buf+!!FLOAT_WORDS_BIG_ENDIAN, r);
2818 decode_ieee_extended (fmt, r, buf)
2819 const struct real_format *fmt;
2823 unsigned long image_hi, sig_hi, sig_lo;
2827 if (FLOAT_WORDS_BIG_ENDIAN)
2828 image_hi = buf[0] >> 16, sig_hi = buf[1], sig_lo = buf[2];
2830 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
2831 sig_lo &= 0xffffffff;
2832 sig_hi &= 0xffffffff;
2833 image_hi &= 0xffffffff;
2835 sign = (image_hi >> 15) & 1;
2836 exp = image_hi & 0x7fff;
2838 memset (r, 0, sizeof (*r));
2842 if ((sig_hi || sig_lo) && fmt->has_denorm)
2844 r->class = rvc_normal;
2847 /* When the IEEE format contains a hidden bit, we know that
2848 it's zero at this point, and so shift up the significand
2849 and decrease the exponent to match. In this case, Motorola
2850 defines the explicit integer bit to be valid, so we don't
2851 know whether the msb is set or not. */
2853 if (HOST_BITS_PER_LONG == 32)
2855 r->sig[SIGSZ-1] = sig_hi;
2856 r->sig[SIGSZ-2] = sig_lo;
2859 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2863 else if (fmt->has_signed_zero)
2866 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
2868 /* See above re "pseudo-infinities" and "pseudo-nans".
2869 Short summary is that the MSB will likely always be
2870 set, and that we don't care about it. */
2871 sig_hi &= 0x7fffffff;
2873 if (sig_hi || sig_lo)
2877 if (HOST_BITS_PER_LONG == 32)
2879 r->sig[SIGSZ-1] = sig_hi;
2880 r->sig[SIGSZ-2] = sig_lo;
2883 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2885 if (!fmt->qnan_msb_set)
2886 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2896 r->class = rvc_normal;
2898 r->exp = exp - 16383 + 1;
2899 if (HOST_BITS_PER_LONG == 32)
2901 r->sig[SIGSZ-1] = sig_hi;
2902 r->sig[SIGSZ-2] = sig_lo;
2905 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2910 decode_ieee_extended_128 (fmt, r, buf)
2911 const struct real_format *fmt;
2915 decode_ieee_extended (fmt, r, buf+!!FLOAT_WORDS_BIG_ENDIAN);
2918 const struct real_format ieee_extended_motorola =
2920 encode_ieee_extended,
2921 decode_ieee_extended,
2934 const struct real_format ieee_extended_intel_96 =
2936 encode_ieee_extended,
2937 decode_ieee_extended,
2950 const struct real_format ieee_extended_intel_128 =
2952 encode_ieee_extended_128,
2953 decode_ieee_extended_128,
2967 /* IEEE quad precision format. */
2969 static void encode_ieee_quad PARAMS ((const struct real_format *fmt,
2970 long *, const REAL_VALUE_TYPE *));
2971 static void decode_ieee_quad PARAMS ((const struct real_format *,
2972 REAL_VALUE_TYPE *, const long *));
2975 encode_ieee_quad (fmt, buf, r)
2976 const struct real_format *fmt;
2978 const REAL_VALUE_TYPE *r;
2980 unsigned long image3, image2, image1, image0, exp;
2981 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2984 image3 = r->sign << 31;
2989 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
2998 image3 |= 32767 << 16;
3001 image3 |= 0x7fffffff;
3002 image2 = 0xffffffff;
3003 image1 = 0xffffffff;
3004 image0 = 0xffffffff;
3011 image3 |= 32767 << 16;
3013 if (HOST_BITS_PER_LONG == 32)
3018 image3 |= u.sig[3] & 0xffff;
3023 image1 = image0 >> 31 >> 1;
3025 image3 |= (image2 >> 31 >> 1) & 0xffff;
3026 image0 &= 0xffffffff;
3027 image2 &= 0xffffffff;
3030 if (!fmt->qnan_msb_set)
3031 image3 ^= 1 << 15 | 1 << 14;
3035 image3 |= 0x7fffffff;
3036 image2 = 0xffffffff;
3037 image1 = 0xffffffff;
3038 image0 = 0xffffffff;
3043 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3044 whereas the intermediate representation is 0.F x 2**exp.
3045 Which means we're off by one. */
3049 exp = r->exp + 16383 - 1;
3050 image3 |= exp << 16;
3052 if (HOST_BITS_PER_LONG == 32)
3057 image3 |= u.sig[3] & 0xffff;
3062 image1 = image0 >> 31 >> 1;
3064 image3 |= (image2 >> 31 >> 1) & 0xffff;
3065 image0 &= 0xffffffff;
3066 image2 &= 0xffffffff;
3074 if (FLOAT_WORDS_BIG_ENDIAN)
3091 decode_ieee_quad (fmt, r, buf)
3092 const struct real_format *fmt;
3096 unsigned long image3, image2, image1, image0;
3100 if (FLOAT_WORDS_BIG_ENDIAN)
3114 image0 &= 0xffffffff;
3115 image1 &= 0xffffffff;
3116 image2 &= 0xffffffff;
3118 sign = (image3 >> 31) & 1;
3119 exp = (image3 >> 16) & 0x7fff;
3122 memset (r, 0, sizeof (*r));
3126 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3128 r->class = rvc_normal;
3131 r->exp = -16382 + (SIGNIFICAND_BITS - 112);
3132 if (HOST_BITS_PER_LONG == 32)
3141 r->sig[0] = (image1 << 31 << 1) | image0;
3142 r->sig[1] = (image3 << 31 << 1) | image2;
3147 else if (fmt->has_signed_zero)
3150 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3152 if (image3 | image2 | image1 | image0)
3157 if (HOST_BITS_PER_LONG == 32)
3166 r->sig[0] = (image1 << 31 << 1) | image0;
3167 r->sig[1] = (image3 << 31 << 1) | image2;
3169 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3171 if (!fmt->qnan_msb_set)
3172 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
3182 r->class = rvc_normal;
3184 r->exp = exp - 16383 + 1;
3186 if (HOST_BITS_PER_LONG == 32)
3195 r->sig[0] = (image1 << 31 << 1) | image0;
3196 r->sig[1] = (image3 << 31 << 1) | image2;
3198 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3199 r->sig[SIGSZ-1] |= SIG_MSB;
3203 const struct real_format ieee_quad =
3220 /* The VAX floating point formats. */
3222 static void encode_vax_f PARAMS ((const struct real_format *fmt,
3223 long *, const REAL_VALUE_TYPE *));
3224 static void decode_vax_f PARAMS ((const struct real_format *,
3225 REAL_VALUE_TYPE *, const long *));
3226 static void encode_vax_d PARAMS ((const struct real_format *fmt,
3227 long *, const REAL_VALUE_TYPE *));
3228 static void decode_vax_d PARAMS ((const struct real_format *,
3229 REAL_VALUE_TYPE *, const long *));
3230 static void encode_vax_g PARAMS ((const struct real_format *fmt,
3231 long *, const REAL_VALUE_TYPE *));
3232 static void decode_vax_g PARAMS ((const struct real_format *,
3233 REAL_VALUE_TYPE *, const long *));
3236 encode_vax_f (fmt, buf, r)
3237 const struct real_format *fmt ATTRIBUTE_UNUSED;
3239 const REAL_VALUE_TYPE *r;
3241 unsigned long sign, exp, sig, image;
3243 sign = r->sign << 15;
3253 image = 0xffff7fff | sign;
3257 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3260 image = (sig << 16) & 0xffff0000;
3274 decode_vax_f (fmt, r, buf)
3275 const struct real_format *fmt ATTRIBUTE_UNUSED;
3279 unsigned long image = buf[0] & 0xffffffff;
3280 int exp = (image >> 7) & 0xff;
3282 memset (r, 0, sizeof (*r));
3286 r->class = rvc_normal;
3287 r->sign = (image >> 15) & 1;
3290 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3291 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3296 encode_vax_d (fmt, buf, r)
3297 const struct real_format *fmt ATTRIBUTE_UNUSED;
3299 const REAL_VALUE_TYPE *r;
3301 unsigned long image0, image1, sign = r->sign << 15;
3306 image0 = image1 = 0;
3311 image0 = 0xffff7fff | sign;
3312 image1 = 0xffffffff;
3316 /* Extract the significand into straight hi:lo. */
3317 if (HOST_BITS_PER_LONG == 64)
3319 image0 = r->sig[SIGSZ-1];
3320 image1 = (image0 >> (64 - 56)) & 0xffffffff;
3321 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3325 image0 = r->sig[SIGSZ-1];
3326 image1 = r->sig[SIGSZ-2];
3327 image1 = (image0 << 24) | (image1 >> 8);
3328 image0 = (image0 >> 8) & 0xffffff;
3331 /* Rearrange the half-words of the significand to match the
3333 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3334 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3336 /* Add the sign and exponent. */
3338 image0 |= (r->exp + 128) << 7;
3345 if (FLOAT_WORDS_BIG_ENDIAN)
3346 buf[0] = image1, buf[1] = image0;
3348 buf[0] = image0, buf[1] = image1;
3352 decode_vax_d (fmt, r, buf)
3353 const struct real_format *fmt ATTRIBUTE_UNUSED;
3357 unsigned long image0, image1;
3360 if (FLOAT_WORDS_BIG_ENDIAN)
3361 image1 = buf[0], image0 = buf[1];
3363 image0 = buf[0], image1 = buf[1];
3364 image0 &= 0xffffffff;
3365 image1 &= 0xffffffff;
3367 exp = (image0 >> 7) & 0x7f;
3369 memset (r, 0, sizeof (*r));
3373 r->class = rvc_normal;
3374 r->sign = (image0 >> 15) & 1;
3377 /* Rearrange the half-words of the external format into
3378 proper ascending order. */
3379 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
3380 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3382 if (HOST_BITS_PER_LONG == 64)
3384 image0 = (image0 << 31 << 1) | image1;
3387 r->sig[SIGSZ-1] = image0;
3391 r->sig[SIGSZ-1] = image0;
3392 r->sig[SIGSZ-2] = image1;
3393 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
3394 r->sig[SIGSZ-1] |= SIG_MSB;
3400 encode_vax_g (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 - 53)) & 0xffffffff;
3425 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
3429 image0 = r->sig[SIGSZ-1];
3430 image1 = r->sig[SIGSZ-2];
3431 image1 = (image0 << 21) | (image1 >> 11);
3432 image0 = (image0 >> 11) & 0xfffff;
3435 /* Rearrange the half-words of the significand to match the
3437 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
3438 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3440 /* Add the sign and exponent. */
3442 image0 |= (r->exp + 1024) << 4;
3449 if (FLOAT_WORDS_BIG_ENDIAN)
3450 buf[0] = image1, buf[1] = image0;
3452 buf[0] = image0, buf[1] = image1;
3456 decode_vax_g (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 >> 4) & 0x7ff;
3473 memset (r, 0, sizeof (*r));
3477 r->class = rvc_normal;
3478 r->sign = (image0 >> 15) & 1;
3479 r->exp = exp - 1024;
3481 /* Rearrange the half-words of the external format into
3482 proper ascending order. */
3483 image0 = ((image0 & 0xf) << 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, 64 - 53);
3498 r->sig[SIGSZ-1] |= SIG_MSB;
3503 const struct real_format vax_f_format =
3519 const struct real_format vax_d_format =
3535 const struct real_format vax_g_format =
3552 /* The IBM S/390 floating point formats. A good reference for these can
3553 be found in chapter 9 of "ESA/390 Principles of Operation", IBM document
3554 number SA22-7201-01. An on-line version can be found here:
3556 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3559 static void encode_i370_single PARAMS ((const struct real_format *fmt,
3560 long *, const REAL_VALUE_TYPE *));
3561 static void decode_i370_single PARAMS ((const struct real_format *,
3562 REAL_VALUE_TYPE *, const long *));
3563 static void encode_i370_double PARAMS ((const struct real_format *fmt,
3564 long *, const REAL_VALUE_TYPE *));
3565 static void decode_i370_double PARAMS ((const struct real_format *,
3566 REAL_VALUE_TYPE *, const long *));
3569 encode_i370_single (fmt, buf, r)
3570 const struct real_format *fmt ATTRIBUTE_UNUSED;
3572 const REAL_VALUE_TYPE *r;
3574 unsigned long sign, exp, sig, image;
3576 sign = r->sign << 31;
3586 image = 0x7fffffff | sign;
3590 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
3591 exp = ((r->exp / 4) + 64) << 24;
3592 image = sign | exp | sig;
3603 decode_i370_single (fmt, r, buf)
3604 const struct real_format *fmt ATTRIBUTE_UNUSED;
3608 unsigned long sign, sig, image = buf[0];
3611 sign = (image >> 31) & 1;
3612 exp = (image >> 24) & 0x7f;
3613 sig = image & 0xffffff;
3615 memset (r, 0, sizeof (*r));
3619 r->class = rvc_normal;
3621 r->exp = (exp - 64) * 4;
3622 r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
3628 encode_i370_double (fmt, buf, r)
3629 const struct real_format *fmt ATTRIBUTE_UNUSED;
3631 const REAL_VALUE_TYPE *r;
3633 unsigned long sign, exp, image_hi, image_lo;
3635 sign = r->sign << 31;
3640 image_hi = image_lo = 0;
3645 image_hi = 0x7fffffff | sign;
3646 image_lo = 0xffffffff;
3650 if (HOST_BITS_PER_LONG == 64)
3652 image_hi = r->sig[SIGSZ-1];
3653 image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
3654 image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
3658 image_hi = r->sig[SIGSZ-1];
3659 image_lo = r->sig[SIGSZ-2];
3660 image_lo = (image_lo >> 8) | (image_hi << 24);
3664 exp = ((r->exp / 4) + 64) << 24;
3665 image_hi |= sign | exp;
3672 if (FLOAT_WORDS_BIG_ENDIAN)
3673 buf[0] = image_hi, buf[1] = image_lo;
3675 buf[0] = image_lo, buf[1] = image_hi;
3679 decode_i370_double (fmt, r, buf)
3680 const struct real_format *fmt ATTRIBUTE_UNUSED;
3684 unsigned long sign, image_hi, image_lo;
3687 if (FLOAT_WORDS_BIG_ENDIAN)
3688 image_hi = buf[0], image_lo = buf[1];
3690 image_lo = buf[0], image_hi = buf[1];
3692 sign = (image_hi >> 31) & 1;
3693 exp = (image_hi >> 24) & 0x7f;
3694 image_hi &= 0xffffff;
3695 image_lo &= 0xffffffff;
3697 memset (r, 0, sizeof (*r));
3699 if (exp || image_hi || image_lo)
3701 r->class = rvc_normal;
3703 r->exp = (exp - 64) * 4 + (SIGNIFICAND_BITS - 56);
3705 if (HOST_BITS_PER_LONG == 32)
3707 r->sig[0] = image_lo;
3708 r->sig[1] = image_hi;
3711 r->sig[0] = image_lo | (image_hi << 31 << 1);
3717 const struct real_format i370_single =
3728 false, /* ??? The encoding does allow for "unnormals". */
3729 false, /* ??? The encoding does allow for "unnormals". */
3733 const struct real_format i370_double =
3744 false, /* ??? The encoding does allow for "unnormals". */
3745 false, /* ??? The encoding does allow for "unnormals". */
3750 /* TMS320C[34]x twos complement floating point format. */
3752 static void encode_c4x_single PARAMS ((const struct real_format *fmt,
3753 long *, const REAL_VALUE_TYPE *));
3754 static void decode_c4x_single PARAMS ((const struct real_format *,
3755 REAL_VALUE_TYPE *, const long *));
3756 static void encode_c4x_extended PARAMS ((const struct real_format *fmt,
3757 long *, const REAL_VALUE_TYPE *));
3758 static void decode_c4x_extended PARAMS ((const struct real_format *,
3759 REAL_VALUE_TYPE *, const long *));
3762 encode_c4x_single (fmt, buf, r)
3763 const struct real_format *fmt ATTRIBUTE_UNUSED;
3765 const REAL_VALUE_TYPE *r;
3767 unsigned long image, exp, sig;
3779 sig = 0x800000 - r->sign;
3784 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3799 image = ((exp & 0xff) << 24) | (sig & 0xffffff);
3804 decode_c4x_single (fmt, r, buf)
3805 const struct real_format *fmt ATTRIBUTE_UNUSED;
3809 unsigned long image = buf[0];
3813 exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
3814 sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
3816 memset (r, 0, sizeof (*r));
3820 r->class = rvc_normal;
3822 sig = sf & 0x7fffff;
3831 sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3834 r->sig[SIGSZ-1] = sig;
3839 encode_c4x_extended (fmt, buf, r)
3840 const struct real_format *fmt ATTRIBUTE_UNUSED;
3842 const REAL_VALUE_TYPE *r;
3844 unsigned long exp, sig;
3856 sig = 0x80000000 - r->sign;
3862 sig = r->sig[SIGSZ-1];
3863 if (HOST_BITS_PER_LONG == 64)
3864 sig = sig >> 1 >> 31;
3881 exp = (exp & 0xff) << 24;
3884 if (FLOAT_WORDS_BIG_ENDIAN)
3885 buf[0] = exp, buf[1] = sig;
3887 buf[0] = sig, buf[0] = exp;
3891 decode_c4x_extended (fmt, r, buf)
3892 const struct real_format *fmt ATTRIBUTE_UNUSED;
3899 if (FLOAT_WORDS_BIG_ENDIAN)
3900 exp = buf[0], sf = buf[1];
3902 sf = buf[0], exp = buf[1];
3904 exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
3905 sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
3907 memset (r, 0, sizeof (*r));
3911 r->class = rvc_normal;
3913 sig = sf & 0x7fffffff;
3922 if (HOST_BITS_PER_LONG == 64)
3923 sig = sig << 1 << 31;
3927 r->sig[SIGSZ-1] = sig;
3931 const struct real_format c4x_single =
3947 const struct real_format c4x_extended =
3949 encode_c4x_extended,
3950 decode_c4x_extended,
3964 /* Initialize things at start of compilation. */
3966 static const struct real_format * format_for_size PARAMS ((int));
3968 static const struct real_format *
3969 format_for_size (size)
3972 #ifndef TARGET_G_FORMAT
3973 #define TARGET_G_FORMAT 0
3976 switch (TARGET_FLOAT_FORMAT)
3978 case IEEE_FLOAT_FORMAT:
3982 return &ieee_single;
3985 return &ieee_double;
3988 if (!INTEL_EXTENDED_IEEE_FORMAT)
3989 return &ieee_extended_motorola;
3991 return &ieee_extended_intel_96;
3994 if (!INTEL_EXTENDED_IEEE_FORMAT)
3997 return &ieee_extended_intel_128;
4001 case VAX_FLOAT_FORMAT:
4005 return &vax_f_format;
4008 if (TARGET_G_FORMAT)
4009 return &vax_g_format;
4011 return &vax_d_format;
4015 case IBM_FLOAT_FORMAT:
4019 return &i370_single;
4021 return &i370_double;
4025 case C4X_FLOAT_FORMAT:
4031 return &c4x_extended;
4044 /* Set up the mode->format table. */
4045 for (i = 0; i < 3; ++i)
4047 enum machine_mode mode;
4051 size = FLOAT_TYPE_SIZE;
4053 size = DOUBLE_TYPE_SIZE;
4055 size = LONG_DOUBLE_TYPE_SIZE;
4057 mode = mode_for_size (size, MODE_FLOAT, 0);
4058 if (mode == BLKmode)
4061 fmt_for_mode[mode - QFmode] = format_for_size (size);