1 /* real.c - software floating point emulation.
2 Copyright (C) 1993-2014 Free Software Foundation, Inc.
3 Contributed by Stephen L. Moshier (moshier@world.std.com).
4 Re-written by Richard Henderson <rth@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
27 #include "diagnostic-core.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. */
63 /* Used to classify two numbers simultaneously. */
64 #define CLASS2(A, B) ((A) << 2 | (B))
66 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
67 #error "Some constant folding done by hand to avoid shift count warnings"
70 static void get_zero (REAL_VALUE_TYPE
*, int);
71 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
72 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
73 static void get_inf (REAL_VALUE_TYPE
*, int);
74 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
75 const REAL_VALUE_TYPE
*, unsigned int);
76 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
78 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
80 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
81 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
82 const REAL_VALUE_TYPE
*);
83 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
84 const REAL_VALUE_TYPE
*, int);
85 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
86 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
87 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
88 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
89 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
90 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
91 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
92 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
93 const REAL_VALUE_TYPE
*);
94 static void normalize (REAL_VALUE_TYPE
*);
96 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
97 const REAL_VALUE_TYPE
*, int);
98 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
99 const REAL_VALUE_TYPE
*);
100 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
101 const REAL_VALUE_TYPE
*);
102 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
103 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
105 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, REAL_VALUE_TYPE
*);
106 static void decimal_from_integer (REAL_VALUE_TYPE
*);
107 static void decimal_integer_string (char *, const REAL_VALUE_TYPE
*,
110 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
111 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
112 static const REAL_VALUE_TYPE
* real_digit (int);
113 static void times_pten (REAL_VALUE_TYPE
*, int);
115 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
117 /* Initialize R with a positive zero. */
120 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
122 memset (r
, 0, sizeof (*r
));
126 /* Initialize R with the canonical quiet NaN. */
129 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
131 memset (r
, 0, sizeof (*r
));
138 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
140 memset (r
, 0, sizeof (*r
));
148 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
150 memset (r
, 0, sizeof (*r
));
156 /* Right-shift the significand of A by N bits; put the result in the
157 significand of R. If any one bits are shifted out, return true. */
160 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
163 unsigned long sticky
= 0;
164 unsigned int i
, ofs
= 0;
166 if (n
>= HOST_BITS_PER_LONG
)
168 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
170 n
&= HOST_BITS_PER_LONG
- 1;
175 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
176 for (i
= 0; i
< SIGSZ
; ++i
)
179 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
180 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
181 << (HOST_BITS_PER_LONG
- n
)));
186 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
187 r
->sig
[i
] = a
->sig
[ofs
+ i
];
188 for (; i
< SIGSZ
; ++i
)
195 /* Right-shift the significand of A by N bits; put the result in the
199 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
202 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
204 n
&= HOST_BITS_PER_LONG
- 1;
207 for (i
= 0; i
< SIGSZ
; ++i
)
210 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
211 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
212 << (HOST_BITS_PER_LONG
- n
)));
217 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
218 r
->sig
[i
] = a
->sig
[ofs
+ i
];
219 for (; i
< SIGSZ
; ++i
)
224 /* Left-shift the significand of A by N bits; put the result in the
228 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
231 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
233 n
&= HOST_BITS_PER_LONG
- 1;
236 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
237 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
238 for (; i
< SIGSZ
; ++i
)
239 r
->sig
[SIGSZ
-1-i
] = 0;
242 for (i
= 0; i
< SIGSZ
; ++i
)
245 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
246 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
247 >> (HOST_BITS_PER_LONG
- n
)));
251 /* Likewise, but N is specialized to 1. */
254 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
258 for (i
= SIGSZ
- 1; i
> 0; --i
)
259 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
260 r
->sig
[0] = a
->sig
[0] << 1;
263 /* Add the significands of A and B, placing the result in R. Return
264 true if there was carry out of the most significant word. */
267 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
268 const REAL_VALUE_TYPE
*b
)
273 for (i
= 0; i
< SIGSZ
; ++i
)
275 unsigned long ai
= a
->sig
[i
];
276 unsigned long ri
= ai
+ b
->sig
[i
];
292 /* Subtract the significands of A and B, placing the result in R. CARRY is
293 true if there's a borrow incoming to the least significant word.
294 Return true if there was borrow out of the most significant word. */
297 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
298 const REAL_VALUE_TYPE
*b
, int carry
)
302 for (i
= 0; i
< SIGSZ
; ++i
)
304 unsigned long ai
= a
->sig
[i
];
305 unsigned long ri
= ai
- b
->sig
[i
];
321 /* Negate the significand A, placing the result in R. */
324 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
329 for (i
= 0; i
< SIGSZ
; ++i
)
331 unsigned long ri
, ai
= a
->sig
[i
];
350 /* Compare significands. Return tri-state vs zero. */
353 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
357 for (i
= SIGSZ
- 1; i
>= 0; --i
)
359 unsigned long ai
= a
->sig
[i
];
360 unsigned long bi
= b
->sig
[i
];
371 /* Return true if A is nonzero. */
374 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
378 for (i
= SIGSZ
- 1; i
>= 0; --i
)
385 /* Set bit N of the significand of R. */
388 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
390 r
->sig
[n
/ HOST_BITS_PER_LONG
]
391 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
394 /* Clear bit N of the significand of R. */
397 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
399 r
->sig
[n
/ HOST_BITS_PER_LONG
]
400 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
403 /* Test bit N of the significand of R. */
406 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
408 /* ??? Compiler bug here if we return this expression directly.
409 The conversion to bool strips the "&1" and we wind up testing
410 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
411 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
415 /* Clear bits 0..N-1 of the significand of R. */
418 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
420 int i
, w
= n
/ HOST_BITS_PER_LONG
;
422 for (i
= 0; i
< w
; ++i
)
425 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
428 /* Divide the significands of A and B, placing the result in R. Return
429 true if the division was inexact. */
432 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
433 const REAL_VALUE_TYPE
*b
)
436 int i
, bit
= SIGNIFICAND_BITS
- 1;
437 unsigned long msb
, inexact
;
440 memset (r
->sig
, 0, sizeof (r
->sig
));
446 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
447 lshift_significand_1 (&u
, &u
);
449 if (msb
|| cmp_significands (&u
, b
) >= 0)
451 sub_significands (&u
, &u
, b
, 0);
452 set_significand_bit (r
, bit
);
457 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
463 /* Adjust the exponent and significand of R such that the most
464 significant bit is set. We underflow to zero and overflow to
465 infinity here, without denormals. (The intermediate representation
466 exponent is large enough to handle target denormals normalized.) */
469 normalize (REAL_VALUE_TYPE
*r
)
477 /* Find the first word that is nonzero. */
478 for (i
= SIGSZ
- 1; i
>= 0; i
--)
480 shift
+= HOST_BITS_PER_LONG
;
484 /* Zero significand flushes to zero. */
492 /* Find the first bit that is nonzero. */
494 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
500 exp
= REAL_EXP (r
) - shift
;
502 get_inf (r
, r
->sign
);
503 else if (exp
< -MAX_EXP
)
504 get_zero (r
, r
->sign
);
507 SET_REAL_EXP (r
, exp
);
508 lshift_significand (r
, r
, shift
);
513 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
514 result may be inexact due to a loss of precision. */
517 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
518 const REAL_VALUE_TYPE
*b
, int subtract_p
)
522 bool inexact
= false;
524 /* Determine if we need to add or subtract. */
526 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
528 switch (CLASS2 (a
->cl
, b
->cl
))
530 case CLASS2 (rvc_zero
, rvc_zero
):
531 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
532 get_zero (r
, sign
& !subtract_p
);
535 case CLASS2 (rvc_zero
, rvc_normal
):
536 case CLASS2 (rvc_zero
, rvc_inf
):
537 case CLASS2 (rvc_zero
, rvc_nan
):
539 case CLASS2 (rvc_normal
, rvc_nan
):
540 case CLASS2 (rvc_inf
, rvc_nan
):
541 case CLASS2 (rvc_nan
, rvc_nan
):
542 /* ANY + NaN = NaN. */
543 case CLASS2 (rvc_normal
, rvc_inf
):
546 r
->sign
= sign
^ subtract_p
;
549 case CLASS2 (rvc_normal
, rvc_zero
):
550 case CLASS2 (rvc_inf
, rvc_zero
):
551 case CLASS2 (rvc_nan
, rvc_zero
):
553 case CLASS2 (rvc_nan
, rvc_normal
):
554 case CLASS2 (rvc_nan
, rvc_inf
):
555 /* NaN + ANY = NaN. */
556 case CLASS2 (rvc_inf
, rvc_normal
):
561 case CLASS2 (rvc_inf
, rvc_inf
):
563 /* Inf - Inf = NaN. */
564 get_canonical_qnan (r
, 0);
566 /* Inf + Inf = Inf. */
570 case CLASS2 (rvc_normal
, rvc_normal
):
577 /* Swap the arguments such that A has the larger exponent. */
578 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
581 const REAL_VALUE_TYPE
*t
;
588 /* If the exponents are not identical, we need to shift the
589 significand of B down. */
592 /* If the exponents are too far apart, the significands
593 do not overlap, which makes the subtraction a noop. */
594 if (dexp
>= SIGNIFICAND_BITS
)
601 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
607 if (sub_significands (r
, a
, b
, inexact
))
609 /* We got a borrow out of the subtraction. That means that
610 A and B had the same exponent, and B had the larger
611 significand. We need to swap the sign and negate the
614 neg_significand (r
, r
);
619 if (add_significands (r
, a
, b
))
621 /* We got carry out of the addition. This means we need to
622 shift the significand back down one bit and increase the
624 inexact
|= sticky_rshift_significand (r
, r
, 1);
625 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
636 SET_REAL_EXP (r
, exp
);
637 /* Zero out the remaining fields. */
642 /* Re-normalize the result. */
645 /* Special case: if the subtraction results in zero, the result
647 if (r
->cl
== rvc_zero
)
650 r
->sig
[0] |= inexact
;
655 /* Calculate R = A * B. Return true if the result may be inexact. */
658 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
659 const REAL_VALUE_TYPE
*b
)
661 REAL_VALUE_TYPE u
, t
, *rr
;
662 unsigned int i
, j
, k
;
663 int sign
= a
->sign
^ b
->sign
;
664 bool inexact
= false;
666 switch (CLASS2 (a
->cl
, b
->cl
))
668 case CLASS2 (rvc_zero
, rvc_zero
):
669 case CLASS2 (rvc_zero
, rvc_normal
):
670 case CLASS2 (rvc_normal
, rvc_zero
):
671 /* +-0 * ANY = 0 with appropriate sign. */
675 case CLASS2 (rvc_zero
, rvc_nan
):
676 case CLASS2 (rvc_normal
, rvc_nan
):
677 case CLASS2 (rvc_inf
, rvc_nan
):
678 case CLASS2 (rvc_nan
, rvc_nan
):
679 /* ANY * NaN = NaN. */
684 case CLASS2 (rvc_nan
, rvc_zero
):
685 case CLASS2 (rvc_nan
, rvc_normal
):
686 case CLASS2 (rvc_nan
, rvc_inf
):
687 /* NaN * ANY = NaN. */
692 case CLASS2 (rvc_zero
, rvc_inf
):
693 case CLASS2 (rvc_inf
, rvc_zero
):
695 get_canonical_qnan (r
, sign
);
698 case CLASS2 (rvc_inf
, rvc_inf
):
699 case CLASS2 (rvc_normal
, rvc_inf
):
700 case CLASS2 (rvc_inf
, rvc_normal
):
701 /* Inf * Inf = Inf, R * Inf = Inf */
705 case CLASS2 (rvc_normal
, rvc_normal
):
712 if (r
== a
|| r
== b
)
718 /* Collect all the partial products. Since we don't have sure access
719 to a widening multiply, we split each long into two half-words.
721 Consider the long-hand form of a four half-word multiplication:
731 We construct partial products of the widened half-word products
732 that are known to not overlap, e.g. DF+DH. Each such partial
733 product is given its proper exponent, which allows us to sum them
734 and obtain the finished product. */
736 for (i
= 0; i
< SIGSZ
* 2; ++i
)
738 unsigned long ai
= a
->sig
[i
/ 2];
740 ai
>>= HOST_BITS_PER_LONG
/ 2;
742 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
747 for (j
= 0; j
< 2; ++j
)
749 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
750 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
759 /* Would underflow to zero, which we shouldn't bother adding. */
764 memset (&u
, 0, sizeof (u
));
766 SET_REAL_EXP (&u
, exp
);
768 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
770 unsigned long bi
= b
->sig
[k
/ 2];
772 bi
>>= HOST_BITS_PER_LONG
/ 2;
774 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
776 u
.sig
[k
/ 2] = ai
* bi
;
780 inexact
|= do_add (rr
, rr
, &u
, 0);
791 /* Calculate R = A / B. Return true if the result may be inexact. */
794 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
795 const REAL_VALUE_TYPE
*b
)
797 int exp
, sign
= a
->sign
^ b
->sign
;
798 REAL_VALUE_TYPE t
, *rr
;
801 switch (CLASS2 (a
->cl
, b
->cl
))
803 case CLASS2 (rvc_zero
, rvc_zero
):
805 case CLASS2 (rvc_inf
, rvc_inf
):
806 /* Inf / Inf = NaN. */
807 get_canonical_qnan (r
, sign
);
810 case CLASS2 (rvc_zero
, rvc_normal
):
811 case CLASS2 (rvc_zero
, rvc_inf
):
813 case CLASS2 (rvc_normal
, rvc_inf
):
818 case CLASS2 (rvc_normal
, rvc_zero
):
820 case CLASS2 (rvc_inf
, rvc_zero
):
825 case CLASS2 (rvc_zero
, rvc_nan
):
826 case CLASS2 (rvc_normal
, rvc_nan
):
827 case CLASS2 (rvc_inf
, rvc_nan
):
828 case CLASS2 (rvc_nan
, rvc_nan
):
829 /* ANY / NaN = NaN. */
834 case CLASS2 (rvc_nan
, rvc_zero
):
835 case CLASS2 (rvc_nan
, rvc_normal
):
836 case CLASS2 (rvc_nan
, rvc_inf
):
837 /* NaN / ANY = NaN. */
842 case CLASS2 (rvc_inf
, rvc_normal
):
847 case CLASS2 (rvc_normal
, rvc_normal
):
854 if (r
== a
|| r
== b
)
859 /* Make sure all fields in the result are initialized. */
864 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
875 SET_REAL_EXP (rr
, exp
);
877 inexact
= div_significands (rr
, a
, b
);
879 /* Re-normalize the result. */
881 rr
->sig
[0] |= inexact
;
889 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
890 one of the two operands is a NaN. */
893 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
898 switch (CLASS2 (a
->cl
, b
->cl
))
900 case CLASS2 (rvc_zero
, rvc_zero
):
901 /* Sign of zero doesn't matter for compares. */
904 case CLASS2 (rvc_normal
, rvc_zero
):
905 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
907 return decimal_do_compare (a
, b
, nan_result
);
909 case CLASS2 (rvc_inf
, rvc_zero
):
910 case CLASS2 (rvc_inf
, rvc_normal
):
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 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
919 return decimal_do_compare (a
, b
, nan_result
);
921 case CLASS2 (rvc_zero
, rvc_inf
):
922 case CLASS2 (rvc_normal
, rvc_inf
):
923 return (b
->sign
? 1 : -1);
925 case CLASS2 (rvc_zero
, rvc_nan
):
926 case CLASS2 (rvc_normal
, rvc_nan
):
927 case CLASS2 (rvc_inf
, rvc_nan
):
928 case CLASS2 (rvc_nan
, rvc_nan
):
929 case CLASS2 (rvc_nan
, rvc_zero
):
930 case CLASS2 (rvc_nan
, rvc_normal
):
931 case CLASS2 (rvc_nan
, rvc_inf
):
934 case CLASS2 (rvc_normal
, rvc_normal
):
941 if (a
->sign
!= b
->sign
)
942 return -a
->sign
- -b
->sign
;
944 if (a
->decimal
|| b
->decimal
)
945 return decimal_do_compare (a
, b
, nan_result
);
947 if (REAL_EXP (a
) > REAL_EXP (b
))
949 else if (REAL_EXP (a
) < REAL_EXP (b
))
952 ret
= cmp_significands (a
, b
);
954 return (a
->sign
? -ret
: ret
);
957 /* Return A truncated to an integral value toward zero. */
960 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
974 decimal_do_fix_trunc (r
, a
);
977 if (REAL_EXP (r
) <= 0)
978 get_zero (r
, r
->sign
);
979 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
980 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
988 /* Perform the binary or unary operation described by CODE.
989 For a unary operation, leave OP1 NULL. This function returns
990 true if the result may be inexact due to loss of precision. */
993 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
994 const REAL_VALUE_TYPE
*op1
)
996 enum tree_code code
= (enum tree_code
) icode
;
998 if (op0
->decimal
|| (op1
&& op1
->decimal
))
999 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1004 /* Clear any padding areas in *r if it isn't equal to one of the
1005 operands so that we can later do bitwise comparisons later on. */
1006 if (r
!= op0
&& r
!= op1
)
1007 memset (r
, '\0', sizeof (*r
));
1008 return do_add (r
, op0
, op1
, 0);
1011 if (r
!= op0
&& r
!= op1
)
1012 memset (r
, '\0', sizeof (*r
));
1013 return do_add (r
, op0
, op1
, 1);
1016 if (r
!= op0
&& r
!= op1
)
1017 memset (r
, '\0', sizeof (*r
));
1018 return do_multiply (r
, op0
, op1
);
1021 if (r
!= op0
&& r
!= op1
)
1022 memset (r
, '\0', sizeof (*r
));
1023 return do_divide (r
, op0
, op1
);
1026 if (op1
->cl
== rvc_nan
)
1028 else if (do_compare (op0
, op1
, -1) < 0)
1035 if (op1
->cl
== rvc_nan
)
1037 else if (do_compare (op0
, op1
, 1) < 0)
1053 case FIX_TRUNC_EXPR
:
1054 do_fix_trunc (r
, op0
);
1064 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1067 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1072 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1075 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1080 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1081 const REAL_VALUE_TYPE
*op1
)
1083 enum tree_code code
= (enum tree_code
) icode
;
1088 return do_compare (op0
, op1
, 1) < 0;
1090 return do_compare (op0
, op1
, 1) <= 0;
1092 return do_compare (op0
, op1
, -1) > 0;
1094 return do_compare (op0
, op1
, -1) >= 0;
1096 return do_compare (op0
, op1
, -1) == 0;
1098 return do_compare (op0
, op1
, -1) != 0;
1099 case UNORDERED_EXPR
:
1100 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1102 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1104 return do_compare (op0
, op1
, -1) < 0;
1106 return do_compare (op0
, op1
, -1) <= 0;
1108 return do_compare (op0
, op1
, 1) > 0;
1110 return do_compare (op0
, op1
, 1) >= 0;
1112 return do_compare (op0
, op1
, 0) == 0;
1114 return do_compare (op0
, op1
, 0) != 0;
1121 /* Return floor log2(R). */
1124 real_exponent (const REAL_VALUE_TYPE
*r
)
1132 return (unsigned int)-1 >> 1;
1134 return REAL_EXP (r
);
1140 /* R = OP0 * 2**EXP. */
1143 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1154 exp
+= REAL_EXP (op0
);
1156 get_inf (r
, r
->sign
);
1157 else if (exp
< -MAX_EXP
)
1158 get_zero (r
, r
->sign
);
1160 SET_REAL_EXP (r
, exp
);
1168 /* Determine whether a floating-point value X is infinite. */
1171 real_isinf (const REAL_VALUE_TYPE
*r
)
1173 return (r
->cl
== rvc_inf
);
1176 /* Determine whether a floating-point value X is a NaN. */
1179 real_isnan (const REAL_VALUE_TYPE
*r
)
1181 return (r
->cl
== rvc_nan
);
1184 /* Determine whether a floating-point value X is finite. */
1187 real_isfinite (const REAL_VALUE_TYPE
*r
)
1189 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1192 /* Determine whether a floating-point value X is negative. */
1195 real_isneg (const REAL_VALUE_TYPE
*r
)
1200 /* Determine whether a floating-point value X is minus zero. */
1203 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1205 return r
->sign
&& r
->cl
== rvc_zero
;
1208 /* Compare two floating-point objects for bitwise identity. */
1211 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1217 if (a
->sign
!= b
->sign
)
1227 if (a
->decimal
!= b
->decimal
)
1229 if (REAL_EXP (a
) != REAL_EXP (b
))
1234 if (a
->signalling
!= b
->signalling
)
1236 /* The significand is ignored for canonical NaNs. */
1237 if (a
->canonical
|| b
->canonical
)
1238 return a
->canonical
== b
->canonical
;
1245 for (i
= 0; i
< SIGSZ
; ++i
)
1246 if (a
->sig
[i
] != b
->sig
[i
])
1252 /* Try to change R into its exact multiplicative inverse in machine
1253 mode MODE. Return true if successful. */
1256 exact_real_inverse (enum machine_mode mode
, REAL_VALUE_TYPE
*r
)
1258 const REAL_VALUE_TYPE
*one
= real_digit (1);
1262 if (r
->cl
!= rvc_normal
)
1265 /* Check for a power of two: all significand bits zero except the MSB. */
1266 for (i
= 0; i
< SIGSZ
-1; ++i
)
1269 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1272 /* Find the inverse and truncate to the required mode. */
1273 do_divide (&u
, one
, r
);
1274 real_convert (&u
, mode
, &u
);
1276 /* The rounding may have overflowed. */
1277 if (u
.cl
!= rvc_normal
)
1279 for (i
= 0; i
< SIGSZ
-1; ++i
)
1282 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1289 /* Return true if arithmetic on values in IMODE that were promoted
1290 from values in TMODE is equivalent to direct arithmetic on values
1294 real_can_shorten_arithmetic (enum machine_mode imode
, enum machine_mode tmode
)
1296 const struct real_format
*tfmt
, *ifmt
;
1297 tfmt
= REAL_MODE_FORMAT (tmode
);
1298 ifmt
= REAL_MODE_FORMAT (imode
);
1299 /* These conditions are conservative rather than trying to catch the
1300 exact boundary conditions; the main case to allow is IEEE float
1302 return (ifmt
->b
== tfmt
->b
1303 && ifmt
->p
> 2 * tfmt
->p
1304 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1305 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1306 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1307 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1308 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1309 && (ifmt
->has_sign_dependent_rounding
1310 == tfmt
->has_sign_dependent_rounding
)
1311 && ifmt
->has_nans
>= tfmt
->has_nans
1312 && ifmt
->has_inf
>= tfmt
->has_inf
1313 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1314 && !MODE_COMPOSITE_P (tmode
)
1315 && !MODE_COMPOSITE_P (imode
));
1318 /* Render R as an integer. */
1321 real_to_integer (const REAL_VALUE_TYPE
*r
)
1323 unsigned HOST_WIDE_INT i
;
1334 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1341 return decimal_real_to_integer (r
);
1343 if (REAL_EXP (r
) <= 0)
1345 /* Only force overflow for unsigned overflow. Signed overflow is
1346 undefined, so it doesn't matter what we return, and some callers
1347 expect to be able to use this routine for both signed and
1348 unsigned conversions. */
1349 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1352 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1353 i
= r
->sig
[SIGSZ
-1];
1356 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1357 i
= r
->sig
[SIGSZ
-1];
1358 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1359 i
|= r
->sig
[SIGSZ
-2];
1362 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1373 /* Likewise, but to an integer pair, HI+LOW. */
1376 real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
1377 const REAL_VALUE_TYPE
*r
)
1380 unsigned HOST_WIDE_INT low
;
1394 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1407 decimal_real_to_integer2 (plow
, phigh
, r
);
1414 /* Only force overflow for unsigned overflow. Signed overflow is
1415 undefined, so it doesn't matter what we return, and some callers
1416 expect to be able to use this routine for both signed and
1417 unsigned conversions. */
1418 if (exp
> HOST_BITS_PER_DOUBLE_INT
)
1421 rshift_significand (&t
, r
, HOST_BITS_PER_DOUBLE_INT
- exp
);
1422 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1424 high
= t
.sig
[SIGSZ
-1];
1425 low
= t
.sig
[SIGSZ
-2];
1429 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
);
1430 high
= t
.sig
[SIGSZ
-1];
1431 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1432 high
|= t
.sig
[SIGSZ
-2];
1434 low
= t
.sig
[SIGSZ
-3];
1435 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1436 low
|= t
.sig
[SIGSZ
-4];
1444 low
= -low
, high
= ~high
;
1456 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1457 of NUM / DEN. Return the quotient and place the remainder in NUM.
1458 It is expected that NUM / DEN are close enough that the quotient is
1461 static unsigned long
1462 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1464 unsigned long q
, msb
;
1465 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1474 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1476 lshift_significand_1 (num
, num
);
1478 if (msb
|| cmp_significands (num
, den
) >= 0)
1480 sub_significands (num
, num
, den
, 0);
1484 while (--expn
>= expd
);
1486 SET_REAL_EXP (num
, expd
);
1492 /* Render R as a decimal floating point constant. Emit DIGITS significant
1493 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1494 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1495 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1496 to a string that, when parsed back in mode MODE, yields the same value. */
1498 #define M_LOG10_2 0.30102999566398119521
1501 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1502 size_t buf_size
, size_t digits
,
1503 int crop_trailing_zeros
, enum machine_mode mode
)
1505 const struct real_format
*fmt
= NULL
;
1506 const REAL_VALUE_TYPE
*one
, *ten
;
1507 REAL_VALUE_TYPE r
, pten
, u
, v
;
1508 int dec_exp
, cmp_one
, digit
;
1510 char *p
, *first
, *last
;
1514 if (mode
!= VOIDmode
)
1516 fmt
= REAL_MODE_FORMAT (mode
);
1524 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1529 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1532 /* ??? Print the significand as well, if not canonical? */
1533 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1534 (r_orig
->signalling
? 'S' : 'Q'));
1542 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1546 /* Bound the number of digits printed by the size of the representation. */
1547 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1548 if (digits
== 0 || digits
> max_digits
)
1549 digits
= max_digits
;
1551 /* Estimate the decimal exponent, and compute the length of the string it
1552 will print as. Be conservative and add one to account for possible
1553 overflow or rounding error. */
1554 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1555 for (max_digits
= 1; dec_exp
; max_digits
++)
1558 /* Bound the number of digits printed by the size of the output buffer. */
1559 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1560 gcc_assert (max_digits
<= buf_size
);
1561 if (digits
> max_digits
)
1562 digits
= max_digits
;
1564 one
= real_digit (1);
1565 ten
= ten_to_ptwo (0);
1573 cmp_one
= do_compare (&r
, one
, 0);
1578 /* Number is greater than one. Convert significand to an integer
1579 and strip trailing decimal zeros. */
1582 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1584 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1585 m
= floor_log2 (max_digits
);
1587 /* Iterate over the bits of the possible powers of 10 that might
1588 be present in U and eliminate them. That is, if we find that
1589 10**2**M divides U evenly, keep the division and increase
1595 do_divide (&t
, &u
, ten_to_ptwo (m
));
1596 do_fix_trunc (&v
, &t
);
1597 if (cmp_significands (&v
, &t
) == 0)
1605 /* Revert the scaling to integer that we performed earlier. */
1606 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1607 - (SIGNIFICAND_BITS
- 1));
1610 /* Find power of 10. Do this by dividing out 10**2**M when
1611 this is larger than the current remainder. Fill PTEN with
1612 the power of 10 that we compute. */
1613 if (REAL_EXP (&r
) > 0)
1615 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1618 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1619 if (do_compare (&u
, ptentwo
, 0) >= 0)
1621 do_divide (&u
, &u
, ptentwo
);
1622 do_multiply (&pten
, &pten
, ptentwo
);
1629 /* We managed to divide off enough tens in the above reduction
1630 loop that we've now got a negative exponent. Fall into the
1631 less-than-one code to compute the proper value for PTEN. */
1638 /* Number is less than one. Pad significand with leading
1644 /* Stop if we'd shift bits off the bottom. */
1648 do_multiply (&u
, &v
, ten
);
1650 /* Stop if we're now >= 1. */
1651 if (REAL_EXP (&u
) > 0)
1659 /* Find power of 10. Do this by multiplying in P=10**2**M when
1660 the current remainder is smaller than 1/P. Fill PTEN with the
1661 power of 10 that we compute. */
1662 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1665 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1666 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1668 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1670 do_multiply (&v
, &v
, ptentwo
);
1671 do_multiply (&pten
, &pten
, ptentwo
);
1677 /* Invert the positive power of 10 that we've collected so far. */
1678 do_divide (&pten
, one
, &pten
);
1686 /* At this point, PTEN should contain the nearest power of 10 smaller
1687 than R, such that this division produces the first digit.
1689 Using a divide-step primitive that returns the complete integral
1690 remainder avoids the rounding error that would be produced if
1691 we were to use do_divide here and then simply multiply by 10 for
1692 each subsequent digit. */
1694 digit
= rtd_divmod (&r
, &pten
);
1696 /* Be prepared for error in that division via underflow ... */
1697 if (digit
== 0 && cmp_significand_0 (&r
))
1699 /* Multiply by 10 and try again. */
1700 do_multiply (&r
, &r
, ten
);
1701 digit
= rtd_divmod (&r
, &pten
);
1703 gcc_assert (digit
!= 0);
1706 /* ... or overflow. */
1716 gcc_assert (digit
<= 10);
1720 /* Generate subsequent digits. */
1721 while (--digits
> 0)
1723 do_multiply (&r
, &r
, ten
);
1724 digit
= rtd_divmod (&r
, &pten
);
1729 /* Generate one more digit with which to do rounding. */
1730 do_multiply (&r
, &r
, ten
);
1731 digit
= rtd_divmod (&r
, &pten
);
1733 /* Round the result. */
1734 if (fmt
&& fmt
->round_towards_zero
)
1736 /* If the format uses round towards zero when parsing the string
1737 back in, we need to always round away from zero here. */
1738 if (cmp_significand_0 (&r
))
1740 round_up
= digit
> 0;
1746 /* Round to nearest. If R is nonzero there are additional
1747 nonzero digits to be extracted. */
1748 if (cmp_significand_0 (&r
))
1750 /* Round to even. */
1751 else if ((p
[-1] - '0') & 1)
1755 round_up
= digit
> 5;
1772 /* Carry out of the first digit. This means we had all 9's and
1773 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1781 /* Insert the decimal point. */
1782 first
[0] = first
[1];
1785 /* If requested, drop trailing zeros. Never crop past "1.0". */
1786 if (crop_trailing_zeros
)
1787 while (last
> first
+ 3 && last
[-1] == '0')
1790 /* Append the exponent. */
1791 sprintf (last
, "e%+d", dec_exp
);
1793 #ifdef ENABLE_CHECKING
1794 /* Verify that we can read the original value back in. */
1795 if (mode
!= VOIDmode
)
1797 real_from_string (&r
, str
);
1798 real_convert (&r
, mode
, &r
);
1799 gcc_assert (real_identical (&r
, r_orig
));
1804 /* Likewise, except always uses round-to-nearest. */
1807 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1808 size_t digits
, int crop_trailing_zeros
)
1810 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1811 digits
, crop_trailing_zeros
, VOIDmode
);
1814 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1815 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1816 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1817 strip trailing zeros. */
1820 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1821 size_t digits
, int crop_trailing_zeros
)
1823 int i
, j
, exp
= REAL_EXP (r
);
1836 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1839 /* ??? Print the significand as well, if not canonical? */
1840 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1841 (r
->signalling
? 'S' : 'Q'));
1849 /* Hexadecimal format for decimal floats is not interesting. */
1850 strcpy (str
, "N/A");
1855 digits
= SIGNIFICAND_BITS
/ 4;
1857 /* Bound the number of digits printed by the size of the output buffer. */
1859 sprintf (exp_buf
, "p%+d", exp
);
1860 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1861 gcc_assert (max_digits
<= buf_size
);
1862 if (digits
> max_digits
)
1863 digits
= max_digits
;
1874 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1875 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1877 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1883 if (crop_trailing_zeros
)
1884 while (p
> first
+ 1 && p
[-1] == '0')
1887 sprintf (p
, "p%+d", exp
);
1890 /* Initialize R from a decimal or hexadecimal string. The string is
1891 assumed to have been syntax checked already. Return -1 if the
1892 value underflows, +1 if overflows, and 0 otherwise. */
1895 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1907 else if (*str
== '+')
1910 if (!strncmp (str
, "QNaN", 4))
1912 get_canonical_qnan (r
, sign
);
1915 else if (!strncmp (str
, "SNaN", 4))
1917 get_canonical_snan (r
, sign
);
1920 else if (!strncmp (str
, "Inf", 3))
1926 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1928 /* Hexadecimal floating point. */
1929 int pos
= SIGNIFICAND_BITS
- 4, d
;
1937 d
= hex_value (*str
);
1942 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1943 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1947 /* Ensure correct rounding by setting last bit if there is
1948 a subsequent nonzero digit. */
1956 if (pos
== SIGNIFICAND_BITS
- 4)
1963 d
= hex_value (*str
);
1968 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1969 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1973 /* Ensure correct rounding by setting last bit if there is
1974 a subsequent nonzero digit. */
1980 /* If the mantissa is zero, ignore the exponent. */
1981 if (!cmp_significand_0 (r
))
1984 if (*str
== 'p' || *str
== 'P')
1986 bool exp_neg
= false;
1994 else if (*str
== '+')
1998 while (ISDIGIT (*str
))
2004 /* Overflowed the exponent. */
2019 SET_REAL_EXP (r
, exp
);
2025 /* Decimal floating point. */
2026 const char *cstr
= str
;
2030 while (*cstr
== '0')
2035 while (*cstr
== '0')
2039 /* If the mantissa is zero, ignore the exponent. */
2040 if (!ISDIGIT (*cstr
))
2043 /* Nonzero value, possibly overflowing or underflowing. */
2044 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2045 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, GMP_RNDZ
);
2046 /* The result should never be a NaN, and because the rounding is
2047 toward zero should never be an infinity. */
2048 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2049 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2054 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2061 real_from_mpfr (r
, m
, NULL_TREE
, GMP_RNDZ
);
2062 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2063 because the hex digits used in real_from_mpfr did not
2064 start with a digit 8 to f, but the exponent bounds above
2065 should have avoided underflow or overflow. */
2066 gcc_assert (r
->cl
= rvc_normal
);
2067 /* Set a sticky bit if mpfr_strtofr was inexact. */
2068 r
->sig
[0] |= inexact
;
2088 /* Legacy. Similar, but return the result directly. */
2091 real_from_string2 (const char *s
, enum machine_mode mode
)
2095 real_from_string (&r
, s
);
2096 if (mode
!= VOIDmode
)
2097 real_convert (&r
, mode
, &r
);
2102 /* Initialize R from string S and desired MODE. */
2105 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, enum machine_mode mode
)
2107 if (DECIMAL_FLOAT_MODE_P (mode
))
2108 decimal_real_from_string (r
, s
);
2110 real_from_string (r
, s
);
2112 if (mode
!= VOIDmode
)
2113 real_convert (r
, mode
, r
);
2116 /* Initialize R from the integer pair HIGH+LOW. */
2119 real_from_integer (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2120 unsigned HOST_WIDE_INT low
, HOST_WIDE_INT high
,
2123 if (low
== 0 && high
== 0)
2127 memset (r
, 0, sizeof (*r
));
2129 r
->sign
= high
< 0 && !unsigned_p
;
2130 SET_REAL_EXP (r
, HOST_BITS_PER_DOUBLE_INT
);
2141 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2143 r
->sig
[SIGSZ
-1] = high
;
2144 r
->sig
[SIGSZ
-2] = low
;
2148 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2149 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2150 r
->sig
[SIGSZ
-2] = high
;
2151 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2152 r
->sig
[SIGSZ
-4] = low
;
2158 if (DECIMAL_FLOAT_MODE_P (mode
))
2159 decimal_from_integer (r
);
2160 else if (mode
!= VOIDmode
)
2161 real_convert (r
, mode
, r
);
2164 /* Render R, an integral value, as a floating point constant with no
2165 specified exponent. */
2168 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2171 int dec_exp
, digit
, digits
;
2172 REAL_VALUE_TYPE r
, pten
;
2178 if (r
.cl
== rvc_zero
)
2187 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2188 digits
= dec_exp
+ 1;
2189 gcc_assert ((digits
+ 2) < (int)buf_size
);
2191 pten
= *real_digit (1);
2192 times_pten (&pten
, dec_exp
);
2198 digit
= rtd_divmod (&r
, &pten
);
2199 gcc_assert (digit
>= 0 && digit
<= 9);
2201 while (--digits
> 0)
2204 digit
= rtd_divmod (&r
, &pten
);
2211 /* Convert a real with an integral value to decimal float. */
2214 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2218 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2219 decimal_real_from_string (r
, str
);
2222 /* Returns 10**2**N. */
2224 static const REAL_VALUE_TYPE
*
2227 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2229 gcc_assert (n
>= 0);
2230 gcc_assert (n
< EXP_BITS
);
2232 if (tens
[n
].cl
== rvc_zero
)
2234 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2236 HOST_WIDE_INT t
= 10;
2239 for (i
= 0; i
< n
; ++i
)
2242 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2246 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2247 do_multiply (&tens
[n
], t
, t
);
2254 /* Returns 10**(-2**N). */
2256 static const REAL_VALUE_TYPE
*
2257 ten_to_mptwo (int n
)
2259 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2261 gcc_assert (n
>= 0);
2262 gcc_assert (n
< EXP_BITS
);
2264 if (tens
[n
].cl
== rvc_zero
)
2265 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2272 static const REAL_VALUE_TYPE
*
2275 static REAL_VALUE_TYPE num
[10];
2277 gcc_assert (n
>= 0);
2278 gcc_assert (n
<= 9);
2280 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2281 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2286 /* Multiply R by 10**EXP. */
2289 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2291 REAL_VALUE_TYPE pten
, *rr
;
2292 bool negative
= (exp
< 0);
2298 pten
= *real_digit (1);
2304 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2306 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2309 do_divide (r
, r
, &pten
);
2312 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2314 const REAL_VALUE_TYPE
*
2317 static REAL_VALUE_TYPE value
;
2319 /* Initialize mathematical constants for constant folding builtins.
2320 These constants need to be given to at least 160 bits precision. */
2321 if (value
.cl
== rvc_zero
)
2324 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2325 mpfr_set_ui (m
, 1, GMP_RNDN
);
2326 mpfr_exp (m
, m
, GMP_RNDN
);
2327 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2334 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2336 const REAL_VALUE_TYPE
*
2337 dconst_third_ptr (void)
2339 static REAL_VALUE_TYPE value
;
2341 /* Initialize mathematical constants for constant folding builtins.
2342 These constants need to be given to at least 160 bits precision. */
2343 if (value
.cl
== rvc_zero
)
2345 real_arithmetic (&value
, RDIV_EXPR
, &dconst1
, real_digit (3));
2350 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2352 const REAL_VALUE_TYPE
*
2353 dconst_sqrt2_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
)
2362 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2363 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2364 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2370 /* Fills R with +Inf. */
2373 real_inf (REAL_VALUE_TYPE
*r
)
2378 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2379 we force a QNaN, else we force an SNaN. The string, if not empty,
2380 is parsed as a number and placed in the significand. Return true
2381 if the string was successfully parsed. */
2384 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2385 enum machine_mode mode
)
2387 const struct real_format
*fmt
;
2389 fmt
= REAL_MODE_FORMAT (mode
);
2395 get_canonical_qnan (r
, 0);
2397 get_canonical_snan (r
, 0);
2403 memset (r
, 0, sizeof (*r
));
2406 /* Parse akin to strtol into the significand of R. */
2408 while (ISSPACE (*str
))
2412 else if (*str
== '+')
2417 if (*str
== 'x' || *str
== 'X')
2426 while ((d
= hex_value (*str
)) < base
)
2433 lshift_significand (r
, r
, 3);
2436 lshift_significand (r
, r
, 4);
2439 lshift_significand_1 (&u
, r
);
2440 lshift_significand (r
, r
, 3);
2441 add_significands (r
, r
, &u
);
2449 add_significands (r
, r
, &u
);
2454 /* Must have consumed the entire string for success. */
2458 /* Shift the significand into place such that the bits
2459 are in the most significant bits for the format. */
2460 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2462 /* Our MSB is always unset for NaNs. */
2463 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2465 /* Force quiet or signalling NaN. */
2466 r
->signalling
= !quiet
;
2472 /* Fills R with the largest finite value representable in mode MODE.
2473 If SIGN is nonzero, R is set to the most negative finite value. */
2476 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
2478 const struct real_format
*fmt
;
2481 fmt
= REAL_MODE_FORMAT (mode
);
2483 memset (r
, 0, sizeof (*r
));
2486 decimal_real_maxval (r
, sign
, mode
);
2491 SET_REAL_EXP (r
, fmt
->emax
);
2493 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2494 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2495 clear_significand_below (r
, np2
);
2497 if (fmt
->pnan
< fmt
->p
)
2498 /* This is an IBM extended double format made up of two IEEE
2499 doubles. The value of the long double is the sum of the
2500 values of the two parts. The most significant part is
2501 required to be the value of the long double rounded to the
2502 nearest double. Rounding means we need a slightly smaller
2503 value for LDBL_MAX. */
2504 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2508 /* Fills R with 2**N. */
2511 real_2expN (REAL_VALUE_TYPE
*r
, int n
, enum machine_mode fmode
)
2513 memset (r
, 0, sizeof (*r
));
2518 else if (n
< -MAX_EXP
)
2523 SET_REAL_EXP (r
, n
);
2524 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2526 if (DECIMAL_FLOAT_MODE_P (fmode
))
2527 decimal_real_convert (r
, fmode
, r
);
2532 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2536 bool round_up
= false;
2542 decimal_round_for_format (fmt
, r
);
2545 /* FIXME. We can come here via fp_easy_constant
2546 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2547 investigated whether this convert needs to be here, or
2548 something else is missing. */
2549 decimal_real_convert (r
, DFmode
, r
);
2553 emin2m1
= fmt
->emin
- 1;
2556 np2
= SIGNIFICAND_BITS
- p2
;
2560 get_zero (r
, r
->sign
);
2562 if (!fmt
->has_signed_zero
)
2567 get_inf (r
, r
->sign
);
2572 clear_significand_below (r
, np2
);
2582 /* Check the range of the exponent. If we're out of range,
2583 either underflow or overflow. */
2584 if (REAL_EXP (r
) > emax2
)
2586 else if (REAL_EXP (r
) <= emin2m1
)
2590 if (!fmt
->has_denorm
)
2592 /* Don't underflow completely until we've had a chance to round. */
2593 if (REAL_EXP (r
) < emin2m1
)
2598 diff
= emin2m1
- REAL_EXP (r
) + 1;
2602 /* De-normalize the significand. */
2603 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2604 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2608 if (!fmt
->round_towards_zero
)
2610 /* There are P2 true significand bits, followed by one guard bit,
2611 followed by one sticky bit, followed by stuff. Fold nonzero
2612 stuff into the sticky bit. */
2613 unsigned long sticky
;
2617 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2618 sticky
|= r
->sig
[i
];
2620 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2622 guard
= test_significand_bit (r
, np2
- 1);
2623 lsb
= test_significand_bit (r
, np2
);
2625 /* Round to even. */
2626 round_up
= guard
&& (sticky
|| lsb
);
2633 set_significand_bit (&u
, np2
);
2635 if (add_significands (r
, r
, &u
))
2637 /* Overflow. Means the significand had been all ones, and
2638 is now all zeros. Need to increase the exponent, and
2639 possibly re-normalize it. */
2640 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2641 if (REAL_EXP (r
) > emax2
)
2643 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2647 /* Catch underflow that we deferred until after rounding. */
2648 if (REAL_EXP (r
) <= emin2m1
)
2651 /* Clear out trailing garbage. */
2652 clear_significand_below (r
, np2
);
2655 /* Extend or truncate to a new mode. */
2658 real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2659 const REAL_VALUE_TYPE
*a
)
2661 const struct real_format
*fmt
;
2663 fmt
= REAL_MODE_FORMAT (mode
);
2668 if (a
->decimal
|| fmt
->b
== 10)
2669 decimal_real_convert (r
, mode
, a
);
2671 round_for_format (fmt
, r
);
2673 /* round_for_format de-normalizes denormals. Undo just that part. */
2674 if (r
->cl
== rvc_normal
)
2678 /* Legacy. Likewise, except return the struct directly. */
2681 real_value_truncate (enum machine_mode mode
, REAL_VALUE_TYPE a
)
2684 real_convert (&r
, mode
, &a
);
2688 /* Return true if truncating to MODE is exact. */
2691 exact_real_truncate (enum machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2693 const struct real_format
*fmt
;
2697 fmt
= REAL_MODE_FORMAT (mode
);
2700 /* Don't allow conversion to denormals. */
2701 emin2m1
= fmt
->emin
- 1;
2702 if (REAL_EXP (a
) <= emin2m1
)
2705 /* After conversion to the new mode, the value must be identical. */
2706 real_convert (&t
, mode
, a
);
2707 return real_identical (&t
, a
);
2710 /* Write R to the given target format. Place the words of the result
2711 in target word order in BUF. There are always 32 bits in each
2712 long, no matter the size of the host long.
2714 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2717 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2718 const struct real_format
*fmt
)
2724 round_for_format (fmt
, &r
);
2728 (*fmt
->encode
) (fmt
, buf
, &r
);
2733 /* Similar, but look up the format from MODE. */
2736 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, enum machine_mode mode
)
2738 const struct real_format
*fmt
;
2740 fmt
= REAL_MODE_FORMAT (mode
);
2743 return real_to_target_fmt (buf
, r
, fmt
);
2746 /* Read R from the given target format. Read the words of the result
2747 in target word order in BUF. There are always 32 bits in each
2748 long, no matter the size of the host long. */
2751 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2752 const struct real_format
*fmt
)
2754 (*fmt
->decode
) (fmt
, r
, buf
);
2757 /* Similar, but look up the format from MODE. */
2760 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, enum machine_mode mode
)
2762 const struct real_format
*fmt
;
2764 fmt
= REAL_MODE_FORMAT (mode
);
2767 (*fmt
->decode
) (fmt
, r
, buf
);
2770 /* Return the number of bits of the largest binary value that the
2771 significand of MODE will hold. */
2772 /* ??? Legacy. Should get access to real_format directly. */
2775 significand_size (enum machine_mode mode
)
2777 const struct real_format
*fmt
;
2779 fmt
= REAL_MODE_FORMAT (mode
);
2785 /* Return the size in bits of the largest binary value that can be
2786 held by the decimal coefficient for this mode. This is one more
2787 than the number of bits required to hold the largest coefficient
2789 double log2_10
= 3.3219281;
2790 return fmt
->p
* log2_10
;
2795 /* Return a hash value for the given real value. */
2796 /* ??? The "unsigned int" return value is intended to be hashval_t,
2797 but I didn't want to pull hashtab.h into real.h. */
2800 real_hash (const REAL_VALUE_TYPE
*r
)
2805 h
= r
->cl
| (r
->sign
<< 2);
2813 h
|= REAL_EXP (r
) << 3;
2818 h
^= (unsigned int)-1;
2827 if (sizeof (unsigned long) > sizeof (unsigned int))
2828 for (i
= 0; i
< SIGSZ
; ++i
)
2830 unsigned long s
= r
->sig
[i
];
2831 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2834 for (i
= 0; i
< SIGSZ
; ++i
)
2840 /* IEEE single-precision format. */
2842 static void encode_ieee_single (const struct real_format
*fmt
,
2843 long *, const REAL_VALUE_TYPE
*);
2844 static void decode_ieee_single (const struct real_format
*,
2845 REAL_VALUE_TYPE
*, const long *);
2848 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2849 const REAL_VALUE_TYPE
*r
)
2851 unsigned long image
, sig
, exp
;
2852 unsigned long sign
= r
->sign
;
2853 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2856 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2867 image
|= 0x7fffffff;
2874 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2875 if (r
->signalling
== fmt
->qnan_msb_set
)
2886 image
|= 0x7fffffff;
2890 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2891 whereas the intermediate representation is 0.F x 2**exp.
2892 Which means we're off by one. */
2896 exp
= REAL_EXP (r
) + 127 - 1;
2909 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2912 unsigned long image
= buf
[0] & 0xffffffff;
2913 bool sign
= (image
>> 31) & 1;
2914 int exp
= (image
>> 23) & 0xff;
2916 memset (r
, 0, sizeof (*r
));
2917 image
<<= HOST_BITS_PER_LONG
- 24;
2922 if (image
&& fmt
->has_denorm
)
2926 SET_REAL_EXP (r
, -126);
2927 r
->sig
[SIGSZ
-1] = image
<< 1;
2930 else if (fmt
->has_signed_zero
)
2933 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2939 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2940 ^ fmt
->qnan_msb_set
);
2941 r
->sig
[SIGSZ
-1] = image
;
2953 SET_REAL_EXP (r
, exp
- 127 + 1);
2954 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2958 const struct real_format ieee_single_format
=
2979 const struct real_format mips_single_format
=
3000 const struct real_format motorola_single_format
=
3021 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3022 single precision with the following differences:
3023 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3025 - NaNs are not supported.
3026 - The range of non-zero numbers in binary is
3027 (001)[1.]000...000 to (255)[1.]111...111.
3028 - Denormals can be represented, but are treated as +0.0 when
3029 used as an operand and are never generated as a result.
3030 - -0.0 can be represented, but a zero result is always +0.0.
3031 - the only supported rounding mode is trunction (towards zero). */
3032 const struct real_format spu_single_format
=
3053 /* IEEE double-precision format. */
3055 static void encode_ieee_double (const struct real_format
*fmt
,
3056 long *, const REAL_VALUE_TYPE
*);
3057 static void decode_ieee_double (const struct real_format
*,
3058 REAL_VALUE_TYPE
*, const long *);
3061 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3062 const REAL_VALUE_TYPE
*r
)
3064 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3065 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3067 image_hi
= r
->sign
<< 31;
3070 if (HOST_BITS_PER_LONG
== 64)
3072 sig_hi
= r
->sig
[SIGSZ
-1];
3073 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3074 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3078 sig_hi
= r
->sig
[SIGSZ
-1];
3079 sig_lo
= r
->sig
[SIGSZ
-2];
3080 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3081 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3091 image_hi
|= 2047 << 20;
3094 image_hi
|= 0x7fffffff;
3095 image_lo
= 0xffffffff;
3104 if (fmt
->canonical_nan_lsbs_set
)
3106 sig_hi
= (1 << 19) - 1;
3107 sig_lo
= 0xffffffff;
3115 if (r
->signalling
== fmt
->qnan_msb_set
)
3116 sig_hi
&= ~(1 << 19);
3119 if (sig_hi
== 0 && sig_lo
== 0)
3122 image_hi
|= 2047 << 20;
3128 image_hi
|= 0x7fffffff;
3129 image_lo
= 0xffffffff;
3134 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3135 whereas the intermediate representation is 0.F x 2**exp.
3136 Which means we're off by one. */
3140 exp
= REAL_EXP (r
) + 1023 - 1;
3141 image_hi
|= exp
<< 20;
3150 if (FLOAT_WORDS_BIG_ENDIAN
)
3151 buf
[0] = image_hi
, buf
[1] = image_lo
;
3153 buf
[0] = image_lo
, buf
[1] = image_hi
;
3157 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3160 unsigned long image_hi
, image_lo
;
3164 if (FLOAT_WORDS_BIG_ENDIAN
)
3165 image_hi
= buf
[0], image_lo
= buf
[1];
3167 image_lo
= buf
[0], image_hi
= buf
[1];
3168 image_lo
&= 0xffffffff;
3169 image_hi
&= 0xffffffff;
3171 sign
= (image_hi
>> 31) & 1;
3172 exp
= (image_hi
>> 20) & 0x7ff;
3174 memset (r
, 0, sizeof (*r
));
3176 image_hi
<<= 32 - 21;
3177 image_hi
|= image_lo
>> 21;
3178 image_hi
&= 0x7fffffff;
3179 image_lo
<<= 32 - 21;
3183 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3187 SET_REAL_EXP (r
, -1022);
3188 if (HOST_BITS_PER_LONG
== 32)
3190 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3192 r
->sig
[SIGSZ
-1] = image_hi
;
3193 r
->sig
[SIGSZ
-2] = image_lo
;
3197 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3198 r
->sig
[SIGSZ
-1] = image_hi
;
3202 else if (fmt
->has_signed_zero
)
3205 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3207 if (image_hi
|| image_lo
)
3211 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3212 if (HOST_BITS_PER_LONG
== 32)
3214 r
->sig
[SIGSZ
-1] = image_hi
;
3215 r
->sig
[SIGSZ
-2] = image_lo
;
3218 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3230 SET_REAL_EXP (r
, exp
- 1023 + 1);
3231 if (HOST_BITS_PER_LONG
== 32)
3233 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3234 r
->sig
[SIGSZ
-2] = image_lo
;
3237 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3241 const struct real_format ieee_double_format
=
3262 const struct real_format mips_double_format
=
3283 const struct real_format motorola_double_format
=
3304 /* IEEE extended real format. This comes in three flavors: Intel's as
3305 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3306 12- and 16-byte images may be big- or little endian; Motorola's is
3307 always big endian. */
3309 /* Helper subroutine which converts from the internal format to the
3310 12-byte little-endian Intel format. Functions below adjust this
3311 for the other possible formats. */
3313 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3314 const REAL_VALUE_TYPE
*r
)
3316 unsigned long image_hi
, sig_hi
, sig_lo
;
3317 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3319 image_hi
= r
->sign
<< 15;
3320 sig_hi
= sig_lo
= 0;
3332 /* Intel requires the explicit integer bit to be set, otherwise
3333 it considers the value a "pseudo-infinity". Motorola docs
3334 say it doesn't care. */
3335 sig_hi
= 0x80000000;
3340 sig_lo
= sig_hi
= 0xffffffff;
3350 if (fmt
->canonical_nan_lsbs_set
)
3352 sig_hi
= (1 << 30) - 1;
3353 sig_lo
= 0xffffffff;
3356 else if (HOST_BITS_PER_LONG
== 32)
3358 sig_hi
= r
->sig
[SIGSZ
-1];
3359 sig_lo
= r
->sig
[SIGSZ
-2];
3363 sig_lo
= r
->sig
[SIGSZ
-1];
3364 sig_hi
= sig_lo
>> 31 >> 1;
3365 sig_lo
&= 0xffffffff;
3367 if (r
->signalling
== fmt
->qnan_msb_set
)
3368 sig_hi
&= ~(1 << 30);
3371 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3374 /* Intel requires the explicit integer bit to be set, otherwise
3375 it considers the value a "pseudo-nan". Motorola docs say it
3377 sig_hi
|= 0x80000000;
3382 sig_lo
= sig_hi
= 0xffffffff;
3388 int exp
= REAL_EXP (r
);
3390 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3391 whereas the intermediate representation is 0.F x 2**exp.
3392 Which means we're off by one.
3394 Except for Motorola, which consider exp=0 and explicit
3395 integer bit set to continue to be normalized. In theory
3396 this discrepancy has been taken care of by the difference
3397 in fmt->emin in round_for_format. */
3404 gcc_assert (exp
>= 0);
3408 if (HOST_BITS_PER_LONG
== 32)
3410 sig_hi
= r
->sig
[SIGSZ
-1];
3411 sig_lo
= r
->sig
[SIGSZ
-2];
3415 sig_lo
= r
->sig
[SIGSZ
-1];
3416 sig_hi
= sig_lo
>> 31 >> 1;
3417 sig_lo
&= 0xffffffff;
3426 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3429 /* Convert from the internal format to the 12-byte Motorola format
3430 for an IEEE extended real. */
3432 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3433 const REAL_VALUE_TYPE
*r
)
3436 encode_ieee_extended (fmt
, intermed
, r
);
3438 /* Motorola chips are assumed always to be big-endian. Also, the
3439 padding in a Motorola extended real goes between the exponent and
3440 the mantissa. At this point the mantissa is entirely within
3441 elements 0 and 1 of intermed, and the exponent entirely within
3442 element 2, so all we have to do is swap the order around, and
3443 shift element 2 left 16 bits. */
3444 buf
[0] = intermed
[2] << 16;
3445 buf
[1] = intermed
[1];
3446 buf
[2] = intermed
[0];
3449 /* Convert from the internal format to the 12-byte Intel format for
3450 an IEEE extended real. */
3452 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3453 const REAL_VALUE_TYPE
*r
)
3455 if (FLOAT_WORDS_BIG_ENDIAN
)
3457 /* All the padding in an Intel-format extended real goes at the high
3458 end, which in this case is after the mantissa, not the exponent.
3459 Therefore we must shift everything down 16 bits. */
3461 encode_ieee_extended (fmt
, intermed
, r
);
3462 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3463 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3464 buf
[2] = (intermed
[0] << 16);
3467 /* encode_ieee_extended produces what we want directly. */
3468 encode_ieee_extended (fmt
, buf
, r
);
3471 /* Convert from the internal format to the 16-byte Intel format for
3472 an IEEE extended real. */
3474 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3475 const REAL_VALUE_TYPE
*r
)
3477 /* All the padding in an Intel-format extended real goes at the high end. */
3478 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3482 /* As above, we have a helper function which converts from 12-byte
3483 little-endian Intel format to internal format. Functions below
3484 adjust for the other possible formats. */
3486 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3489 unsigned long image_hi
, sig_hi
, sig_lo
;
3493 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3494 sig_lo
&= 0xffffffff;
3495 sig_hi
&= 0xffffffff;
3496 image_hi
&= 0xffffffff;
3498 sign
= (image_hi
>> 15) & 1;
3499 exp
= image_hi
& 0x7fff;
3501 memset (r
, 0, sizeof (*r
));
3505 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3510 /* When the IEEE format contains a hidden bit, we know that
3511 it's zero at this point, and so shift up the significand
3512 and decrease the exponent to match. In this case, Motorola
3513 defines the explicit integer bit to be valid, so we don't
3514 know whether the msb is set or not. */
3515 SET_REAL_EXP (r
, fmt
->emin
);
3516 if (HOST_BITS_PER_LONG
== 32)
3518 r
->sig
[SIGSZ
-1] = sig_hi
;
3519 r
->sig
[SIGSZ
-2] = sig_lo
;
3522 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3526 else if (fmt
->has_signed_zero
)
3529 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3531 /* See above re "pseudo-infinities" and "pseudo-nans".
3532 Short summary is that the MSB will likely always be
3533 set, and that we don't care about it. */
3534 sig_hi
&= 0x7fffffff;
3536 if (sig_hi
|| sig_lo
)
3540 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3541 if (HOST_BITS_PER_LONG
== 32)
3543 r
->sig
[SIGSZ
-1] = sig_hi
;
3544 r
->sig
[SIGSZ
-2] = sig_lo
;
3547 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3559 SET_REAL_EXP (r
, exp
- 16383 + 1);
3560 if (HOST_BITS_PER_LONG
== 32)
3562 r
->sig
[SIGSZ
-1] = sig_hi
;
3563 r
->sig
[SIGSZ
-2] = sig_lo
;
3566 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3570 /* Convert from the internal format to the 12-byte Motorola format
3571 for an IEEE extended real. */
3573 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3578 /* Motorola chips are assumed always to be big-endian. Also, the
3579 padding in a Motorola extended real goes between the exponent and
3580 the mantissa; remove it. */
3581 intermed
[0] = buf
[2];
3582 intermed
[1] = buf
[1];
3583 intermed
[2] = (unsigned long)buf
[0] >> 16;
3585 decode_ieee_extended (fmt
, r
, intermed
);
3588 /* Convert from the internal format to the 12-byte Intel format for
3589 an IEEE extended real. */
3591 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3594 if (FLOAT_WORDS_BIG_ENDIAN
)
3596 /* All the padding in an Intel-format extended real goes at the high
3597 end, which in this case is after the mantissa, not the exponent.
3598 Therefore we must shift everything up 16 bits. */
3601 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3602 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3603 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3605 decode_ieee_extended (fmt
, r
, intermed
);
3608 /* decode_ieee_extended produces what we want directly. */
3609 decode_ieee_extended (fmt
, r
, buf
);
3612 /* Convert from the internal format to the 16-byte Intel format for
3613 an IEEE extended real. */
3615 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3618 /* All the padding in an Intel-format extended real goes at the high end. */
3619 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3622 const struct real_format ieee_extended_motorola_format
=
3624 encode_ieee_extended_motorola
,
3625 decode_ieee_extended_motorola
,
3643 const struct real_format ieee_extended_intel_96_format
=
3645 encode_ieee_extended_intel_96
,
3646 decode_ieee_extended_intel_96
,
3664 const struct real_format ieee_extended_intel_128_format
=
3666 encode_ieee_extended_intel_128
,
3667 decode_ieee_extended_intel_128
,
3685 /* The following caters to i386 systems that set the rounding precision
3686 to 53 bits instead of 64, e.g. FreeBSD. */
3687 const struct real_format ieee_extended_intel_96_round_53_format
=
3689 encode_ieee_extended_intel_96
,
3690 decode_ieee_extended_intel_96
,
3708 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3709 numbers whose sum is equal to the extended precision value. The number
3710 with greater magnitude is first. This format has the same magnitude
3711 range as an IEEE double precision value, but effectively 106 bits of
3712 significand precision. Infinity and NaN are represented by their IEEE
3713 double precision value stored in the first number, the second number is
3714 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3716 static void encode_ibm_extended (const struct real_format
*fmt
,
3717 long *, const REAL_VALUE_TYPE
*);
3718 static void decode_ibm_extended (const struct real_format
*,
3719 REAL_VALUE_TYPE
*, const long *);
3722 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3723 const REAL_VALUE_TYPE
*r
)
3725 REAL_VALUE_TYPE u
, normr
, v
;
3726 const struct real_format
*base_fmt
;
3728 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3730 /* Renormalize R before doing any arithmetic on it. */
3732 if (normr
.cl
== rvc_normal
)
3735 /* u = IEEE double precision portion of significand. */
3737 round_for_format (base_fmt
, &u
);
3738 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3740 if (u
.cl
== rvc_normal
)
3742 do_add (&v
, &normr
, &u
, 1);
3743 /* Call round_for_format since we might need to denormalize. */
3744 round_for_format (base_fmt
, &v
);
3745 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3749 /* Inf, NaN, 0 are all representable as doubles, so the
3750 least-significant part can be 0.0. */
3757 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3760 REAL_VALUE_TYPE u
, v
;
3761 const struct real_format
*base_fmt
;
3763 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3764 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3766 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3768 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3769 do_add (r
, &u
, &v
, 0);
3775 const struct real_format ibm_extended_format
=
3777 encode_ibm_extended
,
3778 decode_ibm_extended
,
3796 const struct real_format mips_extended_format
=
3798 encode_ibm_extended
,
3799 decode_ibm_extended
,
3818 /* IEEE quad precision format. */
3820 static void encode_ieee_quad (const struct real_format
*fmt
,
3821 long *, const REAL_VALUE_TYPE
*);
3822 static void decode_ieee_quad (const struct real_format
*,
3823 REAL_VALUE_TYPE
*, const long *);
3826 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3827 const REAL_VALUE_TYPE
*r
)
3829 unsigned long image3
, image2
, image1
, image0
, exp
;
3830 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3833 image3
= r
->sign
<< 31;
3838 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3847 image3
|= 32767 << 16;
3850 image3
|= 0x7fffffff;
3851 image2
= 0xffffffff;
3852 image1
= 0xffffffff;
3853 image0
= 0xffffffff;
3860 image3
|= 32767 << 16;
3864 if (fmt
->canonical_nan_lsbs_set
)
3867 image2
= image1
= image0
= 0xffffffff;
3870 else if (HOST_BITS_PER_LONG
== 32)
3875 image3
|= u
.sig
[3] & 0xffff;
3880 image1
= image0
>> 31 >> 1;
3882 image3
|= (image2
>> 31 >> 1) & 0xffff;
3883 image0
&= 0xffffffff;
3884 image2
&= 0xffffffff;
3886 if (r
->signalling
== fmt
->qnan_msb_set
)
3890 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3895 image3
|= 0x7fffffff;
3896 image2
= 0xffffffff;
3897 image1
= 0xffffffff;
3898 image0
= 0xffffffff;
3903 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3904 whereas the intermediate representation is 0.F x 2**exp.
3905 Which means we're off by one. */
3909 exp
= REAL_EXP (r
) + 16383 - 1;
3910 image3
|= exp
<< 16;
3912 if (HOST_BITS_PER_LONG
== 32)
3917 image3
|= u
.sig
[3] & 0xffff;
3922 image1
= image0
>> 31 >> 1;
3924 image3
|= (image2
>> 31 >> 1) & 0xffff;
3925 image0
&= 0xffffffff;
3926 image2
&= 0xffffffff;
3934 if (FLOAT_WORDS_BIG_ENDIAN
)
3951 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3954 unsigned long image3
, image2
, image1
, image0
;
3958 if (FLOAT_WORDS_BIG_ENDIAN
)
3972 image0
&= 0xffffffff;
3973 image1
&= 0xffffffff;
3974 image2
&= 0xffffffff;
3976 sign
= (image3
>> 31) & 1;
3977 exp
= (image3
>> 16) & 0x7fff;
3980 memset (r
, 0, sizeof (*r
));
3984 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
3989 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
3990 if (HOST_BITS_PER_LONG
== 32)
3999 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4000 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4005 else if (fmt
->has_signed_zero
)
4008 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4010 if (image3
| image2
| image1
| image0
)
4014 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4016 if (HOST_BITS_PER_LONG
== 32)
4025 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4026 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4028 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4040 SET_REAL_EXP (r
, exp
- 16383 + 1);
4042 if (HOST_BITS_PER_LONG
== 32)
4051 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4052 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4054 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4055 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4059 const struct real_format ieee_quad_format
=
4080 const struct real_format mips_quad_format
=
4101 /* Descriptions of VAX floating point formats can be found beginning at
4103 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4105 The thing to remember is that they're almost IEEE, except for word
4106 order, exponent bias, and the lack of infinities, nans, and denormals.
4108 We don't implement the H_floating format here, simply because neither
4109 the VAX or Alpha ports use it. */
4111 static void encode_vax_f (const struct real_format
*fmt
,
4112 long *, const REAL_VALUE_TYPE
*);
4113 static void decode_vax_f (const struct real_format
*,
4114 REAL_VALUE_TYPE
*, const long *);
4115 static void encode_vax_d (const struct real_format
*fmt
,
4116 long *, const REAL_VALUE_TYPE
*);
4117 static void decode_vax_d (const struct real_format
*,
4118 REAL_VALUE_TYPE
*, const long *);
4119 static void encode_vax_g (const struct real_format
*fmt
,
4120 long *, const REAL_VALUE_TYPE
*);
4121 static void decode_vax_g (const struct real_format
*,
4122 REAL_VALUE_TYPE
*, const long *);
4125 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4126 const REAL_VALUE_TYPE
*r
)
4128 unsigned long sign
, exp
, sig
, image
;
4130 sign
= r
->sign
<< 15;
4140 image
= 0xffff7fff | sign
;
4144 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4145 exp
= REAL_EXP (r
) + 128;
4147 image
= (sig
<< 16) & 0xffff0000;
4161 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4162 REAL_VALUE_TYPE
*r
, const long *buf
)
4164 unsigned long image
= buf
[0] & 0xffffffff;
4165 int exp
= (image
>> 7) & 0xff;
4167 memset (r
, 0, sizeof (*r
));
4172 r
->sign
= (image
>> 15) & 1;
4173 SET_REAL_EXP (r
, exp
- 128);
4175 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4176 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4181 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4182 const REAL_VALUE_TYPE
*r
)
4184 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4189 image0
= image1
= 0;
4194 image0
= 0xffff7fff | sign
;
4195 image1
= 0xffffffff;
4199 /* Extract the significand into straight hi:lo. */
4200 if (HOST_BITS_PER_LONG
== 64)
4202 image0
= r
->sig
[SIGSZ
-1];
4203 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4204 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4208 image0
= r
->sig
[SIGSZ
-1];
4209 image1
= r
->sig
[SIGSZ
-2];
4210 image1
= (image0
<< 24) | (image1
>> 8);
4211 image0
= (image0
>> 8) & 0xffffff;
4214 /* Rearrange the half-words of the significand to match the
4216 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4217 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4219 /* Add the sign and exponent. */
4221 image0
|= (REAL_EXP (r
) + 128) << 7;
4228 if (FLOAT_WORDS_BIG_ENDIAN
)
4229 buf
[0] = image1
, buf
[1] = image0
;
4231 buf
[0] = image0
, buf
[1] = image1
;
4235 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4236 REAL_VALUE_TYPE
*r
, const long *buf
)
4238 unsigned long image0
, image1
;
4241 if (FLOAT_WORDS_BIG_ENDIAN
)
4242 image1
= buf
[0], image0
= buf
[1];
4244 image0
= buf
[0], image1
= buf
[1];
4245 image0
&= 0xffffffff;
4246 image1
&= 0xffffffff;
4248 exp
= (image0
>> 7) & 0xff;
4250 memset (r
, 0, sizeof (*r
));
4255 r
->sign
= (image0
>> 15) & 1;
4256 SET_REAL_EXP (r
, exp
- 128);
4258 /* Rearrange the half-words of the external format into
4259 proper ascending order. */
4260 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4261 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4263 if (HOST_BITS_PER_LONG
== 64)
4265 image0
= (image0
<< 31 << 1) | image1
;
4268 r
->sig
[SIGSZ
-1] = image0
;
4272 r
->sig
[SIGSZ
-1] = image0
;
4273 r
->sig
[SIGSZ
-2] = image1
;
4274 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4275 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4281 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4282 const REAL_VALUE_TYPE
*r
)
4284 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4289 image0
= image1
= 0;
4294 image0
= 0xffff7fff | sign
;
4295 image1
= 0xffffffff;
4299 /* Extract the significand into straight hi:lo. */
4300 if (HOST_BITS_PER_LONG
== 64)
4302 image0
= r
->sig
[SIGSZ
-1];
4303 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4304 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4308 image0
= r
->sig
[SIGSZ
-1];
4309 image1
= r
->sig
[SIGSZ
-2];
4310 image1
= (image0
<< 21) | (image1
>> 11);
4311 image0
= (image0
>> 11) & 0xfffff;
4314 /* Rearrange the half-words of the significand to match the
4316 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4317 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4319 /* Add the sign and exponent. */
4321 image0
|= (REAL_EXP (r
) + 1024) << 4;
4328 if (FLOAT_WORDS_BIG_ENDIAN
)
4329 buf
[0] = image1
, buf
[1] = image0
;
4331 buf
[0] = image0
, buf
[1] = image1
;
4335 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4336 REAL_VALUE_TYPE
*r
, const long *buf
)
4338 unsigned long image0
, image1
;
4341 if (FLOAT_WORDS_BIG_ENDIAN
)
4342 image1
= buf
[0], image0
= buf
[1];
4344 image0
= buf
[0], image1
= buf
[1];
4345 image0
&= 0xffffffff;
4346 image1
&= 0xffffffff;
4348 exp
= (image0
>> 4) & 0x7ff;
4350 memset (r
, 0, sizeof (*r
));
4355 r
->sign
= (image0
>> 15) & 1;
4356 SET_REAL_EXP (r
, exp
- 1024);
4358 /* Rearrange the half-words of the external format into
4359 proper ascending order. */
4360 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4361 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4363 if (HOST_BITS_PER_LONG
== 64)
4365 image0
= (image0
<< 31 << 1) | image1
;
4368 r
->sig
[SIGSZ
-1] = image0
;
4372 r
->sig
[SIGSZ
-1] = image0
;
4373 r
->sig
[SIGSZ
-2] = image1
;
4374 lshift_significand (r
, r
, 64 - 53);
4375 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4380 const struct real_format vax_f_format
=
4401 const struct real_format vax_d_format
=
4422 const struct real_format vax_g_format
=
4443 /* Encode real R into a single precision DFP value in BUF. */
4445 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4446 long *buf ATTRIBUTE_UNUSED
,
4447 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4449 encode_decimal32 (fmt
, buf
, r
);
4452 /* Decode a single precision DFP value in BUF into a real R. */
4454 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4455 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4456 const long *buf ATTRIBUTE_UNUSED
)
4458 decode_decimal32 (fmt
, r
, buf
);
4461 /* Encode real R into a double precision DFP value in BUF. */
4463 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4464 long *buf ATTRIBUTE_UNUSED
,
4465 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4467 encode_decimal64 (fmt
, buf
, r
);
4470 /* Decode a double precision DFP value in BUF into a real R. */
4472 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4473 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4474 const long *buf ATTRIBUTE_UNUSED
)
4476 decode_decimal64 (fmt
, r
, buf
);
4479 /* Encode real R into a quad precision DFP value in BUF. */
4481 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4482 long *buf ATTRIBUTE_UNUSED
,
4483 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4485 encode_decimal128 (fmt
, buf
, r
);
4488 /* Decode a quad precision DFP value in BUF into a real R. */
4490 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4491 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4492 const long *buf ATTRIBUTE_UNUSED
)
4494 decode_decimal128 (fmt
, r
, buf
);
4497 /* Single precision decimal floating point (IEEE 754). */
4498 const struct real_format decimal_single_format
=
4500 encode_decimal_single
,
4501 decode_decimal_single
,
4519 /* Double precision decimal floating point (IEEE 754). */
4520 const struct real_format decimal_double_format
=
4522 encode_decimal_double
,
4523 decode_decimal_double
,
4541 /* Quad precision decimal floating point (IEEE 754). */
4542 const struct real_format decimal_quad_format
=
4544 encode_decimal_quad
,
4545 decode_decimal_quad
,
4563 /* Encode half-precision floats. This routine is used both for the IEEE
4564 ARM alternative encodings. */
4566 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4567 const REAL_VALUE_TYPE
*r
)
4569 unsigned long image
, sig
, exp
;
4570 unsigned long sign
= r
->sign
;
4571 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4574 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4592 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4593 if (r
->signalling
== fmt
->qnan_msb_set
)
4608 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4609 whereas the intermediate representation is 0.F x 2**exp.
4610 Which means we're off by one. */
4614 exp
= REAL_EXP (r
) + 15 - 1;
4626 /* Decode half-precision floats. This routine is used both for the IEEE
4627 ARM alternative encodings. */
4629 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4632 unsigned long image
= buf
[0] & 0xffff;
4633 bool sign
= (image
>> 15) & 1;
4634 int exp
= (image
>> 10) & 0x1f;
4636 memset (r
, 0, sizeof (*r
));
4637 image
<<= HOST_BITS_PER_LONG
- 11;
4642 if (image
&& fmt
->has_denorm
)
4646 SET_REAL_EXP (r
, -14);
4647 r
->sig
[SIGSZ
-1] = image
<< 1;
4650 else if (fmt
->has_signed_zero
)
4653 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4659 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4660 ^ fmt
->qnan_msb_set
);
4661 r
->sig
[SIGSZ
-1] = image
;
4673 SET_REAL_EXP (r
, exp
- 15 + 1);
4674 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4678 /* Half-precision format, as specified in IEEE 754R. */
4679 const struct real_format ieee_half_format
=
4700 /* ARM's alternative half-precision format, similar to IEEE but with
4701 no reserved exponent value for NaNs and infinities; rather, it just
4702 extends the range of exponents by one. */
4703 const struct real_format arm_half_format
=
4724 /* A synthetic "format" for internal arithmetic. It's the size of the
4725 internal significand minus the two bits needed for proper rounding.
4726 The encode and decode routines exist only to satisfy our paranoia
4729 static void encode_internal (const struct real_format
*fmt
,
4730 long *, const REAL_VALUE_TYPE
*);
4731 static void decode_internal (const struct real_format
*,
4732 REAL_VALUE_TYPE
*, const long *);
4735 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4736 const REAL_VALUE_TYPE
*r
)
4738 memcpy (buf
, r
, sizeof (*r
));
4742 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4743 REAL_VALUE_TYPE
*r
, const long *buf
)
4745 memcpy (r
, buf
, sizeof (*r
));
4748 const struct real_format real_internal_format
=
4753 SIGNIFICAND_BITS
- 2,
4754 SIGNIFICAND_BITS
- 2,
4769 /* Calculate X raised to the integer exponent N in mode MODE and store
4770 the result in R. Return true if the result may be inexact due to
4771 loss of precision. The algorithm is the classic "left-to-right binary
4772 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4773 Algorithms", "The Art of Computer Programming", Volume 2. */
4776 real_powi (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4777 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4779 unsigned HOST_WIDE_INT bit
;
4781 bool inexact
= false;
4793 /* Don't worry about overflow, from now on n is unsigned. */
4801 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4802 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4806 inexact
|= do_multiply (&t
, &t
, &t
);
4808 inexact
|= do_multiply (&t
, &t
, x
);
4816 inexact
|= do_divide (&t
, &dconst1
, &t
);
4818 real_convert (r
, mode
, &t
);
4822 /* Round X to the nearest integer not larger in absolute value, i.e.
4823 towards zero, placing the result in R in mode MODE. */
4826 real_trunc (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4827 const REAL_VALUE_TYPE
*x
)
4829 do_fix_trunc (r
, x
);
4830 if (mode
!= VOIDmode
)
4831 real_convert (r
, mode
, r
);
4834 /* Round X to the largest integer not greater in value, i.e. round
4835 down, placing the result in R in mode MODE. */
4838 real_floor (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4839 const REAL_VALUE_TYPE
*x
)
4843 do_fix_trunc (&t
, x
);
4844 if (! real_identical (&t
, x
) && x
->sign
)
4845 do_add (&t
, &t
, &dconstm1
, 0);
4846 if (mode
!= VOIDmode
)
4847 real_convert (r
, mode
, &t
);
4852 /* Round X to the smallest integer not less then argument, i.e. round
4853 up, placing the result in R in mode MODE. */
4856 real_ceil (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4857 const REAL_VALUE_TYPE
*x
)
4861 do_fix_trunc (&t
, x
);
4862 if (! real_identical (&t
, x
) && ! x
->sign
)
4863 do_add (&t
, &t
, &dconst1
, 0);
4864 if (mode
!= VOIDmode
)
4865 real_convert (r
, mode
, &t
);
4870 /* Round X to the nearest integer, but round halfway cases away from
4874 real_round (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4875 const REAL_VALUE_TYPE
*x
)
4877 do_add (r
, x
, &dconsthalf
, x
->sign
);
4878 do_fix_trunc (r
, r
);
4879 if (mode
!= VOIDmode
)
4880 real_convert (r
, mode
, r
);
4883 /* Set the sign of R to the sign of X. */
4886 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4891 /* Check whether the real constant value given is an integer. */
4894 real_isinteger (const REAL_VALUE_TYPE
*c
, enum machine_mode mode
)
4896 REAL_VALUE_TYPE cint
;
4898 real_trunc (&cint
, mode
, c
);
4899 return real_identical (c
, &cint
);
4902 /* Write into BUF the maximum representable finite floating-point
4903 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4904 float string. LEN is the size of BUF, and the buffer must be large
4905 enough to contain the resulting string. */
4908 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
4913 strcpy (buf
, "0x0.");
4915 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
4918 *p
++ = "08ce"[n
- i
];
4919 sprintf (p
, "p%d", fmt
->emax
);
4920 if (fmt
->pnan
< fmt
->p
)
4922 /* This is an IBM extended double format made up of two IEEE
4923 doubles. The value of the long double is the sum of the
4924 values of the two parts. The most significant part is
4925 required to be the value of the long double rounded to the
4926 nearest double. Rounding means we need a slightly smaller
4927 value for LDBL_MAX. */
4928 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
4931 gcc_assert (strlen (buf
) < len
);