1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2002, 2003, 2004, 2005, 2007 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 3, 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 COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
33 /* The floating point model used internally is not exactly IEEE 754
34 compliant, and close to the description in the ISO C99 standard,
35 section 5.2.4.2.2 Characteristics of floating types.
39 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
43 b = base or radix, here always 2
45 p = precision (the number of base-b digits in the significand)
46 f_k = the digits of the significand.
48 We differ from typical IEEE 754 encodings in that the entire
49 significand is fractional. Normalized significands are in the
52 A requirement of the model is that P be larger than the largest
53 supported target floating-point type by at least 2 bits. This gives
54 us proper rounding when we truncate to the target type. In addition,
55 E must be large enough to hold the smallest supported denormal number
58 Both of these requirements are easily satisfied. The largest target
59 significand is 113 bits; we store at least 160. The smallest
60 denormal number fits in 17 exponent bits; we store 27.
62 Note that the decimal string conversion routines are sensitive to
63 rounding errors. Since the raw arithmetic routines do not themselves
64 have guard digits or rounding, the computation of 10**exp can
65 accumulate more than a few digits of error. The previous incarnation
66 of real.c successfully used a 144-bit fraction; given the current
67 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits. */
70 /* Used to classify two numbers simultaneously. */
71 #define CLASS2(A, B) ((A) << 2 | (B))
73 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
74 #error "Some constant folding done by hand to avoid shift count warnings"
77 static void get_zero (REAL_VALUE_TYPE *, int);
78 static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
79 static void get_canonical_snan (REAL_VALUE_TYPE *, int);
80 static void get_inf (REAL_VALUE_TYPE *, int);
81 static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
82 const REAL_VALUE_TYPE *, unsigned int);
83 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
85 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
87 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
88 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
89 const REAL_VALUE_TYPE *);
90 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
91 const REAL_VALUE_TYPE *, int);
92 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
93 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
94 static int cmp_significand_0 (const REAL_VALUE_TYPE *);
95 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
96 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
97 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
98 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
99 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
100 const REAL_VALUE_TYPE *);
101 static void normalize (REAL_VALUE_TYPE *);
103 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
104 const REAL_VALUE_TYPE *, int);
105 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
106 const REAL_VALUE_TYPE *);
107 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
108 const REAL_VALUE_TYPE *);
109 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
110 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
112 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
114 static const REAL_VALUE_TYPE * ten_to_ptwo (int);
115 static const REAL_VALUE_TYPE * ten_to_mptwo (int);
116 static const REAL_VALUE_TYPE * real_digit (int);
117 static void times_pten (REAL_VALUE_TYPE *, int);
119 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
121 /* Initialize R with a positive zero. */
124 get_zero (REAL_VALUE_TYPE *r, int sign)
126 memset (r, 0, sizeof (*r));
130 /* Initialize R with the canonical quiet NaN. */
133 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
135 memset (r, 0, sizeof (*r));
142 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
144 memset (r, 0, sizeof (*r));
152 get_inf (REAL_VALUE_TYPE *r, int sign)
154 memset (r, 0, sizeof (*r));
160 /* Right-shift the significand of A by N bits; put the result in the
161 significand of R. If any one bits are shifted out, return true. */
164 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
167 unsigned long sticky = 0;
168 unsigned int i, ofs = 0;
170 if (n >= HOST_BITS_PER_LONG)
172 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
174 n &= HOST_BITS_PER_LONG - 1;
179 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
180 for (i = 0; i < SIGSZ; ++i)
183 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
184 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
185 << (HOST_BITS_PER_LONG - n)));
190 for (i = 0; ofs + i < SIGSZ; ++i)
191 r->sig[i] = a->sig[ofs + i];
192 for (; i < SIGSZ; ++i)
199 /* Right-shift the significand of A by N bits; put the result in the
203 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
206 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
208 n &= HOST_BITS_PER_LONG - 1;
211 for (i = 0; i < SIGSZ; ++i)
214 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
215 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
216 << (HOST_BITS_PER_LONG - n)));
221 for (i = 0; ofs + i < SIGSZ; ++i)
222 r->sig[i] = a->sig[ofs + i];
223 for (; i < SIGSZ; ++i)
228 /* Left-shift the significand of A by N bits; put the result in the
232 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
235 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
237 n &= HOST_BITS_PER_LONG - 1;
240 for (i = 0; ofs + i < SIGSZ; ++i)
241 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
242 for (; i < SIGSZ; ++i)
243 r->sig[SIGSZ-1-i] = 0;
246 for (i = 0; i < SIGSZ; ++i)
249 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
250 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
251 >> (HOST_BITS_PER_LONG - n)));
255 /* Likewise, but N is specialized to 1. */
258 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
262 for (i = SIGSZ - 1; i > 0; --i)
263 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
264 r->sig[0] = a->sig[0] << 1;
267 /* Add the significands of A and B, placing the result in R. Return
268 true if there was carry out of the most significant word. */
271 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
272 const REAL_VALUE_TYPE *b)
277 for (i = 0; i < SIGSZ; ++i)
279 unsigned long ai = a->sig[i];
280 unsigned long ri = ai + b->sig[i];
296 /* Subtract the significands of A and B, placing the result in R. CARRY is
297 true if there's a borrow incoming to the least significant word.
298 Return true if there was borrow out of the most significant word. */
301 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
302 const REAL_VALUE_TYPE *b, int carry)
306 for (i = 0; i < SIGSZ; ++i)
308 unsigned long ai = a->sig[i];
309 unsigned long ri = ai - b->sig[i];
325 /* Negate the significand A, placing the result in R. */
328 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
333 for (i = 0; i < SIGSZ; ++i)
335 unsigned long ri, ai = a->sig[i];
354 /* Compare significands. Return tri-state vs zero. */
357 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
361 for (i = SIGSZ - 1; i >= 0; --i)
363 unsigned long ai = a->sig[i];
364 unsigned long bi = b->sig[i];
375 /* Return true if A is nonzero. */
378 cmp_significand_0 (const REAL_VALUE_TYPE *a)
382 for (i = SIGSZ - 1; i >= 0; --i)
389 /* Set bit N of the significand of R. */
392 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
394 r->sig[n / HOST_BITS_PER_LONG]
395 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
398 /* Clear bit N of the significand of R. */
401 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
403 r->sig[n / HOST_BITS_PER_LONG]
404 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
407 /* Test bit N of the significand of R. */
410 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
412 /* ??? Compiler bug here if we return this expression directly.
413 The conversion to bool strips the "&1" and we wind up testing
414 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
415 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
419 /* Clear bits 0..N-1 of the significand of R. */
422 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
424 int i, w = n / HOST_BITS_PER_LONG;
426 for (i = 0; i < w; ++i)
429 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
432 /* Divide the significands of A and B, placing the result in R. Return
433 true if the division was inexact. */
436 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
437 const REAL_VALUE_TYPE *b)
440 int i, bit = SIGNIFICAND_BITS - 1;
441 unsigned long msb, inexact;
444 memset (r->sig, 0, sizeof (r->sig));
450 msb = u.sig[SIGSZ-1] & SIG_MSB;
451 lshift_significand_1 (&u, &u);
453 if (msb || cmp_significands (&u, b) >= 0)
455 sub_significands (&u, &u, b, 0);
456 set_significand_bit (r, bit);
461 for (i = 0, inexact = 0; i < SIGSZ; i++)
467 /* Adjust the exponent and significand of R such that the most
468 significant bit is set. We underflow to zero and overflow to
469 infinity here, without denormals. (The intermediate representation
470 exponent is large enough to handle target denormals normalized.) */
473 normalize (REAL_VALUE_TYPE *r)
481 /* Find the first word that is nonzero. */
482 for (i = SIGSZ - 1; i >= 0; i--)
484 shift += HOST_BITS_PER_LONG;
488 /* Zero significand flushes to zero. */
496 /* Find the first bit that is nonzero. */
498 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
504 exp = REAL_EXP (r) - shift;
506 get_inf (r, r->sign);
507 else if (exp < -MAX_EXP)
508 get_zero (r, r->sign);
511 SET_REAL_EXP (r, exp);
512 lshift_significand (r, r, shift);
517 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
518 result may be inexact due to a loss of precision. */
521 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
522 const REAL_VALUE_TYPE *b, int subtract_p)
526 bool inexact = false;
528 /* Determine if we need to add or subtract. */
530 subtract_p = (sign ^ b->sign) ^ subtract_p;
532 switch (CLASS2 (a->cl, b->cl))
534 case CLASS2 (rvc_zero, rvc_zero):
535 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
536 get_zero (r, sign & !subtract_p);
539 case CLASS2 (rvc_zero, rvc_normal):
540 case CLASS2 (rvc_zero, rvc_inf):
541 case CLASS2 (rvc_zero, rvc_nan):
543 case CLASS2 (rvc_normal, rvc_nan):
544 case CLASS2 (rvc_inf, rvc_nan):
545 case CLASS2 (rvc_nan, rvc_nan):
546 /* ANY + NaN = NaN. */
547 case CLASS2 (rvc_normal, rvc_inf):
550 r->sign = sign ^ subtract_p;
553 case CLASS2 (rvc_normal, rvc_zero):
554 case CLASS2 (rvc_inf, rvc_zero):
555 case CLASS2 (rvc_nan, rvc_zero):
557 case CLASS2 (rvc_nan, rvc_normal):
558 case CLASS2 (rvc_nan, rvc_inf):
559 /* NaN + ANY = NaN. */
560 case CLASS2 (rvc_inf, rvc_normal):
565 case CLASS2 (rvc_inf, rvc_inf):
567 /* Inf - Inf = NaN. */
568 get_canonical_qnan (r, 0);
570 /* Inf + Inf = Inf. */
574 case CLASS2 (rvc_normal, rvc_normal):
581 /* Swap the arguments such that A has the larger exponent. */
582 dexp = REAL_EXP (a) - REAL_EXP (b);
585 const REAL_VALUE_TYPE *t;
592 /* If the exponents are not identical, we need to shift the
593 significand of B down. */
596 /* If the exponents are too far apart, the significands
597 do not overlap, which makes the subtraction a noop. */
598 if (dexp >= SIGNIFICAND_BITS)
605 inexact |= sticky_rshift_significand (&t, b, dexp);
611 if (sub_significands (r, a, b, inexact))
613 /* We got a borrow out of the subtraction. That means that
614 A and B had the same exponent, and B had the larger
615 significand. We need to swap the sign and negate the
618 neg_significand (r, r);
623 if (add_significands (r, a, b))
625 /* We got carry out of the addition. This means we need to
626 shift the significand back down one bit and increase the
628 inexact |= sticky_rshift_significand (r, r, 1);
629 r->sig[SIGSZ-1] |= SIG_MSB;
640 SET_REAL_EXP (r, exp);
641 /* Zero out the remaining fields. */
646 /* Re-normalize the result. */
649 /* Special case: if the subtraction results in zero, the result
651 if (r->cl == rvc_zero)
654 r->sig[0] |= inexact;
659 /* Calculate R = A * B. Return true if the result may be inexact. */
662 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
663 const REAL_VALUE_TYPE *b)
665 REAL_VALUE_TYPE u, t, *rr;
666 unsigned int i, j, k;
667 int sign = a->sign ^ b->sign;
668 bool inexact = false;
670 switch (CLASS2 (a->cl, b->cl))
672 case CLASS2 (rvc_zero, rvc_zero):
673 case CLASS2 (rvc_zero, rvc_normal):
674 case CLASS2 (rvc_normal, rvc_zero):
675 /* +-0 * ANY = 0 with appropriate sign. */
679 case CLASS2 (rvc_zero, rvc_nan):
680 case CLASS2 (rvc_normal, rvc_nan):
681 case CLASS2 (rvc_inf, rvc_nan):
682 case CLASS2 (rvc_nan, rvc_nan):
683 /* ANY * NaN = NaN. */
688 case CLASS2 (rvc_nan, rvc_zero):
689 case CLASS2 (rvc_nan, rvc_normal):
690 case CLASS2 (rvc_nan, rvc_inf):
691 /* NaN * ANY = NaN. */
696 case CLASS2 (rvc_zero, rvc_inf):
697 case CLASS2 (rvc_inf, rvc_zero):
699 get_canonical_qnan (r, sign);
702 case CLASS2 (rvc_inf, rvc_inf):
703 case CLASS2 (rvc_normal, rvc_inf):
704 case CLASS2 (rvc_inf, rvc_normal):
705 /* Inf * Inf = Inf, R * Inf = Inf */
709 case CLASS2 (rvc_normal, rvc_normal):
716 if (r == a || r == b)
722 /* Collect all the partial products. Since we don't have sure access
723 to a widening multiply, we split each long into two half-words.
725 Consider the long-hand form of a four half-word multiplication:
735 We construct partial products of the widened half-word products
736 that are known to not overlap, e.g. DF+DH. Each such partial
737 product is given its proper exponent, which allows us to sum them
738 and obtain the finished product. */
740 for (i = 0; i < SIGSZ * 2; ++i)
742 unsigned long ai = a->sig[i / 2];
744 ai >>= HOST_BITS_PER_LONG / 2;
746 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
751 for (j = 0; j < 2; ++j)
753 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
754 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
763 /* Would underflow to zero, which we shouldn't bother adding. */
768 memset (&u, 0, sizeof (u));
770 SET_REAL_EXP (&u, exp);
772 for (k = j; k < SIGSZ * 2; k += 2)
774 unsigned long bi = b->sig[k / 2];
776 bi >>= HOST_BITS_PER_LONG / 2;
778 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
780 u.sig[k / 2] = ai * bi;
784 inexact |= do_add (rr, rr, &u, 0);
795 /* Calculate R = A / B. Return true if the result may be inexact. */
798 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
799 const REAL_VALUE_TYPE *b)
801 int exp, sign = a->sign ^ b->sign;
802 REAL_VALUE_TYPE t, *rr;
805 switch (CLASS2 (a->cl, b->cl))
807 case CLASS2 (rvc_zero, rvc_zero):
809 case CLASS2 (rvc_inf, rvc_inf):
810 /* Inf / Inf = NaN. */
811 get_canonical_qnan (r, sign);
814 case CLASS2 (rvc_zero, rvc_normal):
815 case CLASS2 (rvc_zero, rvc_inf):
817 case CLASS2 (rvc_normal, rvc_inf):
822 case CLASS2 (rvc_normal, rvc_zero):
824 case CLASS2 (rvc_inf, rvc_zero):
829 case CLASS2 (rvc_zero, rvc_nan):
830 case CLASS2 (rvc_normal, rvc_nan):
831 case CLASS2 (rvc_inf, rvc_nan):
832 case CLASS2 (rvc_nan, rvc_nan):
833 /* ANY / NaN = NaN. */
838 case CLASS2 (rvc_nan, rvc_zero):
839 case CLASS2 (rvc_nan, rvc_normal):
840 case CLASS2 (rvc_nan, rvc_inf):
841 /* NaN / ANY = NaN. */
846 case CLASS2 (rvc_inf, rvc_normal):
851 case CLASS2 (rvc_normal, rvc_normal):
858 if (r == a || r == b)
863 /* Make sure all fields in the result are initialized. */
868 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
879 SET_REAL_EXP (rr, exp);
881 inexact = div_significands (rr, a, b);
883 /* Re-normalize the result. */
885 rr->sig[0] |= inexact;
893 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
894 one of the two operands is a NaN. */
897 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
902 switch (CLASS2 (a->cl, b->cl))
904 case CLASS2 (rvc_zero, rvc_zero):
905 /* Sign of zero doesn't matter for compares. */
908 case CLASS2 (rvc_inf, rvc_zero):
909 case CLASS2 (rvc_inf, rvc_normal):
910 case CLASS2 (rvc_normal, rvc_zero):
911 return (a->sign ? -1 : 1);
913 case CLASS2 (rvc_inf, rvc_inf):
914 return -a->sign - -b->sign;
916 case CLASS2 (rvc_zero, rvc_normal):
917 case CLASS2 (rvc_zero, rvc_inf):
918 case CLASS2 (rvc_normal, rvc_inf):
919 return (b->sign ? 1 : -1);
921 case CLASS2 (rvc_zero, rvc_nan):
922 case CLASS2 (rvc_normal, rvc_nan):
923 case CLASS2 (rvc_inf, rvc_nan):
924 case CLASS2 (rvc_nan, rvc_nan):
925 case CLASS2 (rvc_nan, rvc_zero):
926 case CLASS2 (rvc_nan, rvc_normal):
927 case CLASS2 (rvc_nan, rvc_inf):
930 case CLASS2 (rvc_normal, rvc_normal):
937 if (a->sign != b->sign)
938 return -a->sign - -b->sign;
940 if (a->decimal || b->decimal)
941 return decimal_do_compare (a, b, nan_result);
943 if (REAL_EXP (a) > REAL_EXP (b))
945 else if (REAL_EXP (a) < REAL_EXP (b))
948 ret = cmp_significands (a, b);
950 return (a->sign ? -ret : ret);
953 /* Return A truncated to an integral value toward zero. */
956 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
970 decimal_do_fix_trunc (r, a);
973 if (REAL_EXP (r) <= 0)
974 get_zero (r, r->sign);
975 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
976 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
984 /* Perform the binary or unary operation described by CODE.
985 For a unary operation, leave OP1 NULL. This function returns
986 true if the result may be inexact due to loss of precision. */
989 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
990 const REAL_VALUE_TYPE *op1)
992 enum tree_code code = icode;
994 if (op0->decimal || (op1 && op1->decimal))
995 return decimal_real_arithmetic (r, icode, op0, op1);
1000 return do_add (r, op0, op1, 0);
1003 return do_add (r, op0, op1, 1);
1006 return do_multiply (r, op0, op1);
1009 return do_divide (r, op0, op1);
1012 if (op1->cl == rvc_nan)
1014 else if (do_compare (op0, op1, -1) < 0)
1021 if (op1->cl == rvc_nan)
1023 else if (do_compare (op0, op1, 1) < 0)
1039 case FIX_TRUNC_EXPR:
1040 do_fix_trunc (r, op0);
1049 /* Legacy. Similar, but return the result directly. */
1052 real_arithmetic2 (int icode, const REAL_VALUE_TYPE *op0,
1053 const REAL_VALUE_TYPE *op1)
1056 real_arithmetic (&r, icode, op0, op1);
1061 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1062 const REAL_VALUE_TYPE *op1)
1064 enum tree_code code = icode;
1069 return do_compare (op0, op1, 1) < 0;
1071 return do_compare (op0, op1, 1) <= 0;
1073 return do_compare (op0, op1, -1) > 0;
1075 return do_compare (op0, op1, -1) >= 0;
1077 return do_compare (op0, op1, -1) == 0;
1079 return do_compare (op0, op1, -1) != 0;
1080 case UNORDERED_EXPR:
1081 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1083 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1085 return do_compare (op0, op1, -1) < 0;
1087 return do_compare (op0, op1, -1) <= 0;
1089 return do_compare (op0, op1, 1) > 0;
1091 return do_compare (op0, op1, 1) >= 0;
1093 return do_compare (op0, op1, 0) == 0;
1095 return do_compare (op0, op1, 0) != 0;
1102 /* Return floor log2(R). */
1105 real_exponent (const REAL_VALUE_TYPE *r)
1113 return (unsigned int)-1 >> 1;
1115 return REAL_EXP (r);
1121 /* R = OP0 * 2**EXP. */
1124 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1135 exp += REAL_EXP (op0);
1137 get_inf (r, r->sign);
1138 else if (exp < -MAX_EXP)
1139 get_zero (r, r->sign);
1141 SET_REAL_EXP (r, exp);
1149 /* Determine whether a floating-point value X is infinite. */
1152 real_isinf (const REAL_VALUE_TYPE *r)
1154 return (r->cl == rvc_inf);
1157 /* Determine whether a floating-point value X is a NaN. */
1160 real_isnan (const REAL_VALUE_TYPE *r)
1162 return (r->cl == rvc_nan);
1165 /* Determine whether a floating-point value X is finite. */
1168 real_isfinite (const REAL_VALUE_TYPE *r)
1170 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1173 /* Determine whether a floating-point value X is negative. */
1176 real_isneg (const REAL_VALUE_TYPE *r)
1181 /* Determine whether a floating-point value X is minus zero. */
1184 real_isnegzero (const REAL_VALUE_TYPE *r)
1186 return r->sign && r->cl == rvc_zero;
1189 /* Compare two floating-point objects for bitwise identity. */
1192 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1198 if (a->sign != b->sign)
1208 if (a->decimal != b->decimal)
1210 if (REAL_EXP (a) != REAL_EXP (b))
1215 if (a->signalling != b->signalling)
1217 /* The significand is ignored for canonical NaNs. */
1218 if (a->canonical || b->canonical)
1219 return a->canonical == b->canonical;
1226 for (i = 0; i < SIGSZ; ++i)
1227 if (a->sig[i] != b->sig[i])
1233 /* Try to change R into its exact multiplicative inverse in machine
1234 mode MODE. Return true if successful. */
1237 exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r)
1239 const REAL_VALUE_TYPE *one = real_digit (1);
1243 if (r->cl != rvc_normal)
1246 /* Check for a power of two: all significand bits zero except the MSB. */
1247 for (i = 0; i < SIGSZ-1; ++i)
1250 if (r->sig[SIGSZ-1] != SIG_MSB)
1253 /* Find the inverse and truncate to the required mode. */
1254 do_divide (&u, one, r);
1255 real_convert (&u, mode, &u);
1257 /* The rounding may have overflowed. */
1258 if (u.cl != rvc_normal)
1260 for (i = 0; i < SIGSZ-1; ++i)
1263 if (u.sig[SIGSZ-1] != SIG_MSB)
1270 /* Render R as an integer. */
1273 real_to_integer (const REAL_VALUE_TYPE *r)
1275 unsigned HOST_WIDE_INT i;
1286 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1293 return decimal_real_to_integer (r);
1295 if (REAL_EXP (r) <= 0)
1297 /* Only force overflow for unsigned overflow. Signed overflow is
1298 undefined, so it doesn't matter what we return, and some callers
1299 expect to be able to use this routine for both signed and
1300 unsigned conversions. */
1301 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1304 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1305 i = r->sig[SIGSZ-1];
1308 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1309 i = r->sig[SIGSZ-1];
1310 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1311 i |= r->sig[SIGSZ-2];
1314 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1325 /* Likewise, but to an integer pair, HI+LOW. */
1328 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
1329 const REAL_VALUE_TYPE *r)
1332 HOST_WIDE_INT low, high;
1345 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1358 decimal_real_to_integer2 (plow, phigh, r);
1365 /* Only force overflow for unsigned overflow. Signed overflow is
1366 undefined, so it doesn't matter what we return, and some callers
1367 expect to be able to use this routine for both signed and
1368 unsigned conversions. */
1369 if (exp > 2*HOST_BITS_PER_WIDE_INT)
1372 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1373 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1375 high = t.sig[SIGSZ-1];
1376 low = t.sig[SIGSZ-2];
1380 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1381 high = t.sig[SIGSZ-1];
1382 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1383 high |= t.sig[SIGSZ-2];
1385 low = t.sig[SIGSZ-3];
1386 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1387 low |= t.sig[SIGSZ-4];
1395 low = -low, high = ~high;
1407 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1408 of NUM / DEN. Return the quotient and place the remainder in NUM.
1409 It is expected that NUM / DEN are close enough that the quotient is
1412 static unsigned long
1413 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1415 unsigned long q, msb;
1416 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1425 msb = num->sig[SIGSZ-1] & SIG_MSB;
1427 lshift_significand_1 (num, num);
1429 if (msb || cmp_significands (num, den) >= 0)
1431 sub_significands (num, num, den, 0);
1435 while (--expn >= expd);
1437 SET_REAL_EXP (num, expd);
1443 /* Render R as a decimal floating point constant. Emit DIGITS significant
1444 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1445 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1448 #define M_LOG10_2 0.30102999566398119521
1451 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1452 size_t digits, int crop_trailing_zeros)
1454 const REAL_VALUE_TYPE *one, *ten;
1455 REAL_VALUE_TYPE r, pten, u, v;
1456 int dec_exp, cmp_one, digit;
1458 char *p, *first, *last;
1465 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1470 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1473 /* ??? Print the significand as well, if not canonical? */
1474 strcpy (str, (r.sign ? "-NaN" : "+NaN"));
1482 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1486 /* Bound the number of digits printed by the size of the representation. */
1487 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1488 if (digits == 0 || digits > max_digits)
1489 digits = max_digits;
1491 /* Estimate the decimal exponent, and compute the length of the string it
1492 will print as. Be conservative and add one to account for possible
1493 overflow or rounding error. */
1494 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1495 for (max_digits = 1; dec_exp ; max_digits++)
1498 /* Bound the number of digits printed by the size of the output buffer. */
1499 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1500 gcc_assert (max_digits <= buf_size);
1501 if (digits > max_digits)
1502 digits = max_digits;
1504 one = real_digit (1);
1505 ten = ten_to_ptwo (0);
1513 cmp_one = do_compare (&r, one, 0);
1518 /* Number is greater than one. Convert significand to an integer
1519 and strip trailing decimal zeros. */
1522 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1524 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1525 m = floor_log2 (max_digits);
1527 /* Iterate over the bits of the possible powers of 10 that might
1528 be present in U and eliminate them. That is, if we find that
1529 10**2**M divides U evenly, keep the division and increase
1535 do_divide (&t, &u, ten_to_ptwo (m));
1536 do_fix_trunc (&v, &t);
1537 if (cmp_significands (&v, &t) == 0)
1545 /* Revert the scaling to integer that we performed earlier. */
1546 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1547 - (SIGNIFICAND_BITS - 1));
1550 /* Find power of 10. Do this by dividing out 10**2**M when
1551 this is larger than the current remainder. Fill PTEN with
1552 the power of 10 that we compute. */
1553 if (REAL_EXP (&r) > 0)
1555 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1558 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1559 if (do_compare (&u, ptentwo, 0) >= 0)
1561 do_divide (&u, &u, ptentwo);
1562 do_multiply (&pten, &pten, ptentwo);
1569 /* We managed to divide off enough tens in the above reduction
1570 loop that we've now got a negative exponent. Fall into the
1571 less-than-one code to compute the proper value for PTEN. */
1578 /* Number is less than one. Pad significand with leading
1584 /* Stop if we'd shift bits off the bottom. */
1588 do_multiply (&u, &v, ten);
1590 /* Stop if we're now >= 1. */
1591 if (REAL_EXP (&u) > 0)
1599 /* Find power of 10. Do this by multiplying in P=10**2**M when
1600 the current remainder is smaller than 1/P. Fill PTEN with the
1601 power of 10 that we compute. */
1602 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1605 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1606 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1608 if (do_compare (&v, ptenmtwo, 0) <= 0)
1610 do_multiply (&v, &v, ptentwo);
1611 do_multiply (&pten, &pten, ptentwo);
1617 /* Invert the positive power of 10 that we've collected so far. */
1618 do_divide (&pten, one, &pten);
1626 /* At this point, PTEN should contain the nearest power of 10 smaller
1627 than R, such that this division produces the first digit.
1629 Using a divide-step primitive that returns the complete integral
1630 remainder avoids the rounding error that would be produced if
1631 we were to use do_divide here and then simply multiply by 10 for
1632 each subsequent digit. */
1634 digit = rtd_divmod (&r, &pten);
1636 /* Be prepared for error in that division via underflow ... */
1637 if (digit == 0 && cmp_significand_0 (&r))
1639 /* Multiply by 10 and try again. */
1640 do_multiply (&r, &r, ten);
1641 digit = rtd_divmod (&r, &pten);
1643 gcc_assert (digit != 0);
1646 /* ... or overflow. */
1656 gcc_assert (digit <= 10);
1660 /* Generate subsequent digits. */
1661 while (--digits > 0)
1663 do_multiply (&r, &r, ten);
1664 digit = rtd_divmod (&r, &pten);
1669 /* Generate one more digit with which to do rounding. */
1670 do_multiply (&r, &r, ten);
1671 digit = rtd_divmod (&r, &pten);
1673 /* Round the result. */
1676 /* Round to nearest. If R is nonzero there are additional
1677 nonzero digits to be extracted. */
1678 if (cmp_significand_0 (&r))
1680 /* Round to even. */
1681 else if ((p[-1] - '0') & 1)
1698 /* Carry out of the first digit. This means we had all 9's and
1699 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1707 /* Insert the decimal point. */
1708 first[0] = first[1];
1711 /* If requested, drop trailing zeros. Never crop past "1.0". */
1712 if (crop_trailing_zeros)
1713 while (last > first + 3 && last[-1] == '0')
1716 /* Append the exponent. */
1717 sprintf (last, "e%+d", dec_exp);
1720 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1721 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1722 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1723 strip trailing zeros. */
1726 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1727 size_t digits, int crop_trailing_zeros)
1729 int i, j, exp = REAL_EXP (r);
1742 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1745 /* ??? Print the significand as well, if not canonical? */
1746 strcpy (str, (r->sign ? "-NaN" : "+NaN"));
1754 /* Hexadecimal format for decimal floats is not interesting. */
1755 strcpy (str, "N/A");
1760 digits = SIGNIFICAND_BITS / 4;
1762 /* Bound the number of digits printed by the size of the output buffer. */
1764 sprintf (exp_buf, "p%+d", exp);
1765 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1766 gcc_assert (max_digits <= buf_size);
1767 if (digits > max_digits)
1768 digits = max_digits;
1779 for (i = SIGSZ - 1; i >= 0; --i)
1780 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1782 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1788 if (crop_trailing_zeros)
1789 while (p > first + 1 && p[-1] == '0')
1792 sprintf (p, "p%+d", exp);
1795 /* Initialize R from a decimal or hexadecimal string. The string is
1796 assumed to have been syntax checked already. Return -1 if the
1797 value underflows, +1 if overflows, and 0 otherwise. */
1800 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1812 else if (*str == '+')
1815 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1817 /* Hexadecimal floating point. */
1818 int pos = SIGNIFICAND_BITS - 4, d;
1826 d = hex_value (*str);
1831 r->sig[pos / HOST_BITS_PER_LONG]
1832 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1836 /* Ensure correct rounding by setting last bit if there is
1837 a subsequent nonzero digit. */
1845 if (pos == SIGNIFICAND_BITS - 4)
1852 d = hex_value (*str);
1857 r->sig[pos / HOST_BITS_PER_LONG]
1858 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1862 /* Ensure correct rounding by setting last bit if there is
1863 a subsequent nonzero digit. */
1869 /* If the mantissa is zero, ignore the exponent. */
1870 if (!cmp_significand_0 (r))
1873 if (*str == 'p' || *str == 'P')
1875 bool exp_neg = false;
1883 else if (*str == '+')
1887 while (ISDIGIT (*str))
1893 /* Overflowed the exponent. */
1908 SET_REAL_EXP (r, exp);
1914 /* Decimal floating point. */
1915 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1920 while (ISDIGIT (*str))
1923 do_multiply (r, r, ten);
1925 do_add (r, r, real_digit (d), 0);
1930 if (r->cl == rvc_zero)
1935 while (ISDIGIT (*str))
1938 do_multiply (r, r, ten);
1940 do_add (r, r, real_digit (d), 0);
1945 /* If the mantissa is zero, ignore the exponent. */
1946 if (r->cl == rvc_zero)
1949 if (*str == 'e' || *str == 'E')
1951 bool exp_neg = false;
1959 else if (*str == '+')
1963 while (ISDIGIT (*str))
1969 /* Overflowed the exponent. */
1983 times_pten (r, exp);
2002 /* Legacy. Similar, but return the result directly. */
2005 real_from_string2 (const char *s, enum machine_mode mode)
2009 real_from_string (&r, s);
2010 if (mode != VOIDmode)
2011 real_convert (&r, mode, &r);
2016 /* Initialize R from string S and desired MODE. */
2019 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2021 if (DECIMAL_FLOAT_MODE_P (mode))
2022 decimal_real_from_string (r, s);
2024 real_from_string (r, s);
2026 if (mode != VOIDmode)
2027 real_convert (r, mode, r);
2030 /* Initialize R from the integer pair HIGH+LOW. */
2033 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2034 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2037 if (low == 0 && high == 0)
2041 memset (r, 0, sizeof (*r));
2043 r->sign = high < 0 && !unsigned_p;
2044 SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT);
2055 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2057 r->sig[SIGSZ-1] = high;
2058 r->sig[SIGSZ-2] = low;
2062 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2063 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2064 r->sig[SIGSZ-2] = high;
2065 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2066 r->sig[SIGSZ-4] = low;
2072 if (mode != VOIDmode)
2073 real_convert (r, mode, r);
2076 /* Returns 10**2**N. */
2078 static const REAL_VALUE_TYPE *
2081 static REAL_VALUE_TYPE tens[EXP_BITS];
2083 gcc_assert (n >= 0);
2084 gcc_assert (n < EXP_BITS);
2086 if (tens[n].cl == rvc_zero)
2088 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2090 HOST_WIDE_INT t = 10;
2093 for (i = 0; i < n; ++i)
2096 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2100 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2101 do_multiply (&tens[n], t, t);
2108 /* Returns 10**(-2**N). */
2110 static const REAL_VALUE_TYPE *
2111 ten_to_mptwo (int n)
2113 static REAL_VALUE_TYPE tens[EXP_BITS];
2115 gcc_assert (n >= 0);
2116 gcc_assert (n < EXP_BITS);
2118 if (tens[n].cl == rvc_zero)
2119 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2126 static const REAL_VALUE_TYPE *
2129 static REAL_VALUE_TYPE num[10];
2131 gcc_assert (n >= 0);
2132 gcc_assert (n <= 9);
2134 if (n > 0 && num[n].cl == rvc_zero)
2135 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2140 /* Multiply R by 10**EXP. */
2143 times_pten (REAL_VALUE_TYPE *r, int exp)
2145 REAL_VALUE_TYPE pten, *rr;
2146 bool negative = (exp < 0);
2152 pten = *real_digit (1);
2158 for (i = 0; exp > 0; ++i, exp >>= 1)
2160 do_multiply (rr, rr, ten_to_ptwo (i));
2163 do_divide (r, r, &pten);
2166 /* Fills R with +Inf. */
2169 real_inf (REAL_VALUE_TYPE *r)
2174 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2175 we force a QNaN, else we force an SNaN. The string, if not empty,
2176 is parsed as a number and placed in the significand. Return true
2177 if the string was successfully parsed. */
2180 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2181 enum machine_mode mode)
2183 const struct real_format *fmt;
2185 fmt = REAL_MODE_FORMAT (mode);
2191 get_canonical_qnan (r, 0);
2193 get_canonical_snan (r, 0);
2199 memset (r, 0, sizeof (*r));
2202 /* Parse akin to strtol into the significand of R. */
2204 while (ISSPACE (*str))
2208 else if (*str == '+')
2213 if (*str == 'x' || *str == 'X')
2222 while ((d = hex_value (*str)) < base)
2229 lshift_significand (r, r, 3);
2232 lshift_significand (r, r, 4);
2235 lshift_significand_1 (&u, r);
2236 lshift_significand (r, r, 3);
2237 add_significands (r, r, &u);
2245 add_significands (r, r, &u);
2250 /* Must have consumed the entire string for success. */
2254 /* Shift the significand into place such that the bits
2255 are in the most significant bits for the format. */
2256 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2258 /* Our MSB is always unset for NaNs. */
2259 r->sig[SIGSZ-1] &= ~SIG_MSB;
2261 /* Force quiet or signalling NaN. */
2262 r->signalling = !quiet;
2268 /* Fills R with the largest finite value representable in mode MODE.
2269 If SIGN is nonzero, R is set to the most negative finite value. */
2272 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2274 const struct real_format *fmt;
2277 fmt = REAL_MODE_FORMAT (mode);
2279 memset (r, 0, sizeof (*r));
2282 decimal_real_maxval (r, sign, mode);
2287 SET_REAL_EXP (r, fmt->emax);
2289 np2 = SIGNIFICAND_BITS - fmt->p;
2290 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2291 clear_significand_below (r, np2);
2293 if (fmt->pnan < fmt->p)
2294 /* This is an IBM extended double format made up of two IEEE
2295 doubles. The value of the long double is the sum of the
2296 values of the two parts. The most significant part is
2297 required to be the value of the long double rounded to the
2298 nearest double. Rounding means we need a slightly smaller
2299 value for LDBL_MAX. */
2300 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan);
2304 /* Fills R with 2**N. */
2307 real_2expN (REAL_VALUE_TYPE *r, int n, enum machine_mode fmode)
2309 memset (r, 0, sizeof (*r));
2314 else if (n < -MAX_EXP)
2319 SET_REAL_EXP (r, n);
2320 r->sig[SIGSZ-1] = SIG_MSB;
2323 if (DECIMAL_FLOAT_MODE_P (fmode))
2324 decimal_real_convert (r, fmode, r);
2329 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2332 unsigned long sticky;
2340 decimal_round_for_format (fmt, r);
2343 /* FIXME. We can come here via fp_easy_constant
2344 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2345 investigated whether this convert needs to be here, or
2346 something else is missing. */
2347 decimal_real_convert (r, DFmode, r);
2351 emin2m1 = fmt->emin - 1;
2354 np2 = SIGNIFICAND_BITS - p2;
2358 get_zero (r, r->sign);
2360 if (!fmt->has_signed_zero)
2365 get_inf (r, r->sign);
2370 clear_significand_below (r, np2);
2380 /* Check the range of the exponent. If we're out of range,
2381 either underflow or overflow. */
2382 if (REAL_EXP (r) > emax2)
2384 else if (REAL_EXP (r) <= emin2m1)
2388 if (!fmt->has_denorm)
2390 /* Don't underflow completely until we've had a chance to round. */
2391 if (REAL_EXP (r) < emin2m1)
2396 diff = emin2m1 - REAL_EXP (r) + 1;
2400 /* De-normalize the significand. */
2401 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2402 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2406 /* There are P2 true significand bits, followed by one guard bit,
2407 followed by one sticky bit, followed by stuff. Fold nonzero
2408 stuff into the sticky bit. */
2411 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2412 sticky |= r->sig[i];
2414 r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2416 guard = test_significand_bit (r, np2 - 1);
2417 lsb = test_significand_bit (r, np2);
2419 /* Round to even. */
2420 if (guard && (sticky || lsb))
2424 set_significand_bit (&u, np2);
2426 if (add_significands (r, r, &u))
2428 /* Overflow. Means the significand had been all ones, and
2429 is now all zeros. Need to increase the exponent, and
2430 possibly re-normalize it. */
2431 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2432 if (REAL_EXP (r) > emax2)
2434 r->sig[SIGSZ-1] = SIG_MSB;
2438 /* Catch underflow that we deferred until after rounding. */
2439 if (REAL_EXP (r) <= emin2m1)
2442 /* Clear out trailing garbage. */
2443 clear_significand_below (r, np2);
2446 /* Extend or truncate to a new mode. */
2449 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2450 const REAL_VALUE_TYPE *a)
2452 const struct real_format *fmt;
2454 fmt = REAL_MODE_FORMAT (mode);
2459 if (a->decimal || fmt->b == 10)
2460 decimal_real_convert (r, mode, a);
2462 round_for_format (fmt, r);
2464 /* round_for_format de-normalizes denormals. Undo just that part. */
2465 if (r->cl == rvc_normal)
2469 /* Legacy. Likewise, except return the struct directly. */
2472 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2475 real_convert (&r, mode, &a);
2479 /* Return true if truncating to MODE is exact. */
2482 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2484 const struct real_format *fmt;
2488 fmt = REAL_MODE_FORMAT (mode);
2491 /* Don't allow conversion to denormals. */
2492 emin2m1 = fmt->emin - 1;
2493 if (REAL_EXP (a) <= emin2m1)
2496 /* After conversion to the new mode, the value must be identical. */
2497 real_convert (&t, mode, a);
2498 return real_identical (&t, a);
2501 /* Write R to the given target format. Place the words of the result
2502 in target word order in BUF. There are always 32 bits in each
2503 long, no matter the size of the host long.
2505 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2508 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2509 const struct real_format *fmt)
2515 round_for_format (fmt, &r);
2519 (*fmt->encode) (fmt, buf, &r);
2524 /* Similar, but look up the format from MODE. */
2527 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2529 const struct real_format *fmt;
2531 fmt = REAL_MODE_FORMAT (mode);
2534 return real_to_target_fmt (buf, r, fmt);
2537 /* Read R from the given target format. Read the words of the result
2538 in target word order in BUF. There are always 32 bits in each
2539 long, no matter the size of the host long. */
2542 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2543 const struct real_format *fmt)
2545 (*fmt->decode) (fmt, r, buf);
2548 /* Similar, but look up the format from MODE. */
2551 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2553 const struct real_format *fmt;
2555 fmt = REAL_MODE_FORMAT (mode);
2558 (*fmt->decode) (fmt, r, buf);
2561 /* Return the number of bits of the largest binary value that the
2562 significand of MODE will hold. */
2563 /* ??? Legacy. Should get access to real_format directly. */
2566 significand_size (enum machine_mode mode)
2568 const struct real_format *fmt;
2570 fmt = REAL_MODE_FORMAT (mode);
2576 /* Return the size in bits of the largest binary value that can be
2577 held by the decimal coefficient for this mode. This is one more
2578 than the number of bits required to hold the largest coefficient
2580 double log2_10 = 3.3219281;
2581 return fmt->p * log2_10;
2586 /* Return a hash value for the given real value. */
2587 /* ??? The "unsigned int" return value is intended to be hashval_t,
2588 but I didn't want to pull hashtab.h into real.h. */
2591 real_hash (const REAL_VALUE_TYPE *r)
2596 h = r->cl | (r->sign << 2);
2604 h |= REAL_EXP (r) << 3;
2609 h ^= (unsigned int)-1;
2618 if (sizeof(unsigned long) > sizeof(unsigned int))
2619 for (i = 0; i < SIGSZ; ++i)
2621 unsigned long s = r->sig[i];
2622 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2625 for (i = 0; i < SIGSZ; ++i)
2631 /* IEEE single-precision format. */
2633 static void encode_ieee_single (const struct real_format *fmt,
2634 long *, const REAL_VALUE_TYPE *);
2635 static void decode_ieee_single (const struct real_format *,
2636 REAL_VALUE_TYPE *, const long *);
2639 encode_ieee_single (const struct real_format *fmt, long *buf,
2640 const REAL_VALUE_TYPE *r)
2642 unsigned long image, sig, exp;
2643 unsigned long sign = r->sign;
2644 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2647 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2658 image |= 0x7fffffff;
2665 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2666 if (r->signalling == fmt->qnan_msb_set)
2677 image |= 0x7fffffff;
2681 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2682 whereas the intermediate representation is 0.F x 2**exp.
2683 Which means we're off by one. */
2687 exp = REAL_EXP (r) + 127 - 1;
2700 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2703 unsigned long image = buf[0] & 0xffffffff;
2704 bool sign = (image >> 31) & 1;
2705 int exp = (image >> 23) & 0xff;
2707 memset (r, 0, sizeof (*r));
2708 image <<= HOST_BITS_PER_LONG - 24;
2713 if (image && fmt->has_denorm)
2717 SET_REAL_EXP (r, -126);
2718 r->sig[SIGSZ-1] = image << 1;
2721 else if (fmt->has_signed_zero)
2724 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2730 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2731 ^ fmt->qnan_msb_set);
2732 r->sig[SIGSZ-1] = image;
2744 SET_REAL_EXP (r, exp - 127 + 1);
2745 r->sig[SIGSZ-1] = image | SIG_MSB;
2749 const struct real_format ieee_single_format =
2768 const struct real_format mips_single_format =
2787 const struct real_format motorola_single_format =
2806 /* IEEE double-precision format. */
2808 static void encode_ieee_double (const struct real_format *fmt,
2809 long *, const REAL_VALUE_TYPE *);
2810 static void decode_ieee_double (const struct real_format *,
2811 REAL_VALUE_TYPE *, const long *);
2814 encode_ieee_double (const struct real_format *fmt, long *buf,
2815 const REAL_VALUE_TYPE *r)
2817 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2818 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2820 image_hi = r->sign << 31;
2823 if (HOST_BITS_PER_LONG == 64)
2825 sig_hi = r->sig[SIGSZ-1];
2826 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2827 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2831 sig_hi = r->sig[SIGSZ-1];
2832 sig_lo = r->sig[SIGSZ-2];
2833 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2834 sig_hi = (sig_hi >> 11) & 0xfffff;
2844 image_hi |= 2047 << 20;
2847 image_hi |= 0x7fffffff;
2848 image_lo = 0xffffffff;
2857 if (fmt->canonical_nan_lsbs_set)
2859 sig_hi = (1 << 19) - 1;
2860 sig_lo = 0xffffffff;
2868 if (r->signalling == fmt->qnan_msb_set)
2869 sig_hi &= ~(1 << 19);
2872 if (sig_hi == 0 && sig_lo == 0)
2875 image_hi |= 2047 << 20;
2881 image_hi |= 0x7fffffff;
2882 image_lo = 0xffffffff;
2887 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2888 whereas the intermediate representation is 0.F x 2**exp.
2889 Which means we're off by one. */
2893 exp = REAL_EXP (r) + 1023 - 1;
2894 image_hi |= exp << 20;
2903 if (FLOAT_WORDS_BIG_ENDIAN)
2904 buf[0] = image_hi, buf[1] = image_lo;
2906 buf[0] = image_lo, buf[1] = image_hi;
2910 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2913 unsigned long image_hi, image_lo;
2917 if (FLOAT_WORDS_BIG_ENDIAN)
2918 image_hi = buf[0], image_lo = buf[1];
2920 image_lo = buf[0], image_hi = buf[1];
2921 image_lo &= 0xffffffff;
2922 image_hi &= 0xffffffff;
2924 sign = (image_hi >> 31) & 1;
2925 exp = (image_hi >> 20) & 0x7ff;
2927 memset (r, 0, sizeof (*r));
2929 image_hi <<= 32 - 21;
2930 image_hi |= image_lo >> 21;
2931 image_hi &= 0x7fffffff;
2932 image_lo <<= 32 - 21;
2936 if ((image_hi || image_lo) && fmt->has_denorm)
2940 SET_REAL_EXP (r, -1022);
2941 if (HOST_BITS_PER_LONG == 32)
2943 image_hi = (image_hi << 1) | (image_lo >> 31);
2945 r->sig[SIGSZ-1] = image_hi;
2946 r->sig[SIGSZ-2] = image_lo;
2950 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2951 r->sig[SIGSZ-1] = image_hi;
2955 else if (fmt->has_signed_zero)
2958 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2960 if (image_hi || image_lo)
2964 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
2965 if (HOST_BITS_PER_LONG == 32)
2967 r->sig[SIGSZ-1] = image_hi;
2968 r->sig[SIGSZ-2] = image_lo;
2971 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
2983 SET_REAL_EXP (r, exp - 1023 + 1);
2984 if (HOST_BITS_PER_LONG == 32)
2986 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
2987 r->sig[SIGSZ-2] = image_lo;
2990 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2994 const struct real_format ieee_double_format =
3013 const struct real_format mips_double_format =
3032 const struct real_format motorola_double_format =
3051 /* IEEE extended real format. This comes in three flavors: Intel's as
3052 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3053 12- and 16-byte images may be big- or little endian; Motorola's is
3054 always big endian. */
3056 /* Helper subroutine which converts from the internal format to the
3057 12-byte little-endian Intel format. Functions below adjust this
3058 for the other possible formats. */
3060 encode_ieee_extended (const struct real_format *fmt, long *buf,
3061 const REAL_VALUE_TYPE *r)
3063 unsigned long image_hi, sig_hi, sig_lo;
3064 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3066 image_hi = r->sign << 15;
3067 sig_hi = sig_lo = 0;
3079 /* Intel requires the explicit integer bit to be set, otherwise
3080 it considers the value a "pseudo-infinity". Motorola docs
3081 say it doesn't care. */
3082 sig_hi = 0x80000000;
3087 sig_lo = sig_hi = 0xffffffff;
3097 if (fmt->canonical_nan_lsbs_set)
3099 sig_hi = (1 << 30) - 1;
3100 sig_lo = 0xffffffff;
3103 else if (HOST_BITS_PER_LONG == 32)
3105 sig_hi = r->sig[SIGSZ-1];
3106 sig_lo = r->sig[SIGSZ-2];
3110 sig_lo = r->sig[SIGSZ-1];
3111 sig_hi = sig_lo >> 31 >> 1;
3112 sig_lo &= 0xffffffff;
3114 if (r->signalling == fmt->qnan_msb_set)
3115 sig_hi &= ~(1 << 30);
3118 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3121 /* Intel requires the explicit integer bit to be set, otherwise
3122 it considers the value a "pseudo-nan". Motorola docs say it
3124 sig_hi |= 0x80000000;
3129 sig_lo = sig_hi = 0xffffffff;
3135 int exp = REAL_EXP (r);
3137 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3138 whereas the intermediate representation is 0.F x 2**exp.
3139 Which means we're off by one.
3141 Except for Motorola, which consider exp=0 and explicit
3142 integer bit set to continue to be normalized. In theory
3143 this discrepancy has been taken care of by the difference
3144 in fmt->emin in round_for_format. */
3151 gcc_assert (exp >= 0);
3155 if (HOST_BITS_PER_LONG == 32)
3157 sig_hi = r->sig[SIGSZ-1];
3158 sig_lo = r->sig[SIGSZ-2];
3162 sig_lo = r->sig[SIGSZ-1];
3163 sig_hi = sig_lo >> 31 >> 1;
3164 sig_lo &= 0xffffffff;
3173 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3176 /* Convert from the internal format to the 12-byte Motorola format
3177 for an IEEE extended real. */
3179 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3180 const REAL_VALUE_TYPE *r)
3183 encode_ieee_extended (fmt, intermed, r);
3185 /* Motorola chips are assumed always to be big-endian. Also, the
3186 padding in a Motorola extended real goes between the exponent and
3187 the mantissa. At this point the mantissa is entirely within
3188 elements 0 and 1 of intermed, and the exponent entirely within
3189 element 2, so all we have to do is swap the order around, and
3190 shift element 2 left 16 bits. */
3191 buf[0] = intermed[2] << 16;
3192 buf[1] = intermed[1];
3193 buf[2] = intermed[0];
3196 /* Convert from the internal format to the 12-byte Intel format for
3197 an IEEE extended real. */
3199 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3200 const REAL_VALUE_TYPE *r)
3202 if (FLOAT_WORDS_BIG_ENDIAN)
3204 /* All the padding in an Intel-format extended real goes at the high
3205 end, which in this case is after the mantissa, not the exponent.
3206 Therefore we must shift everything down 16 bits. */
3208 encode_ieee_extended (fmt, intermed, r);
3209 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3210 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3211 buf[2] = (intermed[0] << 16);
3214 /* encode_ieee_extended produces what we want directly. */
3215 encode_ieee_extended (fmt, buf, r);
3218 /* Convert from the internal format to the 16-byte Intel format for
3219 an IEEE extended real. */
3221 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3222 const REAL_VALUE_TYPE *r)
3224 /* All the padding in an Intel-format extended real goes at the high end. */
3225 encode_ieee_extended_intel_96 (fmt, buf, r);
3229 /* As above, we have a helper function which converts from 12-byte
3230 little-endian Intel format to internal format. Functions below
3231 adjust for the other possible formats. */
3233 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3236 unsigned long image_hi, sig_hi, sig_lo;
3240 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3241 sig_lo &= 0xffffffff;
3242 sig_hi &= 0xffffffff;
3243 image_hi &= 0xffffffff;
3245 sign = (image_hi >> 15) & 1;
3246 exp = image_hi & 0x7fff;
3248 memset (r, 0, sizeof (*r));
3252 if ((sig_hi || sig_lo) && fmt->has_denorm)
3257 /* When the IEEE format contains a hidden bit, we know that
3258 it's zero at this point, and so shift up the significand
3259 and decrease the exponent to match. In this case, Motorola
3260 defines the explicit integer bit to be valid, so we don't
3261 know whether the msb is set or not. */
3262 SET_REAL_EXP (r, fmt->emin);
3263 if (HOST_BITS_PER_LONG == 32)
3265 r->sig[SIGSZ-1] = sig_hi;
3266 r->sig[SIGSZ-2] = sig_lo;
3269 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3273 else if (fmt->has_signed_zero)
3276 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3278 /* See above re "pseudo-infinities" and "pseudo-nans".
3279 Short summary is that the MSB will likely always be
3280 set, and that we don't care about it. */
3281 sig_hi &= 0x7fffffff;
3283 if (sig_hi || sig_lo)
3287 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3288 if (HOST_BITS_PER_LONG == 32)
3290 r->sig[SIGSZ-1] = sig_hi;
3291 r->sig[SIGSZ-2] = sig_lo;
3294 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3306 SET_REAL_EXP (r, exp - 16383 + 1);
3307 if (HOST_BITS_PER_LONG == 32)
3309 r->sig[SIGSZ-1] = sig_hi;
3310 r->sig[SIGSZ-2] = sig_lo;
3313 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3317 /* Convert from the internal format to the 12-byte Motorola format
3318 for an IEEE extended real. */
3320 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3325 /* Motorola chips are assumed always to be big-endian. Also, the
3326 padding in a Motorola extended real goes between the exponent and
3327 the mantissa; remove it. */
3328 intermed[0] = buf[2];
3329 intermed[1] = buf[1];
3330 intermed[2] = (unsigned long)buf[0] >> 16;
3332 decode_ieee_extended (fmt, r, intermed);
3335 /* Convert from the internal format to the 12-byte Intel format for
3336 an IEEE extended real. */
3338 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3341 if (FLOAT_WORDS_BIG_ENDIAN)
3343 /* All the padding in an Intel-format extended real goes at the high
3344 end, which in this case is after the mantissa, not the exponent.
3345 Therefore we must shift everything up 16 bits. */
3348 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3349 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3350 intermed[2] = ((unsigned long)buf[0] >> 16);
3352 decode_ieee_extended (fmt, r, intermed);
3355 /* decode_ieee_extended produces what we want directly. */
3356 decode_ieee_extended (fmt, r, buf);
3359 /* Convert from the internal format to the 16-byte Intel format for
3360 an IEEE extended real. */
3362 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3365 /* All the padding in an Intel-format extended real goes at the high end. */
3366 decode_ieee_extended_intel_96 (fmt, r, buf);
3369 const struct real_format ieee_extended_motorola_format =
3371 encode_ieee_extended_motorola,
3372 decode_ieee_extended_motorola,
3388 const struct real_format ieee_extended_intel_96_format =
3390 encode_ieee_extended_intel_96,
3391 decode_ieee_extended_intel_96,
3407 const struct real_format ieee_extended_intel_128_format =
3409 encode_ieee_extended_intel_128,
3410 decode_ieee_extended_intel_128,
3426 /* The following caters to i386 systems that set the rounding precision
3427 to 53 bits instead of 64, e.g. FreeBSD. */
3428 const struct real_format ieee_extended_intel_96_round_53_format =
3430 encode_ieee_extended_intel_96,
3431 decode_ieee_extended_intel_96,
3447 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3448 numbers whose sum is equal to the extended precision value. The number
3449 with greater magnitude is first. This format has the same magnitude
3450 range as an IEEE double precision value, but effectively 106 bits of
3451 significand precision. Infinity and NaN are represented by their IEEE
3452 double precision value stored in the first number, the second number is
3453 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3455 static void encode_ibm_extended (const struct real_format *fmt,
3456 long *, const REAL_VALUE_TYPE *);
3457 static void decode_ibm_extended (const struct real_format *,
3458 REAL_VALUE_TYPE *, const long *);
3461 encode_ibm_extended (const struct real_format *fmt, long *buf,
3462 const REAL_VALUE_TYPE *r)
3464 REAL_VALUE_TYPE u, normr, v;
3465 const struct real_format *base_fmt;
3467 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3469 /* Renormlize R before doing any arithmetic on it. */
3471 if (normr.cl == rvc_normal)
3474 /* u = IEEE double precision portion of significand. */
3476 round_for_format (base_fmt, &u);
3477 encode_ieee_double (base_fmt, &buf[0], &u);
3479 if (u.cl == rvc_normal)
3481 do_add (&v, &normr, &u, 1);
3482 /* Call round_for_format since we might need to denormalize. */
3483 round_for_format (base_fmt, &v);
3484 encode_ieee_double (base_fmt, &buf[2], &v);
3488 /* Inf, NaN, 0 are all representable as doubles, so the
3489 least-significant part can be 0.0. */
3496 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3499 REAL_VALUE_TYPE u, v;
3500 const struct real_format *base_fmt;
3502 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3503 decode_ieee_double (base_fmt, &u, &buf[0]);
3505 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3507 decode_ieee_double (base_fmt, &v, &buf[2]);
3508 do_add (r, &u, &v, 0);
3514 const struct real_format ibm_extended_format =
3516 encode_ibm_extended,
3517 decode_ibm_extended,
3533 const struct real_format mips_extended_format =
3535 encode_ibm_extended,
3536 decode_ibm_extended,
3553 /* IEEE quad precision format. */
3555 static void encode_ieee_quad (const struct real_format *fmt,
3556 long *, const REAL_VALUE_TYPE *);
3557 static void decode_ieee_quad (const struct real_format *,
3558 REAL_VALUE_TYPE *, const long *);
3561 encode_ieee_quad (const struct real_format *fmt, long *buf,
3562 const REAL_VALUE_TYPE *r)
3564 unsigned long image3, image2, image1, image0, exp;
3565 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3568 image3 = r->sign << 31;
3573 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3582 image3 |= 32767 << 16;
3585 image3 |= 0x7fffffff;
3586 image2 = 0xffffffff;
3587 image1 = 0xffffffff;
3588 image0 = 0xffffffff;
3595 image3 |= 32767 << 16;
3599 if (fmt->canonical_nan_lsbs_set)
3602 image2 = image1 = image0 = 0xffffffff;
3605 else if (HOST_BITS_PER_LONG == 32)
3610 image3 |= u.sig[3] & 0xffff;
3615 image1 = image0 >> 31 >> 1;
3617 image3 |= (image2 >> 31 >> 1) & 0xffff;
3618 image0 &= 0xffffffff;
3619 image2 &= 0xffffffff;
3621 if (r->signalling == fmt->qnan_msb_set)
3625 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3630 image3 |= 0x7fffffff;
3631 image2 = 0xffffffff;
3632 image1 = 0xffffffff;
3633 image0 = 0xffffffff;
3638 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3639 whereas the intermediate representation is 0.F x 2**exp.
3640 Which means we're off by one. */
3644 exp = REAL_EXP (r) + 16383 - 1;
3645 image3 |= exp << 16;
3647 if (HOST_BITS_PER_LONG == 32)
3652 image3 |= u.sig[3] & 0xffff;
3657 image1 = image0 >> 31 >> 1;
3659 image3 |= (image2 >> 31 >> 1) & 0xffff;
3660 image0 &= 0xffffffff;
3661 image2 &= 0xffffffff;
3669 if (FLOAT_WORDS_BIG_ENDIAN)
3686 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3689 unsigned long image3, image2, image1, image0;
3693 if (FLOAT_WORDS_BIG_ENDIAN)
3707 image0 &= 0xffffffff;
3708 image1 &= 0xffffffff;
3709 image2 &= 0xffffffff;
3711 sign = (image3 >> 31) & 1;
3712 exp = (image3 >> 16) & 0x7fff;
3715 memset (r, 0, sizeof (*r));
3719 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3724 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
3725 if (HOST_BITS_PER_LONG == 32)
3734 r->sig[0] = (image1 << 31 << 1) | image0;
3735 r->sig[1] = (image3 << 31 << 1) | image2;
3740 else if (fmt->has_signed_zero)
3743 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3745 if (image3 | image2 | image1 | image0)
3749 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
3751 if (HOST_BITS_PER_LONG == 32)
3760 r->sig[0] = (image1 << 31 << 1) | image0;
3761 r->sig[1] = (image3 << 31 << 1) | image2;
3763 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3775 SET_REAL_EXP (r, exp - 16383 + 1);
3777 if (HOST_BITS_PER_LONG == 32)
3786 r->sig[0] = (image1 << 31 << 1) | image0;
3787 r->sig[1] = (image3 << 31 << 1) | image2;
3789 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3790 r->sig[SIGSZ-1] |= SIG_MSB;
3794 const struct real_format ieee_quad_format =
3813 const struct real_format mips_quad_format =
3832 /* Descriptions of VAX floating point formats can be found beginning at
3834 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
3836 The thing to remember is that they're almost IEEE, except for word
3837 order, exponent bias, and the lack of infinities, nans, and denormals.
3839 We don't implement the H_floating format here, simply because neither
3840 the VAX or Alpha ports use it. */
3842 static void encode_vax_f (const struct real_format *fmt,
3843 long *, const REAL_VALUE_TYPE *);
3844 static void decode_vax_f (const struct real_format *,
3845 REAL_VALUE_TYPE *, const long *);
3846 static void encode_vax_d (const struct real_format *fmt,
3847 long *, const REAL_VALUE_TYPE *);
3848 static void decode_vax_d (const struct real_format *,
3849 REAL_VALUE_TYPE *, const long *);
3850 static void encode_vax_g (const struct real_format *fmt,
3851 long *, const REAL_VALUE_TYPE *);
3852 static void decode_vax_g (const struct real_format *,
3853 REAL_VALUE_TYPE *, const long *);
3856 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
3857 const REAL_VALUE_TYPE *r)
3859 unsigned long sign, exp, sig, image;
3861 sign = r->sign << 15;
3871 image = 0xffff7fff | sign;
3875 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3876 exp = REAL_EXP (r) + 128;
3878 image = (sig << 16) & 0xffff0000;
3892 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
3893 REAL_VALUE_TYPE *r, const long *buf)
3895 unsigned long image = buf[0] & 0xffffffff;
3896 int exp = (image >> 7) & 0xff;
3898 memset (r, 0, sizeof (*r));
3903 r->sign = (image >> 15) & 1;
3904 SET_REAL_EXP (r, exp - 128);
3906 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3907 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3912 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
3913 const REAL_VALUE_TYPE *r)
3915 unsigned long image0, image1, sign = r->sign << 15;
3920 image0 = image1 = 0;
3925 image0 = 0xffff7fff | sign;
3926 image1 = 0xffffffff;
3930 /* Extract the significand into straight hi:lo. */
3931 if (HOST_BITS_PER_LONG == 64)
3933 image0 = r->sig[SIGSZ-1];
3934 image1 = (image0 >> (64 - 56)) & 0xffffffff;
3935 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3939 image0 = r->sig[SIGSZ-1];
3940 image1 = r->sig[SIGSZ-2];
3941 image1 = (image0 << 24) | (image1 >> 8);
3942 image0 = (image0 >> 8) & 0xffffff;
3945 /* Rearrange the half-words of the significand to match the
3947 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3948 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3950 /* Add the sign and exponent. */
3952 image0 |= (REAL_EXP (r) + 128) << 7;
3959 if (FLOAT_WORDS_BIG_ENDIAN)
3960 buf[0] = image1, buf[1] = image0;
3962 buf[0] = image0, buf[1] = image1;
3966 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
3967 REAL_VALUE_TYPE *r, const long *buf)
3969 unsigned long image0, image1;
3972 if (FLOAT_WORDS_BIG_ENDIAN)
3973 image1 = buf[0], image0 = buf[1];
3975 image0 = buf[0], image1 = buf[1];
3976 image0 &= 0xffffffff;
3977 image1 &= 0xffffffff;
3979 exp = (image0 >> 7) & 0xff;
3981 memset (r, 0, sizeof (*r));
3986 r->sign = (image0 >> 15) & 1;
3987 SET_REAL_EXP (r, exp - 128);
3989 /* Rearrange the half-words of the external format into
3990 proper ascending order. */
3991 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
3992 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3994 if (HOST_BITS_PER_LONG == 64)
3996 image0 = (image0 << 31 << 1) | image1;
3999 r->sig[SIGSZ-1] = image0;
4003 r->sig[SIGSZ-1] = image0;
4004 r->sig[SIGSZ-2] = image1;
4005 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4006 r->sig[SIGSZ-1] |= SIG_MSB;
4012 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4013 const REAL_VALUE_TYPE *r)
4015 unsigned long image0, image1, sign = r->sign << 15;
4020 image0 = image1 = 0;
4025 image0 = 0xffff7fff | sign;
4026 image1 = 0xffffffff;
4030 /* Extract the significand into straight hi:lo. */
4031 if (HOST_BITS_PER_LONG == 64)
4033 image0 = r->sig[SIGSZ-1];
4034 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4035 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4039 image0 = r->sig[SIGSZ-1];
4040 image1 = r->sig[SIGSZ-2];
4041 image1 = (image0 << 21) | (image1 >> 11);
4042 image0 = (image0 >> 11) & 0xfffff;
4045 /* Rearrange the half-words of the significand to match the
4047 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4048 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4050 /* Add the sign and exponent. */
4052 image0 |= (REAL_EXP (r) + 1024) << 4;
4059 if (FLOAT_WORDS_BIG_ENDIAN)
4060 buf[0] = image1, buf[1] = image0;
4062 buf[0] = image0, buf[1] = image1;
4066 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4067 REAL_VALUE_TYPE *r, const long *buf)
4069 unsigned long image0, image1;
4072 if (FLOAT_WORDS_BIG_ENDIAN)
4073 image1 = buf[0], image0 = buf[1];
4075 image0 = buf[0], image1 = buf[1];
4076 image0 &= 0xffffffff;
4077 image1 &= 0xffffffff;
4079 exp = (image0 >> 4) & 0x7ff;
4081 memset (r, 0, sizeof (*r));
4086 r->sign = (image0 >> 15) & 1;
4087 SET_REAL_EXP (r, exp - 1024);
4089 /* Rearrange the half-words of the external format into
4090 proper ascending order. */
4091 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4092 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4094 if (HOST_BITS_PER_LONG == 64)
4096 image0 = (image0 << 31 << 1) | image1;
4099 r->sig[SIGSZ-1] = image0;
4103 r->sig[SIGSZ-1] = image0;
4104 r->sig[SIGSZ-2] = image1;
4105 lshift_significand (r, r, 64 - 53);
4106 r->sig[SIGSZ-1] |= SIG_MSB;
4111 const struct real_format vax_f_format =
4130 const struct real_format vax_d_format =
4149 const struct real_format vax_g_format =
4168 /* Encode real R into a single precision DFP value in BUF. */
4170 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4171 long *buf ATTRIBUTE_UNUSED,
4172 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4174 encode_decimal32 (fmt, buf, r);
4177 /* Decode a single precision DFP value in BUF into a real R. */
4179 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4180 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4181 const long *buf ATTRIBUTE_UNUSED)
4183 decode_decimal32 (fmt, r, buf);
4186 /* Encode real R into a double precision DFP value in BUF. */
4188 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4189 long *buf ATTRIBUTE_UNUSED,
4190 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4192 encode_decimal64 (fmt, buf, r);
4195 /* Decode a double precision DFP value in BUF into a real R. */
4197 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4198 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4199 const long *buf ATTRIBUTE_UNUSED)
4201 decode_decimal64 (fmt, r, buf);
4204 /* Encode real R into a quad precision DFP value in BUF. */
4206 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4207 long *buf ATTRIBUTE_UNUSED,
4208 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4210 encode_decimal128 (fmt, buf, r);
4213 /* Decode a quad precision DFP value in BUF into a real R. */
4215 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4216 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4217 const long *buf ATTRIBUTE_UNUSED)
4219 decode_decimal128 (fmt, r, buf);
4222 /* Single precision decimal floating point (IEEE 754R). */
4223 const struct real_format decimal_single_format =
4225 encode_decimal_single,
4226 decode_decimal_single,
4242 /* Double precision decimal floating point (IEEE 754R). */
4243 const struct real_format decimal_double_format =
4245 encode_decimal_double,
4246 decode_decimal_double,
4262 /* Quad precision decimal floating point (IEEE 754R). */
4263 const struct real_format decimal_quad_format =
4265 encode_decimal_quad,
4266 decode_decimal_quad,
4282 /* The "twos-complement" c4x format is officially defined as
4286 This is rather misleading. One must remember that F is signed.
4287 A better description would be
4289 x = -1**s * ((s + 1 + .f) * 2**e
4291 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4292 that's -1 * (1+1+(-.5)) == -1.5. I think.
4294 The constructions here are taken from Tables 5-1 and 5-2 of the
4295 TMS320C4x User's Guide wherein step-by-step instructions for
4296 conversion from IEEE are presented. That's close enough to our
4297 internal representation so as to make things easy.
4299 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
4301 static void encode_c4x_single (const struct real_format *fmt,
4302 long *, const REAL_VALUE_TYPE *);
4303 static void decode_c4x_single (const struct real_format *,
4304 REAL_VALUE_TYPE *, const long *);
4305 static void encode_c4x_extended (const struct real_format *fmt,
4306 long *, const REAL_VALUE_TYPE *);
4307 static void decode_c4x_extended (const struct real_format *,
4308 REAL_VALUE_TYPE *, const long *);
4311 encode_c4x_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4312 long *buf, const REAL_VALUE_TYPE *r)
4314 unsigned long image, exp, sig;
4326 sig = 0x800000 - r->sign;
4330 exp = REAL_EXP (r) - 1;
4331 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4346 image = ((exp & 0xff) << 24) | (sig & 0xffffff);
4351 decode_c4x_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4352 REAL_VALUE_TYPE *r, const long *buf)
4354 unsigned long image = buf[0];
4358 exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
4359 sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
4361 memset (r, 0, sizeof (*r));
4367 sig = sf & 0x7fffff;
4376 sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4378 SET_REAL_EXP (r, exp + 1);
4379 r->sig[SIGSZ-1] = sig;
4384 encode_c4x_extended (const struct real_format *fmt ATTRIBUTE_UNUSED,
4385 long *buf, const REAL_VALUE_TYPE *r)
4387 unsigned long exp, sig;
4399 sig = 0x80000000 - r->sign;
4403 exp = REAL_EXP (r) - 1;
4405 sig = r->sig[SIGSZ-1];
4406 if (HOST_BITS_PER_LONG == 64)
4407 sig = sig >> 1 >> 31;
4424 exp = (exp & 0xff) << 24;
4427 if (FLOAT_WORDS_BIG_ENDIAN)
4428 buf[0] = exp, buf[1] = sig;
4430 buf[0] = sig, buf[0] = exp;
4434 decode_c4x_extended (const struct real_format *fmt ATTRIBUTE_UNUSED,
4435 REAL_VALUE_TYPE *r, const long *buf)
4440 if (FLOAT_WORDS_BIG_ENDIAN)
4441 exp = buf[0], sf = buf[1];
4443 sf = buf[0], exp = buf[1];
4445 exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
4446 sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
4448 memset (r, 0, sizeof (*r));
4454 sig = sf & 0x7fffffff;
4463 if (HOST_BITS_PER_LONG == 64)
4464 sig = sig << 1 << 31;
4467 SET_REAL_EXP (r, exp + 1);
4468 r->sig[SIGSZ-1] = sig;
4472 const struct real_format c4x_single_format =
4491 const struct real_format c4x_extended_format =
4493 encode_c4x_extended,
4494 decode_c4x_extended,
4511 /* A synthetic "format" for internal arithmetic. It's the size of the
4512 internal significand minus the two bits needed for proper rounding.
4513 The encode and decode routines exist only to satisfy our paranoia
4516 static void encode_internal (const struct real_format *fmt,
4517 long *, const REAL_VALUE_TYPE *);
4518 static void decode_internal (const struct real_format *,
4519 REAL_VALUE_TYPE *, const long *);
4522 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4523 const REAL_VALUE_TYPE *r)
4525 memcpy (buf, r, sizeof (*r));
4529 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4530 REAL_VALUE_TYPE *r, const long *buf)
4532 memcpy (r, buf, sizeof (*r));
4535 const struct real_format real_internal_format =
4540 SIGNIFICAND_BITS - 2,
4541 SIGNIFICAND_BITS - 2,
4554 /* Calculate the square root of X in mode MODE, and store the result
4555 in R. Return TRUE if the operation does not raise an exception.
4556 For details see "High Precision Division and Square Root",
4557 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4558 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4561 real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode,
4562 const REAL_VALUE_TYPE *x)
4564 static REAL_VALUE_TYPE halfthree;
4565 static bool init = false;
4566 REAL_VALUE_TYPE h, t, i;
4569 /* sqrt(-0.0) is -0.0. */
4570 if (real_isnegzero (x))
4576 /* Negative arguments return NaN. */
4579 get_canonical_qnan (r, 0);
4583 /* Infinity and NaN return themselves. */
4584 if (!real_isfinite (x))
4592 do_add (&halfthree, &dconst1, &dconsthalf, 0);
4596 /* Initial guess for reciprocal sqrt, i. */
4597 exp = real_exponent (x);
4598 real_ldexp (&i, &dconst1, -exp/2);
4600 /* Newton's iteration for reciprocal sqrt, i. */
4601 for (iter = 0; iter < 16; iter++)
4603 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4604 do_multiply (&t, x, &i);
4605 do_multiply (&h, &t, &i);
4606 do_multiply (&t, &h, &dconsthalf);
4607 do_add (&h, &halfthree, &t, 1);
4608 do_multiply (&t, &i, &h);
4610 /* Check for early convergence. */
4611 if (iter >= 6 && real_identical (&i, &t))
4614 /* ??? Unroll loop to avoid copying. */
4618 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4619 do_multiply (&t, x, &i);
4620 do_multiply (&h, &t, &i);
4621 do_add (&i, &dconst1, &h, 1);
4622 do_multiply (&h, &t, &i);
4623 do_multiply (&i, &dconsthalf, &h);
4624 do_add (&h, &t, &i, 0);
4626 /* ??? We need a Tuckerman test to get the last bit. */
4628 real_convert (r, mode, &h);
4632 /* Calculate X raised to the integer exponent N in mode MODE and store
4633 the result in R. Return true if the result may be inexact due to
4634 loss of precision. The algorithm is the classic "left-to-right binary
4635 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4636 Algorithms", "The Art of Computer Programming", Volume 2. */
4639 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4640 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4642 unsigned HOST_WIDE_INT bit;
4644 bool inexact = false;
4656 /* Don't worry about overflow, from now on n is unsigned. */
4664 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4665 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4669 inexact |= do_multiply (&t, &t, &t);
4671 inexact |= do_multiply (&t, &t, x);
4679 inexact |= do_divide (&t, &dconst1, &t);
4681 real_convert (r, mode, &t);
4685 /* Round X to the nearest integer not larger in absolute value, i.e.
4686 towards zero, placing the result in R in mode MODE. */
4689 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4690 const REAL_VALUE_TYPE *x)
4692 do_fix_trunc (r, x);
4693 if (mode != VOIDmode)
4694 real_convert (r, mode, r);
4697 /* Round X to the largest integer not greater in value, i.e. round
4698 down, placing the result in R in mode MODE. */
4701 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4702 const REAL_VALUE_TYPE *x)
4706 do_fix_trunc (&t, x);
4707 if (! real_identical (&t, x) && x->sign)
4708 do_add (&t, &t, &dconstm1, 0);
4709 if (mode != VOIDmode)
4710 real_convert (r, mode, &t);
4715 /* Round X to the smallest integer not less then argument, i.e. round
4716 up, placing the result in R in mode MODE. */
4719 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4720 const REAL_VALUE_TYPE *x)
4724 do_fix_trunc (&t, x);
4725 if (! real_identical (&t, x) && ! x->sign)
4726 do_add (&t, &t, &dconst1, 0);
4727 if (mode != VOIDmode)
4728 real_convert (r, mode, &t);
4733 /* Round X to the nearest integer, but round halfway cases away from
4737 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4738 const REAL_VALUE_TYPE *x)
4740 do_add (r, x, &dconsthalf, x->sign);
4741 do_fix_trunc (r, r);
4742 if (mode != VOIDmode)
4743 real_convert (r, mode, r);
4746 /* Set the sign of R to the sign of X. */
4749 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4754 /* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible
4755 for initializing and clearing the MPFR parameter. */
4758 mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode)
4760 /* We use a string as an intermediate type. */
4764 /* Take care of Infinity and NaN. */
4765 if (r->cl == rvc_inf)
4767 mpfr_set_inf (m, r->sign == 1 ? -1 : 1);
4771 if (r->cl == rvc_nan)
4777 real_to_hexadecimal (buf, r, sizeof (buf), 0, 1);
4778 /* mpfr_set_str() parses hexadecimal floats from strings in the same
4779 format that GCC will output them. Nothing extra is needed. */
4780 ret = mpfr_set_str (m, buf, 16, rndmode);
4781 gcc_assert (ret == 0);
4784 /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
4785 mode RNDMODE. TYPE is only relevant if M is a NaN. */
4788 real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode)
4790 /* We use a string as an intermediate type. */
4791 char buf[128], *rstr;
4794 /* Take care of Infinity and NaN. */
4798 if (mpfr_sgn (m) < 0)
4799 *r = REAL_VALUE_NEGATE (*r);
4805 real_nan (r, "", 1, TYPE_MODE (type));
4809 rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode);
4811 /* The additional 12 chars add space for the sprintf below. This
4812 leaves 6 digits for the exponent which is supposedly enough. */
4813 gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12);
4815 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
4816 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
4821 sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp);
4823 sprintf (buf, "0x.%sp%d", rstr, (int) exp);
4825 mpfr_free_str (rstr);
4827 real_from_string (r, buf);
4830 /* Check whether the real constant value given is an integer. */
4833 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode)
4835 REAL_VALUE_TYPE cint;
4837 real_trunc (&cint, mode, c);
4838 return real_identical (c, &cint);
4841 /* Write into BUF the maximum representable finite floating-point
4842 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4843 float string. LEN is the size of BUF, and the buffer must be large
4844 enough to contain the resulting string. */
4847 get_max_float (const struct real_format *fmt, char *buf, size_t len)
4852 strcpy (buf, "0x0.");
4854 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
4857 *p++ = "08ce"[n - i];
4858 sprintf (p, "p%d", fmt->emax);
4859 if (fmt->pnan < fmt->p)
4861 /* This is an IBM extended double format made up of two IEEE
4862 doubles. The value of the long double is the sum of the
4863 values of the two parts. The most significant part is
4864 required to be the value of the long double rounded to the
4865 nearest double. Rounding means we need a slightly smaller
4866 value for LDBL_MAX. */
4867 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
4870 gcc_assert (strlen (buf) < len);