1 /* real.c - software floating point emulation.
2 Copyright (C) 1993-2015 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"
28 #include "diagnostic-core.h"
35 /* The floating point model used internally is not exactly IEEE 754
36 compliant, and close to the description in the ISO C99 standard,
37 section 5.2.4.2.2 Characteristics of floating types.
41 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
45 b = base or radix, here always 2
47 p = precision (the number of base-b digits in the significand)
48 f_k = the digits of the significand.
50 We differ from typical IEEE 754 encodings in that the entire
51 significand is fractional. Normalized significands are in the
54 A requirement of the model is that P be larger than the largest
55 supported target floating-point type by at least 2 bits. This gives
56 us proper rounding when we truncate to the target type. In addition,
57 E must be large enough to hold the smallest supported denormal number
60 Both of these requirements are easily satisfied. The largest target
61 significand is 113 bits; we store at least 160. The smallest
62 denormal number fits in 17 exponent bits; we store 26. */
65 /* Used to classify two numbers simultaneously. */
66 #define CLASS2(A, B) ((A) << 2 | (B))
68 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
69 #error "Some constant folding done by hand to avoid shift count warnings"
72 static void get_zero (REAL_VALUE_TYPE
*, int);
73 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
74 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
75 static void get_inf (REAL_VALUE_TYPE
*, int);
76 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
77 const REAL_VALUE_TYPE
*, unsigned int);
78 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
80 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
82 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
83 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
84 const REAL_VALUE_TYPE
*);
85 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
86 const REAL_VALUE_TYPE
*, int);
87 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
88 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
89 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
90 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
91 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
92 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
93 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
94 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
95 const REAL_VALUE_TYPE
*);
96 static void normalize (REAL_VALUE_TYPE
*);
98 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
99 const REAL_VALUE_TYPE
*, int);
100 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
101 const REAL_VALUE_TYPE
*);
102 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
103 const REAL_VALUE_TYPE
*);
104 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
105 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
107 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, REAL_VALUE_TYPE
*);
108 static void decimal_from_integer (REAL_VALUE_TYPE
*);
109 static void decimal_integer_string (char *, const REAL_VALUE_TYPE
*,
112 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
113 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
114 static const REAL_VALUE_TYPE
* real_digit (int);
115 static void times_pten (REAL_VALUE_TYPE
*, int);
117 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
119 /* Initialize R with a positive zero. */
122 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
124 memset (r
, 0, sizeof (*r
));
128 /* Initialize R with the canonical quiet NaN. */
131 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
133 memset (r
, 0, sizeof (*r
));
140 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
142 memset (r
, 0, sizeof (*r
));
150 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
152 memset (r
, 0, sizeof (*r
));
158 /* Right-shift the significand of A by N bits; put the result in the
159 significand of R. If any one bits are shifted out, return true. */
162 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
165 unsigned long sticky
= 0;
166 unsigned int i
, ofs
= 0;
168 if (n
>= HOST_BITS_PER_LONG
)
170 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
172 n
&= HOST_BITS_PER_LONG
- 1;
177 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
178 for (i
= 0; i
< SIGSZ
; ++i
)
181 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
182 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
183 << (HOST_BITS_PER_LONG
- n
)));
188 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
189 r
->sig
[i
] = a
->sig
[ofs
+ i
];
190 for (; i
< SIGSZ
; ++i
)
197 /* Right-shift the significand of A by N bits; put the result in the
201 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
204 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
206 n
&= HOST_BITS_PER_LONG
- 1;
209 for (i
= 0; i
< SIGSZ
; ++i
)
212 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
213 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
214 << (HOST_BITS_PER_LONG
- n
)));
219 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
220 r
->sig
[i
] = a
->sig
[ofs
+ i
];
221 for (; i
< SIGSZ
; ++i
)
226 /* Left-shift the significand of A by N bits; put the result in the
230 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
233 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
235 n
&= HOST_BITS_PER_LONG
- 1;
238 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
239 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
240 for (; i
< SIGSZ
; ++i
)
241 r
->sig
[SIGSZ
-1-i
] = 0;
244 for (i
= 0; i
< SIGSZ
; ++i
)
247 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
248 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
249 >> (HOST_BITS_PER_LONG
- n
)));
253 /* Likewise, but N is specialized to 1. */
256 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
260 for (i
= SIGSZ
- 1; i
> 0; --i
)
261 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
262 r
->sig
[0] = a
->sig
[0] << 1;
265 /* Add the significands of A and B, placing the result in R. Return
266 true if there was carry out of the most significant word. */
269 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
270 const REAL_VALUE_TYPE
*b
)
275 for (i
= 0; i
< SIGSZ
; ++i
)
277 unsigned long ai
= a
->sig
[i
];
278 unsigned long ri
= ai
+ b
->sig
[i
];
294 /* Subtract the significands of A and B, placing the result in R. CARRY is
295 true if there's a borrow incoming to the least significant word.
296 Return true if there was borrow out of the most significant word. */
299 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
300 const REAL_VALUE_TYPE
*b
, int carry
)
304 for (i
= 0; i
< SIGSZ
; ++i
)
306 unsigned long ai
= a
->sig
[i
];
307 unsigned long ri
= ai
- b
->sig
[i
];
323 /* Negate the significand A, placing the result in R. */
326 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
331 for (i
= 0; i
< SIGSZ
; ++i
)
333 unsigned long ri
, ai
= a
->sig
[i
];
352 /* Compare significands. Return tri-state vs zero. */
355 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
359 for (i
= SIGSZ
- 1; i
>= 0; --i
)
361 unsigned long ai
= a
->sig
[i
];
362 unsigned long bi
= b
->sig
[i
];
373 /* Return true if A is nonzero. */
376 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
380 for (i
= SIGSZ
- 1; i
>= 0; --i
)
387 /* Set bit N of the significand of R. */
390 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
392 r
->sig
[n
/ HOST_BITS_PER_LONG
]
393 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
396 /* Clear bit N of the significand of R. */
399 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
401 r
->sig
[n
/ HOST_BITS_PER_LONG
]
402 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
405 /* Test bit N of the significand of R. */
408 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
410 /* ??? Compiler bug here if we return this expression directly.
411 The conversion to bool strips the "&1" and we wind up testing
412 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
413 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
417 /* Clear bits 0..N-1 of the significand of R. */
420 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
422 int i
, w
= n
/ HOST_BITS_PER_LONG
;
424 for (i
= 0; i
< w
; ++i
)
427 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
430 /* Divide the significands of A and B, placing the result in R. Return
431 true if the division was inexact. */
434 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
435 const REAL_VALUE_TYPE
*b
)
438 int i
, bit
= SIGNIFICAND_BITS
- 1;
439 unsigned long msb
, inexact
;
442 memset (r
->sig
, 0, sizeof (r
->sig
));
448 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
449 lshift_significand_1 (&u
, &u
);
451 if (msb
|| cmp_significands (&u
, b
) >= 0)
453 sub_significands (&u
, &u
, b
, 0);
454 set_significand_bit (r
, bit
);
459 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
465 /* Adjust the exponent and significand of R such that the most
466 significant bit is set. We underflow to zero and overflow to
467 infinity here, without denormals. (The intermediate representation
468 exponent is large enough to handle target denormals normalized.) */
471 normalize (REAL_VALUE_TYPE
*r
)
479 /* Find the first word that is nonzero. */
480 for (i
= SIGSZ
- 1; i
>= 0; i
--)
482 shift
+= HOST_BITS_PER_LONG
;
486 /* Zero significand flushes to zero. */
494 /* Find the first bit that is nonzero. */
496 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
502 exp
= REAL_EXP (r
) - shift
;
504 get_inf (r
, r
->sign
);
505 else if (exp
< -MAX_EXP
)
506 get_zero (r
, r
->sign
);
509 SET_REAL_EXP (r
, exp
);
510 lshift_significand (r
, r
, shift
);
515 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
516 result may be inexact due to a loss of precision. */
519 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
520 const REAL_VALUE_TYPE
*b
, int subtract_p
)
524 bool inexact
= false;
526 /* Determine if we need to add or subtract. */
528 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
530 switch (CLASS2 (a
->cl
, b
->cl
))
532 case CLASS2 (rvc_zero
, rvc_zero
):
533 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
534 get_zero (r
, sign
& !subtract_p
);
537 case CLASS2 (rvc_zero
, rvc_normal
):
538 case CLASS2 (rvc_zero
, rvc_inf
):
539 case CLASS2 (rvc_zero
, rvc_nan
):
541 case CLASS2 (rvc_normal
, rvc_nan
):
542 case CLASS2 (rvc_inf
, rvc_nan
):
543 case CLASS2 (rvc_nan
, rvc_nan
):
544 /* ANY + NaN = NaN. */
545 case CLASS2 (rvc_normal
, rvc_inf
):
548 r
->sign
= sign
^ subtract_p
;
551 case CLASS2 (rvc_normal
, rvc_zero
):
552 case CLASS2 (rvc_inf
, rvc_zero
):
553 case CLASS2 (rvc_nan
, rvc_zero
):
555 case CLASS2 (rvc_nan
, rvc_normal
):
556 case CLASS2 (rvc_nan
, rvc_inf
):
557 /* NaN + ANY = NaN. */
558 case CLASS2 (rvc_inf
, rvc_normal
):
563 case CLASS2 (rvc_inf
, rvc_inf
):
565 /* Inf - Inf = NaN. */
566 get_canonical_qnan (r
, 0);
568 /* Inf + Inf = Inf. */
572 case CLASS2 (rvc_normal
, rvc_normal
):
579 /* Swap the arguments such that A has the larger exponent. */
580 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
583 const REAL_VALUE_TYPE
*t
;
590 /* If the exponents are not identical, we need to shift the
591 significand of B down. */
594 /* If the exponents are too far apart, the significands
595 do not overlap, which makes the subtraction a noop. */
596 if (dexp
>= SIGNIFICAND_BITS
)
603 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
609 if (sub_significands (r
, a
, b
, inexact
))
611 /* We got a borrow out of the subtraction. That means that
612 A and B had the same exponent, and B had the larger
613 significand. We need to swap the sign and negate the
616 neg_significand (r
, r
);
621 if (add_significands (r
, a
, b
))
623 /* We got carry out of the addition. This means we need to
624 shift the significand back down one bit and increase the
626 inexact
|= sticky_rshift_significand (r
, r
, 1);
627 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
638 SET_REAL_EXP (r
, exp
);
639 /* Zero out the remaining fields. */
644 /* Re-normalize the result. */
647 /* Special case: if the subtraction results in zero, the result
649 if (r
->cl
== rvc_zero
)
652 r
->sig
[0] |= inexact
;
657 /* Calculate R = A * B. Return true if the result may be inexact. */
660 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
661 const REAL_VALUE_TYPE
*b
)
663 REAL_VALUE_TYPE u
, t
, *rr
;
664 unsigned int i
, j
, k
;
665 int sign
= a
->sign
^ b
->sign
;
666 bool inexact
= false;
668 switch (CLASS2 (a
->cl
, b
->cl
))
670 case CLASS2 (rvc_zero
, rvc_zero
):
671 case CLASS2 (rvc_zero
, rvc_normal
):
672 case CLASS2 (rvc_normal
, rvc_zero
):
673 /* +-0 * ANY = 0 with appropriate sign. */
677 case CLASS2 (rvc_zero
, rvc_nan
):
678 case CLASS2 (rvc_normal
, rvc_nan
):
679 case CLASS2 (rvc_inf
, rvc_nan
):
680 case CLASS2 (rvc_nan
, rvc_nan
):
681 /* ANY * NaN = NaN. */
686 case CLASS2 (rvc_nan
, rvc_zero
):
687 case CLASS2 (rvc_nan
, rvc_normal
):
688 case CLASS2 (rvc_nan
, rvc_inf
):
689 /* NaN * ANY = NaN. */
694 case CLASS2 (rvc_zero
, rvc_inf
):
695 case CLASS2 (rvc_inf
, rvc_zero
):
697 get_canonical_qnan (r
, sign
);
700 case CLASS2 (rvc_inf
, rvc_inf
):
701 case CLASS2 (rvc_normal
, rvc_inf
):
702 case CLASS2 (rvc_inf
, rvc_normal
):
703 /* Inf * Inf = Inf, R * Inf = Inf */
707 case CLASS2 (rvc_normal
, rvc_normal
):
714 if (r
== a
|| r
== b
)
720 /* Collect all the partial products. Since we don't have sure access
721 to a widening multiply, we split each long into two half-words.
723 Consider the long-hand form of a four half-word multiplication:
733 We construct partial products of the widened half-word products
734 that are known to not overlap, e.g. DF+DH. Each such partial
735 product is given its proper exponent, which allows us to sum them
736 and obtain the finished product. */
738 for (i
= 0; i
< SIGSZ
* 2; ++i
)
740 unsigned long ai
= a
->sig
[i
/ 2];
742 ai
>>= HOST_BITS_PER_LONG
/ 2;
744 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
749 for (j
= 0; j
< 2; ++j
)
751 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
752 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
761 /* Would underflow to zero, which we shouldn't bother adding. */
766 memset (&u
, 0, sizeof (u
));
768 SET_REAL_EXP (&u
, exp
);
770 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
772 unsigned long bi
= b
->sig
[k
/ 2];
774 bi
>>= HOST_BITS_PER_LONG
/ 2;
776 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
778 u
.sig
[k
/ 2] = ai
* bi
;
782 inexact
|= do_add (rr
, rr
, &u
, 0);
793 /* Calculate R = A / B. Return true if the result may be inexact. */
796 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
797 const REAL_VALUE_TYPE
*b
)
799 int exp
, sign
= a
->sign
^ b
->sign
;
800 REAL_VALUE_TYPE t
, *rr
;
803 switch (CLASS2 (a
->cl
, b
->cl
))
805 case CLASS2 (rvc_zero
, rvc_zero
):
807 case CLASS2 (rvc_inf
, rvc_inf
):
808 /* Inf / Inf = NaN. */
809 get_canonical_qnan (r
, sign
);
812 case CLASS2 (rvc_zero
, rvc_normal
):
813 case CLASS2 (rvc_zero
, rvc_inf
):
815 case CLASS2 (rvc_normal
, rvc_inf
):
820 case CLASS2 (rvc_normal
, rvc_zero
):
822 case CLASS2 (rvc_inf
, rvc_zero
):
827 case CLASS2 (rvc_zero
, rvc_nan
):
828 case CLASS2 (rvc_normal
, rvc_nan
):
829 case CLASS2 (rvc_inf
, rvc_nan
):
830 case CLASS2 (rvc_nan
, rvc_nan
):
831 /* ANY / NaN = NaN. */
836 case CLASS2 (rvc_nan
, rvc_zero
):
837 case CLASS2 (rvc_nan
, rvc_normal
):
838 case CLASS2 (rvc_nan
, rvc_inf
):
839 /* NaN / ANY = NaN. */
844 case CLASS2 (rvc_inf
, rvc_normal
):
849 case CLASS2 (rvc_normal
, rvc_normal
):
856 if (r
== a
|| r
== b
)
861 /* Make sure all fields in the result are initialized. */
866 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
877 SET_REAL_EXP (rr
, exp
);
879 inexact
= div_significands (rr
, a
, b
);
881 /* Re-normalize the result. */
883 rr
->sig
[0] |= inexact
;
891 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
892 one of the two operands is a NaN. */
895 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
900 switch (CLASS2 (a
->cl
, b
->cl
))
902 case CLASS2 (rvc_zero
, rvc_zero
):
903 /* Sign of zero doesn't matter for compares. */
906 case CLASS2 (rvc_normal
, rvc_zero
):
907 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
909 return decimal_do_compare (a
, b
, nan_result
);
911 case CLASS2 (rvc_inf
, rvc_zero
):
912 case CLASS2 (rvc_inf
, rvc_normal
):
913 return (a
->sign
? -1 : 1);
915 case CLASS2 (rvc_inf
, rvc_inf
):
916 return -a
->sign
- -b
->sign
;
918 case CLASS2 (rvc_zero
, rvc_normal
):
919 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
921 return decimal_do_compare (a
, b
, nan_result
);
923 case CLASS2 (rvc_zero
, rvc_inf
):
924 case CLASS2 (rvc_normal
, rvc_inf
):
925 return (b
->sign
? 1 : -1);
927 case CLASS2 (rvc_zero
, rvc_nan
):
928 case CLASS2 (rvc_normal
, rvc_nan
):
929 case CLASS2 (rvc_inf
, rvc_nan
):
930 case CLASS2 (rvc_nan
, rvc_nan
):
931 case CLASS2 (rvc_nan
, rvc_zero
):
932 case CLASS2 (rvc_nan
, rvc_normal
):
933 case CLASS2 (rvc_nan
, rvc_inf
):
936 case CLASS2 (rvc_normal
, rvc_normal
):
943 if (a
->sign
!= b
->sign
)
944 return -a
->sign
- -b
->sign
;
946 if (a
->decimal
|| b
->decimal
)
947 return decimal_do_compare (a
, b
, nan_result
);
949 if (REAL_EXP (a
) > REAL_EXP (b
))
951 else if (REAL_EXP (a
) < REAL_EXP (b
))
954 ret
= cmp_significands (a
, b
);
956 return (a
->sign
? -ret
: ret
);
959 /* Return A truncated to an integral value toward zero. */
962 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
976 decimal_do_fix_trunc (r
, a
);
979 if (REAL_EXP (r
) <= 0)
980 get_zero (r
, r
->sign
);
981 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
982 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
990 /* Perform the binary or unary operation described by CODE.
991 For a unary operation, leave OP1 NULL. This function returns
992 true if the result may be inexact due to loss of precision. */
995 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
996 const REAL_VALUE_TYPE
*op1
)
998 enum tree_code code
= (enum tree_code
) icode
;
1000 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1001 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1006 /* Clear any padding areas in *r if it isn't equal to one of the
1007 operands so that we can later do bitwise comparisons later on. */
1008 if (r
!= op0
&& r
!= op1
)
1009 memset (r
, '\0', sizeof (*r
));
1010 return do_add (r
, op0
, op1
, 0);
1013 if (r
!= op0
&& r
!= op1
)
1014 memset (r
, '\0', sizeof (*r
));
1015 return do_add (r
, op0
, op1
, 1);
1018 if (r
!= op0
&& r
!= op1
)
1019 memset (r
, '\0', sizeof (*r
));
1020 return do_multiply (r
, op0
, op1
);
1023 if (r
!= op0
&& r
!= op1
)
1024 memset (r
, '\0', sizeof (*r
));
1025 return do_divide (r
, op0
, op1
);
1028 if (op1
->cl
== rvc_nan
)
1030 else if (do_compare (op0
, op1
, -1) < 0)
1037 if (op1
->cl
== rvc_nan
)
1039 else if (do_compare (op0
, op1
, 1) < 0)
1055 case FIX_TRUNC_EXPR
:
1056 do_fix_trunc (r
, op0
);
1066 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1069 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1074 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1077 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1082 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1083 const REAL_VALUE_TYPE
*op1
)
1085 enum tree_code code
= (enum tree_code
) icode
;
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;
1100 return do_compare (op0
, op1
, -1) != 0;
1101 case UNORDERED_EXPR
:
1102 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1104 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
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
, 1) >= 0;
1114 return do_compare (op0
, op1
, 0) == 0;
1116 return do_compare (op0
, op1
, 0) != 0;
1123 /* Return floor log2(R). */
1126 real_exponent (const REAL_VALUE_TYPE
*r
)
1134 return (unsigned int)-1 >> 1;
1136 return REAL_EXP (r
);
1142 /* R = OP0 * 2**EXP. */
1145 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1156 exp
+= REAL_EXP (op0
);
1158 get_inf (r
, r
->sign
);
1159 else if (exp
< -MAX_EXP
)
1160 get_zero (r
, r
->sign
);
1162 SET_REAL_EXP (r
, exp
);
1170 /* Determine whether a floating-point value X is infinite. */
1173 real_isinf (const REAL_VALUE_TYPE
*r
)
1175 return (r
->cl
== rvc_inf
);
1178 /* Determine whether a floating-point value X is a NaN. */
1181 real_isnan (const REAL_VALUE_TYPE
*r
)
1183 return (r
->cl
== rvc_nan
);
1186 /* Determine whether a floating-point value X is finite. */
1189 real_isfinite (const REAL_VALUE_TYPE
*r
)
1191 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1194 /* Determine whether a floating-point value X is negative. */
1197 real_isneg (const REAL_VALUE_TYPE
*r
)
1202 /* Determine whether a floating-point value X is minus zero. */
1205 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1207 return r
->sign
&& r
->cl
== rvc_zero
;
1210 /* Compare two floating-point objects for bitwise identity. */
1213 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1219 if (a
->sign
!= b
->sign
)
1229 if (a
->decimal
!= b
->decimal
)
1231 if (REAL_EXP (a
) != REAL_EXP (b
))
1236 if (a
->signalling
!= b
->signalling
)
1238 /* The significand is ignored for canonical NaNs. */
1239 if (a
->canonical
|| b
->canonical
)
1240 return a
->canonical
== b
->canonical
;
1247 for (i
= 0; i
< SIGSZ
; ++i
)
1248 if (a
->sig
[i
] != b
->sig
[i
])
1254 /* Try to change R into its exact multiplicative inverse in machine
1255 mode MODE. Return true if successful. */
1258 exact_real_inverse (machine_mode mode
, REAL_VALUE_TYPE
*r
)
1260 const REAL_VALUE_TYPE
*one
= real_digit (1);
1264 if (r
->cl
!= rvc_normal
)
1267 /* Check for a power of two: all significand bits zero except the MSB. */
1268 for (i
= 0; i
< SIGSZ
-1; ++i
)
1271 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1274 /* Find the inverse and truncate to the required mode. */
1275 do_divide (&u
, one
, r
);
1276 real_convert (&u
, mode
, &u
);
1278 /* The rounding may have overflowed. */
1279 if (u
.cl
!= rvc_normal
)
1281 for (i
= 0; i
< SIGSZ
-1; ++i
)
1284 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1291 /* Return true if arithmetic on values in IMODE that were promoted
1292 from values in TMODE is equivalent to direct arithmetic on values
1296 real_can_shorten_arithmetic (machine_mode imode
, machine_mode tmode
)
1298 const struct real_format
*tfmt
, *ifmt
;
1299 tfmt
= REAL_MODE_FORMAT (tmode
);
1300 ifmt
= REAL_MODE_FORMAT (imode
);
1301 /* These conditions are conservative rather than trying to catch the
1302 exact boundary conditions; the main case to allow is IEEE float
1304 return (ifmt
->b
== tfmt
->b
1305 && ifmt
->p
> 2 * tfmt
->p
1306 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1307 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1308 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1309 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1310 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1311 && (ifmt
->has_sign_dependent_rounding
1312 == tfmt
->has_sign_dependent_rounding
)
1313 && ifmt
->has_nans
>= tfmt
->has_nans
1314 && ifmt
->has_inf
>= tfmt
->has_inf
1315 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1316 && !MODE_COMPOSITE_P (tmode
)
1317 && !MODE_COMPOSITE_P (imode
));
1320 /* Render R as an integer. */
1323 real_to_integer (const REAL_VALUE_TYPE
*r
)
1325 unsigned HOST_WIDE_INT i
;
1336 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1343 return decimal_real_to_integer (r
);
1345 if (REAL_EXP (r
) <= 0)
1347 /* Only force overflow for unsigned overflow. Signed overflow is
1348 undefined, so it doesn't matter what we return, and some callers
1349 expect to be able to use this routine for both signed and
1350 unsigned conversions. */
1351 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1354 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1355 i
= r
->sig
[SIGSZ
-1];
1358 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1359 i
= r
->sig
[SIGSZ
-1];
1360 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1361 i
|= r
->sig
[SIGSZ
-2];
1364 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1375 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1376 be represented in precision, *FAIL is set to TRUE. */
1379 real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
1381 HOST_WIDE_INT val
[2 * WIDE_INT_MAX_ELTS
];
1390 return wi::zero (precision
);
1398 return wi::set_bit_in_zero (precision
- 1, precision
);
1400 return ~wi::set_bit_in_zero (precision
- 1, precision
);
1404 return decimal_real_to_integer (r
, fail
, precision
);
1409 /* Only force overflow for unsigned overflow. Signed overflow is
1410 undefined, so it doesn't matter what we return, and some callers
1411 expect to be able to use this routine for both signed and
1412 unsigned conversions. */
1413 if (exp
> precision
)
1416 /* Put the significand into a wide_int that has precision W, which
1417 is the smallest HWI-multiple that has at least PRECISION bits.
1418 This ensures that the top bit of the significand is in the
1419 top bit of the wide_int. */
1420 words
= (precision
+ HOST_BITS_PER_WIDE_INT
- 1) / HOST_BITS_PER_WIDE_INT
;
1421 w
= words
* HOST_BITS_PER_WIDE_INT
;
1423 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1424 for (int i
= 0; i
< words
; i
++)
1426 int j
= SIGSZ
- words
+ i
;
1427 val
[i
] = (j
< 0) ? 0 : r
->sig
[j
];
1430 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1431 for (int i
= 0; i
< words
; i
++)
1433 int j
= SIGSZ
- (words
* 2) + (i
* 2);
1440 val
[i
] |= (unsigned HOST_WIDE_INT
) r
->sig
[j
] << HOST_BITS_PER_LONG
;
1443 /* Shift the value into place and truncate to the desired precision. */
1444 result
= wide_int::from_array (val
, words
, w
);
1445 result
= wi::lrshift (result
, w
- exp
);
1446 result
= wide_int::from (result
, precision
, UNSIGNED
);
1458 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1459 of NUM / DEN. Return the quotient and place the remainder in NUM.
1460 It is expected that NUM / DEN are close enough that the quotient is
1463 static unsigned long
1464 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1466 unsigned long q
, msb
;
1467 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1476 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1478 lshift_significand_1 (num
, num
);
1480 if (msb
|| cmp_significands (num
, den
) >= 0)
1482 sub_significands (num
, num
, den
, 0);
1486 while (--expn
>= expd
);
1488 SET_REAL_EXP (num
, expd
);
1494 /* Render R as a decimal floating point constant. Emit DIGITS significant
1495 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1496 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1497 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1498 to a string that, when parsed back in mode MODE, yields the same value. */
1500 #define M_LOG10_2 0.30102999566398119521
1503 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1504 size_t buf_size
, size_t digits
,
1505 int crop_trailing_zeros
, machine_mode mode
)
1507 const struct real_format
*fmt
= NULL
;
1508 const REAL_VALUE_TYPE
*one
, *ten
;
1509 REAL_VALUE_TYPE r
, pten
, u
, v
;
1510 int dec_exp
, cmp_one
, digit
;
1512 char *p
, *first
, *last
;
1516 if (mode
!= VOIDmode
)
1518 fmt
= REAL_MODE_FORMAT (mode
);
1526 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1531 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1534 /* ??? Print the significand as well, if not canonical? */
1535 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1536 (r_orig
->signalling
? 'S' : 'Q'));
1544 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1548 /* Bound the number of digits printed by the size of the representation. */
1549 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1550 if (digits
== 0 || digits
> max_digits
)
1551 digits
= max_digits
;
1553 /* Estimate the decimal exponent, and compute the length of the string it
1554 will print as. Be conservative and add one to account for possible
1555 overflow or rounding error. */
1556 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1557 for (max_digits
= 1; dec_exp
; max_digits
++)
1560 /* Bound the number of digits printed by the size of the output buffer. */
1561 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1562 gcc_assert (max_digits
<= buf_size
);
1563 if (digits
> max_digits
)
1564 digits
= max_digits
;
1566 one
= real_digit (1);
1567 ten
= ten_to_ptwo (0);
1575 cmp_one
= do_compare (&r
, one
, 0);
1580 /* Number is greater than one. Convert significand to an integer
1581 and strip trailing decimal zeros. */
1584 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1586 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1587 m
= floor_log2 (max_digits
);
1589 /* Iterate over the bits of the possible powers of 10 that might
1590 be present in U and eliminate them. That is, if we find that
1591 10**2**M divides U evenly, keep the division and increase
1597 do_divide (&t
, &u
, ten_to_ptwo (m
));
1598 do_fix_trunc (&v
, &t
);
1599 if (cmp_significands (&v
, &t
) == 0)
1607 /* Revert the scaling to integer that we performed earlier. */
1608 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1609 - (SIGNIFICAND_BITS
- 1));
1612 /* Find power of 10. Do this by dividing out 10**2**M when
1613 this is larger than the current remainder. Fill PTEN with
1614 the power of 10 that we compute. */
1615 if (REAL_EXP (&r
) > 0)
1617 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1620 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1621 if (do_compare (&u
, ptentwo
, 0) >= 0)
1623 do_divide (&u
, &u
, ptentwo
);
1624 do_multiply (&pten
, &pten
, ptentwo
);
1631 /* We managed to divide off enough tens in the above reduction
1632 loop that we've now got a negative exponent. Fall into the
1633 less-than-one code to compute the proper value for PTEN. */
1640 /* Number is less than one. Pad significand with leading
1646 /* Stop if we'd shift bits off the bottom. */
1650 do_multiply (&u
, &v
, ten
);
1652 /* Stop if we're now >= 1. */
1653 if (REAL_EXP (&u
) > 0)
1661 /* Find power of 10. Do this by multiplying in P=10**2**M when
1662 the current remainder is smaller than 1/P. Fill PTEN with the
1663 power of 10 that we compute. */
1664 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1667 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1668 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1670 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1672 do_multiply (&v
, &v
, ptentwo
);
1673 do_multiply (&pten
, &pten
, ptentwo
);
1679 /* Invert the positive power of 10 that we've collected so far. */
1680 do_divide (&pten
, one
, &pten
);
1688 /* At this point, PTEN should contain the nearest power of 10 smaller
1689 than R, such that this division produces the first digit.
1691 Using a divide-step primitive that returns the complete integral
1692 remainder avoids the rounding error that would be produced if
1693 we were to use do_divide here and then simply multiply by 10 for
1694 each subsequent digit. */
1696 digit
= rtd_divmod (&r
, &pten
);
1698 /* Be prepared for error in that division via underflow ... */
1699 if (digit
== 0 && cmp_significand_0 (&r
))
1701 /* Multiply by 10 and try again. */
1702 do_multiply (&r
, &r
, ten
);
1703 digit
= rtd_divmod (&r
, &pten
);
1705 gcc_assert (digit
!= 0);
1708 /* ... or overflow. */
1718 gcc_assert (digit
<= 10);
1722 /* Generate subsequent digits. */
1723 while (--digits
> 0)
1725 do_multiply (&r
, &r
, ten
);
1726 digit
= rtd_divmod (&r
, &pten
);
1731 /* Generate one more digit with which to do rounding. */
1732 do_multiply (&r
, &r
, ten
);
1733 digit
= rtd_divmod (&r
, &pten
);
1735 /* Round the result. */
1736 if (fmt
&& fmt
->round_towards_zero
)
1738 /* If the format uses round towards zero when parsing the string
1739 back in, we need to always round away from zero here. */
1740 if (cmp_significand_0 (&r
))
1742 round_up
= digit
> 0;
1748 /* Round to nearest. If R is nonzero there are additional
1749 nonzero digits to be extracted. */
1750 if (cmp_significand_0 (&r
))
1752 /* Round to even. */
1753 else if ((p
[-1] - '0') & 1)
1757 round_up
= digit
> 5;
1774 /* Carry out of the first digit. This means we had all 9's and
1775 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1783 /* Insert the decimal point. */
1784 first
[0] = first
[1];
1787 /* If requested, drop trailing zeros. Never crop past "1.0". */
1788 if (crop_trailing_zeros
)
1789 while (last
> first
+ 3 && last
[-1] == '0')
1792 /* Append the exponent. */
1793 sprintf (last
, "e%+d", dec_exp
);
1795 #ifdef ENABLE_CHECKING
1796 /* Verify that we can read the original value back in. */
1797 if (mode
!= VOIDmode
)
1799 real_from_string (&r
, str
);
1800 real_convert (&r
, mode
, &r
);
1801 gcc_assert (real_identical (&r
, r_orig
));
1806 /* Likewise, except always uses round-to-nearest. */
1809 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1810 size_t digits
, int crop_trailing_zeros
)
1812 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1813 digits
, crop_trailing_zeros
, VOIDmode
);
1816 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1817 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1818 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1819 strip trailing zeros. */
1822 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1823 size_t digits
, int crop_trailing_zeros
)
1825 int i
, j
, exp
= REAL_EXP (r
);
1838 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1841 /* ??? Print the significand as well, if not canonical? */
1842 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1843 (r
->signalling
? 'S' : 'Q'));
1851 /* Hexadecimal format for decimal floats is not interesting. */
1852 strcpy (str
, "N/A");
1857 digits
= SIGNIFICAND_BITS
/ 4;
1859 /* Bound the number of digits printed by the size of the output buffer. */
1861 sprintf (exp_buf
, "p%+d", exp
);
1862 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1863 gcc_assert (max_digits
<= buf_size
);
1864 if (digits
> max_digits
)
1865 digits
= max_digits
;
1876 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1877 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1879 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1885 if (crop_trailing_zeros
)
1886 while (p
> first
+ 1 && p
[-1] == '0')
1889 sprintf (p
, "p%+d", exp
);
1892 /* Initialize R from a decimal or hexadecimal string. The string is
1893 assumed to have been syntax checked already. Return -1 if the
1894 value underflows, +1 if overflows, and 0 otherwise. */
1897 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1909 else if (*str
== '+')
1912 if (!strncmp (str
, "QNaN", 4))
1914 get_canonical_qnan (r
, sign
);
1917 else if (!strncmp (str
, "SNaN", 4))
1919 get_canonical_snan (r
, sign
);
1922 else if (!strncmp (str
, "Inf", 3))
1928 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1930 /* Hexadecimal floating point. */
1931 int pos
= SIGNIFICAND_BITS
- 4, d
;
1939 d
= hex_value (*str
);
1944 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1945 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1949 /* Ensure correct rounding by setting last bit if there is
1950 a subsequent nonzero digit. */
1958 if (pos
== SIGNIFICAND_BITS
- 4)
1965 d
= hex_value (*str
);
1970 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1971 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1975 /* Ensure correct rounding by setting last bit if there is
1976 a subsequent nonzero digit. */
1982 /* If the mantissa is zero, ignore the exponent. */
1983 if (!cmp_significand_0 (r
))
1986 if (*str
== 'p' || *str
== 'P')
1988 bool exp_neg
= false;
1996 else if (*str
== '+')
2000 while (ISDIGIT (*str
))
2006 /* Overflowed the exponent. */
2021 SET_REAL_EXP (r
, exp
);
2027 /* Decimal floating point. */
2028 const char *cstr
= str
;
2032 while (*cstr
== '0')
2037 while (*cstr
== '0')
2041 /* If the mantissa is zero, ignore the exponent. */
2042 if (!ISDIGIT (*cstr
))
2045 /* Nonzero value, possibly overflowing or underflowing. */
2046 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2047 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, GMP_RNDZ
);
2048 /* The result should never be a NaN, and because the rounding is
2049 toward zero should never be an infinity. */
2050 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2051 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2056 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2063 real_from_mpfr (r
, m
, NULL_TREE
, GMP_RNDZ
);
2064 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2065 because the hex digits used in real_from_mpfr did not
2066 start with a digit 8 to f, but the exponent bounds above
2067 should have avoided underflow or overflow. */
2068 gcc_assert (r
->cl
== rvc_normal
);
2069 /* Set a sticky bit if mpfr_strtofr was inexact. */
2070 r
->sig
[0] |= inexact
;
2091 /* Legacy. Similar, but return the result directly. */
2094 real_from_string2 (const char *s
, machine_mode mode
)
2098 real_from_string (&r
, s
);
2099 if (mode
!= VOIDmode
)
2100 real_convert (&r
, mode
, &r
);
2105 /* Initialize R from string S and desired MODE. */
2108 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, machine_mode mode
)
2110 if (DECIMAL_FLOAT_MODE_P (mode
))
2111 decimal_real_from_string (r
, s
);
2113 real_from_string (r
, s
);
2115 if (mode
!= VOIDmode
)
2116 real_convert (r
, mode
, r
);
2119 /* Initialize R from the wide_int VAL_IN. The MODE is not VOIDmode,*/
2122 real_from_integer (REAL_VALUE_TYPE
*r
, machine_mode mode
,
2123 const wide_int_ref
&val_in
, signop sgn
)
2129 unsigned int len
= val_in
.get_precision ();
2131 int maxbitlen
= MAX_BITSIZE_MODE_ANY_INT
+ HOST_BITS_PER_WIDE_INT
;
2132 const unsigned int realmax
= (SIGNIFICAND_BITS
/ HOST_BITS_PER_WIDE_INT
2133 * HOST_BITS_PER_WIDE_INT
);
2135 memset (r
, 0, sizeof (*r
));
2137 r
->sign
= wi::neg_p (val_in
, sgn
);
2139 /* We have to ensure we can negate the largest negative number. */
2140 wide_int val
= wide_int::from (val_in
, maxbitlen
, sgn
);
2145 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2146 won't work with precisions that are not a multiple of
2147 HOST_BITS_PER_WIDE_INT. */
2148 len
+= HOST_BITS_PER_WIDE_INT
- 1;
2150 /* Ensure we can represent the largest negative number. */
2153 len
= len
/HOST_BITS_PER_WIDE_INT
* HOST_BITS_PER_WIDE_INT
;
2155 /* Cap the size to the size allowed by real.h. */
2158 HOST_WIDE_INT cnt_l_z
;
2159 cnt_l_z
= wi::clz (val
);
2161 if (maxbitlen
- cnt_l_z
> realmax
)
2163 e
= maxbitlen
- cnt_l_z
- realmax
;
2165 /* This value is too large, we must shift it right to
2166 preserve all the bits we can, and then bump the
2167 exponent up by that amount. */
2168 val
= wi::lrshift (val
, e
);
2173 /* Clear out top bits so elt will work with precisions that aren't
2174 a multiple of HOST_BITS_PER_WIDE_INT. */
2175 val
= wide_int::from (val
, len
, sgn
);
2176 len
= len
/ HOST_BITS_PER_WIDE_INT
;
2178 SET_REAL_EXP (r
, len
* HOST_BITS_PER_WIDE_INT
+ e
);
2181 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2182 for (i
= len
- 1; i
>= 0; i
--)
2184 r
->sig
[j
--] = val
.elt (i
);
2190 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2191 for (i
= len
- 1; i
>= 0; i
--)
2193 HOST_WIDE_INT e
= val
.elt (i
);
2194 r
->sig
[j
--] = e
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2206 if (DECIMAL_FLOAT_MODE_P (mode
))
2207 decimal_from_integer (r
);
2208 else if (mode
!= VOIDmode
)
2209 real_convert (r
, mode
, r
);
2212 /* Render R, an integral value, as a floating point constant with no
2213 specified exponent. */
2216 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2219 int dec_exp
, digit
, digits
;
2220 REAL_VALUE_TYPE r
, pten
;
2226 if (r
.cl
== rvc_zero
)
2235 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2236 digits
= dec_exp
+ 1;
2237 gcc_assert ((digits
+ 2) < (int)buf_size
);
2239 pten
= *real_digit (1);
2240 times_pten (&pten
, dec_exp
);
2246 digit
= rtd_divmod (&r
, &pten
);
2247 gcc_assert (digit
>= 0 && digit
<= 9);
2249 while (--digits
> 0)
2252 digit
= rtd_divmod (&r
, &pten
);
2259 /* Convert a real with an integral value to decimal float. */
2262 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2266 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2267 decimal_real_from_string (r
, str
);
2270 /* Returns 10**2**N. */
2272 static const REAL_VALUE_TYPE
*
2275 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2277 gcc_assert (n
>= 0);
2278 gcc_assert (n
< EXP_BITS
);
2280 if (tens
[n
].cl
== rvc_zero
)
2282 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2284 HOST_WIDE_INT t
= 10;
2287 for (i
= 0; i
< n
; ++i
)
2290 real_from_integer (&tens
[n
], VOIDmode
, t
, UNSIGNED
);
2294 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2295 do_multiply (&tens
[n
], t
, t
);
2302 /* Returns 10**(-2**N). */
2304 static const REAL_VALUE_TYPE
*
2305 ten_to_mptwo (int n
)
2307 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2309 gcc_assert (n
>= 0);
2310 gcc_assert (n
< EXP_BITS
);
2312 if (tens
[n
].cl
== rvc_zero
)
2313 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2320 static const REAL_VALUE_TYPE
*
2323 static REAL_VALUE_TYPE num
[10];
2325 gcc_assert (n
>= 0);
2326 gcc_assert (n
<= 9);
2328 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2329 real_from_integer (&num
[n
], VOIDmode
, n
, UNSIGNED
);
2334 /* Multiply R by 10**EXP. */
2337 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2339 REAL_VALUE_TYPE pten
, *rr
;
2340 bool negative
= (exp
< 0);
2346 pten
= *real_digit (1);
2352 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2354 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2357 do_divide (r
, r
, &pten
);
2360 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2362 const REAL_VALUE_TYPE
*
2365 static REAL_VALUE_TYPE value
;
2367 /* Initialize mathematical constants for constant folding builtins.
2368 These constants need to be given to at least 160 bits precision. */
2369 if (value
.cl
== rvc_zero
)
2372 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2373 mpfr_set_ui (m
, 1, GMP_RNDN
);
2374 mpfr_exp (m
, m
, GMP_RNDN
);
2375 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2382 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2384 const REAL_VALUE_TYPE
*
2385 dconst_third_ptr (void)
2387 static REAL_VALUE_TYPE value
;
2389 /* Initialize mathematical constants for constant folding builtins.
2390 These constants need to be given to at least 160 bits precision. */
2391 if (value
.cl
== rvc_zero
)
2393 real_arithmetic (&value
, RDIV_EXPR
, &dconst1
, real_digit (3));
2398 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2400 const REAL_VALUE_TYPE
*
2401 dconst_sqrt2_ptr (void)
2403 static REAL_VALUE_TYPE value
;
2405 /* Initialize mathematical constants for constant folding builtins.
2406 These constants need to be given to at least 160 bits precision. */
2407 if (value
.cl
== rvc_zero
)
2410 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2411 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2412 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2418 /* Fills R with +Inf. */
2421 real_inf (REAL_VALUE_TYPE
*r
)
2426 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2427 we force a QNaN, else we force an SNaN. The string, if not empty,
2428 is parsed as a number and placed in the significand. Return true
2429 if the string was successfully parsed. */
2432 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2435 const struct real_format
*fmt
;
2437 fmt
= REAL_MODE_FORMAT (mode
);
2443 get_canonical_qnan (r
, 0);
2445 get_canonical_snan (r
, 0);
2451 memset (r
, 0, sizeof (*r
));
2454 /* Parse akin to strtol into the significand of R. */
2456 while (ISSPACE (*str
))
2460 else if (*str
== '+')
2465 if (*str
== 'x' || *str
== 'X')
2474 while ((d
= hex_value (*str
)) < base
)
2481 lshift_significand (r
, r
, 3);
2484 lshift_significand (r
, r
, 4);
2487 lshift_significand_1 (&u
, r
);
2488 lshift_significand (r
, r
, 3);
2489 add_significands (r
, r
, &u
);
2497 add_significands (r
, r
, &u
);
2502 /* Must have consumed the entire string for success. */
2506 /* Shift the significand into place such that the bits
2507 are in the most significant bits for the format. */
2508 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2510 /* Our MSB is always unset for NaNs. */
2511 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2513 /* Force quiet or signalling NaN. */
2514 r
->signalling
= !quiet
;
2520 /* Fills R with the largest finite value representable in mode MODE.
2521 If SIGN is nonzero, R is set to the most negative finite value. */
2524 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
2526 const struct real_format
*fmt
;
2529 fmt
= REAL_MODE_FORMAT (mode
);
2531 memset (r
, 0, sizeof (*r
));
2534 decimal_real_maxval (r
, sign
, mode
);
2539 SET_REAL_EXP (r
, fmt
->emax
);
2541 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2542 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2543 clear_significand_below (r
, np2
);
2545 if (fmt
->pnan
< fmt
->p
)
2546 /* This is an IBM extended double format made up of two IEEE
2547 doubles. The value of the long double is the sum of the
2548 values of the two parts. The most significant part is
2549 required to be the value of the long double rounded to the
2550 nearest double. Rounding means we need a slightly smaller
2551 value for LDBL_MAX. */
2552 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2556 /* Fills R with 2**N. */
2559 real_2expN (REAL_VALUE_TYPE
*r
, int n
, machine_mode fmode
)
2561 memset (r
, 0, sizeof (*r
));
2566 else if (n
< -MAX_EXP
)
2571 SET_REAL_EXP (r
, n
);
2572 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2574 if (DECIMAL_FLOAT_MODE_P (fmode
))
2575 decimal_real_convert (r
, fmode
, r
);
2580 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2584 bool round_up
= false;
2590 decimal_round_for_format (fmt
, r
);
2593 /* FIXME. We can come here via fp_easy_constant
2594 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2595 investigated whether this convert needs to be here, or
2596 something else is missing. */
2597 decimal_real_convert (r
, DFmode
, r
);
2601 emin2m1
= fmt
->emin
- 1;
2604 np2
= SIGNIFICAND_BITS
- p2
;
2608 get_zero (r
, r
->sign
);
2610 if (!fmt
->has_signed_zero
)
2615 get_inf (r
, r
->sign
);
2620 clear_significand_below (r
, np2
);
2630 /* Check the range of the exponent. If we're out of range,
2631 either underflow or overflow. */
2632 if (REAL_EXP (r
) > emax2
)
2634 else if (REAL_EXP (r
) <= emin2m1
)
2638 if (!fmt
->has_denorm
)
2640 /* Don't underflow completely until we've had a chance to round. */
2641 if (REAL_EXP (r
) < emin2m1
)
2646 diff
= emin2m1
- REAL_EXP (r
) + 1;
2650 /* De-normalize the significand. */
2651 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2652 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2656 if (!fmt
->round_towards_zero
)
2658 /* There are P2 true significand bits, followed by one guard bit,
2659 followed by one sticky bit, followed by stuff. Fold nonzero
2660 stuff into the sticky bit. */
2661 unsigned long sticky
;
2665 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2666 sticky
|= r
->sig
[i
];
2668 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2670 guard
= test_significand_bit (r
, np2
- 1);
2671 lsb
= test_significand_bit (r
, np2
);
2673 /* Round to even. */
2674 round_up
= guard
&& (sticky
|| lsb
);
2681 set_significand_bit (&u
, np2
);
2683 if (add_significands (r
, r
, &u
))
2685 /* Overflow. Means the significand had been all ones, and
2686 is now all zeros. Need to increase the exponent, and
2687 possibly re-normalize it. */
2688 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2689 if (REAL_EXP (r
) > emax2
)
2691 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2695 /* Catch underflow that we deferred until after rounding. */
2696 if (REAL_EXP (r
) <= emin2m1
)
2699 /* Clear out trailing garbage. */
2700 clear_significand_below (r
, np2
);
2703 /* Extend or truncate to a new mode. */
2706 real_convert (REAL_VALUE_TYPE
*r
, machine_mode mode
,
2707 const REAL_VALUE_TYPE
*a
)
2709 const struct real_format
*fmt
;
2711 fmt
= REAL_MODE_FORMAT (mode
);
2716 if (a
->decimal
|| fmt
->b
== 10)
2717 decimal_real_convert (r
, mode
, a
);
2719 round_for_format (fmt
, r
);
2721 /* round_for_format de-normalizes denormals. Undo just that part. */
2722 if (r
->cl
== rvc_normal
)
2726 /* Legacy. Likewise, except return the struct directly. */
2729 real_value_truncate (machine_mode mode
, REAL_VALUE_TYPE a
)
2732 real_convert (&r
, mode
, &a
);
2736 /* Return true if truncating to MODE is exact. */
2739 exact_real_truncate (machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2741 const struct real_format
*fmt
;
2745 fmt
= REAL_MODE_FORMAT (mode
);
2748 /* Don't allow conversion to denormals. */
2749 emin2m1
= fmt
->emin
- 1;
2750 if (REAL_EXP (a
) <= emin2m1
)
2753 /* After conversion to the new mode, the value must be identical. */
2754 real_convert (&t
, mode
, a
);
2755 return real_identical (&t
, a
);
2758 /* Write R to the given target format. Place the words of the result
2759 in target word order in BUF. There are always 32 bits in each
2760 long, no matter the size of the host long.
2762 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2765 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2766 const struct real_format
*fmt
)
2772 round_for_format (fmt
, &r
);
2776 (*fmt
->encode
) (fmt
, buf
, &r
);
2781 /* Similar, but look up the format from MODE. */
2784 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, machine_mode mode
)
2786 const struct real_format
*fmt
;
2788 fmt
= REAL_MODE_FORMAT (mode
);
2791 return real_to_target_fmt (buf
, r
, fmt
);
2794 /* Read R from the given target format. Read the words of the result
2795 in target word order in BUF. There are always 32 bits in each
2796 long, no matter the size of the host long. */
2799 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2800 const struct real_format
*fmt
)
2802 (*fmt
->decode
) (fmt
, r
, buf
);
2805 /* Similar, but look up the format from MODE. */
2808 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, machine_mode mode
)
2810 const struct real_format
*fmt
;
2812 fmt
= REAL_MODE_FORMAT (mode
);
2815 (*fmt
->decode
) (fmt
, r
, buf
);
2818 /* Return the number of bits of the largest binary value that the
2819 significand of MODE will hold. */
2820 /* ??? Legacy. Should get access to real_format directly. */
2823 significand_size (machine_mode mode
)
2825 const struct real_format
*fmt
;
2827 fmt
= REAL_MODE_FORMAT (mode
);
2833 /* Return the size in bits of the largest binary value that can be
2834 held by the decimal coefficient for this mode. This is one more
2835 than the number of bits required to hold the largest coefficient
2837 double log2_10
= 3.3219281;
2838 return fmt
->p
* log2_10
;
2843 /* Return a hash value for the given real value. */
2844 /* ??? The "unsigned int" return value is intended to be hashval_t,
2845 but I didn't want to pull hashtab.h into real.h. */
2848 real_hash (const REAL_VALUE_TYPE
*r
)
2853 h
= r
->cl
| (r
->sign
<< 2);
2861 h
|= REAL_EXP (r
) << 3;
2866 h
^= (unsigned int)-1;
2875 if (sizeof (unsigned long) > sizeof (unsigned int))
2876 for (i
= 0; i
< SIGSZ
; ++i
)
2878 unsigned long s
= r
->sig
[i
];
2879 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2882 for (i
= 0; i
< SIGSZ
; ++i
)
2888 /* IEEE single-precision format. */
2890 static void encode_ieee_single (const struct real_format
*fmt
,
2891 long *, const REAL_VALUE_TYPE
*);
2892 static void decode_ieee_single (const struct real_format
*,
2893 REAL_VALUE_TYPE
*, const long *);
2896 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2897 const REAL_VALUE_TYPE
*r
)
2899 unsigned long image
, sig
, exp
;
2900 unsigned long sign
= r
->sign
;
2901 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2904 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2915 image
|= 0x7fffffff;
2922 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2923 if (r
->signalling
== fmt
->qnan_msb_set
)
2934 image
|= 0x7fffffff;
2938 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2939 whereas the intermediate representation is 0.F x 2**exp.
2940 Which means we're off by one. */
2944 exp
= REAL_EXP (r
) + 127 - 1;
2957 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2960 unsigned long image
= buf
[0] & 0xffffffff;
2961 bool sign
= (image
>> 31) & 1;
2962 int exp
= (image
>> 23) & 0xff;
2964 memset (r
, 0, sizeof (*r
));
2965 image
<<= HOST_BITS_PER_LONG
- 24;
2970 if (image
&& fmt
->has_denorm
)
2974 SET_REAL_EXP (r
, -126);
2975 r
->sig
[SIGSZ
-1] = image
<< 1;
2978 else if (fmt
->has_signed_zero
)
2981 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2987 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2988 ^ fmt
->qnan_msb_set
);
2989 r
->sig
[SIGSZ
-1] = image
;
3001 SET_REAL_EXP (r
, exp
- 127 + 1);
3002 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
3006 const struct real_format ieee_single_format
=
3028 const struct real_format mips_single_format
=
3050 const struct real_format motorola_single_format
=
3072 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3073 single precision with the following differences:
3074 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3076 - NaNs are not supported.
3077 - The range of non-zero numbers in binary is
3078 (001)[1.]000...000 to (255)[1.]111...111.
3079 - Denormals can be represented, but are treated as +0.0 when
3080 used as an operand and are never generated as a result.
3081 - -0.0 can be represented, but a zero result is always +0.0.
3082 - the only supported rounding mode is trunction (towards zero). */
3083 const struct real_format spu_single_format
=
3105 /* IEEE double-precision format. */
3107 static void encode_ieee_double (const struct real_format
*fmt
,
3108 long *, const REAL_VALUE_TYPE
*);
3109 static void decode_ieee_double (const struct real_format
*,
3110 REAL_VALUE_TYPE
*, const long *);
3113 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3114 const REAL_VALUE_TYPE
*r
)
3116 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3117 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3119 image_hi
= r
->sign
<< 31;
3122 if (HOST_BITS_PER_LONG
== 64)
3124 sig_hi
= r
->sig
[SIGSZ
-1];
3125 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3126 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3130 sig_hi
= r
->sig
[SIGSZ
-1];
3131 sig_lo
= r
->sig
[SIGSZ
-2];
3132 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3133 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3143 image_hi
|= 2047 << 20;
3146 image_hi
|= 0x7fffffff;
3147 image_lo
= 0xffffffff;
3156 if (fmt
->canonical_nan_lsbs_set
)
3158 sig_hi
= (1 << 19) - 1;
3159 sig_lo
= 0xffffffff;
3167 if (r
->signalling
== fmt
->qnan_msb_set
)
3168 sig_hi
&= ~(1 << 19);
3171 if (sig_hi
== 0 && sig_lo
== 0)
3174 image_hi
|= 2047 << 20;
3180 image_hi
|= 0x7fffffff;
3181 image_lo
= 0xffffffff;
3186 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3187 whereas the intermediate representation is 0.F x 2**exp.
3188 Which means we're off by one. */
3192 exp
= REAL_EXP (r
) + 1023 - 1;
3193 image_hi
|= exp
<< 20;
3202 if (FLOAT_WORDS_BIG_ENDIAN
)
3203 buf
[0] = image_hi
, buf
[1] = image_lo
;
3205 buf
[0] = image_lo
, buf
[1] = image_hi
;
3209 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3212 unsigned long image_hi
, image_lo
;
3216 if (FLOAT_WORDS_BIG_ENDIAN
)
3217 image_hi
= buf
[0], image_lo
= buf
[1];
3219 image_lo
= buf
[0], image_hi
= buf
[1];
3220 image_lo
&= 0xffffffff;
3221 image_hi
&= 0xffffffff;
3223 sign
= (image_hi
>> 31) & 1;
3224 exp
= (image_hi
>> 20) & 0x7ff;
3226 memset (r
, 0, sizeof (*r
));
3228 image_hi
<<= 32 - 21;
3229 image_hi
|= image_lo
>> 21;
3230 image_hi
&= 0x7fffffff;
3231 image_lo
<<= 32 - 21;
3235 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3239 SET_REAL_EXP (r
, -1022);
3240 if (HOST_BITS_PER_LONG
== 32)
3242 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3244 r
->sig
[SIGSZ
-1] = image_hi
;
3245 r
->sig
[SIGSZ
-2] = image_lo
;
3249 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3250 r
->sig
[SIGSZ
-1] = image_hi
;
3254 else if (fmt
->has_signed_zero
)
3257 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3259 if (image_hi
|| image_lo
)
3263 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3264 if (HOST_BITS_PER_LONG
== 32)
3266 r
->sig
[SIGSZ
-1] = image_hi
;
3267 r
->sig
[SIGSZ
-2] = image_lo
;
3270 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3282 SET_REAL_EXP (r
, exp
- 1023 + 1);
3283 if (HOST_BITS_PER_LONG
== 32)
3285 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3286 r
->sig
[SIGSZ
-2] = image_lo
;
3289 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3293 const struct real_format ieee_double_format
=
3315 const struct real_format mips_double_format
=
3337 const struct real_format motorola_double_format
=
3359 /* IEEE extended real format. This comes in three flavors: Intel's as
3360 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3361 12- and 16-byte images may be big- or little endian; Motorola's is
3362 always big endian. */
3364 /* Helper subroutine which converts from the internal format to the
3365 12-byte little-endian Intel format. Functions below adjust this
3366 for the other possible formats. */
3368 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3369 const REAL_VALUE_TYPE
*r
)
3371 unsigned long image_hi
, sig_hi
, sig_lo
;
3372 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3374 image_hi
= r
->sign
<< 15;
3375 sig_hi
= sig_lo
= 0;
3387 /* Intel requires the explicit integer bit to be set, otherwise
3388 it considers the value a "pseudo-infinity". Motorola docs
3389 say it doesn't care. */
3390 sig_hi
= 0x80000000;
3395 sig_lo
= sig_hi
= 0xffffffff;
3405 if (fmt
->canonical_nan_lsbs_set
)
3407 sig_hi
= (1 << 30) - 1;
3408 sig_lo
= 0xffffffff;
3411 else if (HOST_BITS_PER_LONG
== 32)
3413 sig_hi
= r
->sig
[SIGSZ
-1];
3414 sig_lo
= r
->sig
[SIGSZ
-2];
3418 sig_lo
= r
->sig
[SIGSZ
-1];
3419 sig_hi
= sig_lo
>> 31 >> 1;
3420 sig_lo
&= 0xffffffff;
3422 if (r
->signalling
== fmt
->qnan_msb_set
)
3423 sig_hi
&= ~(1 << 30);
3426 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3429 /* Intel requires the explicit integer bit to be set, otherwise
3430 it considers the value a "pseudo-nan". Motorola docs say it
3432 sig_hi
|= 0x80000000;
3437 sig_lo
= sig_hi
= 0xffffffff;
3443 int exp
= REAL_EXP (r
);
3445 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3446 whereas the intermediate representation is 0.F x 2**exp.
3447 Which means we're off by one.
3449 Except for Motorola, which consider exp=0 and explicit
3450 integer bit set to continue to be normalized. In theory
3451 this discrepancy has been taken care of by the difference
3452 in fmt->emin in round_for_format. */
3459 gcc_assert (exp
>= 0);
3463 if (HOST_BITS_PER_LONG
== 32)
3465 sig_hi
= r
->sig
[SIGSZ
-1];
3466 sig_lo
= r
->sig
[SIGSZ
-2];
3470 sig_lo
= r
->sig
[SIGSZ
-1];
3471 sig_hi
= sig_lo
>> 31 >> 1;
3472 sig_lo
&= 0xffffffff;
3481 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3484 /* Convert from the internal format to the 12-byte Motorola format
3485 for an IEEE extended real. */
3487 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3488 const REAL_VALUE_TYPE
*r
)
3491 encode_ieee_extended (fmt
, intermed
, r
);
3493 if (r
->cl
== rvc_inf
)
3494 /* For infinity clear the explicit integer bit again, so that the
3495 format matches the canonical infinity generated by the FPU. */
3498 /* Motorola chips are assumed always to be big-endian. Also, the
3499 padding in a Motorola extended real goes between the exponent and
3500 the mantissa. At this point the mantissa is entirely within
3501 elements 0 and 1 of intermed, and the exponent entirely within
3502 element 2, so all we have to do is swap the order around, and
3503 shift element 2 left 16 bits. */
3504 buf
[0] = intermed
[2] << 16;
3505 buf
[1] = intermed
[1];
3506 buf
[2] = intermed
[0];
3509 /* Convert from the internal format to the 12-byte Intel format for
3510 an IEEE extended real. */
3512 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3513 const REAL_VALUE_TYPE
*r
)
3515 if (FLOAT_WORDS_BIG_ENDIAN
)
3517 /* All the padding in an Intel-format extended real goes at the high
3518 end, which in this case is after the mantissa, not the exponent.
3519 Therefore we must shift everything down 16 bits. */
3521 encode_ieee_extended (fmt
, intermed
, r
);
3522 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3523 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3524 buf
[2] = (intermed
[0] << 16);
3527 /* encode_ieee_extended produces what we want directly. */
3528 encode_ieee_extended (fmt
, buf
, r
);
3531 /* Convert from the internal format to the 16-byte Intel format for
3532 an IEEE extended real. */
3534 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3535 const REAL_VALUE_TYPE
*r
)
3537 /* All the padding in an Intel-format extended real goes at the high end. */
3538 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3542 /* As above, we have a helper function which converts from 12-byte
3543 little-endian Intel format to internal format. Functions below
3544 adjust for the other possible formats. */
3546 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3549 unsigned long image_hi
, sig_hi
, sig_lo
;
3553 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3554 sig_lo
&= 0xffffffff;
3555 sig_hi
&= 0xffffffff;
3556 image_hi
&= 0xffffffff;
3558 sign
= (image_hi
>> 15) & 1;
3559 exp
= image_hi
& 0x7fff;
3561 memset (r
, 0, sizeof (*r
));
3565 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3570 /* When the IEEE format contains a hidden bit, we know that
3571 it's zero at this point, and so shift up the significand
3572 and decrease the exponent to match. In this case, Motorola
3573 defines the explicit integer bit to be valid, so we don't
3574 know whether the msb is set or not. */
3575 SET_REAL_EXP (r
, fmt
->emin
);
3576 if (HOST_BITS_PER_LONG
== 32)
3578 r
->sig
[SIGSZ
-1] = sig_hi
;
3579 r
->sig
[SIGSZ
-2] = sig_lo
;
3582 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3586 else if (fmt
->has_signed_zero
)
3589 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3591 /* See above re "pseudo-infinities" and "pseudo-nans".
3592 Short summary is that the MSB will likely always be
3593 set, and that we don't care about it. */
3594 sig_hi
&= 0x7fffffff;
3596 if (sig_hi
|| sig_lo
)
3600 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3601 if (HOST_BITS_PER_LONG
== 32)
3603 r
->sig
[SIGSZ
-1] = sig_hi
;
3604 r
->sig
[SIGSZ
-2] = sig_lo
;
3607 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3619 SET_REAL_EXP (r
, exp
- 16383 + 1);
3620 if (HOST_BITS_PER_LONG
== 32)
3622 r
->sig
[SIGSZ
-1] = sig_hi
;
3623 r
->sig
[SIGSZ
-2] = sig_lo
;
3626 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3630 /* Convert from the internal format to the 12-byte Motorola format
3631 for an IEEE extended real. */
3633 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3638 /* Motorola chips are assumed always to be big-endian. Also, the
3639 padding in a Motorola extended real goes between the exponent and
3640 the mantissa; remove it. */
3641 intermed
[0] = buf
[2];
3642 intermed
[1] = buf
[1];
3643 intermed
[2] = (unsigned long)buf
[0] >> 16;
3645 decode_ieee_extended (fmt
, r
, intermed
);
3648 /* Convert from the internal format to the 12-byte Intel format for
3649 an IEEE extended real. */
3651 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3654 if (FLOAT_WORDS_BIG_ENDIAN
)
3656 /* All the padding in an Intel-format extended real goes at the high
3657 end, which in this case is after the mantissa, not the exponent.
3658 Therefore we must shift everything up 16 bits. */
3661 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3662 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3663 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3665 decode_ieee_extended (fmt
, r
, intermed
);
3668 /* decode_ieee_extended produces what we want directly. */
3669 decode_ieee_extended (fmt
, r
, buf
);
3672 /* Convert from the internal format to the 16-byte Intel format for
3673 an IEEE extended real. */
3675 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3678 /* All the padding in an Intel-format extended real goes at the high end. */
3679 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3682 const struct real_format ieee_extended_motorola_format
=
3684 encode_ieee_extended_motorola
,
3685 decode_ieee_extended_motorola
,
3701 "ieee_extended_motorola"
3704 const struct real_format ieee_extended_intel_96_format
=
3706 encode_ieee_extended_intel_96
,
3707 decode_ieee_extended_intel_96
,
3723 "ieee_extended_intel_96"
3726 const struct real_format ieee_extended_intel_128_format
=
3728 encode_ieee_extended_intel_128
,
3729 decode_ieee_extended_intel_128
,
3745 "ieee_extended_intel_128"
3748 /* The following caters to i386 systems that set the rounding precision
3749 to 53 bits instead of 64, e.g. FreeBSD. */
3750 const struct real_format ieee_extended_intel_96_round_53_format
=
3752 encode_ieee_extended_intel_96
,
3753 decode_ieee_extended_intel_96
,
3769 "ieee_extended_intel_96_round_53"
3772 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3773 numbers whose sum is equal to the extended precision value. The number
3774 with greater magnitude is first. This format has the same magnitude
3775 range as an IEEE double precision value, but effectively 106 bits of
3776 significand precision. Infinity and NaN are represented by their IEEE
3777 double precision value stored in the first number, the second number is
3778 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3780 static void encode_ibm_extended (const struct real_format
*fmt
,
3781 long *, const REAL_VALUE_TYPE
*);
3782 static void decode_ibm_extended (const struct real_format
*,
3783 REAL_VALUE_TYPE
*, const long *);
3786 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3787 const REAL_VALUE_TYPE
*r
)
3789 REAL_VALUE_TYPE u
, normr
, v
;
3790 const struct real_format
*base_fmt
;
3792 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3794 /* Renormalize R before doing any arithmetic on it. */
3796 if (normr
.cl
== rvc_normal
)
3799 /* u = IEEE double precision portion of significand. */
3801 round_for_format (base_fmt
, &u
);
3802 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3804 if (u
.cl
== rvc_normal
)
3806 do_add (&v
, &normr
, &u
, 1);
3807 /* Call round_for_format since we might need to denormalize. */
3808 round_for_format (base_fmt
, &v
);
3809 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3813 /* Inf, NaN, 0 are all representable as doubles, so the
3814 least-significant part can be 0.0. */
3821 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3824 REAL_VALUE_TYPE u
, v
;
3825 const struct real_format
*base_fmt
;
3827 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3828 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3830 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3832 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3833 do_add (r
, &u
, &v
, 0);
3839 const struct real_format ibm_extended_format
=
3841 encode_ibm_extended
,
3842 decode_ibm_extended
,
3861 const struct real_format mips_extended_format
=
3863 encode_ibm_extended
,
3864 decode_ibm_extended
,
3884 /* IEEE quad precision format. */
3886 static void encode_ieee_quad (const struct real_format
*fmt
,
3887 long *, const REAL_VALUE_TYPE
*);
3888 static void decode_ieee_quad (const struct real_format
*,
3889 REAL_VALUE_TYPE
*, const long *);
3892 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3893 const REAL_VALUE_TYPE
*r
)
3895 unsigned long image3
, image2
, image1
, image0
, exp
;
3896 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3899 image3
= r
->sign
<< 31;
3904 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3913 image3
|= 32767 << 16;
3916 image3
|= 0x7fffffff;
3917 image2
= 0xffffffff;
3918 image1
= 0xffffffff;
3919 image0
= 0xffffffff;
3926 image3
|= 32767 << 16;
3930 if (fmt
->canonical_nan_lsbs_set
)
3933 image2
= image1
= image0
= 0xffffffff;
3936 else if (HOST_BITS_PER_LONG
== 32)
3941 image3
|= u
.sig
[3] & 0xffff;
3946 image1
= image0
>> 31 >> 1;
3948 image3
|= (image2
>> 31 >> 1) & 0xffff;
3949 image0
&= 0xffffffff;
3950 image2
&= 0xffffffff;
3952 if (r
->signalling
== fmt
->qnan_msb_set
)
3956 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3961 image3
|= 0x7fffffff;
3962 image2
= 0xffffffff;
3963 image1
= 0xffffffff;
3964 image0
= 0xffffffff;
3969 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3970 whereas the intermediate representation is 0.F x 2**exp.
3971 Which means we're off by one. */
3975 exp
= REAL_EXP (r
) + 16383 - 1;
3976 image3
|= exp
<< 16;
3978 if (HOST_BITS_PER_LONG
== 32)
3983 image3
|= u
.sig
[3] & 0xffff;
3988 image1
= image0
>> 31 >> 1;
3990 image3
|= (image2
>> 31 >> 1) & 0xffff;
3991 image0
&= 0xffffffff;
3992 image2
&= 0xffffffff;
4000 if (FLOAT_WORDS_BIG_ENDIAN
)
4017 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4020 unsigned long image3
, image2
, image1
, image0
;
4024 if (FLOAT_WORDS_BIG_ENDIAN
)
4038 image0
&= 0xffffffff;
4039 image1
&= 0xffffffff;
4040 image2
&= 0xffffffff;
4042 sign
= (image3
>> 31) & 1;
4043 exp
= (image3
>> 16) & 0x7fff;
4046 memset (r
, 0, sizeof (*r
));
4050 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4055 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4056 if (HOST_BITS_PER_LONG
== 32)
4065 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4066 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4071 else if (fmt
->has_signed_zero
)
4074 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4076 if (image3
| image2
| image1
| image0
)
4080 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4082 if (HOST_BITS_PER_LONG
== 32)
4091 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4092 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4094 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4106 SET_REAL_EXP (r
, exp
- 16383 + 1);
4108 if (HOST_BITS_PER_LONG
== 32)
4117 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4118 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4120 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4121 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4125 const struct real_format ieee_quad_format
=
4147 const struct real_format mips_quad_format
=
4169 /* Descriptions of VAX floating point formats can be found beginning at
4171 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4173 The thing to remember is that they're almost IEEE, except for word
4174 order, exponent bias, and the lack of infinities, nans, and denormals.
4176 We don't implement the H_floating format here, simply because neither
4177 the VAX or Alpha ports use it. */
4179 static void encode_vax_f (const struct real_format
*fmt
,
4180 long *, const REAL_VALUE_TYPE
*);
4181 static void decode_vax_f (const struct real_format
*,
4182 REAL_VALUE_TYPE
*, const long *);
4183 static void encode_vax_d (const struct real_format
*fmt
,
4184 long *, const REAL_VALUE_TYPE
*);
4185 static void decode_vax_d (const struct real_format
*,
4186 REAL_VALUE_TYPE
*, const long *);
4187 static void encode_vax_g (const struct real_format
*fmt
,
4188 long *, const REAL_VALUE_TYPE
*);
4189 static void decode_vax_g (const struct real_format
*,
4190 REAL_VALUE_TYPE
*, const long *);
4193 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4194 const REAL_VALUE_TYPE
*r
)
4196 unsigned long sign
, exp
, sig
, image
;
4198 sign
= r
->sign
<< 15;
4208 image
= 0xffff7fff | sign
;
4212 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4213 exp
= REAL_EXP (r
) + 128;
4215 image
= (sig
<< 16) & 0xffff0000;
4229 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4230 REAL_VALUE_TYPE
*r
, const long *buf
)
4232 unsigned long image
= buf
[0] & 0xffffffff;
4233 int exp
= (image
>> 7) & 0xff;
4235 memset (r
, 0, sizeof (*r
));
4240 r
->sign
= (image
>> 15) & 1;
4241 SET_REAL_EXP (r
, exp
- 128);
4243 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4244 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4249 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4250 const REAL_VALUE_TYPE
*r
)
4252 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4257 image0
= image1
= 0;
4262 image0
= 0xffff7fff | sign
;
4263 image1
= 0xffffffff;
4267 /* Extract the significand into straight hi:lo. */
4268 if (HOST_BITS_PER_LONG
== 64)
4270 image0
= r
->sig
[SIGSZ
-1];
4271 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4272 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4276 image0
= r
->sig
[SIGSZ
-1];
4277 image1
= r
->sig
[SIGSZ
-2];
4278 image1
= (image0
<< 24) | (image1
>> 8);
4279 image0
= (image0
>> 8) & 0xffffff;
4282 /* Rearrange the half-words of the significand to match the
4284 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4285 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4287 /* Add the sign and exponent. */
4289 image0
|= (REAL_EXP (r
) + 128) << 7;
4296 if (FLOAT_WORDS_BIG_ENDIAN
)
4297 buf
[0] = image1
, buf
[1] = image0
;
4299 buf
[0] = image0
, buf
[1] = image1
;
4303 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4304 REAL_VALUE_TYPE
*r
, const long *buf
)
4306 unsigned long image0
, image1
;
4309 if (FLOAT_WORDS_BIG_ENDIAN
)
4310 image1
= buf
[0], image0
= buf
[1];
4312 image0
= buf
[0], image1
= buf
[1];
4313 image0
&= 0xffffffff;
4314 image1
&= 0xffffffff;
4316 exp
= (image0
>> 7) & 0xff;
4318 memset (r
, 0, sizeof (*r
));
4323 r
->sign
= (image0
>> 15) & 1;
4324 SET_REAL_EXP (r
, exp
- 128);
4326 /* Rearrange the half-words of the external format into
4327 proper ascending order. */
4328 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4329 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4331 if (HOST_BITS_PER_LONG
== 64)
4333 image0
= (image0
<< 31 << 1) | image1
;
4336 r
->sig
[SIGSZ
-1] = image0
;
4340 r
->sig
[SIGSZ
-1] = image0
;
4341 r
->sig
[SIGSZ
-2] = image1
;
4342 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4343 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4349 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4350 const REAL_VALUE_TYPE
*r
)
4352 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4357 image0
= image1
= 0;
4362 image0
= 0xffff7fff | sign
;
4363 image1
= 0xffffffff;
4367 /* Extract the significand into straight hi:lo. */
4368 if (HOST_BITS_PER_LONG
== 64)
4370 image0
= r
->sig
[SIGSZ
-1];
4371 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4372 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4376 image0
= r
->sig
[SIGSZ
-1];
4377 image1
= r
->sig
[SIGSZ
-2];
4378 image1
= (image0
<< 21) | (image1
>> 11);
4379 image0
= (image0
>> 11) & 0xfffff;
4382 /* Rearrange the half-words of the significand to match the
4384 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4385 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4387 /* Add the sign and exponent. */
4389 image0
|= (REAL_EXP (r
) + 1024) << 4;
4396 if (FLOAT_WORDS_BIG_ENDIAN
)
4397 buf
[0] = image1
, buf
[1] = image0
;
4399 buf
[0] = image0
, buf
[1] = image1
;
4403 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4404 REAL_VALUE_TYPE
*r
, const long *buf
)
4406 unsigned long image0
, image1
;
4409 if (FLOAT_WORDS_BIG_ENDIAN
)
4410 image1
= buf
[0], image0
= buf
[1];
4412 image0
= buf
[0], image1
= buf
[1];
4413 image0
&= 0xffffffff;
4414 image1
&= 0xffffffff;
4416 exp
= (image0
>> 4) & 0x7ff;
4418 memset (r
, 0, sizeof (*r
));
4423 r
->sign
= (image0
>> 15) & 1;
4424 SET_REAL_EXP (r
, exp
- 1024);
4426 /* Rearrange the half-words of the external format into
4427 proper ascending order. */
4428 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4429 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4431 if (HOST_BITS_PER_LONG
== 64)
4433 image0
= (image0
<< 31 << 1) | image1
;
4436 r
->sig
[SIGSZ
-1] = image0
;
4440 r
->sig
[SIGSZ
-1] = image0
;
4441 r
->sig
[SIGSZ
-2] = image1
;
4442 lshift_significand (r
, r
, 64 - 53);
4443 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4448 const struct real_format vax_f_format
=
4470 const struct real_format vax_d_format
=
4492 const struct real_format vax_g_format
=
4514 /* Encode real R into a single precision DFP value in BUF. */
4516 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4517 long *buf ATTRIBUTE_UNUSED
,
4518 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4520 encode_decimal32 (fmt
, buf
, r
);
4523 /* Decode a single precision DFP value in BUF into a real R. */
4525 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4526 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4527 const long *buf ATTRIBUTE_UNUSED
)
4529 decode_decimal32 (fmt
, r
, buf
);
4532 /* Encode real R into a double precision DFP value in BUF. */
4534 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4535 long *buf ATTRIBUTE_UNUSED
,
4536 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4538 encode_decimal64 (fmt
, buf
, r
);
4541 /* Decode a double precision DFP value in BUF into a real R. */
4543 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4544 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4545 const long *buf ATTRIBUTE_UNUSED
)
4547 decode_decimal64 (fmt
, r
, buf
);
4550 /* Encode real R into a quad precision DFP value in BUF. */
4552 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4553 long *buf ATTRIBUTE_UNUSED
,
4554 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4556 encode_decimal128 (fmt
, buf
, r
);
4559 /* Decode a quad precision DFP value in BUF into a real R. */
4561 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4562 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4563 const long *buf ATTRIBUTE_UNUSED
)
4565 decode_decimal128 (fmt
, r
, buf
);
4568 /* Single precision decimal floating point (IEEE 754). */
4569 const struct real_format decimal_single_format
=
4571 encode_decimal_single
,
4572 decode_decimal_single
,
4591 /* Double precision decimal floating point (IEEE 754). */
4592 const struct real_format decimal_double_format
=
4594 encode_decimal_double
,
4595 decode_decimal_double
,
4614 /* Quad precision decimal floating point (IEEE 754). */
4615 const struct real_format decimal_quad_format
=
4617 encode_decimal_quad
,
4618 decode_decimal_quad
,
4637 /* Encode half-precision floats. This routine is used both for the IEEE
4638 ARM alternative encodings. */
4640 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4641 const REAL_VALUE_TYPE
*r
)
4643 unsigned long image
, sig
, exp
;
4644 unsigned long sign
= r
->sign
;
4645 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4648 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4666 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4667 if (r
->signalling
== fmt
->qnan_msb_set
)
4682 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4683 whereas the intermediate representation is 0.F x 2**exp.
4684 Which means we're off by one. */
4688 exp
= REAL_EXP (r
) + 15 - 1;
4700 /* Decode half-precision floats. This routine is used both for the IEEE
4701 ARM alternative encodings. */
4703 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4706 unsigned long image
= buf
[0] & 0xffff;
4707 bool sign
= (image
>> 15) & 1;
4708 int exp
= (image
>> 10) & 0x1f;
4710 memset (r
, 0, sizeof (*r
));
4711 image
<<= HOST_BITS_PER_LONG
- 11;
4716 if (image
&& fmt
->has_denorm
)
4720 SET_REAL_EXP (r
, -14);
4721 r
->sig
[SIGSZ
-1] = image
<< 1;
4724 else if (fmt
->has_signed_zero
)
4727 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4733 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4734 ^ fmt
->qnan_msb_set
);
4735 r
->sig
[SIGSZ
-1] = image
;
4747 SET_REAL_EXP (r
, exp
- 15 + 1);
4748 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4752 /* Half-precision format, as specified in IEEE 754R. */
4753 const struct real_format ieee_half_format
=
4775 /* ARM's alternative half-precision format, similar to IEEE but with
4776 no reserved exponent value for NaNs and infinities; rather, it just
4777 extends the range of exponents by one. */
4778 const struct real_format arm_half_format
=
4800 /* A synthetic "format" for internal arithmetic. It's the size of the
4801 internal significand minus the two bits needed for proper rounding.
4802 The encode and decode routines exist only to satisfy our paranoia
4805 static void encode_internal (const struct real_format
*fmt
,
4806 long *, const REAL_VALUE_TYPE
*);
4807 static void decode_internal (const struct real_format
*,
4808 REAL_VALUE_TYPE
*, const long *);
4811 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4812 const REAL_VALUE_TYPE
*r
)
4814 memcpy (buf
, r
, sizeof (*r
));
4818 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4819 REAL_VALUE_TYPE
*r
, const long *buf
)
4821 memcpy (r
, buf
, sizeof (*r
));
4824 const struct real_format real_internal_format
=
4829 SIGNIFICAND_BITS
- 2,
4830 SIGNIFICAND_BITS
- 2,
4846 /* Calculate X raised to the integer exponent N in mode MODE and store
4847 the result in R. Return true if the result may be inexact due to
4848 loss of precision. The algorithm is the classic "left-to-right binary
4849 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4850 Algorithms", "The Art of Computer Programming", Volume 2. */
4853 real_powi (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4854 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4856 unsigned HOST_WIDE_INT bit
;
4858 bool inexact
= false;
4870 /* Don't worry about overflow, from now on n is unsigned. */
4878 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4879 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4883 inexact
|= do_multiply (&t
, &t
, &t
);
4885 inexact
|= do_multiply (&t
, &t
, x
);
4893 inexact
|= do_divide (&t
, &dconst1
, &t
);
4895 real_convert (r
, mode
, &t
);
4899 /* Round X to the nearest integer not larger in absolute value, i.e.
4900 towards zero, placing the result in R in mode MODE. */
4903 real_trunc (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4904 const REAL_VALUE_TYPE
*x
)
4906 do_fix_trunc (r
, x
);
4907 if (mode
!= VOIDmode
)
4908 real_convert (r
, mode
, r
);
4911 /* Round X to the largest integer not greater in value, i.e. round
4912 down, placing the result in R in mode MODE. */
4915 real_floor (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4916 const REAL_VALUE_TYPE
*x
)
4920 do_fix_trunc (&t
, x
);
4921 if (! real_identical (&t
, x
) && x
->sign
)
4922 do_add (&t
, &t
, &dconstm1
, 0);
4923 if (mode
!= VOIDmode
)
4924 real_convert (r
, mode
, &t
);
4929 /* Round X to the smallest integer not less then argument, i.e. round
4930 up, placing the result in R in mode MODE. */
4933 real_ceil (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4934 const REAL_VALUE_TYPE
*x
)
4938 do_fix_trunc (&t
, x
);
4939 if (! real_identical (&t
, x
) && ! x
->sign
)
4940 do_add (&t
, &t
, &dconst1
, 0);
4941 if (mode
!= VOIDmode
)
4942 real_convert (r
, mode
, &t
);
4947 /* Round X to the nearest integer, but round halfway cases away from
4951 real_round (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4952 const REAL_VALUE_TYPE
*x
)
4954 do_add (r
, x
, &dconsthalf
, x
->sign
);
4955 do_fix_trunc (r
, r
);
4956 if (mode
!= VOIDmode
)
4957 real_convert (r
, mode
, r
);
4960 /* Set the sign of R to the sign of X. */
4963 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4968 /* Check whether the real constant value given is an integer. */
4971 real_isinteger (const REAL_VALUE_TYPE
*c
, machine_mode mode
)
4973 REAL_VALUE_TYPE cint
;
4975 real_trunc (&cint
, mode
, c
);
4976 return real_identical (c
, &cint
);
4979 /* Write into BUF the maximum representable finite floating-point
4980 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4981 float string. LEN is the size of BUF, and the buffer must be large
4982 enough to contain the resulting string. */
4985 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
4990 strcpy (buf
, "0x0.");
4992 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
4995 *p
++ = "08ce"[n
- i
];
4996 sprintf (p
, "p%d", fmt
->emax
);
4997 if (fmt
->pnan
< fmt
->p
)
4999 /* This is an IBM extended double format made up of two IEEE
5000 doubles. The value of the long double is the sum of the
5001 values of the two parts. The most significant part is
5002 required to be the value of the long double rounded to the
5003 nearest double. Rounding means we need a slightly smaller
5004 value for LDBL_MAX. */
5005 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
5008 gcc_assert (strlen (buf
) < len
);
5011 /* True if mode M has a NaN representation and
5012 the treatment of NaN operands is important. */
5015 HONOR_NANS (machine_mode m
)
5017 return MODE_HAS_NANS (m
) && !flag_finite_math_only
;
5021 HONOR_NANS (const_tree t
)
5023 return HONOR_NANS (element_mode (t
));
5027 HONOR_NANS (const_rtx x
)
5029 return HONOR_NANS (GET_MODE (x
));
5032 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5035 HONOR_SNANS (machine_mode m
)
5037 return flag_signaling_nans
&& HONOR_NANS (m
);
5041 HONOR_SNANS (const_tree t
)
5043 return HONOR_SNANS (element_mode (t
));
5047 HONOR_SNANS (const_rtx x
)
5049 return HONOR_SNANS (GET_MODE (x
));
5052 /* As for HONOR_NANS, but true if the mode can represent infinity and
5053 the treatment of infinite values is important. */
5056 HONOR_INFINITIES (machine_mode m
)
5058 return MODE_HAS_INFINITIES (m
) && !flag_finite_math_only
;
5062 HONOR_INFINITIES (const_tree t
)
5064 return HONOR_INFINITIES (element_mode (t
));
5068 HONOR_INFINITIES (const_rtx x
)
5070 return HONOR_INFINITIES (GET_MODE (x
));
5073 /* Like HONOR_NANS, but true if the given mode distinguishes between
5074 positive and negative zero, and the sign of zero is important. */
5077 HONOR_SIGNED_ZEROS (machine_mode m
)
5079 return MODE_HAS_SIGNED_ZEROS (m
) && flag_signed_zeros
;
5083 HONOR_SIGNED_ZEROS (const_tree t
)
5085 return HONOR_SIGNED_ZEROS (element_mode (t
));
5089 HONOR_SIGNED_ZEROS (const_rtx x
)
5091 return HONOR_SIGNED_ZEROS (GET_MODE (x
));
5094 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5095 and the rounding mode is important. */
5098 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m
)
5100 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m
) && flag_rounding_math
;
5104 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t
)
5106 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t
));
5110 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x
)
5112 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x
));