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
);
1081 /* Return whether OP0 == OP1. */
1084 real_equal (const REAL_VALUE_TYPE
*op0
, const REAL_VALUE_TYPE
*op1
)
1086 return do_compare (op0
, op1
, -1) == 0;
1089 /* Return whether OP0 < OP1. */
1092 real_less (const REAL_VALUE_TYPE
*op0
, const REAL_VALUE_TYPE
*op1
)
1094 return do_compare (op0
, op1
, 1) < 0;
1098 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1099 const REAL_VALUE_TYPE
*op1
)
1101 enum tree_code code
= (enum tree_code
) icode
;
1106 return real_less (op0
, op1
);
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 real_equal (op0
, op1
);
1116 return do_compare (op0
, op1
, -1) != 0;
1117 case UNORDERED_EXPR
:
1118 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1120 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1122 return do_compare (op0
, op1
, -1) < 0;
1124 return do_compare (op0
, op1
, -1) <= 0;
1126 return do_compare (op0
, op1
, 1) > 0;
1128 return do_compare (op0
, op1
, 1) >= 0;
1130 return do_compare (op0
, op1
, 0) == 0;
1132 return do_compare (op0
, op1
, 0) != 0;
1139 /* Return floor log2(R). */
1142 real_exponent (const REAL_VALUE_TYPE
*r
)
1150 return (unsigned int)-1 >> 1;
1152 return REAL_EXP (r
);
1158 /* R = OP0 * 2**EXP. */
1161 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1172 exp
+= REAL_EXP (op0
);
1174 get_inf (r
, r
->sign
);
1175 else if (exp
< -MAX_EXP
)
1176 get_zero (r
, r
->sign
);
1178 SET_REAL_EXP (r
, exp
);
1186 /* Determine whether a floating-point value X is infinite. */
1189 real_isinf (const REAL_VALUE_TYPE
*r
)
1191 return (r
->cl
== rvc_inf
);
1194 /* Determine whether a floating-point value X is a NaN. */
1197 real_isnan (const REAL_VALUE_TYPE
*r
)
1199 return (r
->cl
== rvc_nan
);
1202 /* Determine whether a floating-point value X is finite. */
1205 real_isfinite (const REAL_VALUE_TYPE
*r
)
1207 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1210 /* Determine whether a floating-point value X is negative. */
1213 real_isneg (const REAL_VALUE_TYPE
*r
)
1218 /* Determine whether a floating-point value X is minus zero. */
1221 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1223 return r
->sign
&& r
->cl
== rvc_zero
;
1226 /* Compare two floating-point objects for bitwise identity. */
1229 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1235 if (a
->sign
!= b
->sign
)
1245 if (a
->decimal
!= b
->decimal
)
1247 if (REAL_EXP (a
) != REAL_EXP (b
))
1252 if (a
->signalling
!= b
->signalling
)
1254 /* The significand is ignored for canonical NaNs. */
1255 if (a
->canonical
|| b
->canonical
)
1256 return a
->canonical
== b
->canonical
;
1263 for (i
= 0; i
< SIGSZ
; ++i
)
1264 if (a
->sig
[i
] != b
->sig
[i
])
1270 /* Try to change R into its exact multiplicative inverse in machine
1271 mode MODE. Return true if successful. */
1274 exact_real_inverse (machine_mode mode
, REAL_VALUE_TYPE
*r
)
1276 const REAL_VALUE_TYPE
*one
= real_digit (1);
1280 if (r
->cl
!= rvc_normal
)
1283 /* Check for a power of two: all significand bits zero except the MSB. */
1284 for (i
= 0; i
< SIGSZ
-1; ++i
)
1287 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1290 /* Find the inverse and truncate to the required mode. */
1291 do_divide (&u
, one
, r
);
1292 real_convert (&u
, mode
, &u
);
1294 /* The rounding may have overflowed. */
1295 if (u
.cl
!= rvc_normal
)
1297 for (i
= 0; i
< SIGSZ
-1; ++i
)
1300 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1307 /* Return true if arithmetic on values in IMODE that were promoted
1308 from values in TMODE is equivalent to direct arithmetic on values
1312 real_can_shorten_arithmetic (machine_mode imode
, machine_mode tmode
)
1314 const struct real_format
*tfmt
, *ifmt
;
1315 tfmt
= REAL_MODE_FORMAT (tmode
);
1316 ifmt
= REAL_MODE_FORMAT (imode
);
1317 /* These conditions are conservative rather than trying to catch the
1318 exact boundary conditions; the main case to allow is IEEE float
1320 return (ifmt
->b
== tfmt
->b
1321 && ifmt
->p
> 2 * tfmt
->p
1322 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1323 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1324 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1325 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1326 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1327 && (ifmt
->has_sign_dependent_rounding
1328 == tfmt
->has_sign_dependent_rounding
)
1329 && ifmt
->has_nans
>= tfmt
->has_nans
1330 && ifmt
->has_inf
>= tfmt
->has_inf
1331 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1332 && !MODE_COMPOSITE_P (tmode
)
1333 && !MODE_COMPOSITE_P (imode
));
1336 /* Render R as an integer. */
1339 real_to_integer (const REAL_VALUE_TYPE
*r
)
1341 unsigned HOST_WIDE_INT i
;
1352 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1359 return decimal_real_to_integer (r
);
1361 if (REAL_EXP (r
) <= 0)
1363 /* Only force overflow for unsigned overflow. Signed overflow is
1364 undefined, so it doesn't matter what we return, and some callers
1365 expect to be able to use this routine for both signed and
1366 unsigned conversions. */
1367 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1370 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1371 i
= r
->sig
[SIGSZ
-1];
1374 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1375 i
= r
->sig
[SIGSZ
-1];
1376 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1377 i
|= r
->sig
[SIGSZ
-2];
1380 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1391 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1392 be represented in precision, *FAIL is set to TRUE. */
1395 real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
1397 HOST_WIDE_INT val
[2 * WIDE_INT_MAX_ELTS
];
1406 return wi::zero (precision
);
1414 return wi::set_bit_in_zero (precision
- 1, precision
);
1416 return ~wi::set_bit_in_zero (precision
- 1, precision
);
1420 return decimal_real_to_integer (r
, fail
, precision
);
1425 /* Only force overflow for unsigned overflow. Signed overflow is
1426 undefined, so it doesn't matter what we return, and some callers
1427 expect to be able to use this routine for both signed and
1428 unsigned conversions. */
1429 if (exp
> precision
)
1432 /* Put the significand into a wide_int that has precision W, which
1433 is the smallest HWI-multiple that has at least PRECISION bits.
1434 This ensures that the top bit of the significand is in the
1435 top bit of the wide_int. */
1436 words
= (precision
+ HOST_BITS_PER_WIDE_INT
- 1) / HOST_BITS_PER_WIDE_INT
;
1437 w
= words
* HOST_BITS_PER_WIDE_INT
;
1439 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1440 for (int i
= 0; i
< words
; i
++)
1442 int j
= SIGSZ
- words
+ i
;
1443 val
[i
] = (j
< 0) ? 0 : r
->sig
[j
];
1446 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1447 for (int i
= 0; i
< words
; i
++)
1449 int j
= SIGSZ
- (words
* 2) + (i
* 2);
1456 val
[i
] |= (unsigned HOST_WIDE_INT
) r
->sig
[j
] << HOST_BITS_PER_LONG
;
1459 /* Shift the value into place and truncate to the desired precision. */
1460 result
= wide_int::from_array (val
, words
, w
);
1461 result
= wi::lrshift (result
, w
- exp
);
1462 result
= wide_int::from (result
, precision
, UNSIGNED
);
1474 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1475 of NUM / DEN. Return the quotient and place the remainder in NUM.
1476 It is expected that NUM / DEN are close enough that the quotient is
1479 static unsigned long
1480 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1482 unsigned long q
, msb
;
1483 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1492 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1494 lshift_significand_1 (num
, num
);
1496 if (msb
|| cmp_significands (num
, den
) >= 0)
1498 sub_significands (num
, num
, den
, 0);
1502 while (--expn
>= expd
);
1504 SET_REAL_EXP (num
, expd
);
1510 /* Render R as a decimal floating point constant. Emit DIGITS significant
1511 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1512 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1513 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1514 to a string that, when parsed back in mode MODE, yields the same value. */
1516 #define M_LOG10_2 0.30102999566398119521
1519 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1520 size_t buf_size
, size_t digits
,
1521 int crop_trailing_zeros
, machine_mode mode
)
1523 const struct real_format
*fmt
= NULL
;
1524 const REAL_VALUE_TYPE
*one
, *ten
;
1525 REAL_VALUE_TYPE r
, pten
, u
, v
;
1526 int dec_exp
, cmp_one
, digit
;
1528 char *p
, *first
, *last
;
1532 if (mode
!= VOIDmode
)
1534 fmt
= REAL_MODE_FORMAT (mode
);
1542 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1547 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1550 /* ??? Print the significand as well, if not canonical? */
1551 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1552 (r_orig
->signalling
? 'S' : 'Q'));
1560 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1564 /* Bound the number of digits printed by the size of the representation. */
1565 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1566 if (digits
== 0 || digits
> max_digits
)
1567 digits
= max_digits
;
1569 /* Estimate the decimal exponent, and compute the length of the string it
1570 will print as. Be conservative and add one to account for possible
1571 overflow or rounding error. */
1572 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1573 for (max_digits
= 1; dec_exp
; max_digits
++)
1576 /* Bound the number of digits printed by the size of the output buffer. */
1577 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1578 gcc_assert (max_digits
<= buf_size
);
1579 if (digits
> max_digits
)
1580 digits
= max_digits
;
1582 one
= real_digit (1);
1583 ten
= ten_to_ptwo (0);
1591 cmp_one
= do_compare (&r
, one
, 0);
1596 /* Number is greater than one. Convert significand to an integer
1597 and strip trailing decimal zeros. */
1600 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1602 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1603 m
= floor_log2 (max_digits
);
1605 /* Iterate over the bits of the possible powers of 10 that might
1606 be present in U and eliminate them. That is, if we find that
1607 10**2**M divides U evenly, keep the division and increase
1613 do_divide (&t
, &u
, ten_to_ptwo (m
));
1614 do_fix_trunc (&v
, &t
);
1615 if (cmp_significands (&v
, &t
) == 0)
1623 /* Revert the scaling to integer that we performed earlier. */
1624 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1625 - (SIGNIFICAND_BITS
- 1));
1628 /* Find power of 10. Do this by dividing out 10**2**M when
1629 this is larger than the current remainder. Fill PTEN with
1630 the power of 10 that we compute. */
1631 if (REAL_EXP (&r
) > 0)
1633 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1636 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1637 if (do_compare (&u
, ptentwo
, 0) >= 0)
1639 do_divide (&u
, &u
, ptentwo
);
1640 do_multiply (&pten
, &pten
, ptentwo
);
1647 /* We managed to divide off enough tens in the above reduction
1648 loop that we've now got a negative exponent. Fall into the
1649 less-than-one code to compute the proper value for PTEN. */
1656 /* Number is less than one. Pad significand with leading
1662 /* Stop if we'd shift bits off the bottom. */
1666 do_multiply (&u
, &v
, ten
);
1668 /* Stop if we're now >= 1. */
1669 if (REAL_EXP (&u
) > 0)
1677 /* Find power of 10. Do this by multiplying in P=10**2**M when
1678 the current remainder is smaller than 1/P. Fill PTEN with the
1679 power of 10 that we compute. */
1680 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1683 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1684 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1686 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1688 do_multiply (&v
, &v
, ptentwo
);
1689 do_multiply (&pten
, &pten
, ptentwo
);
1695 /* Invert the positive power of 10 that we've collected so far. */
1696 do_divide (&pten
, one
, &pten
);
1704 /* At this point, PTEN should contain the nearest power of 10 smaller
1705 than R, such that this division produces the first digit.
1707 Using a divide-step primitive that returns the complete integral
1708 remainder avoids the rounding error that would be produced if
1709 we were to use do_divide here and then simply multiply by 10 for
1710 each subsequent digit. */
1712 digit
= rtd_divmod (&r
, &pten
);
1714 /* Be prepared for error in that division via underflow ... */
1715 if (digit
== 0 && cmp_significand_0 (&r
))
1717 /* Multiply by 10 and try again. */
1718 do_multiply (&r
, &r
, ten
);
1719 digit
= rtd_divmod (&r
, &pten
);
1721 gcc_assert (digit
!= 0);
1724 /* ... or overflow. */
1734 gcc_assert (digit
<= 10);
1738 /* Generate subsequent digits. */
1739 while (--digits
> 0)
1741 do_multiply (&r
, &r
, ten
);
1742 digit
= rtd_divmod (&r
, &pten
);
1747 /* Generate one more digit with which to do rounding. */
1748 do_multiply (&r
, &r
, ten
);
1749 digit
= rtd_divmod (&r
, &pten
);
1751 /* Round the result. */
1752 if (fmt
&& fmt
->round_towards_zero
)
1754 /* If the format uses round towards zero when parsing the string
1755 back in, we need to always round away from zero here. */
1756 if (cmp_significand_0 (&r
))
1758 round_up
= digit
> 0;
1764 /* Round to nearest. If R is nonzero there are additional
1765 nonzero digits to be extracted. */
1766 if (cmp_significand_0 (&r
))
1768 /* Round to even. */
1769 else if ((p
[-1] - '0') & 1)
1773 round_up
= digit
> 5;
1790 /* Carry out of the first digit. This means we had all 9's and
1791 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1799 /* Insert the decimal point. */
1800 first
[0] = first
[1];
1803 /* If requested, drop trailing zeros. Never crop past "1.0". */
1804 if (crop_trailing_zeros
)
1805 while (last
> first
+ 3 && last
[-1] == '0')
1808 /* Append the exponent. */
1809 sprintf (last
, "e%+d", dec_exp
);
1811 #ifdef ENABLE_CHECKING
1812 /* Verify that we can read the original value back in. */
1813 if (mode
!= VOIDmode
)
1815 real_from_string (&r
, str
);
1816 real_convert (&r
, mode
, &r
);
1817 gcc_assert (real_identical (&r
, r_orig
));
1822 /* Likewise, except always uses round-to-nearest. */
1825 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1826 size_t digits
, int crop_trailing_zeros
)
1828 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1829 digits
, crop_trailing_zeros
, VOIDmode
);
1832 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1833 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1834 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1835 strip trailing zeros. */
1838 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1839 size_t digits
, int crop_trailing_zeros
)
1841 int i
, j
, exp
= REAL_EXP (r
);
1854 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1857 /* ??? Print the significand as well, if not canonical? */
1858 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1859 (r
->signalling
? 'S' : 'Q'));
1867 /* Hexadecimal format for decimal floats is not interesting. */
1868 strcpy (str
, "N/A");
1873 digits
= SIGNIFICAND_BITS
/ 4;
1875 /* Bound the number of digits printed by the size of the output buffer. */
1877 sprintf (exp_buf
, "p%+d", exp
);
1878 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1879 gcc_assert (max_digits
<= buf_size
);
1880 if (digits
> max_digits
)
1881 digits
= max_digits
;
1892 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1893 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1895 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1901 if (crop_trailing_zeros
)
1902 while (p
> first
+ 1 && p
[-1] == '0')
1905 sprintf (p
, "p%+d", exp
);
1908 /* Initialize R from a decimal or hexadecimal string. The string is
1909 assumed to have been syntax checked already. Return -1 if the
1910 value underflows, +1 if overflows, and 0 otherwise. */
1913 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1925 else if (*str
== '+')
1928 if (!strncmp (str
, "QNaN", 4))
1930 get_canonical_qnan (r
, sign
);
1933 else if (!strncmp (str
, "SNaN", 4))
1935 get_canonical_snan (r
, sign
);
1938 else if (!strncmp (str
, "Inf", 3))
1944 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1946 /* Hexadecimal floating point. */
1947 int pos
= SIGNIFICAND_BITS
- 4, d
;
1955 d
= hex_value (*str
);
1960 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1961 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1965 /* Ensure correct rounding by setting last bit if there is
1966 a subsequent nonzero digit. */
1974 if (pos
== SIGNIFICAND_BITS
- 4)
1981 d
= hex_value (*str
);
1986 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1987 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1991 /* Ensure correct rounding by setting last bit if there is
1992 a subsequent nonzero digit. */
1998 /* If the mantissa is zero, ignore the exponent. */
1999 if (!cmp_significand_0 (r
))
2002 if (*str
== 'p' || *str
== 'P')
2004 bool exp_neg
= false;
2012 else if (*str
== '+')
2016 while (ISDIGIT (*str
))
2022 /* Overflowed the exponent. */
2037 SET_REAL_EXP (r
, exp
);
2043 /* Decimal floating point. */
2044 const char *cstr
= str
;
2048 while (*cstr
== '0')
2053 while (*cstr
== '0')
2057 /* If the mantissa is zero, ignore the exponent. */
2058 if (!ISDIGIT (*cstr
))
2061 /* Nonzero value, possibly overflowing or underflowing. */
2062 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2063 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, GMP_RNDZ
);
2064 /* The result should never be a NaN, and because the rounding is
2065 toward zero should never be an infinity. */
2066 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2067 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2072 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2079 real_from_mpfr (r
, m
, NULL_TREE
, GMP_RNDZ
);
2080 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2081 because the hex digits used in real_from_mpfr did not
2082 start with a digit 8 to f, but the exponent bounds above
2083 should have avoided underflow or overflow. */
2084 gcc_assert (r
->cl
== rvc_normal
);
2085 /* Set a sticky bit if mpfr_strtofr was inexact. */
2086 r
->sig
[0] |= inexact
;
2107 /* Legacy. Similar, but return the result directly. */
2110 real_from_string2 (const char *s
, machine_mode mode
)
2114 real_from_string (&r
, s
);
2115 if (mode
!= VOIDmode
)
2116 real_convert (&r
, mode
, &r
);
2121 /* Initialize R from string S and desired MODE. */
2124 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, machine_mode mode
)
2126 if (DECIMAL_FLOAT_MODE_P (mode
))
2127 decimal_real_from_string (r
, s
);
2129 real_from_string (r
, s
);
2131 if (mode
!= VOIDmode
)
2132 real_convert (r
, mode
, r
);
2135 /* Initialize R from the wide_int VAL_IN. The MODE is not VOIDmode,*/
2138 real_from_integer (REAL_VALUE_TYPE
*r
, machine_mode mode
,
2139 const wide_int_ref
&val_in
, signop sgn
)
2145 unsigned int len
= val_in
.get_precision ();
2147 int maxbitlen
= MAX_BITSIZE_MODE_ANY_INT
+ HOST_BITS_PER_WIDE_INT
;
2148 const unsigned int realmax
= (SIGNIFICAND_BITS
/ HOST_BITS_PER_WIDE_INT
2149 * HOST_BITS_PER_WIDE_INT
);
2151 memset (r
, 0, sizeof (*r
));
2153 r
->sign
= wi::neg_p (val_in
, sgn
);
2155 /* We have to ensure we can negate the largest negative number. */
2156 wide_int val
= wide_int::from (val_in
, maxbitlen
, sgn
);
2161 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2162 won't work with precisions that are not a multiple of
2163 HOST_BITS_PER_WIDE_INT. */
2164 len
+= HOST_BITS_PER_WIDE_INT
- 1;
2166 /* Ensure we can represent the largest negative number. */
2169 len
= len
/HOST_BITS_PER_WIDE_INT
* HOST_BITS_PER_WIDE_INT
;
2171 /* Cap the size to the size allowed by real.h. */
2174 HOST_WIDE_INT cnt_l_z
;
2175 cnt_l_z
= wi::clz (val
);
2177 if (maxbitlen
- cnt_l_z
> realmax
)
2179 e
= maxbitlen
- cnt_l_z
- realmax
;
2181 /* This value is too large, we must shift it right to
2182 preserve all the bits we can, and then bump the
2183 exponent up by that amount. */
2184 val
= wi::lrshift (val
, e
);
2189 /* Clear out top bits so elt will work with precisions that aren't
2190 a multiple of HOST_BITS_PER_WIDE_INT. */
2191 val
= wide_int::from (val
, len
, sgn
);
2192 len
= len
/ HOST_BITS_PER_WIDE_INT
;
2194 SET_REAL_EXP (r
, len
* HOST_BITS_PER_WIDE_INT
+ e
);
2197 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2198 for (i
= len
- 1; i
>= 0; i
--)
2200 r
->sig
[j
--] = val
.elt (i
);
2206 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2207 for (i
= len
- 1; i
>= 0; i
--)
2209 HOST_WIDE_INT e
= val
.elt (i
);
2210 r
->sig
[j
--] = e
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2222 if (DECIMAL_FLOAT_MODE_P (mode
))
2223 decimal_from_integer (r
);
2224 else if (mode
!= VOIDmode
)
2225 real_convert (r
, mode
, r
);
2228 /* Render R, an integral value, as a floating point constant with no
2229 specified exponent. */
2232 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2235 int dec_exp
, digit
, digits
;
2236 REAL_VALUE_TYPE r
, pten
;
2242 if (r
.cl
== rvc_zero
)
2251 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2252 digits
= dec_exp
+ 1;
2253 gcc_assert ((digits
+ 2) < (int)buf_size
);
2255 pten
= *real_digit (1);
2256 times_pten (&pten
, dec_exp
);
2262 digit
= rtd_divmod (&r
, &pten
);
2263 gcc_assert (digit
>= 0 && digit
<= 9);
2265 while (--digits
> 0)
2268 digit
= rtd_divmod (&r
, &pten
);
2275 /* Convert a real with an integral value to decimal float. */
2278 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2282 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2283 decimal_real_from_string (r
, str
);
2286 /* Returns 10**2**N. */
2288 static const REAL_VALUE_TYPE
*
2291 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2293 gcc_assert (n
>= 0);
2294 gcc_assert (n
< EXP_BITS
);
2296 if (tens
[n
].cl
== rvc_zero
)
2298 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2300 HOST_WIDE_INT t
= 10;
2303 for (i
= 0; i
< n
; ++i
)
2306 real_from_integer (&tens
[n
], VOIDmode
, t
, UNSIGNED
);
2310 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2311 do_multiply (&tens
[n
], t
, t
);
2318 /* Returns 10**(-2**N). */
2320 static const REAL_VALUE_TYPE
*
2321 ten_to_mptwo (int n
)
2323 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2325 gcc_assert (n
>= 0);
2326 gcc_assert (n
< EXP_BITS
);
2328 if (tens
[n
].cl
== rvc_zero
)
2329 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2336 static const REAL_VALUE_TYPE
*
2339 static REAL_VALUE_TYPE num
[10];
2341 gcc_assert (n
>= 0);
2342 gcc_assert (n
<= 9);
2344 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2345 real_from_integer (&num
[n
], VOIDmode
, n
, UNSIGNED
);
2350 /* Multiply R by 10**EXP. */
2353 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2355 REAL_VALUE_TYPE pten
, *rr
;
2356 bool negative
= (exp
< 0);
2362 pten
= *real_digit (1);
2368 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2370 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2373 do_divide (r
, r
, &pten
);
2376 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2378 const REAL_VALUE_TYPE
*
2381 static REAL_VALUE_TYPE value
;
2383 /* Initialize mathematical constants for constant folding builtins.
2384 These constants need to be given to at least 160 bits precision. */
2385 if (value
.cl
== rvc_zero
)
2388 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2389 mpfr_set_ui (m
, 1, GMP_RNDN
);
2390 mpfr_exp (m
, m
, GMP_RNDN
);
2391 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2398 /* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2400 #define CACHED_FRACTION(NAME, N) \
2401 const REAL_VALUE_TYPE * \
2404 static REAL_VALUE_TYPE value; \
2406 /* Initialize mathematical constants for constant folding builtins. \
2407 These constants need to be given to at least 160 bits \
2409 if (value.cl == rvc_zero) \
2410 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2414 CACHED_FRACTION (dconst_third_ptr
, 3)
2415 CACHED_FRACTION (dconst_quarter_ptr
, 4)
2416 CACHED_FRACTION (dconst_sixth_ptr
, 6)
2417 CACHED_FRACTION (dconst_ninth_ptr
, 9)
2419 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2421 const REAL_VALUE_TYPE
*
2422 dconst_sqrt2_ptr (void)
2424 static REAL_VALUE_TYPE value
;
2426 /* Initialize mathematical constants for constant folding builtins.
2427 These constants need to be given to at least 160 bits precision. */
2428 if (value
.cl
== rvc_zero
)
2431 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2432 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2433 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2439 /* Fills R with +Inf. */
2442 real_inf (REAL_VALUE_TYPE
*r
)
2447 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2448 we force a QNaN, else we force an SNaN. The string, if not empty,
2449 is parsed as a number and placed in the significand. Return true
2450 if the string was successfully parsed. */
2453 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2456 const struct real_format
*fmt
;
2458 fmt
= REAL_MODE_FORMAT (mode
);
2464 get_canonical_qnan (r
, 0);
2466 get_canonical_snan (r
, 0);
2472 memset (r
, 0, sizeof (*r
));
2475 /* Parse akin to strtol into the significand of R. */
2477 while (ISSPACE (*str
))
2481 else if (*str
== '+')
2486 if (*str
== 'x' || *str
== 'X')
2495 while ((d
= hex_value (*str
)) < base
)
2502 lshift_significand (r
, r
, 3);
2505 lshift_significand (r
, r
, 4);
2508 lshift_significand_1 (&u
, r
);
2509 lshift_significand (r
, r
, 3);
2510 add_significands (r
, r
, &u
);
2518 add_significands (r
, r
, &u
);
2523 /* Must have consumed the entire string for success. */
2527 /* Shift the significand into place such that the bits
2528 are in the most significant bits for the format. */
2529 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2531 /* Our MSB is always unset for NaNs. */
2532 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2534 /* Force quiet or signalling NaN. */
2535 r
->signalling
= !quiet
;
2541 /* Fills R with the largest finite value representable in mode MODE.
2542 If SIGN is nonzero, R is set to the most negative finite value. */
2545 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
2547 const struct real_format
*fmt
;
2550 fmt
= REAL_MODE_FORMAT (mode
);
2552 memset (r
, 0, sizeof (*r
));
2555 decimal_real_maxval (r
, sign
, mode
);
2560 SET_REAL_EXP (r
, fmt
->emax
);
2562 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2563 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2564 clear_significand_below (r
, np2
);
2566 if (fmt
->pnan
< fmt
->p
)
2567 /* This is an IBM extended double format made up of two IEEE
2568 doubles. The value of the long double is the sum of the
2569 values of the two parts. The most significant part is
2570 required to be the value of the long double rounded to the
2571 nearest double. Rounding means we need a slightly smaller
2572 value for LDBL_MAX. */
2573 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2577 /* Fills R with 2**N. */
2580 real_2expN (REAL_VALUE_TYPE
*r
, int n
, machine_mode fmode
)
2582 memset (r
, 0, sizeof (*r
));
2587 else if (n
< -MAX_EXP
)
2592 SET_REAL_EXP (r
, n
);
2593 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2595 if (DECIMAL_FLOAT_MODE_P (fmode
))
2596 decimal_real_convert (r
, fmode
, r
);
2601 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2605 bool round_up
= false;
2611 decimal_round_for_format (fmt
, r
);
2614 /* FIXME. We can come here via fp_easy_constant
2615 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2616 investigated whether this convert needs to be here, or
2617 something else is missing. */
2618 decimal_real_convert (r
, DFmode
, r
);
2622 emin2m1
= fmt
->emin
- 1;
2625 np2
= SIGNIFICAND_BITS
- p2
;
2629 get_zero (r
, r
->sign
);
2631 if (!fmt
->has_signed_zero
)
2636 get_inf (r
, r
->sign
);
2641 clear_significand_below (r
, np2
);
2651 /* Check the range of the exponent. If we're out of range,
2652 either underflow or overflow. */
2653 if (REAL_EXP (r
) > emax2
)
2655 else if (REAL_EXP (r
) <= emin2m1
)
2659 if (!fmt
->has_denorm
)
2661 /* Don't underflow completely until we've had a chance to round. */
2662 if (REAL_EXP (r
) < emin2m1
)
2667 diff
= emin2m1
- REAL_EXP (r
) + 1;
2671 /* De-normalize the significand. */
2672 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2673 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2677 if (!fmt
->round_towards_zero
)
2679 /* There are P2 true significand bits, followed by one guard bit,
2680 followed by one sticky bit, followed by stuff. Fold nonzero
2681 stuff into the sticky bit. */
2682 unsigned long sticky
;
2686 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2687 sticky
|= r
->sig
[i
];
2689 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2691 guard
= test_significand_bit (r
, np2
- 1);
2692 lsb
= test_significand_bit (r
, np2
);
2694 /* Round to even. */
2695 round_up
= guard
&& (sticky
|| lsb
);
2702 set_significand_bit (&u
, np2
);
2704 if (add_significands (r
, r
, &u
))
2706 /* Overflow. Means the significand had been all ones, and
2707 is now all zeros. Need to increase the exponent, and
2708 possibly re-normalize it. */
2709 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2710 if (REAL_EXP (r
) > emax2
)
2712 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2716 /* Catch underflow that we deferred until after rounding. */
2717 if (REAL_EXP (r
) <= emin2m1
)
2720 /* Clear out trailing garbage. */
2721 clear_significand_below (r
, np2
);
2724 /* Extend or truncate to a new mode. */
2727 real_convert (REAL_VALUE_TYPE
*r
, machine_mode mode
,
2728 const REAL_VALUE_TYPE
*a
)
2730 const struct real_format
*fmt
;
2732 fmt
= REAL_MODE_FORMAT (mode
);
2737 if (a
->decimal
|| fmt
->b
== 10)
2738 decimal_real_convert (r
, mode
, a
);
2740 round_for_format (fmt
, r
);
2742 /* round_for_format de-normalizes denormals. Undo just that part. */
2743 if (r
->cl
== rvc_normal
)
2747 /* Legacy. Likewise, except return the struct directly. */
2750 real_value_truncate (machine_mode mode
, REAL_VALUE_TYPE a
)
2753 real_convert (&r
, mode
, &a
);
2757 /* Return true if truncating to MODE is exact. */
2760 exact_real_truncate (machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2762 const struct real_format
*fmt
;
2766 fmt
= REAL_MODE_FORMAT (mode
);
2769 /* Don't allow conversion to denormals. */
2770 emin2m1
= fmt
->emin
- 1;
2771 if (REAL_EXP (a
) <= emin2m1
)
2774 /* After conversion to the new mode, the value must be identical. */
2775 real_convert (&t
, mode
, a
);
2776 return real_identical (&t
, a
);
2779 /* Write R to the given target format. Place the words of the result
2780 in target word order in BUF. There are always 32 bits in each
2781 long, no matter the size of the host long.
2783 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2786 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2787 const struct real_format
*fmt
)
2793 round_for_format (fmt
, &r
);
2797 (*fmt
->encode
) (fmt
, buf
, &r
);
2802 /* Similar, but look up the format from MODE. */
2805 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, machine_mode mode
)
2807 const struct real_format
*fmt
;
2809 fmt
= REAL_MODE_FORMAT (mode
);
2812 return real_to_target_fmt (buf
, r
, fmt
);
2815 /* Read R from the given target format. Read the words of the result
2816 in target word order in BUF. There are always 32 bits in each
2817 long, no matter the size of the host long. */
2820 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2821 const struct real_format
*fmt
)
2823 (*fmt
->decode
) (fmt
, r
, buf
);
2826 /* Similar, but look up the format from MODE. */
2829 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, machine_mode mode
)
2831 const struct real_format
*fmt
;
2833 fmt
= REAL_MODE_FORMAT (mode
);
2836 (*fmt
->decode
) (fmt
, r
, buf
);
2839 /* Return the number of bits of the largest binary value that the
2840 significand of MODE will hold. */
2841 /* ??? Legacy. Should get access to real_format directly. */
2844 significand_size (machine_mode mode
)
2846 const struct real_format
*fmt
;
2848 fmt
= REAL_MODE_FORMAT (mode
);
2854 /* Return the size in bits of the largest binary value that can be
2855 held by the decimal coefficient for this mode. This is one more
2856 than the number of bits required to hold the largest coefficient
2858 double log2_10
= 3.3219281;
2859 return fmt
->p
* log2_10
;
2864 /* Return a hash value for the given real value. */
2865 /* ??? The "unsigned int" return value is intended to be hashval_t,
2866 but I didn't want to pull hashtab.h into real.h. */
2869 real_hash (const REAL_VALUE_TYPE
*r
)
2874 h
= r
->cl
| (r
->sign
<< 2);
2882 h
|= REAL_EXP (r
) << 3;
2887 h
^= (unsigned int)-1;
2896 if (sizeof (unsigned long) > sizeof (unsigned int))
2897 for (i
= 0; i
< SIGSZ
; ++i
)
2899 unsigned long s
= r
->sig
[i
];
2900 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2903 for (i
= 0; i
< SIGSZ
; ++i
)
2909 /* IEEE single-precision format. */
2911 static void encode_ieee_single (const struct real_format
*fmt
,
2912 long *, const REAL_VALUE_TYPE
*);
2913 static void decode_ieee_single (const struct real_format
*,
2914 REAL_VALUE_TYPE
*, const long *);
2917 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2918 const REAL_VALUE_TYPE
*r
)
2920 unsigned long image
, sig
, exp
;
2921 unsigned long sign
= r
->sign
;
2922 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2925 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2936 image
|= 0x7fffffff;
2943 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2944 if (r
->signalling
== fmt
->qnan_msb_set
)
2955 image
|= 0x7fffffff;
2959 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2960 whereas the intermediate representation is 0.F x 2**exp.
2961 Which means we're off by one. */
2965 exp
= REAL_EXP (r
) + 127 - 1;
2978 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2981 unsigned long image
= buf
[0] & 0xffffffff;
2982 bool sign
= (image
>> 31) & 1;
2983 int exp
= (image
>> 23) & 0xff;
2985 memset (r
, 0, sizeof (*r
));
2986 image
<<= HOST_BITS_PER_LONG
- 24;
2991 if (image
&& fmt
->has_denorm
)
2995 SET_REAL_EXP (r
, -126);
2996 r
->sig
[SIGSZ
-1] = image
<< 1;
2999 else if (fmt
->has_signed_zero
)
3002 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
3008 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
3009 ^ fmt
->qnan_msb_set
);
3010 r
->sig
[SIGSZ
-1] = image
;
3022 SET_REAL_EXP (r
, exp
- 127 + 1);
3023 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
3027 const struct real_format ieee_single_format
=
3049 const struct real_format mips_single_format
=
3071 const struct real_format motorola_single_format
=
3093 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3094 single precision with the following differences:
3095 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3097 - NaNs are not supported.
3098 - The range of non-zero numbers in binary is
3099 (001)[1.]000...000 to (255)[1.]111...111.
3100 - Denormals can be represented, but are treated as +0.0 when
3101 used as an operand and are never generated as a result.
3102 - -0.0 can be represented, but a zero result is always +0.0.
3103 - the only supported rounding mode is trunction (towards zero). */
3104 const struct real_format spu_single_format
=
3126 /* IEEE double-precision format. */
3128 static void encode_ieee_double (const struct real_format
*fmt
,
3129 long *, const REAL_VALUE_TYPE
*);
3130 static void decode_ieee_double (const struct real_format
*,
3131 REAL_VALUE_TYPE
*, const long *);
3134 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3135 const REAL_VALUE_TYPE
*r
)
3137 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3138 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3140 image_hi
= r
->sign
<< 31;
3143 if (HOST_BITS_PER_LONG
== 64)
3145 sig_hi
= r
->sig
[SIGSZ
-1];
3146 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3147 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3151 sig_hi
= r
->sig
[SIGSZ
-1];
3152 sig_lo
= r
->sig
[SIGSZ
-2];
3153 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3154 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3164 image_hi
|= 2047 << 20;
3167 image_hi
|= 0x7fffffff;
3168 image_lo
= 0xffffffff;
3177 if (fmt
->canonical_nan_lsbs_set
)
3179 sig_hi
= (1 << 19) - 1;
3180 sig_lo
= 0xffffffff;
3188 if (r
->signalling
== fmt
->qnan_msb_set
)
3189 sig_hi
&= ~(1 << 19);
3192 if (sig_hi
== 0 && sig_lo
== 0)
3195 image_hi
|= 2047 << 20;
3201 image_hi
|= 0x7fffffff;
3202 image_lo
= 0xffffffff;
3207 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3208 whereas the intermediate representation is 0.F x 2**exp.
3209 Which means we're off by one. */
3213 exp
= REAL_EXP (r
) + 1023 - 1;
3214 image_hi
|= exp
<< 20;
3223 if (FLOAT_WORDS_BIG_ENDIAN
)
3224 buf
[0] = image_hi
, buf
[1] = image_lo
;
3226 buf
[0] = image_lo
, buf
[1] = image_hi
;
3230 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3233 unsigned long image_hi
, image_lo
;
3237 if (FLOAT_WORDS_BIG_ENDIAN
)
3238 image_hi
= buf
[0], image_lo
= buf
[1];
3240 image_lo
= buf
[0], image_hi
= buf
[1];
3241 image_lo
&= 0xffffffff;
3242 image_hi
&= 0xffffffff;
3244 sign
= (image_hi
>> 31) & 1;
3245 exp
= (image_hi
>> 20) & 0x7ff;
3247 memset (r
, 0, sizeof (*r
));
3249 image_hi
<<= 32 - 21;
3250 image_hi
|= image_lo
>> 21;
3251 image_hi
&= 0x7fffffff;
3252 image_lo
<<= 32 - 21;
3256 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3260 SET_REAL_EXP (r
, -1022);
3261 if (HOST_BITS_PER_LONG
== 32)
3263 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3265 r
->sig
[SIGSZ
-1] = image_hi
;
3266 r
->sig
[SIGSZ
-2] = image_lo
;
3270 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3271 r
->sig
[SIGSZ
-1] = image_hi
;
3275 else if (fmt
->has_signed_zero
)
3278 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3280 if (image_hi
|| image_lo
)
3284 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3285 if (HOST_BITS_PER_LONG
== 32)
3287 r
->sig
[SIGSZ
-1] = image_hi
;
3288 r
->sig
[SIGSZ
-2] = image_lo
;
3291 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3303 SET_REAL_EXP (r
, exp
- 1023 + 1);
3304 if (HOST_BITS_PER_LONG
== 32)
3306 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3307 r
->sig
[SIGSZ
-2] = image_lo
;
3310 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3314 const struct real_format ieee_double_format
=
3336 const struct real_format mips_double_format
=
3358 const struct real_format motorola_double_format
=
3380 /* IEEE extended real format. This comes in three flavors: Intel's as
3381 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3382 12- and 16-byte images may be big- or little endian; Motorola's is
3383 always big endian. */
3385 /* Helper subroutine which converts from the internal format to the
3386 12-byte little-endian Intel format. Functions below adjust this
3387 for the other possible formats. */
3389 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3390 const REAL_VALUE_TYPE
*r
)
3392 unsigned long image_hi
, sig_hi
, sig_lo
;
3393 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3395 image_hi
= r
->sign
<< 15;
3396 sig_hi
= sig_lo
= 0;
3408 /* Intel requires the explicit integer bit to be set, otherwise
3409 it considers the value a "pseudo-infinity". Motorola docs
3410 say it doesn't care. */
3411 sig_hi
= 0x80000000;
3416 sig_lo
= sig_hi
= 0xffffffff;
3426 if (fmt
->canonical_nan_lsbs_set
)
3428 sig_hi
= (1 << 30) - 1;
3429 sig_lo
= 0xffffffff;
3432 else if (HOST_BITS_PER_LONG
== 32)
3434 sig_hi
= r
->sig
[SIGSZ
-1];
3435 sig_lo
= r
->sig
[SIGSZ
-2];
3439 sig_lo
= r
->sig
[SIGSZ
-1];
3440 sig_hi
= sig_lo
>> 31 >> 1;
3441 sig_lo
&= 0xffffffff;
3443 if (r
->signalling
== fmt
->qnan_msb_set
)
3444 sig_hi
&= ~(1 << 30);
3447 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3450 /* Intel requires the explicit integer bit to be set, otherwise
3451 it considers the value a "pseudo-nan". Motorola docs say it
3453 sig_hi
|= 0x80000000;
3458 sig_lo
= sig_hi
= 0xffffffff;
3464 int exp
= REAL_EXP (r
);
3466 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3467 whereas the intermediate representation is 0.F x 2**exp.
3468 Which means we're off by one.
3470 Except for Motorola, which consider exp=0 and explicit
3471 integer bit set to continue to be normalized. In theory
3472 this discrepancy has been taken care of by the difference
3473 in fmt->emin in round_for_format. */
3480 gcc_assert (exp
>= 0);
3484 if (HOST_BITS_PER_LONG
== 32)
3486 sig_hi
= r
->sig
[SIGSZ
-1];
3487 sig_lo
= r
->sig
[SIGSZ
-2];
3491 sig_lo
= r
->sig
[SIGSZ
-1];
3492 sig_hi
= sig_lo
>> 31 >> 1;
3493 sig_lo
&= 0xffffffff;
3502 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3505 /* Convert from the internal format to the 12-byte Motorola format
3506 for an IEEE extended real. */
3508 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3509 const REAL_VALUE_TYPE
*r
)
3512 encode_ieee_extended (fmt
, intermed
, r
);
3514 if (r
->cl
== rvc_inf
)
3515 /* For infinity clear the explicit integer bit again, so that the
3516 format matches the canonical infinity generated by the FPU. */
3519 /* Motorola chips are assumed always to be big-endian. Also, the
3520 padding in a Motorola extended real goes between the exponent and
3521 the mantissa. At this point the mantissa is entirely within
3522 elements 0 and 1 of intermed, and the exponent entirely within
3523 element 2, so all we have to do is swap the order around, and
3524 shift element 2 left 16 bits. */
3525 buf
[0] = intermed
[2] << 16;
3526 buf
[1] = intermed
[1];
3527 buf
[2] = intermed
[0];
3530 /* Convert from the internal format to the 12-byte Intel format for
3531 an IEEE extended real. */
3533 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3534 const REAL_VALUE_TYPE
*r
)
3536 if (FLOAT_WORDS_BIG_ENDIAN
)
3538 /* All the padding in an Intel-format extended real goes at the high
3539 end, which in this case is after the mantissa, not the exponent.
3540 Therefore we must shift everything down 16 bits. */
3542 encode_ieee_extended (fmt
, intermed
, r
);
3543 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3544 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3545 buf
[2] = (intermed
[0] << 16);
3548 /* encode_ieee_extended produces what we want directly. */
3549 encode_ieee_extended (fmt
, buf
, r
);
3552 /* Convert from the internal format to the 16-byte Intel format for
3553 an IEEE extended real. */
3555 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3556 const REAL_VALUE_TYPE
*r
)
3558 /* All the padding in an Intel-format extended real goes at the high end. */
3559 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3563 /* As above, we have a helper function which converts from 12-byte
3564 little-endian Intel format to internal format. Functions below
3565 adjust for the other possible formats. */
3567 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3570 unsigned long image_hi
, sig_hi
, sig_lo
;
3574 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3575 sig_lo
&= 0xffffffff;
3576 sig_hi
&= 0xffffffff;
3577 image_hi
&= 0xffffffff;
3579 sign
= (image_hi
>> 15) & 1;
3580 exp
= image_hi
& 0x7fff;
3582 memset (r
, 0, sizeof (*r
));
3586 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3591 /* When the IEEE format contains a hidden bit, we know that
3592 it's zero at this point, and so shift up the significand
3593 and decrease the exponent to match. In this case, Motorola
3594 defines the explicit integer bit to be valid, so we don't
3595 know whether the msb is set or not. */
3596 SET_REAL_EXP (r
, fmt
->emin
);
3597 if (HOST_BITS_PER_LONG
== 32)
3599 r
->sig
[SIGSZ
-1] = sig_hi
;
3600 r
->sig
[SIGSZ
-2] = sig_lo
;
3603 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3607 else if (fmt
->has_signed_zero
)
3610 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3612 /* See above re "pseudo-infinities" and "pseudo-nans".
3613 Short summary is that the MSB will likely always be
3614 set, and that we don't care about it. */
3615 sig_hi
&= 0x7fffffff;
3617 if (sig_hi
|| sig_lo
)
3621 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3622 if (HOST_BITS_PER_LONG
== 32)
3624 r
->sig
[SIGSZ
-1] = sig_hi
;
3625 r
->sig
[SIGSZ
-2] = sig_lo
;
3628 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3640 SET_REAL_EXP (r
, exp
- 16383 + 1);
3641 if (HOST_BITS_PER_LONG
== 32)
3643 r
->sig
[SIGSZ
-1] = sig_hi
;
3644 r
->sig
[SIGSZ
-2] = sig_lo
;
3647 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3651 /* Convert from the internal format to the 12-byte Motorola format
3652 for an IEEE extended real. */
3654 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3659 /* Motorola chips are assumed always to be big-endian. Also, the
3660 padding in a Motorola extended real goes between the exponent and
3661 the mantissa; remove it. */
3662 intermed
[0] = buf
[2];
3663 intermed
[1] = buf
[1];
3664 intermed
[2] = (unsigned long)buf
[0] >> 16;
3666 decode_ieee_extended (fmt
, r
, intermed
);
3669 /* Convert from the internal format to the 12-byte Intel format for
3670 an IEEE extended real. */
3672 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3675 if (FLOAT_WORDS_BIG_ENDIAN
)
3677 /* All the padding in an Intel-format extended real goes at the high
3678 end, which in this case is after the mantissa, not the exponent.
3679 Therefore we must shift everything up 16 bits. */
3682 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3683 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3684 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3686 decode_ieee_extended (fmt
, r
, intermed
);
3689 /* decode_ieee_extended produces what we want directly. */
3690 decode_ieee_extended (fmt
, r
, buf
);
3693 /* Convert from the internal format to the 16-byte Intel format for
3694 an IEEE extended real. */
3696 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3699 /* All the padding in an Intel-format extended real goes at the high end. */
3700 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3703 const struct real_format ieee_extended_motorola_format
=
3705 encode_ieee_extended_motorola
,
3706 decode_ieee_extended_motorola
,
3722 "ieee_extended_motorola"
3725 const struct real_format ieee_extended_intel_96_format
=
3727 encode_ieee_extended_intel_96
,
3728 decode_ieee_extended_intel_96
,
3744 "ieee_extended_intel_96"
3747 const struct real_format ieee_extended_intel_128_format
=
3749 encode_ieee_extended_intel_128
,
3750 decode_ieee_extended_intel_128
,
3766 "ieee_extended_intel_128"
3769 /* The following caters to i386 systems that set the rounding precision
3770 to 53 bits instead of 64, e.g. FreeBSD. */
3771 const struct real_format ieee_extended_intel_96_round_53_format
=
3773 encode_ieee_extended_intel_96
,
3774 decode_ieee_extended_intel_96
,
3790 "ieee_extended_intel_96_round_53"
3793 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3794 numbers whose sum is equal to the extended precision value. The number
3795 with greater magnitude is first. This format has the same magnitude
3796 range as an IEEE double precision value, but effectively 106 bits of
3797 significand precision. Infinity and NaN are represented by their IEEE
3798 double precision value stored in the first number, the second number is
3799 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3801 static void encode_ibm_extended (const struct real_format
*fmt
,
3802 long *, const REAL_VALUE_TYPE
*);
3803 static void decode_ibm_extended (const struct real_format
*,
3804 REAL_VALUE_TYPE
*, const long *);
3807 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3808 const REAL_VALUE_TYPE
*r
)
3810 REAL_VALUE_TYPE u
, normr
, v
;
3811 const struct real_format
*base_fmt
;
3813 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3815 /* Renormalize R before doing any arithmetic on it. */
3817 if (normr
.cl
== rvc_normal
)
3820 /* u = IEEE double precision portion of significand. */
3822 round_for_format (base_fmt
, &u
);
3823 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3825 if (u
.cl
== rvc_normal
)
3827 do_add (&v
, &normr
, &u
, 1);
3828 /* Call round_for_format since we might need to denormalize. */
3829 round_for_format (base_fmt
, &v
);
3830 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3834 /* Inf, NaN, 0 are all representable as doubles, so the
3835 least-significant part can be 0.0. */
3842 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3845 REAL_VALUE_TYPE u
, v
;
3846 const struct real_format
*base_fmt
;
3848 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3849 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3851 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3853 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3854 do_add (r
, &u
, &v
, 0);
3860 const struct real_format ibm_extended_format
=
3862 encode_ibm_extended
,
3863 decode_ibm_extended
,
3882 const struct real_format mips_extended_format
=
3884 encode_ibm_extended
,
3885 decode_ibm_extended
,
3905 /* IEEE quad precision format. */
3907 static void encode_ieee_quad (const struct real_format
*fmt
,
3908 long *, const REAL_VALUE_TYPE
*);
3909 static void decode_ieee_quad (const struct real_format
*,
3910 REAL_VALUE_TYPE
*, const long *);
3913 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3914 const REAL_VALUE_TYPE
*r
)
3916 unsigned long image3
, image2
, image1
, image0
, exp
;
3917 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3920 image3
= r
->sign
<< 31;
3925 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3934 image3
|= 32767 << 16;
3937 image3
|= 0x7fffffff;
3938 image2
= 0xffffffff;
3939 image1
= 0xffffffff;
3940 image0
= 0xffffffff;
3947 image3
|= 32767 << 16;
3951 if (fmt
->canonical_nan_lsbs_set
)
3954 image2
= image1
= image0
= 0xffffffff;
3957 else if (HOST_BITS_PER_LONG
== 32)
3962 image3
|= u
.sig
[3] & 0xffff;
3967 image1
= image0
>> 31 >> 1;
3969 image3
|= (image2
>> 31 >> 1) & 0xffff;
3970 image0
&= 0xffffffff;
3971 image2
&= 0xffffffff;
3973 if (r
->signalling
== fmt
->qnan_msb_set
)
3977 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3982 image3
|= 0x7fffffff;
3983 image2
= 0xffffffff;
3984 image1
= 0xffffffff;
3985 image0
= 0xffffffff;
3990 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3991 whereas the intermediate representation is 0.F x 2**exp.
3992 Which means we're off by one. */
3996 exp
= REAL_EXP (r
) + 16383 - 1;
3997 image3
|= exp
<< 16;
3999 if (HOST_BITS_PER_LONG
== 32)
4004 image3
|= u
.sig
[3] & 0xffff;
4009 image1
= image0
>> 31 >> 1;
4011 image3
|= (image2
>> 31 >> 1) & 0xffff;
4012 image0
&= 0xffffffff;
4013 image2
&= 0xffffffff;
4021 if (FLOAT_WORDS_BIG_ENDIAN
)
4038 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4041 unsigned long image3
, image2
, image1
, image0
;
4045 if (FLOAT_WORDS_BIG_ENDIAN
)
4059 image0
&= 0xffffffff;
4060 image1
&= 0xffffffff;
4061 image2
&= 0xffffffff;
4063 sign
= (image3
>> 31) & 1;
4064 exp
= (image3
>> 16) & 0x7fff;
4067 memset (r
, 0, sizeof (*r
));
4071 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4076 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4077 if (HOST_BITS_PER_LONG
== 32)
4086 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4087 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4092 else if (fmt
->has_signed_zero
)
4095 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4097 if (image3
| image2
| image1
| image0
)
4101 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4103 if (HOST_BITS_PER_LONG
== 32)
4112 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4113 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4115 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4127 SET_REAL_EXP (r
, exp
- 16383 + 1);
4129 if (HOST_BITS_PER_LONG
== 32)
4138 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4139 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4141 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4142 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4146 const struct real_format ieee_quad_format
=
4168 const struct real_format mips_quad_format
=
4190 /* Descriptions of VAX floating point formats can be found beginning at
4192 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4194 The thing to remember is that they're almost IEEE, except for word
4195 order, exponent bias, and the lack of infinities, nans, and denormals.
4197 We don't implement the H_floating format here, simply because neither
4198 the VAX or Alpha ports use it. */
4200 static void encode_vax_f (const struct real_format
*fmt
,
4201 long *, const REAL_VALUE_TYPE
*);
4202 static void decode_vax_f (const struct real_format
*,
4203 REAL_VALUE_TYPE
*, const long *);
4204 static void encode_vax_d (const struct real_format
*fmt
,
4205 long *, const REAL_VALUE_TYPE
*);
4206 static void decode_vax_d (const struct real_format
*,
4207 REAL_VALUE_TYPE
*, const long *);
4208 static void encode_vax_g (const struct real_format
*fmt
,
4209 long *, const REAL_VALUE_TYPE
*);
4210 static void decode_vax_g (const struct real_format
*,
4211 REAL_VALUE_TYPE
*, const long *);
4214 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4215 const REAL_VALUE_TYPE
*r
)
4217 unsigned long sign
, exp
, sig
, image
;
4219 sign
= r
->sign
<< 15;
4229 image
= 0xffff7fff | sign
;
4233 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4234 exp
= REAL_EXP (r
) + 128;
4236 image
= (sig
<< 16) & 0xffff0000;
4250 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4251 REAL_VALUE_TYPE
*r
, const long *buf
)
4253 unsigned long image
= buf
[0] & 0xffffffff;
4254 int exp
= (image
>> 7) & 0xff;
4256 memset (r
, 0, sizeof (*r
));
4261 r
->sign
= (image
>> 15) & 1;
4262 SET_REAL_EXP (r
, exp
- 128);
4264 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4265 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4270 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4271 const REAL_VALUE_TYPE
*r
)
4273 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4278 image0
= image1
= 0;
4283 image0
= 0xffff7fff | sign
;
4284 image1
= 0xffffffff;
4288 /* Extract the significand into straight hi:lo. */
4289 if (HOST_BITS_PER_LONG
== 64)
4291 image0
= r
->sig
[SIGSZ
-1];
4292 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4293 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4297 image0
= r
->sig
[SIGSZ
-1];
4298 image1
= r
->sig
[SIGSZ
-2];
4299 image1
= (image0
<< 24) | (image1
>> 8);
4300 image0
= (image0
>> 8) & 0xffffff;
4303 /* Rearrange the half-words of the significand to match the
4305 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4306 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4308 /* Add the sign and exponent. */
4310 image0
|= (REAL_EXP (r
) + 128) << 7;
4317 if (FLOAT_WORDS_BIG_ENDIAN
)
4318 buf
[0] = image1
, buf
[1] = image0
;
4320 buf
[0] = image0
, buf
[1] = image1
;
4324 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4325 REAL_VALUE_TYPE
*r
, const long *buf
)
4327 unsigned long image0
, image1
;
4330 if (FLOAT_WORDS_BIG_ENDIAN
)
4331 image1
= buf
[0], image0
= buf
[1];
4333 image0
= buf
[0], image1
= buf
[1];
4334 image0
&= 0xffffffff;
4335 image1
&= 0xffffffff;
4337 exp
= (image0
>> 7) & 0xff;
4339 memset (r
, 0, sizeof (*r
));
4344 r
->sign
= (image0
>> 15) & 1;
4345 SET_REAL_EXP (r
, exp
- 128);
4347 /* Rearrange the half-words of the external format into
4348 proper ascending order. */
4349 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4350 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4352 if (HOST_BITS_PER_LONG
== 64)
4354 image0
= (image0
<< 31 << 1) | image1
;
4357 r
->sig
[SIGSZ
-1] = image0
;
4361 r
->sig
[SIGSZ
-1] = image0
;
4362 r
->sig
[SIGSZ
-2] = image1
;
4363 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4364 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4370 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4371 const REAL_VALUE_TYPE
*r
)
4373 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4378 image0
= image1
= 0;
4383 image0
= 0xffff7fff | sign
;
4384 image1
= 0xffffffff;
4388 /* Extract the significand into straight hi:lo. */
4389 if (HOST_BITS_PER_LONG
== 64)
4391 image0
= r
->sig
[SIGSZ
-1];
4392 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4393 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4397 image0
= r
->sig
[SIGSZ
-1];
4398 image1
= r
->sig
[SIGSZ
-2];
4399 image1
= (image0
<< 21) | (image1
>> 11);
4400 image0
= (image0
>> 11) & 0xfffff;
4403 /* Rearrange the half-words of the significand to match the
4405 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4406 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4408 /* Add the sign and exponent. */
4410 image0
|= (REAL_EXP (r
) + 1024) << 4;
4417 if (FLOAT_WORDS_BIG_ENDIAN
)
4418 buf
[0] = image1
, buf
[1] = image0
;
4420 buf
[0] = image0
, buf
[1] = image1
;
4424 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4425 REAL_VALUE_TYPE
*r
, const long *buf
)
4427 unsigned long image0
, image1
;
4430 if (FLOAT_WORDS_BIG_ENDIAN
)
4431 image1
= buf
[0], image0
= buf
[1];
4433 image0
= buf
[0], image1
= buf
[1];
4434 image0
&= 0xffffffff;
4435 image1
&= 0xffffffff;
4437 exp
= (image0
>> 4) & 0x7ff;
4439 memset (r
, 0, sizeof (*r
));
4444 r
->sign
= (image0
>> 15) & 1;
4445 SET_REAL_EXP (r
, exp
- 1024);
4447 /* Rearrange the half-words of the external format into
4448 proper ascending order. */
4449 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4450 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4452 if (HOST_BITS_PER_LONG
== 64)
4454 image0
= (image0
<< 31 << 1) | image1
;
4457 r
->sig
[SIGSZ
-1] = image0
;
4461 r
->sig
[SIGSZ
-1] = image0
;
4462 r
->sig
[SIGSZ
-2] = image1
;
4463 lshift_significand (r
, r
, 64 - 53);
4464 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4469 const struct real_format vax_f_format
=
4491 const struct real_format vax_d_format
=
4513 const struct real_format vax_g_format
=
4535 /* Encode real R into a single precision DFP value in BUF. */
4537 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4538 long *buf ATTRIBUTE_UNUSED
,
4539 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4541 encode_decimal32 (fmt
, buf
, r
);
4544 /* Decode a single precision DFP value in BUF into a real R. */
4546 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4547 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4548 const long *buf ATTRIBUTE_UNUSED
)
4550 decode_decimal32 (fmt
, r
, buf
);
4553 /* Encode real R into a double precision DFP value in BUF. */
4555 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4556 long *buf ATTRIBUTE_UNUSED
,
4557 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4559 encode_decimal64 (fmt
, buf
, r
);
4562 /* Decode a double precision DFP value in BUF into a real R. */
4564 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4565 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4566 const long *buf ATTRIBUTE_UNUSED
)
4568 decode_decimal64 (fmt
, r
, buf
);
4571 /* Encode real R into a quad precision DFP value in BUF. */
4573 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4574 long *buf ATTRIBUTE_UNUSED
,
4575 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4577 encode_decimal128 (fmt
, buf
, r
);
4580 /* Decode a quad precision DFP value in BUF into a real R. */
4582 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4583 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4584 const long *buf ATTRIBUTE_UNUSED
)
4586 decode_decimal128 (fmt
, r
, buf
);
4589 /* Single precision decimal floating point (IEEE 754). */
4590 const struct real_format decimal_single_format
=
4592 encode_decimal_single
,
4593 decode_decimal_single
,
4612 /* Double precision decimal floating point (IEEE 754). */
4613 const struct real_format decimal_double_format
=
4615 encode_decimal_double
,
4616 decode_decimal_double
,
4635 /* Quad precision decimal floating point (IEEE 754). */
4636 const struct real_format decimal_quad_format
=
4638 encode_decimal_quad
,
4639 decode_decimal_quad
,
4658 /* Encode half-precision floats. This routine is used both for the IEEE
4659 ARM alternative encodings. */
4661 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4662 const REAL_VALUE_TYPE
*r
)
4664 unsigned long image
, sig
, exp
;
4665 unsigned long sign
= r
->sign
;
4666 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4669 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4687 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4688 if (r
->signalling
== fmt
->qnan_msb_set
)
4703 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4704 whereas the intermediate representation is 0.F x 2**exp.
4705 Which means we're off by one. */
4709 exp
= REAL_EXP (r
) + 15 - 1;
4721 /* Decode half-precision floats. This routine is used both for the IEEE
4722 ARM alternative encodings. */
4724 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4727 unsigned long image
= buf
[0] & 0xffff;
4728 bool sign
= (image
>> 15) & 1;
4729 int exp
= (image
>> 10) & 0x1f;
4731 memset (r
, 0, sizeof (*r
));
4732 image
<<= HOST_BITS_PER_LONG
- 11;
4737 if (image
&& fmt
->has_denorm
)
4741 SET_REAL_EXP (r
, -14);
4742 r
->sig
[SIGSZ
-1] = image
<< 1;
4745 else if (fmt
->has_signed_zero
)
4748 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4754 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4755 ^ fmt
->qnan_msb_set
);
4756 r
->sig
[SIGSZ
-1] = image
;
4768 SET_REAL_EXP (r
, exp
- 15 + 1);
4769 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4773 /* Half-precision format, as specified in IEEE 754R. */
4774 const struct real_format ieee_half_format
=
4796 /* ARM's alternative half-precision format, similar to IEEE but with
4797 no reserved exponent value for NaNs and infinities; rather, it just
4798 extends the range of exponents by one. */
4799 const struct real_format arm_half_format
=
4821 /* A synthetic "format" for internal arithmetic. It's the size of the
4822 internal significand minus the two bits needed for proper rounding.
4823 The encode and decode routines exist only to satisfy our paranoia
4826 static void encode_internal (const struct real_format
*fmt
,
4827 long *, const REAL_VALUE_TYPE
*);
4828 static void decode_internal (const struct real_format
*,
4829 REAL_VALUE_TYPE
*, const long *);
4832 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4833 const REAL_VALUE_TYPE
*r
)
4835 memcpy (buf
, r
, sizeof (*r
));
4839 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4840 REAL_VALUE_TYPE
*r
, const long *buf
)
4842 memcpy (r
, buf
, sizeof (*r
));
4845 const struct real_format real_internal_format
=
4850 SIGNIFICAND_BITS
- 2,
4851 SIGNIFICAND_BITS
- 2,
4867 /* Calculate X raised to the integer exponent N in mode MODE and store
4868 the result in R. Return true if the result may be inexact due to
4869 loss of precision. The algorithm is the classic "left-to-right binary
4870 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4871 Algorithms", "The Art of Computer Programming", Volume 2. */
4874 real_powi (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4875 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4877 unsigned HOST_WIDE_INT bit
;
4879 bool inexact
= false;
4891 /* Don't worry about overflow, from now on n is unsigned. */
4899 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4900 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4904 inexact
|= do_multiply (&t
, &t
, &t
);
4906 inexact
|= do_multiply (&t
, &t
, x
);
4914 inexact
|= do_divide (&t
, &dconst1
, &t
);
4916 real_convert (r
, mode
, &t
);
4920 /* Round X to the nearest integer not larger in absolute value, i.e.
4921 towards zero, placing the result in R in mode MODE. */
4924 real_trunc (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4925 const REAL_VALUE_TYPE
*x
)
4927 do_fix_trunc (r
, x
);
4928 if (mode
!= VOIDmode
)
4929 real_convert (r
, mode
, r
);
4932 /* Round X to the largest integer not greater in value, i.e. round
4933 down, placing the result in R in mode MODE. */
4936 real_floor (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4937 const REAL_VALUE_TYPE
*x
)
4941 do_fix_trunc (&t
, x
);
4942 if (! real_identical (&t
, x
) && x
->sign
)
4943 do_add (&t
, &t
, &dconstm1
, 0);
4944 if (mode
!= VOIDmode
)
4945 real_convert (r
, mode
, &t
);
4950 /* Round X to the smallest integer not less then argument, i.e. round
4951 up, placing the result in R in mode MODE. */
4954 real_ceil (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4955 const REAL_VALUE_TYPE
*x
)
4959 do_fix_trunc (&t
, x
);
4960 if (! real_identical (&t
, x
) && ! x
->sign
)
4961 do_add (&t
, &t
, &dconst1
, 0);
4962 if (mode
!= VOIDmode
)
4963 real_convert (r
, mode
, &t
);
4968 /* Round X to the nearest integer, but round halfway cases away from
4972 real_round (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4973 const REAL_VALUE_TYPE
*x
)
4975 do_add (r
, x
, &dconsthalf
, x
->sign
);
4976 do_fix_trunc (r
, r
);
4977 if (mode
!= VOIDmode
)
4978 real_convert (r
, mode
, r
);
4981 /* Set the sign of R to the sign of X. */
4984 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4989 /* Check whether the real constant value given is an integer. */
4992 real_isinteger (const REAL_VALUE_TYPE
*c
, machine_mode mode
)
4994 REAL_VALUE_TYPE cint
;
4996 real_trunc (&cint
, mode
, c
);
4997 return real_identical (c
, &cint
);
5000 /* Check whether C is an integer that fits in a HOST_WIDE_INT,
5001 storing it in *INT_OUT if so. */
5004 real_isinteger (const REAL_VALUE_TYPE
*c
, HOST_WIDE_INT
*int_out
)
5006 REAL_VALUE_TYPE cint
;
5008 HOST_WIDE_INT n
= real_to_integer (c
);
5009 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
5010 if (real_identical (c
, &cint
))
5018 /* Write into BUF the maximum representable finite floating-point
5019 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5020 float string. LEN is the size of BUF, and the buffer must be large
5021 enough to contain the resulting string. */
5024 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
5029 strcpy (buf
, "0x0.");
5031 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
5034 *p
++ = "08ce"[n
- i
];
5035 sprintf (p
, "p%d", fmt
->emax
);
5036 if (fmt
->pnan
< fmt
->p
)
5038 /* This is an IBM extended double format made up of two IEEE
5039 doubles. The value of the long double is the sum of the
5040 values of the two parts. The most significant part is
5041 required to be the value of the long double rounded to the
5042 nearest double. Rounding means we need a slightly smaller
5043 value for LDBL_MAX. */
5044 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
5047 gcc_assert (strlen (buf
) < len
);
5050 /* True if mode M has a NaN representation and
5051 the treatment of NaN operands is important. */
5054 HONOR_NANS (machine_mode m
)
5056 return MODE_HAS_NANS (m
) && !flag_finite_math_only
;
5060 HONOR_NANS (const_tree t
)
5062 return HONOR_NANS (element_mode (t
));
5066 HONOR_NANS (const_rtx x
)
5068 return HONOR_NANS (GET_MODE (x
));
5071 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5074 HONOR_SNANS (machine_mode m
)
5076 return flag_signaling_nans
&& HONOR_NANS (m
);
5080 HONOR_SNANS (const_tree t
)
5082 return HONOR_SNANS (element_mode (t
));
5086 HONOR_SNANS (const_rtx x
)
5088 return HONOR_SNANS (GET_MODE (x
));
5091 /* As for HONOR_NANS, but true if the mode can represent infinity and
5092 the treatment of infinite values is important. */
5095 HONOR_INFINITIES (machine_mode m
)
5097 return MODE_HAS_INFINITIES (m
) && !flag_finite_math_only
;
5101 HONOR_INFINITIES (const_tree t
)
5103 return HONOR_INFINITIES (element_mode (t
));
5107 HONOR_INFINITIES (const_rtx x
)
5109 return HONOR_INFINITIES (GET_MODE (x
));
5112 /* Like HONOR_NANS, but true if the given mode distinguishes between
5113 positive and negative zero, and the sign of zero is important. */
5116 HONOR_SIGNED_ZEROS (machine_mode m
)
5118 return MODE_HAS_SIGNED_ZEROS (m
) && flag_signed_zeros
;
5122 HONOR_SIGNED_ZEROS (const_tree t
)
5124 return HONOR_SIGNED_ZEROS (element_mode (t
));
5128 HONOR_SIGNED_ZEROS (const_rtx x
)
5130 return HONOR_SIGNED_ZEROS (GET_MODE (x
));
5133 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5134 and the rounding mode is important. */
5137 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m
)
5139 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m
) && flag_rounding_math
;
5143 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t
)
5145 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t
));
5149 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x
)
5151 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x
));