1 /* real.c - software floating point emulation.
2 Copyright (C) 1993-2016 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 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
426 /* Divide the significands of A and B, placing the result in R. Return
427 true if the division was inexact. */
430 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
431 const REAL_VALUE_TYPE
*b
)
434 int i
, bit
= SIGNIFICAND_BITS
- 1;
435 unsigned long msb
, inexact
;
438 memset (r
->sig
, 0, sizeof (r
->sig
));
444 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
445 lshift_significand_1 (&u
, &u
);
447 if (msb
|| cmp_significands (&u
, b
) >= 0)
449 sub_significands (&u
, &u
, b
, 0);
450 set_significand_bit (r
, bit
);
455 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
461 /* Adjust the exponent and significand of R such that the most
462 significant bit is set. We underflow to zero and overflow to
463 infinity here, without denormals. (The intermediate representation
464 exponent is large enough to handle target denormals normalized.) */
467 normalize (REAL_VALUE_TYPE
*r
)
475 /* Find the first word that is nonzero. */
476 for (i
= SIGSZ
- 1; i
>= 0; i
--)
478 shift
+= HOST_BITS_PER_LONG
;
482 /* Zero significand flushes to zero. */
490 /* Find the first bit that is nonzero. */
492 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
498 exp
= REAL_EXP (r
) - shift
;
500 get_inf (r
, r
->sign
);
501 else if (exp
< -MAX_EXP
)
502 get_zero (r
, r
->sign
);
505 SET_REAL_EXP (r
, exp
);
506 lshift_significand (r
, r
, shift
);
511 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
512 result may be inexact due to a loss of precision. */
515 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
516 const REAL_VALUE_TYPE
*b
, int subtract_p
)
520 bool inexact
= false;
522 /* Determine if we need to add or subtract. */
524 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
526 switch (CLASS2 (a
->cl
, b
->cl
))
528 case CLASS2 (rvc_zero
, rvc_zero
):
529 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
530 get_zero (r
, sign
& !subtract_p
);
533 case CLASS2 (rvc_zero
, rvc_normal
):
534 case CLASS2 (rvc_zero
, rvc_inf
):
535 case CLASS2 (rvc_zero
, rvc_nan
):
537 case CLASS2 (rvc_normal
, rvc_nan
):
538 case CLASS2 (rvc_inf
, rvc_nan
):
539 case CLASS2 (rvc_nan
, rvc_nan
):
540 /* ANY + NaN = NaN. */
541 case CLASS2 (rvc_normal
, rvc_inf
):
544 /* Make resulting NaN value to be qNaN. The caller has the
545 responsibility to avoid the operation if flag_signaling_nans
548 r
->sign
= sign
^ subtract_p
;
551 case CLASS2 (rvc_normal
, rvc_zero
):
552 case CLASS2 (rvc_inf
, rvc_zero
):
553 case CLASS2 (rvc_nan
, rvc_zero
):
555 case CLASS2 (rvc_nan
, rvc_normal
):
556 case CLASS2 (rvc_nan
, rvc_inf
):
557 /* NaN + ANY = NaN. */
558 case CLASS2 (rvc_inf
, rvc_normal
):
561 /* Make resulting NaN value to be qNaN. The caller has the
562 responsibility to avoid the operation if flag_signaling_nans
567 case CLASS2 (rvc_inf
, rvc_inf
):
569 /* Inf - Inf = NaN. */
570 get_canonical_qnan (r
, 0);
572 /* Inf + Inf = Inf. */
576 case CLASS2 (rvc_normal
, rvc_normal
):
583 /* Swap the arguments such that A has the larger exponent. */
584 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
587 const REAL_VALUE_TYPE
*t
;
594 /* If the exponents are not identical, we need to shift the
595 significand of B down. */
598 /* If the exponents are too far apart, the significands
599 do not overlap, which makes the subtraction a noop. */
600 if (dexp
>= SIGNIFICAND_BITS
)
607 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
613 if (sub_significands (r
, a
, b
, inexact
))
615 /* We got a borrow out of the subtraction. That means that
616 A and B had the same exponent, and B had the larger
617 significand. We need to swap the sign and negate the
620 neg_significand (r
, r
);
625 if (add_significands (r
, a
, b
))
627 /* We got carry out of the addition. This means we need to
628 shift the significand back down one bit and increase the
630 inexact
|= sticky_rshift_significand (r
, r
, 1);
631 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
642 SET_REAL_EXP (r
, exp
);
643 /* Zero out the remaining fields. */
648 /* Re-normalize the result. */
651 /* Special case: if the subtraction results in zero, the result
653 if (r
->cl
== rvc_zero
)
656 r
->sig
[0] |= inexact
;
661 /* Calculate R = A * B. Return true if the result may be inexact. */
664 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
665 const REAL_VALUE_TYPE
*b
)
667 REAL_VALUE_TYPE u
, t
, *rr
;
668 unsigned int i
, j
, k
;
669 int sign
= a
->sign
^ b
->sign
;
670 bool inexact
= false;
672 switch (CLASS2 (a
->cl
, b
->cl
))
674 case CLASS2 (rvc_zero
, rvc_zero
):
675 case CLASS2 (rvc_zero
, rvc_normal
):
676 case CLASS2 (rvc_normal
, rvc_zero
):
677 /* +-0 * ANY = 0 with appropriate sign. */
681 case CLASS2 (rvc_zero
, rvc_nan
):
682 case CLASS2 (rvc_normal
, rvc_nan
):
683 case CLASS2 (rvc_inf
, rvc_nan
):
684 case CLASS2 (rvc_nan
, rvc_nan
):
685 /* ANY * NaN = NaN. */
687 /* Make resulting NaN value to be qNaN. The caller has the
688 responsibility to avoid the operation if flag_signaling_nans
694 case CLASS2 (rvc_nan
, rvc_zero
):
695 case CLASS2 (rvc_nan
, rvc_normal
):
696 case CLASS2 (rvc_nan
, rvc_inf
):
697 /* NaN * ANY = NaN. */
699 /* Make resulting NaN value to be qNaN. The caller has the
700 responsibility to avoid the operation if flag_signaling_nans
706 case CLASS2 (rvc_zero
, rvc_inf
):
707 case CLASS2 (rvc_inf
, rvc_zero
):
709 get_canonical_qnan (r
, sign
);
712 case CLASS2 (rvc_inf
, rvc_inf
):
713 case CLASS2 (rvc_normal
, rvc_inf
):
714 case CLASS2 (rvc_inf
, rvc_normal
):
715 /* Inf * Inf = Inf, R * Inf = Inf */
719 case CLASS2 (rvc_normal
, rvc_normal
):
726 if (r
== a
|| r
== b
)
732 /* Collect all the partial products. Since we don't have sure access
733 to a widening multiply, we split each long into two half-words.
735 Consider the long-hand form of a four half-word multiplication:
745 We construct partial products of the widened half-word products
746 that are known to not overlap, e.g. DF+DH. Each such partial
747 product is given its proper exponent, which allows us to sum them
748 and obtain the finished product. */
750 for (i
= 0; i
< SIGSZ
* 2; ++i
)
752 unsigned long ai
= a
->sig
[i
/ 2];
754 ai
>>= HOST_BITS_PER_LONG
/ 2;
756 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
761 for (j
= 0; j
< 2; ++j
)
763 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
764 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
773 /* Would underflow to zero, which we shouldn't bother adding. */
778 memset (&u
, 0, sizeof (u
));
780 SET_REAL_EXP (&u
, exp
);
782 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
784 unsigned long bi
= b
->sig
[k
/ 2];
786 bi
>>= HOST_BITS_PER_LONG
/ 2;
788 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
790 u
.sig
[k
/ 2] = ai
* bi
;
794 inexact
|= do_add (rr
, rr
, &u
, 0);
805 /* Calculate R = A / B. Return true if the result may be inexact. */
808 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
809 const REAL_VALUE_TYPE
*b
)
811 int exp
, sign
= a
->sign
^ b
->sign
;
812 REAL_VALUE_TYPE t
, *rr
;
815 switch (CLASS2 (a
->cl
, b
->cl
))
817 case CLASS2 (rvc_zero
, rvc_zero
):
819 case CLASS2 (rvc_inf
, rvc_inf
):
820 /* Inf / Inf = NaN. */
821 get_canonical_qnan (r
, sign
);
824 case CLASS2 (rvc_zero
, rvc_normal
):
825 case CLASS2 (rvc_zero
, rvc_inf
):
827 case CLASS2 (rvc_normal
, rvc_inf
):
832 case CLASS2 (rvc_normal
, rvc_zero
):
834 case CLASS2 (rvc_inf
, rvc_zero
):
839 case CLASS2 (rvc_zero
, rvc_nan
):
840 case CLASS2 (rvc_normal
, rvc_nan
):
841 case CLASS2 (rvc_inf
, rvc_nan
):
842 case CLASS2 (rvc_nan
, rvc_nan
):
843 /* ANY / NaN = NaN. */
845 /* Make resulting NaN value to be qNaN. The caller has the
846 responsibility to avoid the operation if flag_signaling_nans
852 case CLASS2 (rvc_nan
, rvc_zero
):
853 case CLASS2 (rvc_nan
, rvc_normal
):
854 case CLASS2 (rvc_nan
, rvc_inf
):
855 /* NaN / ANY = NaN. */
857 /* Make resulting NaN value to be qNaN. The caller has the
858 responsibility to avoid the operation if flag_signaling_nans
864 case CLASS2 (rvc_inf
, rvc_normal
):
869 case CLASS2 (rvc_normal
, rvc_normal
):
876 if (r
== a
|| r
== b
)
881 /* Make sure all fields in the result are initialized. */
886 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
897 SET_REAL_EXP (rr
, exp
);
899 inexact
= div_significands (rr
, a
, b
);
901 /* Re-normalize the result. */
903 rr
->sig
[0] |= inexact
;
911 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
912 one of the two operands is a NaN. */
915 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
920 switch (CLASS2 (a
->cl
, b
->cl
))
922 case CLASS2 (rvc_zero
, rvc_zero
):
923 /* Sign of zero doesn't matter for compares. */
926 case CLASS2 (rvc_normal
, rvc_zero
):
927 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
929 return decimal_do_compare (a
, b
, nan_result
);
931 case CLASS2 (rvc_inf
, rvc_zero
):
932 case CLASS2 (rvc_inf
, rvc_normal
):
933 return (a
->sign
? -1 : 1);
935 case CLASS2 (rvc_inf
, rvc_inf
):
936 return -a
->sign
- -b
->sign
;
938 case CLASS2 (rvc_zero
, rvc_normal
):
939 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
941 return decimal_do_compare (a
, b
, nan_result
);
943 case CLASS2 (rvc_zero
, rvc_inf
):
944 case CLASS2 (rvc_normal
, rvc_inf
):
945 return (b
->sign
? 1 : -1);
947 case CLASS2 (rvc_zero
, rvc_nan
):
948 case CLASS2 (rvc_normal
, rvc_nan
):
949 case CLASS2 (rvc_inf
, rvc_nan
):
950 case CLASS2 (rvc_nan
, rvc_nan
):
951 case CLASS2 (rvc_nan
, rvc_zero
):
952 case CLASS2 (rvc_nan
, rvc_normal
):
953 case CLASS2 (rvc_nan
, rvc_inf
):
956 case CLASS2 (rvc_normal
, rvc_normal
):
963 if (a
->sign
!= b
->sign
)
964 return -a
->sign
- -b
->sign
;
966 if (a
->decimal
|| b
->decimal
)
967 return decimal_do_compare (a
, b
, nan_result
);
969 if (REAL_EXP (a
) > REAL_EXP (b
))
971 else if (REAL_EXP (a
) < REAL_EXP (b
))
974 ret
= cmp_significands (a
, b
);
976 return (a
->sign
? -ret
: ret
);
979 /* Return A truncated to an integral value toward zero. */
982 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
991 /* Make resulting NaN value to be qNaN. The caller has the
992 responsibility to avoid the operation if flag_signaling_nans
1000 decimal_do_fix_trunc (r
, a
);
1003 if (REAL_EXP (r
) <= 0)
1004 get_zero (r
, r
->sign
);
1005 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
1006 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
1014 /* Perform the binary or unary operation described by CODE.
1015 For a unary operation, leave OP1 NULL. This function returns
1016 true if the result may be inexact due to loss of precision. */
1019 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
1020 const REAL_VALUE_TYPE
*op1
)
1022 enum tree_code code
= (enum tree_code
) icode
;
1024 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1025 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1030 /* Clear any padding areas in *r if it isn't equal to one of the
1031 operands so that we can later do bitwise comparisons later on. */
1032 if (r
!= op0
&& r
!= op1
)
1033 memset (r
, '\0', sizeof (*r
));
1034 return do_add (r
, op0
, op1
, 0);
1037 if (r
!= op0
&& r
!= op1
)
1038 memset (r
, '\0', sizeof (*r
));
1039 return do_add (r
, op0
, op1
, 1);
1042 if (r
!= op0
&& r
!= op1
)
1043 memset (r
, '\0', sizeof (*r
));
1044 return do_multiply (r
, op0
, op1
);
1047 if (r
!= op0
&& r
!= op1
)
1048 memset (r
, '\0', sizeof (*r
));
1049 return do_divide (r
, op0
, op1
);
1052 if (op1
->cl
== rvc_nan
)
1055 /* Make resulting NaN value to be qNaN. The caller has the
1056 responsibility to avoid the operation if flag_signaling_nans
1060 else if (do_compare (op0
, op1
, -1) < 0)
1067 if (op1
->cl
== rvc_nan
)
1070 /* Make resulting NaN value to be qNaN. The caller has the
1071 responsibility to avoid the operation if flag_signaling_nans
1075 else if (do_compare (op0
, op1
, 1) < 0)
1091 case FIX_TRUNC_EXPR
:
1092 do_fix_trunc (r
, op0
);
1102 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1105 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1110 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1113 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1117 /* Return whether OP0 == OP1. */
1120 real_equal (const REAL_VALUE_TYPE
*op0
, const REAL_VALUE_TYPE
*op1
)
1122 return do_compare (op0
, op1
, -1) == 0;
1125 /* Return whether OP0 < OP1. */
1128 real_less (const REAL_VALUE_TYPE
*op0
, const REAL_VALUE_TYPE
*op1
)
1130 return do_compare (op0
, op1
, 1) < 0;
1134 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1135 const REAL_VALUE_TYPE
*op1
)
1137 enum tree_code code
= (enum tree_code
) icode
;
1142 return real_less (op0
, op1
);
1144 return do_compare (op0
, op1
, 1) <= 0;
1146 return do_compare (op0
, op1
, -1) > 0;
1148 return do_compare (op0
, op1
, -1) >= 0;
1150 return real_equal (op0
, op1
);
1152 return do_compare (op0
, op1
, -1) != 0;
1153 case UNORDERED_EXPR
:
1154 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1156 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1158 return do_compare (op0
, op1
, -1) < 0;
1160 return do_compare (op0
, op1
, -1) <= 0;
1162 return do_compare (op0
, op1
, 1) > 0;
1164 return do_compare (op0
, op1
, 1) >= 0;
1166 return do_compare (op0
, op1
, 0) == 0;
1168 return do_compare (op0
, op1
, 0) != 0;
1175 /* Return floor log2(R). */
1178 real_exponent (const REAL_VALUE_TYPE
*r
)
1186 return (unsigned int)-1 >> 1;
1188 return REAL_EXP (r
);
1194 /* R = OP0 * 2**EXP. */
1197 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1205 /* Make resulting NaN value to be qNaN. The caller has the
1206 responsibility to avoid the operation if flag_signaling_nans
1212 exp
+= REAL_EXP (op0
);
1214 get_inf (r
, r
->sign
);
1215 else if (exp
< -MAX_EXP
)
1216 get_zero (r
, r
->sign
);
1218 SET_REAL_EXP (r
, exp
);
1226 /* Determine whether a floating-point value X is infinite. */
1229 real_isinf (const REAL_VALUE_TYPE
*r
)
1231 return (r
->cl
== rvc_inf
);
1234 /* Determine whether a floating-point value X is a NaN. */
1237 real_isnan (const REAL_VALUE_TYPE
*r
)
1239 return (r
->cl
== rvc_nan
);
1242 /* Determine whether a floating-point value X is a signaling NaN. */
1243 bool real_issignaling_nan (const REAL_VALUE_TYPE
*r
)
1245 return real_isnan (r
) && r
->signalling
;
1248 /* Determine whether a floating-point value X is finite. */
1251 real_isfinite (const REAL_VALUE_TYPE
*r
)
1253 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1256 /* Determine whether a floating-point value X is negative. */
1259 real_isneg (const REAL_VALUE_TYPE
*r
)
1264 /* Determine whether a floating-point value X is minus zero. */
1267 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1269 return r
->sign
&& r
->cl
== rvc_zero
;
1272 /* Compare two floating-point objects for bitwise identity. */
1275 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1281 if (a
->sign
!= b
->sign
)
1291 if (a
->decimal
!= b
->decimal
)
1293 if (REAL_EXP (a
) != REAL_EXP (b
))
1298 if (a
->signalling
!= b
->signalling
)
1300 /* The significand is ignored for canonical NaNs. */
1301 if (a
->canonical
|| b
->canonical
)
1302 return a
->canonical
== b
->canonical
;
1309 for (i
= 0; i
< SIGSZ
; ++i
)
1310 if (a
->sig
[i
] != b
->sig
[i
])
1316 /* Try to change R into its exact multiplicative inverse in format FMT.
1317 Return true if successful. */
1320 exact_real_inverse (format_helper fmt
, REAL_VALUE_TYPE
*r
)
1322 const REAL_VALUE_TYPE
*one
= real_digit (1);
1326 if (r
->cl
!= rvc_normal
)
1329 /* Check for a power of two: all significand bits zero except the MSB. */
1330 for (i
= 0; i
< SIGSZ
-1; ++i
)
1333 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1336 /* Find the inverse and truncate to the required format. */
1337 do_divide (&u
, one
, r
);
1338 real_convert (&u
, fmt
, &u
);
1340 /* The rounding may have overflowed. */
1341 if (u
.cl
!= rvc_normal
)
1343 for (i
= 0; i
< SIGSZ
-1; ++i
)
1346 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1353 /* Return true if arithmetic on values in IMODE that were promoted
1354 from values in TMODE is equivalent to direct arithmetic on values
1358 real_can_shorten_arithmetic (machine_mode imode
, machine_mode tmode
)
1360 const struct real_format
*tfmt
, *ifmt
;
1361 tfmt
= REAL_MODE_FORMAT (tmode
);
1362 ifmt
= REAL_MODE_FORMAT (imode
);
1363 /* These conditions are conservative rather than trying to catch the
1364 exact boundary conditions; the main case to allow is IEEE float
1366 return (ifmt
->b
== tfmt
->b
1367 && ifmt
->p
> 2 * tfmt
->p
1368 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1369 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1370 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1371 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1372 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1373 && (ifmt
->has_sign_dependent_rounding
1374 == tfmt
->has_sign_dependent_rounding
)
1375 && ifmt
->has_nans
>= tfmt
->has_nans
1376 && ifmt
->has_inf
>= tfmt
->has_inf
1377 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1378 && !MODE_COMPOSITE_P (tmode
)
1379 && !MODE_COMPOSITE_P (imode
));
1382 /* Render R as an integer. */
1385 real_to_integer (const REAL_VALUE_TYPE
*r
)
1387 unsigned HOST_WIDE_INT i
;
1398 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1405 return decimal_real_to_integer (r
);
1407 if (REAL_EXP (r
) <= 0)
1409 /* Only force overflow for unsigned overflow. Signed overflow is
1410 undefined, so it doesn't matter what we return, and some callers
1411 expect to be able to use this routine for both signed and
1412 unsigned conversions. */
1413 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1416 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1417 i
= r
->sig
[SIGSZ
-1];
1420 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1421 i
= r
->sig
[SIGSZ
-1];
1422 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1423 i
|= r
->sig
[SIGSZ
-2];
1426 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1437 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1438 be represented in precision, *FAIL is set to TRUE. */
1441 real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
1443 HOST_WIDE_INT val
[2 * WIDE_INT_MAX_ELTS
];
1452 return wi::zero (precision
);
1460 return wi::set_bit_in_zero (precision
- 1, precision
);
1462 return ~wi::set_bit_in_zero (precision
- 1, precision
);
1466 return decimal_real_to_integer (r
, fail
, precision
);
1471 /* Only force overflow for unsigned overflow. Signed overflow is
1472 undefined, so it doesn't matter what we return, and some callers
1473 expect to be able to use this routine for both signed and
1474 unsigned conversions. */
1475 if (exp
> precision
)
1478 /* Put the significand into a wide_int that has precision W, which
1479 is the smallest HWI-multiple that has at least PRECISION bits.
1480 This ensures that the top bit of the significand is in the
1481 top bit of the wide_int. */
1482 words
= (precision
+ HOST_BITS_PER_WIDE_INT
- 1) / HOST_BITS_PER_WIDE_INT
;
1483 w
= words
* HOST_BITS_PER_WIDE_INT
;
1485 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1486 for (int i
= 0; i
< words
; i
++)
1488 int j
= SIGSZ
- words
+ i
;
1489 val
[i
] = (j
< 0) ? 0 : r
->sig
[j
];
1492 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1493 for (int i
= 0; i
< words
; i
++)
1495 int j
= SIGSZ
- (words
* 2) + (i
* 2);
1502 val
[i
] |= (unsigned HOST_WIDE_INT
) r
->sig
[j
] << HOST_BITS_PER_LONG
;
1505 /* Shift the value into place and truncate to the desired precision. */
1506 result
= wide_int::from_array (val
, words
, w
);
1507 result
= wi::lrshift (result
, w
- exp
);
1508 result
= wide_int::from (result
, precision
, UNSIGNED
);
1520 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1521 of NUM / DEN. Return the quotient and place the remainder in NUM.
1522 It is expected that NUM / DEN are close enough that the quotient is
1525 static unsigned long
1526 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1528 unsigned long q
, msb
;
1529 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1538 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1540 lshift_significand_1 (num
, num
);
1542 if (msb
|| cmp_significands (num
, den
) >= 0)
1544 sub_significands (num
, num
, den
, 0);
1548 while (--expn
>= expd
);
1550 SET_REAL_EXP (num
, expd
);
1556 /* Render R as a decimal floating point constant. Emit DIGITS significant
1557 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1558 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1559 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1560 to a string that, when parsed back in mode MODE, yields the same value. */
1562 #define M_LOG10_2 0.30102999566398119521
1565 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1566 size_t buf_size
, size_t digits
,
1567 int crop_trailing_zeros
, machine_mode mode
)
1569 const struct real_format
*fmt
= NULL
;
1570 const REAL_VALUE_TYPE
*one
, *ten
;
1571 REAL_VALUE_TYPE r
, pten
, u
, v
;
1572 int dec_exp
, cmp_one
, digit
;
1574 char *p
, *first
, *last
;
1578 if (mode
!= VOIDmode
)
1580 fmt
= REAL_MODE_FORMAT (mode
);
1588 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1593 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1596 /* ??? Print the significand as well, if not canonical? */
1597 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1598 (r_orig
->signalling
? 'S' : 'Q'));
1606 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1610 /* Bound the number of digits printed by the size of the representation. */
1611 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1612 if (digits
== 0 || digits
> max_digits
)
1613 digits
= max_digits
;
1615 /* Estimate the decimal exponent, and compute the length of the string it
1616 will print as. Be conservative and add one to account for possible
1617 overflow or rounding error. */
1618 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1619 for (max_digits
= 1; dec_exp
; max_digits
++)
1622 /* Bound the number of digits printed by the size of the output buffer. */
1623 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1624 gcc_assert (max_digits
<= buf_size
);
1625 if (digits
> max_digits
)
1626 digits
= max_digits
;
1628 one
= real_digit (1);
1629 ten
= ten_to_ptwo (0);
1637 cmp_one
= do_compare (&r
, one
, 0);
1642 /* Number is greater than one. Convert significand to an integer
1643 and strip trailing decimal zeros. */
1646 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1648 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1649 m
= floor_log2 (max_digits
);
1651 /* Iterate over the bits of the possible powers of 10 that might
1652 be present in U and eliminate them. That is, if we find that
1653 10**2**M divides U evenly, keep the division and increase
1659 do_divide (&t
, &u
, ten_to_ptwo (m
));
1660 do_fix_trunc (&v
, &t
);
1661 if (cmp_significands (&v
, &t
) == 0)
1669 /* Revert the scaling to integer that we performed earlier. */
1670 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1671 - (SIGNIFICAND_BITS
- 1));
1674 /* Find power of 10. Do this by dividing out 10**2**M when
1675 this is larger than the current remainder. Fill PTEN with
1676 the power of 10 that we compute. */
1677 if (REAL_EXP (&r
) > 0)
1679 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1682 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1683 if (do_compare (&u
, ptentwo
, 0) >= 0)
1685 do_divide (&u
, &u
, ptentwo
);
1686 do_multiply (&pten
, &pten
, ptentwo
);
1693 /* We managed to divide off enough tens in the above reduction
1694 loop that we've now got a negative exponent. Fall into the
1695 less-than-one code to compute the proper value for PTEN. */
1702 /* Number is less than one. Pad significand with leading
1708 /* Stop if we'd shift bits off the bottom. */
1712 do_multiply (&u
, &v
, ten
);
1714 /* Stop if we're now >= 1. */
1715 if (REAL_EXP (&u
) > 0)
1723 /* Find power of 10. Do this by multiplying in P=10**2**M when
1724 the current remainder is smaller than 1/P. Fill PTEN with the
1725 power of 10 that we compute. */
1726 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1729 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1730 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1732 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1734 do_multiply (&v
, &v
, ptentwo
);
1735 do_multiply (&pten
, &pten
, ptentwo
);
1741 /* Invert the positive power of 10 that we've collected so far. */
1742 do_divide (&pten
, one
, &pten
);
1750 /* At this point, PTEN should contain the nearest power of 10 smaller
1751 than R, such that this division produces the first digit.
1753 Using a divide-step primitive that returns the complete integral
1754 remainder avoids the rounding error that would be produced if
1755 we were to use do_divide here and then simply multiply by 10 for
1756 each subsequent digit. */
1758 digit
= rtd_divmod (&r
, &pten
);
1760 /* Be prepared for error in that division via underflow ... */
1761 if (digit
== 0 && cmp_significand_0 (&r
))
1763 /* Multiply by 10 and try again. */
1764 do_multiply (&r
, &r
, ten
);
1765 digit
= rtd_divmod (&r
, &pten
);
1767 gcc_assert (digit
!= 0);
1770 /* ... or overflow. */
1780 gcc_assert (digit
<= 10);
1784 /* Generate subsequent digits. */
1785 while (--digits
> 0)
1787 do_multiply (&r
, &r
, ten
);
1788 digit
= rtd_divmod (&r
, &pten
);
1793 /* Generate one more digit with which to do rounding. */
1794 do_multiply (&r
, &r
, ten
);
1795 digit
= rtd_divmod (&r
, &pten
);
1797 /* Round the result. */
1798 if (fmt
&& fmt
->round_towards_zero
)
1800 /* If the format uses round towards zero when parsing the string
1801 back in, we need to always round away from zero here. */
1802 if (cmp_significand_0 (&r
))
1804 round_up
= digit
> 0;
1810 /* Round to nearest. If R is nonzero there are additional
1811 nonzero digits to be extracted. */
1812 if (cmp_significand_0 (&r
))
1814 /* Round to even. */
1815 else if ((p
[-1] - '0') & 1)
1819 round_up
= digit
> 5;
1836 /* Carry out of the first digit. This means we had all 9's and
1837 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1845 /* Insert the decimal point. */
1846 first
[0] = first
[1];
1849 /* If requested, drop trailing zeros. Never crop past "1.0". */
1850 if (crop_trailing_zeros
)
1851 while (last
> first
+ 3 && last
[-1] == '0')
1854 /* Append the exponent. */
1855 sprintf (last
, "e%+d", dec_exp
);
1857 /* Verify that we can read the original value back in. */
1858 if (flag_checking
&& mode
!= VOIDmode
)
1860 real_from_string (&r
, str
);
1861 real_convert (&r
, mode
, &r
);
1862 gcc_assert (real_identical (&r
, r_orig
));
1866 /* Likewise, except always uses round-to-nearest. */
1869 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1870 size_t digits
, int crop_trailing_zeros
)
1872 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1873 digits
, crop_trailing_zeros
, VOIDmode
);
1876 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1877 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1878 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1879 strip trailing zeros. */
1882 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1883 size_t digits
, int crop_trailing_zeros
)
1885 int i
, j
, exp
= REAL_EXP (r
);
1898 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1901 /* ??? Print the significand as well, if not canonical? */
1902 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1903 (r
->signalling
? 'S' : 'Q'));
1911 /* Hexadecimal format for decimal floats is not interesting. */
1912 strcpy (str
, "N/A");
1917 digits
= SIGNIFICAND_BITS
/ 4;
1919 /* Bound the number of digits printed by the size of the output buffer. */
1921 sprintf (exp_buf
, "p%+d", exp
);
1922 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1923 gcc_assert (max_digits
<= buf_size
);
1924 if (digits
> max_digits
)
1925 digits
= max_digits
;
1936 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1937 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1939 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1945 if (crop_trailing_zeros
)
1946 while (p
> first
+ 1 && p
[-1] == '0')
1949 sprintf (p
, "p%+d", exp
);
1952 /* Initialize R from a decimal or hexadecimal string. The string is
1953 assumed to have been syntax checked already. Return -1 if the
1954 value underflows, +1 if overflows, and 0 otherwise. */
1957 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1969 else if (*str
== '+')
1972 if (!strncmp (str
, "QNaN", 4))
1974 get_canonical_qnan (r
, sign
);
1977 else if (!strncmp (str
, "SNaN", 4))
1979 get_canonical_snan (r
, sign
);
1982 else if (!strncmp (str
, "Inf", 3))
1988 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1990 /* Hexadecimal floating point. */
1991 int pos
= SIGNIFICAND_BITS
- 4, d
;
1999 d
= hex_value (*str
);
2004 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
2005 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
2009 /* Ensure correct rounding by setting last bit if there is
2010 a subsequent nonzero digit. */
2018 if (pos
== SIGNIFICAND_BITS
- 4)
2025 d
= hex_value (*str
);
2030 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
2031 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
2035 /* Ensure correct rounding by setting last bit if there is
2036 a subsequent nonzero digit. */
2042 /* If the mantissa is zero, ignore the exponent. */
2043 if (!cmp_significand_0 (r
))
2046 if (*str
== 'p' || *str
== 'P')
2048 bool exp_neg
= false;
2056 else if (*str
== '+')
2060 while (ISDIGIT (*str
))
2066 /* Overflowed the exponent. */
2081 SET_REAL_EXP (r
, exp
);
2087 /* Decimal floating point. */
2088 const char *cstr
= str
;
2092 while (*cstr
== '0')
2097 while (*cstr
== '0')
2101 /* If the mantissa is zero, ignore the exponent. */
2102 if (!ISDIGIT (*cstr
))
2105 /* Nonzero value, possibly overflowing or underflowing. */
2106 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2107 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, GMP_RNDZ
);
2108 /* The result should never be a NaN, and because the rounding is
2109 toward zero should never be an infinity. */
2110 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2111 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2116 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2123 real_from_mpfr (r
, m
, NULL_TREE
, GMP_RNDZ
);
2124 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2125 because the hex digits used in real_from_mpfr did not
2126 start with a digit 8 to f, but the exponent bounds above
2127 should have avoided underflow or overflow. */
2128 gcc_assert (r
->cl
== rvc_normal
);
2129 /* Set a sticky bit if mpfr_strtofr was inexact. */
2130 r
->sig
[0] |= inexact
;
2151 /* Legacy. Similar, but return the result directly. */
2154 real_from_string2 (const char *s
, format_helper fmt
)
2158 real_from_string (&r
, s
);
2160 real_convert (&r
, fmt
, &r
);
2165 /* Initialize R from string S and desired format FMT. */
2168 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, format_helper fmt
)
2170 if (fmt
.decimal_p ())
2171 decimal_real_from_string (r
, s
);
2173 real_from_string (r
, s
);
2176 real_convert (r
, fmt
, r
);
2179 /* Initialize R from the wide_int VAL_IN. Round it to format FMT if
2183 real_from_integer (REAL_VALUE_TYPE
*r
, format_helper fmt
,
2184 const wide_int_ref
&val_in
, signop sgn
)
2190 unsigned int len
= val_in
.get_precision ();
2192 int maxbitlen
= MAX_BITSIZE_MODE_ANY_INT
+ HOST_BITS_PER_WIDE_INT
;
2193 const unsigned int realmax
= (SIGNIFICAND_BITS
/ HOST_BITS_PER_WIDE_INT
2194 * HOST_BITS_PER_WIDE_INT
);
2196 memset (r
, 0, sizeof (*r
));
2198 r
->sign
= wi::neg_p (val_in
, sgn
);
2200 /* We have to ensure we can negate the largest negative number. */
2201 wide_int val
= wide_int::from (val_in
, maxbitlen
, sgn
);
2206 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2207 won't work with precisions that are not a multiple of
2208 HOST_BITS_PER_WIDE_INT. */
2209 len
+= HOST_BITS_PER_WIDE_INT
- 1;
2211 /* Ensure we can represent the largest negative number. */
2214 len
= len
/HOST_BITS_PER_WIDE_INT
* HOST_BITS_PER_WIDE_INT
;
2216 /* Cap the size to the size allowed by real.h. */
2219 HOST_WIDE_INT cnt_l_z
;
2220 cnt_l_z
= wi::clz (val
);
2222 if (maxbitlen
- cnt_l_z
> realmax
)
2224 e
= maxbitlen
- cnt_l_z
- realmax
;
2226 /* This value is too large, we must shift it right to
2227 preserve all the bits we can, and then bump the
2228 exponent up by that amount. */
2229 val
= wi::lrshift (val
, e
);
2234 /* Clear out top bits so elt will work with precisions that aren't
2235 a multiple of HOST_BITS_PER_WIDE_INT. */
2236 val
= wide_int::from (val
, len
, sgn
);
2237 len
= len
/ HOST_BITS_PER_WIDE_INT
;
2239 SET_REAL_EXP (r
, len
* HOST_BITS_PER_WIDE_INT
+ e
);
2242 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2243 for (i
= len
- 1; i
>= 0; i
--)
2245 r
->sig
[j
--] = val
.elt (i
);
2251 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2252 for (i
= len
- 1; i
>= 0; i
--)
2254 HOST_WIDE_INT e
= val
.elt (i
);
2255 r
->sig
[j
--] = e
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2267 if (fmt
.decimal_p ())
2268 decimal_from_integer (r
);
2270 real_convert (r
, fmt
, r
);
2273 /* Render R, an integral value, as a floating point constant with no
2274 specified exponent. */
2277 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2280 int dec_exp
, digit
, digits
;
2281 REAL_VALUE_TYPE r
, pten
;
2287 if (r
.cl
== rvc_zero
)
2296 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2297 digits
= dec_exp
+ 1;
2298 gcc_assert ((digits
+ 2) < (int)buf_size
);
2300 pten
= *real_digit (1);
2301 times_pten (&pten
, dec_exp
);
2307 digit
= rtd_divmod (&r
, &pten
);
2308 gcc_assert (digit
>= 0 && digit
<= 9);
2310 while (--digits
> 0)
2313 digit
= rtd_divmod (&r
, &pten
);
2320 /* Convert a real with an integral value to decimal float. */
2323 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2327 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2328 decimal_real_from_string (r
, str
);
2331 /* Returns 10**2**N. */
2333 static const REAL_VALUE_TYPE
*
2336 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2338 gcc_assert (n
>= 0);
2339 gcc_assert (n
< EXP_BITS
);
2341 if (tens
[n
].cl
== rvc_zero
)
2343 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2345 HOST_WIDE_INT t
= 10;
2348 for (i
= 0; i
< n
; ++i
)
2351 real_from_integer (&tens
[n
], VOIDmode
, t
, UNSIGNED
);
2355 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2356 do_multiply (&tens
[n
], t
, t
);
2363 /* Returns 10**(-2**N). */
2365 static const REAL_VALUE_TYPE
*
2366 ten_to_mptwo (int n
)
2368 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2370 gcc_assert (n
>= 0);
2371 gcc_assert (n
< EXP_BITS
);
2373 if (tens
[n
].cl
== rvc_zero
)
2374 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2381 static const REAL_VALUE_TYPE
*
2384 static REAL_VALUE_TYPE num
[10];
2386 gcc_assert (n
>= 0);
2387 gcc_assert (n
<= 9);
2389 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2390 real_from_integer (&num
[n
], VOIDmode
, n
, UNSIGNED
);
2395 /* Multiply R by 10**EXP. */
2398 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2400 REAL_VALUE_TYPE pten
, *rr
;
2401 bool negative
= (exp
< 0);
2407 pten
= *real_digit (1);
2413 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2415 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2418 do_divide (r
, r
, &pten
);
2421 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2423 const REAL_VALUE_TYPE
*
2426 static REAL_VALUE_TYPE value
;
2428 /* Initialize mathematical constants for constant folding builtins.
2429 These constants need to be given to at least 160 bits precision. */
2430 if (value
.cl
== rvc_zero
)
2433 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2434 mpfr_set_ui (m
, 1, GMP_RNDN
);
2435 mpfr_exp (m
, m
, GMP_RNDN
);
2436 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2443 /* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2445 #define CACHED_FRACTION(NAME, N) \
2446 const REAL_VALUE_TYPE * \
2449 static REAL_VALUE_TYPE value; \
2451 /* Initialize mathematical constants for constant folding builtins. \
2452 These constants need to be given to at least 160 bits \
2454 if (value.cl == rvc_zero) \
2455 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2459 CACHED_FRACTION (dconst_third_ptr
, 3)
2460 CACHED_FRACTION (dconst_quarter_ptr
, 4)
2461 CACHED_FRACTION (dconst_sixth_ptr
, 6)
2462 CACHED_FRACTION (dconst_ninth_ptr
, 9)
2464 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2466 const REAL_VALUE_TYPE
*
2467 dconst_sqrt2_ptr (void)
2469 static REAL_VALUE_TYPE value
;
2471 /* Initialize mathematical constants for constant folding builtins.
2472 These constants need to be given to at least 160 bits precision. */
2473 if (value
.cl
== rvc_zero
)
2476 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2477 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2478 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2484 /* Fills R with +Inf. */
2487 real_inf (REAL_VALUE_TYPE
*r
)
2492 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2493 we force a QNaN, else we force an SNaN. The string, if not empty,
2494 is parsed as a number and placed in the significand. Return true
2495 if the string was successfully parsed. */
2498 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2504 get_canonical_qnan (r
, 0);
2506 get_canonical_snan (r
, 0);
2512 memset (r
, 0, sizeof (*r
));
2515 /* Parse akin to strtol into the significand of R. */
2517 while (ISSPACE (*str
))
2521 else if (*str
== '+')
2526 if (*str
== 'x' || *str
== 'X')
2535 while ((d
= hex_value (*str
)) < base
)
2542 lshift_significand (r
, r
, 3);
2545 lshift_significand (r
, r
, 4);
2548 lshift_significand_1 (&u
, r
);
2549 lshift_significand (r
, r
, 3);
2550 add_significands (r
, r
, &u
);
2558 add_significands (r
, r
, &u
);
2563 /* Must have consumed the entire string for success. */
2567 /* Shift the significand into place such that the bits
2568 are in the most significant bits for the format. */
2569 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2571 /* Our MSB is always unset for NaNs. */
2572 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2574 /* Force quiet or signaling NaN. */
2575 r
->signalling
= !quiet
;
2581 /* Fills R with the largest finite value representable in mode MODE.
2582 If SIGN is nonzero, R is set to the most negative finite value. */
2585 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
2587 const struct real_format
*fmt
;
2590 fmt
= REAL_MODE_FORMAT (mode
);
2592 memset (r
, 0, sizeof (*r
));
2595 decimal_real_maxval (r
, sign
, mode
);
2600 SET_REAL_EXP (r
, fmt
->emax
);
2602 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2603 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2604 clear_significand_below (r
, np2
);
2606 if (fmt
->pnan
< fmt
->p
)
2607 /* This is an IBM extended double format made up of two IEEE
2608 doubles. The value of the long double is the sum of the
2609 values of the two parts. The most significant part is
2610 required to be the value of the long double rounded to the
2611 nearest double. Rounding means we need a slightly smaller
2612 value for LDBL_MAX. */
2613 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2617 /* Fills R with 2**N. */
2620 real_2expN (REAL_VALUE_TYPE
*r
, int n
, format_helper fmt
)
2622 memset (r
, 0, sizeof (*r
));
2627 else if (n
< -MAX_EXP
)
2632 SET_REAL_EXP (r
, n
);
2633 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2635 if (fmt
.decimal_p ())
2636 decimal_real_convert (r
, fmt
, r
);
2641 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2645 bool round_up
= false;
2651 decimal_round_for_format (fmt
, r
);
2654 /* FIXME. We can come here via fp_easy_constant
2655 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2656 investigated whether this convert needs to be here, or
2657 something else is missing. */
2658 decimal_real_convert (r
, REAL_MODE_FORMAT (DFmode
), r
);
2662 emin2m1
= fmt
->emin
- 1;
2665 np2
= SIGNIFICAND_BITS
- p2
;
2669 get_zero (r
, r
->sign
);
2671 if (!fmt
->has_signed_zero
)
2676 get_inf (r
, r
->sign
);
2681 clear_significand_below (r
, np2
);
2691 /* Check the range of the exponent. If we're out of range,
2692 either underflow or overflow. */
2693 if (REAL_EXP (r
) > emax2
)
2695 else if (REAL_EXP (r
) <= emin2m1
)
2699 if (!fmt
->has_denorm
)
2701 /* Don't underflow completely until we've had a chance to round. */
2702 if (REAL_EXP (r
) < emin2m1
)
2707 diff
= emin2m1
- REAL_EXP (r
) + 1;
2711 /* De-normalize the significand. */
2712 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2713 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2717 if (!fmt
->round_towards_zero
)
2719 /* There are P2 true significand bits, followed by one guard bit,
2720 followed by one sticky bit, followed by stuff. Fold nonzero
2721 stuff into the sticky bit. */
2722 unsigned long sticky
;
2726 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2727 sticky
|= r
->sig
[i
];
2729 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2731 guard
= test_significand_bit (r
, np2
- 1);
2732 lsb
= test_significand_bit (r
, np2
);
2734 /* Round to even. */
2735 round_up
= guard
&& (sticky
|| lsb
);
2742 set_significand_bit (&u
, np2
);
2744 if (add_significands (r
, r
, &u
))
2746 /* Overflow. Means the significand had been all ones, and
2747 is now all zeros. Need to increase the exponent, and
2748 possibly re-normalize it. */
2749 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2750 if (REAL_EXP (r
) > emax2
)
2752 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2756 /* Catch underflow that we deferred until after rounding. */
2757 if (REAL_EXP (r
) <= emin2m1
)
2760 /* Clear out trailing garbage. */
2761 clear_significand_below (r
, np2
);
2764 /* Extend or truncate to a new format. */
2767 real_convert (REAL_VALUE_TYPE
*r
, format_helper fmt
,
2768 const REAL_VALUE_TYPE
*a
)
2772 if (a
->decimal
|| fmt
->b
== 10)
2773 decimal_real_convert (r
, fmt
, a
);
2775 round_for_format (fmt
, r
);
2777 /* Make resulting NaN value to be qNaN. The caller has the
2778 responsibility to avoid the operation if flag_signaling_nans
2780 if (r
->cl
== rvc_nan
)
2783 /* round_for_format de-normalizes denormals. Undo just that part. */
2784 if (r
->cl
== rvc_normal
)
2788 /* Legacy. Likewise, except return the struct directly. */
2791 real_value_truncate (format_helper fmt
, REAL_VALUE_TYPE a
)
2794 real_convert (&r
, fmt
, &a
);
2798 /* Return true if truncating to FMT is exact. */
2801 exact_real_truncate (format_helper fmt
, const REAL_VALUE_TYPE
*a
)
2806 /* Don't allow conversion to denormals. */
2807 emin2m1
= fmt
->emin
- 1;
2808 if (REAL_EXP (a
) <= emin2m1
)
2811 /* After conversion to the new format, the value must be identical. */
2812 real_convert (&t
, fmt
, a
);
2813 return real_identical (&t
, a
);
2816 /* Write R to the given target format. Place the words of the result
2817 in target word order in BUF. There are always 32 bits in each
2818 long, no matter the size of the host long.
2820 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2823 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2830 round_for_format (fmt
, &r
);
2834 (*fmt
->encode
) (fmt
, buf
, &r
);
2839 /* Read R from the given target format. Read the words of the result
2840 in target word order in BUF. There are always 32 bits in each
2841 long, no matter the size of the host long. */
2844 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, format_helper fmt
)
2846 (*fmt
->decode
) (fmt
, r
, buf
);
2849 /* Return the number of bits of the largest binary value that the
2850 significand of FMT will hold. */
2851 /* ??? Legacy. Should get access to real_format directly. */
2854 significand_size (format_helper fmt
)
2861 /* Return the size in bits of the largest binary value that can be
2862 held by the decimal coefficient for this format. This is one more
2863 than the number of bits required to hold the largest coefficient
2865 double log2_10
= 3.3219281;
2866 return fmt
->p
* log2_10
;
2871 /* Return a hash value for the given real value. */
2872 /* ??? The "unsigned int" return value is intended to be hashval_t,
2873 but I didn't want to pull hashtab.h into real.h. */
2876 real_hash (const REAL_VALUE_TYPE
*r
)
2881 h
= r
->cl
| (r
->sign
<< 2);
2889 h
|= REAL_EXP (r
) << 3;
2894 h
^= (unsigned int)-1;
2903 if (sizeof (unsigned long) > sizeof (unsigned int))
2904 for (i
= 0; i
< SIGSZ
; ++i
)
2906 unsigned long s
= r
->sig
[i
];
2907 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2910 for (i
= 0; i
< SIGSZ
; ++i
)
2916 /* IEEE single-precision format. */
2918 static void encode_ieee_single (const struct real_format
*fmt
,
2919 long *, const REAL_VALUE_TYPE
*);
2920 static void decode_ieee_single (const struct real_format
*,
2921 REAL_VALUE_TYPE
*, const long *);
2924 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2925 const REAL_VALUE_TYPE
*r
)
2927 unsigned long image
, sig
, exp
;
2928 unsigned long sign
= r
->sign
;
2929 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2932 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2943 image
|= 0x7fffffff;
2950 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2951 if (r
->signalling
== fmt
->qnan_msb_set
)
2962 image
|= 0x7fffffff;
2966 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2967 whereas the intermediate representation is 0.F x 2**exp.
2968 Which means we're off by one. */
2972 exp
= REAL_EXP (r
) + 127 - 1;
2985 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2988 unsigned long image
= buf
[0] & 0xffffffff;
2989 bool sign
= (image
>> 31) & 1;
2990 int exp
= (image
>> 23) & 0xff;
2992 memset (r
, 0, sizeof (*r
));
2993 image
<<= HOST_BITS_PER_LONG
- 24;
2998 if (image
&& fmt
->has_denorm
)
3002 SET_REAL_EXP (r
, -126);
3003 r
->sig
[SIGSZ
-1] = image
<< 1;
3006 else if (fmt
->has_signed_zero
)
3009 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
3015 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
3016 ^ fmt
->qnan_msb_set
);
3017 r
->sig
[SIGSZ
-1] = image
;
3029 SET_REAL_EXP (r
, exp
- 127 + 1);
3030 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
3034 const struct real_format ieee_single_format
=
3056 const struct real_format mips_single_format
=
3078 const struct real_format motorola_single_format
=
3100 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3101 single precision with the following differences:
3102 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3104 - NaNs are not supported.
3105 - The range of non-zero numbers in binary is
3106 (001)[1.]000...000 to (255)[1.]111...111.
3107 - Denormals can be represented, but are treated as +0.0 when
3108 used as an operand and are never generated as a result.
3109 - -0.0 can be represented, but a zero result is always +0.0.
3110 - the only supported rounding mode is trunction (towards zero). */
3111 const struct real_format spu_single_format
=
3133 /* IEEE double-precision format. */
3135 static void encode_ieee_double (const struct real_format
*fmt
,
3136 long *, const REAL_VALUE_TYPE
*);
3137 static void decode_ieee_double (const struct real_format
*,
3138 REAL_VALUE_TYPE
*, const long *);
3141 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3142 const REAL_VALUE_TYPE
*r
)
3144 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3145 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3147 image_hi
= r
->sign
<< 31;
3150 if (HOST_BITS_PER_LONG
== 64)
3152 sig_hi
= r
->sig
[SIGSZ
-1];
3153 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3154 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3158 sig_hi
= r
->sig
[SIGSZ
-1];
3159 sig_lo
= r
->sig
[SIGSZ
-2];
3160 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3161 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3171 image_hi
|= 2047 << 20;
3174 image_hi
|= 0x7fffffff;
3175 image_lo
= 0xffffffff;
3184 if (fmt
->canonical_nan_lsbs_set
)
3186 sig_hi
= (1 << 19) - 1;
3187 sig_lo
= 0xffffffff;
3195 if (r
->signalling
== fmt
->qnan_msb_set
)
3196 sig_hi
&= ~(1 << 19);
3199 if (sig_hi
== 0 && sig_lo
== 0)
3202 image_hi
|= 2047 << 20;
3208 image_hi
|= 0x7fffffff;
3209 image_lo
= 0xffffffff;
3214 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3215 whereas the intermediate representation is 0.F x 2**exp.
3216 Which means we're off by one. */
3220 exp
= REAL_EXP (r
) + 1023 - 1;
3221 image_hi
|= exp
<< 20;
3230 if (FLOAT_WORDS_BIG_ENDIAN
)
3231 buf
[0] = image_hi
, buf
[1] = image_lo
;
3233 buf
[0] = image_lo
, buf
[1] = image_hi
;
3237 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3240 unsigned long image_hi
, image_lo
;
3244 if (FLOAT_WORDS_BIG_ENDIAN
)
3245 image_hi
= buf
[0], image_lo
= buf
[1];
3247 image_lo
= buf
[0], image_hi
= buf
[1];
3248 image_lo
&= 0xffffffff;
3249 image_hi
&= 0xffffffff;
3251 sign
= (image_hi
>> 31) & 1;
3252 exp
= (image_hi
>> 20) & 0x7ff;
3254 memset (r
, 0, sizeof (*r
));
3256 image_hi
<<= 32 - 21;
3257 image_hi
|= image_lo
>> 21;
3258 image_hi
&= 0x7fffffff;
3259 image_lo
<<= 32 - 21;
3263 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3267 SET_REAL_EXP (r
, -1022);
3268 if (HOST_BITS_PER_LONG
== 32)
3270 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3272 r
->sig
[SIGSZ
-1] = image_hi
;
3273 r
->sig
[SIGSZ
-2] = image_lo
;
3277 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3278 r
->sig
[SIGSZ
-1] = image_hi
;
3282 else if (fmt
->has_signed_zero
)
3285 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3287 if (image_hi
|| image_lo
)
3291 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3292 if (HOST_BITS_PER_LONG
== 32)
3294 r
->sig
[SIGSZ
-1] = image_hi
;
3295 r
->sig
[SIGSZ
-2] = image_lo
;
3298 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3310 SET_REAL_EXP (r
, exp
- 1023 + 1);
3311 if (HOST_BITS_PER_LONG
== 32)
3313 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3314 r
->sig
[SIGSZ
-2] = image_lo
;
3317 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3321 const struct real_format ieee_double_format
=
3343 const struct real_format mips_double_format
=
3365 const struct real_format motorola_double_format
=
3387 /* IEEE extended real format. This comes in three flavors: Intel's as
3388 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3389 12- and 16-byte images may be big- or little endian; Motorola's is
3390 always big endian. */
3392 /* Helper subroutine which converts from the internal format to the
3393 12-byte little-endian Intel format. Functions below adjust this
3394 for the other possible formats. */
3396 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3397 const REAL_VALUE_TYPE
*r
)
3399 unsigned long image_hi
, sig_hi
, sig_lo
;
3400 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3402 image_hi
= r
->sign
<< 15;
3403 sig_hi
= sig_lo
= 0;
3415 /* Intel requires the explicit integer bit to be set, otherwise
3416 it considers the value a "pseudo-infinity". Motorola docs
3417 say it doesn't care. */
3418 sig_hi
= 0x80000000;
3423 sig_lo
= sig_hi
= 0xffffffff;
3433 if (fmt
->canonical_nan_lsbs_set
)
3435 sig_hi
= (1 << 30) - 1;
3436 sig_lo
= 0xffffffff;
3439 else if (HOST_BITS_PER_LONG
== 32)
3441 sig_hi
= r
->sig
[SIGSZ
-1];
3442 sig_lo
= r
->sig
[SIGSZ
-2];
3446 sig_lo
= r
->sig
[SIGSZ
-1];
3447 sig_hi
= sig_lo
>> 31 >> 1;
3448 sig_lo
&= 0xffffffff;
3450 if (r
->signalling
== fmt
->qnan_msb_set
)
3451 sig_hi
&= ~(1 << 30);
3454 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3457 /* Intel requires the explicit integer bit to be set, otherwise
3458 it considers the value a "pseudo-nan". Motorola docs say it
3460 sig_hi
|= 0x80000000;
3465 sig_lo
= sig_hi
= 0xffffffff;
3471 int exp
= REAL_EXP (r
);
3473 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3474 whereas the intermediate representation is 0.F x 2**exp.
3475 Which means we're off by one.
3477 Except for Motorola, which consider exp=0 and explicit
3478 integer bit set to continue to be normalized. In theory
3479 this discrepancy has been taken care of by the difference
3480 in fmt->emin in round_for_format. */
3487 gcc_assert (exp
>= 0);
3491 if (HOST_BITS_PER_LONG
== 32)
3493 sig_hi
= r
->sig
[SIGSZ
-1];
3494 sig_lo
= r
->sig
[SIGSZ
-2];
3498 sig_lo
= r
->sig
[SIGSZ
-1];
3499 sig_hi
= sig_lo
>> 31 >> 1;
3500 sig_lo
&= 0xffffffff;
3509 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3512 /* Convert from the internal format to the 12-byte Motorola format
3513 for an IEEE extended real. */
3515 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3516 const REAL_VALUE_TYPE
*r
)
3519 encode_ieee_extended (fmt
, intermed
, r
);
3521 if (r
->cl
== rvc_inf
)
3522 /* For infinity clear the explicit integer bit again, so that the
3523 format matches the canonical infinity generated by the FPU. */
3526 /* Motorola chips are assumed always to be big-endian. Also, the
3527 padding in a Motorola extended real goes between the exponent and
3528 the mantissa. At this point the mantissa is entirely within
3529 elements 0 and 1 of intermed, and the exponent entirely within
3530 element 2, so all we have to do is swap the order around, and
3531 shift element 2 left 16 bits. */
3532 buf
[0] = intermed
[2] << 16;
3533 buf
[1] = intermed
[1];
3534 buf
[2] = intermed
[0];
3537 /* Convert from the internal format to the 12-byte Intel format for
3538 an IEEE extended real. */
3540 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3541 const REAL_VALUE_TYPE
*r
)
3543 if (FLOAT_WORDS_BIG_ENDIAN
)
3545 /* All the padding in an Intel-format extended real goes at the high
3546 end, which in this case is after the mantissa, not the exponent.
3547 Therefore we must shift everything down 16 bits. */
3549 encode_ieee_extended (fmt
, intermed
, r
);
3550 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3551 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3552 buf
[2] = (intermed
[0] << 16);
3555 /* encode_ieee_extended produces what we want directly. */
3556 encode_ieee_extended (fmt
, buf
, r
);
3559 /* Convert from the internal format to the 16-byte Intel format for
3560 an IEEE extended real. */
3562 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3563 const REAL_VALUE_TYPE
*r
)
3565 /* All the padding in an Intel-format extended real goes at the high end. */
3566 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3570 /* As above, we have a helper function which converts from 12-byte
3571 little-endian Intel format to internal format. Functions below
3572 adjust for the other possible formats. */
3574 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3577 unsigned long image_hi
, sig_hi
, sig_lo
;
3581 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3582 sig_lo
&= 0xffffffff;
3583 sig_hi
&= 0xffffffff;
3584 image_hi
&= 0xffffffff;
3586 sign
= (image_hi
>> 15) & 1;
3587 exp
= image_hi
& 0x7fff;
3589 memset (r
, 0, sizeof (*r
));
3593 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3598 /* When the IEEE format contains a hidden bit, we know that
3599 it's zero at this point, and so shift up the significand
3600 and decrease the exponent to match. In this case, Motorola
3601 defines the explicit integer bit to be valid, so we don't
3602 know whether the msb is set or not. */
3603 SET_REAL_EXP (r
, fmt
->emin
);
3604 if (HOST_BITS_PER_LONG
== 32)
3606 r
->sig
[SIGSZ
-1] = sig_hi
;
3607 r
->sig
[SIGSZ
-2] = sig_lo
;
3610 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3614 else if (fmt
->has_signed_zero
)
3617 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3619 /* See above re "pseudo-infinities" and "pseudo-nans".
3620 Short summary is that the MSB will likely always be
3621 set, and that we don't care about it. */
3622 sig_hi
&= 0x7fffffff;
3624 if (sig_hi
|| sig_lo
)
3628 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3629 if (HOST_BITS_PER_LONG
== 32)
3631 r
->sig
[SIGSZ
-1] = sig_hi
;
3632 r
->sig
[SIGSZ
-2] = sig_lo
;
3635 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3647 SET_REAL_EXP (r
, exp
- 16383 + 1);
3648 if (HOST_BITS_PER_LONG
== 32)
3650 r
->sig
[SIGSZ
-1] = sig_hi
;
3651 r
->sig
[SIGSZ
-2] = sig_lo
;
3654 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3658 /* Convert from the internal format to the 12-byte Motorola format
3659 for an IEEE extended real. */
3661 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3666 /* Motorola chips are assumed always to be big-endian. Also, the
3667 padding in a Motorola extended real goes between the exponent and
3668 the mantissa; remove it. */
3669 intermed
[0] = buf
[2];
3670 intermed
[1] = buf
[1];
3671 intermed
[2] = (unsigned long)buf
[0] >> 16;
3673 decode_ieee_extended (fmt
, r
, intermed
);
3676 /* Convert from the internal format to the 12-byte Intel format for
3677 an IEEE extended real. */
3679 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3682 if (FLOAT_WORDS_BIG_ENDIAN
)
3684 /* All the padding in an Intel-format extended real goes at the high
3685 end, which in this case is after the mantissa, not the exponent.
3686 Therefore we must shift everything up 16 bits. */
3689 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3690 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3691 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3693 decode_ieee_extended (fmt
, r
, intermed
);
3696 /* decode_ieee_extended produces what we want directly. */
3697 decode_ieee_extended (fmt
, r
, buf
);
3700 /* Convert from the internal format to the 16-byte Intel format for
3701 an IEEE extended real. */
3703 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3706 /* All the padding in an Intel-format extended real goes at the high end. */
3707 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3710 const struct real_format ieee_extended_motorola_format
=
3712 encode_ieee_extended_motorola
,
3713 decode_ieee_extended_motorola
,
3729 "ieee_extended_motorola"
3732 const struct real_format ieee_extended_intel_96_format
=
3734 encode_ieee_extended_intel_96
,
3735 decode_ieee_extended_intel_96
,
3751 "ieee_extended_intel_96"
3754 const struct real_format ieee_extended_intel_128_format
=
3756 encode_ieee_extended_intel_128
,
3757 decode_ieee_extended_intel_128
,
3773 "ieee_extended_intel_128"
3776 /* The following caters to i386 systems that set the rounding precision
3777 to 53 bits instead of 64, e.g. FreeBSD. */
3778 const struct real_format ieee_extended_intel_96_round_53_format
=
3780 encode_ieee_extended_intel_96
,
3781 decode_ieee_extended_intel_96
,
3797 "ieee_extended_intel_96_round_53"
3800 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3801 numbers whose sum is equal to the extended precision value. The number
3802 with greater magnitude is first. This format has the same magnitude
3803 range as an IEEE double precision value, but effectively 106 bits of
3804 significand precision. Infinity and NaN are represented by their IEEE
3805 double precision value stored in the first number, the second number is
3806 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3808 static void encode_ibm_extended (const struct real_format
*fmt
,
3809 long *, const REAL_VALUE_TYPE
*);
3810 static void decode_ibm_extended (const struct real_format
*,
3811 REAL_VALUE_TYPE
*, const long *);
3814 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3815 const REAL_VALUE_TYPE
*r
)
3817 REAL_VALUE_TYPE u
, normr
, v
;
3818 const struct real_format
*base_fmt
;
3820 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3822 /* Renormalize R before doing any arithmetic on it. */
3824 if (normr
.cl
== rvc_normal
)
3827 /* u = IEEE double precision portion of significand. */
3829 round_for_format (base_fmt
, &u
);
3830 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3832 if (u
.cl
== rvc_normal
)
3834 do_add (&v
, &normr
, &u
, 1);
3835 /* Call round_for_format since we might need to denormalize. */
3836 round_for_format (base_fmt
, &v
);
3837 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3841 /* Inf, NaN, 0 are all representable as doubles, so the
3842 least-significant part can be 0.0. */
3849 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3852 REAL_VALUE_TYPE u
, v
;
3853 const struct real_format
*base_fmt
;
3855 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3856 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3858 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3860 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3861 do_add (r
, &u
, &v
, 0);
3867 const struct real_format ibm_extended_format
=
3869 encode_ibm_extended
,
3870 decode_ibm_extended
,
3889 const struct real_format mips_extended_format
=
3891 encode_ibm_extended
,
3892 decode_ibm_extended
,
3912 /* IEEE quad precision format. */
3914 static void encode_ieee_quad (const struct real_format
*fmt
,
3915 long *, const REAL_VALUE_TYPE
*);
3916 static void decode_ieee_quad (const struct real_format
*,
3917 REAL_VALUE_TYPE
*, const long *);
3920 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3921 const REAL_VALUE_TYPE
*r
)
3923 unsigned long image3
, image2
, image1
, image0
, exp
;
3924 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3927 image3
= r
->sign
<< 31;
3932 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3941 image3
|= 32767 << 16;
3944 image3
|= 0x7fffffff;
3945 image2
= 0xffffffff;
3946 image1
= 0xffffffff;
3947 image0
= 0xffffffff;
3954 image3
|= 32767 << 16;
3958 if (fmt
->canonical_nan_lsbs_set
)
3961 image2
= image1
= image0
= 0xffffffff;
3964 else if (HOST_BITS_PER_LONG
== 32)
3969 image3
|= u
.sig
[3] & 0xffff;
3974 image1
= image0
>> 31 >> 1;
3976 image3
|= (image2
>> 31 >> 1) & 0xffff;
3977 image0
&= 0xffffffff;
3978 image2
&= 0xffffffff;
3980 if (r
->signalling
== fmt
->qnan_msb_set
)
3984 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3989 image3
|= 0x7fffffff;
3990 image2
= 0xffffffff;
3991 image1
= 0xffffffff;
3992 image0
= 0xffffffff;
3997 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3998 whereas the intermediate representation is 0.F x 2**exp.
3999 Which means we're off by one. */
4003 exp
= REAL_EXP (r
) + 16383 - 1;
4004 image3
|= exp
<< 16;
4006 if (HOST_BITS_PER_LONG
== 32)
4011 image3
|= u
.sig
[3] & 0xffff;
4016 image1
= image0
>> 31 >> 1;
4018 image3
|= (image2
>> 31 >> 1) & 0xffff;
4019 image0
&= 0xffffffff;
4020 image2
&= 0xffffffff;
4028 if (FLOAT_WORDS_BIG_ENDIAN
)
4045 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4048 unsigned long image3
, image2
, image1
, image0
;
4052 if (FLOAT_WORDS_BIG_ENDIAN
)
4066 image0
&= 0xffffffff;
4067 image1
&= 0xffffffff;
4068 image2
&= 0xffffffff;
4070 sign
= (image3
>> 31) & 1;
4071 exp
= (image3
>> 16) & 0x7fff;
4074 memset (r
, 0, sizeof (*r
));
4078 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4083 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4084 if (HOST_BITS_PER_LONG
== 32)
4093 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4094 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4099 else if (fmt
->has_signed_zero
)
4102 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4104 if (image3
| image2
| image1
| image0
)
4108 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4110 if (HOST_BITS_PER_LONG
== 32)
4119 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4120 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4122 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4134 SET_REAL_EXP (r
, exp
- 16383 + 1);
4136 if (HOST_BITS_PER_LONG
== 32)
4145 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4146 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4148 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4149 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4153 const struct real_format ieee_quad_format
=
4175 const struct real_format mips_quad_format
=
4197 /* Descriptions of VAX floating point formats can be found beginning at
4199 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4201 The thing to remember is that they're almost IEEE, except for word
4202 order, exponent bias, and the lack of infinities, nans, and denormals.
4204 We don't implement the H_floating format here, simply because neither
4205 the VAX or Alpha ports use it. */
4207 static void encode_vax_f (const struct real_format
*fmt
,
4208 long *, const REAL_VALUE_TYPE
*);
4209 static void decode_vax_f (const struct real_format
*,
4210 REAL_VALUE_TYPE
*, const long *);
4211 static void encode_vax_d (const struct real_format
*fmt
,
4212 long *, const REAL_VALUE_TYPE
*);
4213 static void decode_vax_d (const struct real_format
*,
4214 REAL_VALUE_TYPE
*, const long *);
4215 static void encode_vax_g (const struct real_format
*fmt
,
4216 long *, const REAL_VALUE_TYPE
*);
4217 static void decode_vax_g (const struct real_format
*,
4218 REAL_VALUE_TYPE
*, const long *);
4221 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4222 const REAL_VALUE_TYPE
*r
)
4224 unsigned long sign
, exp
, sig
, image
;
4226 sign
= r
->sign
<< 15;
4236 image
= 0xffff7fff | sign
;
4240 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4241 exp
= REAL_EXP (r
) + 128;
4243 image
= (sig
<< 16) & 0xffff0000;
4257 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4258 REAL_VALUE_TYPE
*r
, const long *buf
)
4260 unsigned long image
= buf
[0] & 0xffffffff;
4261 int exp
= (image
>> 7) & 0xff;
4263 memset (r
, 0, sizeof (*r
));
4268 r
->sign
= (image
>> 15) & 1;
4269 SET_REAL_EXP (r
, exp
- 128);
4271 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4272 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4277 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4278 const REAL_VALUE_TYPE
*r
)
4280 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4285 image0
= image1
= 0;
4290 image0
= 0xffff7fff | sign
;
4291 image1
= 0xffffffff;
4295 /* Extract the significand into straight hi:lo. */
4296 if (HOST_BITS_PER_LONG
== 64)
4298 image0
= r
->sig
[SIGSZ
-1];
4299 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4300 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4304 image0
= r
->sig
[SIGSZ
-1];
4305 image1
= r
->sig
[SIGSZ
-2];
4306 image1
= (image0
<< 24) | (image1
>> 8);
4307 image0
= (image0
>> 8) & 0xffffff;
4310 /* Rearrange the half-words of the significand to match the
4312 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4313 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4315 /* Add the sign and exponent. */
4317 image0
|= (REAL_EXP (r
) + 128) << 7;
4324 if (FLOAT_WORDS_BIG_ENDIAN
)
4325 buf
[0] = image1
, buf
[1] = image0
;
4327 buf
[0] = image0
, buf
[1] = image1
;
4331 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4332 REAL_VALUE_TYPE
*r
, const long *buf
)
4334 unsigned long image0
, image1
;
4337 if (FLOAT_WORDS_BIG_ENDIAN
)
4338 image1
= buf
[0], image0
= buf
[1];
4340 image0
= buf
[0], image1
= buf
[1];
4341 image0
&= 0xffffffff;
4342 image1
&= 0xffffffff;
4344 exp
= (image0
>> 7) & 0xff;
4346 memset (r
, 0, sizeof (*r
));
4351 r
->sign
= (image0
>> 15) & 1;
4352 SET_REAL_EXP (r
, exp
- 128);
4354 /* Rearrange the half-words of the external format into
4355 proper ascending order. */
4356 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4357 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4359 if (HOST_BITS_PER_LONG
== 64)
4361 image0
= (image0
<< 31 << 1) | image1
;
4364 r
->sig
[SIGSZ
-1] = image0
;
4368 r
->sig
[SIGSZ
-1] = image0
;
4369 r
->sig
[SIGSZ
-2] = image1
;
4370 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4371 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4377 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4378 const REAL_VALUE_TYPE
*r
)
4380 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4385 image0
= image1
= 0;
4390 image0
= 0xffff7fff | sign
;
4391 image1
= 0xffffffff;
4395 /* Extract the significand into straight hi:lo. */
4396 if (HOST_BITS_PER_LONG
== 64)
4398 image0
= r
->sig
[SIGSZ
-1];
4399 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4400 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4404 image0
= r
->sig
[SIGSZ
-1];
4405 image1
= r
->sig
[SIGSZ
-2];
4406 image1
= (image0
<< 21) | (image1
>> 11);
4407 image0
= (image0
>> 11) & 0xfffff;
4410 /* Rearrange the half-words of the significand to match the
4412 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4413 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4415 /* Add the sign and exponent. */
4417 image0
|= (REAL_EXP (r
) + 1024) << 4;
4424 if (FLOAT_WORDS_BIG_ENDIAN
)
4425 buf
[0] = image1
, buf
[1] = image0
;
4427 buf
[0] = image0
, buf
[1] = image1
;
4431 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4432 REAL_VALUE_TYPE
*r
, const long *buf
)
4434 unsigned long image0
, image1
;
4437 if (FLOAT_WORDS_BIG_ENDIAN
)
4438 image1
= buf
[0], image0
= buf
[1];
4440 image0
= buf
[0], image1
= buf
[1];
4441 image0
&= 0xffffffff;
4442 image1
&= 0xffffffff;
4444 exp
= (image0
>> 4) & 0x7ff;
4446 memset (r
, 0, sizeof (*r
));
4451 r
->sign
= (image0
>> 15) & 1;
4452 SET_REAL_EXP (r
, exp
- 1024);
4454 /* Rearrange the half-words of the external format into
4455 proper ascending order. */
4456 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4457 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4459 if (HOST_BITS_PER_LONG
== 64)
4461 image0
= (image0
<< 31 << 1) | image1
;
4464 r
->sig
[SIGSZ
-1] = image0
;
4468 r
->sig
[SIGSZ
-1] = image0
;
4469 r
->sig
[SIGSZ
-2] = image1
;
4470 lshift_significand (r
, r
, 64 - 53);
4471 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4476 const struct real_format vax_f_format
=
4498 const struct real_format vax_d_format
=
4520 const struct real_format vax_g_format
=
4542 /* Encode real R into a single precision DFP value in BUF. */
4544 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4545 long *buf ATTRIBUTE_UNUSED
,
4546 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4548 encode_decimal32 (fmt
, buf
, r
);
4551 /* Decode a single precision DFP value in BUF into a real R. */
4553 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4554 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4555 const long *buf ATTRIBUTE_UNUSED
)
4557 decode_decimal32 (fmt
, r
, buf
);
4560 /* Encode real R into a double precision DFP value in BUF. */
4562 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4563 long *buf ATTRIBUTE_UNUSED
,
4564 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4566 encode_decimal64 (fmt
, buf
, r
);
4569 /* Decode a double precision DFP value in BUF into a real R. */
4571 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4572 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4573 const long *buf ATTRIBUTE_UNUSED
)
4575 decode_decimal64 (fmt
, r
, buf
);
4578 /* Encode real R into a quad precision DFP value in BUF. */
4580 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4581 long *buf ATTRIBUTE_UNUSED
,
4582 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4584 encode_decimal128 (fmt
, buf
, r
);
4587 /* Decode a quad precision DFP value in BUF into a real R. */
4589 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4590 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4591 const long *buf ATTRIBUTE_UNUSED
)
4593 decode_decimal128 (fmt
, r
, buf
);
4596 /* Single precision decimal floating point (IEEE 754). */
4597 const struct real_format decimal_single_format
=
4599 encode_decimal_single
,
4600 decode_decimal_single
,
4619 /* Double precision decimal floating point (IEEE 754). */
4620 const struct real_format decimal_double_format
=
4622 encode_decimal_double
,
4623 decode_decimal_double
,
4642 /* Quad precision decimal floating point (IEEE 754). */
4643 const struct real_format decimal_quad_format
=
4645 encode_decimal_quad
,
4646 decode_decimal_quad
,
4665 /* Encode half-precision floats. This routine is used both for the IEEE
4666 ARM alternative encodings. */
4668 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4669 const REAL_VALUE_TYPE
*r
)
4671 unsigned long image
, sig
, exp
;
4672 unsigned long sign
= r
->sign
;
4673 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4676 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4694 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4695 if (r
->signalling
== fmt
->qnan_msb_set
)
4710 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4711 whereas the intermediate representation is 0.F x 2**exp.
4712 Which means we're off by one. */
4716 exp
= REAL_EXP (r
) + 15 - 1;
4728 /* Decode half-precision floats. This routine is used both for the IEEE
4729 ARM alternative encodings. */
4731 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4734 unsigned long image
= buf
[0] & 0xffff;
4735 bool sign
= (image
>> 15) & 1;
4736 int exp
= (image
>> 10) & 0x1f;
4738 memset (r
, 0, sizeof (*r
));
4739 image
<<= HOST_BITS_PER_LONG
- 11;
4744 if (image
&& fmt
->has_denorm
)
4748 SET_REAL_EXP (r
, -14);
4749 r
->sig
[SIGSZ
-1] = image
<< 1;
4752 else if (fmt
->has_signed_zero
)
4755 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4761 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4762 ^ fmt
->qnan_msb_set
);
4763 r
->sig
[SIGSZ
-1] = image
;
4775 SET_REAL_EXP (r
, exp
- 15 + 1);
4776 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4780 /* Half-precision format, as specified in IEEE 754R. */
4781 const struct real_format ieee_half_format
=
4803 /* ARM's alternative half-precision format, similar to IEEE but with
4804 no reserved exponent value for NaNs and infinities; rather, it just
4805 extends the range of exponents by one. */
4806 const struct real_format arm_half_format
=
4828 /* A synthetic "format" for internal arithmetic. It's the size of the
4829 internal significand minus the two bits needed for proper rounding.
4830 The encode and decode routines exist only to satisfy our paranoia
4833 static void encode_internal (const struct real_format
*fmt
,
4834 long *, const REAL_VALUE_TYPE
*);
4835 static void decode_internal (const struct real_format
*,
4836 REAL_VALUE_TYPE
*, const long *);
4839 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4840 const REAL_VALUE_TYPE
*r
)
4842 memcpy (buf
, r
, sizeof (*r
));
4846 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4847 REAL_VALUE_TYPE
*r
, const long *buf
)
4849 memcpy (r
, buf
, sizeof (*r
));
4852 const struct real_format real_internal_format
=
4857 SIGNIFICAND_BITS
- 2,
4858 SIGNIFICAND_BITS
- 2,
4874 /* Calculate X raised to the integer exponent N in format FMT and store
4875 the result in R. Return true if the result may be inexact due to
4876 loss of precision. The algorithm is the classic "left-to-right binary
4877 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4878 Algorithms", "The Art of Computer Programming", Volume 2. */
4881 real_powi (REAL_VALUE_TYPE
*r
, format_helper fmt
,
4882 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4884 unsigned HOST_WIDE_INT bit
;
4886 bool inexact
= false;
4898 /* Don't worry about overflow, from now on n is unsigned. */
4906 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4907 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4911 inexact
|= do_multiply (&t
, &t
, &t
);
4913 inexact
|= do_multiply (&t
, &t
, x
);
4921 inexact
|= do_divide (&t
, &dconst1
, &t
);
4923 real_convert (r
, fmt
, &t
);
4927 /* Round X to the nearest integer not larger in absolute value, i.e.
4928 towards zero, placing the result in R in format FMT. */
4931 real_trunc (REAL_VALUE_TYPE
*r
, format_helper fmt
,
4932 const REAL_VALUE_TYPE
*x
)
4934 do_fix_trunc (r
, x
);
4936 real_convert (r
, fmt
, r
);
4939 /* Round X to the largest integer not greater in value, i.e. round
4940 down, placing the result in R in format FMT. */
4943 real_floor (REAL_VALUE_TYPE
*r
, format_helper fmt
,
4944 const REAL_VALUE_TYPE
*x
)
4948 do_fix_trunc (&t
, x
);
4949 if (! real_identical (&t
, x
) && x
->sign
)
4950 do_add (&t
, &t
, &dconstm1
, 0);
4952 real_convert (r
, fmt
, &t
);
4957 /* Round X to the smallest integer not less then argument, i.e. round
4958 up, placing the result in R in format FMT. */
4961 real_ceil (REAL_VALUE_TYPE
*r
, format_helper fmt
,
4962 const REAL_VALUE_TYPE
*x
)
4966 do_fix_trunc (&t
, x
);
4967 if (! real_identical (&t
, x
) && ! x
->sign
)
4968 do_add (&t
, &t
, &dconst1
, 0);
4970 real_convert (r
, fmt
, &t
);
4975 /* Round X to the nearest integer, but round halfway cases away from
4979 real_round (REAL_VALUE_TYPE
*r
, format_helper fmt
,
4980 const REAL_VALUE_TYPE
*x
)
4982 do_add (r
, x
, &dconsthalf
, x
->sign
);
4983 do_fix_trunc (r
, r
);
4985 real_convert (r
, fmt
, r
);
4988 /* Set the sign of R to the sign of X. */
4991 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4996 /* Check whether the real constant value given is an integer.
4997 Returns false for signaling NaN. */
5000 real_isinteger (const REAL_VALUE_TYPE
*c
, format_helper fmt
)
5002 REAL_VALUE_TYPE cint
;
5004 real_trunc (&cint
, fmt
, c
);
5005 return real_identical (c
, &cint
);
5008 /* Check whether C is an integer that fits in a HOST_WIDE_INT,
5009 storing it in *INT_OUT if so. */
5012 real_isinteger (const REAL_VALUE_TYPE
*c
, HOST_WIDE_INT
*int_out
)
5014 REAL_VALUE_TYPE cint
;
5016 HOST_WIDE_INT n
= real_to_integer (c
);
5017 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
5018 if (real_identical (c
, &cint
))
5026 /* Write into BUF the maximum representable finite floating-point
5027 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5028 float string. LEN is the size of BUF, and the buffer must be large
5029 enough to contain the resulting string. */
5032 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
5037 strcpy (buf
, "0x0.");
5039 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
5042 *p
++ = "08ce"[n
- i
];
5043 sprintf (p
, "p%d", fmt
->emax
);
5044 if (fmt
->pnan
< fmt
->p
)
5046 /* This is an IBM extended double format made up of two IEEE
5047 doubles. The value of the long double is the sum of the
5048 values of the two parts. The most significant part is
5049 required to be the value of the long double rounded to the
5050 nearest double. Rounding means we need a slightly smaller
5051 value for LDBL_MAX. */
5052 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
5055 gcc_assert (strlen (buf
) < len
);
5058 /* True if mode M has a NaN representation and
5059 the treatment of NaN operands is important. */
5062 HONOR_NANS (machine_mode m
)
5064 return MODE_HAS_NANS (m
) && !flag_finite_math_only
;
5068 HONOR_NANS (const_tree t
)
5070 return HONOR_NANS (element_mode (t
));
5074 HONOR_NANS (const_rtx x
)
5076 return HONOR_NANS (GET_MODE (x
));
5079 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5082 HONOR_SNANS (machine_mode m
)
5084 return flag_signaling_nans
&& HONOR_NANS (m
);
5088 HONOR_SNANS (const_tree t
)
5090 return HONOR_SNANS (element_mode (t
));
5094 HONOR_SNANS (const_rtx x
)
5096 return HONOR_SNANS (GET_MODE (x
));
5099 /* As for HONOR_NANS, but true if the mode can represent infinity and
5100 the treatment of infinite values is important. */
5103 HONOR_INFINITIES (machine_mode m
)
5105 return MODE_HAS_INFINITIES (m
) && !flag_finite_math_only
;
5109 HONOR_INFINITIES (const_tree t
)
5111 return HONOR_INFINITIES (element_mode (t
));
5115 HONOR_INFINITIES (const_rtx x
)
5117 return HONOR_INFINITIES (GET_MODE (x
));
5120 /* Like HONOR_NANS, but true if the given mode distinguishes between
5121 positive and negative zero, and the sign of zero is important. */
5124 HONOR_SIGNED_ZEROS (machine_mode m
)
5126 return MODE_HAS_SIGNED_ZEROS (m
) && flag_signed_zeros
;
5130 HONOR_SIGNED_ZEROS (const_tree t
)
5132 return HONOR_SIGNED_ZEROS (element_mode (t
));
5136 HONOR_SIGNED_ZEROS (const_rtx x
)
5138 return HONOR_SIGNED_ZEROS (GET_MODE (x
));
5141 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5142 and the rounding mode is important. */
5145 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m
)
5147 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m
) && flag_rounding_math
;
5151 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t
)
5153 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t
));
5157 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x
)
5159 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x
));