1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002,
3 2003, 2004, 2005, 2007, 2008, 2009 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 26.
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
*);
113 static void decimal_from_integer (REAL_VALUE_TYPE
*);
114 static void decimal_integer_string (char *, const REAL_VALUE_TYPE
*,
117 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
118 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
119 static const REAL_VALUE_TYPE
* real_digit (int);
120 static void times_pten (REAL_VALUE_TYPE
*, int);
122 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
124 /* Initialize R with a positive zero. */
127 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
129 memset (r
, 0, sizeof (*r
));
133 /* Initialize R with the canonical quiet NaN. */
136 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
138 memset (r
, 0, sizeof (*r
));
145 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
147 memset (r
, 0, sizeof (*r
));
155 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
157 memset (r
, 0, sizeof (*r
));
163 /* Right-shift the significand of A by N bits; put the result in the
164 significand of R. If any one bits are shifted out, return true. */
167 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
170 unsigned long sticky
= 0;
171 unsigned int i
, ofs
= 0;
173 if (n
>= HOST_BITS_PER_LONG
)
175 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
177 n
&= HOST_BITS_PER_LONG
- 1;
182 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
183 for (i
= 0; i
< SIGSZ
; ++i
)
186 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
187 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
188 << (HOST_BITS_PER_LONG
- n
)));
193 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
194 r
->sig
[i
] = a
->sig
[ofs
+ i
];
195 for (; i
< SIGSZ
; ++i
)
202 /* Right-shift the significand of A by N bits; put the result in the
206 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
209 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
211 n
&= HOST_BITS_PER_LONG
- 1;
214 for (i
= 0; i
< SIGSZ
; ++i
)
217 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
218 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
219 << (HOST_BITS_PER_LONG
- n
)));
224 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
225 r
->sig
[i
] = a
->sig
[ofs
+ i
];
226 for (; i
< SIGSZ
; ++i
)
231 /* Left-shift the significand of A by N bits; put the result in the
235 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
238 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
240 n
&= HOST_BITS_PER_LONG
- 1;
243 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
244 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
245 for (; i
< SIGSZ
; ++i
)
246 r
->sig
[SIGSZ
-1-i
] = 0;
249 for (i
= 0; i
< SIGSZ
; ++i
)
252 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
253 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
254 >> (HOST_BITS_PER_LONG
- n
)));
258 /* Likewise, but N is specialized to 1. */
261 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
265 for (i
= SIGSZ
- 1; i
> 0; --i
)
266 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
267 r
->sig
[0] = a
->sig
[0] << 1;
270 /* Add the significands of A and B, placing the result in R. Return
271 true if there was carry out of the most significant word. */
274 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
275 const REAL_VALUE_TYPE
*b
)
280 for (i
= 0; i
< SIGSZ
; ++i
)
282 unsigned long ai
= a
->sig
[i
];
283 unsigned long ri
= ai
+ b
->sig
[i
];
299 /* Subtract the significands of A and B, placing the result in R. CARRY is
300 true if there's a borrow incoming to the least significant word.
301 Return true if there was borrow out of the most significant word. */
304 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
305 const REAL_VALUE_TYPE
*b
, int carry
)
309 for (i
= 0; i
< SIGSZ
; ++i
)
311 unsigned long ai
= a
->sig
[i
];
312 unsigned long ri
= ai
- b
->sig
[i
];
328 /* Negate the significand A, placing the result in R. */
331 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
336 for (i
= 0; i
< SIGSZ
; ++i
)
338 unsigned long ri
, ai
= a
->sig
[i
];
357 /* Compare significands. Return tri-state vs zero. */
360 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
364 for (i
= SIGSZ
- 1; i
>= 0; --i
)
366 unsigned long ai
= a
->sig
[i
];
367 unsigned long bi
= b
->sig
[i
];
378 /* Return true if A is nonzero. */
381 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
385 for (i
= SIGSZ
- 1; i
>= 0; --i
)
392 /* Set bit N of the significand of R. */
395 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
397 r
->sig
[n
/ HOST_BITS_PER_LONG
]
398 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
401 /* Clear bit N of the significand of R. */
404 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
406 r
->sig
[n
/ HOST_BITS_PER_LONG
]
407 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
410 /* Test bit N of the significand of R. */
413 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
415 /* ??? Compiler bug here if we return this expression directly.
416 The conversion to bool strips the "&1" and we wind up testing
417 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
418 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
422 /* Clear bits 0..N-1 of the significand of R. */
425 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
427 int i
, w
= n
/ HOST_BITS_PER_LONG
;
429 for (i
= 0; i
< w
; ++i
)
432 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
435 /* Divide the significands of A and B, placing the result in R. Return
436 true if the division was inexact. */
439 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
440 const REAL_VALUE_TYPE
*b
)
443 int i
, bit
= SIGNIFICAND_BITS
- 1;
444 unsigned long msb
, inexact
;
447 memset (r
->sig
, 0, sizeof (r
->sig
));
453 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
454 lshift_significand_1 (&u
, &u
);
456 if (msb
|| cmp_significands (&u
, b
) >= 0)
458 sub_significands (&u
, &u
, b
, 0);
459 set_significand_bit (r
, bit
);
464 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
470 /* Adjust the exponent and significand of R such that the most
471 significant bit is set. We underflow to zero and overflow to
472 infinity here, without denormals. (The intermediate representation
473 exponent is large enough to handle target denormals normalized.) */
476 normalize (REAL_VALUE_TYPE
*r
)
484 /* Find the first word that is nonzero. */
485 for (i
= SIGSZ
- 1; i
>= 0; i
--)
487 shift
+= HOST_BITS_PER_LONG
;
491 /* Zero significand flushes to zero. */
499 /* Find the first bit that is nonzero. */
501 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
507 exp
= REAL_EXP (r
) - shift
;
509 get_inf (r
, r
->sign
);
510 else if (exp
< -MAX_EXP
)
511 get_zero (r
, r
->sign
);
514 SET_REAL_EXP (r
, exp
);
515 lshift_significand (r
, r
, shift
);
520 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
521 result may be inexact due to a loss of precision. */
524 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
525 const REAL_VALUE_TYPE
*b
, int subtract_p
)
529 bool inexact
= false;
531 /* Determine if we need to add or subtract. */
533 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
535 switch (CLASS2 (a
->cl
, b
->cl
))
537 case CLASS2 (rvc_zero
, rvc_zero
):
538 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
539 get_zero (r
, sign
& !subtract_p
);
542 case CLASS2 (rvc_zero
, rvc_normal
):
543 case CLASS2 (rvc_zero
, rvc_inf
):
544 case CLASS2 (rvc_zero
, rvc_nan
):
546 case CLASS2 (rvc_normal
, rvc_nan
):
547 case CLASS2 (rvc_inf
, rvc_nan
):
548 case CLASS2 (rvc_nan
, rvc_nan
):
549 /* ANY + NaN = NaN. */
550 case CLASS2 (rvc_normal
, rvc_inf
):
553 r
->sign
= sign
^ subtract_p
;
556 case CLASS2 (rvc_normal
, rvc_zero
):
557 case CLASS2 (rvc_inf
, rvc_zero
):
558 case CLASS2 (rvc_nan
, rvc_zero
):
560 case CLASS2 (rvc_nan
, rvc_normal
):
561 case CLASS2 (rvc_nan
, rvc_inf
):
562 /* NaN + ANY = NaN. */
563 case CLASS2 (rvc_inf
, rvc_normal
):
568 case CLASS2 (rvc_inf
, rvc_inf
):
570 /* Inf - Inf = NaN. */
571 get_canonical_qnan (r
, 0);
573 /* Inf + Inf = Inf. */
577 case CLASS2 (rvc_normal
, rvc_normal
):
584 /* Swap the arguments such that A has the larger exponent. */
585 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
588 const REAL_VALUE_TYPE
*t
;
595 /* If the exponents are not identical, we need to shift the
596 significand of B down. */
599 /* If the exponents are too far apart, the significands
600 do not overlap, which makes the subtraction a noop. */
601 if (dexp
>= SIGNIFICAND_BITS
)
608 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
614 if (sub_significands (r
, a
, b
, inexact
))
616 /* We got a borrow out of the subtraction. That means that
617 A and B had the same exponent, and B had the larger
618 significand. We need to swap the sign and negate the
621 neg_significand (r
, r
);
626 if (add_significands (r
, a
, b
))
628 /* We got carry out of the addition. This means we need to
629 shift the significand back down one bit and increase the
631 inexact
|= sticky_rshift_significand (r
, r
, 1);
632 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
643 SET_REAL_EXP (r
, exp
);
644 /* Zero out the remaining fields. */
649 /* Re-normalize the result. */
652 /* Special case: if the subtraction results in zero, the result
654 if (r
->cl
== rvc_zero
)
657 r
->sig
[0] |= inexact
;
662 /* Calculate R = A * B. Return true if the result may be inexact. */
665 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
666 const REAL_VALUE_TYPE
*b
)
668 REAL_VALUE_TYPE u
, t
, *rr
;
669 unsigned int i
, j
, k
;
670 int sign
= a
->sign
^ b
->sign
;
671 bool inexact
= false;
673 switch (CLASS2 (a
->cl
, b
->cl
))
675 case CLASS2 (rvc_zero
, rvc_zero
):
676 case CLASS2 (rvc_zero
, rvc_normal
):
677 case CLASS2 (rvc_normal
, rvc_zero
):
678 /* +-0 * ANY = 0 with appropriate sign. */
682 case CLASS2 (rvc_zero
, rvc_nan
):
683 case CLASS2 (rvc_normal
, rvc_nan
):
684 case CLASS2 (rvc_inf
, rvc_nan
):
685 case CLASS2 (rvc_nan
, rvc_nan
):
686 /* ANY * NaN = NaN. */
691 case CLASS2 (rvc_nan
, rvc_zero
):
692 case CLASS2 (rvc_nan
, rvc_normal
):
693 case CLASS2 (rvc_nan
, rvc_inf
):
694 /* NaN * ANY = NaN. */
699 case CLASS2 (rvc_zero
, rvc_inf
):
700 case CLASS2 (rvc_inf
, rvc_zero
):
702 get_canonical_qnan (r
, sign
);
705 case CLASS2 (rvc_inf
, rvc_inf
):
706 case CLASS2 (rvc_normal
, rvc_inf
):
707 case CLASS2 (rvc_inf
, rvc_normal
):
708 /* Inf * Inf = Inf, R * Inf = Inf */
712 case CLASS2 (rvc_normal
, rvc_normal
):
719 if (r
== a
|| r
== b
)
725 /* Collect all the partial products. Since we don't have sure access
726 to a widening multiply, we split each long into two half-words.
728 Consider the long-hand form of a four half-word multiplication:
738 We construct partial products of the widened half-word products
739 that are known to not overlap, e.g. DF+DH. Each such partial
740 product is given its proper exponent, which allows us to sum them
741 and obtain the finished product. */
743 for (i
= 0; i
< SIGSZ
* 2; ++i
)
745 unsigned long ai
= a
->sig
[i
/ 2];
747 ai
>>= HOST_BITS_PER_LONG
/ 2;
749 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
754 for (j
= 0; j
< 2; ++j
)
756 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
757 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
766 /* Would underflow to zero, which we shouldn't bother adding. */
771 memset (&u
, 0, sizeof (u
));
773 SET_REAL_EXP (&u
, exp
);
775 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
777 unsigned long bi
= b
->sig
[k
/ 2];
779 bi
>>= HOST_BITS_PER_LONG
/ 2;
781 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
783 u
.sig
[k
/ 2] = ai
* bi
;
787 inexact
|= do_add (rr
, rr
, &u
, 0);
798 /* Calculate R = A / B. Return true if the result may be inexact. */
801 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
802 const REAL_VALUE_TYPE
*b
)
804 int exp
, sign
= a
->sign
^ b
->sign
;
805 REAL_VALUE_TYPE t
, *rr
;
808 switch (CLASS2 (a
->cl
, b
->cl
))
810 case CLASS2 (rvc_zero
, rvc_zero
):
812 case CLASS2 (rvc_inf
, rvc_inf
):
813 /* Inf / Inf = NaN. */
814 get_canonical_qnan (r
, sign
);
817 case CLASS2 (rvc_zero
, rvc_normal
):
818 case CLASS2 (rvc_zero
, rvc_inf
):
820 case CLASS2 (rvc_normal
, rvc_inf
):
825 case CLASS2 (rvc_normal
, rvc_zero
):
827 case CLASS2 (rvc_inf
, rvc_zero
):
832 case CLASS2 (rvc_zero
, rvc_nan
):
833 case CLASS2 (rvc_normal
, rvc_nan
):
834 case CLASS2 (rvc_inf
, rvc_nan
):
835 case CLASS2 (rvc_nan
, rvc_nan
):
836 /* ANY / NaN = NaN. */
841 case CLASS2 (rvc_nan
, rvc_zero
):
842 case CLASS2 (rvc_nan
, rvc_normal
):
843 case CLASS2 (rvc_nan
, rvc_inf
):
844 /* NaN / ANY = NaN. */
849 case CLASS2 (rvc_inf
, rvc_normal
):
854 case CLASS2 (rvc_normal
, rvc_normal
):
861 if (r
== a
|| r
== b
)
866 /* Make sure all fields in the result are initialized. */
871 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
882 SET_REAL_EXP (rr
, exp
);
884 inexact
= div_significands (rr
, a
, b
);
886 /* Re-normalize the result. */
888 rr
->sig
[0] |= inexact
;
896 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
897 one of the two operands is a NaN. */
900 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
905 switch (CLASS2 (a
->cl
, b
->cl
))
907 case CLASS2 (rvc_zero
, rvc_zero
):
908 /* Sign of zero doesn't matter for compares. */
911 case CLASS2 (rvc_normal
, rvc_zero
):
912 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
914 return decimal_do_compare (a
, b
, nan_result
);
916 case CLASS2 (rvc_inf
, rvc_zero
):
917 case CLASS2 (rvc_inf
, rvc_normal
):
918 return (a
->sign
? -1 : 1);
920 case CLASS2 (rvc_inf
, rvc_inf
):
921 return -a
->sign
- -b
->sign
;
923 case CLASS2 (rvc_zero
, rvc_normal
):
924 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
926 return decimal_do_compare (a
, b
, nan_result
);
928 case CLASS2 (rvc_zero
, rvc_inf
):
929 case CLASS2 (rvc_normal
, rvc_inf
):
930 return (b
->sign
? 1 : -1);
932 case CLASS2 (rvc_zero
, rvc_nan
):
933 case CLASS2 (rvc_normal
, rvc_nan
):
934 case CLASS2 (rvc_inf
, rvc_nan
):
935 case CLASS2 (rvc_nan
, rvc_nan
):
936 case CLASS2 (rvc_nan
, rvc_zero
):
937 case CLASS2 (rvc_nan
, rvc_normal
):
938 case CLASS2 (rvc_nan
, rvc_inf
):
941 case CLASS2 (rvc_normal
, rvc_normal
):
948 if (a
->sign
!= b
->sign
)
949 return -a
->sign
- -b
->sign
;
951 if (a
->decimal
|| b
->decimal
)
952 return decimal_do_compare (a
, b
, nan_result
);
954 if (REAL_EXP (a
) > REAL_EXP (b
))
956 else if (REAL_EXP (a
) < REAL_EXP (b
))
959 ret
= cmp_significands (a
, b
);
961 return (a
->sign
? -ret
: ret
);
964 /* Return A truncated to an integral value toward zero. */
967 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
981 decimal_do_fix_trunc (r
, a
);
984 if (REAL_EXP (r
) <= 0)
985 get_zero (r
, r
->sign
);
986 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
987 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
995 /* Perform the binary or unary operation described by CODE.
996 For a unary operation, leave OP1 NULL. This function returns
997 true if the result may be inexact due to loss of precision. */
1000 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
1001 const REAL_VALUE_TYPE
*op1
)
1003 enum tree_code code
= (enum tree_code
) icode
;
1005 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1006 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1011 return do_add (r
, op0
, op1
, 0);
1014 return do_add (r
, op0
, op1
, 1);
1017 return do_multiply (r
, op0
, op1
);
1020 return do_divide (r
, op0
, op1
);
1023 if (op1
->cl
== rvc_nan
)
1025 else if (do_compare (op0
, op1
, -1) < 0)
1032 if (op1
->cl
== rvc_nan
)
1034 else if (do_compare (op0
, op1
, 1) < 0)
1050 case FIX_TRUNC_EXPR
:
1051 do_fix_trunc (r
, op0
);
1060 /* Legacy. Similar, but return the result directly. */
1063 real_arithmetic2 (int icode
, const REAL_VALUE_TYPE
*op0
,
1064 const REAL_VALUE_TYPE
*op1
)
1067 real_arithmetic (&r
, icode
, op0
, op1
);
1072 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1073 const REAL_VALUE_TYPE
*op1
)
1075 enum tree_code code
= (enum tree_code
) icode
;
1080 return do_compare (op0
, op1
, 1) < 0;
1082 return do_compare (op0
, op1
, 1) <= 0;
1084 return do_compare (op0
, op1
, -1) > 0;
1086 return do_compare (op0
, op1
, -1) >= 0;
1088 return do_compare (op0
, op1
, -1) == 0;
1090 return do_compare (op0
, op1
, -1) != 0;
1091 case UNORDERED_EXPR
:
1092 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1094 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1096 return do_compare (op0
, op1
, -1) < 0;
1098 return do_compare (op0
, op1
, -1) <= 0;
1100 return do_compare (op0
, op1
, 1) > 0;
1102 return do_compare (op0
, op1
, 1) >= 0;
1104 return do_compare (op0
, op1
, 0) == 0;
1106 return do_compare (op0
, op1
, 0) != 0;
1113 /* Return floor log2(R). */
1116 real_exponent (const REAL_VALUE_TYPE
*r
)
1124 return (unsigned int)-1 >> 1;
1126 return REAL_EXP (r
);
1132 /* R = OP0 * 2**EXP. */
1135 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1146 exp
+= REAL_EXP (op0
);
1148 get_inf (r
, r
->sign
);
1149 else if (exp
< -MAX_EXP
)
1150 get_zero (r
, r
->sign
);
1152 SET_REAL_EXP (r
, exp
);
1160 /* Determine whether a floating-point value X is infinite. */
1163 real_isinf (const REAL_VALUE_TYPE
*r
)
1165 return (r
->cl
== rvc_inf
);
1168 /* Determine whether a floating-point value X is a NaN. */
1171 real_isnan (const REAL_VALUE_TYPE
*r
)
1173 return (r
->cl
== rvc_nan
);
1176 /* Determine whether a floating-point value X is finite. */
1179 real_isfinite (const REAL_VALUE_TYPE
*r
)
1181 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1184 /* Determine whether a floating-point value X is negative. */
1187 real_isneg (const REAL_VALUE_TYPE
*r
)
1192 /* Determine whether a floating-point value X is minus zero. */
1195 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1197 return r
->sign
&& r
->cl
== rvc_zero
;
1200 /* Compare two floating-point objects for bitwise identity. */
1203 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1209 if (a
->sign
!= b
->sign
)
1219 if (a
->decimal
!= b
->decimal
)
1221 if (REAL_EXP (a
) != REAL_EXP (b
))
1226 if (a
->signalling
!= b
->signalling
)
1228 /* The significand is ignored for canonical NaNs. */
1229 if (a
->canonical
|| b
->canonical
)
1230 return a
->canonical
== b
->canonical
;
1237 for (i
= 0; i
< SIGSZ
; ++i
)
1238 if (a
->sig
[i
] != b
->sig
[i
])
1244 /* Try to change R into its exact multiplicative inverse in machine
1245 mode MODE. Return true if successful. */
1248 exact_real_inverse (enum machine_mode mode
, REAL_VALUE_TYPE
*r
)
1250 const REAL_VALUE_TYPE
*one
= real_digit (1);
1254 if (r
->cl
!= rvc_normal
)
1257 /* Check for a power of two: all significand bits zero except the MSB. */
1258 for (i
= 0; i
< SIGSZ
-1; ++i
)
1261 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1264 /* Find the inverse and truncate to the required mode. */
1265 do_divide (&u
, one
, r
);
1266 real_convert (&u
, mode
, &u
);
1268 /* The rounding may have overflowed. */
1269 if (u
.cl
!= rvc_normal
)
1271 for (i
= 0; i
< SIGSZ
-1; ++i
)
1274 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1281 /* Return true if arithmetic on values in IMODE that were promoted
1282 from values in TMODE is equivalent to direct arithmetic on values
1286 real_can_shorten_arithmetic (enum machine_mode imode
, enum machine_mode tmode
)
1288 const struct real_format
*tfmt
, *ifmt
;
1289 tfmt
= REAL_MODE_FORMAT (tmode
);
1290 ifmt
= REAL_MODE_FORMAT (imode
);
1291 /* These conditions are conservative rather than trying to catch the
1292 exact boundary conditions; the main case to allow is IEEE float
1294 return (ifmt
->b
== tfmt
->b
1295 && ifmt
->p
> 2 * tfmt
->p
1296 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1297 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1298 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1299 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1300 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1301 && (ifmt
->has_sign_dependent_rounding
1302 == tfmt
->has_sign_dependent_rounding
)
1303 && ifmt
->has_nans
>= tfmt
->has_nans
1304 && ifmt
->has_inf
>= tfmt
->has_inf
1305 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1306 && !MODE_COMPOSITE_P (tmode
)
1307 && !MODE_COMPOSITE_P (imode
));
1310 /* Render R as an integer. */
1313 real_to_integer (const REAL_VALUE_TYPE
*r
)
1315 unsigned HOST_WIDE_INT i
;
1326 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1333 return decimal_real_to_integer (r
);
1335 if (REAL_EXP (r
) <= 0)
1337 /* Only force overflow for unsigned overflow. Signed overflow is
1338 undefined, so it doesn't matter what we return, and some callers
1339 expect to be able to use this routine for both signed and
1340 unsigned conversions. */
1341 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1344 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1345 i
= r
->sig
[SIGSZ
-1];
1348 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1349 i
= r
->sig
[SIGSZ
-1];
1350 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1351 i
|= r
->sig
[SIGSZ
-2];
1354 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1365 /* Likewise, but to an integer pair, HI+LOW. */
1368 real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
1369 const REAL_VALUE_TYPE
*r
)
1372 HOST_WIDE_INT low
, high
;
1385 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1398 decimal_real_to_integer2 (plow
, phigh
, r
);
1405 /* Only force overflow for unsigned overflow. Signed overflow is
1406 undefined, so it doesn't matter what we return, and some callers
1407 expect to be able to use this routine for both signed and
1408 unsigned conversions. */
1409 if (exp
> 2*HOST_BITS_PER_WIDE_INT
)
1412 rshift_significand (&t
, r
, 2*HOST_BITS_PER_WIDE_INT
- exp
);
1413 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1415 high
= t
.sig
[SIGSZ
-1];
1416 low
= t
.sig
[SIGSZ
-2];
1420 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
);
1421 high
= t
.sig
[SIGSZ
-1];
1422 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1423 high
|= t
.sig
[SIGSZ
-2];
1425 low
= t
.sig
[SIGSZ
-3];
1426 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1427 low
|= t
.sig
[SIGSZ
-4];
1435 low
= -low
, high
= ~high
;
1447 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1448 of NUM / DEN. Return the quotient and place the remainder in NUM.
1449 It is expected that NUM / DEN are close enough that the quotient is
1452 static unsigned long
1453 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1455 unsigned long q
, msb
;
1456 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1465 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1467 lshift_significand_1 (num
, num
);
1469 if (msb
|| cmp_significands (num
, den
) >= 0)
1471 sub_significands (num
, num
, den
, 0);
1475 while (--expn
>= expd
);
1477 SET_REAL_EXP (num
, expd
);
1483 /* Render R as a decimal floating point constant. Emit DIGITS significant
1484 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1485 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1486 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1487 to a string that, when parsed back in mode MODE, yields the same value. */
1489 #define M_LOG10_2 0.30102999566398119521
1492 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1493 size_t buf_size
, size_t digits
,
1494 int crop_trailing_zeros
, enum machine_mode mode
)
1496 const struct real_format
*fmt
= NULL
;
1497 const REAL_VALUE_TYPE
*one
, *ten
;
1498 REAL_VALUE_TYPE r
, pten
, u
, v
;
1499 int dec_exp
, cmp_one
, digit
;
1501 char *p
, *first
, *last
;
1505 if (mode
!= VOIDmode
)
1507 fmt
= REAL_MODE_FORMAT (mode
);
1515 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1520 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1523 /* ??? Print the significand as well, if not canonical? */
1524 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1525 (r_orig
->signalling
? 'S' : 'Q'));
1533 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1537 /* Bound the number of digits printed by the size of the representation. */
1538 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1539 if (digits
== 0 || digits
> max_digits
)
1540 digits
= max_digits
;
1542 /* Estimate the decimal exponent, and compute the length of the string it
1543 will print as. Be conservative and add one to account for possible
1544 overflow or rounding error. */
1545 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1546 for (max_digits
= 1; dec_exp
; max_digits
++)
1549 /* Bound the number of digits printed by the size of the output buffer. */
1550 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1551 gcc_assert (max_digits
<= buf_size
);
1552 if (digits
> max_digits
)
1553 digits
= max_digits
;
1555 one
= real_digit (1);
1556 ten
= ten_to_ptwo (0);
1564 cmp_one
= do_compare (&r
, one
, 0);
1569 /* Number is greater than one. Convert significand to an integer
1570 and strip trailing decimal zeros. */
1573 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1575 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1576 m
= floor_log2 (max_digits
);
1578 /* Iterate over the bits of the possible powers of 10 that might
1579 be present in U and eliminate them. That is, if we find that
1580 10**2**M divides U evenly, keep the division and increase
1586 do_divide (&t
, &u
, ten_to_ptwo (m
));
1587 do_fix_trunc (&v
, &t
);
1588 if (cmp_significands (&v
, &t
) == 0)
1596 /* Revert the scaling to integer that we performed earlier. */
1597 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1598 - (SIGNIFICAND_BITS
- 1));
1601 /* Find power of 10. Do this by dividing out 10**2**M when
1602 this is larger than the current remainder. Fill PTEN with
1603 the power of 10 that we compute. */
1604 if (REAL_EXP (&r
) > 0)
1606 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1609 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1610 if (do_compare (&u
, ptentwo
, 0) >= 0)
1612 do_divide (&u
, &u
, ptentwo
);
1613 do_multiply (&pten
, &pten
, ptentwo
);
1620 /* We managed to divide off enough tens in the above reduction
1621 loop that we've now got a negative exponent. Fall into the
1622 less-than-one code to compute the proper value for PTEN. */
1629 /* Number is less than one. Pad significand with leading
1635 /* Stop if we'd shift bits off the bottom. */
1639 do_multiply (&u
, &v
, ten
);
1641 /* Stop if we're now >= 1. */
1642 if (REAL_EXP (&u
) > 0)
1650 /* Find power of 10. Do this by multiplying in P=10**2**M when
1651 the current remainder is smaller than 1/P. Fill PTEN with the
1652 power of 10 that we compute. */
1653 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1656 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1657 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1659 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1661 do_multiply (&v
, &v
, ptentwo
);
1662 do_multiply (&pten
, &pten
, ptentwo
);
1668 /* Invert the positive power of 10 that we've collected so far. */
1669 do_divide (&pten
, one
, &pten
);
1677 /* At this point, PTEN should contain the nearest power of 10 smaller
1678 than R, such that this division produces the first digit.
1680 Using a divide-step primitive that returns the complete integral
1681 remainder avoids the rounding error that would be produced if
1682 we were to use do_divide here and then simply multiply by 10 for
1683 each subsequent digit. */
1685 digit
= rtd_divmod (&r
, &pten
);
1687 /* Be prepared for error in that division via underflow ... */
1688 if (digit
== 0 && cmp_significand_0 (&r
))
1690 /* Multiply by 10 and try again. */
1691 do_multiply (&r
, &r
, ten
);
1692 digit
= rtd_divmod (&r
, &pten
);
1694 gcc_assert (digit
!= 0);
1697 /* ... or overflow. */
1707 gcc_assert (digit
<= 10);
1711 /* Generate subsequent digits. */
1712 while (--digits
> 0)
1714 do_multiply (&r
, &r
, ten
);
1715 digit
= rtd_divmod (&r
, &pten
);
1720 /* Generate one more digit with which to do rounding. */
1721 do_multiply (&r
, &r
, ten
);
1722 digit
= rtd_divmod (&r
, &pten
);
1724 /* Round the result. */
1725 if (fmt
&& fmt
->round_towards_zero
)
1727 /* If the format uses round towards zero when parsing the string
1728 back in, we need to always round away from zero here. */
1729 if (cmp_significand_0 (&r
))
1731 round_up
= digit
> 0;
1737 /* Round to nearest. If R is nonzero there are additional
1738 nonzero digits to be extracted. */
1739 if (cmp_significand_0 (&r
))
1741 /* Round to even. */
1742 else if ((p
[-1] - '0') & 1)
1746 round_up
= digit
> 5;
1763 /* Carry out of the first digit. This means we had all 9's and
1764 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1772 /* Insert the decimal point. */
1773 first
[0] = first
[1];
1776 /* If requested, drop trailing zeros. Never crop past "1.0". */
1777 if (crop_trailing_zeros
)
1778 while (last
> first
+ 3 && last
[-1] == '0')
1781 /* Append the exponent. */
1782 sprintf (last
, "e%+d", dec_exp
);
1784 #ifdef ENABLE_CHECKING
1785 /* Verify that we can read the original value back in. */
1786 if (mode
!= VOIDmode
)
1788 real_from_string (&r
, str
);
1789 real_convert (&r
, mode
, &r
);
1790 gcc_assert (real_identical (&r
, r_orig
));
1795 /* Likewise, except always uses round-to-nearest. */
1798 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1799 size_t digits
, int crop_trailing_zeros
)
1801 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1802 digits
, crop_trailing_zeros
, VOIDmode
);
1805 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1806 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1807 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1808 strip trailing zeros. */
1811 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1812 size_t digits
, int crop_trailing_zeros
)
1814 int i
, j
, exp
= REAL_EXP (r
);
1827 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1830 /* ??? Print the significand as well, if not canonical? */
1831 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1832 (r
->signalling
? 'S' : 'Q'));
1840 /* Hexadecimal format for decimal floats is not interesting. */
1841 strcpy (str
, "N/A");
1846 digits
= SIGNIFICAND_BITS
/ 4;
1848 /* Bound the number of digits printed by the size of the output buffer. */
1850 sprintf (exp_buf
, "p%+d", exp
);
1851 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1852 gcc_assert (max_digits
<= buf_size
);
1853 if (digits
> max_digits
)
1854 digits
= max_digits
;
1865 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1866 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1868 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1874 if (crop_trailing_zeros
)
1875 while (p
> first
+ 1 && p
[-1] == '0')
1878 sprintf (p
, "p%+d", exp
);
1881 /* Initialize R from a decimal or hexadecimal string. The string is
1882 assumed to have been syntax checked already. Return -1 if the
1883 value underflows, +1 if overflows, and 0 otherwise. */
1886 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1898 else if (*str
== '+')
1901 if (!strncmp (str
, "QNaN", 4))
1903 get_canonical_qnan (r
, sign
);
1906 else if (!strncmp (str
, "SNaN", 4))
1908 get_canonical_snan (r
, sign
);
1911 else if (!strncmp (str
, "Inf", 3))
1917 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1919 /* Hexadecimal floating point. */
1920 int pos
= SIGNIFICAND_BITS
- 4, d
;
1928 d
= hex_value (*str
);
1933 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1934 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1938 /* Ensure correct rounding by setting last bit if there is
1939 a subsequent nonzero digit. */
1947 if (pos
== SIGNIFICAND_BITS
- 4)
1954 d
= hex_value (*str
);
1959 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1960 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1964 /* Ensure correct rounding by setting last bit if there is
1965 a subsequent nonzero digit. */
1971 /* If the mantissa is zero, ignore the exponent. */
1972 if (!cmp_significand_0 (r
))
1975 if (*str
== 'p' || *str
== 'P')
1977 bool exp_neg
= false;
1985 else if (*str
== '+')
1989 while (ISDIGIT (*str
))
1995 /* Overflowed the exponent. */
2010 SET_REAL_EXP (r
, exp
);
2016 /* Decimal floating point. */
2017 const REAL_VALUE_TYPE
*ten
= ten_to_ptwo (0);
2022 while (ISDIGIT (*str
))
2025 do_multiply (r
, r
, ten
);
2027 do_add (r
, r
, real_digit (d
), 0);
2032 if (r
->cl
== rvc_zero
)
2037 while (ISDIGIT (*str
))
2040 do_multiply (r
, r
, ten
);
2042 do_add (r
, r
, real_digit (d
), 0);
2047 /* If the mantissa is zero, ignore the exponent. */
2048 if (r
->cl
== rvc_zero
)
2051 if (*str
== 'e' || *str
== 'E')
2053 bool exp_neg
= false;
2061 else if (*str
== '+')
2065 while (ISDIGIT (*str
))
2071 /* Overflowed the exponent. */
2085 times_pten (r
, exp
);
2104 /* Legacy. Similar, but return the result directly. */
2107 real_from_string2 (const char *s
, enum machine_mode mode
)
2111 real_from_string (&r
, s
);
2112 if (mode
!= VOIDmode
)
2113 real_convert (&r
, mode
, &r
);
2118 /* Initialize R from string S and desired MODE. */
2121 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, enum machine_mode mode
)
2123 if (DECIMAL_FLOAT_MODE_P (mode
))
2124 decimal_real_from_string (r
, s
);
2126 real_from_string (r
, s
);
2128 if (mode
!= VOIDmode
)
2129 real_convert (r
, mode
, r
);
2132 /* Initialize R from the integer pair HIGH+LOW. */
2135 real_from_integer (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2136 unsigned HOST_WIDE_INT low
, HOST_WIDE_INT high
,
2139 if (low
== 0 && high
== 0)
2143 memset (r
, 0, sizeof (*r
));
2145 r
->sign
= high
< 0 && !unsigned_p
;
2146 SET_REAL_EXP (r
, 2 * HOST_BITS_PER_WIDE_INT
);
2157 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2159 r
->sig
[SIGSZ
-1] = high
;
2160 r
->sig
[SIGSZ
-2] = low
;
2164 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2165 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2166 r
->sig
[SIGSZ
-2] = high
;
2167 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2168 r
->sig
[SIGSZ
-4] = low
;
2174 if (DECIMAL_FLOAT_MODE_P (mode
))
2175 decimal_from_integer (r
);
2176 else if (mode
!= VOIDmode
)
2177 real_convert (r
, mode
, r
);
2180 /* Render R, an integral value, as a floating point constant with no
2181 specified exponent. */
2184 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2187 int dec_exp
, digit
, digits
;
2188 REAL_VALUE_TYPE r
, pten
;
2194 if (r
.cl
== rvc_zero
)
2203 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2204 digits
= dec_exp
+ 1;
2205 gcc_assert ((digits
+ 2) < (int)buf_size
);
2207 pten
= *real_digit (1);
2208 times_pten (&pten
, dec_exp
);
2214 digit
= rtd_divmod (&r
, &pten
);
2215 gcc_assert (digit
>= 0 && digit
<= 9);
2217 while (--digits
> 0)
2220 digit
= rtd_divmod (&r
, &pten
);
2227 /* Convert a real with an integral value to decimal float. */
2230 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2234 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2235 decimal_real_from_string (r
, str
);
2238 /* Returns 10**2**N. */
2240 static const REAL_VALUE_TYPE
*
2243 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2245 gcc_assert (n
>= 0);
2246 gcc_assert (n
< EXP_BITS
);
2248 if (tens
[n
].cl
== rvc_zero
)
2250 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2252 HOST_WIDE_INT t
= 10;
2255 for (i
= 0; i
< n
; ++i
)
2258 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2262 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2263 do_multiply (&tens
[n
], t
, t
);
2270 /* Returns 10**(-2**N). */
2272 static const REAL_VALUE_TYPE
*
2273 ten_to_mptwo (int n
)
2275 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2277 gcc_assert (n
>= 0);
2278 gcc_assert (n
< EXP_BITS
);
2280 if (tens
[n
].cl
== rvc_zero
)
2281 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2288 static const REAL_VALUE_TYPE
*
2291 static REAL_VALUE_TYPE num
[10];
2293 gcc_assert (n
>= 0);
2294 gcc_assert (n
<= 9);
2296 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2297 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2302 /* Multiply R by 10**EXP. */
2305 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2307 REAL_VALUE_TYPE pten
, *rr
;
2308 bool negative
= (exp
< 0);
2314 pten
= *real_digit (1);
2320 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2322 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2325 do_divide (r
, r
, &pten
);
2328 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2330 const REAL_VALUE_TYPE
*
2333 static REAL_VALUE_TYPE value
;
2335 /* Initialize mathematical constants for constant folding builtins.
2336 These constants need to be given to at least 160 bits precision. */
2337 if (value
.cl
== rvc_zero
)
2340 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2341 mpfr_set_ui (m
, 1, GMP_RNDN
);
2342 mpfr_exp (m
, m
, GMP_RNDN
);
2343 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2350 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2352 const REAL_VALUE_TYPE
*
2353 dconst_third_ptr (void)
2355 static REAL_VALUE_TYPE value
;
2357 /* Initialize mathematical constants for constant folding builtins.
2358 These constants need to be given to at least 160 bits precision. */
2359 if (value
.cl
== rvc_zero
)
2361 real_arithmetic (&value
, RDIV_EXPR
, &dconst1
, real_digit (3));
2366 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2368 const REAL_VALUE_TYPE
*
2369 dconst_sqrt2_ptr (void)
2371 static REAL_VALUE_TYPE value
;
2373 /* Initialize mathematical constants for constant folding builtins.
2374 These constants need to be given to at least 160 bits precision. */
2375 if (value
.cl
== rvc_zero
)
2378 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2379 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2380 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2386 /* Fills R with +Inf. */
2389 real_inf (REAL_VALUE_TYPE
*r
)
2394 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2395 we force a QNaN, else we force an SNaN. The string, if not empty,
2396 is parsed as a number and placed in the significand. Return true
2397 if the string was successfully parsed. */
2400 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2401 enum machine_mode mode
)
2403 const struct real_format
*fmt
;
2405 fmt
= REAL_MODE_FORMAT (mode
);
2411 get_canonical_qnan (r
, 0);
2413 get_canonical_snan (r
, 0);
2419 memset (r
, 0, sizeof (*r
));
2422 /* Parse akin to strtol into the significand of R. */
2424 while (ISSPACE (*str
))
2428 else if (*str
== '+')
2433 if (*str
== 'x' || *str
== 'X')
2442 while ((d
= hex_value (*str
)) < base
)
2449 lshift_significand (r
, r
, 3);
2452 lshift_significand (r
, r
, 4);
2455 lshift_significand_1 (&u
, r
);
2456 lshift_significand (r
, r
, 3);
2457 add_significands (r
, r
, &u
);
2465 add_significands (r
, r
, &u
);
2470 /* Must have consumed the entire string for success. */
2474 /* Shift the significand into place such that the bits
2475 are in the most significant bits for the format. */
2476 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2478 /* Our MSB is always unset for NaNs. */
2479 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2481 /* Force quiet or signalling NaN. */
2482 r
->signalling
= !quiet
;
2488 /* Fills R with the largest finite value representable in mode MODE.
2489 If SIGN is nonzero, R is set to the most negative finite value. */
2492 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
2494 const struct real_format
*fmt
;
2497 fmt
= REAL_MODE_FORMAT (mode
);
2499 memset (r
, 0, sizeof (*r
));
2502 decimal_real_maxval (r
, sign
, mode
);
2507 SET_REAL_EXP (r
, fmt
->emax
);
2509 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2510 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2511 clear_significand_below (r
, np2
);
2513 if (fmt
->pnan
< fmt
->p
)
2514 /* This is an IBM extended double format made up of two IEEE
2515 doubles. The value of the long double is the sum of the
2516 values of the two parts. The most significant part is
2517 required to be the value of the long double rounded to the
2518 nearest double. Rounding means we need a slightly smaller
2519 value for LDBL_MAX. */
2520 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2524 /* Fills R with 2**N. */
2527 real_2expN (REAL_VALUE_TYPE
*r
, int n
, enum machine_mode fmode
)
2529 memset (r
, 0, sizeof (*r
));
2534 else if (n
< -MAX_EXP
)
2539 SET_REAL_EXP (r
, n
);
2540 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2542 if (DECIMAL_FLOAT_MODE_P (fmode
))
2543 decimal_real_convert (r
, fmode
, r
);
2548 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2552 bool round_up
= false;
2558 decimal_round_for_format (fmt
, r
);
2561 /* FIXME. We can come here via fp_easy_constant
2562 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2563 investigated whether this convert needs to be here, or
2564 something else is missing. */
2565 decimal_real_convert (r
, DFmode
, r
);
2569 emin2m1
= fmt
->emin
- 1;
2572 np2
= SIGNIFICAND_BITS
- p2
;
2576 get_zero (r
, r
->sign
);
2578 if (!fmt
->has_signed_zero
)
2583 get_inf (r
, r
->sign
);
2588 clear_significand_below (r
, np2
);
2598 /* Check the range of the exponent. If we're out of range,
2599 either underflow or overflow. */
2600 if (REAL_EXP (r
) > emax2
)
2602 else if (REAL_EXP (r
) <= emin2m1
)
2606 if (!fmt
->has_denorm
)
2608 /* Don't underflow completely until we've had a chance to round. */
2609 if (REAL_EXP (r
) < emin2m1
)
2614 diff
= emin2m1
- REAL_EXP (r
) + 1;
2618 /* De-normalize the significand. */
2619 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2620 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2624 if (!fmt
->round_towards_zero
)
2626 /* There are P2 true significand bits, followed by one guard bit,
2627 followed by one sticky bit, followed by stuff. Fold nonzero
2628 stuff into the sticky bit. */
2629 unsigned long sticky
;
2633 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2634 sticky
|= r
->sig
[i
];
2636 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2638 guard
= test_significand_bit (r
, np2
- 1);
2639 lsb
= test_significand_bit (r
, np2
);
2641 /* Round to even. */
2642 round_up
= guard
&& (sticky
|| lsb
);
2649 set_significand_bit (&u
, np2
);
2651 if (add_significands (r
, r
, &u
))
2653 /* Overflow. Means the significand had been all ones, and
2654 is now all zeros. Need to increase the exponent, and
2655 possibly re-normalize it. */
2656 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2657 if (REAL_EXP (r
) > emax2
)
2659 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2663 /* Catch underflow that we deferred until after rounding. */
2664 if (REAL_EXP (r
) <= emin2m1
)
2667 /* Clear out trailing garbage. */
2668 clear_significand_below (r
, np2
);
2671 /* Extend or truncate to a new mode. */
2674 real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2675 const REAL_VALUE_TYPE
*a
)
2677 const struct real_format
*fmt
;
2679 fmt
= REAL_MODE_FORMAT (mode
);
2684 if (a
->decimal
|| fmt
->b
== 10)
2685 decimal_real_convert (r
, mode
, a
);
2687 round_for_format (fmt
, r
);
2689 /* round_for_format de-normalizes denormals. Undo just that part. */
2690 if (r
->cl
== rvc_normal
)
2694 /* Legacy. Likewise, except return the struct directly. */
2697 real_value_truncate (enum machine_mode mode
, REAL_VALUE_TYPE a
)
2700 real_convert (&r
, mode
, &a
);
2704 /* Return true if truncating to MODE is exact. */
2707 exact_real_truncate (enum machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2709 const struct real_format
*fmt
;
2713 fmt
= REAL_MODE_FORMAT (mode
);
2716 /* Don't allow conversion to denormals. */
2717 emin2m1
= fmt
->emin
- 1;
2718 if (REAL_EXP (a
) <= emin2m1
)
2721 /* After conversion to the new mode, the value must be identical. */
2722 real_convert (&t
, mode
, a
);
2723 return real_identical (&t
, a
);
2726 /* Write R to the given target format. Place the words of the result
2727 in target word order in BUF. There are always 32 bits in each
2728 long, no matter the size of the host long.
2730 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2733 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2734 const struct real_format
*fmt
)
2740 round_for_format (fmt
, &r
);
2744 (*fmt
->encode
) (fmt
, buf
, &r
);
2749 /* Similar, but look up the format from MODE. */
2752 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, enum machine_mode mode
)
2754 const struct real_format
*fmt
;
2756 fmt
= REAL_MODE_FORMAT (mode
);
2759 return real_to_target_fmt (buf
, r
, fmt
);
2762 /* Read R from the given target format. Read the words of the result
2763 in target word order in BUF. There are always 32 bits in each
2764 long, no matter the size of the host long. */
2767 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2768 const struct real_format
*fmt
)
2770 (*fmt
->decode
) (fmt
, r
, buf
);
2773 /* Similar, but look up the format from MODE. */
2776 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, enum machine_mode mode
)
2778 const struct real_format
*fmt
;
2780 fmt
= REAL_MODE_FORMAT (mode
);
2783 (*fmt
->decode
) (fmt
, r
, buf
);
2786 /* Return the number of bits of the largest binary value that the
2787 significand of MODE will hold. */
2788 /* ??? Legacy. Should get access to real_format directly. */
2791 significand_size (enum machine_mode mode
)
2793 const struct real_format
*fmt
;
2795 fmt
= REAL_MODE_FORMAT (mode
);
2801 /* Return the size in bits of the largest binary value that can be
2802 held by the decimal coefficient for this mode. This is one more
2803 than the number of bits required to hold the largest coefficient
2805 double log2_10
= 3.3219281;
2806 return fmt
->p
* log2_10
;
2811 /* Return a hash value for the given real value. */
2812 /* ??? The "unsigned int" return value is intended to be hashval_t,
2813 but I didn't want to pull hashtab.h into real.h. */
2816 real_hash (const REAL_VALUE_TYPE
*r
)
2821 h
= r
->cl
| (r
->sign
<< 2);
2829 h
|= REAL_EXP (r
) << 3;
2834 h
^= (unsigned int)-1;
2843 if (sizeof(unsigned long) > sizeof(unsigned int))
2844 for (i
= 0; i
< SIGSZ
; ++i
)
2846 unsigned long s
= r
->sig
[i
];
2847 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2850 for (i
= 0; i
< SIGSZ
; ++i
)
2856 /* IEEE single-precision format. */
2858 static void encode_ieee_single (const struct real_format
*fmt
,
2859 long *, const REAL_VALUE_TYPE
*);
2860 static void decode_ieee_single (const struct real_format
*,
2861 REAL_VALUE_TYPE
*, const long *);
2864 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2865 const REAL_VALUE_TYPE
*r
)
2867 unsigned long image
, sig
, exp
;
2868 unsigned long sign
= r
->sign
;
2869 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2872 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2883 image
|= 0x7fffffff;
2890 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2891 if (r
->signalling
== fmt
->qnan_msb_set
)
2902 image
|= 0x7fffffff;
2906 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2907 whereas the intermediate representation is 0.F x 2**exp.
2908 Which means we're off by one. */
2912 exp
= REAL_EXP (r
) + 127 - 1;
2925 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2928 unsigned long image
= buf
[0] & 0xffffffff;
2929 bool sign
= (image
>> 31) & 1;
2930 int exp
= (image
>> 23) & 0xff;
2932 memset (r
, 0, sizeof (*r
));
2933 image
<<= HOST_BITS_PER_LONG
- 24;
2938 if (image
&& fmt
->has_denorm
)
2942 SET_REAL_EXP (r
, -126);
2943 r
->sig
[SIGSZ
-1] = image
<< 1;
2946 else if (fmt
->has_signed_zero
)
2949 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2955 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2956 ^ fmt
->qnan_msb_set
);
2957 r
->sig
[SIGSZ
-1] = image
;
2969 SET_REAL_EXP (r
, exp
- 127 + 1);
2970 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2974 const struct real_format ieee_single_format
=
2995 const struct real_format mips_single_format
=
3016 const struct real_format motorola_single_format
=
3037 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3038 single precision with the following differences:
3039 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3041 - NaNs are not supported.
3042 - The range of non-zero numbers in binary is
3043 (001)[1.]000...000 to (255)[1.]111...111.
3044 - Denormals can be represented, but are treated as +0.0 when
3045 used as an operand and are never generated as a result.
3046 - -0.0 can be represented, but a zero result is always +0.0.
3047 - the only supported rounding mode is trunction (towards zero). */
3048 const struct real_format spu_single_format
=
3069 /* IEEE double-precision format. */
3071 static void encode_ieee_double (const struct real_format
*fmt
,
3072 long *, const REAL_VALUE_TYPE
*);
3073 static void decode_ieee_double (const struct real_format
*,
3074 REAL_VALUE_TYPE
*, const long *);
3077 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3078 const REAL_VALUE_TYPE
*r
)
3080 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3081 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3083 image_hi
= r
->sign
<< 31;
3086 if (HOST_BITS_PER_LONG
== 64)
3088 sig_hi
= r
->sig
[SIGSZ
-1];
3089 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3090 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3094 sig_hi
= r
->sig
[SIGSZ
-1];
3095 sig_lo
= r
->sig
[SIGSZ
-2];
3096 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3097 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3107 image_hi
|= 2047 << 20;
3110 image_hi
|= 0x7fffffff;
3111 image_lo
= 0xffffffff;
3120 if (fmt
->canonical_nan_lsbs_set
)
3122 sig_hi
= (1 << 19) - 1;
3123 sig_lo
= 0xffffffff;
3131 if (r
->signalling
== fmt
->qnan_msb_set
)
3132 sig_hi
&= ~(1 << 19);
3135 if (sig_hi
== 0 && sig_lo
== 0)
3138 image_hi
|= 2047 << 20;
3144 image_hi
|= 0x7fffffff;
3145 image_lo
= 0xffffffff;
3150 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3151 whereas the intermediate representation is 0.F x 2**exp.
3152 Which means we're off by one. */
3156 exp
= REAL_EXP (r
) + 1023 - 1;
3157 image_hi
|= exp
<< 20;
3166 if (FLOAT_WORDS_BIG_ENDIAN
)
3167 buf
[0] = image_hi
, buf
[1] = image_lo
;
3169 buf
[0] = image_lo
, buf
[1] = image_hi
;
3173 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3176 unsigned long image_hi
, image_lo
;
3180 if (FLOAT_WORDS_BIG_ENDIAN
)
3181 image_hi
= buf
[0], image_lo
= buf
[1];
3183 image_lo
= buf
[0], image_hi
= buf
[1];
3184 image_lo
&= 0xffffffff;
3185 image_hi
&= 0xffffffff;
3187 sign
= (image_hi
>> 31) & 1;
3188 exp
= (image_hi
>> 20) & 0x7ff;
3190 memset (r
, 0, sizeof (*r
));
3192 image_hi
<<= 32 - 21;
3193 image_hi
|= image_lo
>> 21;
3194 image_hi
&= 0x7fffffff;
3195 image_lo
<<= 32 - 21;
3199 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3203 SET_REAL_EXP (r
, -1022);
3204 if (HOST_BITS_PER_LONG
== 32)
3206 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3208 r
->sig
[SIGSZ
-1] = image_hi
;
3209 r
->sig
[SIGSZ
-2] = image_lo
;
3213 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3214 r
->sig
[SIGSZ
-1] = image_hi
;
3218 else if (fmt
->has_signed_zero
)
3221 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3223 if (image_hi
|| image_lo
)
3227 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3228 if (HOST_BITS_PER_LONG
== 32)
3230 r
->sig
[SIGSZ
-1] = image_hi
;
3231 r
->sig
[SIGSZ
-2] = image_lo
;
3234 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3246 SET_REAL_EXP (r
, exp
- 1023 + 1);
3247 if (HOST_BITS_PER_LONG
== 32)
3249 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3250 r
->sig
[SIGSZ
-2] = image_lo
;
3253 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3257 const struct real_format ieee_double_format
=
3278 const struct real_format mips_double_format
=
3299 const struct real_format motorola_double_format
=
3320 /* IEEE extended real format. This comes in three flavors: Intel's as
3321 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3322 12- and 16-byte images may be big- or little endian; Motorola's is
3323 always big endian. */
3325 /* Helper subroutine which converts from the internal format to the
3326 12-byte little-endian Intel format. Functions below adjust this
3327 for the other possible formats. */
3329 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3330 const REAL_VALUE_TYPE
*r
)
3332 unsigned long image_hi
, sig_hi
, sig_lo
;
3333 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3335 image_hi
= r
->sign
<< 15;
3336 sig_hi
= sig_lo
= 0;
3348 /* Intel requires the explicit integer bit to be set, otherwise
3349 it considers the value a "pseudo-infinity". Motorola docs
3350 say it doesn't care. */
3351 sig_hi
= 0x80000000;
3356 sig_lo
= sig_hi
= 0xffffffff;
3366 if (fmt
->canonical_nan_lsbs_set
)
3368 sig_hi
= (1 << 30) - 1;
3369 sig_lo
= 0xffffffff;
3372 else if (HOST_BITS_PER_LONG
== 32)
3374 sig_hi
= r
->sig
[SIGSZ
-1];
3375 sig_lo
= r
->sig
[SIGSZ
-2];
3379 sig_lo
= r
->sig
[SIGSZ
-1];
3380 sig_hi
= sig_lo
>> 31 >> 1;
3381 sig_lo
&= 0xffffffff;
3383 if (r
->signalling
== fmt
->qnan_msb_set
)
3384 sig_hi
&= ~(1 << 30);
3387 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3390 /* Intel requires the explicit integer bit to be set, otherwise
3391 it considers the value a "pseudo-nan". Motorola docs say it
3393 sig_hi
|= 0x80000000;
3398 sig_lo
= sig_hi
= 0xffffffff;
3404 int exp
= REAL_EXP (r
);
3406 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3407 whereas the intermediate representation is 0.F x 2**exp.
3408 Which means we're off by one.
3410 Except for Motorola, which consider exp=0 and explicit
3411 integer bit set to continue to be normalized. In theory
3412 this discrepancy has been taken care of by the difference
3413 in fmt->emin in round_for_format. */
3420 gcc_assert (exp
>= 0);
3424 if (HOST_BITS_PER_LONG
== 32)
3426 sig_hi
= r
->sig
[SIGSZ
-1];
3427 sig_lo
= r
->sig
[SIGSZ
-2];
3431 sig_lo
= r
->sig
[SIGSZ
-1];
3432 sig_hi
= sig_lo
>> 31 >> 1;
3433 sig_lo
&= 0xffffffff;
3442 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3445 /* Convert from the internal format to the 12-byte Motorola format
3446 for an IEEE extended real. */
3448 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3449 const REAL_VALUE_TYPE
*r
)
3452 encode_ieee_extended (fmt
, intermed
, r
);
3454 /* Motorola chips are assumed always to be big-endian. Also, the
3455 padding in a Motorola extended real goes between the exponent and
3456 the mantissa. At this point the mantissa is entirely within
3457 elements 0 and 1 of intermed, and the exponent entirely within
3458 element 2, so all we have to do is swap the order around, and
3459 shift element 2 left 16 bits. */
3460 buf
[0] = intermed
[2] << 16;
3461 buf
[1] = intermed
[1];
3462 buf
[2] = intermed
[0];
3465 /* Convert from the internal format to the 12-byte Intel format for
3466 an IEEE extended real. */
3468 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3469 const REAL_VALUE_TYPE
*r
)
3471 if (FLOAT_WORDS_BIG_ENDIAN
)
3473 /* All the padding in an Intel-format extended real goes at the high
3474 end, which in this case is after the mantissa, not the exponent.
3475 Therefore we must shift everything down 16 bits. */
3477 encode_ieee_extended (fmt
, intermed
, r
);
3478 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3479 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3480 buf
[2] = (intermed
[0] << 16);
3483 /* encode_ieee_extended produces what we want directly. */
3484 encode_ieee_extended (fmt
, buf
, r
);
3487 /* Convert from the internal format to the 16-byte Intel format for
3488 an IEEE extended real. */
3490 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3491 const REAL_VALUE_TYPE
*r
)
3493 /* All the padding in an Intel-format extended real goes at the high end. */
3494 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3498 /* As above, we have a helper function which converts from 12-byte
3499 little-endian Intel format to internal format. Functions below
3500 adjust for the other possible formats. */
3502 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3505 unsigned long image_hi
, sig_hi
, sig_lo
;
3509 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3510 sig_lo
&= 0xffffffff;
3511 sig_hi
&= 0xffffffff;
3512 image_hi
&= 0xffffffff;
3514 sign
= (image_hi
>> 15) & 1;
3515 exp
= image_hi
& 0x7fff;
3517 memset (r
, 0, sizeof (*r
));
3521 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3526 /* When the IEEE format contains a hidden bit, we know that
3527 it's zero at this point, and so shift up the significand
3528 and decrease the exponent to match. In this case, Motorola
3529 defines the explicit integer bit to be valid, so we don't
3530 know whether the msb is set or not. */
3531 SET_REAL_EXP (r
, fmt
->emin
);
3532 if (HOST_BITS_PER_LONG
== 32)
3534 r
->sig
[SIGSZ
-1] = sig_hi
;
3535 r
->sig
[SIGSZ
-2] = sig_lo
;
3538 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3542 else if (fmt
->has_signed_zero
)
3545 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3547 /* See above re "pseudo-infinities" and "pseudo-nans".
3548 Short summary is that the MSB will likely always be
3549 set, and that we don't care about it. */
3550 sig_hi
&= 0x7fffffff;
3552 if (sig_hi
|| sig_lo
)
3556 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3557 if (HOST_BITS_PER_LONG
== 32)
3559 r
->sig
[SIGSZ
-1] = sig_hi
;
3560 r
->sig
[SIGSZ
-2] = sig_lo
;
3563 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3575 SET_REAL_EXP (r
, exp
- 16383 + 1);
3576 if (HOST_BITS_PER_LONG
== 32)
3578 r
->sig
[SIGSZ
-1] = sig_hi
;
3579 r
->sig
[SIGSZ
-2] = sig_lo
;
3582 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3586 /* Convert from the internal format to the 12-byte Motorola format
3587 for an IEEE extended real. */
3589 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3594 /* Motorola chips are assumed always to be big-endian. Also, the
3595 padding in a Motorola extended real goes between the exponent and
3596 the mantissa; remove it. */
3597 intermed
[0] = buf
[2];
3598 intermed
[1] = buf
[1];
3599 intermed
[2] = (unsigned long)buf
[0] >> 16;
3601 decode_ieee_extended (fmt
, r
, intermed
);
3604 /* Convert from the internal format to the 12-byte Intel format for
3605 an IEEE extended real. */
3607 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3610 if (FLOAT_WORDS_BIG_ENDIAN
)
3612 /* All the padding in an Intel-format extended real goes at the high
3613 end, which in this case is after the mantissa, not the exponent.
3614 Therefore we must shift everything up 16 bits. */
3617 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3618 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3619 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3621 decode_ieee_extended (fmt
, r
, intermed
);
3624 /* decode_ieee_extended produces what we want directly. */
3625 decode_ieee_extended (fmt
, r
, buf
);
3628 /* Convert from the internal format to the 16-byte Intel format for
3629 an IEEE extended real. */
3631 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3634 /* All the padding in an Intel-format extended real goes at the high end. */
3635 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3638 const struct real_format ieee_extended_motorola_format
=
3640 encode_ieee_extended_motorola
,
3641 decode_ieee_extended_motorola
,
3659 const struct real_format ieee_extended_intel_96_format
=
3661 encode_ieee_extended_intel_96
,
3662 decode_ieee_extended_intel_96
,
3680 const struct real_format ieee_extended_intel_128_format
=
3682 encode_ieee_extended_intel_128
,
3683 decode_ieee_extended_intel_128
,
3701 /* The following caters to i386 systems that set the rounding precision
3702 to 53 bits instead of 64, e.g. FreeBSD. */
3703 const struct real_format ieee_extended_intel_96_round_53_format
=
3705 encode_ieee_extended_intel_96
,
3706 decode_ieee_extended_intel_96
,
3724 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3725 numbers whose sum is equal to the extended precision value. The number
3726 with greater magnitude is first. This format has the same magnitude
3727 range as an IEEE double precision value, but effectively 106 bits of
3728 significand precision. Infinity and NaN are represented by their IEEE
3729 double precision value stored in the first number, the second number is
3730 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3732 static void encode_ibm_extended (const struct real_format
*fmt
,
3733 long *, const REAL_VALUE_TYPE
*);
3734 static void decode_ibm_extended (const struct real_format
*,
3735 REAL_VALUE_TYPE
*, const long *);
3738 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3739 const REAL_VALUE_TYPE
*r
)
3741 REAL_VALUE_TYPE u
, normr
, v
;
3742 const struct real_format
*base_fmt
;
3744 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3746 /* Renormalize R before doing any arithmetic on it. */
3748 if (normr
.cl
== rvc_normal
)
3751 /* u = IEEE double precision portion of significand. */
3753 round_for_format (base_fmt
, &u
);
3754 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3756 if (u
.cl
== rvc_normal
)
3758 do_add (&v
, &normr
, &u
, 1);
3759 /* Call round_for_format since we might need to denormalize. */
3760 round_for_format (base_fmt
, &v
);
3761 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3765 /* Inf, NaN, 0 are all representable as doubles, so the
3766 least-significant part can be 0.0. */
3773 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3776 REAL_VALUE_TYPE u
, v
;
3777 const struct real_format
*base_fmt
;
3779 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3780 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3782 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3784 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3785 do_add (r
, &u
, &v
, 0);
3791 const struct real_format ibm_extended_format
=
3793 encode_ibm_extended
,
3794 decode_ibm_extended
,
3812 const struct real_format mips_extended_format
=
3814 encode_ibm_extended
,
3815 decode_ibm_extended
,
3834 /* IEEE quad precision format. */
3836 static void encode_ieee_quad (const struct real_format
*fmt
,
3837 long *, const REAL_VALUE_TYPE
*);
3838 static void decode_ieee_quad (const struct real_format
*,
3839 REAL_VALUE_TYPE
*, const long *);
3842 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3843 const REAL_VALUE_TYPE
*r
)
3845 unsigned long image3
, image2
, image1
, image0
, exp
;
3846 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3849 image3
= r
->sign
<< 31;
3854 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3863 image3
|= 32767 << 16;
3866 image3
|= 0x7fffffff;
3867 image2
= 0xffffffff;
3868 image1
= 0xffffffff;
3869 image0
= 0xffffffff;
3876 image3
|= 32767 << 16;
3880 if (fmt
->canonical_nan_lsbs_set
)
3883 image2
= image1
= image0
= 0xffffffff;
3886 else if (HOST_BITS_PER_LONG
== 32)
3891 image3
|= u
.sig
[3] & 0xffff;
3896 image1
= image0
>> 31 >> 1;
3898 image3
|= (image2
>> 31 >> 1) & 0xffff;
3899 image0
&= 0xffffffff;
3900 image2
&= 0xffffffff;
3902 if (r
->signalling
== fmt
->qnan_msb_set
)
3906 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3911 image3
|= 0x7fffffff;
3912 image2
= 0xffffffff;
3913 image1
= 0xffffffff;
3914 image0
= 0xffffffff;
3919 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3920 whereas the intermediate representation is 0.F x 2**exp.
3921 Which means we're off by one. */
3925 exp
= REAL_EXP (r
) + 16383 - 1;
3926 image3
|= exp
<< 16;
3928 if (HOST_BITS_PER_LONG
== 32)
3933 image3
|= u
.sig
[3] & 0xffff;
3938 image1
= image0
>> 31 >> 1;
3940 image3
|= (image2
>> 31 >> 1) & 0xffff;
3941 image0
&= 0xffffffff;
3942 image2
&= 0xffffffff;
3950 if (FLOAT_WORDS_BIG_ENDIAN
)
3967 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3970 unsigned long image3
, image2
, image1
, image0
;
3974 if (FLOAT_WORDS_BIG_ENDIAN
)
3988 image0
&= 0xffffffff;
3989 image1
&= 0xffffffff;
3990 image2
&= 0xffffffff;
3992 sign
= (image3
>> 31) & 1;
3993 exp
= (image3
>> 16) & 0x7fff;
3996 memset (r
, 0, sizeof (*r
));
4000 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4005 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4006 if (HOST_BITS_PER_LONG
== 32)
4015 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4016 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4021 else if (fmt
->has_signed_zero
)
4024 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4026 if (image3
| image2
| image1
| image0
)
4030 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4032 if (HOST_BITS_PER_LONG
== 32)
4041 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4042 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4044 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4056 SET_REAL_EXP (r
, exp
- 16383 + 1);
4058 if (HOST_BITS_PER_LONG
== 32)
4067 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4068 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4070 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4071 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4075 const struct real_format ieee_quad_format
=
4096 const struct real_format mips_quad_format
=
4117 /* Descriptions of VAX floating point formats can be found beginning at
4119 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4121 The thing to remember is that they're almost IEEE, except for word
4122 order, exponent bias, and the lack of infinities, nans, and denormals.
4124 We don't implement the H_floating format here, simply because neither
4125 the VAX or Alpha ports use it. */
4127 static void encode_vax_f (const struct real_format
*fmt
,
4128 long *, const REAL_VALUE_TYPE
*);
4129 static void decode_vax_f (const struct real_format
*,
4130 REAL_VALUE_TYPE
*, const long *);
4131 static void encode_vax_d (const struct real_format
*fmt
,
4132 long *, const REAL_VALUE_TYPE
*);
4133 static void decode_vax_d (const struct real_format
*,
4134 REAL_VALUE_TYPE
*, const long *);
4135 static void encode_vax_g (const struct real_format
*fmt
,
4136 long *, const REAL_VALUE_TYPE
*);
4137 static void decode_vax_g (const struct real_format
*,
4138 REAL_VALUE_TYPE
*, const long *);
4141 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4142 const REAL_VALUE_TYPE
*r
)
4144 unsigned long sign
, exp
, sig
, image
;
4146 sign
= r
->sign
<< 15;
4156 image
= 0xffff7fff | sign
;
4160 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4161 exp
= REAL_EXP (r
) + 128;
4163 image
= (sig
<< 16) & 0xffff0000;
4177 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4178 REAL_VALUE_TYPE
*r
, const long *buf
)
4180 unsigned long image
= buf
[0] & 0xffffffff;
4181 int exp
= (image
>> 7) & 0xff;
4183 memset (r
, 0, sizeof (*r
));
4188 r
->sign
= (image
>> 15) & 1;
4189 SET_REAL_EXP (r
, exp
- 128);
4191 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4192 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4197 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4198 const REAL_VALUE_TYPE
*r
)
4200 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4205 image0
= image1
= 0;
4210 image0
= 0xffff7fff | sign
;
4211 image1
= 0xffffffff;
4215 /* Extract the significand into straight hi:lo. */
4216 if (HOST_BITS_PER_LONG
== 64)
4218 image0
= r
->sig
[SIGSZ
-1];
4219 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4220 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4224 image0
= r
->sig
[SIGSZ
-1];
4225 image1
= r
->sig
[SIGSZ
-2];
4226 image1
= (image0
<< 24) | (image1
>> 8);
4227 image0
= (image0
>> 8) & 0xffffff;
4230 /* Rearrange the half-words of the significand to match the
4232 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4233 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4235 /* Add the sign and exponent. */
4237 image0
|= (REAL_EXP (r
) + 128) << 7;
4244 if (FLOAT_WORDS_BIG_ENDIAN
)
4245 buf
[0] = image1
, buf
[1] = image0
;
4247 buf
[0] = image0
, buf
[1] = image1
;
4251 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4252 REAL_VALUE_TYPE
*r
, const long *buf
)
4254 unsigned long image0
, image1
;
4257 if (FLOAT_WORDS_BIG_ENDIAN
)
4258 image1
= buf
[0], image0
= buf
[1];
4260 image0
= buf
[0], image1
= buf
[1];
4261 image0
&= 0xffffffff;
4262 image1
&= 0xffffffff;
4264 exp
= (image0
>> 7) & 0xff;
4266 memset (r
, 0, sizeof (*r
));
4271 r
->sign
= (image0
>> 15) & 1;
4272 SET_REAL_EXP (r
, exp
- 128);
4274 /* Rearrange the half-words of the external format into
4275 proper ascending order. */
4276 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4277 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4279 if (HOST_BITS_PER_LONG
== 64)
4281 image0
= (image0
<< 31 << 1) | image1
;
4284 r
->sig
[SIGSZ
-1] = image0
;
4288 r
->sig
[SIGSZ
-1] = image0
;
4289 r
->sig
[SIGSZ
-2] = image1
;
4290 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4291 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4297 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4298 const REAL_VALUE_TYPE
*r
)
4300 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4305 image0
= image1
= 0;
4310 image0
= 0xffff7fff | sign
;
4311 image1
= 0xffffffff;
4315 /* Extract the significand into straight hi:lo. */
4316 if (HOST_BITS_PER_LONG
== 64)
4318 image0
= r
->sig
[SIGSZ
-1];
4319 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4320 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4324 image0
= r
->sig
[SIGSZ
-1];
4325 image1
= r
->sig
[SIGSZ
-2];
4326 image1
= (image0
<< 21) | (image1
>> 11);
4327 image0
= (image0
>> 11) & 0xfffff;
4330 /* Rearrange the half-words of the significand to match the
4332 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4333 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4335 /* Add the sign and exponent. */
4337 image0
|= (REAL_EXP (r
) + 1024) << 4;
4344 if (FLOAT_WORDS_BIG_ENDIAN
)
4345 buf
[0] = image1
, buf
[1] = image0
;
4347 buf
[0] = image0
, buf
[1] = image1
;
4351 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4352 REAL_VALUE_TYPE
*r
, const long *buf
)
4354 unsigned long image0
, image1
;
4357 if (FLOAT_WORDS_BIG_ENDIAN
)
4358 image1
= buf
[0], image0
= buf
[1];
4360 image0
= buf
[0], image1
= buf
[1];
4361 image0
&= 0xffffffff;
4362 image1
&= 0xffffffff;
4364 exp
= (image0
>> 4) & 0x7ff;
4366 memset (r
, 0, sizeof (*r
));
4371 r
->sign
= (image0
>> 15) & 1;
4372 SET_REAL_EXP (r
, exp
- 1024);
4374 /* Rearrange the half-words of the external format into
4375 proper ascending order. */
4376 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4377 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4379 if (HOST_BITS_PER_LONG
== 64)
4381 image0
= (image0
<< 31 << 1) | image1
;
4384 r
->sig
[SIGSZ
-1] = image0
;
4388 r
->sig
[SIGSZ
-1] = image0
;
4389 r
->sig
[SIGSZ
-2] = image1
;
4390 lshift_significand (r
, r
, 64 - 53);
4391 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4396 const struct real_format vax_f_format
=
4417 const struct real_format vax_d_format
=
4438 const struct real_format vax_g_format
=
4459 /* Encode real R into a single precision DFP value in BUF. */
4461 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4462 long *buf ATTRIBUTE_UNUSED
,
4463 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4465 encode_decimal32 (fmt
, buf
, r
);
4468 /* Decode a single precision DFP value in BUF into a real R. */
4470 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4471 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4472 const long *buf ATTRIBUTE_UNUSED
)
4474 decode_decimal32 (fmt
, r
, buf
);
4477 /* Encode real R into a double precision DFP value in BUF. */
4479 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4480 long *buf ATTRIBUTE_UNUSED
,
4481 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4483 encode_decimal64 (fmt
, buf
, r
);
4486 /* Decode a double precision DFP value in BUF into a real R. */
4488 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4489 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4490 const long *buf ATTRIBUTE_UNUSED
)
4492 decode_decimal64 (fmt
, r
, buf
);
4495 /* Encode real R into a quad precision DFP value in BUF. */
4497 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4498 long *buf ATTRIBUTE_UNUSED
,
4499 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4501 encode_decimal128 (fmt
, buf
, r
);
4504 /* Decode a quad precision DFP value in BUF into a real R. */
4506 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4507 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4508 const long *buf ATTRIBUTE_UNUSED
)
4510 decode_decimal128 (fmt
, r
, buf
);
4513 /* Single precision decimal floating point (IEEE 754). */
4514 const struct real_format decimal_single_format
=
4516 encode_decimal_single
,
4517 decode_decimal_single
,
4535 /* Double precision decimal floating point (IEEE 754). */
4536 const struct real_format decimal_double_format
=
4538 encode_decimal_double
,
4539 decode_decimal_double
,
4557 /* Quad precision decimal floating point (IEEE 754). */
4558 const struct real_format decimal_quad_format
=
4560 encode_decimal_quad
,
4561 decode_decimal_quad
,
4579 /* Encode half-precision floats. This routine is used both for the IEEE
4580 ARM alternative encodings. */
4582 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4583 const REAL_VALUE_TYPE
*r
)
4585 unsigned long image
, sig
, exp
;
4586 unsigned long sign
= r
->sign
;
4587 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4590 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4608 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4609 if (r
->signalling
== fmt
->qnan_msb_set
)
4624 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4625 whereas the intermediate representation is 0.F x 2**exp.
4626 Which means we're off by one. */
4630 exp
= REAL_EXP (r
) + 15 - 1;
4642 /* Decode half-precision floats. This routine is used both for the IEEE
4643 ARM alternative encodings. */
4645 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4648 unsigned long image
= buf
[0] & 0xffff;
4649 bool sign
= (image
>> 15) & 1;
4650 int exp
= (image
>> 10) & 0x1f;
4652 memset (r
, 0, sizeof (*r
));
4653 image
<<= HOST_BITS_PER_LONG
- 11;
4658 if (image
&& fmt
->has_denorm
)
4662 SET_REAL_EXP (r
, -14);
4663 r
->sig
[SIGSZ
-1] = image
<< 1;
4666 else if (fmt
->has_signed_zero
)
4669 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4675 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4676 ^ fmt
->qnan_msb_set
);
4677 r
->sig
[SIGSZ
-1] = image
;
4689 SET_REAL_EXP (r
, exp
- 15 + 1);
4690 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4694 /* Half-precision format, as specified in IEEE 754R. */
4695 const struct real_format ieee_half_format
=
4716 /* ARM's alternative half-precision format, similar to IEEE but with
4717 no reserved exponent value for NaNs and infinities; rather, it just
4718 extends the range of exponents by one. */
4719 const struct real_format arm_half_format
=
4740 /* A synthetic "format" for internal arithmetic. It's the size of the
4741 internal significand minus the two bits needed for proper rounding.
4742 The encode and decode routines exist only to satisfy our paranoia
4745 static void encode_internal (const struct real_format
*fmt
,
4746 long *, const REAL_VALUE_TYPE
*);
4747 static void decode_internal (const struct real_format
*,
4748 REAL_VALUE_TYPE
*, const long *);
4751 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4752 const REAL_VALUE_TYPE
*r
)
4754 memcpy (buf
, r
, sizeof (*r
));
4758 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4759 REAL_VALUE_TYPE
*r
, const long *buf
)
4761 memcpy (r
, buf
, sizeof (*r
));
4764 const struct real_format real_internal_format
=
4769 SIGNIFICAND_BITS
- 2,
4770 SIGNIFICAND_BITS
- 2,
4785 /* Calculate the square root of X in mode MODE, and store the result
4786 in R. Return TRUE if the operation does not raise an exception.
4787 For details see "High Precision Division and Square Root",
4788 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4789 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4792 real_sqrt (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4793 const REAL_VALUE_TYPE
*x
)
4795 static REAL_VALUE_TYPE halfthree
;
4796 static bool init
= false;
4797 REAL_VALUE_TYPE h
, t
, i
;
4800 /* sqrt(-0.0) is -0.0. */
4801 if (real_isnegzero (x
))
4807 /* Negative arguments return NaN. */
4810 get_canonical_qnan (r
, 0);
4814 /* Infinity and NaN return themselves. */
4815 if (!real_isfinite (x
))
4823 do_add (&halfthree
, &dconst1
, &dconsthalf
, 0);
4827 /* Initial guess for reciprocal sqrt, i. */
4828 exp
= real_exponent (x
);
4829 real_ldexp (&i
, &dconst1
, -exp
/2);
4831 /* Newton's iteration for reciprocal sqrt, i. */
4832 for (iter
= 0; iter
< 16; iter
++)
4834 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4835 do_multiply (&t
, x
, &i
);
4836 do_multiply (&h
, &t
, &i
);
4837 do_multiply (&t
, &h
, &dconsthalf
);
4838 do_add (&h
, &halfthree
, &t
, 1);
4839 do_multiply (&t
, &i
, &h
);
4841 /* Check for early convergence. */
4842 if (iter
>= 6 && real_identical (&i
, &t
))
4845 /* ??? Unroll loop to avoid copying. */
4849 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4850 do_multiply (&t
, x
, &i
);
4851 do_multiply (&h
, &t
, &i
);
4852 do_add (&i
, &dconst1
, &h
, 1);
4853 do_multiply (&h
, &t
, &i
);
4854 do_multiply (&i
, &dconsthalf
, &h
);
4855 do_add (&h
, &t
, &i
, 0);
4857 /* ??? We need a Tuckerman test to get the last bit. */
4859 real_convert (r
, mode
, &h
);
4863 /* Calculate X raised to the integer exponent N in mode MODE and store
4864 the result in R. Return true if the result may be inexact due to
4865 loss of precision. The algorithm is the classic "left-to-right binary
4866 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4867 Algorithms", "The Art of Computer Programming", Volume 2. */
4870 real_powi (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4871 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4873 unsigned HOST_WIDE_INT bit
;
4875 bool inexact
= false;
4887 /* Don't worry about overflow, from now on n is unsigned. */
4895 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4896 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4900 inexact
|= do_multiply (&t
, &t
, &t
);
4902 inexact
|= do_multiply (&t
, &t
, x
);
4910 inexact
|= do_divide (&t
, &dconst1
, &t
);
4912 real_convert (r
, mode
, &t
);
4916 /* Round X to the nearest integer not larger in absolute value, i.e.
4917 towards zero, placing the result in R in mode MODE. */
4920 real_trunc (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4921 const REAL_VALUE_TYPE
*x
)
4923 do_fix_trunc (r
, x
);
4924 if (mode
!= VOIDmode
)
4925 real_convert (r
, mode
, r
);
4928 /* Round X to the largest integer not greater in value, i.e. round
4929 down, placing the result in R in mode MODE. */
4932 real_floor (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4933 const REAL_VALUE_TYPE
*x
)
4937 do_fix_trunc (&t
, x
);
4938 if (! real_identical (&t
, x
) && x
->sign
)
4939 do_add (&t
, &t
, &dconstm1
, 0);
4940 if (mode
!= VOIDmode
)
4941 real_convert (r
, mode
, &t
);
4946 /* Round X to the smallest integer not less then argument, i.e. round
4947 up, placing the result in R in mode MODE. */
4950 real_ceil (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4951 const REAL_VALUE_TYPE
*x
)
4955 do_fix_trunc (&t
, x
);
4956 if (! real_identical (&t
, x
) && ! x
->sign
)
4957 do_add (&t
, &t
, &dconst1
, 0);
4958 if (mode
!= VOIDmode
)
4959 real_convert (r
, mode
, &t
);
4964 /* Round X to the nearest integer, but round halfway cases away from
4968 real_round (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4969 const REAL_VALUE_TYPE
*x
)
4971 do_add (r
, x
, &dconsthalf
, x
->sign
);
4972 do_fix_trunc (r
, r
);
4973 if (mode
!= VOIDmode
)
4974 real_convert (r
, mode
, r
);
4977 /* Set the sign of R to the sign of X. */
4980 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4985 /* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible
4986 for initializing and clearing the MPFR parameter. */
4989 mpfr_from_real (mpfr_ptr m
, const REAL_VALUE_TYPE
*r
, mp_rnd_t rndmode
)
4991 /* We use a string as an intermediate type. */
4995 /* Take care of Infinity and NaN. */
4996 if (r
->cl
== rvc_inf
)
4998 mpfr_set_inf (m
, r
->sign
== 1 ? -1 : 1);
5002 if (r
->cl
== rvc_nan
)
5008 real_to_hexadecimal (buf
, r
, sizeof (buf
), 0, 1);
5009 /* mpfr_set_str() parses hexadecimal floats from strings in the same
5010 format that GCC will output them. Nothing extra is needed. */
5011 ret
= mpfr_set_str (m
, buf
, 16, rndmode
);
5012 gcc_assert (ret
== 0);
5015 /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
5016 mode RNDMODE. TYPE is only relevant if M is a NaN. */
5019 real_from_mpfr (REAL_VALUE_TYPE
*r
, mpfr_srcptr m
, tree type
, mp_rnd_t rndmode
)
5021 /* We use a string as an intermediate type. */
5022 char buf
[128], *rstr
;
5025 /* Take care of Infinity and NaN. */
5029 if (mpfr_sgn (m
) < 0)
5030 *r
= REAL_VALUE_NEGATE (*r
);
5036 real_nan (r
, "", 1, TYPE_MODE (type
));
5040 rstr
= mpfr_get_str (NULL
, &exp
, 16, 0, m
, rndmode
);
5042 /* The additional 12 chars add space for the sprintf below. This
5043 leaves 6 digits for the exponent which is supposedly enough. */
5044 gcc_assert (rstr
!= NULL
&& strlen (rstr
) < sizeof (buf
) - 12);
5046 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
5047 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
5052 sprintf (buf
, "-0x.%sp%d", &rstr
[1], (int) exp
);
5054 sprintf (buf
, "0x.%sp%d", rstr
, (int) exp
);
5056 mpfr_free_str (rstr
);
5058 real_from_string (r
, buf
);
5061 /* Check whether the real constant value given is an integer. */
5064 real_isinteger (const REAL_VALUE_TYPE
*c
, enum machine_mode mode
)
5066 REAL_VALUE_TYPE cint
;
5068 real_trunc (&cint
, mode
, c
);
5069 return real_identical (c
, &cint
);
5072 /* Write into BUF the maximum representable finite floating-point
5073 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5074 float string. LEN is the size of BUF, and the buffer must be large
5075 enough to contain the resulting string. */
5078 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
5083 strcpy (buf
, "0x0.");
5085 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
5088 *p
++ = "08ce"[n
- i
];
5089 sprintf (p
, "p%d", fmt
->emax
);
5090 if (fmt
->pnan
< fmt
->p
)
5092 /* This is an IBM extended double format made up of two IEEE
5093 doubles. The value of the long double is the sum of the
5094 values of the two parts. The most significant part is
5095 required to be the value of the long double rounded to the
5096 nearest double. Rounding means we need a slightly smaller
5097 value for LDBL_MAX. */
5098 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
5101 gcc_assert (strlen (buf
) < len
);