1 /* real.cc - software floating point emulation.
2 Copyright (C) 1993-2023 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"
31 /* The floating point model used internally is not exactly IEEE 754
32 compliant, and close to the description in the ISO C99 standard,
33 section 5.2.4.2.2 Characteristics of floating types.
37 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
41 b = base or radix, here always 2
43 p = precision (the number of base-b digits in the significand)
44 f_k = the digits of the significand.
46 We differ from typical IEEE 754 encodings in that the entire
47 significand is fractional. Normalized significands are in the
50 A requirement of the model is that P be larger than the largest
51 supported target floating-point type by at least 2 bits. This gives
52 us proper rounding when we truncate to the target type. In addition,
53 E must be large enough to hold the smallest supported denormal number
56 Both of these requirements are easily satisfied. The largest target
57 significand is 113 bits; we store at least 160. The smallest
58 denormal number fits in 17 exponent bits; we store 26. */
61 /* Used to classify two numbers simultaneously. */
62 #define CLASS2(A, B) ((A) << 2 | (B))
64 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
65 #error "Some constant folding done by hand to avoid shift count warnings"
68 static void get_zero (REAL_VALUE_TYPE
*, int);
69 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
70 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
71 static void get_inf (REAL_VALUE_TYPE
*, int);
72 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
73 const REAL_VALUE_TYPE
*, unsigned int);
74 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
76 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
78 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
79 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
80 const REAL_VALUE_TYPE
*);
81 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
82 const REAL_VALUE_TYPE
*, int);
83 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
84 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
85 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
86 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
87 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
88 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
89 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
90 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
91 const REAL_VALUE_TYPE
*);
92 static void normalize (REAL_VALUE_TYPE
*);
94 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
95 const REAL_VALUE_TYPE
*, int);
96 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
97 const REAL_VALUE_TYPE
*);
98 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
99 const REAL_VALUE_TYPE
*);
100 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
101 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
103 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, REAL_VALUE_TYPE
*);
104 static void decimal_from_integer (REAL_VALUE_TYPE
*);
105 static void decimal_integer_string (char *, const REAL_VALUE_TYPE
*,
108 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
109 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
110 static const REAL_VALUE_TYPE
* real_digit (int);
111 static void times_pten (REAL_VALUE_TYPE
*, int);
113 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
115 /* Determine whether a floating-point value X is a denormal. R is
116 expected to be in denormal form, so this function is only
117 meaningful after a call to round_for_format. */
120 real_isdenormal (const REAL_VALUE_TYPE
*r
)
122 return r
->cl
== rvc_normal
&& (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
125 /* Initialize R with a positive zero. */
128 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
130 memset (r
, 0, sizeof (*r
));
134 /* Initialize R with the canonical quiet NaN. */
137 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
139 memset (r
, 0, sizeof (*r
));
146 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
148 memset (r
, 0, sizeof (*r
));
156 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
158 memset (r
, 0, sizeof (*r
));
164 /* Right-shift the significand of A by N bits; put the result in the
165 significand of R. If any one bits are shifted out, return true. */
168 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
171 unsigned long sticky
= 0;
172 unsigned int i
, ofs
= 0;
174 if (n
>= HOST_BITS_PER_LONG
)
176 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
178 n
&= HOST_BITS_PER_LONG
- 1;
183 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
184 for (i
= 0; i
< SIGSZ
; ++i
)
187 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
188 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
189 << (HOST_BITS_PER_LONG
- n
)));
194 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
195 r
->sig
[i
] = a
->sig
[ofs
+ i
];
196 for (; i
< SIGSZ
; ++i
)
203 /* Right-shift the significand of A by N bits; put the result in the
207 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
210 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
212 n
&= HOST_BITS_PER_LONG
- 1;
215 for (i
= 0; i
< SIGSZ
; ++i
)
218 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
219 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
220 << (HOST_BITS_PER_LONG
- n
)));
225 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
226 r
->sig
[i
] = a
->sig
[ofs
+ i
];
227 for (; i
< SIGSZ
; ++i
)
232 /* Left-shift the significand of A by N bits; put the result in the
236 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
239 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
241 n
&= HOST_BITS_PER_LONG
- 1;
244 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
245 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
246 for (; i
< SIGSZ
; ++i
)
247 r
->sig
[SIGSZ
-1-i
] = 0;
250 for (i
= 0; i
< SIGSZ
; ++i
)
253 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
254 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
255 >> (HOST_BITS_PER_LONG
- n
)));
259 /* Likewise, but N is specialized to 1. */
262 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
266 for (i
= SIGSZ
- 1; i
> 0; --i
)
267 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
268 r
->sig
[0] = a
->sig
[0] << 1;
271 /* Add the significands of A and B, placing the result in R. Return
272 true if there was carry out of the most significant word. */
275 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
276 const REAL_VALUE_TYPE
*b
)
281 for (i
= 0; i
< SIGSZ
; ++i
)
283 unsigned long ai
= a
->sig
[i
];
284 unsigned long ri
= ai
+ b
->sig
[i
];
300 /* Subtract the significands of A and B, placing the result in R. CARRY is
301 true if there's a borrow incoming to the least significant word.
302 Return true if there was borrow out of the most significant word. */
305 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
306 const REAL_VALUE_TYPE
*b
, int carry
)
310 for (i
= 0; i
< SIGSZ
; ++i
)
312 unsigned long ai
= a
->sig
[i
];
313 unsigned long ri
= ai
- b
->sig
[i
];
329 /* Negate the significand A, placing the result in R. */
332 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
337 for (i
= 0; i
< SIGSZ
; ++i
)
339 unsigned long ri
, ai
= a
->sig
[i
];
358 /* Compare significands. Return tri-state vs zero. */
361 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
365 for (i
= SIGSZ
- 1; i
>= 0; --i
)
367 unsigned long ai
= a
->sig
[i
];
368 unsigned long bi
= b
->sig
[i
];
379 /* Return true if A is nonzero. */
382 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
386 for (i
= SIGSZ
- 1; i
>= 0; --i
)
393 /* Set bit N of the significand of R. */
396 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
398 r
->sig
[n
/ HOST_BITS_PER_LONG
]
399 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
402 /* Clear bit N of the significand of R. */
405 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
407 r
->sig
[n
/ HOST_BITS_PER_LONG
]
408 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
411 /* Test bit N of the significand of R. */
414 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
416 /* ??? Compiler bug here if we return this expression directly.
417 The conversion to bool strips the "&1" and we wind up testing
418 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
419 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
423 /* Clear bits 0..N-1 of the significand of R. */
426 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
428 int i
, w
= n
/ HOST_BITS_PER_LONG
;
430 for (i
= 0; i
< w
; ++i
)
433 /* We are actually passing N == SIGNIFICAND_BITS which would result
434 in an out-of-bound access below. */
435 if (n
% HOST_BITS_PER_LONG
!= 0)
436 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
439 /* Divide the significands of A and B, placing the result in R. Return
440 true if the division was inexact. */
443 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
444 const REAL_VALUE_TYPE
*b
)
447 int i
, bit
= SIGNIFICAND_BITS
- 1;
448 unsigned long msb
, inexact
;
451 memset (r
->sig
, 0, sizeof (r
->sig
));
457 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
458 lshift_significand_1 (&u
, &u
);
460 if (msb
|| cmp_significands (&u
, b
) >= 0)
462 sub_significands (&u
, &u
, b
, 0);
463 set_significand_bit (r
, bit
);
468 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
474 /* Adjust the exponent and significand of R such that the most
475 significant bit is set. We underflow to zero and overflow to
476 infinity here, without denormals. (The intermediate representation
477 exponent is large enough to handle target denormals normalized.) */
480 normalize (REAL_VALUE_TYPE
*r
)
488 /* Find the first word that is nonzero. */
489 for (i
= SIGSZ
- 1; i
>= 0; i
--)
491 shift
+= HOST_BITS_PER_LONG
;
495 /* Zero significand flushes to zero. */
503 /* Find the first bit that is nonzero. */
505 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
511 exp
= REAL_EXP (r
) - shift
;
513 get_inf (r
, r
->sign
);
514 else if (exp
< -MAX_EXP
)
515 get_zero (r
, r
->sign
);
518 SET_REAL_EXP (r
, exp
);
519 lshift_significand (r
, r
, shift
);
524 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
525 result may be inexact due to a loss of precision. */
528 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
529 const REAL_VALUE_TYPE
*b
, int subtract_p
)
533 bool inexact
= false;
535 /* Determine if we need to add or subtract. */
537 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
539 switch (CLASS2 (a
->cl
, b
->cl
))
541 case CLASS2 (rvc_zero
, rvc_zero
):
542 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
543 get_zero (r
, sign
& !subtract_p
);
546 case CLASS2 (rvc_zero
, rvc_normal
):
547 case CLASS2 (rvc_zero
, rvc_inf
):
548 case CLASS2 (rvc_zero
, rvc_nan
):
550 case CLASS2 (rvc_normal
, rvc_nan
):
551 case CLASS2 (rvc_inf
, rvc_nan
):
552 case CLASS2 (rvc_nan
, rvc_nan
):
553 /* ANY + NaN = NaN. */
554 case CLASS2 (rvc_normal
, rvc_inf
):
557 /* Make resulting NaN value to be qNaN. The caller has the
558 responsibility to avoid the operation if flag_signaling_nans
561 r
->sign
= sign
^ subtract_p
;
564 case CLASS2 (rvc_normal
, rvc_zero
):
565 case CLASS2 (rvc_inf
, rvc_zero
):
566 case CLASS2 (rvc_nan
, rvc_zero
):
568 case CLASS2 (rvc_nan
, rvc_normal
):
569 case CLASS2 (rvc_nan
, rvc_inf
):
570 /* NaN + ANY = NaN. */
571 case CLASS2 (rvc_inf
, rvc_normal
):
574 /* Make resulting NaN value to be qNaN. The caller has the
575 responsibility to avoid the operation if flag_signaling_nans
580 case CLASS2 (rvc_inf
, rvc_inf
):
582 /* Inf - Inf = NaN. */
583 get_canonical_qnan (r
, 0);
585 /* Inf + Inf = Inf. */
589 case CLASS2 (rvc_normal
, rvc_normal
):
596 /* Swap the arguments such that A has the larger exponent. */
597 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
600 const REAL_VALUE_TYPE
*t
;
607 /* If the exponents are not identical, we need to shift the
608 significand of B down. */
611 /* If the exponents are too far apart, the significands
612 do not overlap, which makes the subtraction a noop. */
613 if (dexp
>= SIGNIFICAND_BITS
)
620 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
626 if (sub_significands (r
, a
, b
, inexact
))
628 /* We got a borrow out of the subtraction. That means that
629 A and B had the same exponent, and B had the larger
630 significand. We need to swap the sign and negate the
633 neg_significand (r
, r
);
638 if (add_significands (r
, a
, b
))
640 /* We got carry out of the addition. This means we need to
641 shift the significand back down one bit and increase the
643 inexact
|= sticky_rshift_significand (r
, r
, 1);
644 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
655 SET_REAL_EXP (r
, exp
);
656 /* Zero out the remaining fields. */
661 /* Re-normalize the result. */
664 /* Special case: if the subtraction results in zero, the result
666 if (r
->cl
== rvc_zero
)
669 r
->sig
[0] |= inexact
;
674 /* Calculate R = A * B. Return true if the result may be inexact. */
677 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
678 const REAL_VALUE_TYPE
*b
)
680 REAL_VALUE_TYPE u
, t
, *rr
;
681 unsigned int i
, j
, k
;
682 int sign
= a
->sign
^ b
->sign
;
683 bool inexact
= false;
685 switch (CLASS2 (a
->cl
, b
->cl
))
687 case CLASS2 (rvc_zero
, rvc_zero
):
688 case CLASS2 (rvc_zero
, rvc_normal
):
689 case CLASS2 (rvc_normal
, rvc_zero
):
690 /* +-0 * ANY = 0 with appropriate sign. */
694 case CLASS2 (rvc_zero
, rvc_nan
):
695 case CLASS2 (rvc_normal
, rvc_nan
):
696 case CLASS2 (rvc_inf
, rvc_nan
):
697 case CLASS2 (rvc_nan
, rvc_nan
):
698 /* ANY * NaN = NaN. */
700 /* Make resulting NaN value to be qNaN. The caller has the
701 responsibility to avoid the operation if flag_signaling_nans
707 case CLASS2 (rvc_nan
, rvc_zero
):
708 case CLASS2 (rvc_nan
, rvc_normal
):
709 case CLASS2 (rvc_nan
, rvc_inf
):
710 /* NaN * ANY = NaN. */
712 /* Make resulting NaN value to be qNaN. The caller has the
713 responsibility to avoid the operation if flag_signaling_nans
719 case CLASS2 (rvc_zero
, rvc_inf
):
720 case CLASS2 (rvc_inf
, rvc_zero
):
722 get_canonical_qnan (r
, sign
);
725 case CLASS2 (rvc_inf
, rvc_inf
):
726 case CLASS2 (rvc_normal
, rvc_inf
):
727 case CLASS2 (rvc_inf
, rvc_normal
):
728 /* Inf * Inf = Inf, R * Inf = Inf */
732 case CLASS2 (rvc_normal
, rvc_normal
):
739 if (r
== a
|| r
== b
)
745 /* Collect all the partial products. Since we don't have sure access
746 to a widening multiply, we split each long into two half-words.
748 Consider the long-hand form of a four half-word multiplication:
758 We construct partial products of the widened half-word products
759 that are known to not overlap, e.g. DF+DH. Each such partial
760 product is given its proper exponent, which allows us to sum them
761 and obtain the finished product. */
763 for (i
= 0; i
< SIGSZ
* 2; ++i
)
765 unsigned long ai
= a
->sig
[i
/ 2];
767 ai
>>= HOST_BITS_PER_LONG
/ 2;
769 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
774 for (j
= 0; j
< 2; ++j
)
776 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
777 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
786 /* Would underflow to zero, which we shouldn't bother adding. */
791 memset (&u
, 0, sizeof (u
));
793 SET_REAL_EXP (&u
, exp
);
795 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
797 unsigned long bi
= b
->sig
[k
/ 2];
799 bi
>>= HOST_BITS_PER_LONG
/ 2;
801 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
803 u
.sig
[k
/ 2] = ai
* bi
;
807 inexact
|= do_add (rr
, rr
, &u
, 0);
818 /* Calculate R = A / B. Return true if the result may be inexact. */
821 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
822 const REAL_VALUE_TYPE
*b
)
824 int exp
, sign
= a
->sign
^ b
->sign
;
825 REAL_VALUE_TYPE t
, *rr
;
828 switch (CLASS2 (a
->cl
, b
->cl
))
830 case CLASS2 (rvc_zero
, rvc_zero
):
832 case CLASS2 (rvc_inf
, rvc_inf
):
833 /* Inf / Inf = NaN. */
834 get_canonical_qnan (r
, sign
);
837 case CLASS2 (rvc_zero
, rvc_normal
):
838 case CLASS2 (rvc_zero
, rvc_inf
):
840 case CLASS2 (rvc_normal
, rvc_inf
):
845 case CLASS2 (rvc_normal
, rvc_zero
):
847 case CLASS2 (rvc_inf
, rvc_zero
):
852 case CLASS2 (rvc_zero
, rvc_nan
):
853 case CLASS2 (rvc_normal
, rvc_nan
):
854 case CLASS2 (rvc_inf
, rvc_nan
):
855 case CLASS2 (rvc_nan
, rvc_nan
):
856 /* ANY / NaN = NaN. */
858 /* Make resulting NaN value to be qNaN. The caller has the
859 responsibility to avoid the operation if flag_signaling_nans
865 case CLASS2 (rvc_nan
, rvc_zero
):
866 case CLASS2 (rvc_nan
, rvc_normal
):
867 case CLASS2 (rvc_nan
, rvc_inf
):
868 /* NaN / ANY = NaN. */
870 /* Make resulting NaN value to be qNaN. The caller has the
871 responsibility to avoid the operation if flag_signaling_nans
877 case CLASS2 (rvc_inf
, rvc_normal
):
882 case CLASS2 (rvc_normal
, rvc_normal
):
889 if (r
== a
|| r
== b
)
894 /* Make sure all fields in the result are initialized. */
899 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
910 SET_REAL_EXP (rr
, exp
);
912 inexact
= div_significands (rr
, a
, b
);
914 /* Re-normalize the result. */
916 rr
->sig
[0] |= inexact
;
924 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
925 one of the two operands is a NaN. */
928 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
933 switch (CLASS2 (a
->cl
, b
->cl
))
935 case CLASS2 (rvc_zero
, rvc_zero
):
936 /* Sign of zero doesn't matter for compares. */
939 case CLASS2 (rvc_normal
, rvc_zero
):
940 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
942 return decimal_do_compare (a
, b
, nan_result
);
944 case CLASS2 (rvc_inf
, rvc_zero
):
945 case CLASS2 (rvc_inf
, rvc_normal
):
946 return (a
->sign
? -1 : 1);
948 case CLASS2 (rvc_inf
, rvc_inf
):
949 return -a
->sign
- -b
->sign
;
951 case CLASS2 (rvc_zero
, rvc_normal
):
952 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
954 return decimal_do_compare (a
, b
, nan_result
);
956 case CLASS2 (rvc_zero
, rvc_inf
):
957 case CLASS2 (rvc_normal
, rvc_inf
):
958 return (b
->sign
? 1 : -1);
960 case CLASS2 (rvc_zero
, rvc_nan
):
961 case CLASS2 (rvc_normal
, rvc_nan
):
962 case CLASS2 (rvc_inf
, rvc_nan
):
963 case CLASS2 (rvc_nan
, rvc_nan
):
964 case CLASS2 (rvc_nan
, rvc_zero
):
965 case CLASS2 (rvc_nan
, rvc_normal
):
966 case CLASS2 (rvc_nan
, rvc_inf
):
969 case CLASS2 (rvc_normal
, rvc_normal
):
976 if (a
->decimal
|| b
->decimal
)
977 return decimal_do_compare (a
, b
, nan_result
);
979 if (a
->sign
!= b
->sign
)
980 return -a
->sign
- -b
->sign
;
982 if (REAL_EXP (a
) > REAL_EXP (b
))
984 else if (REAL_EXP (a
) < REAL_EXP (b
))
987 ret
= cmp_significands (a
, b
);
989 return (a
->sign
? -ret
: ret
);
992 /* Return A truncated to an integral value toward zero. */
995 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
1004 /* Make resulting NaN value to be qNaN. The caller has the
1005 responsibility to avoid the operation if flag_signaling_nans
1013 decimal_do_fix_trunc (r
, a
);
1016 if (REAL_EXP (r
) <= 0)
1017 get_zero (r
, r
->sign
);
1018 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
1019 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
1027 /* Perform the binary or unary operation described by CODE.
1028 For a unary operation, leave OP1 NULL. This function returns
1029 true if the result may be inexact due to loss of precision. */
1032 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
1033 const REAL_VALUE_TYPE
*op1
)
1035 enum tree_code code
= (enum tree_code
) icode
;
1037 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1038 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1043 /* Clear any padding areas in *r if it isn't equal to one of the
1044 operands so that we can later do bitwise comparisons later on. */
1045 if (r
!= op0
&& r
!= op1
)
1046 memset (r
, '\0', sizeof (*r
));
1047 return do_add (r
, op0
, op1
, 0);
1050 if (r
!= op0
&& r
!= op1
)
1051 memset (r
, '\0', sizeof (*r
));
1052 return do_add (r
, op0
, op1
, 1);
1055 if (r
!= op0
&& r
!= op1
)
1056 memset (r
, '\0', sizeof (*r
));
1057 return do_multiply (r
, op0
, op1
);
1060 if (r
!= op0
&& r
!= op1
)
1061 memset (r
, '\0', sizeof (*r
));
1062 return do_divide (r
, op0
, op1
);
1065 if (op1
->cl
== rvc_nan
)
1068 /* Make resulting NaN value to be qNaN. The caller has the
1069 responsibility to avoid the operation if flag_signaling_nans
1073 else if (do_compare (op0
, op1
, -1) < 0)
1080 if (op1
->cl
== rvc_nan
)
1083 /* Make resulting NaN value to be qNaN. The caller has the
1084 responsibility to avoid the operation if flag_signaling_nans
1088 else if (do_compare (op0
, op1
, 1) < 0)
1104 case FIX_TRUNC_EXPR
:
1105 do_fix_trunc (r
, op0
);
1115 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1118 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1123 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1126 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1130 /* Return whether OP0 == OP1. */
1133 real_equal (const REAL_VALUE_TYPE
*op0
, const REAL_VALUE_TYPE
*op1
)
1135 return do_compare (op0
, op1
, -1) == 0;
1138 /* Return whether OP0 < OP1. */
1141 real_less (const REAL_VALUE_TYPE
*op0
, const REAL_VALUE_TYPE
*op1
)
1143 return do_compare (op0
, op1
, 1) < 0;
1147 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1148 const REAL_VALUE_TYPE
*op1
)
1150 enum tree_code code
= (enum tree_code
) icode
;
1155 return real_less (op0
, op1
);
1157 return do_compare (op0
, op1
, 1) <= 0;
1159 return do_compare (op0
, op1
, -1) > 0;
1161 return do_compare (op0
, op1
, -1) >= 0;
1163 return real_equal (op0
, op1
);
1165 return do_compare (op0
, op1
, -1) != 0;
1166 case UNORDERED_EXPR
:
1167 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1169 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1171 return do_compare (op0
, op1
, -1) < 0;
1173 return do_compare (op0
, op1
, -1) <= 0;
1175 return do_compare (op0
, op1
, 1) > 0;
1177 return do_compare (op0
, op1
, 1) >= 0;
1179 return do_compare (op0
, op1
, 0) == 0;
1181 return do_compare (op0
, op1
, 0) != 0;
1188 /* Return floor log2(R). */
1191 real_exponent (const REAL_VALUE_TYPE
*r
)
1199 return (unsigned int)-1 >> 1;
1201 return REAL_EXP (r
);
1207 /* R = OP0 * 2**EXP. */
1210 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1218 /* Make resulting NaN value to be qNaN. The caller has the
1219 responsibility to avoid the operation if flag_signaling_nans
1225 exp
+= REAL_EXP (op0
);
1227 get_inf (r
, r
->sign
);
1228 else if (exp
< -MAX_EXP
)
1229 get_zero (r
, r
->sign
);
1231 SET_REAL_EXP (r
, exp
);
1239 /* Determine whether a floating-point value X is infinite. */
1242 real_isinf (const REAL_VALUE_TYPE
*r
)
1244 return (r
->cl
== rvc_inf
);
1247 /* Determine whether a floating-point value X is infinite with SIGN. */
1250 real_isinf (const REAL_VALUE_TYPE
*r
, bool sign
)
1252 return real_isinf (r
) && r
->sign
== sign
;
1255 /* Determine whether a floating-point value X is a NaN. */
1258 real_isnan (const REAL_VALUE_TYPE
*r
)
1260 return (r
->cl
== rvc_nan
);
1263 /* Determine whether a floating-point value X is a signaling NaN. */
1264 bool real_issignaling_nan (const REAL_VALUE_TYPE
*r
)
1266 return real_isnan (r
) && r
->signalling
;
1269 /* Determine whether a floating-point value X is finite. */
1272 real_isfinite (const REAL_VALUE_TYPE
*r
)
1274 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1277 /* Determine whether a floating-point value X is negative. */
1280 real_isneg (const REAL_VALUE_TYPE
*r
)
1285 /* Determine whether a floating-point value X is plus or minus zero. */
1288 real_iszero (const REAL_VALUE_TYPE
*r
)
1290 return r
->cl
== rvc_zero
;
1293 /* Determine whether a floating-point value X is zero with SIGN. */
1296 real_iszero (const REAL_VALUE_TYPE
*r
, bool sign
)
1298 return real_iszero (r
) && r
->sign
== sign
;
1301 /* Determine whether a floating-point value X is minus zero. */
1304 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1306 return r
->sign
&& r
->cl
== rvc_zero
;
1309 /* Compare two floating-point objects for bitwise identity. */
1312 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1318 if (a
->sign
!= b
->sign
)
1328 if (a
->decimal
!= b
->decimal
)
1330 if (REAL_EXP (a
) != REAL_EXP (b
))
1335 if (a
->signalling
!= b
->signalling
)
1337 /* The significand is ignored for canonical NaNs. */
1338 if (a
->canonical
|| b
->canonical
)
1339 return a
->canonical
== b
->canonical
;
1346 for (i
= 0; i
< SIGSZ
; ++i
)
1347 if (a
->sig
[i
] != b
->sig
[i
])
1353 /* Try to change R into its exact multiplicative inverse in format FMT.
1354 Return true if successful. */
1357 exact_real_inverse (format_helper fmt
, REAL_VALUE_TYPE
*r
)
1359 const REAL_VALUE_TYPE
*one
= real_digit (1);
1363 if (r
->cl
!= rvc_normal
)
1366 /* Check for a power of two: all significand bits zero except the MSB. */
1367 for (i
= 0; i
< SIGSZ
-1; ++i
)
1370 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1373 /* Find the inverse and truncate to the required format. */
1374 do_divide (&u
, one
, r
);
1375 real_convert (&u
, fmt
, &u
);
1377 /* The rounding may have overflowed. */
1378 if (u
.cl
!= rvc_normal
)
1380 for (i
= 0; i
< SIGSZ
-1; ++i
)
1383 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1390 /* Return true if arithmetic on values in IMODE that were promoted
1391 from values in TMODE is equivalent to direct arithmetic on values
1395 real_can_shorten_arithmetic (machine_mode imode
, machine_mode tmode
)
1397 const struct real_format
*tfmt
, *ifmt
;
1398 tfmt
= REAL_MODE_FORMAT (tmode
);
1399 ifmt
= REAL_MODE_FORMAT (imode
);
1400 /* These conditions are conservative rather than trying to catch the
1401 exact boundary conditions; the main case to allow is IEEE float
1403 return (ifmt
->b
== tfmt
->b
1404 && ifmt
->p
> 2 * tfmt
->p
1405 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1406 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1407 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1408 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1409 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1410 && (ifmt
->has_sign_dependent_rounding
1411 == tfmt
->has_sign_dependent_rounding
)
1412 && ifmt
->has_nans
>= tfmt
->has_nans
1413 && ifmt
->has_inf
>= tfmt
->has_inf
1414 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1415 && !MODE_COMPOSITE_P (tmode
)
1416 && !MODE_COMPOSITE_P (imode
));
1419 /* Render R as an integer. */
1422 real_to_integer (const REAL_VALUE_TYPE
*r
)
1424 unsigned HOST_WIDE_INT i
;
1435 i
= HOST_WIDE_INT_1U
<< (HOST_BITS_PER_WIDE_INT
- 1);
1442 return decimal_real_to_integer (r
);
1444 if (REAL_EXP (r
) <= 0)
1446 /* Only force overflow for unsigned overflow. Signed overflow is
1447 undefined, so it doesn't matter what we return, and some callers
1448 expect to be able to use this routine for both signed and
1449 unsigned conversions. */
1450 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1453 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1454 i
= r
->sig
[SIGSZ
-1];
1457 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1458 i
= r
->sig
[SIGSZ
-1];
1459 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1460 i
|= r
->sig
[SIGSZ
-2];
1463 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1474 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1475 be represented in precision, *FAIL is set to TRUE. */
1478 real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
1480 HOST_WIDE_INT valb
[WIDE_INT_MAX_INL_ELTS
], *val
;
1489 return wi::zero (precision
);
1497 return wi::set_bit_in_zero (precision
- 1, precision
);
1499 return ~wi::set_bit_in_zero (precision
- 1, precision
);
1503 return decimal_real_to_integer (r
, fail
, precision
);
1508 /* Only force overflow for unsigned overflow. Signed overflow is
1509 undefined, so it doesn't matter what we return, and some callers
1510 expect to be able to use this routine for both signed and
1511 unsigned conversions. */
1512 if (exp
> precision
)
1515 /* Put the significand into a wide_int that has precision W, which
1516 is the smallest HWI-multiple that has at least PRECISION bits.
1517 This ensures that the top bit of the significand is in the
1518 top bit of the wide_int. */
1519 words
= ((precision
+ HOST_BITS_PER_WIDE_INT
- 1)
1520 / HOST_BITS_PER_WIDE_INT
);
1522 if (UNLIKELY (words
> WIDE_INT_MAX_INL_ELTS
))
1523 val
= XALLOCAVEC (HOST_WIDE_INT
, words
);
1524 w
= words
* HOST_BITS_PER_WIDE_INT
;
1526 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1527 for (int i
= 0; i
< words
; i
++)
1529 int j
= SIGSZ
- words
+ i
;
1530 val
[i
] = (j
< 0) ? 0 : r
->sig
[j
];
1533 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1534 for (int i
= 0; i
< words
; i
++)
1536 int j
= SIGSZ
- (words
* 2) + (i
* 2);
1543 val
[i
] |= (unsigned HOST_WIDE_INT
) r
->sig
[j
] << HOST_BITS_PER_LONG
;
1546 /* Shift the value into place and truncate to the desired precision. */
1547 result
= wide_int::from_array (val
, words
, w
);
1548 result
= wi::lrshift (result
, w
- exp
);
1549 result
= wide_int::from (result
, precision
, UNSIGNED
);
1561 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1562 of NUM / DEN. Return the quotient and place the remainder in NUM.
1563 It is expected that NUM / DEN are close enough that the quotient is
1566 static unsigned long
1567 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1569 unsigned long q
, msb
;
1570 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1579 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1581 lshift_significand_1 (num
, num
);
1583 if (msb
|| cmp_significands (num
, den
) >= 0)
1585 sub_significands (num
, num
, den
, 0);
1589 while (--expn
>= expd
);
1591 SET_REAL_EXP (num
, expd
);
1597 /* Render R as a decimal floating point constant. Emit DIGITS significant
1598 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1599 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1600 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1601 to a string that, when parsed back in mode MODE, yields the same value. */
1603 #define M_LOG10_2 0.30102999566398119521
1606 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1607 size_t buf_size
, size_t digits
,
1608 int crop_trailing_zeros
, machine_mode mode
)
1610 const struct real_format
*fmt
= NULL
;
1611 const REAL_VALUE_TYPE
*one
, *ten
;
1612 REAL_VALUE_TYPE r
, pten
, u
, v
;
1613 int dec_exp
, cmp_one
, digit
;
1615 char *p
, *first
, *last
;
1619 if (mode
!= VOIDmode
)
1621 fmt
= REAL_MODE_FORMAT (mode
);
1629 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1634 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1637 /* ??? Print the significand as well, if not canonical? */
1638 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1639 (r_orig
->signalling
? 'S' : 'Q'));
1647 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1651 /* Bound the number of digits printed by the size of the representation. */
1652 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1653 if (digits
== 0 || digits
> max_digits
)
1654 digits
= max_digits
;
1656 /* Estimate the decimal exponent, and compute the length of the string it
1657 will print as. Be conservative and add one to account for possible
1658 overflow or rounding error. */
1659 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1660 for (max_digits
= 1; dec_exp
; max_digits
++)
1663 /* Bound the number of digits printed by the size of the output buffer. */
1664 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1665 gcc_assert (max_digits
<= buf_size
);
1666 if (digits
> max_digits
)
1667 digits
= max_digits
;
1669 one
= real_digit (1);
1670 ten
= ten_to_ptwo (0);
1678 cmp_one
= do_compare (&r
, one
, 0);
1683 /* Number is greater than one. Convert significand to an integer
1684 and strip trailing decimal zeros. */
1687 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1689 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1690 m
= floor_log2 (max_digits
);
1692 /* Iterate over the bits of the possible powers of 10 that might
1693 be present in U and eliminate them. That is, if we find that
1694 10**2**M divides U evenly, keep the division and increase
1700 do_divide (&t
, &u
, ten_to_ptwo (m
));
1701 do_fix_trunc (&v
, &t
);
1702 if (cmp_significands (&v
, &t
) == 0)
1710 /* Revert the scaling to integer that we performed earlier. */
1711 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1712 - (SIGNIFICAND_BITS
- 1));
1715 /* Find power of 10. Do this by dividing out 10**2**M when
1716 this is larger than the current remainder. Fill PTEN with
1717 the power of 10 that we compute. */
1718 if (REAL_EXP (&r
) > 0)
1720 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1723 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1724 if (do_compare (&u
, ptentwo
, 0) >= 0)
1726 do_divide (&u
, &u
, ptentwo
);
1727 do_multiply (&pten
, &pten
, ptentwo
);
1734 /* We managed to divide off enough tens in the above reduction
1735 loop that we've now got a negative exponent. Fall into the
1736 less-than-one code to compute the proper value for PTEN. */
1743 /* Number is less than one. Pad significand with leading
1749 /* Stop if we'd shift bits off the bottom. */
1753 do_multiply (&u
, &v
, ten
);
1755 /* Stop if we're now >= 1 or zero. */
1756 if (REAL_EXP (&u
) > 0 || u
.cl
== rvc_zero
)
1764 /* Find power of 10. Do this by multiplying in P=10**2**M when
1765 the current remainder is smaller than 1/P. Fill PTEN with the
1766 power of 10 that we compute. */
1767 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1770 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1771 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1773 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1775 do_multiply (&v
, &v
, ptentwo
);
1776 do_multiply (&pten
, &pten
, ptentwo
);
1782 /* Invert the positive power of 10 that we've collected so far. */
1783 do_divide (&pten
, one
, &pten
);
1791 /* At this point, PTEN should contain the nearest power of 10 smaller
1792 than R, such that this division produces the first digit.
1794 Using a divide-step primitive that returns the complete integral
1795 remainder avoids the rounding error that would be produced if
1796 we were to use do_divide here and then simply multiply by 10 for
1797 each subsequent digit. */
1799 digit
= rtd_divmod (&r
, &pten
);
1801 /* Be prepared for error in that division via underflow ... */
1802 if (digit
== 0 && cmp_significand_0 (&r
))
1804 /* Multiply by 10 and try again. */
1805 do_multiply (&r
, &r
, ten
);
1806 digit
= rtd_divmod (&r
, &pten
);
1808 gcc_assert (digit
!= 0);
1811 /* ... or overflow. */
1821 gcc_assert (digit
<= 10);
1825 /* Generate subsequent digits. */
1826 while (--digits
> 0)
1828 do_multiply (&r
, &r
, ten
);
1829 digit
= rtd_divmod (&r
, &pten
);
1834 /* Generate one more digit with which to do rounding. */
1835 do_multiply (&r
, &r
, ten
);
1836 digit
= rtd_divmod (&r
, &pten
);
1838 /* Round the result. */
1839 if (fmt
&& fmt
->round_towards_zero
)
1841 /* If the format uses round towards zero when parsing the string
1842 back in, we need to always round away from zero here. */
1843 if (cmp_significand_0 (&r
))
1845 round_up
= digit
> 0;
1851 /* Round to nearest. If R is nonzero there are additional
1852 nonzero digits to be extracted. */
1853 if (cmp_significand_0 (&r
))
1855 /* Round to even. */
1856 else if ((p
[-1] - '0') & 1)
1860 round_up
= digit
> 5;
1877 /* Carry out of the first digit. This means we had all 9's and
1878 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1886 /* Insert the decimal point. */
1887 first
[0] = first
[1];
1890 /* If requested, drop trailing zeros. Never crop past "1.0". */
1891 if (crop_trailing_zeros
)
1892 while (last
> first
+ 3 && last
[-1] == '0')
1895 /* Append the exponent. */
1896 sprintf (last
, "e%+d", dec_exp
);
1898 /* Verify that we can read the original value back in. */
1899 if (flag_checking
&& mode
!= VOIDmode
)
1901 real_from_string (&r
, str
);
1902 real_convert (&r
, mode
, &r
);
1903 gcc_assert (real_identical (&r
, r_orig
));
1907 /* Likewise, except always uses round-to-nearest. */
1910 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1911 size_t digits
, int crop_trailing_zeros
)
1913 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1914 digits
, crop_trailing_zeros
, VOIDmode
);
1918 debug (const REAL_VALUE_TYPE
&r
)
1921 real_to_hexadecimal (s
, &r
, sizeof (s
), 0, 1);
1922 fprintf (stderr
, "%s\n", s
);
1925 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1926 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1927 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1928 strip trailing zeros. */
1931 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1932 size_t digits
, int crop_trailing_zeros
)
1934 int i
, j
, exp
= REAL_EXP (r
);
1947 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1950 /* ??? Print the significand as well, if not canonical? */
1951 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1952 (r
->signalling
? 'S' : 'Q'));
1960 /* Hexadecimal format for decimal floats is not interesting. */
1961 strcpy (str
, "N/A");
1966 digits
= SIGNIFICAND_BITS
/ 4;
1968 /* Bound the number of digits printed by the size of the output buffer. */
1970 sprintf (exp_buf
, "p%+d", exp
);
1971 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1972 gcc_assert (max_digits
<= buf_size
);
1973 if (digits
> max_digits
)
1974 digits
= max_digits
;
1985 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1986 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1988 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1994 if (crop_trailing_zeros
)
1995 while (p
> first
+ 1 && p
[-1] == '0')
1998 sprintf (p
, "p%+d", exp
);
2001 /* Initialize R from a decimal or hexadecimal string. The string is
2002 assumed to have been syntax checked already. Return -1 if the
2003 value underflows, +1 if overflows, and 0 otherwise. */
2006 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
2018 else if (*str
== '+')
2021 if (startswith (str
, "QNaN"))
2023 get_canonical_qnan (r
, sign
);
2026 else if (startswith (str
, "SNaN"))
2028 get_canonical_snan (r
, sign
);
2031 else if (startswith (str
, "Inf"))
2037 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
2039 /* Hexadecimal floating point. */
2040 int pos
= SIGNIFICAND_BITS
- 4, d
;
2048 d
= hex_value (*str
);
2053 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
2054 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
2058 /* Ensure correct rounding by setting last bit if there is
2059 a subsequent nonzero digit. */
2067 if (pos
== SIGNIFICAND_BITS
- 4)
2074 d
= hex_value (*str
);
2079 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
2080 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
2084 /* Ensure correct rounding by setting last bit if there is
2085 a subsequent nonzero digit. */
2091 /* If the mantissa is zero, ignore the exponent. */
2092 if (!cmp_significand_0 (r
))
2095 if (*str
== 'p' || *str
== 'P')
2097 bool exp_neg
= false;
2105 else if (*str
== '+')
2109 while (ISDIGIT (*str
))
2115 /* Overflowed the exponent. */
2130 SET_REAL_EXP (r
, exp
);
2136 /* Decimal floating point. */
2137 const char *cstr
= str
;
2140 while (*cstr
== '0')
2145 while (*cstr
== '0')
2149 /* If the mantissa is zero, ignore the exponent. */
2150 if (!ISDIGIT (*cstr
))
2153 /* Nonzero value, possibly overflowing or underflowing. */
2154 auto_mpfr
m (SIGNIFICAND_BITS
);
2155 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, MPFR_RNDZ
);
2156 /* The result should never be a NaN, and because the rounding is
2157 toward zero should never be an infinity. */
2158 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2159 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2161 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2165 real_from_mpfr (r
, m
, NULL_TREE
, MPFR_RNDZ
);
2166 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2167 because the hex digits used in real_from_mpfr did not
2168 start with a digit 8 to f, but the exponent bounds above
2169 should have avoided underflow or overflow. */
2170 gcc_assert (r
->cl
== rvc_normal
);
2171 /* Set a sticky bit if mpfr_strtofr was inexact. */
2172 r
->sig
[0] |= inexact
;
2192 /* Legacy. Similar, but return the result directly. */
2195 real_from_string2 (const char *s
, format_helper fmt
)
2199 real_from_string (&r
, s
);
2201 real_convert (&r
, fmt
, &r
);
2206 /* Initialize R from string S and desired format FMT. */
2209 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, format_helper fmt
)
2211 if (fmt
.decimal_p ())
2212 decimal_real_from_string (r
, s
);
2214 real_from_string (r
, s
);
2217 real_convert (r
, fmt
, r
);
2220 /* Initialize R from the wide_int VAL_IN. Round it to format FMT if
2224 real_from_integer (REAL_VALUE_TYPE
*r
, format_helper fmt
,
2225 const wide_int_ref
&val_in
, signop sgn
)
2231 unsigned int len
= val_in
.get_precision ();
2233 int maxbitlen
= MAX_BITSIZE_MODE_ANY_INT
+ HOST_BITS_PER_WIDE_INT
;
2234 const unsigned int realmax
= (SIGNIFICAND_BITS
/ HOST_BITS_PER_WIDE_INT
2235 * HOST_BITS_PER_WIDE_INT
);
2237 memset (r
, 0, sizeof (*r
));
2239 r
->sign
= wi::neg_p (val_in
, sgn
);
2241 /* We have to ensure we can negate the largest negative number. */
2242 wide_int val
= wide_int::from (val_in
, maxbitlen
, sgn
);
2247 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2248 won't work with precisions that are not a multiple of
2249 HOST_BITS_PER_WIDE_INT. */
2250 len
+= HOST_BITS_PER_WIDE_INT
- 1;
2252 /* Ensure we can represent the largest negative number. */
2255 len
= len
/HOST_BITS_PER_WIDE_INT
* HOST_BITS_PER_WIDE_INT
;
2257 /* Cap the size to the size allowed by real.h. */
2260 HOST_WIDE_INT cnt_l_z
;
2261 cnt_l_z
= wi::clz (val
);
2263 if (maxbitlen
- cnt_l_z
> realmax
)
2265 e
= maxbitlen
- cnt_l_z
- realmax
;
2267 /* This value is too large, we must shift it right to
2268 preserve all the bits we can, and then bump the
2269 exponent up by that amount. */
2270 val
= wi::lrshift (val
, e
);
2275 /* Clear out top bits so elt will work with precisions that aren't
2276 a multiple of HOST_BITS_PER_WIDE_INT. */
2277 val
= wide_int::from (val
, len
, sgn
);
2278 len
= len
/ HOST_BITS_PER_WIDE_INT
;
2280 SET_REAL_EXP (r
, len
* HOST_BITS_PER_WIDE_INT
+ e
);
2283 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2284 for (i
= len
- 1; i
>= 0; i
--)
2286 r
->sig
[j
--] = val
.elt (i
);
2292 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2293 for (i
= len
- 1; i
>= 0; i
--)
2295 HOST_WIDE_INT e
= val
.elt (i
);
2296 r
->sig
[j
--] = e
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2308 if (fmt
.decimal_p ())
2309 decimal_from_integer (r
);
2311 real_convert (r
, fmt
, r
);
2314 /* Render R, an integral value, as a floating point constant with no
2315 specified exponent. */
2318 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2321 int dec_exp
, digit
, digits
;
2322 REAL_VALUE_TYPE r
, pten
;
2328 if (r
.cl
== rvc_zero
)
2337 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2338 digits
= dec_exp
+ 1;
2339 gcc_assert ((digits
+ 2) < (int)buf_size
);
2341 pten
= *real_digit (1);
2342 times_pten (&pten
, dec_exp
);
2348 digit
= rtd_divmod (&r
, &pten
);
2349 gcc_assert (digit
>= 0 && digit
<= 9);
2351 while (--digits
> 0)
2354 digit
= rtd_divmod (&r
, &pten
);
2361 /* Convert a real with an integral value to decimal float. */
2364 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2368 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2369 decimal_real_from_string (r
, str
);
2372 /* Returns 10**2**N. */
2374 static const REAL_VALUE_TYPE
*
2377 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2379 gcc_assert (n
>= 0);
2380 gcc_assert (n
< EXP_BITS
);
2382 if (tens
[n
].cl
== rvc_zero
)
2384 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2386 HOST_WIDE_INT t
= 10;
2389 for (i
= 0; i
< n
; ++i
)
2392 real_from_integer (&tens
[n
], VOIDmode
, t
, UNSIGNED
);
2396 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2397 do_multiply (&tens
[n
], t
, t
);
2404 /* Returns 10**(-2**N). */
2406 static const REAL_VALUE_TYPE
*
2407 ten_to_mptwo (int n
)
2409 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2411 gcc_assert (n
>= 0);
2412 gcc_assert (n
< EXP_BITS
);
2414 if (tens
[n
].cl
== rvc_zero
)
2415 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2422 static const REAL_VALUE_TYPE
*
2425 static REAL_VALUE_TYPE num
[10];
2427 gcc_assert (n
>= 0);
2428 gcc_assert (n
<= 9);
2430 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2431 real_from_integer (&num
[n
], VOIDmode
, n
, UNSIGNED
);
2436 /* Multiply R by 10**EXP. */
2439 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2441 REAL_VALUE_TYPE pten
, *rr
;
2442 bool negative
= (exp
< 0);
2448 pten
= *real_digit (1);
2454 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2456 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2459 do_divide (r
, r
, &pten
);
2462 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2464 const REAL_VALUE_TYPE
*
2467 static REAL_VALUE_TYPE value
;
2469 /* Initialize mathematical constants for constant folding builtins.
2470 These constants need to be given to at least 160 bits precision. */
2471 if (value
.cl
== rvc_zero
)
2473 auto_mpfr
m (SIGNIFICAND_BITS
);
2474 mpfr_set_ui (m
, 1, MPFR_RNDN
);
2475 mpfr_exp (m
, m
, MPFR_RNDN
);
2476 real_from_mpfr (&value
, m
, NULL_TREE
, MPFR_RNDN
);
2482 /* Returns the special REAL_VALUE_TYPE corresponding to 'pi'. */
2484 const REAL_VALUE_TYPE
*
2485 dconst_pi_ptr (void)
2487 static REAL_VALUE_TYPE value
;
2489 /* Initialize mathematical constants for constant folding builtins.
2490 These constants need to be given to at least 160 bits precision. */
2491 if (value
.cl
== rvc_zero
)
2493 auto_mpfr
m (SIGNIFICAND_BITS
);
2494 mpfr_set_si (m
, -1, MPFR_RNDN
);
2495 mpfr_acos (m
, m
, MPFR_RNDN
);
2496 real_from_mpfr (&value
, m
, NULL_TREE
, MPFR_RNDN
);
2502 /* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2504 #define CACHED_FRACTION(NAME, N) \
2505 const REAL_VALUE_TYPE * \
2508 static REAL_VALUE_TYPE value; \
2510 /* Initialize mathematical constants for constant folding builtins. \
2511 These constants need to be given to at least 160 bits \
2513 if (value.cl == rvc_zero) \
2514 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2518 CACHED_FRACTION (dconst_third_ptr
, 3)
2519 CACHED_FRACTION (dconst_quarter_ptr
, 4)
2520 CACHED_FRACTION (dconst_sixth_ptr
, 6)
2521 CACHED_FRACTION (dconst_ninth_ptr
, 9)
2523 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2525 const REAL_VALUE_TYPE
*
2526 dconst_sqrt2_ptr (void)
2528 static REAL_VALUE_TYPE value
;
2530 /* Initialize mathematical constants for constant folding builtins.
2531 These constants need to be given to at least 160 bits precision. */
2532 if (value
.cl
== rvc_zero
)
2534 auto_mpfr
m (SIGNIFICAND_BITS
);
2535 mpfr_sqrt_ui (m
, 2, MPFR_RNDN
);
2536 real_from_mpfr (&value
, m
, NULL_TREE
, MPFR_RNDN
);
2541 /* Fills R with Inf with SIGN. */
2544 real_inf (REAL_VALUE_TYPE
*r
, bool sign
)
2549 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2550 we force a QNaN, else we force an SNaN. The string, if not empty,
2551 is parsed as a number and placed in the significand. Return true
2552 if the string was successfully parsed. */
2555 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2561 get_canonical_qnan (r
, 0);
2563 get_canonical_snan (r
, 0);
2569 memset (r
, 0, sizeof (*r
));
2572 /* Parse akin to strtol into the significand of R. */
2574 while (ISSPACE (*str
))
2578 else if (*str
== '+')
2583 if (*str
== 'x' || *str
== 'X')
2592 while ((d
= hex_value (*str
)) < base
)
2599 lshift_significand (r
, r
, 3);
2602 lshift_significand (r
, r
, 4);
2605 lshift_significand_1 (&u
, r
);
2606 lshift_significand (r
, r
, 3);
2607 add_significands (r
, r
, &u
);
2615 add_significands (r
, r
, &u
);
2620 /* Must have consumed the entire string for success. */
2624 /* Shift the significand into place such that the bits
2625 are in the most significant bits for the format. */
2626 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2628 /* Our MSB is always unset for NaNs. */
2629 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2631 /* Force quiet or signaling NaN. */
2632 r
->signalling
= !quiet
;
2638 /* Fills R with the largest finite value representable in mode MODE.
2639 If SIGN is nonzero, R is set to the most negative finite value. */
2642 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
2644 const struct real_format
*fmt
;
2647 fmt
= REAL_MODE_FORMAT (mode
);
2649 memset (r
, 0, sizeof (*r
));
2652 decimal_real_maxval (r
, sign
, mode
);
2657 SET_REAL_EXP (r
, fmt
->emax
);
2659 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2660 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2661 clear_significand_below (r
, np2
);
2663 if (fmt
->pnan
< fmt
->p
)
2664 /* This is an IBM extended double format made up of two IEEE
2665 doubles. The value of the long double is the sum of the
2666 values of the two parts. The most significant part is
2667 required to be the value of the long double rounded to the
2668 nearest double. Rounding means we need a slightly smaller
2669 value for LDBL_MAX. */
2670 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2674 /* Fills R with 2**N. */
2677 real_2expN (REAL_VALUE_TYPE
*r
, int n
, format_helper fmt
)
2679 memset (r
, 0, sizeof (*r
));
2684 else if (n
< -MAX_EXP
)
2689 SET_REAL_EXP (r
, n
);
2690 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2692 if (fmt
.decimal_p ())
2693 decimal_real_convert (r
, fmt
, r
);
2698 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2702 bool round_up
= false;
2708 decimal_round_for_format (fmt
, r
);
2711 /* FIXME. We can come here via fp_easy_constant
2712 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2713 investigated whether this convert needs to be here, or
2714 something else is missing. */
2715 decimal_real_convert (r
, REAL_MODE_FORMAT (DFmode
), r
);
2719 emin2m1
= fmt
->emin
- 1;
2722 np2
= SIGNIFICAND_BITS
- p2
;
2726 get_zero (r
, r
->sign
);
2729 if (!fmt
->has_signed_zero
)
2734 get_inf (r
, r
->sign
);
2739 clear_significand_below (r
, np2
);
2749 /* Check the range of the exponent. If we're out of range,
2750 either underflow or overflow. */
2751 if (REAL_EXP (r
) > emax2
)
2753 else if (REAL_EXP (r
) <= emin2m1
)
2757 if (!fmt
->has_denorm
)
2759 /* Don't underflow completely until we've had a chance to round. */
2760 if (REAL_EXP (r
) < emin2m1
)
2765 diff
= emin2m1
- REAL_EXP (r
) + 1;
2769 /* De-normalize the significand. */
2770 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2771 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2775 if (!fmt
->round_towards_zero
)
2777 /* There are P2 true significand bits, followed by one guard bit,
2778 followed by one sticky bit, followed by stuff. Fold nonzero
2779 stuff into the sticky bit. */
2780 unsigned long sticky
;
2784 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2785 sticky
|= r
->sig
[i
];
2787 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2789 guard
= test_significand_bit (r
, np2
- 1);
2790 lsb
= test_significand_bit (r
, np2
);
2792 /* Round to even. */
2793 round_up
= guard
&& (sticky
|| lsb
);
2800 set_significand_bit (&u
, np2
);
2802 if (add_significands (r
, r
, &u
))
2804 /* Overflow. Means the significand had been all ones, and
2805 is now all zeros. Need to increase the exponent, and
2806 possibly re-normalize it. */
2807 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2808 if (REAL_EXP (r
) > emax2
)
2810 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2814 /* Catch underflow that we deferred until after rounding. */
2815 if (REAL_EXP (r
) <= emin2m1
)
2818 /* Clear out trailing garbage. */
2819 clear_significand_below (r
, np2
);
2822 /* Extend or truncate to a new format. */
2825 real_convert (REAL_VALUE_TYPE
*r
, format_helper fmt
,
2826 const REAL_VALUE_TYPE
*a
)
2830 if (a
->decimal
|| fmt
->b
== 10)
2831 decimal_real_convert (r
, fmt
, a
);
2833 round_for_format (fmt
, r
);
2835 /* Make resulting NaN value to be qNaN. The caller has the
2836 responsibility to avoid the operation if flag_signaling_nans
2838 if (r
->cl
== rvc_nan
)
2841 /* round_for_format de-normalizes denormals. Undo just that part. */
2842 if (r
->cl
== rvc_normal
)
2846 /* Legacy. Likewise, except return the struct directly. */
2849 real_value_truncate (format_helper fmt
, REAL_VALUE_TYPE a
)
2852 real_convert (&r
, fmt
, &a
);
2856 /* Return true if truncating to FMT is exact. */
2859 exact_real_truncate (format_helper fmt
, const REAL_VALUE_TYPE
*a
)
2864 /* Don't allow conversion to denormals. */
2865 emin2m1
= fmt
->emin
- 1;
2866 if (REAL_EXP (a
) <= emin2m1
)
2869 /* After conversion to the new format, the value must be identical. */
2870 real_convert (&t
, fmt
, a
);
2871 return real_identical (&t
, a
);
2874 /* Write R to the given target format. Place the words of the result
2875 in target word order in BUF. There are always 32 bits in each
2876 long, no matter the size of the host long.
2878 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2881 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2888 round_for_format (fmt
, &r
);
2892 (*fmt
->encode
) (fmt
, buf
, &r
);
2897 /* Read R from the given target format. Read the words of the result
2898 in target word order in BUF. There are always 32 bits in each
2899 long, no matter the size of the host long. */
2902 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, format_helper fmt
)
2904 (*fmt
->decode
) (fmt
, r
, buf
);
2907 /* Return the number of bits of the largest binary value that the
2908 significand of FMT will hold. */
2909 /* ??? Legacy. Should get access to real_format directly. */
2912 significand_size (format_helper fmt
)
2919 /* Return the size in bits of the largest binary value that can be
2920 held by the decimal coefficient for this format. This is one more
2921 than the number of bits required to hold the largest coefficient
2923 double log2_10
= 3.3219281;
2924 return fmt
->p
* log2_10
;
2929 /* Return a hash value for the given real value. */
2930 /* ??? The "unsigned int" return value is intended to be hashval_t,
2931 but I didn't want to pull hashtab.h into real.h. */
2934 real_hash (const REAL_VALUE_TYPE
*r
)
2939 h
= r
->cl
| (r
->sign
<< 2);
2947 h
|= (unsigned int)REAL_EXP (r
) << 3;
2952 h
^= (unsigned int)-1;
2961 if (sizeof (unsigned long) > sizeof (unsigned int))
2962 for (i
= 0; i
< SIGSZ
; ++i
)
2964 unsigned long s
= r
->sig
[i
];
2965 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2968 for (i
= 0; i
< SIGSZ
; ++i
)
2974 /* IEEE single-precision format. */
2976 static void encode_ieee_single (const struct real_format
*fmt
,
2977 long *, const REAL_VALUE_TYPE
*);
2978 static void decode_ieee_single (const struct real_format
*,
2979 REAL_VALUE_TYPE
*, const long *);
2982 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2983 const REAL_VALUE_TYPE
*r
)
2985 unsigned long image
, sig
, exp
;
2986 unsigned long sign
= r
->sign
;
2989 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
3000 image
|= 0x7fffffff;
3007 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
3008 if (r
->signalling
== fmt
->qnan_msb_set
)
3019 image
|= 0x7fffffff;
3023 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3024 whereas the intermediate representation is 0.F x 2**exp.
3025 Which means we're off by one. */
3026 if (real_isdenormal (r
))
3029 exp
= REAL_EXP (r
) + 127 - 1;
3042 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3045 unsigned long image
= buf
[0] & 0xffffffff;
3046 bool sign
= (image
>> 31) & 1;
3047 int exp
= (image
>> 23) & 0xff;
3049 memset (r
, 0, sizeof (*r
));
3050 image
<<= HOST_BITS_PER_LONG
- 24;
3055 if (image
&& fmt
->has_denorm
)
3059 SET_REAL_EXP (r
, -126);
3060 r
->sig
[SIGSZ
-1] = image
<< 1;
3063 else if (fmt
->has_signed_zero
)
3066 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
3072 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
3073 ^ fmt
->qnan_msb_set
);
3074 r
->sig
[SIGSZ
-1] = image
;
3086 SET_REAL_EXP (r
, exp
- 127 + 1);
3087 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
3091 const struct real_format ieee_single_format
=
3114 const struct real_format mips_single_format
=
3137 const struct real_format motorola_single_format
=
3160 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3161 single precision with the following differences:
3162 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3164 - NaNs are not supported.
3165 - The range of non-zero numbers in binary is
3166 (001)[1.]000...000 to (255)[1.]111...111.
3167 - Denormals can be represented, but are treated as +0.0 when
3168 used as an operand and are never generated as a result.
3169 - -0.0 can be represented, but a zero result is always +0.0.
3170 - the only supported rounding mode is trunction (towards zero). */
3171 const struct real_format spu_single_format
=
3194 /* IEEE double-precision format. */
3196 static void encode_ieee_double (const struct real_format
*fmt
,
3197 long *, const REAL_VALUE_TYPE
*);
3198 static void decode_ieee_double (const struct real_format
*,
3199 REAL_VALUE_TYPE
*, const long *);
3202 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3203 const REAL_VALUE_TYPE
*r
)
3205 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3206 unsigned long sign
= r
->sign
;
3208 image_hi
= sign
<< 31;
3211 if (HOST_BITS_PER_LONG
== 64)
3213 sig_hi
= r
->sig
[SIGSZ
-1];
3214 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3215 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3219 sig_hi
= r
->sig
[SIGSZ
-1];
3220 sig_lo
= r
->sig
[SIGSZ
-2];
3221 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3222 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3232 image_hi
|= 2047 << 20;
3235 image_hi
|= 0x7fffffff;
3236 image_lo
= 0xffffffff;
3245 if (fmt
->canonical_nan_lsbs_set
)
3247 sig_hi
= (1 << 19) - 1;
3248 sig_lo
= 0xffffffff;
3256 if (r
->signalling
== fmt
->qnan_msb_set
)
3257 sig_hi
&= ~(1 << 19);
3260 if (sig_hi
== 0 && sig_lo
== 0)
3263 image_hi
|= 2047 << 20;
3269 image_hi
|= 0x7fffffff;
3270 image_lo
= 0xffffffff;
3275 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3276 whereas the intermediate representation is 0.F x 2**exp.
3277 Which means we're off by one. */
3278 if (real_isdenormal (r
))
3281 exp
= REAL_EXP (r
) + 1023 - 1;
3282 image_hi
|= exp
<< 20;
3291 if (FLOAT_WORDS_BIG_ENDIAN
)
3292 buf
[0] = image_hi
, buf
[1] = image_lo
;
3294 buf
[0] = image_lo
, buf
[1] = image_hi
;
3298 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3301 unsigned long image_hi
, image_lo
;
3305 if (FLOAT_WORDS_BIG_ENDIAN
)
3306 image_hi
= buf
[0], image_lo
= buf
[1];
3308 image_lo
= buf
[0], image_hi
= buf
[1];
3309 image_lo
&= 0xffffffff;
3310 image_hi
&= 0xffffffff;
3312 sign
= (image_hi
>> 31) & 1;
3313 exp
= (image_hi
>> 20) & 0x7ff;
3315 memset (r
, 0, sizeof (*r
));
3317 image_hi
<<= 32 - 21;
3318 image_hi
|= image_lo
>> 21;
3319 image_hi
&= 0x7fffffff;
3320 image_lo
<<= 32 - 21;
3324 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3328 SET_REAL_EXP (r
, -1022);
3329 if (HOST_BITS_PER_LONG
== 32)
3331 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3333 r
->sig
[SIGSZ
-1] = image_hi
;
3334 r
->sig
[SIGSZ
-2] = image_lo
;
3338 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3339 r
->sig
[SIGSZ
-1] = image_hi
;
3343 else if (fmt
->has_signed_zero
)
3346 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3348 if (image_hi
|| image_lo
)
3352 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3353 if (HOST_BITS_PER_LONG
== 32)
3355 r
->sig
[SIGSZ
-1] = image_hi
;
3356 r
->sig
[SIGSZ
-2] = image_lo
;
3359 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3371 SET_REAL_EXP (r
, exp
- 1023 + 1);
3372 if (HOST_BITS_PER_LONG
== 32)
3374 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3375 r
->sig
[SIGSZ
-2] = image_lo
;
3378 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3382 const struct real_format ieee_double_format
=
3405 const struct real_format mips_double_format
=
3428 const struct real_format motorola_double_format
=
3451 /* IEEE extended real format. This comes in three flavors: Intel's as
3452 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3453 12- and 16-byte images may be big- or little endian; Motorola's is
3454 always big endian. */
3456 /* Helper subroutine which converts from the internal format to the
3457 12-byte little-endian Intel format. Functions below adjust this
3458 for the other possible formats. */
3460 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3461 const REAL_VALUE_TYPE
*r
)
3463 unsigned long image_hi
, sig_hi
, sig_lo
;
3465 image_hi
= r
->sign
<< 15;
3466 sig_hi
= sig_lo
= 0;
3478 /* Intel requires the explicit integer bit to be set, otherwise
3479 it considers the value a "pseudo-infinity". Motorola docs
3480 say it doesn't care. */
3481 sig_hi
= 0x80000000;
3486 sig_lo
= sig_hi
= 0xffffffff;
3496 if (fmt
->canonical_nan_lsbs_set
)
3498 sig_hi
= (1 << 30) - 1;
3499 sig_lo
= 0xffffffff;
3502 else if (HOST_BITS_PER_LONG
== 32)
3504 sig_hi
= r
->sig
[SIGSZ
-1];
3505 sig_lo
= r
->sig
[SIGSZ
-2];
3509 sig_lo
= r
->sig
[SIGSZ
-1];
3510 sig_hi
= sig_lo
>> 31 >> 1;
3511 sig_lo
&= 0xffffffff;
3513 if (r
->signalling
== fmt
->qnan_msb_set
)
3514 sig_hi
&= ~(1 << 30);
3517 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3520 /* Intel requires the explicit integer bit to be set, otherwise
3521 it considers the value a "pseudo-nan". Motorola docs say it
3523 sig_hi
|= 0x80000000;
3528 sig_lo
= sig_hi
= 0xffffffff;
3534 int exp
= REAL_EXP (r
);
3536 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3537 whereas the intermediate representation is 0.F x 2**exp.
3538 Which means we're off by one.
3540 Except for Motorola, which consider exp=0 and explicit
3541 integer bit set to continue to be normalized. In theory
3542 this discrepancy has been taken care of by the difference
3543 in fmt->emin in round_for_format. */
3545 if (real_isdenormal (r
))
3550 gcc_assert (exp
>= 0);
3554 if (HOST_BITS_PER_LONG
== 32)
3556 sig_hi
= r
->sig
[SIGSZ
-1];
3557 sig_lo
= r
->sig
[SIGSZ
-2];
3561 sig_lo
= r
->sig
[SIGSZ
-1];
3562 sig_hi
= sig_lo
>> 31 >> 1;
3563 sig_lo
&= 0xffffffff;
3572 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3575 /* Convert from the internal format to the 12-byte Motorola format
3576 for an IEEE extended real. */
3578 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3579 const REAL_VALUE_TYPE
*r
)
3582 encode_ieee_extended (fmt
, intermed
, r
);
3584 if (r
->cl
== rvc_inf
)
3585 /* For infinity clear the explicit integer bit again, so that the
3586 format matches the canonical infinity generated by the FPU. */
3589 /* Motorola chips are assumed always to be big-endian. Also, the
3590 padding in a Motorola extended real goes between the exponent and
3591 the mantissa. At this point the mantissa is entirely within
3592 elements 0 and 1 of intermed, and the exponent entirely within
3593 element 2, so all we have to do is swap the order around, and
3594 shift element 2 left 16 bits. */
3595 buf
[0] = intermed
[2] << 16;
3596 buf
[1] = intermed
[1];
3597 buf
[2] = intermed
[0];
3600 /* Convert from the internal format to the 12-byte Intel format for
3601 an IEEE extended real. */
3603 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3604 const REAL_VALUE_TYPE
*r
)
3606 if (FLOAT_WORDS_BIG_ENDIAN
)
3608 /* All the padding in an Intel-format extended real goes at the high
3609 end, which in this case is after the mantissa, not the exponent.
3610 Therefore we must shift everything down 16 bits. */
3612 encode_ieee_extended (fmt
, intermed
, r
);
3613 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3614 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3615 buf
[2] = (intermed
[0] << 16);
3618 /* encode_ieee_extended produces what we want directly. */
3619 encode_ieee_extended (fmt
, buf
, r
);
3622 /* Convert from the internal format to the 16-byte Intel format for
3623 an IEEE extended real. */
3625 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3626 const REAL_VALUE_TYPE
*r
)
3628 /* All the padding in an Intel-format extended real goes at the high end. */
3629 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3633 /* As above, we have a helper function which converts from 12-byte
3634 little-endian Intel format to internal format. Functions below
3635 adjust for the other possible formats. */
3637 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3640 unsigned long image_hi
, sig_hi
, sig_lo
;
3644 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3645 sig_lo
&= 0xffffffff;
3646 sig_hi
&= 0xffffffff;
3647 image_hi
&= 0xffffffff;
3649 sign
= (image_hi
>> 15) & 1;
3650 exp
= image_hi
& 0x7fff;
3652 memset (r
, 0, sizeof (*r
));
3656 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3661 /* When the IEEE format contains a hidden bit, we know that
3662 it's zero at this point, and so shift up the significand
3663 and decrease the exponent to match. In this case, Motorola
3664 defines the explicit integer bit to be valid, so we don't
3665 know whether the msb is set or not. */
3666 SET_REAL_EXP (r
, fmt
->emin
);
3667 if (HOST_BITS_PER_LONG
== 32)
3669 r
->sig
[SIGSZ
-1] = sig_hi
;
3670 r
->sig
[SIGSZ
-2] = sig_lo
;
3673 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3677 else if (fmt
->has_signed_zero
)
3680 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3682 /* See above re "pseudo-infinities" and "pseudo-nans".
3683 Short summary is that the MSB will likely always be
3684 set, and that we don't care about it. */
3685 sig_hi
&= 0x7fffffff;
3687 if (sig_hi
|| sig_lo
)
3691 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3692 if (HOST_BITS_PER_LONG
== 32)
3694 r
->sig
[SIGSZ
-1] = sig_hi
;
3695 r
->sig
[SIGSZ
-2] = sig_lo
;
3698 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3710 SET_REAL_EXP (r
, exp
- 16383 + 1);
3711 if (HOST_BITS_PER_LONG
== 32)
3713 r
->sig
[SIGSZ
-1] = sig_hi
;
3714 r
->sig
[SIGSZ
-2] = sig_lo
;
3717 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3721 /* Convert from the internal format to the 12-byte Motorola format
3722 for an IEEE extended real. */
3724 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3729 /* Motorola chips are assumed always to be big-endian. Also, the
3730 padding in a Motorola extended real goes between the exponent and
3731 the mantissa; remove it. */
3732 intermed
[0] = buf
[2];
3733 intermed
[1] = buf
[1];
3734 intermed
[2] = (unsigned long)buf
[0] >> 16;
3736 decode_ieee_extended (fmt
, r
, intermed
);
3739 /* Convert from the internal format to the 12-byte Intel format for
3740 an IEEE extended real. */
3742 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3745 if (FLOAT_WORDS_BIG_ENDIAN
)
3747 /* All the padding in an Intel-format extended real goes at the high
3748 end, which in this case is after the mantissa, not the exponent.
3749 Therefore we must shift everything up 16 bits. */
3752 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3753 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3754 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3756 decode_ieee_extended (fmt
, r
, intermed
);
3759 /* decode_ieee_extended produces what we want directly. */
3760 decode_ieee_extended (fmt
, r
, buf
);
3763 /* Convert from the internal format to the 16-byte Intel format for
3764 an IEEE extended real. */
3766 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3769 /* All the padding in an Intel-format extended real goes at the high end. */
3770 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3773 const struct real_format ieee_extended_motorola_format
=
3775 encode_ieee_extended_motorola
,
3776 decode_ieee_extended_motorola
,
3793 "ieee_extended_motorola"
3796 const struct real_format ieee_extended_intel_96_format
=
3798 encode_ieee_extended_intel_96
,
3799 decode_ieee_extended_intel_96
,
3816 "ieee_extended_intel_96"
3819 const struct real_format ieee_extended_intel_128_format
=
3821 encode_ieee_extended_intel_128
,
3822 decode_ieee_extended_intel_128
,
3839 "ieee_extended_intel_128"
3842 /* The following caters to i386 systems that set the rounding precision
3843 to 53 bits instead of 64, e.g. FreeBSD. */
3844 const struct real_format ieee_extended_intel_96_round_53_format
=
3846 encode_ieee_extended_intel_96
,
3847 decode_ieee_extended_intel_96
,
3864 "ieee_extended_intel_96_round_53"
3867 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3868 numbers whose sum is equal to the extended precision value. The number
3869 with greater magnitude is first. This format has the same magnitude
3870 range as an IEEE double precision value, but effectively 106 bits of
3871 significand precision. Infinity and NaN are represented by their IEEE
3872 double precision value stored in the first number, the second number is
3873 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3875 static void encode_ibm_extended (const struct real_format
*fmt
,
3876 long *, const REAL_VALUE_TYPE
*);
3877 static void decode_ibm_extended (const struct real_format
*,
3878 REAL_VALUE_TYPE
*, const long *);
3881 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3882 const REAL_VALUE_TYPE
*r
)
3884 REAL_VALUE_TYPE u
, normr
, v
;
3885 const struct real_format
*base_fmt
;
3887 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3889 /* Renormalize R before doing any arithmetic on it. */
3891 if (normr
.cl
== rvc_normal
)
3894 /* u = IEEE double precision portion of significand. */
3896 round_for_format (base_fmt
, &u
);
3897 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3899 if (u
.cl
== rvc_normal
)
3901 do_add (&v
, &normr
, &u
, 1);
3902 /* Call round_for_format since we might need to denormalize. */
3903 round_for_format (base_fmt
, &v
);
3904 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3908 /* Inf, NaN, 0 are all representable as doubles, so the
3909 least-significant part can be 0.0. */
3916 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3919 REAL_VALUE_TYPE u
, v
;
3920 const struct real_format
*base_fmt
;
3922 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3923 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3925 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3927 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3928 do_add (r
, &u
, &v
, 0);
3934 const struct real_format ibm_extended_format
=
3936 encode_ibm_extended
,
3937 decode_ibm_extended
,
3957 const struct real_format mips_extended_format
=
3959 encode_ibm_extended
,
3960 decode_ibm_extended
,
3981 /* IEEE quad precision format. */
3983 static void encode_ieee_quad (const struct real_format
*fmt
,
3984 long *, const REAL_VALUE_TYPE
*);
3985 static void decode_ieee_quad (const struct real_format
*,
3986 REAL_VALUE_TYPE
*, const long *);
3989 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3990 const REAL_VALUE_TYPE
*r
)
3992 unsigned long image3
, image2
, image1
, image0
, exp
;
3993 unsigned long sign
= r
->sign
;
3996 image3
= sign
<< 31;
4001 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
4010 image3
|= 32767 << 16;
4013 image3
|= 0x7fffffff;
4014 image2
= 0xffffffff;
4015 image1
= 0xffffffff;
4016 image0
= 0xffffffff;
4023 image3
|= 32767 << 16;
4027 if (fmt
->canonical_nan_lsbs_set
)
4030 image2
= image1
= image0
= 0xffffffff;
4033 else if (HOST_BITS_PER_LONG
== 32)
4038 image3
|= u
.sig
[3] & 0xffff;
4043 image1
= image0
>> 31 >> 1;
4045 image3
|= (image2
>> 31 >> 1) & 0xffff;
4046 image0
&= 0xffffffff;
4047 image2
&= 0xffffffff;
4049 if (r
->signalling
== fmt
->qnan_msb_set
)
4053 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
4058 image3
|= 0x7fffffff;
4059 image2
= 0xffffffff;
4060 image1
= 0xffffffff;
4061 image0
= 0xffffffff;
4066 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4067 whereas the intermediate representation is 0.F x 2**exp.
4068 Which means we're off by one. */
4069 if (real_isdenormal (r
))
4072 exp
= REAL_EXP (r
) + 16383 - 1;
4073 image3
|= exp
<< 16;
4075 if (HOST_BITS_PER_LONG
== 32)
4080 image3
|= u
.sig
[3] & 0xffff;
4085 image1
= image0
>> 31 >> 1;
4087 image3
|= (image2
>> 31 >> 1) & 0xffff;
4088 image0
&= 0xffffffff;
4089 image2
&= 0xffffffff;
4097 if (FLOAT_WORDS_BIG_ENDIAN
)
4114 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4117 unsigned long image3
, image2
, image1
, image0
;
4121 if (FLOAT_WORDS_BIG_ENDIAN
)
4135 image0
&= 0xffffffff;
4136 image1
&= 0xffffffff;
4137 image2
&= 0xffffffff;
4139 sign
= (image3
>> 31) & 1;
4140 exp
= (image3
>> 16) & 0x7fff;
4143 memset (r
, 0, sizeof (*r
));
4147 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4152 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4153 if (HOST_BITS_PER_LONG
== 32)
4162 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4163 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4168 else if (fmt
->has_signed_zero
)
4171 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4173 if (image3
| image2
| image1
| image0
)
4177 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4179 if (HOST_BITS_PER_LONG
== 32)
4188 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4189 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4191 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4203 SET_REAL_EXP (r
, exp
- 16383 + 1);
4205 if (HOST_BITS_PER_LONG
== 32)
4214 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4215 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4217 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4218 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4222 const struct real_format ieee_quad_format
=
4245 const struct real_format mips_quad_format
=
4268 /* Descriptions of VAX floating point formats can be found beginning at
4270 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4272 The thing to remember is that they're almost IEEE, except for word
4273 order, exponent bias, and the lack of infinities, nans, and denormals.
4275 We don't implement the H_floating format here, simply because neither
4276 the VAX or Alpha ports use it. */
4278 static void encode_vax_f (const struct real_format
*fmt
,
4279 long *, const REAL_VALUE_TYPE
*);
4280 static void decode_vax_f (const struct real_format
*,
4281 REAL_VALUE_TYPE
*, const long *);
4282 static void encode_vax_d (const struct real_format
*fmt
,
4283 long *, const REAL_VALUE_TYPE
*);
4284 static void decode_vax_d (const struct real_format
*,
4285 REAL_VALUE_TYPE
*, const long *);
4286 static void encode_vax_g (const struct real_format
*fmt
,
4287 long *, const REAL_VALUE_TYPE
*);
4288 static void decode_vax_g (const struct real_format
*,
4289 REAL_VALUE_TYPE
*, const long *);
4292 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4293 const REAL_VALUE_TYPE
*r
)
4295 unsigned long sign
, exp
, sig
, image
;
4297 sign
= r
->sign
<< 15;
4307 image
= 0xffff7fff | sign
;
4311 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4312 exp
= REAL_EXP (r
) + 128;
4314 image
= (sig
<< 16) & 0xffff0000;
4328 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4329 REAL_VALUE_TYPE
*r
, const long *buf
)
4331 unsigned long image
= buf
[0] & 0xffffffff;
4332 int exp
= (image
>> 7) & 0xff;
4334 memset (r
, 0, sizeof (*r
));
4339 r
->sign
= (image
>> 15) & 1;
4340 SET_REAL_EXP (r
, exp
- 128);
4342 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4343 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4348 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4349 const REAL_VALUE_TYPE
*r
)
4351 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4356 image0
= image1
= 0;
4361 image0
= 0xffff7fff | sign
;
4362 image1
= 0xffffffff;
4366 /* Extract the significand into straight hi:lo. */
4367 if (HOST_BITS_PER_LONG
== 64)
4369 image0
= r
->sig
[SIGSZ
-1];
4370 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4371 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4375 image0
= r
->sig
[SIGSZ
-1];
4376 image1
= r
->sig
[SIGSZ
-2];
4377 image1
= (image0
<< 24) | (image1
>> 8);
4378 image0
= (image0
>> 8) & 0xffffff;
4381 /* Rearrange the half-words of the significand to match the
4383 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4384 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4386 /* Add the sign and exponent. */
4388 image0
|= (REAL_EXP (r
) + 128) << 7;
4395 if (FLOAT_WORDS_BIG_ENDIAN
)
4396 buf
[0] = image1
, buf
[1] = image0
;
4398 buf
[0] = image0
, buf
[1] = image1
;
4402 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4403 REAL_VALUE_TYPE
*r
, const long *buf
)
4405 unsigned long image0
, image1
;
4408 if (FLOAT_WORDS_BIG_ENDIAN
)
4409 image1
= buf
[0], image0
= buf
[1];
4411 image0
= buf
[0], image1
= buf
[1];
4412 image0
&= 0xffffffff;
4413 image1
&= 0xffffffff;
4415 exp
= (image0
>> 7) & 0xff;
4417 memset (r
, 0, sizeof (*r
));
4422 r
->sign
= (image0
>> 15) & 1;
4423 SET_REAL_EXP (r
, exp
- 128);
4425 /* Rearrange the half-words of the external format into
4426 proper ascending order. */
4427 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4428 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4430 if (HOST_BITS_PER_LONG
== 64)
4432 image0
= (image0
<< 31 << 1) | image1
;
4435 r
->sig
[SIGSZ
-1] = image0
;
4439 r
->sig
[SIGSZ
-1] = image0
;
4440 r
->sig
[SIGSZ
-2] = image1
;
4441 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4442 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4448 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4449 const REAL_VALUE_TYPE
*r
)
4451 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4456 image0
= image1
= 0;
4461 image0
= 0xffff7fff | sign
;
4462 image1
= 0xffffffff;
4466 /* Extract the significand into straight hi:lo. */
4467 if (HOST_BITS_PER_LONG
== 64)
4469 image0
= r
->sig
[SIGSZ
-1];
4470 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4471 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4475 image0
= r
->sig
[SIGSZ
-1];
4476 image1
= r
->sig
[SIGSZ
-2];
4477 image1
= (image0
<< 21) | (image1
>> 11);
4478 image0
= (image0
>> 11) & 0xfffff;
4481 /* Rearrange the half-words of the significand to match the
4483 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4484 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4486 /* Add the sign and exponent. */
4488 image0
|= (REAL_EXP (r
) + 1024) << 4;
4495 if (FLOAT_WORDS_BIG_ENDIAN
)
4496 buf
[0] = image1
, buf
[1] = image0
;
4498 buf
[0] = image0
, buf
[1] = image1
;
4502 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4503 REAL_VALUE_TYPE
*r
, const long *buf
)
4505 unsigned long image0
, image1
;
4508 if (FLOAT_WORDS_BIG_ENDIAN
)
4509 image1
= buf
[0], image0
= buf
[1];
4511 image0
= buf
[0], image1
= buf
[1];
4512 image0
&= 0xffffffff;
4513 image1
&= 0xffffffff;
4515 exp
= (image0
>> 4) & 0x7ff;
4517 memset (r
, 0, sizeof (*r
));
4522 r
->sign
= (image0
>> 15) & 1;
4523 SET_REAL_EXP (r
, exp
- 1024);
4525 /* Rearrange the half-words of the external format into
4526 proper ascending order. */
4527 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4528 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4530 if (HOST_BITS_PER_LONG
== 64)
4532 image0
= (image0
<< 31 << 1) | image1
;
4535 r
->sig
[SIGSZ
-1] = image0
;
4539 r
->sig
[SIGSZ
-1] = image0
;
4540 r
->sig
[SIGSZ
-2] = image1
;
4541 lshift_significand (r
, r
, 64 - 53);
4542 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4547 const struct real_format vax_f_format
=
4570 const struct real_format vax_d_format
=
4593 const struct real_format vax_g_format
=
4616 /* Encode real R into a single precision DFP value in BUF. */
4618 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4619 long *buf ATTRIBUTE_UNUSED
,
4620 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4622 encode_decimal32 (fmt
, buf
, r
);
4625 /* Decode a single precision DFP value in BUF into a real R. */
4627 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4628 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4629 const long *buf ATTRIBUTE_UNUSED
)
4631 decode_decimal32 (fmt
, r
, buf
);
4634 /* Encode real R into a double precision DFP value in BUF. */
4636 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4637 long *buf ATTRIBUTE_UNUSED
,
4638 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4640 encode_decimal64 (fmt
, buf
, r
);
4643 /* Decode a double precision DFP value in BUF into a real R. */
4645 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4646 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4647 const long *buf ATTRIBUTE_UNUSED
)
4649 decode_decimal64 (fmt
, r
, buf
);
4652 /* Encode real R into a quad precision DFP value in BUF. */
4654 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4655 long *buf ATTRIBUTE_UNUSED
,
4656 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4658 encode_decimal128 (fmt
, buf
, r
);
4661 /* Decode a quad precision DFP value in BUF into a real R. */
4663 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4664 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4665 const long *buf ATTRIBUTE_UNUSED
)
4667 decode_decimal128 (fmt
, r
, buf
);
4670 /* Single precision decimal floating point (IEEE 754). */
4671 const struct real_format decimal_single_format
=
4673 encode_decimal_single
,
4674 decode_decimal_single
,
4694 /* Double precision decimal floating point (IEEE 754). */
4695 const struct real_format decimal_double_format
=
4697 encode_decimal_double
,
4698 decode_decimal_double
,
4718 /* Quad precision decimal floating point (IEEE 754). */
4719 const struct real_format decimal_quad_format
=
4721 encode_decimal_quad
,
4722 decode_decimal_quad
,
4742 /* Encode half-precision floats. This routine is used both for the IEEE
4743 ARM alternative encodings. */
4745 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4746 const REAL_VALUE_TYPE
*r
)
4748 unsigned long image
, sig
, exp
;
4749 unsigned long sign
= r
->sign
;
4752 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4770 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4771 if (r
->signalling
== fmt
->qnan_msb_set
)
4786 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4787 whereas the intermediate representation is 0.F x 2**exp.
4788 Which means we're off by one. */
4789 if (real_isdenormal (r
))
4792 exp
= REAL_EXP (r
) + 15 - 1;
4804 /* Decode half-precision floats. This routine is used both for the IEEE
4805 ARM alternative encodings. */
4807 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4810 unsigned long image
= buf
[0] & 0xffff;
4811 bool sign
= (image
>> 15) & 1;
4812 int exp
= (image
>> 10) & 0x1f;
4814 memset (r
, 0, sizeof (*r
));
4815 image
<<= HOST_BITS_PER_LONG
- 11;
4820 if (image
&& fmt
->has_denorm
)
4824 SET_REAL_EXP (r
, -14);
4825 r
->sig
[SIGSZ
-1] = image
<< 1;
4828 else if (fmt
->has_signed_zero
)
4831 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4837 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4838 ^ fmt
->qnan_msb_set
);
4839 r
->sig
[SIGSZ
-1] = image
;
4851 SET_REAL_EXP (r
, exp
- 15 + 1);
4852 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4856 /* Encode arm_bfloat types. */
4858 encode_arm_bfloat_half (const struct real_format
*fmt
, long *buf
,
4859 const REAL_VALUE_TYPE
*r
)
4861 unsigned long image
, sig
, exp
;
4862 unsigned long sign
= r
->sign
;
4865 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 8)) & 0x7f;
4883 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 6) - 1 : 0);
4884 if (r
->signalling
== fmt
->qnan_msb_set
)
4899 if (real_isdenormal (r
))
4902 exp
= REAL_EXP (r
) + 127 - 1;
4914 /* Decode arm_bfloat types. */
4916 decode_arm_bfloat_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4919 unsigned long image
= buf
[0] & 0xffff;
4920 bool sign
= (image
>> 15) & 1;
4921 int exp
= (image
>> 7) & 0xff;
4923 memset (r
, 0, sizeof (*r
));
4924 image
<<= HOST_BITS_PER_LONG
- 8;
4929 if (image
&& fmt
->has_denorm
)
4933 SET_REAL_EXP (r
, -126);
4934 r
->sig
[SIGSZ
-1] = image
<< 1;
4937 else if (fmt
->has_signed_zero
)
4940 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
4946 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4947 ^ fmt
->qnan_msb_set
);
4948 r
->sig
[SIGSZ
-1] = image
;
4960 SET_REAL_EXP (r
, exp
- 127 + 1);
4961 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4965 /* Half-precision format, as specified in IEEE 754R. */
4966 const struct real_format ieee_half_format
=
4989 /* ARM's alternative half-precision format, similar to IEEE but with
4990 no reserved exponent value for NaNs and infinities; rather, it just
4991 extends the range of exponents by one. */
4992 const struct real_format arm_half_format
=
5015 /* ARM Bfloat half-precision format. This format resembles a truncated
5016 (16-bit) version of the 32-bit IEEE 754 single-precision floating-point
5018 const struct real_format arm_bfloat_half_format
=
5020 encode_arm_bfloat_half
,
5021 decode_arm_bfloat_half
,
5042 /* A synthetic "format" for internal arithmetic. It's the size of the
5043 internal significand minus the two bits needed for proper rounding.
5044 The encode and decode routines exist only to satisfy our paranoia
5047 static void encode_internal (const struct real_format
*fmt
,
5048 long *, const REAL_VALUE_TYPE
*);
5049 static void decode_internal (const struct real_format
*,
5050 REAL_VALUE_TYPE
*, const long *);
5053 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
5054 const REAL_VALUE_TYPE
*r
)
5056 memcpy (buf
, r
, sizeof (*r
));
5060 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
5061 REAL_VALUE_TYPE
*r
, const long *buf
)
5063 memcpy (r
, buf
, sizeof (*r
));
5066 const struct real_format real_internal_format
=
5071 SIGNIFICAND_BITS
- 2,
5072 SIGNIFICAND_BITS
- 2,
5089 /* Calculate X raised to the integer exponent N in format FMT and store
5090 the result in R. Return true if the result may be inexact due to
5091 loss of precision. The algorithm is the classic "left-to-right binary
5092 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
5093 Algorithms", "The Art of Computer Programming", Volume 2. */
5096 real_powi (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5097 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
5099 unsigned HOST_WIDE_INT bit
;
5101 bool inexact
= false;
5113 /* Don't worry about overflow, from now on n is unsigned. */
5121 bit
= HOST_WIDE_INT_1U
<< (HOST_BITS_PER_WIDE_INT
- 1);
5122 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
5126 inexact
|= do_multiply (&t
, &t
, &t
);
5128 inexact
|= do_multiply (&t
, &t
, x
);
5136 inexact
|= do_divide (&t
, &dconst1
, &t
);
5138 real_convert (r
, fmt
, &t
);
5142 /* Round X to the nearest integer not larger in absolute value, i.e.
5143 towards zero, placing the result in R in format FMT. */
5146 real_trunc (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5147 const REAL_VALUE_TYPE
*x
)
5149 do_fix_trunc (r
, x
);
5151 real_convert (r
, fmt
, r
);
5154 /* Round X to the largest integer not greater in value, i.e. round
5155 down, placing the result in R in format FMT. */
5158 real_floor (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5159 const REAL_VALUE_TYPE
*x
)
5163 do_fix_trunc (&t
, x
);
5164 if (! real_identical (&t
, x
) && x
->sign
)
5165 do_add (&t
, &t
, &dconstm1
, 0);
5167 real_convert (r
, fmt
, &t
);
5172 /* Round X to the smallest integer not less then argument, i.e. round
5173 up, placing the result in R in format FMT. */
5176 real_ceil (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5177 const REAL_VALUE_TYPE
*x
)
5181 do_fix_trunc (&t
, x
);
5182 if (! real_identical (&t
, x
) && ! x
->sign
)
5183 do_add (&t
, &t
, &dconst1
, 0);
5185 real_convert (r
, fmt
, &t
);
5190 /* Round X to the nearest integer, but round halfway cases away from
5194 real_round (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5195 const REAL_VALUE_TYPE
*x
)
5197 do_add (r
, x
, &dconsthalf
, x
->sign
);
5198 do_fix_trunc (r
, r
);
5200 real_convert (r
, fmt
, r
);
5203 /* Return true (including 0) if integer part of R is even, else return
5204 false. The function is not valid for rvc_inf and rvc_nan classes. */
5207 is_even (REAL_VALUE_TYPE
*r
)
5209 gcc_assert (r
->cl
!= rvc_inf
);
5210 gcc_assert (r
->cl
!= rvc_nan
);
5212 if (r
->cl
== rvc_zero
)
5215 /* For (-1,1), number is even. */
5216 if (REAL_EXP (r
) <= 0)
5219 /* Check lowest bit, if not set, return true. */
5220 else if (REAL_EXP (r
) <= SIGNIFICAND_BITS
)
5222 unsigned int n
= SIGNIFICAND_BITS
- REAL_EXP (r
);
5223 int w
= n
/ HOST_BITS_PER_LONG
;
5225 unsigned long num
= ((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
5227 if ((r
->sig
[w
] & num
) == 0)
5236 /* Return true if R is halfway between two integers, else return
5240 is_halfway_below (const REAL_VALUE_TYPE
*r
)
5242 if (r
->cl
!= rvc_normal
)
5245 /* For numbers (-0.5,0) and (0,0.5). */
5246 if (REAL_EXP (r
) < 0)
5249 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
5251 unsigned int n
= SIGNIFICAND_BITS
- REAL_EXP (r
) - 1;
5252 int w
= n
/ HOST_BITS_PER_LONG
;
5254 for (int i
= 0; i
< w
; ++i
)
5258 unsigned long num
= 1UL << (n
% HOST_BITS_PER_LONG
);
5260 if ((r
->sig
[w
] & num
) != 0 && (r
->sig
[w
] & (num
- 1)) == 0)
5266 /* Round X to nearest integer, rounding halfway cases towards even. */
5269 real_roundeven (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5270 const REAL_VALUE_TYPE
*x
)
5272 if (is_halfway_below (x
))
5274 /* Special case as -0.5 rounds to -0.0 and
5275 similarly +0.5 rounds to +0.0. */
5276 if (REAL_EXP (x
) == 0)
5279 clear_significand_below (r
, SIGNIFICAND_BITS
);
5283 do_add (r
, x
, &dconsthalf
, x
->sign
);
5285 do_add (r
, r
, &dconstm1
, x
->sign
);
5288 real_convert (r
, fmt
, r
);
5291 real_round (r
, fmt
, x
);
5294 /* Set the sign of R to the sign of X. */
5297 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
5302 /* Check whether the real constant value given is an integer.
5303 Returns false for signaling NaN. */
5306 real_isinteger (const REAL_VALUE_TYPE
*c
, format_helper fmt
)
5308 REAL_VALUE_TYPE cint
;
5310 real_trunc (&cint
, fmt
, c
);
5311 return real_identical (c
, &cint
);
5314 /* Check whether C is an integer that fits in a HOST_WIDE_INT,
5315 storing it in *INT_OUT if so. */
5318 real_isinteger (const REAL_VALUE_TYPE
*c
, HOST_WIDE_INT
*int_out
)
5320 REAL_VALUE_TYPE cint
;
5322 HOST_WIDE_INT n
= real_to_integer (c
);
5323 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
5324 if (real_identical (c
, &cint
))
5332 /* Calculate nextafter (X, Y) or nexttoward (X, Y). Return true if
5333 underflow or overflow needs to be raised. */
5336 real_nextafter (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5337 const REAL_VALUE_TYPE
*x
, const REAL_VALUE_TYPE
*y
)
5339 int cmp
= do_compare (x
, y
, 2);
5340 /* If either operand is NaN, return qNaN. */
5343 get_canonical_qnan (r
, 0);
5346 /* If x == y, return y cast to target type. */
5349 real_convert (r
, fmt
, y
);
5353 if (x
->cl
== rvc_zero
)
5355 get_zero (r
, y
->sign
);
5357 SET_REAL_EXP (r
, fmt
->emin
- fmt
->p
+ 1);
5358 r
->sig
[SIGSZ
- 1] = SIG_MSB
;
5362 int np2
= SIGNIFICAND_BITS
- fmt
->p
;
5363 /* For denormals adjust np2 correspondingly. */
5364 if (x
->cl
== rvc_normal
&& REAL_EXP (x
) < fmt
->emin
)
5365 np2
+= fmt
->emin
- REAL_EXP (x
);
5368 get_zero (r
, x
->sign
);
5370 set_significand_bit (&u
, np2
);
5372 SET_REAL_EXP (r
, REAL_EXP (x
));
5374 if (x
->cl
== rvc_inf
)
5376 bool borrow
= sub_significands (r
, r
, &u
, 0);
5377 gcc_assert (borrow
);
5378 SET_REAL_EXP (r
, fmt
->emax
);
5380 else if (cmp
== (x
->sign
? 1 : -1))
5382 if (add_significands (r
, x
, &u
))
5384 /* Overflow. Means the significand had been all ones, and
5385 is now all zeros. Need to increase the exponent, and
5386 possibly re-normalize it. */
5387 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
5388 if (REAL_EXP (r
) > fmt
->emax
)
5390 get_inf (r
, x
->sign
);
5393 r
->sig
[SIGSZ
- 1] = SIG_MSB
;
5398 if (REAL_EXP (x
) > fmt
->emin
&& x
->sig
[SIGSZ
- 1] == SIG_MSB
)
5401 for (i
= SIGSZ
- 2; i
>= 0; i
--)
5406 /* When mantissa is 1.0, we need to subtract only
5407 half of u: nextafter (1.0, 0.0) is 1.0 - __DBL_EPSILON__ / 2
5408 rather than 1.0 - __DBL_EPSILON__. */
5409 clear_significand_bit (&u
, np2
);
5411 set_significand_bit (&u
, np2
);
5414 sub_significands (r
, x
, &u
, 0);
5417 /* Clear out trailing garbage. */
5418 clear_significand_below (r
, np2
);
5420 if (REAL_EXP (r
) <= fmt
->emin
- fmt
->p
)
5422 get_zero (r
, x
->sign
);
5425 return r
->cl
== rvc_zero
|| REAL_EXP (r
) < fmt
->emin
;
5428 /* Write into BUF the maximum representable finite floating-point
5429 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5430 float string. LEN is the size of BUF, and the buffer must be large
5431 enough to contain the resulting string. If NORM_MAX, instead write
5432 the maximum representable finite normalized floating-point number,
5433 defined to be such that all choices of digits for that exponent are
5434 representable in the format (this only makes a difference for IBM
5438 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
,
5443 bool is_ibm_extended
= fmt
->pnan
< fmt
->p
;
5445 strcpy (buf
, "0x0.");
5447 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
5450 *p
++ = "08ce"[n
- i
];
5452 (is_ibm_extended
&& norm_max
) ? fmt
->emax
- 1 : fmt
->emax
);
5453 if (is_ibm_extended
&& !norm_max
)
5455 /* This is an IBM extended double format made up of two IEEE
5456 doubles. The value of the long double is the sum of the
5457 values of the two parts. The most significant part is
5458 required to be the value of the long double rounded to the
5459 nearest double. Rounding means we need a slightly smaller
5460 value for LDBL_MAX. */
5461 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
5464 gcc_assert (strlen (buf
) < len
);
5467 /* True if all values of integral type can be represented
5468 by this floating-point type exactly. */
5470 bool format_helper::can_represent_integral_type_p (tree type
) const
5472 gcc_assert (! decimal_p () && INTEGRAL_TYPE_P (type
));
5474 /* INT?_MIN is power-of-two so it takes
5475 only one mantissa bit. */
5476 bool signed_p
= TYPE_SIGN (type
) == SIGNED
;
5477 return TYPE_PRECISION (type
) - signed_p
<= significand_size (*this);
5480 /* True if mode M has a NaN representation and
5481 the treatment of NaN operands is important. */
5484 HONOR_NANS (machine_mode m
)
5486 return MODE_HAS_NANS (m
) && !flag_finite_math_only
;
5490 HONOR_NANS (const_tree t
)
5492 return HONOR_NANS (element_mode (t
));
5496 HONOR_NANS (const_rtx x
)
5498 return HONOR_NANS (GET_MODE (x
));
5501 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5504 HONOR_SNANS (machine_mode m
)
5506 return flag_signaling_nans
&& HONOR_NANS (m
);
5510 HONOR_SNANS (const_tree t
)
5512 return HONOR_SNANS (element_mode (t
));
5516 HONOR_SNANS (const_rtx x
)
5518 return HONOR_SNANS (GET_MODE (x
));
5521 /* As for HONOR_NANS, but true if the mode can represent infinity and
5522 the treatment of infinite values is important. */
5525 HONOR_INFINITIES (machine_mode m
)
5527 return MODE_HAS_INFINITIES (m
) && !flag_finite_math_only
;
5531 HONOR_INFINITIES (const_tree t
)
5533 return HONOR_INFINITIES (element_mode (t
));
5537 HONOR_INFINITIES (const_rtx x
)
5539 return HONOR_INFINITIES (GET_MODE (x
));
5542 /* Like HONOR_NANS, but true if the given mode distinguishes between
5543 positive and negative zero, and the sign of zero is important. */
5546 HONOR_SIGNED_ZEROS (machine_mode m
)
5548 return MODE_HAS_SIGNED_ZEROS (m
) && flag_signed_zeros
;
5552 HONOR_SIGNED_ZEROS (const_tree t
)
5554 return HONOR_SIGNED_ZEROS (element_mode (t
));
5558 HONOR_SIGNED_ZEROS (const_rtx x
)
5560 return HONOR_SIGNED_ZEROS (GET_MODE (x
));
5563 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5564 and the rounding mode is important. */
5567 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m
)
5569 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m
) && flag_rounding_math
;
5573 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t
)
5575 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t
));
5579 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x
)
5581 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x
));
5584 /* Fills r with the largest value such that 1 + r*r won't overflow.
5585 This is used in both sin (atan (x)) and cos (atan(x)) optimizations. */
5588 build_sinatan_real (REAL_VALUE_TYPE
* r
, tree type
)
5590 REAL_VALUE_TYPE maxval
;
5591 mpfr_t mpfr_const1
, mpfr_c
, mpfr_maxval
;
5592 machine_mode mode
= TYPE_MODE (type
);
5593 const struct real_format
* fmt
= REAL_MODE_FORMAT (mode
);
5595 real_maxval (&maxval
, 0, mode
);
5597 mpfr_inits (mpfr_const1
, mpfr_c
, mpfr_maxval
, NULL
);
5599 mpfr_from_real (mpfr_const1
, &dconst1
, MPFR_RNDN
);
5600 mpfr_from_real (mpfr_maxval
, &maxval
, MPFR_RNDN
);
5602 mpfr_sub (mpfr_c
, mpfr_maxval
, mpfr_const1
, MPFR_RNDN
);
5603 mpfr_sqrt (mpfr_c
, mpfr_c
, MPFR_RNDZ
);
5605 real_from_mpfr (r
, mpfr_c
, fmt
, MPFR_RNDZ
);
5607 mpfr_clears (mpfr_const1
, mpfr_c
, mpfr_maxval
, NULL
);