1 /* real.cc - software floating point emulation.
2 Copyright (C) 1993-2022 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 /* Initialize R with a positive zero. */
118 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
120 memset (r
, 0, sizeof (*r
));
124 /* Initialize R with the canonical quiet NaN. */
127 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
129 memset (r
, 0, sizeof (*r
));
136 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
138 memset (r
, 0, sizeof (*r
));
146 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
148 memset (r
, 0, sizeof (*r
));
154 /* Right-shift the significand of A by N bits; put the result in the
155 significand of R. If any one bits are shifted out, return true. */
158 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
161 unsigned long sticky
= 0;
162 unsigned int i
, ofs
= 0;
164 if (n
>= HOST_BITS_PER_LONG
)
166 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
168 n
&= HOST_BITS_PER_LONG
- 1;
173 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
174 for (i
= 0; i
< SIGSZ
; ++i
)
177 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
178 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
179 << (HOST_BITS_PER_LONG
- n
)));
184 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
185 r
->sig
[i
] = a
->sig
[ofs
+ i
];
186 for (; i
< SIGSZ
; ++i
)
193 /* Right-shift the significand of A by N bits; put the result in the
197 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
200 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
202 n
&= HOST_BITS_PER_LONG
- 1;
205 for (i
= 0; i
< SIGSZ
; ++i
)
208 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
209 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
210 << (HOST_BITS_PER_LONG
- n
)));
215 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
216 r
->sig
[i
] = a
->sig
[ofs
+ i
];
217 for (; i
< SIGSZ
; ++i
)
222 /* Left-shift the significand of A by N bits; put the result in the
226 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
229 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
231 n
&= HOST_BITS_PER_LONG
- 1;
234 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
235 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
236 for (; i
< SIGSZ
; ++i
)
237 r
->sig
[SIGSZ
-1-i
] = 0;
240 for (i
= 0; i
< SIGSZ
; ++i
)
243 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
244 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
245 >> (HOST_BITS_PER_LONG
- n
)));
249 /* Likewise, but N is specialized to 1. */
252 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
256 for (i
= SIGSZ
- 1; i
> 0; --i
)
257 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
258 r
->sig
[0] = a
->sig
[0] << 1;
261 /* Add the significands of A and B, placing the result in R. Return
262 true if there was carry out of the most significant word. */
265 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
266 const REAL_VALUE_TYPE
*b
)
271 for (i
= 0; i
< SIGSZ
; ++i
)
273 unsigned long ai
= a
->sig
[i
];
274 unsigned long ri
= ai
+ b
->sig
[i
];
290 /* Subtract the significands of A and B, placing the result in R. CARRY is
291 true if there's a borrow incoming to the least significant word.
292 Return true if there was borrow out of the most significant word. */
295 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
296 const REAL_VALUE_TYPE
*b
, int carry
)
300 for (i
= 0; i
< SIGSZ
; ++i
)
302 unsigned long ai
= a
->sig
[i
];
303 unsigned long ri
= ai
- b
->sig
[i
];
319 /* Negate the significand A, placing the result in R. */
322 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
327 for (i
= 0; i
< SIGSZ
; ++i
)
329 unsigned long ri
, ai
= a
->sig
[i
];
348 /* Compare significands. Return tri-state vs zero. */
351 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
355 for (i
= SIGSZ
- 1; i
>= 0; --i
)
357 unsigned long ai
= a
->sig
[i
];
358 unsigned long bi
= b
->sig
[i
];
369 /* Return true if A is nonzero. */
372 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
376 for (i
= SIGSZ
- 1; i
>= 0; --i
)
383 /* Set bit N of the significand of R. */
386 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
388 r
->sig
[n
/ HOST_BITS_PER_LONG
]
389 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
392 /* Clear bit N of the significand of R. */
395 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
397 r
->sig
[n
/ HOST_BITS_PER_LONG
]
398 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
401 /* Test bit N of the significand of R. */
404 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
406 /* ??? Compiler bug here if we return this expression directly.
407 The conversion to bool strips the "&1" and we wind up testing
408 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
409 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
413 /* Clear bits 0..N-1 of the significand of R. */
416 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
418 int i
, w
= n
/ HOST_BITS_PER_LONG
;
420 for (i
= 0; i
< w
; ++i
)
423 /* We are actually passing N == SIGNIFICAND_BITS which would result
424 in an out-of-bound access below. */
425 if (n
% HOST_BITS_PER_LONG
!= 0)
426 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
429 /* Divide the significands of A and B, placing the result in R. Return
430 true if the division was inexact. */
433 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
434 const REAL_VALUE_TYPE
*b
)
437 int i
, bit
= SIGNIFICAND_BITS
- 1;
438 unsigned long msb
, inexact
;
441 memset (r
->sig
, 0, sizeof (r
->sig
));
447 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
448 lshift_significand_1 (&u
, &u
);
450 if (msb
|| cmp_significands (&u
, b
) >= 0)
452 sub_significands (&u
, &u
, b
, 0);
453 set_significand_bit (r
, bit
);
458 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
464 /* Adjust the exponent and significand of R such that the most
465 significant bit is set. We underflow to zero and overflow to
466 infinity here, without denormals. (The intermediate representation
467 exponent is large enough to handle target denormals normalized.) */
470 normalize (REAL_VALUE_TYPE
*r
)
478 /* Find the first word that is nonzero. */
479 for (i
= SIGSZ
- 1; i
>= 0; i
--)
481 shift
+= HOST_BITS_PER_LONG
;
485 /* Zero significand flushes to zero. */
493 /* Find the first bit that is nonzero. */
495 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
501 exp
= REAL_EXP (r
) - shift
;
503 get_inf (r
, r
->sign
);
504 else if (exp
< -MAX_EXP
)
505 get_zero (r
, r
->sign
);
508 SET_REAL_EXP (r
, exp
);
509 lshift_significand (r
, r
, shift
);
514 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
515 result may be inexact due to a loss of precision. */
518 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
519 const REAL_VALUE_TYPE
*b
, int subtract_p
)
523 bool inexact
= false;
525 /* Determine if we need to add or subtract. */
527 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
529 switch (CLASS2 (a
->cl
, b
->cl
))
531 case CLASS2 (rvc_zero
, rvc_zero
):
532 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
533 get_zero (r
, sign
& !subtract_p
);
536 case CLASS2 (rvc_zero
, rvc_normal
):
537 case CLASS2 (rvc_zero
, rvc_inf
):
538 case CLASS2 (rvc_zero
, rvc_nan
):
540 case CLASS2 (rvc_normal
, rvc_nan
):
541 case CLASS2 (rvc_inf
, rvc_nan
):
542 case CLASS2 (rvc_nan
, rvc_nan
):
543 /* ANY + NaN = NaN. */
544 case CLASS2 (rvc_normal
, rvc_inf
):
547 /* Make resulting NaN value to be qNaN. The caller has the
548 responsibility to avoid the operation if flag_signaling_nans
551 r
->sign
= sign
^ subtract_p
;
554 case CLASS2 (rvc_normal
, rvc_zero
):
555 case CLASS2 (rvc_inf
, rvc_zero
):
556 case CLASS2 (rvc_nan
, rvc_zero
):
558 case CLASS2 (rvc_nan
, rvc_normal
):
559 case CLASS2 (rvc_nan
, rvc_inf
):
560 /* NaN + ANY = NaN. */
561 case CLASS2 (rvc_inf
, rvc_normal
):
564 /* Make resulting NaN value to be qNaN. The caller has the
565 responsibility to avoid the operation if flag_signaling_nans
570 case CLASS2 (rvc_inf
, rvc_inf
):
572 /* Inf - Inf = NaN. */
573 get_canonical_qnan (r
, 0);
575 /* Inf + Inf = Inf. */
579 case CLASS2 (rvc_normal
, rvc_normal
):
586 /* Swap the arguments such that A has the larger exponent. */
587 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
590 const REAL_VALUE_TYPE
*t
;
597 /* If the exponents are not identical, we need to shift the
598 significand of B down. */
601 /* If the exponents are too far apart, the significands
602 do not overlap, which makes the subtraction a noop. */
603 if (dexp
>= SIGNIFICAND_BITS
)
610 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
616 if (sub_significands (r
, a
, b
, inexact
))
618 /* We got a borrow out of the subtraction. That means that
619 A and B had the same exponent, and B had the larger
620 significand. We need to swap the sign and negate the
623 neg_significand (r
, r
);
628 if (add_significands (r
, a
, b
))
630 /* We got carry out of the addition. This means we need to
631 shift the significand back down one bit and increase the
633 inexact
|= sticky_rshift_significand (r
, r
, 1);
634 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
645 SET_REAL_EXP (r
, exp
);
646 /* Zero out the remaining fields. */
651 /* Re-normalize the result. */
654 /* Special case: if the subtraction results in zero, the result
656 if (r
->cl
== rvc_zero
)
659 r
->sig
[0] |= inexact
;
664 /* Calculate R = A * B. Return true if the result may be inexact. */
667 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
668 const REAL_VALUE_TYPE
*b
)
670 REAL_VALUE_TYPE u
, t
, *rr
;
671 unsigned int i
, j
, k
;
672 int sign
= a
->sign
^ b
->sign
;
673 bool inexact
= false;
675 switch (CLASS2 (a
->cl
, b
->cl
))
677 case CLASS2 (rvc_zero
, rvc_zero
):
678 case CLASS2 (rvc_zero
, rvc_normal
):
679 case CLASS2 (rvc_normal
, rvc_zero
):
680 /* +-0 * ANY = 0 with appropriate sign. */
684 case CLASS2 (rvc_zero
, rvc_nan
):
685 case CLASS2 (rvc_normal
, rvc_nan
):
686 case CLASS2 (rvc_inf
, rvc_nan
):
687 case CLASS2 (rvc_nan
, rvc_nan
):
688 /* ANY * NaN = NaN. */
690 /* Make resulting NaN value to be qNaN. The caller has the
691 responsibility to avoid the operation if flag_signaling_nans
697 case CLASS2 (rvc_nan
, rvc_zero
):
698 case CLASS2 (rvc_nan
, rvc_normal
):
699 case CLASS2 (rvc_nan
, rvc_inf
):
700 /* NaN * ANY = NaN. */
702 /* Make resulting NaN value to be qNaN. The caller has the
703 responsibility to avoid the operation if flag_signaling_nans
709 case CLASS2 (rvc_zero
, rvc_inf
):
710 case CLASS2 (rvc_inf
, rvc_zero
):
712 get_canonical_qnan (r
, sign
);
715 case CLASS2 (rvc_inf
, rvc_inf
):
716 case CLASS2 (rvc_normal
, rvc_inf
):
717 case CLASS2 (rvc_inf
, rvc_normal
):
718 /* Inf * Inf = Inf, R * Inf = Inf */
722 case CLASS2 (rvc_normal
, rvc_normal
):
729 if (r
== a
|| r
== b
)
735 /* Collect all the partial products. Since we don't have sure access
736 to a widening multiply, we split each long into two half-words.
738 Consider the long-hand form of a four half-word multiplication:
748 We construct partial products of the widened half-word products
749 that are known to not overlap, e.g. DF+DH. Each such partial
750 product is given its proper exponent, which allows us to sum them
751 and obtain the finished product. */
753 for (i
= 0; i
< SIGSZ
* 2; ++i
)
755 unsigned long ai
= a
->sig
[i
/ 2];
757 ai
>>= HOST_BITS_PER_LONG
/ 2;
759 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
764 for (j
= 0; j
< 2; ++j
)
766 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
767 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
776 /* Would underflow to zero, which we shouldn't bother adding. */
781 memset (&u
, 0, sizeof (u
));
783 SET_REAL_EXP (&u
, exp
);
785 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
787 unsigned long bi
= b
->sig
[k
/ 2];
789 bi
>>= HOST_BITS_PER_LONG
/ 2;
791 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
793 u
.sig
[k
/ 2] = ai
* bi
;
797 inexact
|= do_add (rr
, rr
, &u
, 0);
808 /* Calculate R = A / B. Return true if the result may be inexact. */
811 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
812 const REAL_VALUE_TYPE
*b
)
814 int exp
, sign
= a
->sign
^ b
->sign
;
815 REAL_VALUE_TYPE t
, *rr
;
818 switch (CLASS2 (a
->cl
, b
->cl
))
820 case CLASS2 (rvc_zero
, rvc_zero
):
822 case CLASS2 (rvc_inf
, rvc_inf
):
823 /* Inf / Inf = NaN. */
824 get_canonical_qnan (r
, sign
);
827 case CLASS2 (rvc_zero
, rvc_normal
):
828 case CLASS2 (rvc_zero
, rvc_inf
):
830 case CLASS2 (rvc_normal
, rvc_inf
):
835 case CLASS2 (rvc_normal
, rvc_zero
):
837 case CLASS2 (rvc_inf
, rvc_zero
):
842 case CLASS2 (rvc_zero
, rvc_nan
):
843 case CLASS2 (rvc_normal
, rvc_nan
):
844 case CLASS2 (rvc_inf
, rvc_nan
):
845 case CLASS2 (rvc_nan
, rvc_nan
):
846 /* ANY / NaN = NaN. */
848 /* Make resulting NaN value to be qNaN. The caller has the
849 responsibility to avoid the operation if flag_signaling_nans
855 case CLASS2 (rvc_nan
, rvc_zero
):
856 case CLASS2 (rvc_nan
, rvc_normal
):
857 case CLASS2 (rvc_nan
, rvc_inf
):
858 /* NaN / ANY = NaN. */
860 /* Make resulting NaN value to be qNaN. The caller has the
861 responsibility to avoid the operation if flag_signaling_nans
867 case CLASS2 (rvc_inf
, rvc_normal
):
872 case CLASS2 (rvc_normal
, rvc_normal
):
879 if (r
== a
|| r
== b
)
884 /* Make sure all fields in the result are initialized. */
889 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
900 SET_REAL_EXP (rr
, exp
);
902 inexact
= div_significands (rr
, a
, b
);
904 /* Re-normalize the result. */
906 rr
->sig
[0] |= inexact
;
914 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
915 one of the two operands is a NaN. */
918 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
923 switch (CLASS2 (a
->cl
, b
->cl
))
925 case CLASS2 (rvc_zero
, rvc_zero
):
926 /* Sign of zero doesn't matter for compares. */
929 case CLASS2 (rvc_normal
, rvc_zero
):
930 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
932 return decimal_do_compare (a
, b
, nan_result
);
934 case CLASS2 (rvc_inf
, rvc_zero
):
935 case CLASS2 (rvc_inf
, rvc_normal
):
936 return (a
->sign
? -1 : 1);
938 case CLASS2 (rvc_inf
, rvc_inf
):
939 return -a
->sign
- -b
->sign
;
941 case CLASS2 (rvc_zero
, rvc_normal
):
942 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
944 return decimal_do_compare (a
, b
, nan_result
);
946 case CLASS2 (rvc_zero
, rvc_inf
):
947 case CLASS2 (rvc_normal
, rvc_inf
):
948 return (b
->sign
? 1 : -1);
950 case CLASS2 (rvc_zero
, rvc_nan
):
951 case CLASS2 (rvc_normal
, rvc_nan
):
952 case CLASS2 (rvc_inf
, rvc_nan
):
953 case CLASS2 (rvc_nan
, rvc_nan
):
954 case CLASS2 (rvc_nan
, rvc_zero
):
955 case CLASS2 (rvc_nan
, rvc_normal
):
956 case CLASS2 (rvc_nan
, rvc_inf
):
959 case CLASS2 (rvc_normal
, rvc_normal
):
966 if (a
->decimal
|| b
->decimal
)
967 return decimal_do_compare (a
, b
, nan_result
);
969 if (a
->sign
!= b
->sign
)
970 return -a
->sign
- -b
->sign
;
972 if (REAL_EXP (a
) > REAL_EXP (b
))
974 else if (REAL_EXP (a
) < REAL_EXP (b
))
977 ret
= cmp_significands (a
, b
);
979 return (a
->sign
? -ret
: ret
);
982 /* Return A truncated to an integral value toward zero. */
985 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
994 /* Make resulting NaN value to be qNaN. The caller has the
995 responsibility to avoid the operation if flag_signaling_nans
1003 decimal_do_fix_trunc (r
, a
);
1006 if (REAL_EXP (r
) <= 0)
1007 get_zero (r
, r
->sign
);
1008 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
1009 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
1017 /* Perform the binary or unary operation described by CODE.
1018 For a unary operation, leave OP1 NULL. This function returns
1019 true if the result may be inexact due to loss of precision. */
1022 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
1023 const REAL_VALUE_TYPE
*op1
)
1025 enum tree_code code
= (enum tree_code
) icode
;
1027 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1028 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1033 /* Clear any padding areas in *r if it isn't equal to one of the
1034 operands so that we can later do bitwise comparisons later on. */
1035 if (r
!= op0
&& r
!= op1
)
1036 memset (r
, '\0', sizeof (*r
));
1037 return do_add (r
, op0
, op1
, 0);
1040 if (r
!= op0
&& r
!= op1
)
1041 memset (r
, '\0', sizeof (*r
));
1042 return do_add (r
, op0
, op1
, 1);
1045 if (r
!= op0
&& r
!= op1
)
1046 memset (r
, '\0', sizeof (*r
));
1047 return do_multiply (r
, op0
, op1
);
1050 if (r
!= op0
&& r
!= op1
)
1051 memset (r
, '\0', sizeof (*r
));
1052 return do_divide (r
, op0
, op1
);
1055 if (op1
->cl
== rvc_nan
)
1058 /* Make resulting NaN value to be qNaN. The caller has the
1059 responsibility to avoid the operation if flag_signaling_nans
1063 else if (do_compare (op0
, op1
, -1) < 0)
1070 if (op1
->cl
== rvc_nan
)
1073 /* Make resulting NaN value to be qNaN. The caller has the
1074 responsibility to avoid the operation if flag_signaling_nans
1078 else if (do_compare (op0
, op1
, 1) < 0)
1094 case FIX_TRUNC_EXPR
:
1095 do_fix_trunc (r
, op0
);
1105 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1108 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1113 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1116 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1120 /* Return whether OP0 == OP1. */
1123 real_equal (const REAL_VALUE_TYPE
*op0
, const REAL_VALUE_TYPE
*op1
)
1125 return do_compare (op0
, op1
, -1) == 0;
1128 /* Return whether OP0 < OP1. */
1131 real_less (const REAL_VALUE_TYPE
*op0
, const REAL_VALUE_TYPE
*op1
)
1133 return do_compare (op0
, op1
, 1) < 0;
1137 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1138 const REAL_VALUE_TYPE
*op1
)
1140 enum tree_code code
= (enum tree_code
) icode
;
1145 return real_less (op0
, op1
);
1147 return do_compare (op0
, op1
, 1) <= 0;
1149 return do_compare (op0
, op1
, -1) > 0;
1151 return do_compare (op0
, op1
, -1) >= 0;
1153 return real_equal (op0
, op1
);
1155 return do_compare (op0
, op1
, -1) != 0;
1156 case UNORDERED_EXPR
:
1157 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1159 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1161 return do_compare (op0
, op1
, -1) < 0;
1163 return do_compare (op0
, op1
, -1) <= 0;
1165 return do_compare (op0
, op1
, 1) > 0;
1167 return do_compare (op0
, op1
, 1) >= 0;
1169 return do_compare (op0
, op1
, 0) == 0;
1171 return do_compare (op0
, op1
, 0) != 0;
1178 /* Return floor log2(R). */
1181 real_exponent (const REAL_VALUE_TYPE
*r
)
1189 return (unsigned int)-1 >> 1;
1191 return REAL_EXP (r
);
1197 /* R = OP0 * 2**EXP. */
1200 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1208 /* Make resulting NaN value to be qNaN. The caller has the
1209 responsibility to avoid the operation if flag_signaling_nans
1215 exp
+= REAL_EXP (op0
);
1217 get_inf (r
, r
->sign
);
1218 else if (exp
< -MAX_EXP
)
1219 get_zero (r
, r
->sign
);
1221 SET_REAL_EXP (r
, exp
);
1229 /* Determine whether a floating-point value X is infinite. */
1232 real_isinf (const REAL_VALUE_TYPE
*r
)
1234 return (r
->cl
== rvc_inf
);
1237 /* Determine whether a floating-point value X is infinite with SIGN. */
1240 real_isinf (const REAL_VALUE_TYPE
*r
, bool sign
)
1242 return real_isinf (r
) && r
->sign
== sign
;
1245 /* Determine whether a floating-point value X is a NaN. */
1248 real_isnan (const REAL_VALUE_TYPE
*r
)
1250 return (r
->cl
== rvc_nan
);
1253 /* Determine whether a floating-point value X is a signaling NaN. */
1254 bool real_issignaling_nan (const REAL_VALUE_TYPE
*r
)
1256 return real_isnan (r
) && r
->signalling
;
1259 /* Determine whether a floating-point value X is finite. */
1262 real_isfinite (const REAL_VALUE_TYPE
*r
)
1264 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1267 /* Determine whether a floating-point value X is negative. */
1270 real_isneg (const REAL_VALUE_TYPE
*r
)
1275 /* Determine whether a floating-point value X is plus or minus zero. */
1278 real_iszero (const REAL_VALUE_TYPE
*r
)
1280 return r
->cl
== rvc_zero
;
1283 /* Determine whether a floating-point value X is zero with SIGN. */
1286 real_iszero (const REAL_VALUE_TYPE
*r
, bool sign
)
1288 return real_iszero (r
) && r
->sign
== sign
;
1291 /* Determine whether a floating-point value X is minus zero. */
1294 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1296 return r
->sign
&& r
->cl
== rvc_zero
;
1299 /* Compare two floating-point objects for bitwise identity. */
1302 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1308 if (a
->sign
!= b
->sign
)
1318 if (a
->decimal
!= b
->decimal
)
1320 if (REAL_EXP (a
) != REAL_EXP (b
))
1325 if (a
->signalling
!= b
->signalling
)
1327 /* The significand is ignored for canonical NaNs. */
1328 if (a
->canonical
|| b
->canonical
)
1329 return a
->canonical
== b
->canonical
;
1336 for (i
= 0; i
< SIGSZ
; ++i
)
1337 if (a
->sig
[i
] != b
->sig
[i
])
1343 /* Try to change R into its exact multiplicative inverse in format FMT.
1344 Return true if successful. */
1347 exact_real_inverse (format_helper fmt
, REAL_VALUE_TYPE
*r
)
1349 const REAL_VALUE_TYPE
*one
= real_digit (1);
1353 if (r
->cl
!= rvc_normal
)
1356 /* Check for a power of two: all significand bits zero except the MSB. */
1357 for (i
= 0; i
< SIGSZ
-1; ++i
)
1360 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1363 /* Find the inverse and truncate to the required format. */
1364 do_divide (&u
, one
, r
);
1365 real_convert (&u
, fmt
, &u
);
1367 /* The rounding may have overflowed. */
1368 if (u
.cl
!= rvc_normal
)
1370 for (i
= 0; i
< SIGSZ
-1; ++i
)
1373 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1380 /* Return true if arithmetic on values in IMODE that were promoted
1381 from values in TMODE is equivalent to direct arithmetic on values
1385 real_can_shorten_arithmetic (machine_mode imode
, machine_mode tmode
)
1387 const struct real_format
*tfmt
, *ifmt
;
1388 tfmt
= REAL_MODE_FORMAT (tmode
);
1389 ifmt
= REAL_MODE_FORMAT (imode
);
1390 /* These conditions are conservative rather than trying to catch the
1391 exact boundary conditions; the main case to allow is IEEE float
1393 return (ifmt
->b
== tfmt
->b
1394 && ifmt
->p
> 2 * tfmt
->p
1395 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1396 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1397 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1398 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1399 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1400 && (ifmt
->has_sign_dependent_rounding
1401 == tfmt
->has_sign_dependent_rounding
)
1402 && ifmt
->has_nans
>= tfmt
->has_nans
1403 && ifmt
->has_inf
>= tfmt
->has_inf
1404 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1405 && !MODE_COMPOSITE_P (tmode
)
1406 && !MODE_COMPOSITE_P (imode
));
1409 /* Render R as an integer. */
1412 real_to_integer (const REAL_VALUE_TYPE
*r
)
1414 unsigned HOST_WIDE_INT i
;
1425 i
= HOST_WIDE_INT_1U
<< (HOST_BITS_PER_WIDE_INT
- 1);
1432 return decimal_real_to_integer (r
);
1434 if (REAL_EXP (r
) <= 0)
1436 /* Only force overflow for unsigned overflow. Signed overflow is
1437 undefined, so it doesn't matter what we return, and some callers
1438 expect to be able to use this routine for both signed and
1439 unsigned conversions. */
1440 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1443 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1444 i
= r
->sig
[SIGSZ
-1];
1447 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1448 i
= r
->sig
[SIGSZ
-1];
1449 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1450 i
|= r
->sig
[SIGSZ
-2];
1453 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1464 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1465 be represented in precision, *FAIL is set to TRUE. */
1468 real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
1470 HOST_WIDE_INT val
[2 * WIDE_INT_MAX_ELTS
];
1479 return wi::zero (precision
);
1487 return wi::set_bit_in_zero (precision
- 1, precision
);
1489 return ~wi::set_bit_in_zero (precision
- 1, precision
);
1493 return decimal_real_to_integer (r
, fail
, precision
);
1498 /* Only force overflow for unsigned overflow. Signed overflow is
1499 undefined, so it doesn't matter what we return, and some callers
1500 expect to be able to use this routine for both signed and
1501 unsigned conversions. */
1502 if (exp
> precision
)
1505 /* Put the significand into a wide_int that has precision W, which
1506 is the smallest HWI-multiple that has at least PRECISION bits.
1507 This ensures that the top bit of the significand is in the
1508 top bit of the wide_int. */
1509 words
= (precision
+ HOST_BITS_PER_WIDE_INT
- 1) / HOST_BITS_PER_WIDE_INT
;
1510 w
= words
* HOST_BITS_PER_WIDE_INT
;
1512 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1513 for (int i
= 0; i
< words
; i
++)
1515 int j
= SIGSZ
- words
+ i
;
1516 val
[i
] = (j
< 0) ? 0 : r
->sig
[j
];
1519 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1520 for (int i
= 0; i
< words
; i
++)
1522 int j
= SIGSZ
- (words
* 2) + (i
* 2);
1529 val
[i
] |= (unsigned HOST_WIDE_INT
) r
->sig
[j
] << HOST_BITS_PER_LONG
;
1532 /* Shift the value into place and truncate to the desired precision. */
1533 result
= wide_int::from_array (val
, words
, w
);
1534 result
= wi::lrshift (result
, w
- exp
);
1535 result
= wide_int::from (result
, precision
, UNSIGNED
);
1547 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1548 of NUM / DEN. Return the quotient and place the remainder in NUM.
1549 It is expected that NUM / DEN are close enough that the quotient is
1552 static unsigned long
1553 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1555 unsigned long q
, msb
;
1556 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1565 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1567 lshift_significand_1 (num
, num
);
1569 if (msb
|| cmp_significands (num
, den
) >= 0)
1571 sub_significands (num
, num
, den
, 0);
1575 while (--expn
>= expd
);
1577 SET_REAL_EXP (num
, expd
);
1583 /* Render R as a decimal floating point constant. Emit DIGITS significant
1584 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1585 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1586 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1587 to a string that, when parsed back in mode MODE, yields the same value. */
1589 #define M_LOG10_2 0.30102999566398119521
1592 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1593 size_t buf_size
, size_t digits
,
1594 int crop_trailing_zeros
, machine_mode mode
)
1596 const struct real_format
*fmt
= NULL
;
1597 const REAL_VALUE_TYPE
*one
, *ten
;
1598 REAL_VALUE_TYPE r
, pten
, u
, v
;
1599 int dec_exp
, cmp_one
, digit
;
1601 char *p
, *first
, *last
;
1605 if (mode
!= VOIDmode
)
1607 fmt
= REAL_MODE_FORMAT (mode
);
1615 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1620 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1623 /* ??? Print the significand as well, if not canonical? */
1624 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1625 (r_orig
->signalling
? 'S' : 'Q'));
1633 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1637 /* Bound the number of digits printed by the size of the representation. */
1638 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1639 if (digits
== 0 || digits
> max_digits
)
1640 digits
= max_digits
;
1642 /* Estimate the decimal exponent, and compute the length of the string it
1643 will print as. Be conservative and add one to account for possible
1644 overflow or rounding error. */
1645 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1646 for (max_digits
= 1; dec_exp
; max_digits
++)
1649 /* Bound the number of digits printed by the size of the output buffer. */
1650 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1651 gcc_assert (max_digits
<= buf_size
);
1652 if (digits
> max_digits
)
1653 digits
= max_digits
;
1655 one
= real_digit (1);
1656 ten
= ten_to_ptwo (0);
1664 cmp_one
= do_compare (&r
, one
, 0);
1669 /* Number is greater than one. Convert significand to an integer
1670 and strip trailing decimal zeros. */
1673 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1675 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1676 m
= floor_log2 (max_digits
);
1678 /* Iterate over the bits of the possible powers of 10 that might
1679 be present in U and eliminate them. That is, if we find that
1680 10**2**M divides U evenly, keep the division and increase
1686 do_divide (&t
, &u
, ten_to_ptwo (m
));
1687 do_fix_trunc (&v
, &t
);
1688 if (cmp_significands (&v
, &t
) == 0)
1696 /* Revert the scaling to integer that we performed earlier. */
1697 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1698 - (SIGNIFICAND_BITS
- 1));
1701 /* Find power of 10. Do this by dividing out 10**2**M when
1702 this is larger than the current remainder. Fill PTEN with
1703 the power of 10 that we compute. */
1704 if (REAL_EXP (&r
) > 0)
1706 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1709 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1710 if (do_compare (&u
, ptentwo
, 0) >= 0)
1712 do_divide (&u
, &u
, ptentwo
);
1713 do_multiply (&pten
, &pten
, ptentwo
);
1720 /* We managed to divide off enough tens in the above reduction
1721 loop that we've now got a negative exponent. Fall into the
1722 less-than-one code to compute the proper value for PTEN. */
1729 /* Number is less than one. Pad significand with leading
1735 /* Stop if we'd shift bits off the bottom. */
1739 do_multiply (&u
, &v
, ten
);
1741 /* Stop if we're now >= 1 or zero. */
1742 if (REAL_EXP (&u
) > 0 || u
.cl
== rvc_zero
)
1750 /* Find power of 10. Do this by multiplying in P=10**2**M when
1751 the current remainder is smaller than 1/P. Fill PTEN with the
1752 power of 10 that we compute. */
1753 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1756 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1757 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1759 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1761 do_multiply (&v
, &v
, ptentwo
);
1762 do_multiply (&pten
, &pten
, ptentwo
);
1768 /* Invert the positive power of 10 that we've collected so far. */
1769 do_divide (&pten
, one
, &pten
);
1777 /* At this point, PTEN should contain the nearest power of 10 smaller
1778 than R, such that this division produces the first digit.
1780 Using a divide-step primitive that returns the complete integral
1781 remainder avoids the rounding error that would be produced if
1782 we were to use do_divide here and then simply multiply by 10 for
1783 each subsequent digit. */
1785 digit
= rtd_divmod (&r
, &pten
);
1787 /* Be prepared for error in that division via underflow ... */
1788 if (digit
== 0 && cmp_significand_0 (&r
))
1790 /* Multiply by 10 and try again. */
1791 do_multiply (&r
, &r
, ten
);
1792 digit
= rtd_divmod (&r
, &pten
);
1794 gcc_assert (digit
!= 0);
1797 /* ... or overflow. */
1807 gcc_assert (digit
<= 10);
1811 /* Generate subsequent digits. */
1812 while (--digits
> 0)
1814 do_multiply (&r
, &r
, ten
);
1815 digit
= rtd_divmod (&r
, &pten
);
1820 /* Generate one more digit with which to do rounding. */
1821 do_multiply (&r
, &r
, ten
);
1822 digit
= rtd_divmod (&r
, &pten
);
1824 /* Round the result. */
1825 if (fmt
&& fmt
->round_towards_zero
)
1827 /* If the format uses round towards zero when parsing the string
1828 back in, we need to always round away from zero here. */
1829 if (cmp_significand_0 (&r
))
1831 round_up
= digit
> 0;
1837 /* Round to nearest. If R is nonzero there are additional
1838 nonzero digits to be extracted. */
1839 if (cmp_significand_0 (&r
))
1841 /* Round to even. */
1842 else if ((p
[-1] - '0') & 1)
1846 round_up
= digit
> 5;
1863 /* Carry out of the first digit. This means we had all 9's and
1864 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1872 /* Insert the decimal point. */
1873 first
[0] = first
[1];
1876 /* If requested, drop trailing zeros. Never crop past "1.0". */
1877 if (crop_trailing_zeros
)
1878 while (last
> first
+ 3 && last
[-1] == '0')
1881 /* Append the exponent. */
1882 sprintf (last
, "e%+d", dec_exp
);
1884 /* Verify that we can read the original value back in. */
1885 if (flag_checking
&& mode
!= VOIDmode
)
1887 real_from_string (&r
, str
);
1888 real_convert (&r
, mode
, &r
);
1889 gcc_assert (real_identical (&r
, r_orig
));
1893 /* Likewise, except always uses round-to-nearest. */
1896 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1897 size_t digits
, int crop_trailing_zeros
)
1899 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1900 digits
, crop_trailing_zeros
, VOIDmode
);
1904 debug (const REAL_VALUE_TYPE
&r
)
1907 real_to_hexadecimal (s
, &r
, sizeof (s
), 0, 1);
1908 fprintf (stderr
, "%s\n", s
);
1911 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1912 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1913 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1914 strip trailing zeros. */
1917 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1918 size_t digits
, int crop_trailing_zeros
)
1920 int i
, j
, exp
= REAL_EXP (r
);
1933 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1936 /* ??? Print the significand as well, if not canonical? */
1937 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1938 (r
->signalling
? 'S' : 'Q'));
1946 /* Hexadecimal format for decimal floats is not interesting. */
1947 strcpy (str
, "N/A");
1952 digits
= SIGNIFICAND_BITS
/ 4;
1954 /* Bound the number of digits printed by the size of the output buffer. */
1956 sprintf (exp_buf
, "p%+d", exp
);
1957 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1958 gcc_assert (max_digits
<= buf_size
);
1959 if (digits
> max_digits
)
1960 digits
= max_digits
;
1971 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1972 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1974 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1980 if (crop_trailing_zeros
)
1981 while (p
> first
+ 1 && p
[-1] == '0')
1984 sprintf (p
, "p%+d", exp
);
1987 /* Initialize R from a decimal or hexadecimal string. The string is
1988 assumed to have been syntax checked already. Return -1 if the
1989 value underflows, +1 if overflows, and 0 otherwise. */
1992 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
2004 else if (*str
== '+')
2007 if (startswith (str
, "QNaN"))
2009 get_canonical_qnan (r
, sign
);
2012 else if (startswith (str
, "SNaN"))
2014 get_canonical_snan (r
, sign
);
2017 else if (startswith (str
, "Inf"))
2023 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
2025 /* Hexadecimal floating point. */
2026 int pos
= SIGNIFICAND_BITS
- 4, d
;
2034 d
= hex_value (*str
);
2039 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
2040 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
2044 /* Ensure correct rounding by setting last bit if there is
2045 a subsequent nonzero digit. */
2053 if (pos
== SIGNIFICAND_BITS
- 4)
2060 d
= hex_value (*str
);
2065 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
2066 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
2070 /* Ensure correct rounding by setting last bit if there is
2071 a subsequent nonzero digit. */
2077 /* If the mantissa is zero, ignore the exponent. */
2078 if (!cmp_significand_0 (r
))
2081 if (*str
== 'p' || *str
== 'P')
2083 bool exp_neg
= false;
2091 else if (*str
== '+')
2095 while (ISDIGIT (*str
))
2101 /* Overflowed the exponent. */
2116 SET_REAL_EXP (r
, exp
);
2122 /* Decimal floating point. */
2123 const char *cstr
= str
;
2127 while (*cstr
== '0')
2132 while (*cstr
== '0')
2136 /* If the mantissa is zero, ignore the exponent. */
2137 if (!ISDIGIT (*cstr
))
2140 /* Nonzero value, possibly overflowing or underflowing. */
2141 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2142 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, MPFR_RNDZ
);
2143 /* The result should never be a NaN, and because the rounding is
2144 toward zero should never be an infinity. */
2145 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2146 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2151 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2158 real_from_mpfr (r
, m
, NULL_TREE
, MPFR_RNDZ
);
2159 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2160 because the hex digits used in real_from_mpfr did not
2161 start with a digit 8 to f, but the exponent bounds above
2162 should have avoided underflow or overflow. */
2163 gcc_assert (r
->cl
== rvc_normal
);
2164 /* Set a sticky bit if mpfr_strtofr was inexact. */
2165 r
->sig
[0] |= inexact
;
2186 /* Legacy. Similar, but return the result directly. */
2189 real_from_string2 (const char *s
, format_helper fmt
)
2193 real_from_string (&r
, s
);
2195 real_convert (&r
, fmt
, &r
);
2200 /* Initialize R from string S and desired format FMT. */
2203 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, format_helper fmt
)
2205 if (fmt
.decimal_p ())
2206 decimal_real_from_string (r
, s
);
2208 real_from_string (r
, s
);
2211 real_convert (r
, fmt
, r
);
2214 /* Initialize R from the wide_int VAL_IN. Round it to format FMT if
2218 real_from_integer (REAL_VALUE_TYPE
*r
, format_helper fmt
,
2219 const wide_int_ref
&val_in
, signop sgn
)
2225 unsigned int len
= val_in
.get_precision ();
2227 int maxbitlen
= MAX_BITSIZE_MODE_ANY_INT
+ HOST_BITS_PER_WIDE_INT
;
2228 const unsigned int realmax
= (SIGNIFICAND_BITS
/ HOST_BITS_PER_WIDE_INT
2229 * HOST_BITS_PER_WIDE_INT
);
2231 memset (r
, 0, sizeof (*r
));
2233 r
->sign
= wi::neg_p (val_in
, sgn
);
2235 /* We have to ensure we can negate the largest negative number. */
2236 wide_int val
= wide_int::from (val_in
, maxbitlen
, sgn
);
2241 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2242 won't work with precisions that are not a multiple of
2243 HOST_BITS_PER_WIDE_INT. */
2244 len
+= HOST_BITS_PER_WIDE_INT
- 1;
2246 /* Ensure we can represent the largest negative number. */
2249 len
= len
/HOST_BITS_PER_WIDE_INT
* HOST_BITS_PER_WIDE_INT
;
2251 /* Cap the size to the size allowed by real.h. */
2254 HOST_WIDE_INT cnt_l_z
;
2255 cnt_l_z
= wi::clz (val
);
2257 if (maxbitlen
- cnt_l_z
> realmax
)
2259 e
= maxbitlen
- cnt_l_z
- realmax
;
2261 /* This value is too large, we must shift it right to
2262 preserve all the bits we can, and then bump the
2263 exponent up by that amount. */
2264 val
= wi::lrshift (val
, e
);
2269 /* Clear out top bits so elt will work with precisions that aren't
2270 a multiple of HOST_BITS_PER_WIDE_INT. */
2271 val
= wide_int::from (val
, len
, sgn
);
2272 len
= len
/ HOST_BITS_PER_WIDE_INT
;
2274 SET_REAL_EXP (r
, len
* HOST_BITS_PER_WIDE_INT
+ e
);
2277 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2278 for (i
= len
- 1; i
>= 0; i
--)
2280 r
->sig
[j
--] = val
.elt (i
);
2286 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2287 for (i
= len
- 1; i
>= 0; i
--)
2289 HOST_WIDE_INT e
= val
.elt (i
);
2290 r
->sig
[j
--] = e
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2302 if (fmt
.decimal_p ())
2303 decimal_from_integer (r
);
2305 real_convert (r
, fmt
, r
);
2308 /* Render R, an integral value, as a floating point constant with no
2309 specified exponent. */
2312 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2315 int dec_exp
, digit
, digits
;
2316 REAL_VALUE_TYPE r
, pten
;
2322 if (r
.cl
== rvc_zero
)
2331 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2332 digits
= dec_exp
+ 1;
2333 gcc_assert ((digits
+ 2) < (int)buf_size
);
2335 pten
= *real_digit (1);
2336 times_pten (&pten
, dec_exp
);
2342 digit
= rtd_divmod (&r
, &pten
);
2343 gcc_assert (digit
>= 0 && digit
<= 9);
2345 while (--digits
> 0)
2348 digit
= rtd_divmod (&r
, &pten
);
2355 /* Convert a real with an integral value to decimal float. */
2358 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2362 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2363 decimal_real_from_string (r
, str
);
2366 /* Returns 10**2**N. */
2368 static const REAL_VALUE_TYPE
*
2371 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2373 gcc_assert (n
>= 0);
2374 gcc_assert (n
< EXP_BITS
);
2376 if (tens
[n
].cl
== rvc_zero
)
2378 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2380 HOST_WIDE_INT t
= 10;
2383 for (i
= 0; i
< n
; ++i
)
2386 real_from_integer (&tens
[n
], VOIDmode
, t
, UNSIGNED
);
2390 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2391 do_multiply (&tens
[n
], t
, t
);
2398 /* Returns 10**(-2**N). */
2400 static const REAL_VALUE_TYPE
*
2401 ten_to_mptwo (int n
)
2403 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2405 gcc_assert (n
>= 0);
2406 gcc_assert (n
< EXP_BITS
);
2408 if (tens
[n
].cl
== rvc_zero
)
2409 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2416 static const REAL_VALUE_TYPE
*
2419 static REAL_VALUE_TYPE num
[10];
2421 gcc_assert (n
>= 0);
2422 gcc_assert (n
<= 9);
2424 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2425 real_from_integer (&num
[n
], VOIDmode
, n
, UNSIGNED
);
2430 /* Multiply R by 10**EXP. */
2433 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2435 REAL_VALUE_TYPE pten
, *rr
;
2436 bool negative
= (exp
< 0);
2442 pten
= *real_digit (1);
2448 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2450 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2453 do_divide (r
, r
, &pten
);
2456 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2458 const REAL_VALUE_TYPE
*
2461 static REAL_VALUE_TYPE value
;
2463 /* Initialize mathematical constants for constant folding builtins.
2464 These constants need to be given to at least 160 bits precision. */
2465 if (value
.cl
== rvc_zero
)
2468 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2469 mpfr_set_ui (m
, 1, MPFR_RNDN
);
2470 mpfr_exp (m
, m
, MPFR_RNDN
);
2471 real_from_mpfr (&value
, m
, NULL_TREE
, MPFR_RNDN
);
2478 /* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2480 #define CACHED_FRACTION(NAME, N) \
2481 const REAL_VALUE_TYPE * \
2484 static REAL_VALUE_TYPE value; \
2486 /* Initialize mathematical constants for constant folding builtins. \
2487 These constants need to be given to at least 160 bits \
2489 if (value.cl == rvc_zero) \
2490 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2494 CACHED_FRACTION (dconst_third_ptr
, 3)
2495 CACHED_FRACTION (dconst_quarter_ptr
, 4)
2496 CACHED_FRACTION (dconst_sixth_ptr
, 6)
2497 CACHED_FRACTION (dconst_ninth_ptr
, 9)
2499 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2501 const REAL_VALUE_TYPE
*
2502 dconst_sqrt2_ptr (void)
2504 static REAL_VALUE_TYPE value
;
2506 /* Initialize mathematical constants for constant folding builtins.
2507 These constants need to be given to at least 160 bits precision. */
2508 if (value
.cl
== rvc_zero
)
2511 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2512 mpfr_sqrt_ui (m
, 2, MPFR_RNDN
);
2513 real_from_mpfr (&value
, m
, NULL_TREE
, MPFR_RNDN
);
2519 /* Fills R with Inf with SIGN. */
2522 real_inf (REAL_VALUE_TYPE
*r
, bool sign
)
2527 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2528 we force a QNaN, else we force an SNaN. The string, if not empty,
2529 is parsed as a number and placed in the significand. Return true
2530 if the string was successfully parsed. */
2533 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2539 get_canonical_qnan (r
, 0);
2541 get_canonical_snan (r
, 0);
2547 memset (r
, 0, sizeof (*r
));
2550 /* Parse akin to strtol into the significand of R. */
2552 while (ISSPACE (*str
))
2556 else if (*str
== '+')
2561 if (*str
== 'x' || *str
== 'X')
2570 while ((d
= hex_value (*str
)) < base
)
2577 lshift_significand (r
, r
, 3);
2580 lshift_significand (r
, r
, 4);
2583 lshift_significand_1 (&u
, r
);
2584 lshift_significand (r
, r
, 3);
2585 add_significands (r
, r
, &u
);
2593 add_significands (r
, r
, &u
);
2598 /* Must have consumed the entire string for success. */
2602 /* Shift the significand into place such that the bits
2603 are in the most significant bits for the format. */
2604 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2606 /* Our MSB is always unset for NaNs. */
2607 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2609 /* Force quiet or signaling NaN. */
2610 r
->signalling
= !quiet
;
2616 /* Fills R with the largest finite value representable in mode MODE.
2617 If SIGN is nonzero, R is set to the most negative finite value. */
2620 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
2622 const struct real_format
*fmt
;
2625 fmt
= REAL_MODE_FORMAT (mode
);
2627 memset (r
, 0, sizeof (*r
));
2630 decimal_real_maxval (r
, sign
, mode
);
2635 SET_REAL_EXP (r
, fmt
->emax
);
2637 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2638 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2639 clear_significand_below (r
, np2
);
2641 if (fmt
->pnan
< fmt
->p
)
2642 /* This is an IBM extended double format made up of two IEEE
2643 doubles. The value of the long double is the sum of the
2644 values of the two parts. The most significant part is
2645 required to be the value of the long double rounded to the
2646 nearest double. Rounding means we need a slightly smaller
2647 value for LDBL_MAX. */
2648 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2652 /* Fills R with 2**N. */
2655 real_2expN (REAL_VALUE_TYPE
*r
, int n
, format_helper fmt
)
2657 memset (r
, 0, sizeof (*r
));
2662 else if (n
< -MAX_EXP
)
2667 SET_REAL_EXP (r
, n
);
2668 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2670 if (fmt
.decimal_p ())
2671 decimal_real_convert (r
, fmt
, r
);
2676 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2680 bool round_up
= false;
2686 decimal_round_for_format (fmt
, r
);
2689 /* FIXME. We can come here via fp_easy_constant
2690 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2691 investigated whether this convert needs to be here, or
2692 something else is missing. */
2693 decimal_real_convert (r
, REAL_MODE_FORMAT (DFmode
), r
);
2697 emin2m1
= fmt
->emin
- 1;
2700 np2
= SIGNIFICAND_BITS
- p2
;
2704 get_zero (r
, r
->sign
);
2707 if (!fmt
->has_signed_zero
)
2712 get_inf (r
, r
->sign
);
2717 clear_significand_below (r
, np2
);
2727 /* Check the range of the exponent. If we're out of range,
2728 either underflow or overflow. */
2729 if (REAL_EXP (r
) > emax2
)
2731 else if (REAL_EXP (r
) <= emin2m1
)
2735 if (!fmt
->has_denorm
)
2737 /* Don't underflow completely until we've had a chance to round. */
2738 if (REAL_EXP (r
) < emin2m1
)
2743 diff
= emin2m1
- REAL_EXP (r
) + 1;
2747 /* De-normalize the significand. */
2748 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2749 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2753 if (!fmt
->round_towards_zero
)
2755 /* There are P2 true significand bits, followed by one guard bit,
2756 followed by one sticky bit, followed by stuff. Fold nonzero
2757 stuff into the sticky bit. */
2758 unsigned long sticky
;
2762 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2763 sticky
|= r
->sig
[i
];
2765 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2767 guard
= test_significand_bit (r
, np2
- 1);
2768 lsb
= test_significand_bit (r
, np2
);
2770 /* Round to even. */
2771 round_up
= guard
&& (sticky
|| lsb
);
2778 set_significand_bit (&u
, np2
);
2780 if (add_significands (r
, r
, &u
))
2782 /* Overflow. Means the significand had been all ones, and
2783 is now all zeros. Need to increase the exponent, and
2784 possibly re-normalize it. */
2785 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2786 if (REAL_EXP (r
) > emax2
)
2788 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2792 /* Catch underflow that we deferred until after rounding. */
2793 if (REAL_EXP (r
) <= emin2m1
)
2796 /* Clear out trailing garbage. */
2797 clear_significand_below (r
, np2
);
2800 /* Extend or truncate to a new format. */
2803 real_convert (REAL_VALUE_TYPE
*r
, format_helper fmt
,
2804 const REAL_VALUE_TYPE
*a
)
2808 if (a
->decimal
|| fmt
->b
== 10)
2809 decimal_real_convert (r
, fmt
, a
);
2811 round_for_format (fmt
, r
);
2813 /* Make resulting NaN value to be qNaN. The caller has the
2814 responsibility to avoid the operation if flag_signaling_nans
2816 if (r
->cl
== rvc_nan
)
2819 /* round_for_format de-normalizes denormals. Undo just that part. */
2820 if (r
->cl
== rvc_normal
)
2824 /* Legacy. Likewise, except return the struct directly. */
2827 real_value_truncate (format_helper fmt
, REAL_VALUE_TYPE a
)
2830 real_convert (&r
, fmt
, &a
);
2834 /* Return true if truncating to FMT is exact. */
2837 exact_real_truncate (format_helper fmt
, const REAL_VALUE_TYPE
*a
)
2842 /* Don't allow conversion to denormals. */
2843 emin2m1
= fmt
->emin
- 1;
2844 if (REAL_EXP (a
) <= emin2m1
)
2847 /* After conversion to the new format, the value must be identical. */
2848 real_convert (&t
, fmt
, a
);
2849 return real_identical (&t
, a
);
2852 /* Write R to the given target format. Place the words of the result
2853 in target word order in BUF. There are always 32 bits in each
2854 long, no matter the size of the host long.
2856 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2859 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2866 round_for_format (fmt
, &r
);
2870 (*fmt
->encode
) (fmt
, buf
, &r
);
2875 /* Read R from the given target format. Read the words of the result
2876 in target word order in BUF. There are always 32 bits in each
2877 long, no matter the size of the host long. */
2880 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, format_helper fmt
)
2882 (*fmt
->decode
) (fmt
, r
, buf
);
2885 /* Return the number of bits of the largest binary value that the
2886 significand of FMT will hold. */
2887 /* ??? Legacy. Should get access to real_format directly. */
2890 significand_size (format_helper fmt
)
2897 /* Return the size in bits of the largest binary value that can be
2898 held by the decimal coefficient for this format. This is one more
2899 than the number of bits required to hold the largest coefficient
2901 double log2_10
= 3.3219281;
2902 return fmt
->p
* log2_10
;
2907 /* Return a hash value for the given real value. */
2908 /* ??? The "unsigned int" return value is intended to be hashval_t,
2909 but I didn't want to pull hashtab.h into real.h. */
2912 real_hash (const REAL_VALUE_TYPE
*r
)
2917 h
= r
->cl
| (r
->sign
<< 2);
2925 h
|= (unsigned int)REAL_EXP (r
) << 3;
2930 h
^= (unsigned int)-1;
2939 if (sizeof (unsigned long) > sizeof (unsigned int))
2940 for (i
= 0; i
< SIGSZ
; ++i
)
2942 unsigned long s
= r
->sig
[i
];
2943 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2946 for (i
= 0; i
< SIGSZ
; ++i
)
2952 /* IEEE single-precision format. */
2954 static void encode_ieee_single (const struct real_format
*fmt
,
2955 long *, const REAL_VALUE_TYPE
*);
2956 static void decode_ieee_single (const struct real_format
*,
2957 REAL_VALUE_TYPE
*, const long *);
2960 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2961 const REAL_VALUE_TYPE
*r
)
2963 unsigned long image
, sig
, exp
;
2964 unsigned long sign
= r
->sign
;
2965 bool denormal
= real_isdenormal (r
);
2968 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2979 image
|= 0x7fffffff;
2986 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2987 if (r
->signalling
== fmt
->qnan_msb_set
)
2998 image
|= 0x7fffffff;
3002 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3003 whereas the intermediate representation is 0.F x 2**exp.
3004 Which means we're off by one. */
3008 exp
= REAL_EXP (r
) + 127 - 1;
3021 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3024 unsigned long image
= buf
[0] & 0xffffffff;
3025 bool sign
= (image
>> 31) & 1;
3026 int exp
= (image
>> 23) & 0xff;
3028 memset (r
, 0, sizeof (*r
));
3029 image
<<= HOST_BITS_PER_LONG
- 24;
3034 if (image
&& fmt
->has_denorm
)
3038 SET_REAL_EXP (r
, -126);
3039 r
->sig
[SIGSZ
-1] = image
<< 1;
3042 else if (fmt
->has_signed_zero
)
3045 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
3051 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
3052 ^ fmt
->qnan_msb_set
);
3053 r
->sig
[SIGSZ
-1] = image
;
3065 SET_REAL_EXP (r
, exp
- 127 + 1);
3066 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
3070 const struct real_format ieee_single_format
=
3093 const struct real_format mips_single_format
=
3116 const struct real_format motorola_single_format
=
3139 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3140 single precision with the following differences:
3141 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3143 - NaNs are not supported.
3144 - The range of non-zero numbers in binary is
3145 (001)[1.]000...000 to (255)[1.]111...111.
3146 - Denormals can be represented, but are treated as +0.0 when
3147 used as an operand and are never generated as a result.
3148 - -0.0 can be represented, but a zero result is always +0.0.
3149 - the only supported rounding mode is trunction (towards zero). */
3150 const struct real_format spu_single_format
=
3173 /* IEEE double-precision format. */
3175 static void encode_ieee_double (const struct real_format
*fmt
,
3176 long *, const REAL_VALUE_TYPE
*);
3177 static void decode_ieee_double (const struct real_format
*,
3178 REAL_VALUE_TYPE
*, const long *);
3181 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3182 const REAL_VALUE_TYPE
*r
)
3184 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3185 unsigned long sign
= r
->sign
;
3186 bool denormal
= real_isdenormal (r
);
3188 image_hi
= sign
<< 31;
3191 if (HOST_BITS_PER_LONG
== 64)
3193 sig_hi
= r
->sig
[SIGSZ
-1];
3194 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3195 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3199 sig_hi
= r
->sig
[SIGSZ
-1];
3200 sig_lo
= r
->sig
[SIGSZ
-2];
3201 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3202 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3212 image_hi
|= 2047 << 20;
3215 image_hi
|= 0x7fffffff;
3216 image_lo
= 0xffffffff;
3225 if (fmt
->canonical_nan_lsbs_set
)
3227 sig_hi
= (1 << 19) - 1;
3228 sig_lo
= 0xffffffff;
3236 if (r
->signalling
== fmt
->qnan_msb_set
)
3237 sig_hi
&= ~(1 << 19);
3240 if (sig_hi
== 0 && sig_lo
== 0)
3243 image_hi
|= 2047 << 20;
3249 image_hi
|= 0x7fffffff;
3250 image_lo
= 0xffffffff;
3255 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3256 whereas the intermediate representation is 0.F x 2**exp.
3257 Which means we're off by one. */
3261 exp
= REAL_EXP (r
) + 1023 - 1;
3262 image_hi
|= exp
<< 20;
3271 if (FLOAT_WORDS_BIG_ENDIAN
)
3272 buf
[0] = image_hi
, buf
[1] = image_lo
;
3274 buf
[0] = image_lo
, buf
[1] = image_hi
;
3278 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3281 unsigned long image_hi
, image_lo
;
3285 if (FLOAT_WORDS_BIG_ENDIAN
)
3286 image_hi
= buf
[0], image_lo
= buf
[1];
3288 image_lo
= buf
[0], image_hi
= buf
[1];
3289 image_lo
&= 0xffffffff;
3290 image_hi
&= 0xffffffff;
3292 sign
= (image_hi
>> 31) & 1;
3293 exp
= (image_hi
>> 20) & 0x7ff;
3295 memset (r
, 0, sizeof (*r
));
3297 image_hi
<<= 32 - 21;
3298 image_hi
|= image_lo
>> 21;
3299 image_hi
&= 0x7fffffff;
3300 image_lo
<<= 32 - 21;
3304 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3308 SET_REAL_EXP (r
, -1022);
3309 if (HOST_BITS_PER_LONG
== 32)
3311 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3313 r
->sig
[SIGSZ
-1] = image_hi
;
3314 r
->sig
[SIGSZ
-2] = image_lo
;
3318 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3319 r
->sig
[SIGSZ
-1] = image_hi
;
3323 else if (fmt
->has_signed_zero
)
3326 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3328 if (image_hi
|| image_lo
)
3332 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3333 if (HOST_BITS_PER_LONG
== 32)
3335 r
->sig
[SIGSZ
-1] = image_hi
;
3336 r
->sig
[SIGSZ
-2] = image_lo
;
3339 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3351 SET_REAL_EXP (r
, exp
- 1023 + 1);
3352 if (HOST_BITS_PER_LONG
== 32)
3354 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3355 r
->sig
[SIGSZ
-2] = image_lo
;
3358 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3362 const struct real_format ieee_double_format
=
3385 const struct real_format mips_double_format
=
3408 const struct real_format motorola_double_format
=
3431 /* IEEE extended real format. This comes in three flavors: Intel's as
3432 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3433 12- and 16-byte images may be big- or little endian; Motorola's is
3434 always big endian. */
3436 /* Helper subroutine which converts from the internal format to the
3437 12-byte little-endian Intel format. Functions below adjust this
3438 for the other possible formats. */
3440 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3441 const REAL_VALUE_TYPE
*r
)
3443 unsigned long image_hi
, sig_hi
, sig_lo
;
3444 bool denormal
= real_isdenormal (r
);
3446 image_hi
= r
->sign
<< 15;
3447 sig_hi
= sig_lo
= 0;
3459 /* Intel requires the explicit integer bit to be set, otherwise
3460 it considers the value a "pseudo-infinity". Motorola docs
3461 say it doesn't care. */
3462 sig_hi
= 0x80000000;
3467 sig_lo
= sig_hi
= 0xffffffff;
3477 if (fmt
->canonical_nan_lsbs_set
)
3479 sig_hi
= (1 << 30) - 1;
3480 sig_lo
= 0xffffffff;
3483 else if (HOST_BITS_PER_LONG
== 32)
3485 sig_hi
= r
->sig
[SIGSZ
-1];
3486 sig_lo
= r
->sig
[SIGSZ
-2];
3490 sig_lo
= r
->sig
[SIGSZ
-1];
3491 sig_hi
= sig_lo
>> 31 >> 1;
3492 sig_lo
&= 0xffffffff;
3494 if (r
->signalling
== fmt
->qnan_msb_set
)
3495 sig_hi
&= ~(1 << 30);
3498 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3501 /* Intel requires the explicit integer bit to be set, otherwise
3502 it considers the value a "pseudo-nan". Motorola docs say it
3504 sig_hi
|= 0x80000000;
3509 sig_lo
= sig_hi
= 0xffffffff;
3515 int exp
= REAL_EXP (r
);
3517 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3518 whereas the intermediate representation is 0.F x 2**exp.
3519 Which means we're off by one.
3521 Except for Motorola, which consider exp=0 and explicit
3522 integer bit set to continue to be normalized. In theory
3523 this discrepancy has been taken care of by the difference
3524 in fmt->emin in round_for_format. */
3531 gcc_assert (exp
>= 0);
3535 if (HOST_BITS_PER_LONG
== 32)
3537 sig_hi
= r
->sig
[SIGSZ
-1];
3538 sig_lo
= r
->sig
[SIGSZ
-2];
3542 sig_lo
= r
->sig
[SIGSZ
-1];
3543 sig_hi
= sig_lo
>> 31 >> 1;
3544 sig_lo
&= 0xffffffff;
3553 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3556 /* Convert from the internal format to the 12-byte Motorola format
3557 for an IEEE extended real. */
3559 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3560 const REAL_VALUE_TYPE
*r
)
3563 encode_ieee_extended (fmt
, intermed
, r
);
3565 if (r
->cl
== rvc_inf
)
3566 /* For infinity clear the explicit integer bit again, so that the
3567 format matches the canonical infinity generated by the FPU. */
3570 /* Motorola chips are assumed always to be big-endian. Also, the
3571 padding in a Motorola extended real goes between the exponent and
3572 the mantissa. At this point the mantissa is entirely within
3573 elements 0 and 1 of intermed, and the exponent entirely within
3574 element 2, so all we have to do is swap the order around, and
3575 shift element 2 left 16 bits. */
3576 buf
[0] = intermed
[2] << 16;
3577 buf
[1] = intermed
[1];
3578 buf
[2] = intermed
[0];
3581 /* Convert from the internal format to the 12-byte Intel format for
3582 an IEEE extended real. */
3584 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3585 const REAL_VALUE_TYPE
*r
)
3587 if (FLOAT_WORDS_BIG_ENDIAN
)
3589 /* All the padding in an Intel-format extended real goes at the high
3590 end, which in this case is after the mantissa, not the exponent.
3591 Therefore we must shift everything down 16 bits. */
3593 encode_ieee_extended (fmt
, intermed
, r
);
3594 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3595 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3596 buf
[2] = (intermed
[0] << 16);
3599 /* encode_ieee_extended produces what we want directly. */
3600 encode_ieee_extended (fmt
, buf
, r
);
3603 /* Convert from the internal format to the 16-byte Intel format for
3604 an IEEE extended real. */
3606 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3607 const REAL_VALUE_TYPE
*r
)
3609 /* All the padding in an Intel-format extended real goes at the high end. */
3610 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3614 /* As above, we have a helper function which converts from 12-byte
3615 little-endian Intel format to internal format. Functions below
3616 adjust for the other possible formats. */
3618 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3621 unsigned long image_hi
, sig_hi
, sig_lo
;
3625 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3626 sig_lo
&= 0xffffffff;
3627 sig_hi
&= 0xffffffff;
3628 image_hi
&= 0xffffffff;
3630 sign
= (image_hi
>> 15) & 1;
3631 exp
= image_hi
& 0x7fff;
3633 memset (r
, 0, sizeof (*r
));
3637 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3642 /* When the IEEE format contains a hidden bit, we know that
3643 it's zero at this point, and so shift up the significand
3644 and decrease the exponent to match. In this case, Motorola
3645 defines the explicit integer bit to be valid, so we don't
3646 know whether the msb is set or not. */
3647 SET_REAL_EXP (r
, fmt
->emin
);
3648 if (HOST_BITS_PER_LONG
== 32)
3650 r
->sig
[SIGSZ
-1] = sig_hi
;
3651 r
->sig
[SIGSZ
-2] = sig_lo
;
3654 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3658 else if (fmt
->has_signed_zero
)
3661 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3663 /* See above re "pseudo-infinities" and "pseudo-nans".
3664 Short summary is that the MSB will likely always be
3665 set, and that we don't care about it. */
3666 sig_hi
&= 0x7fffffff;
3668 if (sig_hi
|| sig_lo
)
3672 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3673 if (HOST_BITS_PER_LONG
== 32)
3675 r
->sig
[SIGSZ
-1] = sig_hi
;
3676 r
->sig
[SIGSZ
-2] = sig_lo
;
3679 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3691 SET_REAL_EXP (r
, exp
- 16383 + 1);
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
;
3702 /* Convert from the internal format to the 12-byte Motorola format
3703 for an IEEE extended real. */
3705 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3710 /* Motorola chips are assumed always to be big-endian. Also, the
3711 padding in a Motorola extended real goes between the exponent and
3712 the mantissa; remove it. */
3713 intermed
[0] = buf
[2];
3714 intermed
[1] = buf
[1];
3715 intermed
[2] = (unsigned long)buf
[0] >> 16;
3717 decode_ieee_extended (fmt
, r
, intermed
);
3720 /* Convert from the internal format to the 12-byte Intel format for
3721 an IEEE extended real. */
3723 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3726 if (FLOAT_WORDS_BIG_ENDIAN
)
3728 /* All the padding in an Intel-format extended real goes at the high
3729 end, which in this case is after the mantissa, not the exponent.
3730 Therefore we must shift everything up 16 bits. */
3733 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3734 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3735 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3737 decode_ieee_extended (fmt
, r
, intermed
);
3740 /* decode_ieee_extended produces what we want directly. */
3741 decode_ieee_extended (fmt
, r
, buf
);
3744 /* Convert from the internal format to the 16-byte Intel format for
3745 an IEEE extended real. */
3747 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3750 /* All the padding in an Intel-format extended real goes at the high end. */
3751 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3754 const struct real_format ieee_extended_motorola_format
=
3756 encode_ieee_extended_motorola
,
3757 decode_ieee_extended_motorola
,
3774 "ieee_extended_motorola"
3777 const struct real_format ieee_extended_intel_96_format
=
3779 encode_ieee_extended_intel_96
,
3780 decode_ieee_extended_intel_96
,
3797 "ieee_extended_intel_96"
3800 const struct real_format ieee_extended_intel_128_format
=
3802 encode_ieee_extended_intel_128
,
3803 decode_ieee_extended_intel_128
,
3820 "ieee_extended_intel_128"
3823 /* The following caters to i386 systems that set the rounding precision
3824 to 53 bits instead of 64, e.g. FreeBSD. */
3825 const struct real_format ieee_extended_intel_96_round_53_format
=
3827 encode_ieee_extended_intel_96
,
3828 decode_ieee_extended_intel_96
,
3845 "ieee_extended_intel_96_round_53"
3848 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3849 numbers whose sum is equal to the extended precision value. The number
3850 with greater magnitude is first. This format has the same magnitude
3851 range as an IEEE double precision value, but effectively 106 bits of
3852 significand precision. Infinity and NaN are represented by their IEEE
3853 double precision value stored in the first number, the second number is
3854 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3856 static void encode_ibm_extended (const struct real_format
*fmt
,
3857 long *, const REAL_VALUE_TYPE
*);
3858 static void decode_ibm_extended (const struct real_format
*,
3859 REAL_VALUE_TYPE
*, const long *);
3862 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3863 const REAL_VALUE_TYPE
*r
)
3865 REAL_VALUE_TYPE u
, normr
, v
;
3866 const struct real_format
*base_fmt
;
3868 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3870 /* Renormalize R before doing any arithmetic on it. */
3872 if (normr
.cl
== rvc_normal
)
3875 /* u = IEEE double precision portion of significand. */
3877 round_for_format (base_fmt
, &u
);
3878 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3880 if (u
.cl
== rvc_normal
)
3882 do_add (&v
, &normr
, &u
, 1);
3883 /* Call round_for_format since we might need to denormalize. */
3884 round_for_format (base_fmt
, &v
);
3885 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3889 /* Inf, NaN, 0 are all representable as doubles, so the
3890 least-significant part can be 0.0. */
3897 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3900 REAL_VALUE_TYPE u
, v
;
3901 const struct real_format
*base_fmt
;
3903 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3904 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3906 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3908 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3909 do_add (r
, &u
, &v
, 0);
3915 const struct real_format ibm_extended_format
=
3917 encode_ibm_extended
,
3918 decode_ibm_extended
,
3938 const struct real_format mips_extended_format
=
3940 encode_ibm_extended
,
3941 decode_ibm_extended
,
3962 /* IEEE quad precision format. */
3964 static void encode_ieee_quad (const struct real_format
*fmt
,
3965 long *, const REAL_VALUE_TYPE
*);
3966 static void decode_ieee_quad (const struct real_format
*,
3967 REAL_VALUE_TYPE
*, const long *);
3970 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3971 const REAL_VALUE_TYPE
*r
)
3973 unsigned long image3
, image2
, image1
, image0
, exp
;
3974 unsigned long sign
= r
->sign
;
3975 bool denormal
= real_isdenormal (r
);
3978 image3
= sign
<< 31;
3983 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3992 image3
|= 32767 << 16;
3995 image3
|= 0x7fffffff;
3996 image2
= 0xffffffff;
3997 image1
= 0xffffffff;
3998 image0
= 0xffffffff;
4005 image3
|= 32767 << 16;
4009 if (fmt
->canonical_nan_lsbs_set
)
4012 image2
= image1
= image0
= 0xffffffff;
4015 else if (HOST_BITS_PER_LONG
== 32)
4020 image3
|= u
.sig
[3] & 0xffff;
4025 image1
= image0
>> 31 >> 1;
4027 image3
|= (image2
>> 31 >> 1) & 0xffff;
4028 image0
&= 0xffffffff;
4029 image2
&= 0xffffffff;
4031 if (r
->signalling
== fmt
->qnan_msb_set
)
4035 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
4040 image3
|= 0x7fffffff;
4041 image2
= 0xffffffff;
4042 image1
= 0xffffffff;
4043 image0
= 0xffffffff;
4048 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4049 whereas the intermediate representation is 0.F x 2**exp.
4050 Which means we're off by one. */
4054 exp
= REAL_EXP (r
) + 16383 - 1;
4055 image3
|= exp
<< 16;
4057 if (HOST_BITS_PER_LONG
== 32)
4062 image3
|= u
.sig
[3] & 0xffff;
4067 image1
= image0
>> 31 >> 1;
4069 image3
|= (image2
>> 31 >> 1) & 0xffff;
4070 image0
&= 0xffffffff;
4071 image2
&= 0xffffffff;
4079 if (FLOAT_WORDS_BIG_ENDIAN
)
4096 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4099 unsigned long image3
, image2
, image1
, image0
;
4103 if (FLOAT_WORDS_BIG_ENDIAN
)
4117 image0
&= 0xffffffff;
4118 image1
&= 0xffffffff;
4119 image2
&= 0xffffffff;
4121 sign
= (image3
>> 31) & 1;
4122 exp
= (image3
>> 16) & 0x7fff;
4125 memset (r
, 0, sizeof (*r
));
4129 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4134 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4135 if (HOST_BITS_PER_LONG
== 32)
4144 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4145 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4150 else if (fmt
->has_signed_zero
)
4153 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4155 if (image3
| image2
| image1
| image0
)
4159 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4161 if (HOST_BITS_PER_LONG
== 32)
4170 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4171 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4173 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4185 SET_REAL_EXP (r
, exp
- 16383 + 1);
4187 if (HOST_BITS_PER_LONG
== 32)
4196 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4197 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4199 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4200 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4204 const struct real_format ieee_quad_format
=
4227 const struct real_format mips_quad_format
=
4250 /* Descriptions of VAX floating point formats can be found beginning at
4252 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4254 The thing to remember is that they're almost IEEE, except for word
4255 order, exponent bias, and the lack of infinities, nans, and denormals.
4257 We don't implement the H_floating format here, simply because neither
4258 the VAX or Alpha ports use it. */
4260 static void encode_vax_f (const struct real_format
*fmt
,
4261 long *, const REAL_VALUE_TYPE
*);
4262 static void decode_vax_f (const struct real_format
*,
4263 REAL_VALUE_TYPE
*, const long *);
4264 static void encode_vax_d (const struct real_format
*fmt
,
4265 long *, const REAL_VALUE_TYPE
*);
4266 static void decode_vax_d (const struct real_format
*,
4267 REAL_VALUE_TYPE
*, const long *);
4268 static void encode_vax_g (const struct real_format
*fmt
,
4269 long *, const REAL_VALUE_TYPE
*);
4270 static void decode_vax_g (const struct real_format
*,
4271 REAL_VALUE_TYPE
*, const long *);
4274 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4275 const REAL_VALUE_TYPE
*r
)
4277 unsigned long sign
, exp
, sig
, image
;
4279 sign
= r
->sign
<< 15;
4289 image
= 0xffff7fff | sign
;
4293 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4294 exp
= REAL_EXP (r
) + 128;
4296 image
= (sig
<< 16) & 0xffff0000;
4310 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4311 REAL_VALUE_TYPE
*r
, const long *buf
)
4313 unsigned long image
= buf
[0] & 0xffffffff;
4314 int exp
= (image
>> 7) & 0xff;
4316 memset (r
, 0, sizeof (*r
));
4321 r
->sign
= (image
>> 15) & 1;
4322 SET_REAL_EXP (r
, exp
- 128);
4324 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4325 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4330 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4331 const REAL_VALUE_TYPE
*r
)
4333 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4338 image0
= image1
= 0;
4343 image0
= 0xffff7fff | sign
;
4344 image1
= 0xffffffff;
4348 /* Extract the significand into straight hi:lo. */
4349 if (HOST_BITS_PER_LONG
== 64)
4351 image0
= r
->sig
[SIGSZ
-1];
4352 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4353 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4357 image0
= r
->sig
[SIGSZ
-1];
4358 image1
= r
->sig
[SIGSZ
-2];
4359 image1
= (image0
<< 24) | (image1
>> 8);
4360 image0
= (image0
>> 8) & 0xffffff;
4363 /* Rearrange the half-words of the significand to match the
4365 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4366 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4368 /* Add the sign and exponent. */
4370 image0
|= (REAL_EXP (r
) + 128) << 7;
4377 if (FLOAT_WORDS_BIG_ENDIAN
)
4378 buf
[0] = image1
, buf
[1] = image0
;
4380 buf
[0] = image0
, buf
[1] = image1
;
4384 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4385 REAL_VALUE_TYPE
*r
, const long *buf
)
4387 unsigned long image0
, image1
;
4390 if (FLOAT_WORDS_BIG_ENDIAN
)
4391 image1
= buf
[0], image0
= buf
[1];
4393 image0
= buf
[0], image1
= buf
[1];
4394 image0
&= 0xffffffff;
4395 image1
&= 0xffffffff;
4397 exp
= (image0
>> 7) & 0xff;
4399 memset (r
, 0, sizeof (*r
));
4404 r
->sign
= (image0
>> 15) & 1;
4405 SET_REAL_EXP (r
, exp
- 128);
4407 /* Rearrange the half-words of the external format into
4408 proper ascending order. */
4409 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4410 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4412 if (HOST_BITS_PER_LONG
== 64)
4414 image0
= (image0
<< 31 << 1) | image1
;
4417 r
->sig
[SIGSZ
-1] = image0
;
4421 r
->sig
[SIGSZ
-1] = image0
;
4422 r
->sig
[SIGSZ
-2] = image1
;
4423 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4424 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4430 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4431 const REAL_VALUE_TYPE
*r
)
4433 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4438 image0
= image1
= 0;
4443 image0
= 0xffff7fff | sign
;
4444 image1
= 0xffffffff;
4448 /* Extract the significand into straight hi:lo. */
4449 if (HOST_BITS_PER_LONG
== 64)
4451 image0
= r
->sig
[SIGSZ
-1];
4452 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4453 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4457 image0
= r
->sig
[SIGSZ
-1];
4458 image1
= r
->sig
[SIGSZ
-2];
4459 image1
= (image0
<< 21) | (image1
>> 11);
4460 image0
= (image0
>> 11) & 0xfffff;
4463 /* Rearrange the half-words of the significand to match the
4465 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4466 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4468 /* Add the sign and exponent. */
4470 image0
|= (REAL_EXP (r
) + 1024) << 4;
4477 if (FLOAT_WORDS_BIG_ENDIAN
)
4478 buf
[0] = image1
, buf
[1] = image0
;
4480 buf
[0] = image0
, buf
[1] = image1
;
4484 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4485 REAL_VALUE_TYPE
*r
, const long *buf
)
4487 unsigned long image0
, image1
;
4490 if (FLOAT_WORDS_BIG_ENDIAN
)
4491 image1
= buf
[0], image0
= buf
[1];
4493 image0
= buf
[0], image1
= buf
[1];
4494 image0
&= 0xffffffff;
4495 image1
&= 0xffffffff;
4497 exp
= (image0
>> 4) & 0x7ff;
4499 memset (r
, 0, sizeof (*r
));
4504 r
->sign
= (image0
>> 15) & 1;
4505 SET_REAL_EXP (r
, exp
- 1024);
4507 /* Rearrange the half-words of the external format into
4508 proper ascending order. */
4509 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4510 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4512 if (HOST_BITS_PER_LONG
== 64)
4514 image0
= (image0
<< 31 << 1) | image1
;
4517 r
->sig
[SIGSZ
-1] = image0
;
4521 r
->sig
[SIGSZ
-1] = image0
;
4522 r
->sig
[SIGSZ
-2] = image1
;
4523 lshift_significand (r
, r
, 64 - 53);
4524 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4529 const struct real_format vax_f_format
=
4552 const struct real_format vax_d_format
=
4575 const struct real_format vax_g_format
=
4598 /* Encode real R into a single precision DFP value in BUF. */
4600 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4601 long *buf ATTRIBUTE_UNUSED
,
4602 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4604 encode_decimal32 (fmt
, buf
, r
);
4607 /* Decode a single precision DFP value in BUF into a real R. */
4609 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4610 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4611 const long *buf ATTRIBUTE_UNUSED
)
4613 decode_decimal32 (fmt
, r
, buf
);
4616 /* Encode real R into a double precision DFP value in BUF. */
4618 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4619 long *buf ATTRIBUTE_UNUSED
,
4620 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4622 encode_decimal64 (fmt
, buf
, r
);
4625 /* Decode a double precision DFP value in BUF into a real R. */
4627 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4628 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4629 const long *buf ATTRIBUTE_UNUSED
)
4631 decode_decimal64 (fmt
, r
, buf
);
4634 /* Encode real R into a quad precision DFP value in BUF. */
4636 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4637 long *buf ATTRIBUTE_UNUSED
,
4638 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4640 encode_decimal128 (fmt
, buf
, r
);
4643 /* Decode a quad precision DFP value in BUF into a real R. */
4645 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4646 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4647 const long *buf ATTRIBUTE_UNUSED
)
4649 decode_decimal128 (fmt
, r
, buf
);
4652 /* Single precision decimal floating point (IEEE 754). */
4653 const struct real_format decimal_single_format
=
4655 encode_decimal_single
,
4656 decode_decimal_single
,
4676 /* Double precision decimal floating point (IEEE 754). */
4677 const struct real_format decimal_double_format
=
4679 encode_decimal_double
,
4680 decode_decimal_double
,
4700 /* Quad precision decimal floating point (IEEE 754). */
4701 const struct real_format decimal_quad_format
=
4703 encode_decimal_quad
,
4704 decode_decimal_quad
,
4724 /* Encode half-precision floats. This routine is used both for the IEEE
4725 ARM alternative encodings. */
4727 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4728 const REAL_VALUE_TYPE
*r
)
4730 unsigned long image
, sig
, exp
;
4731 unsigned long sign
= r
->sign
;
4732 bool denormal
= real_isdenormal (r
);
4735 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4753 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4754 if (r
->signalling
== fmt
->qnan_msb_set
)
4769 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4770 whereas the intermediate representation is 0.F x 2**exp.
4771 Which means we're off by one. */
4775 exp
= REAL_EXP (r
) + 15 - 1;
4787 /* Decode half-precision floats. This routine is used both for the IEEE
4788 ARM alternative encodings. */
4790 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4793 unsigned long image
= buf
[0] & 0xffff;
4794 bool sign
= (image
>> 15) & 1;
4795 int exp
= (image
>> 10) & 0x1f;
4797 memset (r
, 0, sizeof (*r
));
4798 image
<<= HOST_BITS_PER_LONG
- 11;
4803 if (image
&& fmt
->has_denorm
)
4807 SET_REAL_EXP (r
, -14);
4808 r
->sig
[SIGSZ
-1] = image
<< 1;
4811 else if (fmt
->has_signed_zero
)
4814 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4820 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4821 ^ fmt
->qnan_msb_set
);
4822 r
->sig
[SIGSZ
-1] = image
;
4834 SET_REAL_EXP (r
, exp
- 15 + 1);
4835 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4839 /* Encode arm_bfloat types. */
4841 encode_arm_bfloat_half (const struct real_format
*fmt
, long *buf
,
4842 const REAL_VALUE_TYPE
*r
)
4844 unsigned long image
, sig
, exp
;
4845 unsigned long sign
= r
->sign
;
4846 bool denormal
= real_isdenormal (r
);
4849 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 8)) & 0x7f;
4867 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 6) - 1 : 0);
4868 if (r
->signalling
== fmt
->qnan_msb_set
)
4886 exp
= REAL_EXP (r
) + 127 - 1;
4898 /* Decode arm_bfloat types. */
4900 decode_arm_bfloat_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4903 unsigned long image
= buf
[0] & 0xffff;
4904 bool sign
= (image
>> 15) & 1;
4905 int exp
= (image
>> 7) & 0xff;
4907 memset (r
, 0, sizeof (*r
));
4908 image
<<= HOST_BITS_PER_LONG
- 8;
4913 if (image
&& fmt
->has_denorm
)
4917 SET_REAL_EXP (r
, -126);
4918 r
->sig
[SIGSZ
-1] = image
<< 1;
4921 else if (fmt
->has_signed_zero
)
4924 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
4930 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4931 ^ fmt
->qnan_msb_set
);
4932 r
->sig
[SIGSZ
-1] = image
;
4944 SET_REAL_EXP (r
, exp
- 127 + 1);
4945 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4949 /* Half-precision format, as specified in IEEE 754R. */
4950 const struct real_format ieee_half_format
=
4973 /* ARM's alternative half-precision format, similar to IEEE but with
4974 no reserved exponent value for NaNs and infinities; rather, it just
4975 extends the range of exponents by one. */
4976 const struct real_format arm_half_format
=
4999 /* ARM Bfloat half-precision format. This format resembles a truncated
5000 (16-bit) version of the 32-bit IEEE 754 single-precision floating-point
5002 const struct real_format arm_bfloat_half_format
=
5004 encode_arm_bfloat_half
,
5005 decode_arm_bfloat_half
,
5026 /* A synthetic "format" for internal arithmetic. It's the size of the
5027 internal significand minus the two bits needed for proper rounding.
5028 The encode and decode routines exist only to satisfy our paranoia
5031 static void encode_internal (const struct real_format
*fmt
,
5032 long *, const REAL_VALUE_TYPE
*);
5033 static void decode_internal (const struct real_format
*,
5034 REAL_VALUE_TYPE
*, const long *);
5037 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
5038 const REAL_VALUE_TYPE
*r
)
5040 memcpy (buf
, r
, sizeof (*r
));
5044 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
5045 REAL_VALUE_TYPE
*r
, const long *buf
)
5047 memcpy (r
, buf
, sizeof (*r
));
5050 const struct real_format real_internal_format
=
5055 SIGNIFICAND_BITS
- 2,
5056 SIGNIFICAND_BITS
- 2,
5073 /* Calculate X raised to the integer exponent N in format FMT and store
5074 the result in R. Return true if the result may be inexact due to
5075 loss of precision. The algorithm is the classic "left-to-right binary
5076 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
5077 Algorithms", "The Art of Computer Programming", Volume 2. */
5080 real_powi (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5081 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
5083 unsigned HOST_WIDE_INT bit
;
5085 bool inexact
= false;
5097 /* Don't worry about overflow, from now on n is unsigned. */
5105 bit
= HOST_WIDE_INT_1U
<< (HOST_BITS_PER_WIDE_INT
- 1);
5106 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
5110 inexact
|= do_multiply (&t
, &t
, &t
);
5112 inexact
|= do_multiply (&t
, &t
, x
);
5120 inexact
|= do_divide (&t
, &dconst1
, &t
);
5122 real_convert (r
, fmt
, &t
);
5126 /* Round X to the nearest integer not larger in absolute value, i.e.
5127 towards zero, placing the result in R in format FMT. */
5130 real_trunc (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5131 const REAL_VALUE_TYPE
*x
)
5133 do_fix_trunc (r
, x
);
5135 real_convert (r
, fmt
, r
);
5138 /* Round X to the largest integer not greater in value, i.e. round
5139 down, placing the result in R in format FMT. */
5142 real_floor (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5143 const REAL_VALUE_TYPE
*x
)
5147 do_fix_trunc (&t
, x
);
5148 if (! real_identical (&t
, x
) && x
->sign
)
5149 do_add (&t
, &t
, &dconstm1
, 0);
5151 real_convert (r
, fmt
, &t
);
5156 /* Round X to the smallest integer not less then argument, i.e. round
5157 up, placing the result in R in format FMT. */
5160 real_ceil (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5161 const REAL_VALUE_TYPE
*x
)
5165 do_fix_trunc (&t
, x
);
5166 if (! real_identical (&t
, x
) && ! x
->sign
)
5167 do_add (&t
, &t
, &dconst1
, 0);
5169 real_convert (r
, fmt
, &t
);
5174 /* Round X to the nearest integer, but round halfway cases away from
5178 real_round (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5179 const REAL_VALUE_TYPE
*x
)
5181 do_add (r
, x
, &dconsthalf
, x
->sign
);
5182 do_fix_trunc (r
, r
);
5184 real_convert (r
, fmt
, r
);
5187 /* Return true (including 0) if integer part of R is even, else return
5188 false. The function is not valid for rvc_inf and rvc_nan classes. */
5191 is_even (REAL_VALUE_TYPE
*r
)
5193 gcc_assert (r
->cl
!= rvc_inf
);
5194 gcc_assert (r
->cl
!= rvc_nan
);
5196 if (r
->cl
== rvc_zero
)
5199 /* For (-1,1), number is even. */
5200 if (REAL_EXP (r
) <= 0)
5203 /* Check lowest bit, if not set, return true. */
5204 else if (REAL_EXP (r
) <= SIGNIFICAND_BITS
)
5206 unsigned int n
= SIGNIFICAND_BITS
- REAL_EXP (r
);
5207 int w
= n
/ HOST_BITS_PER_LONG
;
5209 unsigned long num
= ((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
5211 if ((r
->sig
[w
] & num
) == 0)
5220 /* Return true if R is halfway between two integers, else return
5224 is_halfway_below (const REAL_VALUE_TYPE
*r
)
5226 if (r
->cl
!= rvc_normal
)
5229 /* For numbers (-0.5,0) and (0,0.5). */
5230 if (REAL_EXP (r
) < 0)
5233 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
5235 unsigned int n
= SIGNIFICAND_BITS
- REAL_EXP (r
) - 1;
5236 int w
= n
/ HOST_BITS_PER_LONG
;
5238 for (int i
= 0; i
< w
; ++i
)
5242 unsigned long num
= 1UL << (n
% HOST_BITS_PER_LONG
);
5244 if ((r
->sig
[w
] & num
) != 0 && (r
->sig
[w
] & (num
- 1)) == 0)
5250 /* Round X to nearest integer, rounding halfway cases towards even. */
5253 real_roundeven (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5254 const REAL_VALUE_TYPE
*x
)
5256 if (is_halfway_below (x
))
5258 /* Special case as -0.5 rounds to -0.0 and
5259 similarly +0.5 rounds to +0.0. */
5260 if (REAL_EXP (x
) == 0)
5263 clear_significand_below (r
, SIGNIFICAND_BITS
);
5267 do_add (r
, x
, &dconsthalf
, x
->sign
);
5269 do_add (r
, r
, &dconstm1
, x
->sign
);
5272 real_convert (r
, fmt
, r
);
5275 real_round (r
, fmt
, x
);
5278 /* Set the sign of R to the sign of X. */
5281 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
5286 /* Check whether the real constant value given is an integer.
5287 Returns false for signaling NaN. */
5290 real_isinteger (const REAL_VALUE_TYPE
*c
, format_helper fmt
)
5292 REAL_VALUE_TYPE cint
;
5294 real_trunc (&cint
, fmt
, c
);
5295 return real_identical (c
, &cint
);
5298 /* Check whether C is an integer that fits in a HOST_WIDE_INT,
5299 storing it in *INT_OUT if so. */
5302 real_isinteger (const REAL_VALUE_TYPE
*c
, HOST_WIDE_INT
*int_out
)
5304 REAL_VALUE_TYPE cint
;
5306 HOST_WIDE_INT n
= real_to_integer (c
);
5307 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
5308 if (real_identical (c
, &cint
))
5316 /* Calculate nextafter (X, Y) or nexttoward (X, Y). Return true if
5317 underflow or overflow needs to be raised. */
5320 real_nextafter (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5321 const REAL_VALUE_TYPE
*x
, const REAL_VALUE_TYPE
*y
)
5323 int cmp
= do_compare (x
, y
, 2);
5324 /* If either operand is NaN, return qNaN. */
5327 get_canonical_qnan (r
, 0);
5330 /* If x == y, return y cast to target type. */
5333 real_convert (r
, fmt
, y
);
5337 if (x
->cl
== rvc_zero
)
5339 get_zero (r
, y
->sign
);
5341 SET_REAL_EXP (r
, fmt
->emin
- fmt
->p
+ 1);
5342 r
->sig
[SIGSZ
- 1] = SIG_MSB
;
5346 int np2
= SIGNIFICAND_BITS
- fmt
->p
;
5347 /* For denormals adjust np2 correspondingly. */
5348 if (x
->cl
== rvc_normal
&& REAL_EXP (x
) < fmt
->emin
)
5349 np2
+= fmt
->emin
- REAL_EXP (x
);
5352 get_zero (r
, x
->sign
);
5354 set_significand_bit (&u
, np2
);
5356 SET_REAL_EXP (r
, REAL_EXP (x
));
5358 if (x
->cl
== rvc_inf
)
5360 bool borrow
= sub_significands (r
, r
, &u
, 0);
5361 gcc_assert (borrow
);
5362 SET_REAL_EXP (r
, fmt
->emax
);
5364 else if (cmp
== (x
->sign
? 1 : -1))
5366 if (add_significands (r
, x
, &u
))
5368 /* Overflow. Means the significand had been all ones, and
5369 is now all zeros. Need to increase the exponent, and
5370 possibly re-normalize it. */
5371 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
5372 if (REAL_EXP (r
) > fmt
->emax
)
5374 get_inf (r
, x
->sign
);
5377 r
->sig
[SIGSZ
- 1] = SIG_MSB
;
5382 if (REAL_EXP (x
) > fmt
->emin
&& x
->sig
[SIGSZ
- 1] == SIG_MSB
)
5385 for (i
= SIGSZ
- 2; i
>= 0; i
--)
5390 /* When mantissa is 1.0, we need to subtract only
5391 half of u: nextafter (1.0, 0.0) is 1.0 - __DBL_EPSILON__ / 2
5392 rather than 1.0 - __DBL_EPSILON__. */
5393 clear_significand_bit (&u
, np2
);
5395 set_significand_bit (&u
, np2
);
5398 sub_significands (r
, x
, &u
, 0);
5401 /* Clear out trailing garbage. */
5402 clear_significand_below (r
, np2
);
5404 if (REAL_EXP (r
) <= fmt
->emin
- fmt
->p
)
5406 get_zero (r
, x
->sign
);
5409 return r
->cl
== rvc_zero
|| REAL_EXP (r
) < fmt
->emin
;
5412 /* Write into BUF the maximum representable finite floating-point
5413 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5414 float string. LEN is the size of BUF, and the buffer must be large
5415 enough to contain the resulting string. If NORM_MAX, instead write
5416 the maximum representable finite normalized floating-point number,
5417 defined to be such that all choices of digits for that exponent are
5418 representable in the format (this only makes a difference for IBM
5422 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
,
5427 bool is_ibm_extended
= fmt
->pnan
< fmt
->p
;
5429 strcpy (buf
, "0x0.");
5431 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
5434 *p
++ = "08ce"[n
- i
];
5436 (is_ibm_extended
&& norm_max
) ? fmt
->emax
- 1 : fmt
->emax
);
5437 if (is_ibm_extended
&& !norm_max
)
5439 /* This is an IBM extended double format made up of two IEEE
5440 doubles. The value of the long double is the sum of the
5441 values of the two parts. The most significant part is
5442 required to be the value of the long double rounded to the
5443 nearest double. Rounding means we need a slightly smaller
5444 value for LDBL_MAX. */
5445 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
5448 gcc_assert (strlen (buf
) < len
);
5451 /* True if all values of integral type can be represented
5452 by this floating-point type exactly. */
5454 bool format_helper::can_represent_integral_type_p (tree type
) const
5456 gcc_assert (! decimal_p () && INTEGRAL_TYPE_P (type
));
5458 /* INT?_MIN is power-of-two so it takes
5459 only one mantissa bit. */
5460 bool signed_p
= TYPE_SIGN (type
) == SIGNED
;
5461 return TYPE_PRECISION (type
) - signed_p
<= significand_size (*this);
5464 /* True if mode M has a NaN representation and
5465 the treatment of NaN operands is important. */
5468 HONOR_NANS (machine_mode m
)
5470 return MODE_HAS_NANS (m
) && !flag_finite_math_only
;
5474 HONOR_NANS (const_tree t
)
5476 return HONOR_NANS (element_mode (t
));
5480 HONOR_NANS (const_rtx x
)
5482 return HONOR_NANS (GET_MODE (x
));
5485 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5488 HONOR_SNANS (machine_mode m
)
5490 return flag_signaling_nans
&& HONOR_NANS (m
);
5494 HONOR_SNANS (const_tree t
)
5496 return HONOR_SNANS (element_mode (t
));
5500 HONOR_SNANS (const_rtx x
)
5502 return HONOR_SNANS (GET_MODE (x
));
5505 /* As for HONOR_NANS, but true if the mode can represent infinity and
5506 the treatment of infinite values is important. */
5509 HONOR_INFINITIES (machine_mode m
)
5511 return MODE_HAS_INFINITIES (m
) && !flag_finite_math_only
;
5515 HONOR_INFINITIES (const_tree t
)
5517 return HONOR_INFINITIES (element_mode (t
));
5521 HONOR_INFINITIES (const_rtx x
)
5523 return HONOR_INFINITIES (GET_MODE (x
));
5526 /* Like HONOR_NANS, but true if the given mode distinguishes between
5527 positive and negative zero, and the sign of zero is important. */
5530 HONOR_SIGNED_ZEROS (machine_mode m
)
5532 return MODE_HAS_SIGNED_ZEROS (m
) && flag_signed_zeros
;
5536 HONOR_SIGNED_ZEROS (const_tree t
)
5538 return HONOR_SIGNED_ZEROS (element_mode (t
));
5542 HONOR_SIGNED_ZEROS (const_rtx x
)
5544 return HONOR_SIGNED_ZEROS (GET_MODE (x
));
5547 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5548 and the rounding mode is important. */
5551 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m
)
5553 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m
) && flag_rounding_math
;
5557 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t
)
5559 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t
));
5563 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x
)
5565 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x
));
5568 /* Fills r with the largest value such that 1 + r*r won't overflow.
5569 This is used in both sin (atan (x)) and cos (atan(x)) optimizations. */
5572 build_sinatan_real (REAL_VALUE_TYPE
* r
, tree type
)
5574 REAL_VALUE_TYPE maxval
;
5575 mpfr_t mpfr_const1
, mpfr_c
, mpfr_maxval
;
5576 machine_mode mode
= TYPE_MODE (type
);
5577 const struct real_format
* fmt
= REAL_MODE_FORMAT (mode
);
5579 real_maxval (&maxval
, 0, mode
);
5581 mpfr_inits (mpfr_const1
, mpfr_c
, mpfr_maxval
, NULL
);
5583 mpfr_from_real (mpfr_const1
, &dconst1
, MPFR_RNDN
);
5584 mpfr_from_real (mpfr_maxval
, &maxval
, MPFR_RNDN
);
5586 mpfr_sub (mpfr_c
, mpfr_maxval
, mpfr_const1
, MPFR_RNDN
);
5587 mpfr_sqrt (mpfr_c
, mpfr_c
, MPFR_RNDZ
);
5589 real_from_mpfr (r
, mpfr_c
, fmt
, MPFR_RNDZ
);
5591 mpfr_clears (mpfr_const1
, mpfr_c
, mpfr_maxval
, NULL
);