1 /* real.c - software floating point emulation.
2 Copyright (C) 1993-2020 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 a NaN. */
1240 real_isnan (const REAL_VALUE_TYPE
*r
)
1242 return (r
->cl
== rvc_nan
);
1245 /* Determine whether a floating-point value X is a signaling NaN. */
1246 bool real_issignaling_nan (const REAL_VALUE_TYPE
*r
)
1248 return real_isnan (r
) && r
->signalling
;
1251 /* Determine whether a floating-point value X is finite. */
1254 real_isfinite (const REAL_VALUE_TYPE
*r
)
1256 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1259 /* Determine whether a floating-point value X is negative. */
1262 real_isneg (const REAL_VALUE_TYPE
*r
)
1267 /* Determine whether a floating-point value X is minus zero. */
1270 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1272 return r
->sign
&& r
->cl
== rvc_zero
;
1275 /* Compare two floating-point objects for bitwise identity. */
1278 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1284 if (a
->sign
!= b
->sign
)
1294 if (a
->decimal
!= b
->decimal
)
1296 if (REAL_EXP (a
) != REAL_EXP (b
))
1301 if (a
->signalling
!= b
->signalling
)
1303 /* The significand is ignored for canonical NaNs. */
1304 if (a
->canonical
|| b
->canonical
)
1305 return a
->canonical
== b
->canonical
;
1312 for (i
= 0; i
< SIGSZ
; ++i
)
1313 if (a
->sig
[i
] != b
->sig
[i
])
1319 /* Try to change R into its exact multiplicative inverse in format FMT.
1320 Return true if successful. */
1323 exact_real_inverse (format_helper fmt
, REAL_VALUE_TYPE
*r
)
1325 const REAL_VALUE_TYPE
*one
= real_digit (1);
1329 if (r
->cl
!= rvc_normal
)
1332 /* Check for a power of two: all significand bits zero except the MSB. */
1333 for (i
= 0; i
< SIGSZ
-1; ++i
)
1336 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1339 /* Find the inverse and truncate to the required format. */
1340 do_divide (&u
, one
, r
);
1341 real_convert (&u
, fmt
, &u
);
1343 /* The rounding may have overflowed. */
1344 if (u
.cl
!= rvc_normal
)
1346 for (i
= 0; i
< SIGSZ
-1; ++i
)
1349 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1356 /* Return true if arithmetic on values in IMODE that were promoted
1357 from values in TMODE is equivalent to direct arithmetic on values
1361 real_can_shorten_arithmetic (machine_mode imode
, machine_mode tmode
)
1363 const struct real_format
*tfmt
, *ifmt
;
1364 tfmt
= REAL_MODE_FORMAT (tmode
);
1365 ifmt
= REAL_MODE_FORMAT (imode
);
1366 /* These conditions are conservative rather than trying to catch the
1367 exact boundary conditions; the main case to allow is IEEE float
1369 return (ifmt
->b
== tfmt
->b
1370 && ifmt
->p
> 2 * tfmt
->p
1371 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1372 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1373 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1374 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1375 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1376 && (ifmt
->has_sign_dependent_rounding
1377 == tfmt
->has_sign_dependent_rounding
)
1378 && ifmt
->has_nans
>= tfmt
->has_nans
1379 && ifmt
->has_inf
>= tfmt
->has_inf
1380 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1381 && !MODE_COMPOSITE_P (tmode
)
1382 && !MODE_COMPOSITE_P (imode
));
1385 /* Render R as an integer. */
1388 real_to_integer (const REAL_VALUE_TYPE
*r
)
1390 unsigned HOST_WIDE_INT i
;
1401 i
= HOST_WIDE_INT_1U
<< (HOST_BITS_PER_WIDE_INT
- 1);
1408 return decimal_real_to_integer (r
);
1410 if (REAL_EXP (r
) <= 0)
1412 /* Only force overflow for unsigned overflow. Signed overflow is
1413 undefined, so it doesn't matter what we return, and some callers
1414 expect to be able to use this routine for both signed and
1415 unsigned conversions. */
1416 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1419 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1420 i
= r
->sig
[SIGSZ
-1];
1423 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1424 i
= r
->sig
[SIGSZ
-1];
1425 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1426 i
|= r
->sig
[SIGSZ
-2];
1429 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1440 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1441 be represented in precision, *FAIL is set to TRUE. */
1444 real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
1446 HOST_WIDE_INT val
[2 * WIDE_INT_MAX_ELTS
];
1455 return wi::zero (precision
);
1463 return wi::set_bit_in_zero (precision
- 1, precision
);
1465 return ~wi::set_bit_in_zero (precision
- 1, precision
);
1469 return decimal_real_to_integer (r
, fail
, precision
);
1474 /* Only force overflow for unsigned overflow. Signed overflow is
1475 undefined, so it doesn't matter what we return, and some callers
1476 expect to be able to use this routine for both signed and
1477 unsigned conversions. */
1478 if (exp
> precision
)
1481 /* Put the significand into a wide_int that has precision W, which
1482 is the smallest HWI-multiple that has at least PRECISION bits.
1483 This ensures that the top bit of the significand is in the
1484 top bit of the wide_int. */
1485 words
= (precision
+ HOST_BITS_PER_WIDE_INT
- 1) / HOST_BITS_PER_WIDE_INT
;
1486 w
= words
* HOST_BITS_PER_WIDE_INT
;
1488 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1489 for (int i
= 0; i
< words
; i
++)
1491 int j
= SIGSZ
- words
+ i
;
1492 val
[i
] = (j
< 0) ? 0 : r
->sig
[j
];
1495 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1496 for (int i
= 0; i
< words
; i
++)
1498 int j
= SIGSZ
- (words
* 2) + (i
* 2);
1505 val
[i
] |= (unsigned HOST_WIDE_INT
) r
->sig
[j
] << HOST_BITS_PER_LONG
;
1508 /* Shift the value into place and truncate to the desired precision. */
1509 result
= wide_int::from_array (val
, words
, w
);
1510 result
= wi::lrshift (result
, w
- exp
);
1511 result
= wide_int::from (result
, precision
, UNSIGNED
);
1523 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1524 of NUM / DEN. Return the quotient and place the remainder in NUM.
1525 It is expected that NUM / DEN are close enough that the quotient is
1528 static unsigned long
1529 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1531 unsigned long q
, msb
;
1532 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1541 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1543 lshift_significand_1 (num
, num
);
1545 if (msb
|| cmp_significands (num
, den
) >= 0)
1547 sub_significands (num
, num
, den
, 0);
1551 while (--expn
>= expd
);
1553 SET_REAL_EXP (num
, expd
);
1559 /* Render R as a decimal floating point constant. Emit DIGITS significant
1560 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1561 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1562 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1563 to a string that, when parsed back in mode MODE, yields the same value. */
1565 #define M_LOG10_2 0.30102999566398119521
1568 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1569 size_t buf_size
, size_t digits
,
1570 int crop_trailing_zeros
, machine_mode mode
)
1572 const struct real_format
*fmt
= NULL
;
1573 const REAL_VALUE_TYPE
*one
, *ten
;
1574 REAL_VALUE_TYPE r
, pten
, u
, v
;
1575 int dec_exp
, cmp_one
, digit
;
1577 char *p
, *first
, *last
;
1581 if (mode
!= VOIDmode
)
1583 fmt
= REAL_MODE_FORMAT (mode
);
1591 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1596 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1599 /* ??? Print the significand as well, if not canonical? */
1600 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1601 (r_orig
->signalling
? 'S' : 'Q'));
1609 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1613 /* Bound the number of digits printed by the size of the representation. */
1614 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1615 if (digits
== 0 || digits
> max_digits
)
1616 digits
= max_digits
;
1618 /* Estimate the decimal exponent, and compute the length of the string it
1619 will print as. Be conservative and add one to account for possible
1620 overflow or rounding error. */
1621 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1622 for (max_digits
= 1; dec_exp
; max_digits
++)
1625 /* Bound the number of digits printed by the size of the output buffer. */
1626 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1627 gcc_assert (max_digits
<= buf_size
);
1628 if (digits
> max_digits
)
1629 digits
= max_digits
;
1631 one
= real_digit (1);
1632 ten
= ten_to_ptwo (0);
1640 cmp_one
= do_compare (&r
, one
, 0);
1645 /* Number is greater than one. Convert significand to an integer
1646 and strip trailing decimal zeros. */
1649 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1651 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1652 m
= floor_log2 (max_digits
);
1654 /* Iterate over the bits of the possible powers of 10 that might
1655 be present in U and eliminate them. That is, if we find that
1656 10**2**M divides U evenly, keep the division and increase
1662 do_divide (&t
, &u
, ten_to_ptwo (m
));
1663 do_fix_trunc (&v
, &t
);
1664 if (cmp_significands (&v
, &t
) == 0)
1672 /* Revert the scaling to integer that we performed earlier. */
1673 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1674 - (SIGNIFICAND_BITS
- 1));
1677 /* Find power of 10. Do this by dividing out 10**2**M when
1678 this is larger than the current remainder. Fill PTEN with
1679 the power of 10 that we compute. */
1680 if (REAL_EXP (&r
) > 0)
1682 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1685 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1686 if (do_compare (&u
, ptentwo
, 0) >= 0)
1688 do_divide (&u
, &u
, ptentwo
);
1689 do_multiply (&pten
, &pten
, ptentwo
);
1696 /* We managed to divide off enough tens in the above reduction
1697 loop that we've now got a negative exponent. Fall into the
1698 less-than-one code to compute the proper value for PTEN. */
1705 /* Number is less than one. Pad significand with leading
1711 /* Stop if we'd shift bits off the bottom. */
1715 do_multiply (&u
, &v
, ten
);
1717 /* Stop if we're now >= 1 or zero. */
1718 if (REAL_EXP (&u
) > 0 || u
.cl
== rvc_zero
)
1726 /* Find power of 10. Do this by multiplying in P=10**2**M when
1727 the current remainder is smaller than 1/P. Fill PTEN with the
1728 power of 10 that we compute. */
1729 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1732 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1733 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1735 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1737 do_multiply (&v
, &v
, ptentwo
);
1738 do_multiply (&pten
, &pten
, ptentwo
);
1744 /* Invert the positive power of 10 that we've collected so far. */
1745 do_divide (&pten
, one
, &pten
);
1753 /* At this point, PTEN should contain the nearest power of 10 smaller
1754 than R, such that this division produces the first digit.
1756 Using a divide-step primitive that returns the complete integral
1757 remainder avoids the rounding error that would be produced if
1758 we were to use do_divide here and then simply multiply by 10 for
1759 each subsequent digit. */
1761 digit
= rtd_divmod (&r
, &pten
);
1763 /* Be prepared for error in that division via underflow ... */
1764 if (digit
== 0 && cmp_significand_0 (&r
))
1766 /* Multiply by 10 and try again. */
1767 do_multiply (&r
, &r
, ten
);
1768 digit
= rtd_divmod (&r
, &pten
);
1770 gcc_assert (digit
!= 0);
1773 /* ... or overflow. */
1783 gcc_assert (digit
<= 10);
1787 /* Generate subsequent digits. */
1788 while (--digits
> 0)
1790 do_multiply (&r
, &r
, ten
);
1791 digit
= rtd_divmod (&r
, &pten
);
1796 /* Generate one more digit with which to do rounding. */
1797 do_multiply (&r
, &r
, ten
);
1798 digit
= rtd_divmod (&r
, &pten
);
1800 /* Round the result. */
1801 if (fmt
&& fmt
->round_towards_zero
)
1803 /* If the format uses round towards zero when parsing the string
1804 back in, we need to always round away from zero here. */
1805 if (cmp_significand_0 (&r
))
1807 round_up
= digit
> 0;
1813 /* Round to nearest. If R is nonzero there are additional
1814 nonzero digits to be extracted. */
1815 if (cmp_significand_0 (&r
))
1817 /* Round to even. */
1818 else if ((p
[-1] - '0') & 1)
1822 round_up
= digit
> 5;
1839 /* Carry out of the first digit. This means we had all 9's and
1840 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1848 /* Insert the decimal point. */
1849 first
[0] = first
[1];
1852 /* If requested, drop trailing zeros. Never crop past "1.0". */
1853 if (crop_trailing_zeros
)
1854 while (last
> first
+ 3 && last
[-1] == '0')
1857 /* Append the exponent. */
1858 sprintf (last
, "e%+d", dec_exp
);
1860 /* Verify that we can read the original value back in. */
1861 if (flag_checking
&& mode
!= VOIDmode
)
1863 real_from_string (&r
, str
);
1864 real_convert (&r
, mode
, &r
);
1865 gcc_assert (real_identical (&r
, r_orig
));
1869 /* Likewise, except always uses round-to-nearest. */
1872 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1873 size_t digits
, int crop_trailing_zeros
)
1875 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1876 digits
, crop_trailing_zeros
, VOIDmode
);
1879 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1880 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1881 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1882 strip trailing zeros. */
1885 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1886 size_t digits
, int crop_trailing_zeros
)
1888 int i
, j
, exp
= REAL_EXP (r
);
1901 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1904 /* ??? Print the significand as well, if not canonical? */
1905 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1906 (r
->signalling
? 'S' : 'Q'));
1914 /* Hexadecimal format for decimal floats is not interesting. */
1915 strcpy (str
, "N/A");
1920 digits
= SIGNIFICAND_BITS
/ 4;
1922 /* Bound the number of digits printed by the size of the output buffer. */
1924 sprintf (exp_buf
, "p%+d", exp
);
1925 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1926 gcc_assert (max_digits
<= buf_size
);
1927 if (digits
> max_digits
)
1928 digits
= max_digits
;
1939 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1940 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1942 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1948 if (crop_trailing_zeros
)
1949 while (p
> first
+ 1 && p
[-1] == '0')
1952 sprintf (p
, "p%+d", exp
);
1955 /* Initialize R from a decimal or hexadecimal string. The string is
1956 assumed to have been syntax checked already. Return -1 if the
1957 value underflows, +1 if overflows, and 0 otherwise. */
1960 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1972 else if (*str
== '+')
1975 if (!strncmp (str
, "QNaN", 4))
1977 get_canonical_qnan (r
, sign
);
1980 else if (!strncmp (str
, "SNaN", 4))
1982 get_canonical_snan (r
, sign
);
1985 else if (!strncmp (str
, "Inf", 3))
1991 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1993 /* Hexadecimal floating point. */
1994 int pos
= SIGNIFICAND_BITS
- 4, d
;
2002 d
= hex_value (*str
);
2007 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
2008 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
2012 /* Ensure correct rounding by setting last bit if there is
2013 a subsequent nonzero digit. */
2021 if (pos
== SIGNIFICAND_BITS
- 4)
2028 d
= hex_value (*str
);
2033 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
2034 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
2038 /* Ensure correct rounding by setting last bit if there is
2039 a subsequent nonzero digit. */
2045 /* If the mantissa is zero, ignore the exponent. */
2046 if (!cmp_significand_0 (r
))
2049 if (*str
== 'p' || *str
== 'P')
2051 bool exp_neg
= false;
2059 else if (*str
== '+')
2063 while (ISDIGIT (*str
))
2069 /* Overflowed the exponent. */
2084 SET_REAL_EXP (r
, exp
);
2090 /* Decimal floating point. */
2091 const char *cstr
= str
;
2095 while (*cstr
== '0')
2100 while (*cstr
== '0')
2104 /* If the mantissa is zero, ignore the exponent. */
2105 if (!ISDIGIT (*cstr
))
2108 /* Nonzero value, possibly overflowing or underflowing. */
2109 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2110 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, MPFR_RNDZ
);
2111 /* The result should never be a NaN, and because the rounding is
2112 toward zero should never be an infinity. */
2113 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2114 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2119 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2126 real_from_mpfr (r
, m
, NULL_TREE
, MPFR_RNDZ
);
2127 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2128 because the hex digits used in real_from_mpfr did not
2129 start with a digit 8 to f, but the exponent bounds above
2130 should have avoided underflow or overflow. */
2131 gcc_assert (r
->cl
== rvc_normal
);
2132 /* Set a sticky bit if mpfr_strtofr was inexact. */
2133 r
->sig
[0] |= inexact
;
2154 /* Legacy. Similar, but return the result directly. */
2157 real_from_string2 (const char *s
, format_helper fmt
)
2161 real_from_string (&r
, s
);
2163 real_convert (&r
, fmt
, &r
);
2168 /* Initialize R from string S and desired format FMT. */
2171 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, format_helper fmt
)
2173 if (fmt
.decimal_p ())
2174 decimal_real_from_string (r
, s
);
2176 real_from_string (r
, s
);
2179 real_convert (r
, fmt
, r
);
2182 /* Initialize R from the wide_int VAL_IN. Round it to format FMT if
2186 real_from_integer (REAL_VALUE_TYPE
*r
, format_helper fmt
,
2187 const wide_int_ref
&val_in
, signop sgn
)
2193 unsigned int len
= val_in
.get_precision ();
2195 int maxbitlen
= MAX_BITSIZE_MODE_ANY_INT
+ HOST_BITS_PER_WIDE_INT
;
2196 const unsigned int realmax
= (SIGNIFICAND_BITS
/ HOST_BITS_PER_WIDE_INT
2197 * HOST_BITS_PER_WIDE_INT
);
2199 memset (r
, 0, sizeof (*r
));
2201 r
->sign
= wi::neg_p (val_in
, sgn
);
2203 /* We have to ensure we can negate the largest negative number. */
2204 wide_int val
= wide_int::from (val_in
, maxbitlen
, sgn
);
2209 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2210 won't work with precisions that are not a multiple of
2211 HOST_BITS_PER_WIDE_INT. */
2212 len
+= HOST_BITS_PER_WIDE_INT
- 1;
2214 /* Ensure we can represent the largest negative number. */
2217 len
= len
/HOST_BITS_PER_WIDE_INT
* HOST_BITS_PER_WIDE_INT
;
2219 /* Cap the size to the size allowed by real.h. */
2222 HOST_WIDE_INT cnt_l_z
;
2223 cnt_l_z
= wi::clz (val
);
2225 if (maxbitlen
- cnt_l_z
> realmax
)
2227 e
= maxbitlen
- cnt_l_z
- realmax
;
2229 /* This value is too large, we must shift it right to
2230 preserve all the bits we can, and then bump the
2231 exponent up by that amount. */
2232 val
= wi::lrshift (val
, e
);
2237 /* Clear out top bits so elt will work with precisions that aren't
2238 a multiple of HOST_BITS_PER_WIDE_INT. */
2239 val
= wide_int::from (val
, len
, sgn
);
2240 len
= len
/ HOST_BITS_PER_WIDE_INT
;
2242 SET_REAL_EXP (r
, len
* HOST_BITS_PER_WIDE_INT
+ e
);
2245 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2246 for (i
= len
- 1; i
>= 0; i
--)
2248 r
->sig
[j
--] = val
.elt (i
);
2254 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2255 for (i
= len
- 1; i
>= 0; i
--)
2257 HOST_WIDE_INT e
= val
.elt (i
);
2258 r
->sig
[j
--] = e
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2270 if (fmt
.decimal_p ())
2271 decimal_from_integer (r
);
2273 real_convert (r
, fmt
, r
);
2276 /* Render R, an integral value, as a floating point constant with no
2277 specified exponent. */
2280 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2283 int dec_exp
, digit
, digits
;
2284 REAL_VALUE_TYPE r
, pten
;
2290 if (r
.cl
== rvc_zero
)
2299 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2300 digits
= dec_exp
+ 1;
2301 gcc_assert ((digits
+ 2) < (int)buf_size
);
2303 pten
= *real_digit (1);
2304 times_pten (&pten
, dec_exp
);
2310 digit
= rtd_divmod (&r
, &pten
);
2311 gcc_assert (digit
>= 0 && digit
<= 9);
2313 while (--digits
> 0)
2316 digit
= rtd_divmod (&r
, &pten
);
2323 /* Convert a real with an integral value to decimal float. */
2326 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2330 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2331 decimal_real_from_string (r
, str
);
2334 /* Returns 10**2**N. */
2336 static const REAL_VALUE_TYPE
*
2339 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2341 gcc_assert (n
>= 0);
2342 gcc_assert (n
< EXP_BITS
);
2344 if (tens
[n
].cl
== rvc_zero
)
2346 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2348 HOST_WIDE_INT t
= 10;
2351 for (i
= 0; i
< n
; ++i
)
2354 real_from_integer (&tens
[n
], VOIDmode
, t
, UNSIGNED
);
2358 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2359 do_multiply (&tens
[n
], t
, t
);
2366 /* Returns 10**(-2**N). */
2368 static const REAL_VALUE_TYPE
*
2369 ten_to_mptwo (int n
)
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
)
2377 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2384 static const REAL_VALUE_TYPE
*
2387 static REAL_VALUE_TYPE num
[10];
2389 gcc_assert (n
>= 0);
2390 gcc_assert (n
<= 9);
2392 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2393 real_from_integer (&num
[n
], VOIDmode
, n
, UNSIGNED
);
2398 /* Multiply R by 10**EXP. */
2401 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2403 REAL_VALUE_TYPE pten
, *rr
;
2404 bool negative
= (exp
< 0);
2410 pten
= *real_digit (1);
2416 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2418 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2421 do_divide (r
, r
, &pten
);
2424 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2426 const REAL_VALUE_TYPE
*
2429 static REAL_VALUE_TYPE value
;
2431 /* Initialize mathematical constants for constant folding builtins.
2432 These constants need to be given to at least 160 bits precision. */
2433 if (value
.cl
== rvc_zero
)
2436 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2437 mpfr_set_ui (m
, 1, MPFR_RNDN
);
2438 mpfr_exp (m
, m
, MPFR_RNDN
);
2439 real_from_mpfr (&value
, m
, NULL_TREE
, MPFR_RNDN
);
2446 /* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2448 #define CACHED_FRACTION(NAME, N) \
2449 const REAL_VALUE_TYPE * \
2452 static REAL_VALUE_TYPE value; \
2454 /* Initialize mathematical constants for constant folding builtins. \
2455 These constants need to be given to at least 160 bits \
2457 if (value.cl == rvc_zero) \
2458 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2462 CACHED_FRACTION (dconst_third_ptr
, 3)
2463 CACHED_FRACTION (dconst_quarter_ptr
, 4)
2464 CACHED_FRACTION (dconst_sixth_ptr
, 6)
2465 CACHED_FRACTION (dconst_ninth_ptr
, 9)
2467 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2469 const REAL_VALUE_TYPE
*
2470 dconst_sqrt2_ptr (void)
2472 static REAL_VALUE_TYPE value
;
2474 /* Initialize mathematical constants for constant folding builtins.
2475 These constants need to be given to at least 160 bits precision. */
2476 if (value
.cl
== rvc_zero
)
2479 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2480 mpfr_sqrt_ui (m
, 2, MPFR_RNDN
);
2481 real_from_mpfr (&value
, m
, NULL_TREE
, MPFR_RNDN
);
2487 /* Fills R with +Inf. */
2490 real_inf (REAL_VALUE_TYPE
*r
)
2495 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2496 we force a QNaN, else we force an SNaN. The string, if not empty,
2497 is parsed as a number and placed in the significand. Return true
2498 if the string was successfully parsed. */
2501 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2507 get_canonical_qnan (r
, 0);
2509 get_canonical_snan (r
, 0);
2515 memset (r
, 0, sizeof (*r
));
2518 /* Parse akin to strtol into the significand of R. */
2520 while (ISSPACE (*str
))
2524 else if (*str
== '+')
2529 if (*str
== 'x' || *str
== 'X')
2538 while ((d
= hex_value (*str
)) < base
)
2545 lshift_significand (r
, r
, 3);
2548 lshift_significand (r
, r
, 4);
2551 lshift_significand_1 (&u
, r
);
2552 lshift_significand (r
, r
, 3);
2553 add_significands (r
, r
, &u
);
2561 add_significands (r
, r
, &u
);
2566 /* Must have consumed the entire string for success. */
2570 /* Shift the significand into place such that the bits
2571 are in the most significant bits for the format. */
2572 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2574 /* Our MSB is always unset for NaNs. */
2575 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2577 /* Force quiet or signaling NaN. */
2578 r
->signalling
= !quiet
;
2584 /* Fills R with the largest finite value representable in mode MODE.
2585 If SIGN is nonzero, R is set to the most negative finite value. */
2588 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
2590 const struct real_format
*fmt
;
2593 fmt
= REAL_MODE_FORMAT (mode
);
2595 memset (r
, 0, sizeof (*r
));
2598 decimal_real_maxval (r
, sign
, mode
);
2603 SET_REAL_EXP (r
, fmt
->emax
);
2605 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2606 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2607 clear_significand_below (r
, np2
);
2609 if (fmt
->pnan
< fmt
->p
)
2610 /* This is an IBM extended double format made up of two IEEE
2611 doubles. The value of the long double is the sum of the
2612 values of the two parts. The most significant part is
2613 required to be the value of the long double rounded to the
2614 nearest double. Rounding means we need a slightly smaller
2615 value for LDBL_MAX. */
2616 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2620 /* Fills R with 2**N. */
2623 real_2expN (REAL_VALUE_TYPE
*r
, int n
, format_helper fmt
)
2625 memset (r
, 0, sizeof (*r
));
2630 else if (n
< -MAX_EXP
)
2635 SET_REAL_EXP (r
, n
);
2636 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2638 if (fmt
.decimal_p ())
2639 decimal_real_convert (r
, fmt
, r
);
2644 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2648 bool round_up
= false;
2654 decimal_round_for_format (fmt
, r
);
2657 /* FIXME. We can come here via fp_easy_constant
2658 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2659 investigated whether this convert needs to be here, or
2660 something else is missing. */
2661 decimal_real_convert (r
, REAL_MODE_FORMAT (DFmode
), r
);
2665 emin2m1
= fmt
->emin
- 1;
2668 np2
= SIGNIFICAND_BITS
- p2
;
2672 get_zero (r
, r
->sign
);
2675 if (!fmt
->has_signed_zero
)
2680 get_inf (r
, r
->sign
);
2685 clear_significand_below (r
, np2
);
2695 /* Check the range of the exponent. If we're out of range,
2696 either underflow or overflow. */
2697 if (REAL_EXP (r
) > emax2
)
2699 else if (REAL_EXP (r
) <= emin2m1
)
2703 if (!fmt
->has_denorm
)
2705 /* Don't underflow completely until we've had a chance to round. */
2706 if (REAL_EXP (r
) < emin2m1
)
2711 diff
= emin2m1
- REAL_EXP (r
) + 1;
2715 /* De-normalize the significand. */
2716 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2717 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2721 if (!fmt
->round_towards_zero
)
2723 /* There are P2 true significand bits, followed by one guard bit,
2724 followed by one sticky bit, followed by stuff. Fold nonzero
2725 stuff into the sticky bit. */
2726 unsigned long sticky
;
2730 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2731 sticky
|= r
->sig
[i
];
2733 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2735 guard
= test_significand_bit (r
, np2
- 1);
2736 lsb
= test_significand_bit (r
, np2
);
2738 /* Round to even. */
2739 round_up
= guard
&& (sticky
|| lsb
);
2746 set_significand_bit (&u
, np2
);
2748 if (add_significands (r
, r
, &u
))
2750 /* Overflow. Means the significand had been all ones, and
2751 is now all zeros. Need to increase the exponent, and
2752 possibly re-normalize it. */
2753 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2754 if (REAL_EXP (r
) > emax2
)
2756 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2760 /* Catch underflow that we deferred until after rounding. */
2761 if (REAL_EXP (r
) <= emin2m1
)
2764 /* Clear out trailing garbage. */
2765 clear_significand_below (r
, np2
);
2768 /* Extend or truncate to a new format. */
2771 real_convert (REAL_VALUE_TYPE
*r
, format_helper fmt
,
2772 const REAL_VALUE_TYPE
*a
)
2776 if (a
->decimal
|| fmt
->b
== 10)
2777 decimal_real_convert (r
, fmt
, a
);
2779 round_for_format (fmt
, r
);
2781 /* Make resulting NaN value to be qNaN. The caller has the
2782 responsibility to avoid the operation if flag_signaling_nans
2784 if (r
->cl
== rvc_nan
)
2787 /* round_for_format de-normalizes denormals. Undo just that part. */
2788 if (r
->cl
== rvc_normal
)
2792 /* Legacy. Likewise, except return the struct directly. */
2795 real_value_truncate (format_helper fmt
, REAL_VALUE_TYPE a
)
2798 real_convert (&r
, fmt
, &a
);
2802 /* Return true if truncating to FMT is exact. */
2805 exact_real_truncate (format_helper fmt
, const REAL_VALUE_TYPE
*a
)
2810 /* Don't allow conversion to denormals. */
2811 emin2m1
= fmt
->emin
- 1;
2812 if (REAL_EXP (a
) <= emin2m1
)
2815 /* After conversion to the new format, the value must be identical. */
2816 real_convert (&t
, fmt
, a
);
2817 return real_identical (&t
, a
);
2820 /* Write R to the given target format. Place the words of the result
2821 in target word order in BUF. There are always 32 bits in each
2822 long, no matter the size of the host long.
2824 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2827 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2834 round_for_format (fmt
, &r
);
2838 (*fmt
->encode
) (fmt
, buf
, &r
);
2843 /* Read R from the given target format. Read the words of the result
2844 in target word order in BUF. There are always 32 bits in each
2845 long, no matter the size of the host long. */
2848 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, format_helper fmt
)
2850 (*fmt
->decode
) (fmt
, r
, buf
);
2853 /* Return the number of bits of the largest binary value that the
2854 significand of FMT will hold. */
2855 /* ??? Legacy. Should get access to real_format directly. */
2858 significand_size (format_helper fmt
)
2865 /* Return the size in bits of the largest binary value that can be
2866 held by the decimal coefficient for this format. This is one more
2867 than the number of bits required to hold the largest coefficient
2869 double log2_10
= 3.3219281;
2870 return fmt
->p
* log2_10
;
2875 /* Return a hash value for the given real value. */
2876 /* ??? The "unsigned int" return value is intended to be hashval_t,
2877 but I didn't want to pull hashtab.h into real.h. */
2880 real_hash (const REAL_VALUE_TYPE
*r
)
2885 h
= r
->cl
| (r
->sign
<< 2);
2893 h
|= (unsigned int)REAL_EXP (r
) << 3;
2898 h
^= (unsigned int)-1;
2907 if (sizeof (unsigned long) > sizeof (unsigned int))
2908 for (i
= 0; i
< SIGSZ
; ++i
)
2910 unsigned long s
= r
->sig
[i
];
2911 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2914 for (i
= 0; i
< SIGSZ
; ++i
)
2920 /* IEEE single-precision format. */
2922 static void encode_ieee_single (const struct real_format
*fmt
,
2923 long *, const REAL_VALUE_TYPE
*);
2924 static void decode_ieee_single (const struct real_format
*,
2925 REAL_VALUE_TYPE
*, const long *);
2928 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2929 const REAL_VALUE_TYPE
*r
)
2931 unsigned long image
, sig
, exp
;
2932 unsigned long sign
= r
->sign
;
2933 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2936 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2947 image
|= 0x7fffffff;
2954 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2955 if (r
->signalling
== fmt
->qnan_msb_set
)
2966 image
|= 0x7fffffff;
2970 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2971 whereas the intermediate representation is 0.F x 2**exp.
2972 Which means we're off by one. */
2976 exp
= REAL_EXP (r
) + 127 - 1;
2989 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2992 unsigned long image
= buf
[0] & 0xffffffff;
2993 bool sign
= (image
>> 31) & 1;
2994 int exp
= (image
>> 23) & 0xff;
2996 memset (r
, 0, sizeof (*r
));
2997 image
<<= HOST_BITS_PER_LONG
- 24;
3002 if (image
&& fmt
->has_denorm
)
3006 SET_REAL_EXP (r
, -126);
3007 r
->sig
[SIGSZ
-1] = image
<< 1;
3010 else if (fmt
->has_signed_zero
)
3013 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
3019 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
3020 ^ fmt
->qnan_msb_set
);
3021 r
->sig
[SIGSZ
-1] = image
;
3033 SET_REAL_EXP (r
, exp
- 127 + 1);
3034 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
3038 const struct real_format ieee_single_format
=
3061 const struct real_format mips_single_format
=
3084 const struct real_format motorola_single_format
=
3107 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3108 single precision with the following differences:
3109 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3111 - NaNs are not supported.
3112 - The range of non-zero numbers in binary is
3113 (001)[1.]000...000 to (255)[1.]111...111.
3114 - Denormals can be represented, but are treated as +0.0 when
3115 used as an operand and are never generated as a result.
3116 - -0.0 can be represented, but a zero result is always +0.0.
3117 - the only supported rounding mode is trunction (towards zero). */
3118 const struct real_format spu_single_format
=
3141 /* IEEE double-precision format. */
3143 static void encode_ieee_double (const struct real_format
*fmt
,
3144 long *, const REAL_VALUE_TYPE
*);
3145 static void decode_ieee_double (const struct real_format
*,
3146 REAL_VALUE_TYPE
*, const long *);
3149 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3150 const REAL_VALUE_TYPE
*r
)
3152 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3153 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3155 image_hi
= r
->sign
<< 31;
3158 if (HOST_BITS_PER_LONG
== 64)
3160 sig_hi
= r
->sig
[SIGSZ
-1];
3161 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3162 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3166 sig_hi
= r
->sig
[SIGSZ
-1];
3167 sig_lo
= r
->sig
[SIGSZ
-2];
3168 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3169 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3179 image_hi
|= 2047 << 20;
3182 image_hi
|= 0x7fffffff;
3183 image_lo
= 0xffffffff;
3192 if (fmt
->canonical_nan_lsbs_set
)
3194 sig_hi
= (1 << 19) - 1;
3195 sig_lo
= 0xffffffff;
3203 if (r
->signalling
== fmt
->qnan_msb_set
)
3204 sig_hi
&= ~(1 << 19);
3207 if (sig_hi
== 0 && sig_lo
== 0)
3210 image_hi
|= 2047 << 20;
3216 image_hi
|= 0x7fffffff;
3217 image_lo
= 0xffffffff;
3222 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3223 whereas the intermediate representation is 0.F x 2**exp.
3224 Which means we're off by one. */
3228 exp
= REAL_EXP (r
) + 1023 - 1;
3229 image_hi
|= exp
<< 20;
3238 if (FLOAT_WORDS_BIG_ENDIAN
)
3239 buf
[0] = image_hi
, buf
[1] = image_lo
;
3241 buf
[0] = image_lo
, buf
[1] = image_hi
;
3245 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3248 unsigned long image_hi
, image_lo
;
3252 if (FLOAT_WORDS_BIG_ENDIAN
)
3253 image_hi
= buf
[0], image_lo
= buf
[1];
3255 image_lo
= buf
[0], image_hi
= buf
[1];
3256 image_lo
&= 0xffffffff;
3257 image_hi
&= 0xffffffff;
3259 sign
= (image_hi
>> 31) & 1;
3260 exp
= (image_hi
>> 20) & 0x7ff;
3262 memset (r
, 0, sizeof (*r
));
3264 image_hi
<<= 32 - 21;
3265 image_hi
|= image_lo
>> 21;
3266 image_hi
&= 0x7fffffff;
3267 image_lo
<<= 32 - 21;
3271 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3275 SET_REAL_EXP (r
, -1022);
3276 if (HOST_BITS_PER_LONG
== 32)
3278 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3280 r
->sig
[SIGSZ
-1] = image_hi
;
3281 r
->sig
[SIGSZ
-2] = image_lo
;
3285 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3286 r
->sig
[SIGSZ
-1] = image_hi
;
3290 else if (fmt
->has_signed_zero
)
3293 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3295 if (image_hi
|| image_lo
)
3299 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3300 if (HOST_BITS_PER_LONG
== 32)
3302 r
->sig
[SIGSZ
-1] = image_hi
;
3303 r
->sig
[SIGSZ
-2] = image_lo
;
3306 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3318 SET_REAL_EXP (r
, exp
- 1023 + 1);
3319 if (HOST_BITS_PER_LONG
== 32)
3321 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3322 r
->sig
[SIGSZ
-2] = image_lo
;
3325 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3329 const struct real_format ieee_double_format
=
3352 const struct real_format mips_double_format
=
3375 const struct real_format motorola_double_format
=
3398 /* IEEE extended real format. This comes in three flavors: Intel's as
3399 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3400 12- and 16-byte images may be big- or little endian; Motorola's is
3401 always big endian. */
3403 /* Helper subroutine which converts from the internal format to the
3404 12-byte little-endian Intel format. Functions below adjust this
3405 for the other possible formats. */
3407 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3408 const REAL_VALUE_TYPE
*r
)
3410 unsigned long image_hi
, sig_hi
, sig_lo
;
3411 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3413 image_hi
= r
->sign
<< 15;
3414 sig_hi
= sig_lo
= 0;
3426 /* Intel requires the explicit integer bit to be set, otherwise
3427 it considers the value a "pseudo-infinity". Motorola docs
3428 say it doesn't care. */
3429 sig_hi
= 0x80000000;
3434 sig_lo
= sig_hi
= 0xffffffff;
3444 if (fmt
->canonical_nan_lsbs_set
)
3446 sig_hi
= (1 << 30) - 1;
3447 sig_lo
= 0xffffffff;
3450 else if (HOST_BITS_PER_LONG
== 32)
3452 sig_hi
= r
->sig
[SIGSZ
-1];
3453 sig_lo
= r
->sig
[SIGSZ
-2];
3457 sig_lo
= r
->sig
[SIGSZ
-1];
3458 sig_hi
= sig_lo
>> 31 >> 1;
3459 sig_lo
&= 0xffffffff;
3461 if (r
->signalling
== fmt
->qnan_msb_set
)
3462 sig_hi
&= ~(1 << 30);
3465 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3468 /* Intel requires the explicit integer bit to be set, otherwise
3469 it considers the value a "pseudo-nan". Motorola docs say it
3471 sig_hi
|= 0x80000000;
3476 sig_lo
= sig_hi
= 0xffffffff;
3482 int exp
= REAL_EXP (r
);
3484 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3485 whereas the intermediate representation is 0.F x 2**exp.
3486 Which means we're off by one.
3488 Except for Motorola, which consider exp=0 and explicit
3489 integer bit set to continue to be normalized. In theory
3490 this discrepancy has been taken care of by the difference
3491 in fmt->emin in round_for_format. */
3498 gcc_assert (exp
>= 0);
3502 if (HOST_BITS_PER_LONG
== 32)
3504 sig_hi
= r
->sig
[SIGSZ
-1];
3505 sig_lo
= r
->sig
[SIGSZ
-2];
3509 sig_lo
= r
->sig
[SIGSZ
-1];
3510 sig_hi
= sig_lo
>> 31 >> 1;
3511 sig_lo
&= 0xffffffff;
3520 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3523 /* Convert from the internal format to the 12-byte Motorola format
3524 for an IEEE extended real. */
3526 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3527 const REAL_VALUE_TYPE
*r
)
3530 encode_ieee_extended (fmt
, intermed
, r
);
3532 if (r
->cl
== rvc_inf
)
3533 /* For infinity clear the explicit integer bit again, so that the
3534 format matches the canonical infinity generated by the FPU. */
3537 /* Motorola chips are assumed always to be big-endian. Also, the
3538 padding in a Motorola extended real goes between the exponent and
3539 the mantissa. At this point the mantissa is entirely within
3540 elements 0 and 1 of intermed, and the exponent entirely within
3541 element 2, so all we have to do is swap the order around, and
3542 shift element 2 left 16 bits. */
3543 buf
[0] = intermed
[2] << 16;
3544 buf
[1] = intermed
[1];
3545 buf
[2] = intermed
[0];
3548 /* Convert from the internal format to the 12-byte Intel format for
3549 an IEEE extended real. */
3551 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3552 const REAL_VALUE_TYPE
*r
)
3554 if (FLOAT_WORDS_BIG_ENDIAN
)
3556 /* All the padding in an Intel-format extended real goes at the high
3557 end, which in this case is after the mantissa, not the exponent.
3558 Therefore we must shift everything down 16 bits. */
3560 encode_ieee_extended (fmt
, intermed
, r
);
3561 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3562 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3563 buf
[2] = (intermed
[0] << 16);
3566 /* encode_ieee_extended produces what we want directly. */
3567 encode_ieee_extended (fmt
, buf
, r
);
3570 /* Convert from the internal format to the 16-byte Intel format for
3571 an IEEE extended real. */
3573 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3574 const REAL_VALUE_TYPE
*r
)
3576 /* All the padding in an Intel-format extended real goes at the high end. */
3577 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3581 /* As above, we have a helper function which converts from 12-byte
3582 little-endian Intel format to internal format. Functions below
3583 adjust for the other possible formats. */
3585 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3588 unsigned long image_hi
, sig_hi
, sig_lo
;
3592 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3593 sig_lo
&= 0xffffffff;
3594 sig_hi
&= 0xffffffff;
3595 image_hi
&= 0xffffffff;
3597 sign
= (image_hi
>> 15) & 1;
3598 exp
= image_hi
& 0x7fff;
3600 memset (r
, 0, sizeof (*r
));
3604 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3609 /* When the IEEE format contains a hidden bit, we know that
3610 it's zero at this point, and so shift up the significand
3611 and decrease the exponent to match. In this case, Motorola
3612 defines the explicit integer bit to be valid, so we don't
3613 know whether the msb is set or not. */
3614 SET_REAL_EXP (r
, fmt
->emin
);
3615 if (HOST_BITS_PER_LONG
== 32)
3617 r
->sig
[SIGSZ
-1] = sig_hi
;
3618 r
->sig
[SIGSZ
-2] = sig_lo
;
3621 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3625 else if (fmt
->has_signed_zero
)
3628 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3630 /* See above re "pseudo-infinities" and "pseudo-nans".
3631 Short summary is that the MSB will likely always be
3632 set, and that we don't care about it. */
3633 sig_hi
&= 0x7fffffff;
3635 if (sig_hi
|| sig_lo
)
3639 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3640 if (HOST_BITS_PER_LONG
== 32)
3642 r
->sig
[SIGSZ
-1] = sig_hi
;
3643 r
->sig
[SIGSZ
-2] = sig_lo
;
3646 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3658 SET_REAL_EXP (r
, exp
- 16383 + 1);
3659 if (HOST_BITS_PER_LONG
== 32)
3661 r
->sig
[SIGSZ
-1] = sig_hi
;
3662 r
->sig
[SIGSZ
-2] = sig_lo
;
3665 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3669 /* Convert from the internal format to the 12-byte Motorola format
3670 for an IEEE extended real. */
3672 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3677 /* Motorola chips are assumed always to be big-endian. Also, the
3678 padding in a Motorola extended real goes between the exponent and
3679 the mantissa; remove it. */
3680 intermed
[0] = buf
[2];
3681 intermed
[1] = buf
[1];
3682 intermed
[2] = (unsigned long)buf
[0] >> 16;
3684 decode_ieee_extended (fmt
, r
, intermed
);
3687 /* Convert from the internal format to the 12-byte Intel format for
3688 an IEEE extended real. */
3690 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3693 if (FLOAT_WORDS_BIG_ENDIAN
)
3695 /* All the padding in an Intel-format extended real goes at the high
3696 end, which in this case is after the mantissa, not the exponent.
3697 Therefore we must shift everything up 16 bits. */
3700 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3701 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3702 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3704 decode_ieee_extended (fmt
, r
, intermed
);
3707 /* decode_ieee_extended produces what we want directly. */
3708 decode_ieee_extended (fmt
, r
, buf
);
3711 /* Convert from the internal format to the 16-byte Intel format for
3712 an IEEE extended real. */
3714 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3717 /* All the padding in an Intel-format extended real goes at the high end. */
3718 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3721 const struct real_format ieee_extended_motorola_format
=
3723 encode_ieee_extended_motorola
,
3724 decode_ieee_extended_motorola
,
3741 "ieee_extended_motorola"
3744 const struct real_format ieee_extended_intel_96_format
=
3746 encode_ieee_extended_intel_96
,
3747 decode_ieee_extended_intel_96
,
3764 "ieee_extended_intel_96"
3767 const struct real_format ieee_extended_intel_128_format
=
3769 encode_ieee_extended_intel_128
,
3770 decode_ieee_extended_intel_128
,
3787 "ieee_extended_intel_128"
3790 /* The following caters to i386 systems that set the rounding precision
3791 to 53 bits instead of 64, e.g. FreeBSD. */
3792 const struct real_format ieee_extended_intel_96_round_53_format
=
3794 encode_ieee_extended_intel_96
,
3795 decode_ieee_extended_intel_96
,
3812 "ieee_extended_intel_96_round_53"
3815 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3816 numbers whose sum is equal to the extended precision value. The number
3817 with greater magnitude is first. This format has the same magnitude
3818 range as an IEEE double precision value, but effectively 106 bits of
3819 significand precision. Infinity and NaN are represented by their IEEE
3820 double precision value stored in the first number, the second number is
3821 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3823 static void encode_ibm_extended (const struct real_format
*fmt
,
3824 long *, const REAL_VALUE_TYPE
*);
3825 static void decode_ibm_extended (const struct real_format
*,
3826 REAL_VALUE_TYPE
*, const long *);
3829 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3830 const REAL_VALUE_TYPE
*r
)
3832 REAL_VALUE_TYPE u
, normr
, v
;
3833 const struct real_format
*base_fmt
;
3835 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3837 /* Renormalize R before doing any arithmetic on it. */
3839 if (normr
.cl
== rvc_normal
)
3842 /* u = IEEE double precision portion of significand. */
3844 round_for_format (base_fmt
, &u
);
3845 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3847 if (u
.cl
== rvc_normal
)
3849 do_add (&v
, &normr
, &u
, 1);
3850 /* Call round_for_format since we might need to denormalize. */
3851 round_for_format (base_fmt
, &v
);
3852 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3856 /* Inf, NaN, 0 are all representable as doubles, so the
3857 least-significant part can be 0.0. */
3864 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3867 REAL_VALUE_TYPE u
, v
;
3868 const struct real_format
*base_fmt
;
3870 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3871 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3873 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3875 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3876 do_add (r
, &u
, &v
, 0);
3882 const struct real_format ibm_extended_format
=
3884 encode_ibm_extended
,
3885 decode_ibm_extended
,
3905 const struct real_format mips_extended_format
=
3907 encode_ibm_extended
,
3908 decode_ibm_extended
,
3929 /* IEEE quad precision format. */
3931 static void encode_ieee_quad (const struct real_format
*fmt
,
3932 long *, const REAL_VALUE_TYPE
*);
3933 static void decode_ieee_quad (const struct real_format
*,
3934 REAL_VALUE_TYPE
*, const long *);
3937 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3938 const REAL_VALUE_TYPE
*r
)
3940 unsigned long image3
, image2
, image1
, image0
, exp
;
3941 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3944 image3
= r
->sign
<< 31;
3949 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3958 image3
|= 32767 << 16;
3961 image3
|= 0x7fffffff;
3962 image2
= 0xffffffff;
3963 image1
= 0xffffffff;
3964 image0
= 0xffffffff;
3971 image3
|= 32767 << 16;
3975 if (fmt
->canonical_nan_lsbs_set
)
3978 image2
= image1
= image0
= 0xffffffff;
3981 else if (HOST_BITS_PER_LONG
== 32)
3986 image3
|= u
.sig
[3] & 0xffff;
3991 image1
= image0
>> 31 >> 1;
3993 image3
|= (image2
>> 31 >> 1) & 0xffff;
3994 image0
&= 0xffffffff;
3995 image2
&= 0xffffffff;
3997 if (r
->signalling
== fmt
->qnan_msb_set
)
4001 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
4006 image3
|= 0x7fffffff;
4007 image2
= 0xffffffff;
4008 image1
= 0xffffffff;
4009 image0
= 0xffffffff;
4014 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4015 whereas the intermediate representation is 0.F x 2**exp.
4016 Which means we're off by one. */
4020 exp
= REAL_EXP (r
) + 16383 - 1;
4021 image3
|= exp
<< 16;
4023 if (HOST_BITS_PER_LONG
== 32)
4028 image3
|= u
.sig
[3] & 0xffff;
4033 image1
= image0
>> 31 >> 1;
4035 image3
|= (image2
>> 31 >> 1) & 0xffff;
4036 image0
&= 0xffffffff;
4037 image2
&= 0xffffffff;
4045 if (FLOAT_WORDS_BIG_ENDIAN
)
4062 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4065 unsigned long image3
, image2
, image1
, image0
;
4069 if (FLOAT_WORDS_BIG_ENDIAN
)
4083 image0
&= 0xffffffff;
4084 image1
&= 0xffffffff;
4085 image2
&= 0xffffffff;
4087 sign
= (image3
>> 31) & 1;
4088 exp
= (image3
>> 16) & 0x7fff;
4091 memset (r
, 0, sizeof (*r
));
4095 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4100 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4101 if (HOST_BITS_PER_LONG
== 32)
4110 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4111 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4116 else if (fmt
->has_signed_zero
)
4119 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4121 if (image3
| image2
| image1
| image0
)
4125 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4127 if (HOST_BITS_PER_LONG
== 32)
4136 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4137 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4139 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4151 SET_REAL_EXP (r
, exp
- 16383 + 1);
4153 if (HOST_BITS_PER_LONG
== 32)
4162 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4163 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4165 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4166 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4170 const struct real_format ieee_quad_format
=
4193 const struct real_format mips_quad_format
=
4216 /* Descriptions of VAX floating point formats can be found beginning at
4218 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4220 The thing to remember is that they're almost IEEE, except for word
4221 order, exponent bias, and the lack of infinities, nans, and denormals.
4223 We don't implement the H_floating format here, simply because neither
4224 the VAX or Alpha ports use it. */
4226 static void encode_vax_f (const struct real_format
*fmt
,
4227 long *, const REAL_VALUE_TYPE
*);
4228 static void decode_vax_f (const struct real_format
*,
4229 REAL_VALUE_TYPE
*, const long *);
4230 static void encode_vax_d (const struct real_format
*fmt
,
4231 long *, const REAL_VALUE_TYPE
*);
4232 static void decode_vax_d (const struct real_format
*,
4233 REAL_VALUE_TYPE
*, const long *);
4234 static void encode_vax_g (const struct real_format
*fmt
,
4235 long *, const REAL_VALUE_TYPE
*);
4236 static void decode_vax_g (const struct real_format
*,
4237 REAL_VALUE_TYPE
*, const long *);
4240 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4241 const REAL_VALUE_TYPE
*r
)
4243 unsigned long sign
, exp
, sig
, image
;
4245 sign
= r
->sign
<< 15;
4255 image
= 0xffff7fff | sign
;
4259 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4260 exp
= REAL_EXP (r
) + 128;
4262 image
= (sig
<< 16) & 0xffff0000;
4276 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4277 REAL_VALUE_TYPE
*r
, const long *buf
)
4279 unsigned long image
= buf
[0] & 0xffffffff;
4280 int exp
= (image
>> 7) & 0xff;
4282 memset (r
, 0, sizeof (*r
));
4287 r
->sign
= (image
>> 15) & 1;
4288 SET_REAL_EXP (r
, exp
- 128);
4290 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4291 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4296 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4297 const REAL_VALUE_TYPE
*r
)
4299 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4304 image0
= image1
= 0;
4309 image0
= 0xffff7fff | sign
;
4310 image1
= 0xffffffff;
4314 /* Extract the significand into straight hi:lo. */
4315 if (HOST_BITS_PER_LONG
== 64)
4317 image0
= r
->sig
[SIGSZ
-1];
4318 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4319 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4323 image0
= r
->sig
[SIGSZ
-1];
4324 image1
= r
->sig
[SIGSZ
-2];
4325 image1
= (image0
<< 24) | (image1
>> 8);
4326 image0
= (image0
>> 8) & 0xffffff;
4329 /* Rearrange the half-words of the significand to match the
4331 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4332 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4334 /* Add the sign and exponent. */
4336 image0
|= (REAL_EXP (r
) + 128) << 7;
4343 if (FLOAT_WORDS_BIG_ENDIAN
)
4344 buf
[0] = image1
, buf
[1] = image0
;
4346 buf
[0] = image0
, buf
[1] = image1
;
4350 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4351 REAL_VALUE_TYPE
*r
, const long *buf
)
4353 unsigned long image0
, image1
;
4356 if (FLOAT_WORDS_BIG_ENDIAN
)
4357 image1
= buf
[0], image0
= buf
[1];
4359 image0
= buf
[0], image1
= buf
[1];
4360 image0
&= 0xffffffff;
4361 image1
&= 0xffffffff;
4363 exp
= (image0
>> 7) & 0xff;
4365 memset (r
, 0, sizeof (*r
));
4370 r
->sign
= (image0
>> 15) & 1;
4371 SET_REAL_EXP (r
, exp
- 128);
4373 /* Rearrange the half-words of the external format into
4374 proper ascending order. */
4375 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4376 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4378 if (HOST_BITS_PER_LONG
== 64)
4380 image0
= (image0
<< 31 << 1) | image1
;
4383 r
->sig
[SIGSZ
-1] = image0
;
4387 r
->sig
[SIGSZ
-1] = image0
;
4388 r
->sig
[SIGSZ
-2] = image1
;
4389 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4390 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4396 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4397 const REAL_VALUE_TYPE
*r
)
4399 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4404 image0
= image1
= 0;
4409 image0
= 0xffff7fff | sign
;
4410 image1
= 0xffffffff;
4414 /* Extract the significand into straight hi:lo. */
4415 if (HOST_BITS_PER_LONG
== 64)
4417 image0
= r
->sig
[SIGSZ
-1];
4418 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4419 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4423 image0
= r
->sig
[SIGSZ
-1];
4424 image1
= r
->sig
[SIGSZ
-2];
4425 image1
= (image0
<< 21) | (image1
>> 11);
4426 image0
= (image0
>> 11) & 0xfffff;
4429 /* Rearrange the half-words of the significand to match the
4431 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4432 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4434 /* Add the sign and exponent. */
4436 image0
|= (REAL_EXP (r
) + 1024) << 4;
4443 if (FLOAT_WORDS_BIG_ENDIAN
)
4444 buf
[0] = image1
, buf
[1] = image0
;
4446 buf
[0] = image0
, buf
[1] = image1
;
4450 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4451 REAL_VALUE_TYPE
*r
, const long *buf
)
4453 unsigned long image0
, image1
;
4456 if (FLOAT_WORDS_BIG_ENDIAN
)
4457 image1
= buf
[0], image0
= buf
[1];
4459 image0
= buf
[0], image1
= buf
[1];
4460 image0
&= 0xffffffff;
4461 image1
&= 0xffffffff;
4463 exp
= (image0
>> 4) & 0x7ff;
4465 memset (r
, 0, sizeof (*r
));
4470 r
->sign
= (image0
>> 15) & 1;
4471 SET_REAL_EXP (r
, exp
- 1024);
4473 /* Rearrange the half-words of the external format into
4474 proper ascending order. */
4475 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4476 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4478 if (HOST_BITS_PER_LONG
== 64)
4480 image0
= (image0
<< 31 << 1) | image1
;
4483 r
->sig
[SIGSZ
-1] = image0
;
4487 r
->sig
[SIGSZ
-1] = image0
;
4488 r
->sig
[SIGSZ
-2] = image1
;
4489 lshift_significand (r
, r
, 64 - 53);
4490 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4495 const struct real_format vax_f_format
=
4518 const struct real_format vax_d_format
=
4541 const struct real_format vax_g_format
=
4564 /* Encode real R into a single precision DFP value in BUF. */
4566 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4567 long *buf ATTRIBUTE_UNUSED
,
4568 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4570 encode_decimal32 (fmt
, buf
, r
);
4573 /* Decode a single precision DFP value in BUF into a real R. */
4575 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4576 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4577 const long *buf ATTRIBUTE_UNUSED
)
4579 decode_decimal32 (fmt
, r
, buf
);
4582 /* Encode real R into a double precision DFP value in BUF. */
4584 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4585 long *buf ATTRIBUTE_UNUSED
,
4586 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4588 encode_decimal64 (fmt
, buf
, r
);
4591 /* Decode a double precision DFP value in BUF into a real R. */
4593 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4594 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4595 const long *buf ATTRIBUTE_UNUSED
)
4597 decode_decimal64 (fmt
, r
, buf
);
4600 /* Encode real R into a quad precision DFP value in BUF. */
4602 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4603 long *buf ATTRIBUTE_UNUSED
,
4604 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4606 encode_decimal128 (fmt
, buf
, r
);
4609 /* Decode a quad precision DFP value in BUF into a real R. */
4611 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4612 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4613 const long *buf ATTRIBUTE_UNUSED
)
4615 decode_decimal128 (fmt
, r
, buf
);
4618 /* Single precision decimal floating point (IEEE 754). */
4619 const struct real_format decimal_single_format
=
4621 encode_decimal_single
,
4622 decode_decimal_single
,
4642 /* Double precision decimal floating point (IEEE 754). */
4643 const struct real_format decimal_double_format
=
4645 encode_decimal_double
,
4646 decode_decimal_double
,
4666 /* Quad precision decimal floating point (IEEE 754). */
4667 const struct real_format decimal_quad_format
=
4669 encode_decimal_quad
,
4670 decode_decimal_quad
,
4690 /* Encode half-precision floats. This routine is used both for the IEEE
4691 ARM alternative encodings. */
4693 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4694 const REAL_VALUE_TYPE
*r
)
4696 unsigned long image
, sig
, exp
;
4697 unsigned long sign
= r
->sign
;
4698 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4701 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4719 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4720 if (r
->signalling
== fmt
->qnan_msb_set
)
4735 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4736 whereas the intermediate representation is 0.F x 2**exp.
4737 Which means we're off by one. */
4741 exp
= REAL_EXP (r
) + 15 - 1;
4753 /* Decode half-precision floats. This routine is used both for the IEEE
4754 ARM alternative encodings. */
4756 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4759 unsigned long image
= buf
[0] & 0xffff;
4760 bool sign
= (image
>> 15) & 1;
4761 int exp
= (image
>> 10) & 0x1f;
4763 memset (r
, 0, sizeof (*r
));
4764 image
<<= HOST_BITS_PER_LONG
- 11;
4769 if (image
&& fmt
->has_denorm
)
4773 SET_REAL_EXP (r
, -14);
4774 r
->sig
[SIGSZ
-1] = image
<< 1;
4777 else if (fmt
->has_signed_zero
)
4780 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4786 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4787 ^ fmt
->qnan_msb_set
);
4788 r
->sig
[SIGSZ
-1] = image
;
4800 SET_REAL_EXP (r
, exp
- 15 + 1);
4801 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4805 /* Encode arm_bfloat types. */
4807 encode_arm_bfloat_half (const struct real_format
*fmt
, long *buf
,
4808 const REAL_VALUE_TYPE
*r
)
4810 unsigned long image
, sig
, exp
;
4811 unsigned long sign
= r
->sign
;
4812 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4815 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 8)) & 0x7f;
4833 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 6) - 1 : 0);
4834 if (r
->signalling
== fmt
->qnan_msb_set
)
4852 exp
= REAL_EXP (r
) + 127 - 1;
4864 /* Decode arm_bfloat types. */
4866 decode_arm_bfloat_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4869 unsigned long image
= buf
[0] & 0xffff;
4870 bool sign
= (image
>> 15) & 1;
4871 int exp
= (image
>> 7) & 0xff;
4873 memset (r
, 0, sizeof (*r
));
4874 image
<<= HOST_BITS_PER_LONG
- 8;
4879 if (image
&& fmt
->has_denorm
)
4883 SET_REAL_EXP (r
, -126);
4884 r
->sig
[SIGSZ
-1] = image
<< 1;
4887 else if (fmt
->has_signed_zero
)
4890 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
4896 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4897 ^ fmt
->qnan_msb_set
);
4898 r
->sig
[SIGSZ
-1] = image
;
4910 SET_REAL_EXP (r
, exp
- 127 + 1);
4911 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4915 /* Half-precision format, as specified in IEEE 754R. */
4916 const struct real_format ieee_half_format
=
4939 /* ARM's alternative half-precision format, similar to IEEE but with
4940 no reserved exponent value for NaNs and infinities; rather, it just
4941 extends the range of exponents by one. */
4942 const struct real_format arm_half_format
=
4965 /* ARM Bfloat half-precision format. This format resembles a truncated
4966 (16-bit) version of the 32-bit IEEE 754 single-precision floating-point
4968 const struct real_format arm_bfloat_half_format
=
4970 encode_arm_bfloat_half
,
4971 decode_arm_bfloat_half
,
4992 /* A synthetic "format" for internal arithmetic. It's the size of the
4993 internal significand minus the two bits needed for proper rounding.
4994 The encode and decode routines exist only to satisfy our paranoia
4997 static void encode_internal (const struct real_format
*fmt
,
4998 long *, const REAL_VALUE_TYPE
*);
4999 static void decode_internal (const struct real_format
*,
5000 REAL_VALUE_TYPE
*, const long *);
5003 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
5004 const REAL_VALUE_TYPE
*r
)
5006 memcpy (buf
, r
, sizeof (*r
));
5010 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
5011 REAL_VALUE_TYPE
*r
, const long *buf
)
5013 memcpy (r
, buf
, sizeof (*r
));
5016 const struct real_format real_internal_format
=
5021 SIGNIFICAND_BITS
- 2,
5022 SIGNIFICAND_BITS
- 2,
5039 /* Calculate X raised to the integer exponent N in format FMT and store
5040 the result in R. Return true if the result may be inexact due to
5041 loss of precision. The algorithm is the classic "left-to-right binary
5042 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
5043 Algorithms", "The Art of Computer Programming", Volume 2. */
5046 real_powi (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5047 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
5049 unsigned HOST_WIDE_INT bit
;
5051 bool inexact
= false;
5063 /* Don't worry about overflow, from now on n is unsigned. */
5071 bit
= HOST_WIDE_INT_1U
<< (HOST_BITS_PER_WIDE_INT
- 1);
5072 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
5076 inexact
|= do_multiply (&t
, &t
, &t
);
5078 inexact
|= do_multiply (&t
, &t
, x
);
5086 inexact
|= do_divide (&t
, &dconst1
, &t
);
5088 real_convert (r
, fmt
, &t
);
5092 /* Round X to the nearest integer not larger in absolute value, i.e.
5093 towards zero, placing the result in R in format FMT. */
5096 real_trunc (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5097 const REAL_VALUE_TYPE
*x
)
5099 do_fix_trunc (r
, x
);
5101 real_convert (r
, fmt
, r
);
5104 /* Round X to the largest integer not greater in value, i.e. round
5105 down, placing the result in R in format FMT. */
5108 real_floor (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5109 const REAL_VALUE_TYPE
*x
)
5113 do_fix_trunc (&t
, x
);
5114 if (! real_identical (&t
, x
) && x
->sign
)
5115 do_add (&t
, &t
, &dconstm1
, 0);
5117 real_convert (r
, fmt
, &t
);
5122 /* Round X to the smallest integer not less then argument, i.e. round
5123 up, placing the result in R in format FMT. */
5126 real_ceil (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5127 const REAL_VALUE_TYPE
*x
)
5131 do_fix_trunc (&t
, x
);
5132 if (! real_identical (&t
, x
) && ! x
->sign
)
5133 do_add (&t
, &t
, &dconst1
, 0);
5135 real_convert (r
, fmt
, &t
);
5140 /* Round X to the nearest integer, but round halfway cases away from
5144 real_round (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5145 const REAL_VALUE_TYPE
*x
)
5147 do_add (r
, x
, &dconsthalf
, x
->sign
);
5148 do_fix_trunc (r
, r
);
5150 real_convert (r
, fmt
, r
);
5153 /* Return true (including 0) if integer part of R is even, else return
5154 false. The function is not valid for rvc_inf and rvc_nan classes. */
5157 is_even (REAL_VALUE_TYPE
*r
)
5159 gcc_assert (r
->cl
!= rvc_inf
);
5160 gcc_assert (r
->cl
!= rvc_nan
);
5162 if (r
->cl
== rvc_zero
)
5165 /* For (-1,1), number is even. */
5166 if (REAL_EXP (r
) <= 0)
5169 /* Check lowest bit, if not set, return true. */
5170 else if (REAL_EXP (r
) <= SIGNIFICAND_BITS
)
5172 unsigned int n
= SIGNIFICAND_BITS
- REAL_EXP (r
);
5173 int w
= n
/ HOST_BITS_PER_LONG
;
5175 unsigned long num
= ((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
5177 if ((r
->sig
[w
] & num
) == 0)
5186 /* Return true if R is halfway between two integers, else return
5190 is_halfway_below (const REAL_VALUE_TYPE
*r
)
5192 if (r
->cl
!= rvc_normal
)
5195 /* For numbers (-0.5,0) and (0,0.5). */
5196 if (REAL_EXP (r
) < 0)
5199 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
5201 unsigned int n
= SIGNIFICAND_BITS
- REAL_EXP (r
) - 1;
5202 int w
= n
/ HOST_BITS_PER_LONG
;
5204 for (int i
= 0; i
< w
; ++i
)
5208 unsigned long num
= 1UL << (n
% HOST_BITS_PER_LONG
);
5210 if ((r
->sig
[w
] & num
) != 0 && (r
->sig
[w
] & (num
- 1)) == 0)
5216 /* Round X to nearest integer, rounding halfway cases towards even. */
5219 real_roundeven (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5220 const REAL_VALUE_TYPE
*x
)
5222 if (is_halfway_below (x
))
5224 /* Special case as -0.5 rounds to -0.0 and
5225 similarly +0.5 rounds to +0.0. */
5226 if (REAL_EXP (x
) == 0)
5229 clear_significand_below (r
, SIGNIFICAND_BITS
);
5233 do_add (r
, x
, &dconsthalf
, x
->sign
);
5235 do_add (r
, r
, &dconstm1
, x
->sign
);
5238 real_convert (r
, fmt
, r
);
5241 real_round (r
, fmt
, x
);
5244 /* Set the sign of R to the sign of X. */
5247 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
5252 /* Check whether the real constant value given is an integer.
5253 Returns false for signaling NaN. */
5256 real_isinteger (const REAL_VALUE_TYPE
*c
, format_helper fmt
)
5258 REAL_VALUE_TYPE cint
;
5260 real_trunc (&cint
, fmt
, c
);
5261 return real_identical (c
, &cint
);
5264 /* Check whether C is an integer that fits in a HOST_WIDE_INT,
5265 storing it in *INT_OUT if so. */
5268 real_isinteger (const REAL_VALUE_TYPE
*c
, HOST_WIDE_INT
*int_out
)
5270 REAL_VALUE_TYPE cint
;
5272 HOST_WIDE_INT n
= real_to_integer (c
);
5273 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
5274 if (real_identical (c
, &cint
))
5282 /* Calculate nextafter (X, Y) or nexttoward (X, Y). Return true if
5283 underflow or overflow needs to be raised. */
5286 real_nextafter (REAL_VALUE_TYPE
*r
, format_helper fmt
,
5287 const REAL_VALUE_TYPE
*x
, const REAL_VALUE_TYPE
*y
)
5289 int cmp
= do_compare (x
, y
, 2);
5290 /* If either operand is NaN, return qNaN. */
5293 get_canonical_qnan (r
, 0);
5296 /* If x == y, return y cast to target type. */
5299 real_convert (r
, fmt
, y
);
5303 if (x
->cl
== rvc_zero
)
5305 get_zero (r
, y
->sign
);
5307 SET_REAL_EXP (r
, fmt
->emin
- fmt
->p
+ 1);
5308 r
->sig
[SIGSZ
- 1] = SIG_MSB
;
5312 int np2
= SIGNIFICAND_BITS
- fmt
->p
;
5313 /* For denormals adjust np2 correspondingly. */
5314 if (x
->cl
== rvc_normal
&& REAL_EXP (x
) < fmt
->emin
)
5315 np2
+= fmt
->emin
- REAL_EXP (x
);
5318 get_zero (r
, x
->sign
);
5320 set_significand_bit (&u
, np2
);
5322 SET_REAL_EXP (r
, REAL_EXP (x
));
5324 if (x
->cl
== rvc_inf
)
5326 bool borrow
= sub_significands (r
, r
, &u
, 0);
5327 gcc_assert (borrow
);
5328 SET_REAL_EXP (r
, fmt
->emax
);
5330 else if (cmp
== (x
->sign
? 1 : -1))
5332 if (add_significands (r
, x
, &u
))
5334 /* Overflow. Means the significand had been all ones, and
5335 is now all zeros. Need to increase the exponent, and
5336 possibly re-normalize it. */
5337 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
5338 if (REAL_EXP (r
) > fmt
->emax
)
5340 get_inf (r
, x
->sign
);
5343 r
->sig
[SIGSZ
- 1] = SIG_MSB
;
5348 if (REAL_EXP (x
) > fmt
->emin
&& x
->sig
[SIGSZ
- 1] == SIG_MSB
)
5351 for (i
= SIGSZ
- 2; i
>= 0; i
--)
5356 /* When mantissa is 1.0, we need to subtract only
5357 half of u: nextafter (1.0, 0.0) is 1.0 - __DBL_EPSILON__ / 2
5358 rather than 1.0 - __DBL_EPSILON__. */
5359 clear_significand_bit (&u
, np2
);
5361 set_significand_bit (&u
, np2
);
5364 sub_significands (r
, x
, &u
, 0);
5367 /* Clear out trailing garbage. */
5368 clear_significand_below (r
, np2
);
5370 if (REAL_EXP (r
) <= fmt
->emin
- fmt
->p
)
5372 get_zero (r
, x
->sign
);
5375 return r
->cl
== rvc_zero
|| REAL_EXP (r
) < fmt
->emin
;
5378 /* Write into BUF the maximum representable finite floating-point
5379 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5380 float string. LEN is the size of BUF, and the buffer must be large
5381 enough to contain the resulting string. If NORM_MAX, instead write
5382 the maximum representable finite normalized floating-point number,
5383 defined to be such that all choices of digits for that exponent are
5384 representable in the format (this only makes a difference for IBM
5388 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
,
5393 bool is_ibm_extended
= fmt
->pnan
< fmt
->p
;
5395 strcpy (buf
, "0x0.");
5397 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
5400 *p
++ = "08ce"[n
- i
];
5402 (is_ibm_extended
&& norm_max
) ? fmt
->emax
- 1 : fmt
->emax
);
5403 if (is_ibm_extended
&& !norm_max
)
5405 /* This is an IBM extended double format made up of two IEEE
5406 doubles. The value of the long double is the sum of the
5407 values of the two parts. The most significant part is
5408 required to be the value of the long double rounded to the
5409 nearest double. Rounding means we need a slightly smaller
5410 value for LDBL_MAX. */
5411 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
5414 gcc_assert (strlen (buf
) < len
);
5417 /* True if all values of integral type can be represented
5418 by this floating-point type exactly. */
5420 bool format_helper::can_represent_integral_type_p (tree type
) const
5422 gcc_assert (! decimal_p () && INTEGRAL_TYPE_P (type
));
5424 /* INT?_MIN is power-of-two so it takes
5425 only one mantissa bit. */
5426 bool signed_p
= TYPE_SIGN (type
) == SIGNED
;
5427 return TYPE_PRECISION (type
) - signed_p
<= significand_size (*this);
5430 /* True if mode M has a NaN representation and
5431 the treatment of NaN operands is important. */
5434 HONOR_NANS (machine_mode m
)
5436 return MODE_HAS_NANS (m
) && !flag_finite_math_only
;
5440 HONOR_NANS (const_tree t
)
5442 return HONOR_NANS (element_mode (t
));
5446 HONOR_NANS (const_rtx x
)
5448 return HONOR_NANS (GET_MODE (x
));
5451 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5454 HONOR_SNANS (machine_mode m
)
5456 return flag_signaling_nans
&& HONOR_NANS (m
);
5460 HONOR_SNANS (const_tree t
)
5462 return HONOR_SNANS (element_mode (t
));
5466 HONOR_SNANS (const_rtx x
)
5468 return HONOR_SNANS (GET_MODE (x
));
5471 /* As for HONOR_NANS, but true if the mode can represent infinity and
5472 the treatment of infinite values is important. */
5475 HONOR_INFINITIES (machine_mode m
)
5477 return MODE_HAS_INFINITIES (m
) && !flag_finite_math_only
;
5481 HONOR_INFINITIES (const_tree t
)
5483 return HONOR_INFINITIES (element_mode (t
));
5487 HONOR_INFINITIES (const_rtx x
)
5489 return HONOR_INFINITIES (GET_MODE (x
));
5492 /* Like HONOR_NANS, but true if the given mode distinguishes between
5493 positive and negative zero, and the sign of zero is important. */
5496 HONOR_SIGNED_ZEROS (machine_mode m
)
5498 return MODE_HAS_SIGNED_ZEROS (m
) && flag_signed_zeros
;
5502 HONOR_SIGNED_ZEROS (const_tree t
)
5504 return HONOR_SIGNED_ZEROS (element_mode (t
));
5508 HONOR_SIGNED_ZEROS (const_rtx x
)
5510 return HONOR_SIGNED_ZEROS (GET_MODE (x
));
5513 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5514 and the rounding mode is important. */
5517 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m
)
5519 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m
) && flag_rounding_math
;
5523 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t
)
5525 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t
));
5529 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x
)
5531 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x
));
5534 /* Fills r with the largest value such that 1 + r*r won't overflow.
5535 This is used in both sin (atan (x)) and cos (atan(x)) optimizations. */
5538 build_sinatan_real (REAL_VALUE_TYPE
* r
, tree type
)
5540 REAL_VALUE_TYPE maxval
;
5541 mpfr_t mpfr_const1
, mpfr_c
, mpfr_maxval
;
5542 machine_mode mode
= TYPE_MODE (type
);
5543 const struct real_format
* fmt
= REAL_MODE_FORMAT (mode
);
5545 real_maxval (&maxval
, 0, mode
);
5547 mpfr_inits (mpfr_const1
, mpfr_c
, mpfr_maxval
, NULL
);
5549 mpfr_from_real (mpfr_const1
, &dconst1
, MPFR_RNDN
);
5550 mpfr_from_real (mpfr_maxval
, &maxval
, MPFR_RNDN
);
5552 mpfr_sub (mpfr_c
, mpfr_maxval
, mpfr_const1
, MPFR_RNDN
);
5553 mpfr_sqrt (mpfr_c
, mpfr_c
, MPFR_RNDZ
);
5555 real_from_mpfr (r
, mpfr_c
, fmt
, MPFR_RNDZ
);
5557 mpfr_clears (mpfr_const1
, mpfr_c
, mpfr_maxval
, NULL
);