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 HOST_WIDE_INT low
, high
;
1393 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1406 decimal_real_to_integer2 (plow
, phigh
, r
);
1413 /* Only force overflow for unsigned overflow. Signed overflow is
1414 undefined, so it doesn't matter what we return, and some callers
1415 expect to be able to use this routine for both signed and
1416 unsigned conversions. */
1417 if (exp
> HOST_BITS_PER_DOUBLE_INT
)
1420 rshift_significand (&t
, r
, HOST_BITS_PER_DOUBLE_INT
- exp
);
1421 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1423 high
= t
.sig
[SIGSZ
-1];
1424 low
= t
.sig
[SIGSZ
-2];
1428 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
);
1429 high
= t
.sig
[SIGSZ
-1];
1430 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1431 high
|= t
.sig
[SIGSZ
-2];
1433 low
= t
.sig
[SIGSZ
-3];
1434 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1435 low
|= t
.sig
[SIGSZ
-4];
1443 low
= -low
, high
= ~high
;
1455 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1456 of NUM / DEN. Return the quotient and place the remainder in NUM.
1457 It is expected that NUM / DEN are close enough that the quotient is
1460 static unsigned long
1461 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1463 unsigned long q
, msb
;
1464 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1473 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1475 lshift_significand_1 (num
, num
);
1477 if (msb
|| cmp_significands (num
, den
) >= 0)
1479 sub_significands (num
, num
, den
, 0);
1483 while (--expn
>= expd
);
1485 SET_REAL_EXP (num
, expd
);
1491 /* Render R as a decimal floating point constant. Emit DIGITS significant
1492 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1493 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1494 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1495 to a string that, when parsed back in mode MODE, yields the same value. */
1497 #define M_LOG10_2 0.30102999566398119521
1500 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1501 size_t buf_size
, size_t digits
,
1502 int crop_trailing_zeros
, enum machine_mode mode
)
1504 const struct real_format
*fmt
= NULL
;
1505 const REAL_VALUE_TYPE
*one
, *ten
;
1506 REAL_VALUE_TYPE r
, pten
, u
, v
;
1507 int dec_exp
, cmp_one
, digit
;
1509 char *p
, *first
, *last
;
1513 if (mode
!= VOIDmode
)
1515 fmt
= REAL_MODE_FORMAT (mode
);
1523 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1528 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1531 /* ??? Print the significand as well, if not canonical? */
1532 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1533 (r_orig
->signalling
? 'S' : 'Q'));
1541 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1545 /* Bound the number of digits printed by the size of the representation. */
1546 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1547 if (digits
== 0 || digits
> max_digits
)
1548 digits
= max_digits
;
1550 /* Estimate the decimal exponent, and compute the length of the string it
1551 will print as. Be conservative and add one to account for possible
1552 overflow or rounding error. */
1553 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1554 for (max_digits
= 1; dec_exp
; max_digits
++)
1557 /* Bound the number of digits printed by the size of the output buffer. */
1558 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1559 gcc_assert (max_digits
<= buf_size
);
1560 if (digits
> max_digits
)
1561 digits
= max_digits
;
1563 one
= real_digit (1);
1564 ten
= ten_to_ptwo (0);
1572 cmp_one
= do_compare (&r
, one
, 0);
1577 /* Number is greater than one. Convert significand to an integer
1578 and strip trailing decimal zeros. */
1581 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1583 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1584 m
= floor_log2 (max_digits
);
1586 /* Iterate over the bits of the possible powers of 10 that might
1587 be present in U and eliminate them. That is, if we find that
1588 10**2**M divides U evenly, keep the division and increase
1594 do_divide (&t
, &u
, ten_to_ptwo (m
));
1595 do_fix_trunc (&v
, &t
);
1596 if (cmp_significands (&v
, &t
) == 0)
1604 /* Revert the scaling to integer that we performed earlier. */
1605 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1606 - (SIGNIFICAND_BITS
- 1));
1609 /* Find power of 10. Do this by dividing out 10**2**M when
1610 this is larger than the current remainder. Fill PTEN with
1611 the power of 10 that we compute. */
1612 if (REAL_EXP (&r
) > 0)
1614 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1617 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1618 if (do_compare (&u
, ptentwo
, 0) >= 0)
1620 do_divide (&u
, &u
, ptentwo
);
1621 do_multiply (&pten
, &pten
, ptentwo
);
1628 /* We managed to divide off enough tens in the above reduction
1629 loop that we've now got a negative exponent. Fall into the
1630 less-than-one code to compute the proper value for PTEN. */
1637 /* Number is less than one. Pad significand with leading
1643 /* Stop if we'd shift bits off the bottom. */
1647 do_multiply (&u
, &v
, ten
);
1649 /* Stop if we're now >= 1. */
1650 if (REAL_EXP (&u
) > 0)
1658 /* Find power of 10. Do this by multiplying in P=10**2**M when
1659 the current remainder is smaller than 1/P. Fill PTEN with the
1660 power of 10 that we compute. */
1661 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1664 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1665 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1667 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1669 do_multiply (&v
, &v
, ptentwo
);
1670 do_multiply (&pten
, &pten
, ptentwo
);
1676 /* Invert the positive power of 10 that we've collected so far. */
1677 do_divide (&pten
, one
, &pten
);
1685 /* At this point, PTEN should contain the nearest power of 10 smaller
1686 than R, such that this division produces the first digit.
1688 Using a divide-step primitive that returns the complete integral
1689 remainder avoids the rounding error that would be produced if
1690 we were to use do_divide here and then simply multiply by 10 for
1691 each subsequent digit. */
1693 digit
= rtd_divmod (&r
, &pten
);
1695 /* Be prepared for error in that division via underflow ... */
1696 if (digit
== 0 && cmp_significand_0 (&r
))
1698 /* Multiply by 10 and try again. */
1699 do_multiply (&r
, &r
, ten
);
1700 digit
= rtd_divmod (&r
, &pten
);
1702 gcc_assert (digit
!= 0);
1705 /* ... or overflow. */
1715 gcc_assert (digit
<= 10);
1719 /* Generate subsequent digits. */
1720 while (--digits
> 0)
1722 do_multiply (&r
, &r
, ten
);
1723 digit
= rtd_divmod (&r
, &pten
);
1728 /* Generate one more digit with which to do rounding. */
1729 do_multiply (&r
, &r
, ten
);
1730 digit
= rtd_divmod (&r
, &pten
);
1732 /* Round the result. */
1733 if (fmt
&& fmt
->round_towards_zero
)
1735 /* If the format uses round towards zero when parsing the string
1736 back in, we need to always round away from zero here. */
1737 if (cmp_significand_0 (&r
))
1739 round_up
= digit
> 0;
1745 /* Round to nearest. If R is nonzero there are additional
1746 nonzero digits to be extracted. */
1747 if (cmp_significand_0 (&r
))
1749 /* Round to even. */
1750 else if ((p
[-1] - '0') & 1)
1754 round_up
= digit
> 5;
1771 /* Carry out of the first digit. This means we had all 9's and
1772 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1780 /* Insert the decimal point. */
1781 first
[0] = first
[1];
1784 /* If requested, drop trailing zeros. Never crop past "1.0". */
1785 if (crop_trailing_zeros
)
1786 while (last
> first
+ 3 && last
[-1] == '0')
1789 /* Append the exponent. */
1790 sprintf (last
, "e%+d", dec_exp
);
1792 #ifdef ENABLE_CHECKING
1793 /* Verify that we can read the original value back in. */
1794 if (mode
!= VOIDmode
)
1796 real_from_string (&r
, str
);
1797 real_convert (&r
, mode
, &r
);
1798 gcc_assert (real_identical (&r
, r_orig
));
1803 /* Likewise, except always uses round-to-nearest. */
1806 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1807 size_t digits
, int crop_trailing_zeros
)
1809 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1810 digits
, crop_trailing_zeros
, VOIDmode
);
1813 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1814 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1815 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1816 strip trailing zeros. */
1819 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1820 size_t digits
, int crop_trailing_zeros
)
1822 int i
, j
, exp
= REAL_EXP (r
);
1835 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1838 /* ??? Print the significand as well, if not canonical? */
1839 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1840 (r
->signalling
? 'S' : 'Q'));
1848 /* Hexadecimal format for decimal floats is not interesting. */
1849 strcpy (str
, "N/A");
1854 digits
= SIGNIFICAND_BITS
/ 4;
1856 /* Bound the number of digits printed by the size of the output buffer. */
1858 sprintf (exp_buf
, "p%+d", exp
);
1859 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1860 gcc_assert (max_digits
<= buf_size
);
1861 if (digits
> max_digits
)
1862 digits
= max_digits
;
1873 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1874 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1876 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1882 if (crop_trailing_zeros
)
1883 while (p
> first
+ 1 && p
[-1] == '0')
1886 sprintf (p
, "p%+d", exp
);
1889 /* Initialize R from a decimal or hexadecimal string. The string is
1890 assumed to have been syntax checked already. Return -1 if the
1891 value underflows, +1 if overflows, and 0 otherwise. */
1894 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1906 else if (*str
== '+')
1909 if (!strncmp (str
, "QNaN", 4))
1911 get_canonical_qnan (r
, sign
);
1914 else if (!strncmp (str
, "SNaN", 4))
1916 get_canonical_snan (r
, sign
);
1919 else if (!strncmp (str
, "Inf", 3))
1925 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1927 /* Hexadecimal floating point. */
1928 int pos
= SIGNIFICAND_BITS
- 4, d
;
1936 d
= hex_value (*str
);
1941 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1942 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1946 /* Ensure correct rounding by setting last bit if there is
1947 a subsequent nonzero digit. */
1955 if (pos
== SIGNIFICAND_BITS
- 4)
1962 d
= hex_value (*str
);
1967 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1968 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1972 /* Ensure correct rounding by setting last bit if there is
1973 a subsequent nonzero digit. */
1979 /* If the mantissa is zero, ignore the exponent. */
1980 if (!cmp_significand_0 (r
))
1983 if (*str
== 'p' || *str
== 'P')
1985 bool exp_neg
= false;
1993 else if (*str
== '+')
1997 while (ISDIGIT (*str
))
2003 /* Overflowed the exponent. */
2018 SET_REAL_EXP (r
, exp
);
2024 /* Decimal floating point. */
2025 const char *cstr
= str
;
2029 while (*cstr
== '0')
2034 while (*cstr
== '0')
2038 /* If the mantissa is zero, ignore the exponent. */
2039 if (!ISDIGIT (*cstr
))
2042 /* Nonzero value, possibly overflowing or underflowing. */
2043 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2044 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, GMP_RNDZ
);
2045 /* The result should never be a NaN, and because the rounding is
2046 toward zero should never be an infinity. */
2047 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2048 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2053 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2060 real_from_mpfr (r
, m
, NULL_TREE
, GMP_RNDZ
);
2061 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2062 because the hex digits used in real_from_mpfr did not
2063 start with a digit 8 to f, but the exponent bounds above
2064 should have avoided underflow or overflow. */
2065 gcc_assert (r
->cl
= rvc_normal
);
2066 /* Set a sticky bit if mpfr_strtofr was inexact. */
2067 r
->sig
[0] |= inexact
;
2087 /* Legacy. Similar, but return the result directly. */
2090 real_from_string2 (const char *s
, enum machine_mode mode
)
2094 real_from_string (&r
, s
);
2095 if (mode
!= VOIDmode
)
2096 real_convert (&r
, mode
, &r
);
2101 /* Initialize R from string S and desired MODE. */
2104 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, enum machine_mode mode
)
2106 if (DECIMAL_FLOAT_MODE_P (mode
))
2107 decimal_real_from_string (r
, s
);
2109 real_from_string (r
, s
);
2111 if (mode
!= VOIDmode
)
2112 real_convert (r
, mode
, r
);
2115 /* Initialize R from the integer pair HIGH+LOW. */
2118 real_from_integer (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2119 unsigned HOST_WIDE_INT low
, HOST_WIDE_INT high
,
2122 if (low
== 0 && high
== 0)
2126 memset (r
, 0, sizeof (*r
));
2128 r
->sign
= high
< 0 && !unsigned_p
;
2129 SET_REAL_EXP (r
, HOST_BITS_PER_DOUBLE_INT
);
2140 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2142 r
->sig
[SIGSZ
-1] = high
;
2143 r
->sig
[SIGSZ
-2] = low
;
2147 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2148 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2149 r
->sig
[SIGSZ
-2] = high
;
2150 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2151 r
->sig
[SIGSZ
-4] = low
;
2157 if (DECIMAL_FLOAT_MODE_P (mode
))
2158 decimal_from_integer (r
);
2159 else if (mode
!= VOIDmode
)
2160 real_convert (r
, mode
, r
);
2163 /* Render R, an integral value, as a floating point constant with no
2164 specified exponent. */
2167 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2170 int dec_exp
, digit
, digits
;
2171 REAL_VALUE_TYPE r
, pten
;
2177 if (r
.cl
== rvc_zero
)
2186 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2187 digits
= dec_exp
+ 1;
2188 gcc_assert ((digits
+ 2) < (int)buf_size
);
2190 pten
= *real_digit (1);
2191 times_pten (&pten
, dec_exp
);
2197 digit
= rtd_divmod (&r
, &pten
);
2198 gcc_assert (digit
>= 0 && digit
<= 9);
2200 while (--digits
> 0)
2203 digit
= rtd_divmod (&r
, &pten
);
2210 /* Convert a real with an integral value to decimal float. */
2213 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2217 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2218 decimal_real_from_string (r
, str
);
2221 /* Returns 10**2**N. */
2223 static const REAL_VALUE_TYPE
*
2226 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2228 gcc_assert (n
>= 0);
2229 gcc_assert (n
< EXP_BITS
);
2231 if (tens
[n
].cl
== rvc_zero
)
2233 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2235 HOST_WIDE_INT t
= 10;
2238 for (i
= 0; i
< n
; ++i
)
2241 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2245 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2246 do_multiply (&tens
[n
], t
, t
);
2253 /* Returns 10**(-2**N). */
2255 static const REAL_VALUE_TYPE
*
2256 ten_to_mptwo (int n
)
2258 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2260 gcc_assert (n
>= 0);
2261 gcc_assert (n
< EXP_BITS
);
2263 if (tens
[n
].cl
== rvc_zero
)
2264 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2271 static const REAL_VALUE_TYPE
*
2274 static REAL_VALUE_TYPE num
[10];
2276 gcc_assert (n
>= 0);
2277 gcc_assert (n
<= 9);
2279 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2280 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2285 /* Multiply R by 10**EXP. */
2288 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2290 REAL_VALUE_TYPE pten
, *rr
;
2291 bool negative
= (exp
< 0);
2297 pten
= *real_digit (1);
2303 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2305 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2308 do_divide (r
, r
, &pten
);
2311 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2313 const REAL_VALUE_TYPE
*
2316 static REAL_VALUE_TYPE value
;
2318 /* Initialize mathematical constants for constant folding builtins.
2319 These constants need to be given to at least 160 bits precision. */
2320 if (value
.cl
== rvc_zero
)
2323 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2324 mpfr_set_ui (m
, 1, GMP_RNDN
);
2325 mpfr_exp (m
, m
, GMP_RNDN
);
2326 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2333 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2335 const REAL_VALUE_TYPE
*
2336 dconst_third_ptr (void)
2338 static REAL_VALUE_TYPE value
;
2340 /* Initialize mathematical constants for constant folding builtins.
2341 These constants need to be given to at least 160 bits precision. */
2342 if (value
.cl
== rvc_zero
)
2344 real_arithmetic (&value
, RDIV_EXPR
, &dconst1
, real_digit (3));
2349 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2351 const REAL_VALUE_TYPE
*
2352 dconst_sqrt2_ptr (void)
2354 static REAL_VALUE_TYPE value
;
2356 /* Initialize mathematical constants for constant folding builtins.
2357 These constants need to be given to at least 160 bits precision. */
2358 if (value
.cl
== rvc_zero
)
2361 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2362 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2363 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2369 /* Fills R with +Inf. */
2372 real_inf (REAL_VALUE_TYPE
*r
)
2377 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2378 we force a QNaN, else we force an SNaN. The string, if not empty,
2379 is parsed as a number and placed in the significand. Return true
2380 if the string was successfully parsed. */
2383 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2384 enum machine_mode mode
)
2386 const struct real_format
*fmt
;
2388 fmt
= REAL_MODE_FORMAT (mode
);
2394 get_canonical_qnan (r
, 0);
2396 get_canonical_snan (r
, 0);
2402 memset (r
, 0, sizeof (*r
));
2405 /* Parse akin to strtol into the significand of R. */
2407 while (ISSPACE (*str
))
2411 else if (*str
== '+')
2416 if (*str
== 'x' || *str
== 'X')
2425 while ((d
= hex_value (*str
)) < base
)
2432 lshift_significand (r
, r
, 3);
2435 lshift_significand (r
, r
, 4);
2438 lshift_significand_1 (&u
, r
);
2439 lshift_significand (r
, r
, 3);
2440 add_significands (r
, r
, &u
);
2448 add_significands (r
, r
, &u
);
2453 /* Must have consumed the entire string for success. */
2457 /* Shift the significand into place such that the bits
2458 are in the most significant bits for the format. */
2459 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2461 /* Our MSB is always unset for NaNs. */
2462 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2464 /* Force quiet or signalling NaN. */
2465 r
->signalling
= !quiet
;
2471 /* Fills R with the largest finite value representable in mode MODE.
2472 If SIGN is nonzero, R is set to the most negative finite value. */
2475 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
2477 const struct real_format
*fmt
;
2480 fmt
= REAL_MODE_FORMAT (mode
);
2482 memset (r
, 0, sizeof (*r
));
2485 decimal_real_maxval (r
, sign
, mode
);
2490 SET_REAL_EXP (r
, fmt
->emax
);
2492 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2493 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2494 clear_significand_below (r
, np2
);
2496 if (fmt
->pnan
< fmt
->p
)
2497 /* This is an IBM extended double format made up of two IEEE
2498 doubles. The value of the long double is the sum of the
2499 values of the two parts. The most significant part is
2500 required to be the value of the long double rounded to the
2501 nearest double. Rounding means we need a slightly smaller
2502 value for LDBL_MAX. */
2503 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2507 /* Fills R with 2**N. */
2510 real_2expN (REAL_VALUE_TYPE
*r
, int n
, enum machine_mode fmode
)
2512 memset (r
, 0, sizeof (*r
));
2517 else if (n
< -MAX_EXP
)
2522 SET_REAL_EXP (r
, n
);
2523 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2525 if (DECIMAL_FLOAT_MODE_P (fmode
))
2526 decimal_real_convert (r
, fmode
, r
);
2531 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2535 bool round_up
= false;
2541 decimal_round_for_format (fmt
, r
);
2544 /* FIXME. We can come here via fp_easy_constant
2545 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2546 investigated whether this convert needs to be here, or
2547 something else is missing. */
2548 decimal_real_convert (r
, DFmode
, r
);
2552 emin2m1
= fmt
->emin
- 1;
2555 np2
= SIGNIFICAND_BITS
- p2
;
2559 get_zero (r
, r
->sign
);
2561 if (!fmt
->has_signed_zero
)
2566 get_inf (r
, r
->sign
);
2571 clear_significand_below (r
, np2
);
2581 /* Check the range of the exponent. If we're out of range,
2582 either underflow or overflow. */
2583 if (REAL_EXP (r
) > emax2
)
2585 else if (REAL_EXP (r
) <= emin2m1
)
2589 if (!fmt
->has_denorm
)
2591 /* Don't underflow completely until we've had a chance to round. */
2592 if (REAL_EXP (r
) < emin2m1
)
2597 diff
= emin2m1
- REAL_EXP (r
) + 1;
2601 /* De-normalize the significand. */
2602 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2603 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2607 if (!fmt
->round_towards_zero
)
2609 /* There are P2 true significand bits, followed by one guard bit,
2610 followed by one sticky bit, followed by stuff. Fold nonzero
2611 stuff into the sticky bit. */
2612 unsigned long sticky
;
2616 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2617 sticky
|= r
->sig
[i
];
2619 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2621 guard
= test_significand_bit (r
, np2
- 1);
2622 lsb
= test_significand_bit (r
, np2
);
2624 /* Round to even. */
2625 round_up
= guard
&& (sticky
|| lsb
);
2632 set_significand_bit (&u
, np2
);
2634 if (add_significands (r
, r
, &u
))
2636 /* Overflow. Means the significand had been all ones, and
2637 is now all zeros. Need to increase the exponent, and
2638 possibly re-normalize it. */
2639 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2640 if (REAL_EXP (r
) > emax2
)
2642 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2646 /* Catch underflow that we deferred until after rounding. */
2647 if (REAL_EXP (r
) <= emin2m1
)
2650 /* Clear out trailing garbage. */
2651 clear_significand_below (r
, np2
);
2654 /* Extend or truncate to a new mode. */
2657 real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2658 const REAL_VALUE_TYPE
*a
)
2660 const struct real_format
*fmt
;
2662 fmt
= REAL_MODE_FORMAT (mode
);
2667 if (a
->decimal
|| fmt
->b
== 10)
2668 decimal_real_convert (r
, mode
, a
);
2670 round_for_format (fmt
, r
);
2672 /* round_for_format de-normalizes denormals. Undo just that part. */
2673 if (r
->cl
== rvc_normal
)
2677 /* Legacy. Likewise, except return the struct directly. */
2680 real_value_truncate (enum machine_mode mode
, REAL_VALUE_TYPE a
)
2683 real_convert (&r
, mode
, &a
);
2687 /* Return true if truncating to MODE is exact. */
2690 exact_real_truncate (enum machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2692 const struct real_format
*fmt
;
2696 fmt
= REAL_MODE_FORMAT (mode
);
2699 /* Don't allow conversion to denormals. */
2700 emin2m1
= fmt
->emin
- 1;
2701 if (REAL_EXP (a
) <= emin2m1
)
2704 /* After conversion to the new mode, the value must be identical. */
2705 real_convert (&t
, mode
, a
);
2706 return real_identical (&t
, a
);
2709 /* Write R to the given target format. Place the words of the result
2710 in target word order in BUF. There are always 32 bits in each
2711 long, no matter the size of the host long.
2713 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2716 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2717 const struct real_format
*fmt
)
2723 round_for_format (fmt
, &r
);
2727 (*fmt
->encode
) (fmt
, buf
, &r
);
2732 /* Similar, but look up the format from MODE. */
2735 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, enum machine_mode mode
)
2737 const struct real_format
*fmt
;
2739 fmt
= REAL_MODE_FORMAT (mode
);
2742 return real_to_target_fmt (buf
, r
, fmt
);
2745 /* Read R from the given target format. Read the words of the result
2746 in target word order in BUF. There are always 32 bits in each
2747 long, no matter the size of the host long. */
2750 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2751 const struct real_format
*fmt
)
2753 (*fmt
->decode
) (fmt
, r
, buf
);
2756 /* Similar, but look up the format from MODE. */
2759 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, enum machine_mode mode
)
2761 const struct real_format
*fmt
;
2763 fmt
= REAL_MODE_FORMAT (mode
);
2766 (*fmt
->decode
) (fmt
, r
, buf
);
2769 /* Return the number of bits of the largest binary value that the
2770 significand of MODE will hold. */
2771 /* ??? Legacy. Should get access to real_format directly. */
2774 significand_size (enum machine_mode mode
)
2776 const struct real_format
*fmt
;
2778 fmt
= REAL_MODE_FORMAT (mode
);
2784 /* Return the size in bits of the largest binary value that can be
2785 held by the decimal coefficient for this mode. This is one more
2786 than the number of bits required to hold the largest coefficient
2788 double log2_10
= 3.3219281;
2789 return fmt
->p
* log2_10
;
2794 /* Return a hash value for the given real value. */
2795 /* ??? The "unsigned int" return value is intended to be hashval_t,
2796 but I didn't want to pull hashtab.h into real.h. */
2799 real_hash (const REAL_VALUE_TYPE
*r
)
2804 h
= r
->cl
| (r
->sign
<< 2);
2812 h
|= REAL_EXP (r
) << 3;
2817 h
^= (unsigned int)-1;
2826 if (sizeof (unsigned long) > sizeof (unsigned int))
2827 for (i
= 0; i
< SIGSZ
; ++i
)
2829 unsigned long s
= r
->sig
[i
];
2830 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2833 for (i
= 0; i
< SIGSZ
; ++i
)
2839 /* IEEE single-precision format. */
2841 static void encode_ieee_single (const struct real_format
*fmt
,
2842 long *, const REAL_VALUE_TYPE
*);
2843 static void decode_ieee_single (const struct real_format
*,
2844 REAL_VALUE_TYPE
*, const long *);
2847 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2848 const REAL_VALUE_TYPE
*r
)
2850 unsigned long image
, sig
, exp
;
2851 unsigned long sign
= r
->sign
;
2852 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2855 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2866 image
|= 0x7fffffff;
2873 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2874 if (r
->signalling
== fmt
->qnan_msb_set
)
2885 image
|= 0x7fffffff;
2889 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2890 whereas the intermediate representation is 0.F x 2**exp.
2891 Which means we're off by one. */
2895 exp
= REAL_EXP (r
) + 127 - 1;
2908 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2911 unsigned long image
= buf
[0] & 0xffffffff;
2912 bool sign
= (image
>> 31) & 1;
2913 int exp
= (image
>> 23) & 0xff;
2915 memset (r
, 0, sizeof (*r
));
2916 image
<<= HOST_BITS_PER_LONG
- 24;
2921 if (image
&& fmt
->has_denorm
)
2925 SET_REAL_EXP (r
, -126);
2926 r
->sig
[SIGSZ
-1] = image
<< 1;
2929 else if (fmt
->has_signed_zero
)
2932 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2938 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2939 ^ fmt
->qnan_msb_set
);
2940 r
->sig
[SIGSZ
-1] = image
;
2952 SET_REAL_EXP (r
, exp
- 127 + 1);
2953 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2957 const struct real_format ieee_single_format
=
2978 const struct real_format mips_single_format
=
2999 const struct real_format motorola_single_format
=
3020 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3021 single precision with the following differences:
3022 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3024 - NaNs are not supported.
3025 - The range of non-zero numbers in binary is
3026 (001)[1.]000...000 to (255)[1.]111...111.
3027 - Denormals can be represented, but are treated as +0.0 when
3028 used as an operand and are never generated as a result.
3029 - -0.0 can be represented, but a zero result is always +0.0.
3030 - the only supported rounding mode is trunction (towards zero). */
3031 const struct real_format spu_single_format
=
3052 /* IEEE double-precision format. */
3054 static void encode_ieee_double (const struct real_format
*fmt
,
3055 long *, const REAL_VALUE_TYPE
*);
3056 static void decode_ieee_double (const struct real_format
*,
3057 REAL_VALUE_TYPE
*, const long *);
3060 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3061 const REAL_VALUE_TYPE
*r
)
3063 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3064 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3066 image_hi
= r
->sign
<< 31;
3069 if (HOST_BITS_PER_LONG
== 64)
3071 sig_hi
= r
->sig
[SIGSZ
-1];
3072 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3073 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3077 sig_hi
= r
->sig
[SIGSZ
-1];
3078 sig_lo
= r
->sig
[SIGSZ
-2];
3079 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3080 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3090 image_hi
|= 2047 << 20;
3093 image_hi
|= 0x7fffffff;
3094 image_lo
= 0xffffffff;
3103 if (fmt
->canonical_nan_lsbs_set
)
3105 sig_hi
= (1 << 19) - 1;
3106 sig_lo
= 0xffffffff;
3114 if (r
->signalling
== fmt
->qnan_msb_set
)
3115 sig_hi
&= ~(1 << 19);
3118 if (sig_hi
== 0 && sig_lo
== 0)
3121 image_hi
|= 2047 << 20;
3127 image_hi
|= 0x7fffffff;
3128 image_lo
= 0xffffffff;
3133 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3134 whereas the intermediate representation is 0.F x 2**exp.
3135 Which means we're off by one. */
3139 exp
= REAL_EXP (r
) + 1023 - 1;
3140 image_hi
|= exp
<< 20;
3149 if (FLOAT_WORDS_BIG_ENDIAN
)
3150 buf
[0] = image_hi
, buf
[1] = image_lo
;
3152 buf
[0] = image_lo
, buf
[1] = image_hi
;
3156 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3159 unsigned long image_hi
, image_lo
;
3163 if (FLOAT_WORDS_BIG_ENDIAN
)
3164 image_hi
= buf
[0], image_lo
= buf
[1];
3166 image_lo
= buf
[0], image_hi
= buf
[1];
3167 image_lo
&= 0xffffffff;
3168 image_hi
&= 0xffffffff;
3170 sign
= (image_hi
>> 31) & 1;
3171 exp
= (image_hi
>> 20) & 0x7ff;
3173 memset (r
, 0, sizeof (*r
));
3175 image_hi
<<= 32 - 21;
3176 image_hi
|= image_lo
>> 21;
3177 image_hi
&= 0x7fffffff;
3178 image_lo
<<= 32 - 21;
3182 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3186 SET_REAL_EXP (r
, -1022);
3187 if (HOST_BITS_PER_LONG
== 32)
3189 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3191 r
->sig
[SIGSZ
-1] = image_hi
;
3192 r
->sig
[SIGSZ
-2] = image_lo
;
3196 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3197 r
->sig
[SIGSZ
-1] = image_hi
;
3201 else if (fmt
->has_signed_zero
)
3204 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3206 if (image_hi
|| image_lo
)
3210 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3211 if (HOST_BITS_PER_LONG
== 32)
3213 r
->sig
[SIGSZ
-1] = image_hi
;
3214 r
->sig
[SIGSZ
-2] = image_lo
;
3217 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3229 SET_REAL_EXP (r
, exp
- 1023 + 1);
3230 if (HOST_BITS_PER_LONG
== 32)
3232 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3233 r
->sig
[SIGSZ
-2] = image_lo
;
3236 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3240 const struct real_format ieee_double_format
=
3261 const struct real_format mips_double_format
=
3282 const struct real_format motorola_double_format
=
3303 /* IEEE extended real format. This comes in three flavors: Intel's as
3304 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3305 12- and 16-byte images may be big- or little endian; Motorola's is
3306 always big endian. */
3308 /* Helper subroutine which converts from the internal format to the
3309 12-byte little-endian Intel format. Functions below adjust this
3310 for the other possible formats. */
3312 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3313 const REAL_VALUE_TYPE
*r
)
3315 unsigned long image_hi
, sig_hi
, sig_lo
;
3316 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3318 image_hi
= r
->sign
<< 15;
3319 sig_hi
= sig_lo
= 0;
3331 /* Intel requires the explicit integer bit to be set, otherwise
3332 it considers the value a "pseudo-infinity". Motorola docs
3333 say it doesn't care. */
3334 sig_hi
= 0x80000000;
3339 sig_lo
= sig_hi
= 0xffffffff;
3349 if (fmt
->canonical_nan_lsbs_set
)
3351 sig_hi
= (1 << 30) - 1;
3352 sig_lo
= 0xffffffff;
3355 else if (HOST_BITS_PER_LONG
== 32)
3357 sig_hi
= r
->sig
[SIGSZ
-1];
3358 sig_lo
= r
->sig
[SIGSZ
-2];
3362 sig_lo
= r
->sig
[SIGSZ
-1];
3363 sig_hi
= sig_lo
>> 31 >> 1;
3364 sig_lo
&= 0xffffffff;
3366 if (r
->signalling
== fmt
->qnan_msb_set
)
3367 sig_hi
&= ~(1 << 30);
3370 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3373 /* Intel requires the explicit integer bit to be set, otherwise
3374 it considers the value a "pseudo-nan". Motorola docs say it
3376 sig_hi
|= 0x80000000;
3381 sig_lo
= sig_hi
= 0xffffffff;
3387 int exp
= REAL_EXP (r
);
3389 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3390 whereas the intermediate representation is 0.F x 2**exp.
3391 Which means we're off by one.
3393 Except for Motorola, which consider exp=0 and explicit
3394 integer bit set to continue to be normalized. In theory
3395 this discrepancy has been taken care of by the difference
3396 in fmt->emin in round_for_format. */
3403 gcc_assert (exp
>= 0);
3407 if (HOST_BITS_PER_LONG
== 32)
3409 sig_hi
= r
->sig
[SIGSZ
-1];
3410 sig_lo
= r
->sig
[SIGSZ
-2];
3414 sig_lo
= r
->sig
[SIGSZ
-1];
3415 sig_hi
= sig_lo
>> 31 >> 1;
3416 sig_lo
&= 0xffffffff;
3425 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3428 /* Convert from the internal format to the 12-byte Motorola format
3429 for an IEEE extended real. */
3431 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3432 const REAL_VALUE_TYPE
*r
)
3435 encode_ieee_extended (fmt
, intermed
, r
);
3437 /* Motorola chips are assumed always to be big-endian. Also, the
3438 padding in a Motorola extended real goes between the exponent and
3439 the mantissa. At this point the mantissa is entirely within
3440 elements 0 and 1 of intermed, and the exponent entirely within
3441 element 2, so all we have to do is swap the order around, and
3442 shift element 2 left 16 bits. */
3443 buf
[0] = intermed
[2] << 16;
3444 buf
[1] = intermed
[1];
3445 buf
[2] = intermed
[0];
3448 /* Convert from the internal format to the 12-byte Intel format for
3449 an IEEE extended real. */
3451 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3452 const REAL_VALUE_TYPE
*r
)
3454 if (FLOAT_WORDS_BIG_ENDIAN
)
3456 /* All the padding in an Intel-format extended real goes at the high
3457 end, which in this case is after the mantissa, not the exponent.
3458 Therefore we must shift everything down 16 bits. */
3460 encode_ieee_extended (fmt
, intermed
, r
);
3461 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3462 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3463 buf
[2] = (intermed
[0] << 16);
3466 /* encode_ieee_extended produces what we want directly. */
3467 encode_ieee_extended (fmt
, buf
, r
);
3470 /* Convert from the internal format to the 16-byte Intel format for
3471 an IEEE extended real. */
3473 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3474 const REAL_VALUE_TYPE
*r
)
3476 /* All the padding in an Intel-format extended real goes at the high end. */
3477 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3481 /* As above, we have a helper function which converts from 12-byte
3482 little-endian Intel format to internal format. Functions below
3483 adjust for the other possible formats. */
3485 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3488 unsigned long image_hi
, sig_hi
, sig_lo
;
3492 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3493 sig_lo
&= 0xffffffff;
3494 sig_hi
&= 0xffffffff;
3495 image_hi
&= 0xffffffff;
3497 sign
= (image_hi
>> 15) & 1;
3498 exp
= image_hi
& 0x7fff;
3500 memset (r
, 0, sizeof (*r
));
3504 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3509 /* When the IEEE format contains a hidden bit, we know that
3510 it's zero at this point, and so shift up the significand
3511 and decrease the exponent to match. In this case, Motorola
3512 defines the explicit integer bit to be valid, so we don't
3513 know whether the msb is set or not. */
3514 SET_REAL_EXP (r
, fmt
->emin
);
3515 if (HOST_BITS_PER_LONG
== 32)
3517 r
->sig
[SIGSZ
-1] = sig_hi
;
3518 r
->sig
[SIGSZ
-2] = sig_lo
;
3521 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3525 else if (fmt
->has_signed_zero
)
3528 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3530 /* See above re "pseudo-infinities" and "pseudo-nans".
3531 Short summary is that the MSB will likely always be
3532 set, and that we don't care about it. */
3533 sig_hi
&= 0x7fffffff;
3535 if (sig_hi
|| sig_lo
)
3539 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3540 if (HOST_BITS_PER_LONG
== 32)
3542 r
->sig
[SIGSZ
-1] = sig_hi
;
3543 r
->sig
[SIGSZ
-2] = sig_lo
;
3546 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3558 SET_REAL_EXP (r
, exp
- 16383 + 1);
3559 if (HOST_BITS_PER_LONG
== 32)
3561 r
->sig
[SIGSZ
-1] = sig_hi
;
3562 r
->sig
[SIGSZ
-2] = sig_lo
;
3565 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3569 /* Convert from the internal format to the 12-byte Motorola format
3570 for an IEEE extended real. */
3572 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3577 /* Motorola chips are assumed always to be big-endian. Also, the
3578 padding in a Motorola extended real goes between the exponent and
3579 the mantissa; remove it. */
3580 intermed
[0] = buf
[2];
3581 intermed
[1] = buf
[1];
3582 intermed
[2] = (unsigned long)buf
[0] >> 16;
3584 decode_ieee_extended (fmt
, r
, intermed
);
3587 /* Convert from the internal format to the 12-byte Intel format for
3588 an IEEE extended real. */
3590 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3593 if (FLOAT_WORDS_BIG_ENDIAN
)
3595 /* All the padding in an Intel-format extended real goes at the high
3596 end, which in this case is after the mantissa, not the exponent.
3597 Therefore we must shift everything up 16 bits. */
3600 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3601 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3602 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3604 decode_ieee_extended (fmt
, r
, intermed
);
3607 /* decode_ieee_extended produces what we want directly. */
3608 decode_ieee_extended (fmt
, r
, buf
);
3611 /* Convert from the internal format to the 16-byte Intel format for
3612 an IEEE extended real. */
3614 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3617 /* All the padding in an Intel-format extended real goes at the high end. */
3618 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3621 const struct real_format ieee_extended_motorola_format
=
3623 encode_ieee_extended_motorola
,
3624 decode_ieee_extended_motorola
,
3642 const struct real_format ieee_extended_intel_96_format
=
3644 encode_ieee_extended_intel_96
,
3645 decode_ieee_extended_intel_96
,
3663 const struct real_format ieee_extended_intel_128_format
=
3665 encode_ieee_extended_intel_128
,
3666 decode_ieee_extended_intel_128
,
3684 /* The following caters to i386 systems that set the rounding precision
3685 to 53 bits instead of 64, e.g. FreeBSD. */
3686 const struct real_format ieee_extended_intel_96_round_53_format
=
3688 encode_ieee_extended_intel_96
,
3689 decode_ieee_extended_intel_96
,
3707 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3708 numbers whose sum is equal to the extended precision value. The number
3709 with greater magnitude is first. This format has the same magnitude
3710 range as an IEEE double precision value, but effectively 106 bits of
3711 significand precision. Infinity and NaN are represented by their IEEE
3712 double precision value stored in the first number, the second number is
3713 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3715 static void encode_ibm_extended (const struct real_format
*fmt
,
3716 long *, const REAL_VALUE_TYPE
*);
3717 static void decode_ibm_extended (const struct real_format
*,
3718 REAL_VALUE_TYPE
*, const long *);
3721 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3722 const REAL_VALUE_TYPE
*r
)
3724 REAL_VALUE_TYPE u
, normr
, v
;
3725 const struct real_format
*base_fmt
;
3727 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3729 /* Renormalize R before doing any arithmetic on it. */
3731 if (normr
.cl
== rvc_normal
)
3734 /* u = IEEE double precision portion of significand. */
3736 round_for_format (base_fmt
, &u
);
3737 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3739 if (u
.cl
== rvc_normal
)
3741 do_add (&v
, &normr
, &u
, 1);
3742 /* Call round_for_format since we might need to denormalize. */
3743 round_for_format (base_fmt
, &v
);
3744 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3748 /* Inf, NaN, 0 are all representable as doubles, so the
3749 least-significant part can be 0.0. */
3756 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3759 REAL_VALUE_TYPE u
, v
;
3760 const struct real_format
*base_fmt
;
3762 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3763 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3765 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3767 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3768 do_add (r
, &u
, &v
, 0);
3774 const struct real_format ibm_extended_format
=
3776 encode_ibm_extended
,
3777 decode_ibm_extended
,
3795 const struct real_format mips_extended_format
=
3797 encode_ibm_extended
,
3798 decode_ibm_extended
,
3817 /* IEEE quad precision format. */
3819 static void encode_ieee_quad (const struct real_format
*fmt
,
3820 long *, const REAL_VALUE_TYPE
*);
3821 static void decode_ieee_quad (const struct real_format
*,
3822 REAL_VALUE_TYPE
*, const long *);
3825 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3826 const REAL_VALUE_TYPE
*r
)
3828 unsigned long image3
, image2
, image1
, image0
, exp
;
3829 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3832 image3
= r
->sign
<< 31;
3837 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3846 image3
|= 32767 << 16;
3849 image3
|= 0x7fffffff;
3850 image2
= 0xffffffff;
3851 image1
= 0xffffffff;
3852 image0
= 0xffffffff;
3859 image3
|= 32767 << 16;
3863 if (fmt
->canonical_nan_lsbs_set
)
3866 image2
= image1
= image0
= 0xffffffff;
3869 else if (HOST_BITS_PER_LONG
== 32)
3874 image3
|= u
.sig
[3] & 0xffff;
3879 image1
= image0
>> 31 >> 1;
3881 image3
|= (image2
>> 31 >> 1) & 0xffff;
3882 image0
&= 0xffffffff;
3883 image2
&= 0xffffffff;
3885 if (r
->signalling
== fmt
->qnan_msb_set
)
3889 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3894 image3
|= 0x7fffffff;
3895 image2
= 0xffffffff;
3896 image1
= 0xffffffff;
3897 image0
= 0xffffffff;
3902 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3903 whereas the intermediate representation is 0.F x 2**exp.
3904 Which means we're off by one. */
3908 exp
= REAL_EXP (r
) + 16383 - 1;
3909 image3
|= exp
<< 16;
3911 if (HOST_BITS_PER_LONG
== 32)
3916 image3
|= u
.sig
[3] & 0xffff;
3921 image1
= image0
>> 31 >> 1;
3923 image3
|= (image2
>> 31 >> 1) & 0xffff;
3924 image0
&= 0xffffffff;
3925 image2
&= 0xffffffff;
3933 if (FLOAT_WORDS_BIG_ENDIAN
)
3950 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3953 unsigned long image3
, image2
, image1
, image0
;
3957 if (FLOAT_WORDS_BIG_ENDIAN
)
3971 image0
&= 0xffffffff;
3972 image1
&= 0xffffffff;
3973 image2
&= 0xffffffff;
3975 sign
= (image3
>> 31) & 1;
3976 exp
= (image3
>> 16) & 0x7fff;
3979 memset (r
, 0, sizeof (*r
));
3983 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
3988 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
3989 if (HOST_BITS_PER_LONG
== 32)
3998 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3999 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4004 else if (fmt
->has_signed_zero
)
4007 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4009 if (image3
| image2
| image1
| image0
)
4013 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4015 if (HOST_BITS_PER_LONG
== 32)
4024 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4025 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4027 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4039 SET_REAL_EXP (r
, exp
- 16383 + 1);
4041 if (HOST_BITS_PER_LONG
== 32)
4050 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4051 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4053 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4054 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4058 const struct real_format ieee_quad_format
=
4079 const struct real_format mips_quad_format
=
4100 /* Descriptions of VAX floating point formats can be found beginning at
4102 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4104 The thing to remember is that they're almost IEEE, except for word
4105 order, exponent bias, and the lack of infinities, nans, and denormals.
4107 We don't implement the H_floating format here, simply because neither
4108 the VAX or Alpha ports use it. */
4110 static void encode_vax_f (const struct real_format
*fmt
,
4111 long *, const REAL_VALUE_TYPE
*);
4112 static void decode_vax_f (const struct real_format
*,
4113 REAL_VALUE_TYPE
*, const long *);
4114 static void encode_vax_d (const struct real_format
*fmt
,
4115 long *, const REAL_VALUE_TYPE
*);
4116 static void decode_vax_d (const struct real_format
*,
4117 REAL_VALUE_TYPE
*, const long *);
4118 static void encode_vax_g (const struct real_format
*fmt
,
4119 long *, const REAL_VALUE_TYPE
*);
4120 static void decode_vax_g (const struct real_format
*,
4121 REAL_VALUE_TYPE
*, const long *);
4124 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4125 const REAL_VALUE_TYPE
*r
)
4127 unsigned long sign
, exp
, sig
, image
;
4129 sign
= r
->sign
<< 15;
4139 image
= 0xffff7fff | sign
;
4143 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4144 exp
= REAL_EXP (r
) + 128;
4146 image
= (sig
<< 16) & 0xffff0000;
4160 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4161 REAL_VALUE_TYPE
*r
, const long *buf
)
4163 unsigned long image
= buf
[0] & 0xffffffff;
4164 int exp
= (image
>> 7) & 0xff;
4166 memset (r
, 0, sizeof (*r
));
4171 r
->sign
= (image
>> 15) & 1;
4172 SET_REAL_EXP (r
, exp
- 128);
4174 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4175 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4180 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4181 const REAL_VALUE_TYPE
*r
)
4183 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4188 image0
= image1
= 0;
4193 image0
= 0xffff7fff | sign
;
4194 image1
= 0xffffffff;
4198 /* Extract the significand into straight hi:lo. */
4199 if (HOST_BITS_PER_LONG
== 64)
4201 image0
= r
->sig
[SIGSZ
-1];
4202 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4203 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4207 image0
= r
->sig
[SIGSZ
-1];
4208 image1
= r
->sig
[SIGSZ
-2];
4209 image1
= (image0
<< 24) | (image1
>> 8);
4210 image0
= (image0
>> 8) & 0xffffff;
4213 /* Rearrange the half-words of the significand to match the
4215 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4216 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4218 /* Add the sign and exponent. */
4220 image0
|= (REAL_EXP (r
) + 128) << 7;
4227 if (FLOAT_WORDS_BIG_ENDIAN
)
4228 buf
[0] = image1
, buf
[1] = image0
;
4230 buf
[0] = image0
, buf
[1] = image1
;
4234 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4235 REAL_VALUE_TYPE
*r
, const long *buf
)
4237 unsigned long image0
, image1
;
4240 if (FLOAT_WORDS_BIG_ENDIAN
)
4241 image1
= buf
[0], image0
= buf
[1];
4243 image0
= buf
[0], image1
= buf
[1];
4244 image0
&= 0xffffffff;
4245 image1
&= 0xffffffff;
4247 exp
= (image0
>> 7) & 0xff;
4249 memset (r
, 0, sizeof (*r
));
4254 r
->sign
= (image0
>> 15) & 1;
4255 SET_REAL_EXP (r
, exp
- 128);
4257 /* Rearrange the half-words of the external format into
4258 proper ascending order. */
4259 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4260 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4262 if (HOST_BITS_PER_LONG
== 64)
4264 image0
= (image0
<< 31 << 1) | image1
;
4267 r
->sig
[SIGSZ
-1] = image0
;
4271 r
->sig
[SIGSZ
-1] = image0
;
4272 r
->sig
[SIGSZ
-2] = image1
;
4273 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4274 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4280 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4281 const REAL_VALUE_TYPE
*r
)
4283 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4288 image0
= image1
= 0;
4293 image0
= 0xffff7fff | sign
;
4294 image1
= 0xffffffff;
4298 /* Extract the significand into straight hi:lo. */
4299 if (HOST_BITS_PER_LONG
== 64)
4301 image0
= r
->sig
[SIGSZ
-1];
4302 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4303 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4307 image0
= r
->sig
[SIGSZ
-1];
4308 image1
= r
->sig
[SIGSZ
-2];
4309 image1
= (image0
<< 21) | (image1
>> 11);
4310 image0
= (image0
>> 11) & 0xfffff;
4313 /* Rearrange the half-words of the significand to match the
4315 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4316 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4318 /* Add the sign and exponent. */
4320 image0
|= (REAL_EXP (r
) + 1024) << 4;
4327 if (FLOAT_WORDS_BIG_ENDIAN
)
4328 buf
[0] = image1
, buf
[1] = image0
;
4330 buf
[0] = image0
, buf
[1] = image1
;
4334 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4335 REAL_VALUE_TYPE
*r
, const long *buf
)
4337 unsigned long image0
, image1
;
4340 if (FLOAT_WORDS_BIG_ENDIAN
)
4341 image1
= buf
[0], image0
= buf
[1];
4343 image0
= buf
[0], image1
= buf
[1];
4344 image0
&= 0xffffffff;
4345 image1
&= 0xffffffff;
4347 exp
= (image0
>> 4) & 0x7ff;
4349 memset (r
, 0, sizeof (*r
));
4354 r
->sign
= (image0
>> 15) & 1;
4355 SET_REAL_EXP (r
, exp
- 1024);
4357 /* Rearrange the half-words of the external format into
4358 proper ascending order. */
4359 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4360 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4362 if (HOST_BITS_PER_LONG
== 64)
4364 image0
= (image0
<< 31 << 1) | image1
;
4367 r
->sig
[SIGSZ
-1] = image0
;
4371 r
->sig
[SIGSZ
-1] = image0
;
4372 r
->sig
[SIGSZ
-2] = image1
;
4373 lshift_significand (r
, r
, 64 - 53);
4374 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4379 const struct real_format vax_f_format
=
4400 const struct real_format vax_d_format
=
4421 const struct real_format vax_g_format
=
4442 /* Encode real R into a single precision DFP value in BUF. */
4444 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4445 long *buf ATTRIBUTE_UNUSED
,
4446 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4448 encode_decimal32 (fmt
, buf
, r
);
4451 /* Decode a single precision DFP value in BUF into a real R. */
4453 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4454 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4455 const long *buf ATTRIBUTE_UNUSED
)
4457 decode_decimal32 (fmt
, r
, buf
);
4460 /* Encode real R into a double precision DFP value in BUF. */
4462 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4463 long *buf ATTRIBUTE_UNUSED
,
4464 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4466 encode_decimal64 (fmt
, buf
, r
);
4469 /* Decode a double precision DFP value in BUF into a real R. */
4471 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4472 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4473 const long *buf ATTRIBUTE_UNUSED
)
4475 decode_decimal64 (fmt
, r
, buf
);
4478 /* Encode real R into a quad precision DFP value in BUF. */
4480 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4481 long *buf ATTRIBUTE_UNUSED
,
4482 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4484 encode_decimal128 (fmt
, buf
, r
);
4487 /* Decode a quad precision DFP value in BUF into a real R. */
4489 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4490 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4491 const long *buf ATTRIBUTE_UNUSED
)
4493 decode_decimal128 (fmt
, r
, buf
);
4496 /* Single precision decimal floating point (IEEE 754). */
4497 const struct real_format decimal_single_format
=
4499 encode_decimal_single
,
4500 decode_decimal_single
,
4518 /* Double precision decimal floating point (IEEE 754). */
4519 const struct real_format decimal_double_format
=
4521 encode_decimal_double
,
4522 decode_decimal_double
,
4540 /* Quad precision decimal floating point (IEEE 754). */
4541 const struct real_format decimal_quad_format
=
4543 encode_decimal_quad
,
4544 decode_decimal_quad
,
4562 /* Encode half-precision floats. This routine is used both for the IEEE
4563 ARM alternative encodings. */
4565 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4566 const REAL_VALUE_TYPE
*r
)
4568 unsigned long image
, sig
, exp
;
4569 unsigned long sign
= r
->sign
;
4570 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4573 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4591 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4592 if (r
->signalling
== fmt
->qnan_msb_set
)
4607 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4608 whereas the intermediate representation is 0.F x 2**exp.
4609 Which means we're off by one. */
4613 exp
= REAL_EXP (r
) + 15 - 1;
4625 /* Decode half-precision floats. This routine is used both for the IEEE
4626 ARM alternative encodings. */
4628 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4631 unsigned long image
= buf
[0] & 0xffff;
4632 bool sign
= (image
>> 15) & 1;
4633 int exp
= (image
>> 10) & 0x1f;
4635 memset (r
, 0, sizeof (*r
));
4636 image
<<= HOST_BITS_PER_LONG
- 11;
4641 if (image
&& fmt
->has_denorm
)
4645 SET_REAL_EXP (r
, -14);
4646 r
->sig
[SIGSZ
-1] = image
<< 1;
4649 else if (fmt
->has_signed_zero
)
4652 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4658 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4659 ^ fmt
->qnan_msb_set
);
4660 r
->sig
[SIGSZ
-1] = image
;
4672 SET_REAL_EXP (r
, exp
- 15 + 1);
4673 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4677 /* Half-precision format, as specified in IEEE 754R. */
4678 const struct real_format ieee_half_format
=
4699 /* ARM's alternative half-precision format, similar to IEEE but with
4700 no reserved exponent value for NaNs and infinities; rather, it just
4701 extends the range of exponents by one. */
4702 const struct real_format arm_half_format
=
4723 /* A synthetic "format" for internal arithmetic. It's the size of the
4724 internal significand minus the two bits needed for proper rounding.
4725 The encode and decode routines exist only to satisfy our paranoia
4728 static void encode_internal (const struct real_format
*fmt
,
4729 long *, const REAL_VALUE_TYPE
*);
4730 static void decode_internal (const struct real_format
*,
4731 REAL_VALUE_TYPE
*, const long *);
4734 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4735 const REAL_VALUE_TYPE
*r
)
4737 memcpy (buf
, r
, sizeof (*r
));
4741 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4742 REAL_VALUE_TYPE
*r
, const long *buf
)
4744 memcpy (r
, buf
, sizeof (*r
));
4747 const struct real_format real_internal_format
=
4752 SIGNIFICAND_BITS
- 2,
4753 SIGNIFICAND_BITS
- 2,
4768 /* Calculate X raised to the integer exponent N in mode MODE and store
4769 the result in R. Return true if the result may be inexact due to
4770 loss of precision. The algorithm is the classic "left-to-right binary
4771 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4772 Algorithms", "The Art of Computer Programming", Volume 2. */
4775 real_powi (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4776 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4778 unsigned HOST_WIDE_INT bit
;
4780 bool inexact
= false;
4792 /* Don't worry about overflow, from now on n is unsigned. */
4800 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4801 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4805 inexact
|= do_multiply (&t
, &t
, &t
);
4807 inexact
|= do_multiply (&t
, &t
, x
);
4815 inexact
|= do_divide (&t
, &dconst1
, &t
);
4817 real_convert (r
, mode
, &t
);
4821 /* Round X to the nearest integer not larger in absolute value, i.e.
4822 towards zero, placing the result in R in mode MODE. */
4825 real_trunc (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4826 const REAL_VALUE_TYPE
*x
)
4828 do_fix_trunc (r
, x
);
4829 if (mode
!= VOIDmode
)
4830 real_convert (r
, mode
, r
);
4833 /* Round X to the largest integer not greater in value, i.e. round
4834 down, placing the result in R in mode MODE. */
4837 real_floor (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4838 const REAL_VALUE_TYPE
*x
)
4842 do_fix_trunc (&t
, x
);
4843 if (! real_identical (&t
, x
) && x
->sign
)
4844 do_add (&t
, &t
, &dconstm1
, 0);
4845 if (mode
!= VOIDmode
)
4846 real_convert (r
, mode
, &t
);
4851 /* Round X to the smallest integer not less then argument, i.e. round
4852 up, placing the result in R in mode MODE. */
4855 real_ceil (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4856 const REAL_VALUE_TYPE
*x
)
4860 do_fix_trunc (&t
, x
);
4861 if (! real_identical (&t
, x
) && ! x
->sign
)
4862 do_add (&t
, &t
, &dconst1
, 0);
4863 if (mode
!= VOIDmode
)
4864 real_convert (r
, mode
, &t
);
4869 /* Round X to the nearest integer, but round halfway cases away from
4873 real_round (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4874 const REAL_VALUE_TYPE
*x
)
4876 do_add (r
, x
, &dconsthalf
, x
->sign
);
4877 do_fix_trunc (r
, r
);
4878 if (mode
!= VOIDmode
)
4879 real_convert (r
, mode
, r
);
4882 /* Set the sign of R to the sign of X. */
4885 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4890 /* Check whether the real constant value given is an integer. */
4893 real_isinteger (const REAL_VALUE_TYPE
*c
, enum machine_mode mode
)
4895 REAL_VALUE_TYPE cint
;
4897 real_trunc (&cint
, mode
, c
);
4898 return real_identical (c
, &cint
);
4901 /* Write into BUF the maximum representable finite floating-point
4902 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4903 float string. LEN is the size of BUF, and the buffer must be large
4904 enough to contain the resulting string. */
4907 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
4912 strcpy (buf
, "0x0.");
4914 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
4917 *p
++ = "08ce"[n
- i
];
4918 sprintf (p
, "p%d", fmt
->emax
);
4919 if (fmt
->pnan
< fmt
->p
)
4921 /* This is an IBM extended double format made up of two IEEE
4922 doubles. The value of the long double is the sum of the
4923 values of the two parts. The most significant part is
4924 required to be the value of the long double rounded to the
4925 nearest double. Rounding means we need a slightly smaller
4926 value for LDBL_MAX. */
4927 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
4930 gcc_assert (strlen (buf
) < len
);