1 /* real.c - software floating point emulation.
2 Copyright (C) 1993-2015 Free Software Foundation, Inc.
3 Contributed by Stephen L. Moshier (moshier@world.std.com).
4 Re-written by Richard Henderson <rth@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "double-int.h"
36 #include "diagnostic-core.h"
45 /* The floating point model used internally is not exactly IEEE 754
46 compliant, and close to the description in the ISO C99 standard,
47 section 5.2.4.2.2 Characteristics of floating types.
51 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
55 b = base or radix, here always 2
57 p = precision (the number of base-b digits in the significand)
58 f_k = the digits of the significand.
60 We differ from typical IEEE 754 encodings in that the entire
61 significand is fractional. Normalized significands are in the
64 A requirement of the model is that P be larger than the largest
65 supported target floating-point type by at least 2 bits. This gives
66 us proper rounding when we truncate to the target type. In addition,
67 E must be large enough to hold the smallest supported denormal number
70 Both of these requirements are easily satisfied. The largest target
71 significand is 113 bits; we store at least 160. The smallest
72 denormal number fits in 17 exponent bits; we store 26. */
75 /* Used to classify two numbers simultaneously. */
76 #define CLASS2(A, B) ((A) << 2 | (B))
78 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
79 #error "Some constant folding done by hand to avoid shift count warnings"
82 static void get_zero (REAL_VALUE_TYPE
*, int);
83 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
84 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
85 static void get_inf (REAL_VALUE_TYPE
*, int);
86 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
87 const REAL_VALUE_TYPE
*, unsigned int);
88 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
90 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
92 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
93 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
94 const REAL_VALUE_TYPE
*);
95 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
96 const REAL_VALUE_TYPE
*, int);
97 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
98 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
99 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
100 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
101 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
102 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
103 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
104 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
105 const REAL_VALUE_TYPE
*);
106 static void normalize (REAL_VALUE_TYPE
*);
108 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
109 const REAL_VALUE_TYPE
*, int);
110 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
111 const REAL_VALUE_TYPE
*);
112 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
113 const REAL_VALUE_TYPE
*);
114 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
115 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
117 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, REAL_VALUE_TYPE
*);
118 static void decimal_from_integer (REAL_VALUE_TYPE
*);
119 static void decimal_integer_string (char *, const REAL_VALUE_TYPE
*,
122 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
123 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
124 static const REAL_VALUE_TYPE
* real_digit (int);
125 static void times_pten (REAL_VALUE_TYPE
*, int);
127 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
129 /* Initialize R with a positive zero. */
132 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
134 memset (r
, 0, sizeof (*r
));
138 /* Initialize R with the canonical quiet NaN. */
141 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
143 memset (r
, 0, sizeof (*r
));
150 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
152 memset (r
, 0, sizeof (*r
));
160 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
162 memset (r
, 0, sizeof (*r
));
168 /* Right-shift the significand of A by N bits; put the result in the
169 significand of R. If any one bits are shifted out, return true. */
172 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
175 unsigned long sticky
= 0;
176 unsigned int i
, ofs
= 0;
178 if (n
>= HOST_BITS_PER_LONG
)
180 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
182 n
&= HOST_BITS_PER_LONG
- 1;
187 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
188 for (i
= 0; i
< SIGSZ
; ++i
)
191 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
192 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
193 << (HOST_BITS_PER_LONG
- n
)));
198 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
199 r
->sig
[i
] = a
->sig
[ofs
+ i
];
200 for (; i
< SIGSZ
; ++i
)
207 /* Right-shift the significand of A by N bits; put the result in the
211 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
214 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
216 n
&= HOST_BITS_PER_LONG
- 1;
219 for (i
= 0; i
< SIGSZ
; ++i
)
222 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
223 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
224 << (HOST_BITS_PER_LONG
- n
)));
229 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
230 r
->sig
[i
] = a
->sig
[ofs
+ i
];
231 for (; i
< SIGSZ
; ++i
)
236 /* Left-shift the significand of A by N bits; put the result in the
240 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
243 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
245 n
&= HOST_BITS_PER_LONG
- 1;
248 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
249 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
250 for (; i
< SIGSZ
; ++i
)
251 r
->sig
[SIGSZ
-1-i
] = 0;
254 for (i
= 0; i
< SIGSZ
; ++i
)
257 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
258 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
259 >> (HOST_BITS_PER_LONG
- n
)));
263 /* Likewise, but N is specialized to 1. */
266 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
270 for (i
= SIGSZ
- 1; i
> 0; --i
)
271 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
272 r
->sig
[0] = a
->sig
[0] << 1;
275 /* Add the significands of A and B, placing the result in R. Return
276 true if there was carry out of the most significant word. */
279 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
280 const REAL_VALUE_TYPE
*b
)
285 for (i
= 0; i
< SIGSZ
; ++i
)
287 unsigned long ai
= a
->sig
[i
];
288 unsigned long ri
= ai
+ b
->sig
[i
];
304 /* Subtract the significands of A and B, placing the result in R. CARRY is
305 true if there's a borrow incoming to the least significant word.
306 Return true if there was borrow out of the most significant word. */
309 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
310 const REAL_VALUE_TYPE
*b
, int carry
)
314 for (i
= 0; i
< SIGSZ
; ++i
)
316 unsigned long ai
= a
->sig
[i
];
317 unsigned long ri
= ai
- b
->sig
[i
];
333 /* Negate the significand A, placing the result in R. */
336 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
341 for (i
= 0; i
< SIGSZ
; ++i
)
343 unsigned long ri
, ai
= a
->sig
[i
];
362 /* Compare significands. Return tri-state vs zero. */
365 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
369 for (i
= SIGSZ
- 1; i
>= 0; --i
)
371 unsigned long ai
= a
->sig
[i
];
372 unsigned long bi
= b
->sig
[i
];
383 /* Return true if A is nonzero. */
386 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
390 for (i
= SIGSZ
- 1; i
>= 0; --i
)
397 /* Set bit N of the significand of R. */
400 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
402 r
->sig
[n
/ HOST_BITS_PER_LONG
]
403 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
406 /* Clear bit N of the significand of R. */
409 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
411 r
->sig
[n
/ HOST_BITS_PER_LONG
]
412 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
415 /* Test bit N of the significand of R. */
418 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
420 /* ??? Compiler bug here if we return this expression directly.
421 The conversion to bool strips the "&1" and we wind up testing
422 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
423 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
427 /* Clear bits 0..N-1 of the significand of R. */
430 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
432 int i
, w
= n
/ HOST_BITS_PER_LONG
;
434 for (i
= 0; i
< w
; ++i
)
437 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
440 /* Divide the significands of A and B, placing the result in R. Return
441 true if the division was inexact. */
444 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
445 const REAL_VALUE_TYPE
*b
)
448 int i
, bit
= SIGNIFICAND_BITS
- 1;
449 unsigned long msb
, inexact
;
452 memset (r
->sig
, 0, sizeof (r
->sig
));
458 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
459 lshift_significand_1 (&u
, &u
);
461 if (msb
|| cmp_significands (&u
, b
) >= 0)
463 sub_significands (&u
, &u
, b
, 0);
464 set_significand_bit (r
, bit
);
469 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
475 /* Adjust the exponent and significand of R such that the most
476 significant bit is set. We underflow to zero and overflow to
477 infinity here, without denormals. (The intermediate representation
478 exponent is large enough to handle target denormals normalized.) */
481 normalize (REAL_VALUE_TYPE
*r
)
489 /* Find the first word that is nonzero. */
490 for (i
= SIGSZ
- 1; i
>= 0; i
--)
492 shift
+= HOST_BITS_PER_LONG
;
496 /* Zero significand flushes to zero. */
504 /* Find the first bit that is nonzero. */
506 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
512 exp
= REAL_EXP (r
) - shift
;
514 get_inf (r
, r
->sign
);
515 else if (exp
< -MAX_EXP
)
516 get_zero (r
, r
->sign
);
519 SET_REAL_EXP (r
, exp
);
520 lshift_significand (r
, r
, shift
);
525 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
526 result may be inexact due to a loss of precision. */
529 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
530 const REAL_VALUE_TYPE
*b
, int subtract_p
)
534 bool inexact
= false;
536 /* Determine if we need to add or subtract. */
538 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
540 switch (CLASS2 (a
->cl
, b
->cl
))
542 case CLASS2 (rvc_zero
, rvc_zero
):
543 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
544 get_zero (r
, sign
& !subtract_p
);
547 case CLASS2 (rvc_zero
, rvc_normal
):
548 case CLASS2 (rvc_zero
, rvc_inf
):
549 case CLASS2 (rvc_zero
, rvc_nan
):
551 case CLASS2 (rvc_normal
, rvc_nan
):
552 case CLASS2 (rvc_inf
, rvc_nan
):
553 case CLASS2 (rvc_nan
, rvc_nan
):
554 /* ANY + NaN = NaN. */
555 case CLASS2 (rvc_normal
, rvc_inf
):
558 r
->sign
= sign
^ subtract_p
;
561 case CLASS2 (rvc_normal
, rvc_zero
):
562 case CLASS2 (rvc_inf
, rvc_zero
):
563 case CLASS2 (rvc_nan
, rvc_zero
):
565 case CLASS2 (rvc_nan
, rvc_normal
):
566 case CLASS2 (rvc_nan
, rvc_inf
):
567 /* NaN + ANY = NaN. */
568 case CLASS2 (rvc_inf
, rvc_normal
):
573 case CLASS2 (rvc_inf
, rvc_inf
):
575 /* Inf - Inf = NaN. */
576 get_canonical_qnan (r
, 0);
578 /* Inf + Inf = Inf. */
582 case CLASS2 (rvc_normal
, rvc_normal
):
589 /* Swap the arguments such that A has the larger exponent. */
590 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
593 const REAL_VALUE_TYPE
*t
;
600 /* If the exponents are not identical, we need to shift the
601 significand of B down. */
604 /* If the exponents are too far apart, the significands
605 do not overlap, which makes the subtraction a noop. */
606 if (dexp
>= SIGNIFICAND_BITS
)
613 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
619 if (sub_significands (r
, a
, b
, inexact
))
621 /* We got a borrow out of the subtraction. That means that
622 A and B had the same exponent, and B had the larger
623 significand. We need to swap the sign and negate the
626 neg_significand (r
, r
);
631 if (add_significands (r
, a
, b
))
633 /* We got carry out of the addition. This means we need to
634 shift the significand back down one bit and increase the
636 inexact
|= sticky_rshift_significand (r
, r
, 1);
637 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
648 SET_REAL_EXP (r
, exp
);
649 /* Zero out the remaining fields. */
654 /* Re-normalize the result. */
657 /* Special case: if the subtraction results in zero, the result
659 if (r
->cl
== rvc_zero
)
662 r
->sig
[0] |= inexact
;
667 /* Calculate R = A * B. Return true if the result may be inexact. */
670 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
671 const REAL_VALUE_TYPE
*b
)
673 REAL_VALUE_TYPE u
, t
, *rr
;
674 unsigned int i
, j
, k
;
675 int sign
= a
->sign
^ b
->sign
;
676 bool inexact
= false;
678 switch (CLASS2 (a
->cl
, b
->cl
))
680 case CLASS2 (rvc_zero
, rvc_zero
):
681 case CLASS2 (rvc_zero
, rvc_normal
):
682 case CLASS2 (rvc_normal
, rvc_zero
):
683 /* +-0 * ANY = 0 with appropriate sign. */
687 case CLASS2 (rvc_zero
, rvc_nan
):
688 case CLASS2 (rvc_normal
, rvc_nan
):
689 case CLASS2 (rvc_inf
, rvc_nan
):
690 case CLASS2 (rvc_nan
, rvc_nan
):
691 /* ANY * NaN = NaN. */
696 case CLASS2 (rvc_nan
, rvc_zero
):
697 case CLASS2 (rvc_nan
, rvc_normal
):
698 case CLASS2 (rvc_nan
, rvc_inf
):
699 /* NaN * ANY = NaN. */
704 case CLASS2 (rvc_zero
, rvc_inf
):
705 case CLASS2 (rvc_inf
, rvc_zero
):
707 get_canonical_qnan (r
, sign
);
710 case CLASS2 (rvc_inf
, rvc_inf
):
711 case CLASS2 (rvc_normal
, rvc_inf
):
712 case CLASS2 (rvc_inf
, rvc_normal
):
713 /* Inf * Inf = Inf, R * Inf = Inf */
717 case CLASS2 (rvc_normal
, rvc_normal
):
724 if (r
== a
|| r
== b
)
730 /* Collect all the partial products. Since we don't have sure access
731 to a widening multiply, we split each long into two half-words.
733 Consider the long-hand form of a four half-word multiplication:
743 We construct partial products of the widened half-word products
744 that are known to not overlap, e.g. DF+DH. Each such partial
745 product is given its proper exponent, which allows us to sum them
746 and obtain the finished product. */
748 for (i
= 0; i
< SIGSZ
* 2; ++i
)
750 unsigned long ai
= a
->sig
[i
/ 2];
752 ai
>>= HOST_BITS_PER_LONG
/ 2;
754 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
759 for (j
= 0; j
< 2; ++j
)
761 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
762 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
771 /* Would underflow to zero, which we shouldn't bother adding. */
776 memset (&u
, 0, sizeof (u
));
778 SET_REAL_EXP (&u
, exp
);
780 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
782 unsigned long bi
= b
->sig
[k
/ 2];
784 bi
>>= HOST_BITS_PER_LONG
/ 2;
786 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
788 u
.sig
[k
/ 2] = ai
* bi
;
792 inexact
|= do_add (rr
, rr
, &u
, 0);
803 /* Calculate R = A / B. Return true if the result may be inexact. */
806 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
807 const REAL_VALUE_TYPE
*b
)
809 int exp
, sign
= a
->sign
^ b
->sign
;
810 REAL_VALUE_TYPE t
, *rr
;
813 switch (CLASS2 (a
->cl
, b
->cl
))
815 case CLASS2 (rvc_zero
, rvc_zero
):
817 case CLASS2 (rvc_inf
, rvc_inf
):
818 /* Inf / Inf = NaN. */
819 get_canonical_qnan (r
, sign
);
822 case CLASS2 (rvc_zero
, rvc_normal
):
823 case CLASS2 (rvc_zero
, rvc_inf
):
825 case CLASS2 (rvc_normal
, rvc_inf
):
830 case CLASS2 (rvc_normal
, rvc_zero
):
832 case CLASS2 (rvc_inf
, rvc_zero
):
837 case CLASS2 (rvc_zero
, rvc_nan
):
838 case CLASS2 (rvc_normal
, rvc_nan
):
839 case CLASS2 (rvc_inf
, rvc_nan
):
840 case CLASS2 (rvc_nan
, rvc_nan
):
841 /* ANY / NaN = NaN. */
846 case CLASS2 (rvc_nan
, rvc_zero
):
847 case CLASS2 (rvc_nan
, rvc_normal
):
848 case CLASS2 (rvc_nan
, rvc_inf
):
849 /* NaN / ANY = NaN. */
854 case CLASS2 (rvc_inf
, rvc_normal
):
859 case CLASS2 (rvc_normal
, rvc_normal
):
866 if (r
== a
|| r
== b
)
871 /* Make sure all fields in the result are initialized. */
876 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
887 SET_REAL_EXP (rr
, exp
);
889 inexact
= div_significands (rr
, a
, b
);
891 /* Re-normalize the result. */
893 rr
->sig
[0] |= inexact
;
901 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
902 one of the two operands is a NaN. */
905 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
910 switch (CLASS2 (a
->cl
, b
->cl
))
912 case CLASS2 (rvc_zero
, rvc_zero
):
913 /* Sign of zero doesn't matter for compares. */
916 case CLASS2 (rvc_normal
, rvc_zero
):
917 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
919 return decimal_do_compare (a
, b
, nan_result
);
921 case CLASS2 (rvc_inf
, rvc_zero
):
922 case CLASS2 (rvc_inf
, rvc_normal
):
923 return (a
->sign
? -1 : 1);
925 case CLASS2 (rvc_inf
, rvc_inf
):
926 return -a
->sign
- -b
->sign
;
928 case CLASS2 (rvc_zero
, rvc_normal
):
929 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
931 return decimal_do_compare (a
, b
, nan_result
);
933 case CLASS2 (rvc_zero
, rvc_inf
):
934 case CLASS2 (rvc_normal
, rvc_inf
):
935 return (b
->sign
? 1 : -1);
937 case CLASS2 (rvc_zero
, rvc_nan
):
938 case CLASS2 (rvc_normal
, rvc_nan
):
939 case CLASS2 (rvc_inf
, rvc_nan
):
940 case CLASS2 (rvc_nan
, rvc_nan
):
941 case CLASS2 (rvc_nan
, rvc_zero
):
942 case CLASS2 (rvc_nan
, rvc_normal
):
943 case CLASS2 (rvc_nan
, rvc_inf
):
946 case CLASS2 (rvc_normal
, rvc_normal
):
953 if (a
->sign
!= b
->sign
)
954 return -a
->sign
- -b
->sign
;
956 if (a
->decimal
|| b
->decimal
)
957 return decimal_do_compare (a
, b
, nan_result
);
959 if (REAL_EXP (a
) > REAL_EXP (b
))
961 else if (REAL_EXP (a
) < REAL_EXP (b
))
964 ret
= cmp_significands (a
, b
);
966 return (a
->sign
? -ret
: ret
);
969 /* Return A truncated to an integral value toward zero. */
972 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
986 decimal_do_fix_trunc (r
, a
);
989 if (REAL_EXP (r
) <= 0)
990 get_zero (r
, r
->sign
);
991 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
992 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
1000 /* Perform the binary or unary operation described by CODE.
1001 For a unary operation, leave OP1 NULL. This function returns
1002 true if the result may be inexact due to loss of precision. */
1005 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
1006 const REAL_VALUE_TYPE
*op1
)
1008 enum tree_code code
= (enum tree_code
) icode
;
1010 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1011 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1016 /* Clear any padding areas in *r if it isn't equal to one of the
1017 operands so that we can later do bitwise comparisons later on. */
1018 if (r
!= op0
&& r
!= op1
)
1019 memset (r
, '\0', sizeof (*r
));
1020 return do_add (r
, op0
, op1
, 0);
1023 if (r
!= op0
&& r
!= op1
)
1024 memset (r
, '\0', sizeof (*r
));
1025 return do_add (r
, op0
, op1
, 1);
1028 if (r
!= op0
&& r
!= op1
)
1029 memset (r
, '\0', sizeof (*r
));
1030 return do_multiply (r
, op0
, op1
);
1033 if (r
!= op0
&& r
!= op1
)
1034 memset (r
, '\0', sizeof (*r
));
1035 return do_divide (r
, op0
, op1
);
1038 if (op1
->cl
== rvc_nan
)
1040 else if (do_compare (op0
, op1
, -1) < 0)
1047 if (op1
->cl
== rvc_nan
)
1049 else if (do_compare (op0
, op1
, 1) < 0)
1065 case FIX_TRUNC_EXPR
:
1066 do_fix_trunc (r
, op0
);
1076 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1079 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1084 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1087 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1092 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1093 const REAL_VALUE_TYPE
*op1
)
1095 enum tree_code code
= (enum tree_code
) icode
;
1100 return do_compare (op0
, op1
, 1) < 0;
1102 return do_compare (op0
, op1
, 1) <= 0;
1104 return do_compare (op0
, op1
, -1) > 0;
1106 return do_compare (op0
, op1
, -1) >= 0;
1108 return do_compare (op0
, op1
, -1) == 0;
1110 return do_compare (op0
, op1
, -1) != 0;
1111 case UNORDERED_EXPR
:
1112 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1114 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1116 return do_compare (op0
, op1
, -1) < 0;
1118 return do_compare (op0
, op1
, -1) <= 0;
1120 return do_compare (op0
, op1
, 1) > 0;
1122 return do_compare (op0
, op1
, 1) >= 0;
1124 return do_compare (op0
, op1
, 0) == 0;
1126 return do_compare (op0
, op1
, 0) != 0;
1133 /* Return floor log2(R). */
1136 real_exponent (const REAL_VALUE_TYPE
*r
)
1144 return (unsigned int)-1 >> 1;
1146 return REAL_EXP (r
);
1152 /* R = OP0 * 2**EXP. */
1155 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1166 exp
+= REAL_EXP (op0
);
1168 get_inf (r
, r
->sign
);
1169 else if (exp
< -MAX_EXP
)
1170 get_zero (r
, r
->sign
);
1172 SET_REAL_EXP (r
, exp
);
1180 /* Determine whether a floating-point value X is infinite. */
1183 real_isinf (const REAL_VALUE_TYPE
*r
)
1185 return (r
->cl
== rvc_inf
);
1188 /* Determine whether a floating-point value X is a NaN. */
1191 real_isnan (const REAL_VALUE_TYPE
*r
)
1193 return (r
->cl
== rvc_nan
);
1196 /* Determine whether a floating-point value X is finite. */
1199 real_isfinite (const REAL_VALUE_TYPE
*r
)
1201 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1204 /* Determine whether a floating-point value X is negative. */
1207 real_isneg (const REAL_VALUE_TYPE
*r
)
1212 /* Determine whether a floating-point value X is minus zero. */
1215 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1217 return r
->sign
&& r
->cl
== rvc_zero
;
1220 /* Compare two floating-point objects for bitwise identity. */
1223 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1229 if (a
->sign
!= b
->sign
)
1239 if (a
->decimal
!= b
->decimal
)
1241 if (REAL_EXP (a
) != REAL_EXP (b
))
1246 if (a
->signalling
!= b
->signalling
)
1248 /* The significand is ignored for canonical NaNs. */
1249 if (a
->canonical
|| b
->canonical
)
1250 return a
->canonical
== b
->canonical
;
1257 for (i
= 0; i
< SIGSZ
; ++i
)
1258 if (a
->sig
[i
] != b
->sig
[i
])
1264 /* Try to change R into its exact multiplicative inverse in machine
1265 mode MODE. Return true if successful. */
1268 exact_real_inverse (machine_mode mode
, REAL_VALUE_TYPE
*r
)
1270 const REAL_VALUE_TYPE
*one
= real_digit (1);
1274 if (r
->cl
!= rvc_normal
)
1277 /* Check for a power of two: all significand bits zero except the MSB. */
1278 for (i
= 0; i
< SIGSZ
-1; ++i
)
1281 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1284 /* Find the inverse and truncate to the required mode. */
1285 do_divide (&u
, one
, r
);
1286 real_convert (&u
, mode
, &u
);
1288 /* The rounding may have overflowed. */
1289 if (u
.cl
!= rvc_normal
)
1291 for (i
= 0; i
< SIGSZ
-1; ++i
)
1294 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1301 /* Return true if arithmetic on values in IMODE that were promoted
1302 from values in TMODE is equivalent to direct arithmetic on values
1306 real_can_shorten_arithmetic (machine_mode imode
, machine_mode tmode
)
1308 const struct real_format
*tfmt
, *ifmt
;
1309 tfmt
= REAL_MODE_FORMAT (tmode
);
1310 ifmt
= REAL_MODE_FORMAT (imode
);
1311 /* These conditions are conservative rather than trying to catch the
1312 exact boundary conditions; the main case to allow is IEEE float
1314 return (ifmt
->b
== tfmt
->b
1315 && ifmt
->p
> 2 * tfmt
->p
1316 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1317 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1318 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1319 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1320 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1321 && (ifmt
->has_sign_dependent_rounding
1322 == tfmt
->has_sign_dependent_rounding
)
1323 && ifmt
->has_nans
>= tfmt
->has_nans
1324 && ifmt
->has_inf
>= tfmt
->has_inf
1325 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1326 && !MODE_COMPOSITE_P (tmode
)
1327 && !MODE_COMPOSITE_P (imode
));
1330 /* Render R as an integer. */
1333 real_to_integer (const REAL_VALUE_TYPE
*r
)
1335 unsigned HOST_WIDE_INT i
;
1346 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1353 return decimal_real_to_integer (r
);
1355 if (REAL_EXP (r
) <= 0)
1357 /* Only force overflow for unsigned overflow. Signed overflow is
1358 undefined, so it doesn't matter what we return, and some callers
1359 expect to be able to use this routine for both signed and
1360 unsigned conversions. */
1361 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1364 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1365 i
= r
->sig
[SIGSZ
-1];
1368 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1369 i
= r
->sig
[SIGSZ
-1];
1370 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1371 i
|= r
->sig
[SIGSZ
-2];
1374 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1385 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1386 be represented in precision, *FAIL is set to TRUE. */
1389 real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
1391 HOST_WIDE_INT val
[2 * WIDE_INT_MAX_ELTS
];
1400 return wi::zero (precision
);
1408 return wi::set_bit_in_zero (precision
- 1, precision
);
1410 return ~wi::set_bit_in_zero (precision
- 1, precision
);
1414 return decimal_real_to_integer (r
, fail
, precision
);
1419 /* Only force overflow for unsigned overflow. Signed overflow is
1420 undefined, so it doesn't matter what we return, and some callers
1421 expect to be able to use this routine for both signed and
1422 unsigned conversions. */
1423 if (exp
> precision
)
1426 /* Put the significand into a wide_int that has precision W, which
1427 is the smallest HWI-multiple that has at least PRECISION bits.
1428 This ensures that the top bit of the significand is in the
1429 top bit of the wide_int. */
1430 words
= (precision
+ HOST_BITS_PER_WIDE_INT
- 1) / HOST_BITS_PER_WIDE_INT
;
1431 w
= words
* HOST_BITS_PER_WIDE_INT
;
1433 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1434 for (int i
= 0; i
< words
; i
++)
1436 int j
= SIGSZ
- words
+ i
;
1437 val
[i
] = (j
< 0) ? 0 : r
->sig
[j
];
1440 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1441 for (int i
= 0; i
< words
; i
++)
1443 int j
= SIGSZ
- (words
* 2) + (i
* 2);
1450 val
[i
] |= (unsigned HOST_WIDE_INT
) r
->sig
[j
] << HOST_BITS_PER_LONG
;
1453 /* Shift the value into place and truncate to the desired precision. */
1454 result
= wide_int::from_array (val
, words
, w
);
1455 result
= wi::lrshift (result
, w
- exp
);
1456 result
= wide_int::from (result
, precision
, UNSIGNED
);
1468 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1469 of NUM / DEN. Return the quotient and place the remainder in NUM.
1470 It is expected that NUM / DEN are close enough that the quotient is
1473 static unsigned long
1474 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1476 unsigned long q
, msb
;
1477 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1486 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1488 lshift_significand_1 (num
, num
);
1490 if (msb
|| cmp_significands (num
, den
) >= 0)
1492 sub_significands (num
, num
, den
, 0);
1496 while (--expn
>= expd
);
1498 SET_REAL_EXP (num
, expd
);
1504 /* Render R as a decimal floating point constant. Emit DIGITS significant
1505 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1506 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1507 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1508 to a string that, when parsed back in mode MODE, yields the same value. */
1510 #define M_LOG10_2 0.30102999566398119521
1513 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1514 size_t buf_size
, size_t digits
,
1515 int crop_trailing_zeros
, machine_mode mode
)
1517 const struct real_format
*fmt
= NULL
;
1518 const REAL_VALUE_TYPE
*one
, *ten
;
1519 REAL_VALUE_TYPE r
, pten
, u
, v
;
1520 int dec_exp
, cmp_one
, digit
;
1522 char *p
, *first
, *last
;
1526 if (mode
!= VOIDmode
)
1528 fmt
= REAL_MODE_FORMAT (mode
);
1536 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1541 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1544 /* ??? Print the significand as well, if not canonical? */
1545 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1546 (r_orig
->signalling
? 'S' : 'Q'));
1554 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1558 /* Bound the number of digits printed by the size of the representation. */
1559 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1560 if (digits
== 0 || digits
> max_digits
)
1561 digits
= max_digits
;
1563 /* Estimate the decimal exponent, and compute the length of the string it
1564 will print as. Be conservative and add one to account for possible
1565 overflow or rounding error. */
1566 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1567 for (max_digits
= 1; dec_exp
; max_digits
++)
1570 /* Bound the number of digits printed by the size of the output buffer. */
1571 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1572 gcc_assert (max_digits
<= buf_size
);
1573 if (digits
> max_digits
)
1574 digits
= max_digits
;
1576 one
= real_digit (1);
1577 ten
= ten_to_ptwo (0);
1585 cmp_one
= do_compare (&r
, one
, 0);
1590 /* Number is greater than one. Convert significand to an integer
1591 and strip trailing decimal zeros. */
1594 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1596 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1597 m
= floor_log2 (max_digits
);
1599 /* Iterate over the bits of the possible powers of 10 that might
1600 be present in U and eliminate them. That is, if we find that
1601 10**2**M divides U evenly, keep the division and increase
1607 do_divide (&t
, &u
, ten_to_ptwo (m
));
1608 do_fix_trunc (&v
, &t
);
1609 if (cmp_significands (&v
, &t
) == 0)
1617 /* Revert the scaling to integer that we performed earlier. */
1618 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1619 - (SIGNIFICAND_BITS
- 1));
1622 /* Find power of 10. Do this by dividing out 10**2**M when
1623 this is larger than the current remainder. Fill PTEN with
1624 the power of 10 that we compute. */
1625 if (REAL_EXP (&r
) > 0)
1627 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1630 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1631 if (do_compare (&u
, ptentwo
, 0) >= 0)
1633 do_divide (&u
, &u
, ptentwo
);
1634 do_multiply (&pten
, &pten
, ptentwo
);
1641 /* We managed to divide off enough tens in the above reduction
1642 loop that we've now got a negative exponent. Fall into the
1643 less-than-one code to compute the proper value for PTEN. */
1650 /* Number is less than one. Pad significand with leading
1656 /* Stop if we'd shift bits off the bottom. */
1660 do_multiply (&u
, &v
, ten
);
1662 /* Stop if we're now >= 1. */
1663 if (REAL_EXP (&u
) > 0)
1671 /* Find power of 10. Do this by multiplying in P=10**2**M when
1672 the current remainder is smaller than 1/P. Fill PTEN with the
1673 power of 10 that we compute. */
1674 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1677 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1678 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1680 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1682 do_multiply (&v
, &v
, ptentwo
);
1683 do_multiply (&pten
, &pten
, ptentwo
);
1689 /* Invert the positive power of 10 that we've collected so far. */
1690 do_divide (&pten
, one
, &pten
);
1698 /* At this point, PTEN should contain the nearest power of 10 smaller
1699 than R, such that this division produces the first digit.
1701 Using a divide-step primitive that returns the complete integral
1702 remainder avoids the rounding error that would be produced if
1703 we were to use do_divide here and then simply multiply by 10 for
1704 each subsequent digit. */
1706 digit
= rtd_divmod (&r
, &pten
);
1708 /* Be prepared for error in that division via underflow ... */
1709 if (digit
== 0 && cmp_significand_0 (&r
))
1711 /* Multiply by 10 and try again. */
1712 do_multiply (&r
, &r
, ten
);
1713 digit
= rtd_divmod (&r
, &pten
);
1715 gcc_assert (digit
!= 0);
1718 /* ... or overflow. */
1728 gcc_assert (digit
<= 10);
1732 /* Generate subsequent digits. */
1733 while (--digits
> 0)
1735 do_multiply (&r
, &r
, ten
);
1736 digit
= rtd_divmod (&r
, &pten
);
1741 /* Generate one more digit with which to do rounding. */
1742 do_multiply (&r
, &r
, ten
);
1743 digit
= rtd_divmod (&r
, &pten
);
1745 /* Round the result. */
1746 if (fmt
&& fmt
->round_towards_zero
)
1748 /* If the format uses round towards zero when parsing the string
1749 back in, we need to always round away from zero here. */
1750 if (cmp_significand_0 (&r
))
1752 round_up
= digit
> 0;
1758 /* Round to nearest. If R is nonzero there are additional
1759 nonzero digits to be extracted. */
1760 if (cmp_significand_0 (&r
))
1762 /* Round to even. */
1763 else if ((p
[-1] - '0') & 1)
1767 round_up
= digit
> 5;
1784 /* Carry out of the first digit. This means we had all 9's and
1785 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1793 /* Insert the decimal point. */
1794 first
[0] = first
[1];
1797 /* If requested, drop trailing zeros. Never crop past "1.0". */
1798 if (crop_trailing_zeros
)
1799 while (last
> first
+ 3 && last
[-1] == '0')
1802 /* Append the exponent. */
1803 sprintf (last
, "e%+d", dec_exp
);
1805 #ifdef ENABLE_CHECKING
1806 /* Verify that we can read the original value back in. */
1807 if (mode
!= VOIDmode
)
1809 real_from_string (&r
, str
);
1810 real_convert (&r
, mode
, &r
);
1811 gcc_assert (real_identical (&r
, r_orig
));
1816 /* Likewise, except always uses round-to-nearest. */
1819 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1820 size_t digits
, int crop_trailing_zeros
)
1822 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1823 digits
, crop_trailing_zeros
, VOIDmode
);
1826 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1827 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1828 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1829 strip trailing zeros. */
1832 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1833 size_t digits
, int crop_trailing_zeros
)
1835 int i
, j
, exp
= REAL_EXP (r
);
1848 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1851 /* ??? Print the significand as well, if not canonical? */
1852 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1853 (r
->signalling
? 'S' : 'Q'));
1861 /* Hexadecimal format for decimal floats is not interesting. */
1862 strcpy (str
, "N/A");
1867 digits
= SIGNIFICAND_BITS
/ 4;
1869 /* Bound the number of digits printed by the size of the output buffer. */
1871 sprintf (exp_buf
, "p%+d", exp
);
1872 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1873 gcc_assert (max_digits
<= buf_size
);
1874 if (digits
> max_digits
)
1875 digits
= max_digits
;
1886 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1887 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1889 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1895 if (crop_trailing_zeros
)
1896 while (p
> first
+ 1 && p
[-1] == '0')
1899 sprintf (p
, "p%+d", exp
);
1902 /* Initialize R from a decimal or hexadecimal string. The string is
1903 assumed to have been syntax checked already. Return -1 if the
1904 value underflows, +1 if overflows, and 0 otherwise. */
1907 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1919 else if (*str
== '+')
1922 if (!strncmp (str
, "QNaN", 4))
1924 get_canonical_qnan (r
, sign
);
1927 else if (!strncmp (str
, "SNaN", 4))
1929 get_canonical_snan (r
, sign
);
1932 else if (!strncmp (str
, "Inf", 3))
1938 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1940 /* Hexadecimal floating point. */
1941 int pos
= SIGNIFICAND_BITS
- 4, d
;
1949 d
= hex_value (*str
);
1954 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1955 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1959 /* Ensure correct rounding by setting last bit if there is
1960 a subsequent nonzero digit. */
1968 if (pos
== SIGNIFICAND_BITS
- 4)
1975 d
= hex_value (*str
);
1980 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1981 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1985 /* Ensure correct rounding by setting last bit if there is
1986 a subsequent nonzero digit. */
1992 /* If the mantissa is zero, ignore the exponent. */
1993 if (!cmp_significand_0 (r
))
1996 if (*str
== 'p' || *str
== 'P')
1998 bool exp_neg
= false;
2006 else if (*str
== '+')
2010 while (ISDIGIT (*str
))
2016 /* Overflowed the exponent. */
2031 SET_REAL_EXP (r
, exp
);
2037 /* Decimal floating point. */
2038 const char *cstr
= str
;
2042 while (*cstr
== '0')
2047 while (*cstr
== '0')
2051 /* If the mantissa is zero, ignore the exponent. */
2052 if (!ISDIGIT (*cstr
))
2055 /* Nonzero value, possibly overflowing or underflowing. */
2056 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2057 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, GMP_RNDZ
);
2058 /* The result should never be a NaN, and because the rounding is
2059 toward zero should never be an infinity. */
2060 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2061 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2066 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2073 real_from_mpfr (r
, m
, NULL_TREE
, GMP_RNDZ
);
2074 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2075 because the hex digits used in real_from_mpfr did not
2076 start with a digit 8 to f, but the exponent bounds above
2077 should have avoided underflow or overflow. */
2078 gcc_assert (r
->cl
= rvc_normal
);
2079 /* Set a sticky bit if mpfr_strtofr was inexact. */
2080 r
->sig
[0] |= inexact
;
2101 /* Legacy. Similar, but return the result directly. */
2104 real_from_string2 (const char *s
, machine_mode mode
)
2108 real_from_string (&r
, s
);
2109 if (mode
!= VOIDmode
)
2110 real_convert (&r
, mode
, &r
);
2115 /* Initialize R from string S and desired MODE. */
2118 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, machine_mode mode
)
2120 if (DECIMAL_FLOAT_MODE_P (mode
))
2121 decimal_real_from_string (r
, s
);
2123 real_from_string (r
, s
);
2125 if (mode
!= VOIDmode
)
2126 real_convert (r
, mode
, r
);
2129 /* Initialize R from the wide_int VAL_IN. The MODE is not VOIDmode,*/
2132 real_from_integer (REAL_VALUE_TYPE
*r
, machine_mode mode
,
2133 const wide_int_ref
&val_in
, signop sgn
)
2139 unsigned int len
= val_in
.get_precision ();
2141 int maxbitlen
= MAX_BITSIZE_MODE_ANY_INT
+ HOST_BITS_PER_WIDE_INT
;
2142 const unsigned int realmax
= (SIGNIFICAND_BITS
/ HOST_BITS_PER_WIDE_INT
2143 * HOST_BITS_PER_WIDE_INT
);
2145 memset (r
, 0, sizeof (*r
));
2147 r
->sign
= wi::neg_p (val_in
, sgn
);
2149 /* We have to ensure we can negate the largest negative number. */
2150 wide_int val
= wide_int::from (val_in
, maxbitlen
, sgn
);
2155 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2156 won't work with precisions that are not a multiple of
2157 HOST_BITS_PER_WIDE_INT. */
2158 len
+= HOST_BITS_PER_WIDE_INT
- 1;
2160 /* Ensure we can represent the largest negative number. */
2163 len
= len
/HOST_BITS_PER_WIDE_INT
* HOST_BITS_PER_WIDE_INT
;
2165 /* Cap the size to the size allowed by real.h. */
2168 HOST_WIDE_INT cnt_l_z
;
2169 cnt_l_z
= wi::clz (val
);
2171 if (maxbitlen
- cnt_l_z
> realmax
)
2173 e
= maxbitlen
- cnt_l_z
- realmax
;
2175 /* This value is too large, we must shift it right to
2176 preserve all the bits we can, and then bump the
2177 exponent up by that amount. */
2178 val
= wi::lrshift (val
, e
);
2183 /* Clear out top bits so elt will work with precisions that aren't
2184 a multiple of HOST_BITS_PER_WIDE_INT. */
2185 val
= wide_int::from (val
, len
, sgn
);
2186 len
= len
/ HOST_BITS_PER_WIDE_INT
;
2188 SET_REAL_EXP (r
, len
* HOST_BITS_PER_WIDE_INT
+ e
);
2191 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2192 for (i
= len
- 1; i
>= 0; i
--)
2194 r
->sig
[j
--] = val
.elt (i
);
2200 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2201 for (i
= len
- 1; i
>= 0; i
--)
2203 HOST_WIDE_INT e
= val
.elt (i
);
2204 r
->sig
[j
--] = e
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2216 if (DECIMAL_FLOAT_MODE_P (mode
))
2217 decimal_from_integer (r
);
2218 else if (mode
!= VOIDmode
)
2219 real_convert (r
, mode
, r
);
2222 /* Render R, an integral value, as a floating point constant with no
2223 specified exponent. */
2226 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2229 int dec_exp
, digit
, digits
;
2230 REAL_VALUE_TYPE r
, pten
;
2236 if (r
.cl
== rvc_zero
)
2245 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2246 digits
= dec_exp
+ 1;
2247 gcc_assert ((digits
+ 2) < (int)buf_size
);
2249 pten
= *real_digit (1);
2250 times_pten (&pten
, dec_exp
);
2256 digit
= rtd_divmod (&r
, &pten
);
2257 gcc_assert (digit
>= 0 && digit
<= 9);
2259 while (--digits
> 0)
2262 digit
= rtd_divmod (&r
, &pten
);
2269 /* Convert a real with an integral value to decimal float. */
2272 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2276 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2277 decimal_real_from_string (r
, str
);
2280 /* Returns 10**2**N. */
2282 static const REAL_VALUE_TYPE
*
2285 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2287 gcc_assert (n
>= 0);
2288 gcc_assert (n
< EXP_BITS
);
2290 if (tens
[n
].cl
== rvc_zero
)
2292 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2294 HOST_WIDE_INT t
= 10;
2297 for (i
= 0; i
< n
; ++i
)
2300 real_from_integer (&tens
[n
], VOIDmode
, t
, UNSIGNED
);
2304 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2305 do_multiply (&tens
[n
], t
, t
);
2312 /* Returns 10**(-2**N). */
2314 static const REAL_VALUE_TYPE
*
2315 ten_to_mptwo (int n
)
2317 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2319 gcc_assert (n
>= 0);
2320 gcc_assert (n
< EXP_BITS
);
2322 if (tens
[n
].cl
== rvc_zero
)
2323 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2330 static const REAL_VALUE_TYPE
*
2333 static REAL_VALUE_TYPE num
[10];
2335 gcc_assert (n
>= 0);
2336 gcc_assert (n
<= 9);
2338 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2339 real_from_integer (&num
[n
], VOIDmode
, n
, UNSIGNED
);
2344 /* Multiply R by 10**EXP. */
2347 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2349 REAL_VALUE_TYPE pten
, *rr
;
2350 bool negative
= (exp
< 0);
2356 pten
= *real_digit (1);
2362 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2364 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2367 do_divide (r
, r
, &pten
);
2370 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2372 const REAL_VALUE_TYPE
*
2375 static REAL_VALUE_TYPE value
;
2377 /* Initialize mathematical constants for constant folding builtins.
2378 These constants need to be given to at least 160 bits precision. */
2379 if (value
.cl
== rvc_zero
)
2382 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2383 mpfr_set_ui (m
, 1, GMP_RNDN
);
2384 mpfr_exp (m
, m
, GMP_RNDN
);
2385 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2392 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2394 const REAL_VALUE_TYPE
*
2395 dconst_third_ptr (void)
2397 static REAL_VALUE_TYPE value
;
2399 /* Initialize mathematical constants for constant folding builtins.
2400 These constants need to be given to at least 160 bits precision. */
2401 if (value
.cl
== rvc_zero
)
2403 real_arithmetic (&value
, RDIV_EXPR
, &dconst1
, real_digit (3));
2408 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2410 const REAL_VALUE_TYPE
*
2411 dconst_sqrt2_ptr (void)
2413 static REAL_VALUE_TYPE value
;
2415 /* Initialize mathematical constants for constant folding builtins.
2416 These constants need to be given to at least 160 bits precision. */
2417 if (value
.cl
== rvc_zero
)
2420 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2421 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2422 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2428 /* Fills R with +Inf. */
2431 real_inf (REAL_VALUE_TYPE
*r
)
2436 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2437 we force a QNaN, else we force an SNaN. The string, if not empty,
2438 is parsed as a number and placed in the significand. Return true
2439 if the string was successfully parsed. */
2442 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2445 const struct real_format
*fmt
;
2447 fmt
= REAL_MODE_FORMAT (mode
);
2453 get_canonical_qnan (r
, 0);
2455 get_canonical_snan (r
, 0);
2461 memset (r
, 0, sizeof (*r
));
2464 /* Parse akin to strtol into the significand of R. */
2466 while (ISSPACE (*str
))
2470 else if (*str
== '+')
2475 if (*str
== 'x' || *str
== 'X')
2484 while ((d
= hex_value (*str
)) < base
)
2491 lshift_significand (r
, r
, 3);
2494 lshift_significand (r
, r
, 4);
2497 lshift_significand_1 (&u
, r
);
2498 lshift_significand (r
, r
, 3);
2499 add_significands (r
, r
, &u
);
2507 add_significands (r
, r
, &u
);
2512 /* Must have consumed the entire string for success. */
2516 /* Shift the significand into place such that the bits
2517 are in the most significant bits for the format. */
2518 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2520 /* Our MSB is always unset for NaNs. */
2521 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2523 /* Force quiet or signalling NaN. */
2524 r
->signalling
= !quiet
;
2530 /* Fills R with the largest finite value representable in mode MODE.
2531 If SIGN is nonzero, R is set to the most negative finite value. */
2534 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
2536 const struct real_format
*fmt
;
2539 fmt
= REAL_MODE_FORMAT (mode
);
2541 memset (r
, 0, sizeof (*r
));
2544 decimal_real_maxval (r
, sign
, mode
);
2549 SET_REAL_EXP (r
, fmt
->emax
);
2551 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2552 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2553 clear_significand_below (r
, np2
);
2555 if (fmt
->pnan
< fmt
->p
)
2556 /* This is an IBM extended double format made up of two IEEE
2557 doubles. The value of the long double is the sum of the
2558 values of the two parts. The most significant part is
2559 required to be the value of the long double rounded to the
2560 nearest double. Rounding means we need a slightly smaller
2561 value for LDBL_MAX. */
2562 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2566 /* Fills R with 2**N. */
2569 real_2expN (REAL_VALUE_TYPE
*r
, int n
, machine_mode fmode
)
2571 memset (r
, 0, sizeof (*r
));
2576 else if (n
< -MAX_EXP
)
2581 SET_REAL_EXP (r
, n
);
2582 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2584 if (DECIMAL_FLOAT_MODE_P (fmode
))
2585 decimal_real_convert (r
, fmode
, r
);
2590 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2594 bool round_up
= false;
2600 decimal_round_for_format (fmt
, r
);
2603 /* FIXME. We can come here via fp_easy_constant
2604 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2605 investigated whether this convert needs to be here, or
2606 something else is missing. */
2607 decimal_real_convert (r
, DFmode
, r
);
2611 emin2m1
= fmt
->emin
- 1;
2614 np2
= SIGNIFICAND_BITS
- p2
;
2618 get_zero (r
, r
->sign
);
2620 if (!fmt
->has_signed_zero
)
2625 get_inf (r
, r
->sign
);
2630 clear_significand_below (r
, np2
);
2640 /* Check the range of the exponent. If we're out of range,
2641 either underflow or overflow. */
2642 if (REAL_EXP (r
) > emax2
)
2644 else if (REAL_EXP (r
) <= emin2m1
)
2648 if (!fmt
->has_denorm
)
2650 /* Don't underflow completely until we've had a chance to round. */
2651 if (REAL_EXP (r
) < emin2m1
)
2656 diff
= emin2m1
- REAL_EXP (r
) + 1;
2660 /* De-normalize the significand. */
2661 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2662 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2666 if (!fmt
->round_towards_zero
)
2668 /* There are P2 true significand bits, followed by one guard bit,
2669 followed by one sticky bit, followed by stuff. Fold nonzero
2670 stuff into the sticky bit. */
2671 unsigned long sticky
;
2675 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2676 sticky
|= r
->sig
[i
];
2678 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2680 guard
= test_significand_bit (r
, np2
- 1);
2681 lsb
= test_significand_bit (r
, np2
);
2683 /* Round to even. */
2684 round_up
= guard
&& (sticky
|| lsb
);
2691 set_significand_bit (&u
, np2
);
2693 if (add_significands (r
, r
, &u
))
2695 /* Overflow. Means the significand had been all ones, and
2696 is now all zeros. Need to increase the exponent, and
2697 possibly re-normalize it. */
2698 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2699 if (REAL_EXP (r
) > emax2
)
2701 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2705 /* Catch underflow that we deferred until after rounding. */
2706 if (REAL_EXP (r
) <= emin2m1
)
2709 /* Clear out trailing garbage. */
2710 clear_significand_below (r
, np2
);
2713 /* Extend or truncate to a new mode. */
2716 real_convert (REAL_VALUE_TYPE
*r
, machine_mode mode
,
2717 const REAL_VALUE_TYPE
*a
)
2719 const struct real_format
*fmt
;
2721 fmt
= REAL_MODE_FORMAT (mode
);
2726 if (a
->decimal
|| fmt
->b
== 10)
2727 decimal_real_convert (r
, mode
, a
);
2729 round_for_format (fmt
, r
);
2731 /* round_for_format de-normalizes denormals. Undo just that part. */
2732 if (r
->cl
== rvc_normal
)
2736 /* Legacy. Likewise, except return the struct directly. */
2739 real_value_truncate (machine_mode mode
, REAL_VALUE_TYPE a
)
2742 real_convert (&r
, mode
, &a
);
2746 /* Return true if truncating to MODE is exact. */
2749 exact_real_truncate (machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2751 const struct real_format
*fmt
;
2755 fmt
= REAL_MODE_FORMAT (mode
);
2758 /* Don't allow conversion to denormals. */
2759 emin2m1
= fmt
->emin
- 1;
2760 if (REAL_EXP (a
) <= emin2m1
)
2763 /* After conversion to the new mode, the value must be identical. */
2764 real_convert (&t
, mode
, a
);
2765 return real_identical (&t
, a
);
2768 /* Write R to the given target format. Place the words of the result
2769 in target word order in BUF. There are always 32 bits in each
2770 long, no matter the size of the host long.
2772 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2775 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2776 const struct real_format
*fmt
)
2782 round_for_format (fmt
, &r
);
2786 (*fmt
->encode
) (fmt
, buf
, &r
);
2791 /* Similar, but look up the format from MODE. */
2794 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, machine_mode mode
)
2796 const struct real_format
*fmt
;
2798 fmt
= REAL_MODE_FORMAT (mode
);
2801 return real_to_target_fmt (buf
, r
, fmt
);
2804 /* Read R from the given target format. Read the words of the result
2805 in target word order in BUF. There are always 32 bits in each
2806 long, no matter the size of the host long. */
2809 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2810 const struct real_format
*fmt
)
2812 (*fmt
->decode
) (fmt
, r
, buf
);
2815 /* Similar, but look up the format from MODE. */
2818 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, machine_mode mode
)
2820 const struct real_format
*fmt
;
2822 fmt
= REAL_MODE_FORMAT (mode
);
2825 (*fmt
->decode
) (fmt
, r
, buf
);
2828 /* Return the number of bits of the largest binary value that the
2829 significand of MODE will hold. */
2830 /* ??? Legacy. Should get access to real_format directly. */
2833 significand_size (machine_mode mode
)
2835 const struct real_format
*fmt
;
2837 fmt
= REAL_MODE_FORMAT (mode
);
2843 /* Return the size in bits of the largest binary value that can be
2844 held by the decimal coefficient for this mode. This is one more
2845 than the number of bits required to hold the largest coefficient
2847 double log2_10
= 3.3219281;
2848 return fmt
->p
* log2_10
;
2853 /* Return a hash value for the given real value. */
2854 /* ??? The "unsigned int" return value is intended to be hashval_t,
2855 but I didn't want to pull hashtab.h into real.h. */
2858 real_hash (const REAL_VALUE_TYPE
*r
)
2863 h
= r
->cl
| (r
->sign
<< 2);
2871 h
|= REAL_EXP (r
) << 3;
2876 h
^= (unsigned int)-1;
2885 if (sizeof (unsigned long) > sizeof (unsigned int))
2886 for (i
= 0; i
< SIGSZ
; ++i
)
2888 unsigned long s
= r
->sig
[i
];
2889 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2892 for (i
= 0; i
< SIGSZ
; ++i
)
2898 /* IEEE single-precision format. */
2900 static void encode_ieee_single (const struct real_format
*fmt
,
2901 long *, const REAL_VALUE_TYPE
*);
2902 static void decode_ieee_single (const struct real_format
*,
2903 REAL_VALUE_TYPE
*, const long *);
2906 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2907 const REAL_VALUE_TYPE
*r
)
2909 unsigned long image
, sig
, exp
;
2910 unsigned long sign
= r
->sign
;
2911 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2914 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2925 image
|= 0x7fffffff;
2932 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2933 if (r
->signalling
== fmt
->qnan_msb_set
)
2944 image
|= 0x7fffffff;
2948 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2949 whereas the intermediate representation is 0.F x 2**exp.
2950 Which means we're off by one. */
2954 exp
= REAL_EXP (r
) + 127 - 1;
2967 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2970 unsigned long image
= buf
[0] & 0xffffffff;
2971 bool sign
= (image
>> 31) & 1;
2972 int exp
= (image
>> 23) & 0xff;
2974 memset (r
, 0, sizeof (*r
));
2975 image
<<= HOST_BITS_PER_LONG
- 24;
2980 if (image
&& fmt
->has_denorm
)
2984 SET_REAL_EXP (r
, -126);
2985 r
->sig
[SIGSZ
-1] = image
<< 1;
2988 else if (fmt
->has_signed_zero
)
2991 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2997 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2998 ^ fmt
->qnan_msb_set
);
2999 r
->sig
[SIGSZ
-1] = image
;
3011 SET_REAL_EXP (r
, exp
- 127 + 1);
3012 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
3016 const struct real_format ieee_single_format
=
3037 const struct real_format mips_single_format
=
3058 const struct real_format motorola_single_format
=
3079 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3080 single precision with the following differences:
3081 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3083 - NaNs are not supported.
3084 - The range of non-zero numbers in binary is
3085 (001)[1.]000...000 to (255)[1.]111...111.
3086 - Denormals can be represented, but are treated as +0.0 when
3087 used as an operand and are never generated as a result.
3088 - -0.0 can be represented, but a zero result is always +0.0.
3089 - the only supported rounding mode is trunction (towards zero). */
3090 const struct real_format spu_single_format
=
3111 /* IEEE double-precision format. */
3113 static void encode_ieee_double (const struct real_format
*fmt
,
3114 long *, const REAL_VALUE_TYPE
*);
3115 static void decode_ieee_double (const struct real_format
*,
3116 REAL_VALUE_TYPE
*, const long *);
3119 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3120 const REAL_VALUE_TYPE
*r
)
3122 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3123 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3125 image_hi
= r
->sign
<< 31;
3128 if (HOST_BITS_PER_LONG
== 64)
3130 sig_hi
= r
->sig
[SIGSZ
-1];
3131 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3132 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3136 sig_hi
= r
->sig
[SIGSZ
-1];
3137 sig_lo
= r
->sig
[SIGSZ
-2];
3138 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3139 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3149 image_hi
|= 2047 << 20;
3152 image_hi
|= 0x7fffffff;
3153 image_lo
= 0xffffffff;
3162 if (fmt
->canonical_nan_lsbs_set
)
3164 sig_hi
= (1 << 19) - 1;
3165 sig_lo
= 0xffffffff;
3173 if (r
->signalling
== fmt
->qnan_msb_set
)
3174 sig_hi
&= ~(1 << 19);
3177 if (sig_hi
== 0 && sig_lo
== 0)
3180 image_hi
|= 2047 << 20;
3186 image_hi
|= 0x7fffffff;
3187 image_lo
= 0xffffffff;
3192 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3193 whereas the intermediate representation is 0.F x 2**exp.
3194 Which means we're off by one. */
3198 exp
= REAL_EXP (r
) + 1023 - 1;
3199 image_hi
|= exp
<< 20;
3208 if (FLOAT_WORDS_BIG_ENDIAN
)
3209 buf
[0] = image_hi
, buf
[1] = image_lo
;
3211 buf
[0] = image_lo
, buf
[1] = image_hi
;
3215 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3218 unsigned long image_hi
, image_lo
;
3222 if (FLOAT_WORDS_BIG_ENDIAN
)
3223 image_hi
= buf
[0], image_lo
= buf
[1];
3225 image_lo
= buf
[0], image_hi
= buf
[1];
3226 image_lo
&= 0xffffffff;
3227 image_hi
&= 0xffffffff;
3229 sign
= (image_hi
>> 31) & 1;
3230 exp
= (image_hi
>> 20) & 0x7ff;
3232 memset (r
, 0, sizeof (*r
));
3234 image_hi
<<= 32 - 21;
3235 image_hi
|= image_lo
>> 21;
3236 image_hi
&= 0x7fffffff;
3237 image_lo
<<= 32 - 21;
3241 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3245 SET_REAL_EXP (r
, -1022);
3246 if (HOST_BITS_PER_LONG
== 32)
3248 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3250 r
->sig
[SIGSZ
-1] = image_hi
;
3251 r
->sig
[SIGSZ
-2] = image_lo
;
3255 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3256 r
->sig
[SIGSZ
-1] = image_hi
;
3260 else if (fmt
->has_signed_zero
)
3263 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3265 if (image_hi
|| image_lo
)
3269 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3270 if (HOST_BITS_PER_LONG
== 32)
3272 r
->sig
[SIGSZ
-1] = image_hi
;
3273 r
->sig
[SIGSZ
-2] = image_lo
;
3276 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3288 SET_REAL_EXP (r
, exp
- 1023 + 1);
3289 if (HOST_BITS_PER_LONG
== 32)
3291 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3292 r
->sig
[SIGSZ
-2] = image_lo
;
3295 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3299 const struct real_format ieee_double_format
=
3320 const struct real_format mips_double_format
=
3341 const struct real_format motorola_double_format
=
3362 /* IEEE extended real format. This comes in three flavors: Intel's as
3363 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3364 12- and 16-byte images may be big- or little endian; Motorola's is
3365 always big endian. */
3367 /* Helper subroutine which converts from the internal format to the
3368 12-byte little-endian Intel format. Functions below adjust this
3369 for the other possible formats. */
3371 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3372 const REAL_VALUE_TYPE
*r
)
3374 unsigned long image_hi
, sig_hi
, sig_lo
;
3375 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3377 image_hi
= r
->sign
<< 15;
3378 sig_hi
= sig_lo
= 0;
3390 /* Intel requires the explicit integer bit to be set, otherwise
3391 it considers the value a "pseudo-infinity". Motorola docs
3392 say it doesn't care. */
3393 sig_hi
= 0x80000000;
3398 sig_lo
= sig_hi
= 0xffffffff;
3408 if (fmt
->canonical_nan_lsbs_set
)
3410 sig_hi
= (1 << 30) - 1;
3411 sig_lo
= 0xffffffff;
3414 else if (HOST_BITS_PER_LONG
== 32)
3416 sig_hi
= r
->sig
[SIGSZ
-1];
3417 sig_lo
= r
->sig
[SIGSZ
-2];
3421 sig_lo
= r
->sig
[SIGSZ
-1];
3422 sig_hi
= sig_lo
>> 31 >> 1;
3423 sig_lo
&= 0xffffffff;
3425 if (r
->signalling
== fmt
->qnan_msb_set
)
3426 sig_hi
&= ~(1 << 30);
3429 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3432 /* Intel requires the explicit integer bit to be set, otherwise
3433 it considers the value a "pseudo-nan". Motorola docs say it
3435 sig_hi
|= 0x80000000;
3440 sig_lo
= sig_hi
= 0xffffffff;
3446 int exp
= REAL_EXP (r
);
3448 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3449 whereas the intermediate representation is 0.F x 2**exp.
3450 Which means we're off by one.
3452 Except for Motorola, which consider exp=0 and explicit
3453 integer bit set to continue to be normalized. In theory
3454 this discrepancy has been taken care of by the difference
3455 in fmt->emin in round_for_format. */
3462 gcc_assert (exp
>= 0);
3466 if (HOST_BITS_PER_LONG
== 32)
3468 sig_hi
= r
->sig
[SIGSZ
-1];
3469 sig_lo
= r
->sig
[SIGSZ
-2];
3473 sig_lo
= r
->sig
[SIGSZ
-1];
3474 sig_hi
= sig_lo
>> 31 >> 1;
3475 sig_lo
&= 0xffffffff;
3484 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3487 /* Convert from the internal format to the 12-byte Motorola format
3488 for an IEEE extended real. */
3490 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3491 const REAL_VALUE_TYPE
*r
)
3494 encode_ieee_extended (fmt
, intermed
, r
);
3496 if (r
->cl
== rvc_inf
)
3497 /* For infinity clear the explicit integer bit again, so that the
3498 format matches the canonical infinity generated by the FPU. */
3501 /* Motorola chips are assumed always to be big-endian. Also, the
3502 padding in a Motorola extended real goes between the exponent and
3503 the mantissa. At this point the mantissa is entirely within
3504 elements 0 and 1 of intermed, and the exponent entirely within
3505 element 2, so all we have to do is swap the order around, and
3506 shift element 2 left 16 bits. */
3507 buf
[0] = intermed
[2] << 16;
3508 buf
[1] = intermed
[1];
3509 buf
[2] = intermed
[0];
3512 /* Convert from the internal format to the 12-byte Intel format for
3513 an IEEE extended real. */
3515 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3516 const REAL_VALUE_TYPE
*r
)
3518 if (FLOAT_WORDS_BIG_ENDIAN
)
3520 /* All the padding in an Intel-format extended real goes at the high
3521 end, which in this case is after the mantissa, not the exponent.
3522 Therefore we must shift everything down 16 bits. */
3524 encode_ieee_extended (fmt
, intermed
, r
);
3525 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3526 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3527 buf
[2] = (intermed
[0] << 16);
3530 /* encode_ieee_extended produces what we want directly. */
3531 encode_ieee_extended (fmt
, buf
, r
);
3534 /* Convert from the internal format to the 16-byte Intel format for
3535 an IEEE extended real. */
3537 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3538 const REAL_VALUE_TYPE
*r
)
3540 /* All the padding in an Intel-format extended real goes at the high end. */
3541 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3545 /* As above, we have a helper function which converts from 12-byte
3546 little-endian Intel format to internal format. Functions below
3547 adjust for the other possible formats. */
3549 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3552 unsigned long image_hi
, sig_hi
, sig_lo
;
3556 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3557 sig_lo
&= 0xffffffff;
3558 sig_hi
&= 0xffffffff;
3559 image_hi
&= 0xffffffff;
3561 sign
= (image_hi
>> 15) & 1;
3562 exp
= image_hi
& 0x7fff;
3564 memset (r
, 0, sizeof (*r
));
3568 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3573 /* When the IEEE format contains a hidden bit, we know that
3574 it's zero at this point, and so shift up the significand
3575 and decrease the exponent to match. In this case, Motorola
3576 defines the explicit integer bit to be valid, so we don't
3577 know whether the msb is set or not. */
3578 SET_REAL_EXP (r
, fmt
->emin
);
3579 if (HOST_BITS_PER_LONG
== 32)
3581 r
->sig
[SIGSZ
-1] = sig_hi
;
3582 r
->sig
[SIGSZ
-2] = sig_lo
;
3585 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3589 else if (fmt
->has_signed_zero
)
3592 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3594 /* See above re "pseudo-infinities" and "pseudo-nans".
3595 Short summary is that the MSB will likely always be
3596 set, and that we don't care about it. */
3597 sig_hi
&= 0x7fffffff;
3599 if (sig_hi
|| sig_lo
)
3603 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
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
;
3622 SET_REAL_EXP (r
, exp
- 16383 + 1);
3623 if (HOST_BITS_PER_LONG
== 32)
3625 r
->sig
[SIGSZ
-1] = sig_hi
;
3626 r
->sig
[SIGSZ
-2] = sig_lo
;
3629 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3633 /* Convert from the internal format to the 12-byte Motorola format
3634 for an IEEE extended real. */
3636 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3641 /* Motorola chips are assumed always to be big-endian. Also, the
3642 padding in a Motorola extended real goes between the exponent and
3643 the mantissa; remove it. */
3644 intermed
[0] = buf
[2];
3645 intermed
[1] = buf
[1];
3646 intermed
[2] = (unsigned long)buf
[0] >> 16;
3648 decode_ieee_extended (fmt
, r
, intermed
);
3651 /* Convert from the internal format to the 12-byte Intel format for
3652 an IEEE extended real. */
3654 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3657 if (FLOAT_WORDS_BIG_ENDIAN
)
3659 /* All the padding in an Intel-format extended real goes at the high
3660 end, which in this case is after the mantissa, not the exponent.
3661 Therefore we must shift everything up 16 bits. */
3664 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3665 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3666 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3668 decode_ieee_extended (fmt
, r
, intermed
);
3671 /* decode_ieee_extended produces what we want directly. */
3672 decode_ieee_extended (fmt
, r
, buf
);
3675 /* Convert from the internal format to the 16-byte Intel format for
3676 an IEEE extended real. */
3678 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3681 /* All the padding in an Intel-format extended real goes at the high end. */
3682 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3685 const struct real_format ieee_extended_motorola_format
=
3687 encode_ieee_extended_motorola
,
3688 decode_ieee_extended_motorola
,
3706 const struct real_format ieee_extended_intel_96_format
=
3708 encode_ieee_extended_intel_96
,
3709 decode_ieee_extended_intel_96
,
3727 const struct real_format ieee_extended_intel_128_format
=
3729 encode_ieee_extended_intel_128
,
3730 decode_ieee_extended_intel_128
,
3748 /* The following caters to i386 systems that set the rounding precision
3749 to 53 bits instead of 64, e.g. FreeBSD. */
3750 const struct real_format ieee_extended_intel_96_round_53_format
=
3752 encode_ieee_extended_intel_96
,
3753 decode_ieee_extended_intel_96
,
3771 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3772 numbers whose sum is equal to the extended precision value. The number
3773 with greater magnitude is first. This format has the same magnitude
3774 range as an IEEE double precision value, but effectively 106 bits of
3775 significand precision. Infinity and NaN are represented by their IEEE
3776 double precision value stored in the first number, the second number is
3777 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3779 static void encode_ibm_extended (const struct real_format
*fmt
,
3780 long *, const REAL_VALUE_TYPE
*);
3781 static void decode_ibm_extended (const struct real_format
*,
3782 REAL_VALUE_TYPE
*, const long *);
3785 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3786 const REAL_VALUE_TYPE
*r
)
3788 REAL_VALUE_TYPE u
, normr
, v
;
3789 const struct real_format
*base_fmt
;
3791 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3793 /* Renormalize R before doing any arithmetic on it. */
3795 if (normr
.cl
== rvc_normal
)
3798 /* u = IEEE double precision portion of significand. */
3800 round_for_format (base_fmt
, &u
);
3801 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3803 if (u
.cl
== rvc_normal
)
3805 do_add (&v
, &normr
, &u
, 1);
3806 /* Call round_for_format since we might need to denormalize. */
3807 round_for_format (base_fmt
, &v
);
3808 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3812 /* Inf, NaN, 0 are all representable as doubles, so the
3813 least-significant part can be 0.0. */
3820 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3823 REAL_VALUE_TYPE u
, v
;
3824 const struct real_format
*base_fmt
;
3826 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3827 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3829 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3831 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3832 do_add (r
, &u
, &v
, 0);
3838 const struct real_format ibm_extended_format
=
3840 encode_ibm_extended
,
3841 decode_ibm_extended
,
3859 const struct real_format mips_extended_format
=
3861 encode_ibm_extended
,
3862 decode_ibm_extended
,
3881 /* IEEE quad precision format. */
3883 static void encode_ieee_quad (const struct real_format
*fmt
,
3884 long *, const REAL_VALUE_TYPE
*);
3885 static void decode_ieee_quad (const struct real_format
*,
3886 REAL_VALUE_TYPE
*, const long *);
3889 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3890 const REAL_VALUE_TYPE
*r
)
3892 unsigned long image3
, image2
, image1
, image0
, exp
;
3893 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3896 image3
= r
->sign
<< 31;
3901 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3910 image3
|= 32767 << 16;
3913 image3
|= 0x7fffffff;
3914 image2
= 0xffffffff;
3915 image1
= 0xffffffff;
3916 image0
= 0xffffffff;
3923 image3
|= 32767 << 16;
3927 if (fmt
->canonical_nan_lsbs_set
)
3930 image2
= image1
= image0
= 0xffffffff;
3933 else if (HOST_BITS_PER_LONG
== 32)
3938 image3
|= u
.sig
[3] & 0xffff;
3943 image1
= image0
>> 31 >> 1;
3945 image3
|= (image2
>> 31 >> 1) & 0xffff;
3946 image0
&= 0xffffffff;
3947 image2
&= 0xffffffff;
3949 if (r
->signalling
== fmt
->qnan_msb_set
)
3953 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3958 image3
|= 0x7fffffff;
3959 image2
= 0xffffffff;
3960 image1
= 0xffffffff;
3961 image0
= 0xffffffff;
3966 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3967 whereas the intermediate representation is 0.F x 2**exp.
3968 Which means we're off by one. */
3972 exp
= REAL_EXP (r
) + 16383 - 1;
3973 image3
|= exp
<< 16;
3975 if (HOST_BITS_PER_LONG
== 32)
3980 image3
|= u
.sig
[3] & 0xffff;
3985 image1
= image0
>> 31 >> 1;
3987 image3
|= (image2
>> 31 >> 1) & 0xffff;
3988 image0
&= 0xffffffff;
3989 image2
&= 0xffffffff;
3997 if (FLOAT_WORDS_BIG_ENDIAN
)
4014 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4017 unsigned long image3
, image2
, image1
, image0
;
4021 if (FLOAT_WORDS_BIG_ENDIAN
)
4035 image0
&= 0xffffffff;
4036 image1
&= 0xffffffff;
4037 image2
&= 0xffffffff;
4039 sign
= (image3
>> 31) & 1;
4040 exp
= (image3
>> 16) & 0x7fff;
4043 memset (r
, 0, sizeof (*r
));
4047 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4052 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4053 if (HOST_BITS_PER_LONG
== 32)
4062 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4063 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4068 else if (fmt
->has_signed_zero
)
4071 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4073 if (image3
| image2
| image1
| image0
)
4077 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4079 if (HOST_BITS_PER_LONG
== 32)
4088 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4089 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4091 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4103 SET_REAL_EXP (r
, exp
- 16383 + 1);
4105 if (HOST_BITS_PER_LONG
== 32)
4114 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4115 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4117 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4118 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4122 const struct real_format ieee_quad_format
=
4143 const struct real_format mips_quad_format
=
4164 /* Descriptions of VAX floating point formats can be found beginning at
4166 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4168 The thing to remember is that they're almost IEEE, except for word
4169 order, exponent bias, and the lack of infinities, nans, and denormals.
4171 We don't implement the H_floating format here, simply because neither
4172 the VAX or Alpha ports use it. */
4174 static void encode_vax_f (const struct real_format
*fmt
,
4175 long *, const REAL_VALUE_TYPE
*);
4176 static void decode_vax_f (const struct real_format
*,
4177 REAL_VALUE_TYPE
*, const long *);
4178 static void encode_vax_d (const struct real_format
*fmt
,
4179 long *, const REAL_VALUE_TYPE
*);
4180 static void decode_vax_d (const struct real_format
*,
4181 REAL_VALUE_TYPE
*, const long *);
4182 static void encode_vax_g (const struct real_format
*fmt
,
4183 long *, const REAL_VALUE_TYPE
*);
4184 static void decode_vax_g (const struct real_format
*,
4185 REAL_VALUE_TYPE
*, const long *);
4188 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4189 const REAL_VALUE_TYPE
*r
)
4191 unsigned long sign
, exp
, sig
, image
;
4193 sign
= r
->sign
<< 15;
4203 image
= 0xffff7fff | sign
;
4207 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4208 exp
= REAL_EXP (r
) + 128;
4210 image
= (sig
<< 16) & 0xffff0000;
4224 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4225 REAL_VALUE_TYPE
*r
, const long *buf
)
4227 unsigned long image
= buf
[0] & 0xffffffff;
4228 int exp
= (image
>> 7) & 0xff;
4230 memset (r
, 0, sizeof (*r
));
4235 r
->sign
= (image
>> 15) & 1;
4236 SET_REAL_EXP (r
, exp
- 128);
4238 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4239 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4244 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4245 const REAL_VALUE_TYPE
*r
)
4247 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4252 image0
= image1
= 0;
4257 image0
= 0xffff7fff | sign
;
4258 image1
= 0xffffffff;
4262 /* Extract the significand into straight hi:lo. */
4263 if (HOST_BITS_PER_LONG
== 64)
4265 image0
= r
->sig
[SIGSZ
-1];
4266 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4267 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4271 image0
= r
->sig
[SIGSZ
-1];
4272 image1
= r
->sig
[SIGSZ
-2];
4273 image1
= (image0
<< 24) | (image1
>> 8);
4274 image0
= (image0
>> 8) & 0xffffff;
4277 /* Rearrange the half-words of the significand to match the
4279 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4280 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4282 /* Add the sign and exponent. */
4284 image0
|= (REAL_EXP (r
) + 128) << 7;
4291 if (FLOAT_WORDS_BIG_ENDIAN
)
4292 buf
[0] = image1
, buf
[1] = image0
;
4294 buf
[0] = image0
, buf
[1] = image1
;
4298 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4299 REAL_VALUE_TYPE
*r
, const long *buf
)
4301 unsigned long image0
, image1
;
4304 if (FLOAT_WORDS_BIG_ENDIAN
)
4305 image1
= buf
[0], image0
= buf
[1];
4307 image0
= buf
[0], image1
= buf
[1];
4308 image0
&= 0xffffffff;
4309 image1
&= 0xffffffff;
4311 exp
= (image0
>> 7) & 0xff;
4313 memset (r
, 0, sizeof (*r
));
4318 r
->sign
= (image0
>> 15) & 1;
4319 SET_REAL_EXP (r
, exp
- 128);
4321 /* Rearrange the half-words of the external format into
4322 proper ascending order. */
4323 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4324 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4326 if (HOST_BITS_PER_LONG
== 64)
4328 image0
= (image0
<< 31 << 1) | image1
;
4331 r
->sig
[SIGSZ
-1] = image0
;
4335 r
->sig
[SIGSZ
-1] = image0
;
4336 r
->sig
[SIGSZ
-2] = image1
;
4337 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4338 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4344 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4345 const REAL_VALUE_TYPE
*r
)
4347 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4352 image0
= image1
= 0;
4357 image0
= 0xffff7fff | sign
;
4358 image1
= 0xffffffff;
4362 /* Extract the significand into straight hi:lo. */
4363 if (HOST_BITS_PER_LONG
== 64)
4365 image0
= r
->sig
[SIGSZ
-1];
4366 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4367 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4371 image0
= r
->sig
[SIGSZ
-1];
4372 image1
= r
->sig
[SIGSZ
-2];
4373 image1
= (image0
<< 21) | (image1
>> 11);
4374 image0
= (image0
>> 11) & 0xfffff;
4377 /* Rearrange the half-words of the significand to match the
4379 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4380 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4382 /* Add the sign and exponent. */
4384 image0
|= (REAL_EXP (r
) + 1024) << 4;
4391 if (FLOAT_WORDS_BIG_ENDIAN
)
4392 buf
[0] = image1
, buf
[1] = image0
;
4394 buf
[0] = image0
, buf
[1] = image1
;
4398 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4399 REAL_VALUE_TYPE
*r
, const long *buf
)
4401 unsigned long image0
, image1
;
4404 if (FLOAT_WORDS_BIG_ENDIAN
)
4405 image1
= buf
[0], image0
= buf
[1];
4407 image0
= buf
[0], image1
= buf
[1];
4408 image0
&= 0xffffffff;
4409 image1
&= 0xffffffff;
4411 exp
= (image0
>> 4) & 0x7ff;
4413 memset (r
, 0, sizeof (*r
));
4418 r
->sign
= (image0
>> 15) & 1;
4419 SET_REAL_EXP (r
, exp
- 1024);
4421 /* Rearrange the half-words of the external format into
4422 proper ascending order. */
4423 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4424 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4426 if (HOST_BITS_PER_LONG
== 64)
4428 image0
= (image0
<< 31 << 1) | image1
;
4431 r
->sig
[SIGSZ
-1] = image0
;
4435 r
->sig
[SIGSZ
-1] = image0
;
4436 r
->sig
[SIGSZ
-2] = image1
;
4437 lshift_significand (r
, r
, 64 - 53);
4438 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4443 const struct real_format vax_f_format
=
4464 const struct real_format vax_d_format
=
4485 const struct real_format vax_g_format
=
4506 /* Encode real R into a single precision DFP value in BUF. */
4508 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4509 long *buf ATTRIBUTE_UNUSED
,
4510 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4512 encode_decimal32 (fmt
, buf
, r
);
4515 /* Decode a single precision DFP value in BUF into a real R. */
4517 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4518 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4519 const long *buf ATTRIBUTE_UNUSED
)
4521 decode_decimal32 (fmt
, r
, buf
);
4524 /* Encode real R into a double precision DFP value in BUF. */
4526 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4527 long *buf ATTRIBUTE_UNUSED
,
4528 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4530 encode_decimal64 (fmt
, buf
, r
);
4533 /* Decode a double precision DFP value in BUF into a real R. */
4535 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4536 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4537 const long *buf ATTRIBUTE_UNUSED
)
4539 decode_decimal64 (fmt
, r
, buf
);
4542 /* Encode real R into a quad precision DFP value in BUF. */
4544 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4545 long *buf ATTRIBUTE_UNUSED
,
4546 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4548 encode_decimal128 (fmt
, buf
, r
);
4551 /* Decode a quad precision DFP value in BUF into a real R. */
4553 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4554 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4555 const long *buf ATTRIBUTE_UNUSED
)
4557 decode_decimal128 (fmt
, r
, buf
);
4560 /* Single precision decimal floating point (IEEE 754). */
4561 const struct real_format decimal_single_format
=
4563 encode_decimal_single
,
4564 decode_decimal_single
,
4582 /* Double precision decimal floating point (IEEE 754). */
4583 const struct real_format decimal_double_format
=
4585 encode_decimal_double
,
4586 decode_decimal_double
,
4604 /* Quad precision decimal floating point (IEEE 754). */
4605 const struct real_format decimal_quad_format
=
4607 encode_decimal_quad
,
4608 decode_decimal_quad
,
4626 /* Encode half-precision floats. This routine is used both for the IEEE
4627 ARM alternative encodings. */
4629 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4630 const REAL_VALUE_TYPE
*r
)
4632 unsigned long image
, sig
, exp
;
4633 unsigned long sign
= r
->sign
;
4634 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4637 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4655 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4656 if (r
->signalling
== fmt
->qnan_msb_set
)
4671 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4672 whereas the intermediate representation is 0.F x 2**exp.
4673 Which means we're off by one. */
4677 exp
= REAL_EXP (r
) + 15 - 1;
4689 /* Decode half-precision floats. This routine is used both for the IEEE
4690 ARM alternative encodings. */
4692 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4695 unsigned long image
= buf
[0] & 0xffff;
4696 bool sign
= (image
>> 15) & 1;
4697 int exp
= (image
>> 10) & 0x1f;
4699 memset (r
, 0, sizeof (*r
));
4700 image
<<= HOST_BITS_PER_LONG
- 11;
4705 if (image
&& fmt
->has_denorm
)
4709 SET_REAL_EXP (r
, -14);
4710 r
->sig
[SIGSZ
-1] = image
<< 1;
4713 else if (fmt
->has_signed_zero
)
4716 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4722 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4723 ^ fmt
->qnan_msb_set
);
4724 r
->sig
[SIGSZ
-1] = image
;
4736 SET_REAL_EXP (r
, exp
- 15 + 1);
4737 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4741 /* Half-precision format, as specified in IEEE 754R. */
4742 const struct real_format ieee_half_format
=
4763 /* ARM's alternative half-precision format, similar to IEEE but with
4764 no reserved exponent value for NaNs and infinities; rather, it just
4765 extends the range of exponents by one. */
4766 const struct real_format arm_half_format
=
4787 /* A synthetic "format" for internal arithmetic. It's the size of the
4788 internal significand minus the two bits needed for proper rounding.
4789 The encode and decode routines exist only to satisfy our paranoia
4792 static void encode_internal (const struct real_format
*fmt
,
4793 long *, const REAL_VALUE_TYPE
*);
4794 static void decode_internal (const struct real_format
*,
4795 REAL_VALUE_TYPE
*, const long *);
4798 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4799 const REAL_VALUE_TYPE
*r
)
4801 memcpy (buf
, r
, sizeof (*r
));
4805 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4806 REAL_VALUE_TYPE
*r
, const long *buf
)
4808 memcpy (r
, buf
, sizeof (*r
));
4811 const struct real_format real_internal_format
=
4816 SIGNIFICAND_BITS
- 2,
4817 SIGNIFICAND_BITS
- 2,
4832 /* Calculate X raised to the integer exponent N in mode MODE and store
4833 the result in R. Return true if the result may be inexact due to
4834 loss of precision. The algorithm is the classic "left-to-right binary
4835 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4836 Algorithms", "The Art of Computer Programming", Volume 2. */
4839 real_powi (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4840 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4842 unsigned HOST_WIDE_INT bit
;
4844 bool inexact
= false;
4856 /* Don't worry about overflow, from now on n is unsigned. */
4864 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4865 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4869 inexact
|= do_multiply (&t
, &t
, &t
);
4871 inexact
|= do_multiply (&t
, &t
, x
);
4879 inexact
|= do_divide (&t
, &dconst1
, &t
);
4881 real_convert (r
, mode
, &t
);
4885 /* Round X to the nearest integer not larger in absolute value, i.e.
4886 towards zero, placing the result in R in mode MODE. */
4889 real_trunc (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4890 const REAL_VALUE_TYPE
*x
)
4892 do_fix_trunc (r
, x
);
4893 if (mode
!= VOIDmode
)
4894 real_convert (r
, mode
, r
);
4897 /* Round X to the largest integer not greater in value, i.e. round
4898 down, placing the result in R in mode MODE. */
4901 real_floor (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4902 const REAL_VALUE_TYPE
*x
)
4906 do_fix_trunc (&t
, x
);
4907 if (! real_identical (&t
, x
) && x
->sign
)
4908 do_add (&t
, &t
, &dconstm1
, 0);
4909 if (mode
!= VOIDmode
)
4910 real_convert (r
, mode
, &t
);
4915 /* Round X to the smallest integer not less then argument, i.e. round
4916 up, placing the result in R in mode MODE. */
4919 real_ceil (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4920 const REAL_VALUE_TYPE
*x
)
4924 do_fix_trunc (&t
, x
);
4925 if (! real_identical (&t
, x
) && ! x
->sign
)
4926 do_add (&t
, &t
, &dconst1
, 0);
4927 if (mode
!= VOIDmode
)
4928 real_convert (r
, mode
, &t
);
4933 /* Round X to the nearest integer, but round halfway cases away from
4937 real_round (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4938 const REAL_VALUE_TYPE
*x
)
4940 do_add (r
, x
, &dconsthalf
, x
->sign
);
4941 do_fix_trunc (r
, r
);
4942 if (mode
!= VOIDmode
)
4943 real_convert (r
, mode
, r
);
4946 /* Set the sign of R to the sign of X. */
4949 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4954 /* Check whether the real constant value given is an integer. */
4957 real_isinteger (const REAL_VALUE_TYPE
*c
, machine_mode mode
)
4959 REAL_VALUE_TYPE cint
;
4961 real_trunc (&cint
, mode
, c
);
4962 return real_identical (c
, &cint
);
4965 /* Write into BUF the maximum representable finite floating-point
4966 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4967 float string. LEN is the size of BUF, and the buffer must be large
4968 enough to contain the resulting string. */
4971 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
4976 strcpy (buf
, "0x0.");
4978 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
4981 *p
++ = "08ce"[n
- i
];
4982 sprintf (p
, "p%d", fmt
->emax
);
4983 if (fmt
->pnan
< fmt
->p
)
4985 /* This is an IBM extended double format made up of two IEEE
4986 doubles. The value of the long double is the sum of the
4987 values of the two parts. The most significant part is
4988 required to be the value of the long double rounded to the
4989 nearest double. Rounding means we need a slightly smaller
4990 value for LDBL_MAX. */
4991 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
4994 gcc_assert (strlen (buf
) < len
);
4997 /* True if mode M has a NaN representation and
4998 the treatment of NaN operands is important. */
5001 HONOR_NANS (machine_mode m
)
5003 return MODE_HAS_NANS (m
) && !flag_finite_math_only
;
5007 HONOR_NANS (const_tree t
)
5009 return HONOR_NANS (element_mode (t
));
5013 HONOR_NANS (const_rtx x
)
5015 return HONOR_NANS (GET_MODE (x
));
5018 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5021 HONOR_SNANS (machine_mode m
)
5023 return flag_signaling_nans
&& HONOR_NANS (m
);
5027 HONOR_SNANS (const_tree t
)
5029 return HONOR_SNANS (element_mode (t
));
5033 HONOR_SNANS (const_rtx x
)
5035 return HONOR_SNANS (GET_MODE (x
));
5038 /* As for HONOR_NANS, but true if the mode can represent infinity and
5039 the treatment of infinite values is important. */
5042 HONOR_INFINITIES (machine_mode m
)
5044 return MODE_HAS_INFINITIES (m
) && !flag_finite_math_only
;
5048 HONOR_INFINITIES (const_tree t
)
5050 return HONOR_INFINITIES (element_mode (t
));
5054 HONOR_INFINITIES (const_rtx x
)
5056 return HONOR_INFINITIES (GET_MODE (x
));
5059 /* Like HONOR_NANS, but true if the given mode distinguishes between
5060 positive and negative zero, and the sign of zero is important. */
5063 HONOR_SIGNED_ZEROS (machine_mode m
)
5065 return MODE_HAS_SIGNED_ZEROS (m
) && flag_signed_zeros
;
5069 HONOR_SIGNED_ZEROS (const_tree t
)
5071 return HONOR_SIGNED_ZEROS (element_mode (t
));
5075 HONOR_SIGNED_ZEROS (const_rtx x
)
5077 return HONOR_SIGNED_ZEROS (GET_MODE (x
));
5080 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5081 and the rounding mode is important. */
5084 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m
)
5086 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m
) && flag_rounding_math
;
5090 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t
)
5092 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t
));
5096 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x
)
5098 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x
));