1 /* real.c - software floating point emulation.
2 Copyright (C) 1993-2014 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"
27 #include "diagnostic-core.h"
34 /* The floating point model used internally is not exactly IEEE 754
35 compliant, and close to the description in the ISO C99 standard,
36 section 5.2.4.2.2 Characteristics of floating types.
40 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
44 b = base or radix, here always 2
46 p = precision (the number of base-b digits in the significand)
47 f_k = the digits of the significand.
49 We differ from typical IEEE 754 encodings in that the entire
50 significand is fractional. Normalized significands are in the
53 A requirement of the model is that P be larger than the largest
54 supported target floating-point type by at least 2 bits. This gives
55 us proper rounding when we truncate to the target type. In addition,
56 E must be large enough to hold the smallest supported denormal number
59 Both of these requirements are easily satisfied. The largest target
60 significand is 113 bits; we store at least 160. The smallest
61 denormal number fits in 17 exponent bits; we store 26. */
64 /* Used to classify two numbers simultaneously. */
65 #define CLASS2(A, B) ((A) << 2 | (B))
67 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
68 #error "Some constant folding done by hand to avoid shift count warnings"
71 static void get_zero (REAL_VALUE_TYPE
*, int);
72 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
73 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
74 static void get_inf (REAL_VALUE_TYPE
*, int);
75 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
76 const REAL_VALUE_TYPE
*, unsigned int);
77 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
79 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
81 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
82 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
83 const REAL_VALUE_TYPE
*);
84 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
85 const REAL_VALUE_TYPE
*, int);
86 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
87 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
88 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
89 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
90 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
91 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
92 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
93 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
94 const REAL_VALUE_TYPE
*);
95 static void normalize (REAL_VALUE_TYPE
*);
97 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
98 const REAL_VALUE_TYPE
*, int);
99 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
100 const REAL_VALUE_TYPE
*);
101 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
102 const REAL_VALUE_TYPE
*);
103 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
104 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
106 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, REAL_VALUE_TYPE
*);
107 static void decimal_from_integer (REAL_VALUE_TYPE
*);
108 static void decimal_integer_string (char *, const REAL_VALUE_TYPE
*,
111 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
112 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
113 static const REAL_VALUE_TYPE
* real_digit (int);
114 static void times_pten (REAL_VALUE_TYPE
*, int);
116 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
118 /* Initialize R with a positive zero. */
121 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
123 memset (r
, 0, sizeof (*r
));
127 /* Initialize R with the canonical quiet NaN. */
130 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
132 memset (r
, 0, sizeof (*r
));
139 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
141 memset (r
, 0, sizeof (*r
));
149 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
151 memset (r
, 0, sizeof (*r
));
157 /* Right-shift the significand of A by N bits; put the result in the
158 significand of R. If any one bits are shifted out, return true. */
161 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
164 unsigned long sticky
= 0;
165 unsigned int i
, ofs
= 0;
167 if (n
>= HOST_BITS_PER_LONG
)
169 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
171 n
&= HOST_BITS_PER_LONG
- 1;
176 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
177 for (i
= 0; i
< SIGSZ
; ++i
)
180 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
181 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
182 << (HOST_BITS_PER_LONG
- n
)));
187 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
188 r
->sig
[i
] = a
->sig
[ofs
+ i
];
189 for (; i
< SIGSZ
; ++i
)
196 /* Right-shift the significand of A by N bits; put the result in the
200 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
203 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
205 n
&= HOST_BITS_PER_LONG
- 1;
208 for (i
= 0; i
< SIGSZ
; ++i
)
211 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
212 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
213 << (HOST_BITS_PER_LONG
- n
)));
218 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
219 r
->sig
[i
] = a
->sig
[ofs
+ i
];
220 for (; i
< SIGSZ
; ++i
)
225 /* Left-shift the significand of A by N bits; put the result in the
229 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
232 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
234 n
&= HOST_BITS_PER_LONG
- 1;
237 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
238 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
239 for (; i
< SIGSZ
; ++i
)
240 r
->sig
[SIGSZ
-1-i
] = 0;
243 for (i
= 0; i
< SIGSZ
; ++i
)
246 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
247 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
248 >> (HOST_BITS_PER_LONG
- n
)));
252 /* Likewise, but N is specialized to 1. */
255 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
259 for (i
= SIGSZ
- 1; i
> 0; --i
)
260 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
261 r
->sig
[0] = a
->sig
[0] << 1;
264 /* Add the significands of A and B, placing the result in R. Return
265 true if there was carry out of the most significant word. */
268 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
269 const REAL_VALUE_TYPE
*b
)
274 for (i
= 0; i
< SIGSZ
; ++i
)
276 unsigned long ai
= a
->sig
[i
];
277 unsigned long ri
= ai
+ b
->sig
[i
];
293 /* Subtract the significands of A and B, placing the result in R. CARRY is
294 true if there's a borrow incoming to the least significant word.
295 Return true if there was borrow out of the most significant word. */
298 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
299 const REAL_VALUE_TYPE
*b
, int carry
)
303 for (i
= 0; i
< SIGSZ
; ++i
)
305 unsigned long ai
= a
->sig
[i
];
306 unsigned long ri
= ai
- b
->sig
[i
];
322 /* Negate the significand A, placing the result in R. */
325 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
330 for (i
= 0; i
< SIGSZ
; ++i
)
332 unsigned long ri
, ai
= a
->sig
[i
];
351 /* Compare significands. Return tri-state vs zero. */
354 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
358 for (i
= SIGSZ
- 1; i
>= 0; --i
)
360 unsigned long ai
= a
->sig
[i
];
361 unsigned long bi
= b
->sig
[i
];
372 /* Return true if A is nonzero. */
375 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
379 for (i
= SIGSZ
- 1; i
>= 0; --i
)
386 /* Set bit N of the significand of R. */
389 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
391 r
->sig
[n
/ HOST_BITS_PER_LONG
]
392 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
395 /* Clear bit N of the significand of R. */
398 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
400 r
->sig
[n
/ HOST_BITS_PER_LONG
]
401 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
404 /* Test bit N of the significand of R. */
407 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
409 /* ??? Compiler bug here if we return this expression directly.
410 The conversion to bool strips the "&1" and we wind up testing
411 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
412 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
416 /* Clear bits 0..N-1 of the significand of R. */
419 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
421 int i
, w
= n
/ HOST_BITS_PER_LONG
;
423 for (i
= 0; i
< w
; ++i
)
426 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
429 /* Divide the significands of A and B, placing the result in R. Return
430 true if the division was inexact. */
433 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
434 const REAL_VALUE_TYPE
*b
)
437 int i
, bit
= SIGNIFICAND_BITS
- 1;
438 unsigned long msb
, inexact
;
441 memset (r
->sig
, 0, sizeof (r
->sig
));
447 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
448 lshift_significand_1 (&u
, &u
);
450 if (msb
|| cmp_significands (&u
, b
) >= 0)
452 sub_significands (&u
, &u
, b
, 0);
453 set_significand_bit (r
, bit
);
458 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
464 /* Adjust the exponent and significand of R such that the most
465 significant bit is set. We underflow to zero and overflow to
466 infinity here, without denormals. (The intermediate representation
467 exponent is large enough to handle target denormals normalized.) */
470 normalize (REAL_VALUE_TYPE
*r
)
478 /* Find the first word that is nonzero. */
479 for (i
= SIGSZ
- 1; i
>= 0; i
--)
481 shift
+= HOST_BITS_PER_LONG
;
485 /* Zero significand flushes to zero. */
493 /* Find the first bit that is nonzero. */
495 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
501 exp
= REAL_EXP (r
) - shift
;
503 get_inf (r
, r
->sign
);
504 else if (exp
< -MAX_EXP
)
505 get_zero (r
, r
->sign
);
508 SET_REAL_EXP (r
, exp
);
509 lshift_significand (r
, r
, shift
);
514 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
515 result may be inexact due to a loss of precision. */
518 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
519 const REAL_VALUE_TYPE
*b
, int subtract_p
)
523 bool inexact
= false;
525 /* Determine if we need to add or subtract. */
527 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
529 switch (CLASS2 (a
->cl
, b
->cl
))
531 case CLASS2 (rvc_zero
, rvc_zero
):
532 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
533 get_zero (r
, sign
& !subtract_p
);
536 case CLASS2 (rvc_zero
, rvc_normal
):
537 case CLASS2 (rvc_zero
, rvc_inf
):
538 case CLASS2 (rvc_zero
, rvc_nan
):
540 case CLASS2 (rvc_normal
, rvc_nan
):
541 case CLASS2 (rvc_inf
, rvc_nan
):
542 case CLASS2 (rvc_nan
, rvc_nan
):
543 /* ANY + NaN = NaN. */
544 case CLASS2 (rvc_normal
, rvc_inf
):
547 r
->sign
= sign
^ subtract_p
;
550 case CLASS2 (rvc_normal
, rvc_zero
):
551 case CLASS2 (rvc_inf
, rvc_zero
):
552 case CLASS2 (rvc_nan
, rvc_zero
):
554 case CLASS2 (rvc_nan
, rvc_normal
):
555 case CLASS2 (rvc_nan
, rvc_inf
):
556 /* NaN + ANY = NaN. */
557 case CLASS2 (rvc_inf
, rvc_normal
):
562 case CLASS2 (rvc_inf
, rvc_inf
):
564 /* Inf - Inf = NaN. */
565 get_canonical_qnan (r
, 0);
567 /* Inf + Inf = Inf. */
571 case CLASS2 (rvc_normal
, rvc_normal
):
578 /* Swap the arguments such that A has the larger exponent. */
579 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
582 const REAL_VALUE_TYPE
*t
;
589 /* If the exponents are not identical, we need to shift the
590 significand of B down. */
593 /* If the exponents are too far apart, the significands
594 do not overlap, which makes the subtraction a noop. */
595 if (dexp
>= SIGNIFICAND_BITS
)
602 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
608 if (sub_significands (r
, a
, b
, inexact
))
610 /* We got a borrow out of the subtraction. That means that
611 A and B had the same exponent, and B had the larger
612 significand. We need to swap the sign and negate the
615 neg_significand (r
, r
);
620 if (add_significands (r
, a
, b
))
622 /* We got carry out of the addition. This means we need to
623 shift the significand back down one bit and increase the
625 inexact
|= sticky_rshift_significand (r
, r
, 1);
626 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
637 SET_REAL_EXP (r
, exp
);
638 /* Zero out the remaining fields. */
643 /* Re-normalize the result. */
646 /* Special case: if the subtraction results in zero, the result
648 if (r
->cl
== rvc_zero
)
651 r
->sig
[0] |= inexact
;
656 /* Calculate R = A * B. Return true if the result may be inexact. */
659 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
660 const REAL_VALUE_TYPE
*b
)
662 REAL_VALUE_TYPE u
, t
, *rr
;
663 unsigned int i
, j
, k
;
664 int sign
= a
->sign
^ b
->sign
;
665 bool inexact
= false;
667 switch (CLASS2 (a
->cl
, b
->cl
))
669 case CLASS2 (rvc_zero
, rvc_zero
):
670 case CLASS2 (rvc_zero
, rvc_normal
):
671 case CLASS2 (rvc_normal
, rvc_zero
):
672 /* +-0 * ANY = 0 with appropriate sign. */
676 case CLASS2 (rvc_zero
, rvc_nan
):
677 case CLASS2 (rvc_normal
, rvc_nan
):
678 case CLASS2 (rvc_inf
, rvc_nan
):
679 case CLASS2 (rvc_nan
, rvc_nan
):
680 /* ANY * NaN = NaN. */
685 case CLASS2 (rvc_nan
, rvc_zero
):
686 case CLASS2 (rvc_nan
, rvc_normal
):
687 case CLASS2 (rvc_nan
, rvc_inf
):
688 /* NaN * ANY = NaN. */
693 case CLASS2 (rvc_zero
, rvc_inf
):
694 case CLASS2 (rvc_inf
, rvc_zero
):
696 get_canonical_qnan (r
, sign
);
699 case CLASS2 (rvc_inf
, rvc_inf
):
700 case CLASS2 (rvc_normal
, rvc_inf
):
701 case CLASS2 (rvc_inf
, rvc_normal
):
702 /* Inf * Inf = Inf, R * Inf = Inf */
706 case CLASS2 (rvc_normal
, rvc_normal
):
713 if (r
== a
|| r
== b
)
719 /* Collect all the partial products. Since we don't have sure access
720 to a widening multiply, we split each long into two half-words.
722 Consider the long-hand form of a four half-word multiplication:
732 We construct partial products of the widened half-word products
733 that are known to not overlap, e.g. DF+DH. Each such partial
734 product is given its proper exponent, which allows us to sum them
735 and obtain the finished product. */
737 for (i
= 0; i
< SIGSZ
* 2; ++i
)
739 unsigned long ai
= a
->sig
[i
/ 2];
741 ai
>>= HOST_BITS_PER_LONG
/ 2;
743 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
748 for (j
= 0; j
< 2; ++j
)
750 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
751 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
760 /* Would underflow to zero, which we shouldn't bother adding. */
765 memset (&u
, 0, sizeof (u
));
767 SET_REAL_EXP (&u
, exp
);
769 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
771 unsigned long bi
= b
->sig
[k
/ 2];
773 bi
>>= HOST_BITS_PER_LONG
/ 2;
775 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
777 u
.sig
[k
/ 2] = ai
* bi
;
781 inexact
|= do_add (rr
, rr
, &u
, 0);
792 /* Calculate R = A / B. Return true if the result may be inexact. */
795 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
796 const REAL_VALUE_TYPE
*b
)
798 int exp
, sign
= a
->sign
^ b
->sign
;
799 REAL_VALUE_TYPE t
, *rr
;
802 switch (CLASS2 (a
->cl
, b
->cl
))
804 case CLASS2 (rvc_zero
, rvc_zero
):
806 case CLASS2 (rvc_inf
, rvc_inf
):
807 /* Inf / Inf = NaN. */
808 get_canonical_qnan (r
, sign
);
811 case CLASS2 (rvc_zero
, rvc_normal
):
812 case CLASS2 (rvc_zero
, rvc_inf
):
814 case CLASS2 (rvc_normal
, rvc_inf
):
819 case CLASS2 (rvc_normal
, rvc_zero
):
821 case CLASS2 (rvc_inf
, rvc_zero
):
826 case CLASS2 (rvc_zero
, rvc_nan
):
827 case CLASS2 (rvc_normal
, rvc_nan
):
828 case CLASS2 (rvc_inf
, rvc_nan
):
829 case CLASS2 (rvc_nan
, rvc_nan
):
830 /* ANY / NaN = NaN. */
835 case CLASS2 (rvc_nan
, rvc_zero
):
836 case CLASS2 (rvc_nan
, rvc_normal
):
837 case CLASS2 (rvc_nan
, rvc_inf
):
838 /* NaN / ANY = NaN. */
843 case CLASS2 (rvc_inf
, rvc_normal
):
848 case CLASS2 (rvc_normal
, rvc_normal
):
855 if (r
== a
|| r
== b
)
860 /* Make sure all fields in the result are initialized. */
865 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
876 SET_REAL_EXP (rr
, exp
);
878 inexact
= div_significands (rr
, a
, b
);
880 /* Re-normalize the result. */
882 rr
->sig
[0] |= inexact
;
890 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
891 one of the two operands is a NaN. */
894 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
899 switch (CLASS2 (a
->cl
, b
->cl
))
901 case CLASS2 (rvc_zero
, rvc_zero
):
902 /* Sign of zero doesn't matter for compares. */
905 case CLASS2 (rvc_normal
, rvc_zero
):
906 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
908 return decimal_do_compare (a
, b
, nan_result
);
910 case CLASS2 (rvc_inf
, rvc_zero
):
911 case CLASS2 (rvc_inf
, rvc_normal
):
912 return (a
->sign
? -1 : 1);
914 case CLASS2 (rvc_inf
, rvc_inf
):
915 return -a
->sign
- -b
->sign
;
917 case CLASS2 (rvc_zero
, rvc_normal
):
918 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
920 return decimal_do_compare (a
, b
, nan_result
);
922 case CLASS2 (rvc_zero
, rvc_inf
):
923 case CLASS2 (rvc_normal
, rvc_inf
):
924 return (b
->sign
? 1 : -1);
926 case CLASS2 (rvc_zero
, rvc_nan
):
927 case CLASS2 (rvc_normal
, rvc_nan
):
928 case CLASS2 (rvc_inf
, rvc_nan
):
929 case CLASS2 (rvc_nan
, rvc_nan
):
930 case CLASS2 (rvc_nan
, rvc_zero
):
931 case CLASS2 (rvc_nan
, rvc_normal
):
932 case CLASS2 (rvc_nan
, rvc_inf
):
935 case CLASS2 (rvc_normal
, rvc_normal
):
942 if (a
->sign
!= b
->sign
)
943 return -a
->sign
- -b
->sign
;
945 if (a
->decimal
|| b
->decimal
)
946 return decimal_do_compare (a
, b
, nan_result
);
948 if (REAL_EXP (a
) > REAL_EXP (b
))
950 else if (REAL_EXP (a
) < REAL_EXP (b
))
953 ret
= cmp_significands (a
, b
);
955 return (a
->sign
? -ret
: ret
);
958 /* Return A truncated to an integral value toward zero. */
961 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
975 decimal_do_fix_trunc (r
, a
);
978 if (REAL_EXP (r
) <= 0)
979 get_zero (r
, r
->sign
);
980 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
981 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
989 /* Perform the binary or unary operation described by CODE.
990 For a unary operation, leave OP1 NULL. This function returns
991 true if the result may be inexact due to loss of precision. */
994 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
995 const REAL_VALUE_TYPE
*op1
)
997 enum tree_code code
= (enum tree_code
) icode
;
999 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1000 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1005 /* Clear any padding areas in *r if it isn't equal to one of the
1006 operands so that we can later do bitwise comparisons later on. */
1007 if (r
!= op0
&& r
!= op1
)
1008 memset (r
, '\0', sizeof (*r
));
1009 return do_add (r
, op0
, op1
, 0);
1012 if (r
!= op0
&& r
!= op1
)
1013 memset (r
, '\0', sizeof (*r
));
1014 return do_add (r
, op0
, op1
, 1);
1017 if (r
!= op0
&& r
!= op1
)
1018 memset (r
, '\0', sizeof (*r
));
1019 return do_multiply (r
, op0
, op1
);
1022 if (r
!= op0
&& r
!= op1
)
1023 memset (r
, '\0', sizeof (*r
));
1024 return do_divide (r
, op0
, op1
);
1027 if (op1
->cl
== rvc_nan
)
1029 else if (do_compare (op0
, op1
, -1) < 0)
1036 if (op1
->cl
== rvc_nan
)
1038 else if (do_compare (op0
, op1
, 1) < 0)
1054 case FIX_TRUNC_EXPR
:
1055 do_fix_trunc (r
, op0
);
1065 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1068 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1073 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1076 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1081 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1082 const REAL_VALUE_TYPE
*op1
)
1084 enum tree_code code
= (enum tree_code
) icode
;
1089 return do_compare (op0
, op1
, 1) < 0;
1091 return do_compare (op0
, op1
, 1) <= 0;
1093 return do_compare (op0
, op1
, -1) > 0;
1095 return do_compare (op0
, op1
, -1) >= 0;
1097 return do_compare (op0
, op1
, -1) == 0;
1099 return do_compare (op0
, op1
, -1) != 0;
1100 case UNORDERED_EXPR
:
1101 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1103 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1105 return do_compare (op0
, op1
, -1) < 0;
1107 return do_compare (op0
, op1
, -1) <= 0;
1109 return do_compare (op0
, op1
, 1) > 0;
1111 return do_compare (op0
, op1
, 1) >= 0;
1113 return do_compare (op0
, op1
, 0) == 0;
1115 return do_compare (op0
, op1
, 0) != 0;
1122 /* Return floor log2(R). */
1125 real_exponent (const REAL_VALUE_TYPE
*r
)
1133 return (unsigned int)-1 >> 1;
1135 return REAL_EXP (r
);
1141 /* R = OP0 * 2**EXP. */
1144 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1155 exp
+= REAL_EXP (op0
);
1157 get_inf (r
, r
->sign
);
1158 else if (exp
< -MAX_EXP
)
1159 get_zero (r
, r
->sign
);
1161 SET_REAL_EXP (r
, exp
);
1169 /* Determine whether a floating-point value X is infinite. */
1172 real_isinf (const REAL_VALUE_TYPE
*r
)
1174 return (r
->cl
== rvc_inf
);
1177 /* Determine whether a floating-point value X is a NaN. */
1180 real_isnan (const REAL_VALUE_TYPE
*r
)
1182 return (r
->cl
== rvc_nan
);
1185 /* Determine whether a floating-point value X is finite. */
1188 real_isfinite (const REAL_VALUE_TYPE
*r
)
1190 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1193 /* Determine whether a floating-point value X is negative. */
1196 real_isneg (const REAL_VALUE_TYPE
*r
)
1201 /* Determine whether a floating-point value X is minus zero. */
1204 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1206 return r
->sign
&& r
->cl
== rvc_zero
;
1209 /* Compare two floating-point objects for bitwise identity. */
1212 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1218 if (a
->sign
!= b
->sign
)
1228 if (a
->decimal
!= b
->decimal
)
1230 if (REAL_EXP (a
) != REAL_EXP (b
))
1235 if (a
->signalling
!= b
->signalling
)
1237 /* The significand is ignored for canonical NaNs. */
1238 if (a
->canonical
|| b
->canonical
)
1239 return a
->canonical
== b
->canonical
;
1246 for (i
= 0; i
< SIGSZ
; ++i
)
1247 if (a
->sig
[i
] != b
->sig
[i
])
1253 /* Try to change R into its exact multiplicative inverse in machine
1254 mode MODE. Return true if successful. */
1257 exact_real_inverse (enum machine_mode mode
, REAL_VALUE_TYPE
*r
)
1259 const REAL_VALUE_TYPE
*one
= real_digit (1);
1263 if (r
->cl
!= rvc_normal
)
1266 /* Check for a power of two: all significand bits zero except the MSB. */
1267 for (i
= 0; i
< SIGSZ
-1; ++i
)
1270 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1273 /* Find the inverse and truncate to the required mode. */
1274 do_divide (&u
, one
, r
);
1275 real_convert (&u
, mode
, &u
);
1277 /* The rounding may have overflowed. */
1278 if (u
.cl
!= rvc_normal
)
1280 for (i
= 0; i
< SIGSZ
-1; ++i
)
1283 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1290 /* Return true if arithmetic on values in IMODE that were promoted
1291 from values in TMODE is equivalent to direct arithmetic on values
1295 real_can_shorten_arithmetic (enum machine_mode imode
, enum machine_mode tmode
)
1297 const struct real_format
*tfmt
, *ifmt
;
1298 tfmt
= REAL_MODE_FORMAT (tmode
);
1299 ifmt
= REAL_MODE_FORMAT (imode
);
1300 /* These conditions are conservative rather than trying to catch the
1301 exact boundary conditions; the main case to allow is IEEE float
1303 return (ifmt
->b
== tfmt
->b
1304 && ifmt
->p
> 2 * tfmt
->p
1305 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1306 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1307 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1308 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1309 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1310 && (ifmt
->has_sign_dependent_rounding
1311 == tfmt
->has_sign_dependent_rounding
)
1312 && ifmt
->has_nans
>= tfmt
->has_nans
1313 && ifmt
->has_inf
>= tfmt
->has_inf
1314 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1315 && !MODE_COMPOSITE_P (tmode
)
1316 && !MODE_COMPOSITE_P (imode
));
1319 /* Render R as an integer. */
1322 real_to_integer (const REAL_VALUE_TYPE
*r
)
1324 unsigned HOST_WIDE_INT i
;
1335 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1342 return decimal_real_to_integer (r
);
1344 if (REAL_EXP (r
) <= 0)
1346 /* Only force overflow for unsigned overflow. Signed overflow is
1347 undefined, so it doesn't matter what we return, and some callers
1348 expect to be able to use this routine for both signed and
1349 unsigned conversions. */
1350 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1353 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1354 i
= r
->sig
[SIGSZ
-1];
1357 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1358 i
= r
->sig
[SIGSZ
-1];
1359 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1360 i
|= r
->sig
[SIGSZ
-2];
1363 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1374 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1375 be represented in precision, *FAIL is set to TRUE. */
1378 real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
1380 HOST_WIDE_INT val
[2 * WIDE_INT_MAX_ELTS
];
1389 return wi::zero (precision
);
1397 return wi::set_bit_in_zero (precision
- 1, precision
);
1399 return ~wi::set_bit_in_zero (precision
- 1, precision
);
1403 return decimal_real_to_integer (r
, fail
, precision
);
1408 /* Only force overflow for unsigned overflow. Signed overflow is
1409 undefined, so it doesn't matter what we return, and some callers
1410 expect to be able to use this routine for both signed and
1411 unsigned conversions. */
1412 if (exp
> precision
)
1415 /* Put the significand into a wide_int that has precision W, which
1416 is the smallest HWI-multiple that has at least PRECISION bits.
1417 This ensures that the top bit of the significand is in the
1418 top bit of the wide_int. */
1419 words
= (precision
+ HOST_BITS_PER_WIDE_INT
- 1) / HOST_BITS_PER_WIDE_INT
;
1420 w
= words
* HOST_BITS_PER_WIDE_INT
;
1422 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1423 for (int i
= 0; i
< words
; i
++)
1425 int j
= SIGSZ
- words
+ i
;
1426 val
[i
] = (j
< 0) ? 0 : r
->sig
[j
];
1429 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1430 for (int i
= 0; i
< words
; i
++)
1432 int j
= SIGSZ
- (words
* 2) + (i
* 2);
1439 val
[i
] |= (unsigned HOST_WIDE_INT
) r
->sig
[j
] << HOST_BITS_PER_LONG
;
1442 /* Shift the value into place and truncate to the desired precision. */
1443 result
= wide_int::from_array (val
, words
, w
);
1444 result
= wi::lrshift (result
, w
- exp
);
1445 result
= wide_int::from (result
, precision
, UNSIGNED
);
1457 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1458 of NUM / DEN. Return the quotient and place the remainder in NUM.
1459 It is expected that NUM / DEN are close enough that the quotient is
1462 static unsigned long
1463 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1465 unsigned long q
, msb
;
1466 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1475 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1477 lshift_significand_1 (num
, num
);
1479 if (msb
|| cmp_significands (num
, den
) >= 0)
1481 sub_significands (num
, num
, den
, 0);
1485 while (--expn
>= expd
);
1487 SET_REAL_EXP (num
, expd
);
1493 /* Render R as a decimal floating point constant. Emit DIGITS significant
1494 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1495 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1496 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1497 to a string that, when parsed back in mode MODE, yields the same value. */
1499 #define M_LOG10_2 0.30102999566398119521
1502 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1503 size_t buf_size
, size_t digits
,
1504 int crop_trailing_zeros
, enum machine_mode mode
)
1506 const struct real_format
*fmt
= NULL
;
1507 const REAL_VALUE_TYPE
*one
, *ten
;
1508 REAL_VALUE_TYPE r
, pten
, u
, v
;
1509 int dec_exp
, cmp_one
, digit
;
1511 char *p
, *first
, *last
;
1515 if (mode
!= VOIDmode
)
1517 fmt
= REAL_MODE_FORMAT (mode
);
1525 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1530 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1533 /* ??? Print the significand as well, if not canonical? */
1534 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1535 (r_orig
->signalling
? 'S' : 'Q'));
1543 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1547 /* Bound the number of digits printed by the size of the representation. */
1548 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1549 if (digits
== 0 || digits
> max_digits
)
1550 digits
= max_digits
;
1552 /* Estimate the decimal exponent, and compute the length of the string it
1553 will print as. Be conservative and add one to account for possible
1554 overflow or rounding error. */
1555 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1556 for (max_digits
= 1; dec_exp
; max_digits
++)
1559 /* Bound the number of digits printed by the size of the output buffer. */
1560 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1561 gcc_assert (max_digits
<= buf_size
);
1562 if (digits
> max_digits
)
1563 digits
= max_digits
;
1565 one
= real_digit (1);
1566 ten
= ten_to_ptwo (0);
1574 cmp_one
= do_compare (&r
, one
, 0);
1579 /* Number is greater than one. Convert significand to an integer
1580 and strip trailing decimal zeros. */
1583 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1585 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1586 m
= floor_log2 (max_digits
);
1588 /* Iterate over the bits of the possible powers of 10 that might
1589 be present in U and eliminate them. That is, if we find that
1590 10**2**M divides U evenly, keep the division and increase
1596 do_divide (&t
, &u
, ten_to_ptwo (m
));
1597 do_fix_trunc (&v
, &t
);
1598 if (cmp_significands (&v
, &t
) == 0)
1606 /* Revert the scaling to integer that we performed earlier. */
1607 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1608 - (SIGNIFICAND_BITS
- 1));
1611 /* Find power of 10. Do this by dividing out 10**2**M when
1612 this is larger than the current remainder. Fill PTEN with
1613 the power of 10 that we compute. */
1614 if (REAL_EXP (&r
) > 0)
1616 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1619 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1620 if (do_compare (&u
, ptentwo
, 0) >= 0)
1622 do_divide (&u
, &u
, ptentwo
);
1623 do_multiply (&pten
, &pten
, ptentwo
);
1630 /* We managed to divide off enough tens in the above reduction
1631 loop that we've now got a negative exponent. Fall into the
1632 less-than-one code to compute the proper value for PTEN. */
1639 /* Number is less than one. Pad significand with leading
1645 /* Stop if we'd shift bits off the bottom. */
1649 do_multiply (&u
, &v
, ten
);
1651 /* Stop if we're now >= 1. */
1652 if (REAL_EXP (&u
) > 0)
1660 /* Find power of 10. Do this by multiplying in P=10**2**M when
1661 the current remainder is smaller than 1/P. Fill PTEN with the
1662 power of 10 that we compute. */
1663 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1666 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1667 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1669 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1671 do_multiply (&v
, &v
, ptentwo
);
1672 do_multiply (&pten
, &pten
, ptentwo
);
1678 /* Invert the positive power of 10 that we've collected so far. */
1679 do_divide (&pten
, one
, &pten
);
1687 /* At this point, PTEN should contain the nearest power of 10 smaller
1688 than R, such that this division produces the first digit.
1690 Using a divide-step primitive that returns the complete integral
1691 remainder avoids the rounding error that would be produced if
1692 we were to use do_divide here and then simply multiply by 10 for
1693 each subsequent digit. */
1695 digit
= rtd_divmod (&r
, &pten
);
1697 /* Be prepared for error in that division via underflow ... */
1698 if (digit
== 0 && cmp_significand_0 (&r
))
1700 /* Multiply by 10 and try again. */
1701 do_multiply (&r
, &r
, ten
);
1702 digit
= rtd_divmod (&r
, &pten
);
1704 gcc_assert (digit
!= 0);
1707 /* ... or overflow. */
1717 gcc_assert (digit
<= 10);
1721 /* Generate subsequent digits. */
1722 while (--digits
> 0)
1724 do_multiply (&r
, &r
, ten
);
1725 digit
= rtd_divmod (&r
, &pten
);
1730 /* Generate one more digit with which to do rounding. */
1731 do_multiply (&r
, &r
, ten
);
1732 digit
= rtd_divmod (&r
, &pten
);
1734 /* Round the result. */
1735 if (fmt
&& fmt
->round_towards_zero
)
1737 /* If the format uses round towards zero when parsing the string
1738 back in, we need to always round away from zero here. */
1739 if (cmp_significand_0 (&r
))
1741 round_up
= digit
> 0;
1747 /* Round to nearest. If R is nonzero there are additional
1748 nonzero digits to be extracted. */
1749 if (cmp_significand_0 (&r
))
1751 /* Round to even. */
1752 else if ((p
[-1] - '0') & 1)
1756 round_up
= digit
> 5;
1773 /* Carry out of the first digit. This means we had all 9's and
1774 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1782 /* Insert the decimal point. */
1783 first
[0] = first
[1];
1786 /* If requested, drop trailing zeros. Never crop past "1.0". */
1787 if (crop_trailing_zeros
)
1788 while (last
> first
+ 3 && last
[-1] == '0')
1791 /* Append the exponent. */
1792 sprintf (last
, "e%+d", dec_exp
);
1794 #ifdef ENABLE_CHECKING
1795 /* Verify that we can read the original value back in. */
1796 if (mode
!= VOIDmode
)
1798 real_from_string (&r
, str
);
1799 real_convert (&r
, mode
, &r
);
1800 gcc_assert (real_identical (&r
, r_orig
));
1805 /* Likewise, except always uses round-to-nearest. */
1808 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1809 size_t digits
, int crop_trailing_zeros
)
1811 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1812 digits
, crop_trailing_zeros
, VOIDmode
);
1815 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1816 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1817 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1818 strip trailing zeros. */
1821 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1822 size_t digits
, int crop_trailing_zeros
)
1824 int i
, j
, exp
= REAL_EXP (r
);
1837 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1840 /* ??? Print the significand as well, if not canonical? */
1841 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1842 (r
->signalling
? 'S' : 'Q'));
1850 /* Hexadecimal format for decimal floats is not interesting. */
1851 strcpy (str
, "N/A");
1856 digits
= SIGNIFICAND_BITS
/ 4;
1858 /* Bound the number of digits printed by the size of the output buffer. */
1860 sprintf (exp_buf
, "p%+d", exp
);
1861 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1862 gcc_assert (max_digits
<= buf_size
);
1863 if (digits
> max_digits
)
1864 digits
= max_digits
;
1875 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1876 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1878 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1884 if (crop_trailing_zeros
)
1885 while (p
> first
+ 1 && p
[-1] == '0')
1888 sprintf (p
, "p%+d", exp
);
1891 /* Initialize R from a decimal or hexadecimal string. The string is
1892 assumed to have been syntax checked already. Return -1 if the
1893 value underflows, +1 if overflows, and 0 otherwise. */
1896 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1908 else if (*str
== '+')
1911 if (!strncmp (str
, "QNaN", 4))
1913 get_canonical_qnan (r
, sign
);
1916 else if (!strncmp (str
, "SNaN", 4))
1918 get_canonical_snan (r
, sign
);
1921 else if (!strncmp (str
, "Inf", 3))
1927 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1929 /* Hexadecimal floating point. */
1930 int pos
= SIGNIFICAND_BITS
- 4, d
;
1938 d
= hex_value (*str
);
1943 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1944 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1948 /* Ensure correct rounding by setting last bit if there is
1949 a subsequent nonzero digit. */
1957 if (pos
== SIGNIFICAND_BITS
- 4)
1964 d
= hex_value (*str
);
1969 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1970 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1974 /* Ensure correct rounding by setting last bit if there is
1975 a subsequent nonzero digit. */
1981 /* If the mantissa is zero, ignore the exponent. */
1982 if (!cmp_significand_0 (r
))
1985 if (*str
== 'p' || *str
== 'P')
1987 bool exp_neg
= false;
1995 else if (*str
== '+')
1999 while (ISDIGIT (*str
))
2005 /* Overflowed the exponent. */
2020 SET_REAL_EXP (r
, exp
);
2026 /* Decimal floating point. */
2027 const char *cstr
= str
;
2031 while (*cstr
== '0')
2036 while (*cstr
== '0')
2040 /* If the mantissa is zero, ignore the exponent. */
2041 if (!ISDIGIT (*cstr
))
2044 /* Nonzero value, possibly overflowing or underflowing. */
2045 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2046 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, GMP_RNDZ
);
2047 /* The result should never be a NaN, and because the rounding is
2048 toward zero should never be an infinity. */
2049 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2050 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2055 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2062 real_from_mpfr (r
, m
, NULL_TREE
, GMP_RNDZ
);
2063 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2064 because the hex digits used in real_from_mpfr did not
2065 start with a digit 8 to f, but the exponent bounds above
2066 should have avoided underflow or overflow. */
2067 gcc_assert (r
->cl
= rvc_normal
);
2068 /* Set a sticky bit if mpfr_strtofr was inexact. */
2069 r
->sig
[0] |= inexact
;
2089 /* Legacy. Similar, but return the result directly. */
2092 real_from_string2 (const char *s
, enum machine_mode mode
)
2096 real_from_string (&r
, s
);
2097 if (mode
!= VOIDmode
)
2098 real_convert (&r
, mode
, &r
);
2103 /* Initialize R from string S and desired MODE. */
2106 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, enum machine_mode mode
)
2108 if (DECIMAL_FLOAT_MODE_P (mode
))
2109 decimal_real_from_string (r
, s
);
2111 real_from_string (r
, s
);
2113 if (mode
!= VOIDmode
)
2114 real_convert (r
, mode
, r
);
2117 /* Initialize R from the wide_int VAL_IN. The MODE is not VOIDmode,*/
2120 real_from_integer (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2121 const wide_int_ref
&val_in
, signop sgn
)
2127 unsigned int len
= val_in
.get_precision ();
2129 int maxbitlen
= MAX_BITSIZE_MODE_ANY_INT
+ HOST_BITS_PER_WIDE_INT
;
2130 const unsigned int realmax
= (SIGNIFICAND_BITS
/ HOST_BITS_PER_WIDE_INT
2131 * HOST_BITS_PER_WIDE_INT
);
2133 memset (r
, 0, sizeof (*r
));
2135 r
->sign
= wi::neg_p (val_in
, sgn
);
2137 /* We have to ensure we can negate the largest negative number. */
2138 wide_int val
= wide_int::from (val_in
, maxbitlen
, sgn
);
2143 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2144 won't work with precisions that are not a multiple of
2145 HOST_BITS_PER_WIDE_INT. */
2146 len
+= HOST_BITS_PER_WIDE_INT
- 1;
2148 /* Ensure we can represent the largest negative number. */
2151 len
= len
/HOST_BITS_PER_WIDE_INT
* HOST_BITS_PER_WIDE_INT
;
2153 /* Cap the size to the size allowed by real.h. */
2156 HOST_WIDE_INT cnt_l_z
;
2157 cnt_l_z
= wi::clz (val
);
2159 if (maxbitlen
- cnt_l_z
> realmax
)
2161 e
= maxbitlen
- cnt_l_z
- realmax
;
2163 /* This value is too large, we must shift it right to
2164 preserve all the bits we can, and then bump the
2165 exponent up by that amount. */
2166 val
= wi::lrshift (val
, e
);
2171 /* Clear out top bits so elt will work with precisions that aren't
2172 a multiple of HOST_BITS_PER_WIDE_INT. */
2173 val
= wide_int::from (val
, len
, sgn
);
2174 len
= len
/ HOST_BITS_PER_WIDE_INT
;
2176 SET_REAL_EXP (r
, len
* HOST_BITS_PER_WIDE_INT
+ e
);
2179 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2180 for (i
= len
- 1; i
>= 0; i
--)
2182 r
->sig
[j
--] = val
.elt (i
);
2188 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2189 for (i
= len
- 1; i
>= 0; i
--)
2191 HOST_WIDE_INT e
= val
.elt (i
);
2192 r
->sig
[j
--] = e
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2204 if (DECIMAL_FLOAT_MODE_P (mode
))
2205 decimal_from_integer (r
);
2206 else if (mode
!= VOIDmode
)
2207 real_convert (r
, mode
, r
);
2210 /* Render R, an integral value, as a floating point constant with no
2211 specified exponent. */
2214 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2217 int dec_exp
, digit
, digits
;
2218 REAL_VALUE_TYPE r
, pten
;
2224 if (r
.cl
== rvc_zero
)
2233 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2234 digits
= dec_exp
+ 1;
2235 gcc_assert ((digits
+ 2) < (int)buf_size
);
2237 pten
= *real_digit (1);
2238 times_pten (&pten
, dec_exp
);
2244 digit
= rtd_divmod (&r
, &pten
);
2245 gcc_assert (digit
>= 0 && digit
<= 9);
2247 while (--digits
> 0)
2250 digit
= rtd_divmod (&r
, &pten
);
2257 /* Convert a real with an integral value to decimal float. */
2260 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2264 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2265 decimal_real_from_string (r
, str
);
2268 /* Returns 10**2**N. */
2270 static const REAL_VALUE_TYPE
*
2273 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2275 gcc_assert (n
>= 0);
2276 gcc_assert (n
< EXP_BITS
);
2278 if (tens
[n
].cl
== rvc_zero
)
2280 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2282 HOST_WIDE_INT t
= 10;
2285 for (i
= 0; i
< n
; ++i
)
2288 real_from_integer (&tens
[n
], VOIDmode
, t
, UNSIGNED
);
2292 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2293 do_multiply (&tens
[n
], t
, t
);
2300 /* Returns 10**(-2**N). */
2302 static const REAL_VALUE_TYPE
*
2303 ten_to_mptwo (int n
)
2305 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2307 gcc_assert (n
>= 0);
2308 gcc_assert (n
< EXP_BITS
);
2310 if (tens
[n
].cl
== rvc_zero
)
2311 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2318 static const REAL_VALUE_TYPE
*
2321 static REAL_VALUE_TYPE num
[10];
2323 gcc_assert (n
>= 0);
2324 gcc_assert (n
<= 9);
2326 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2327 real_from_integer (&num
[n
], VOIDmode
, n
, UNSIGNED
);
2332 /* Multiply R by 10**EXP. */
2335 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2337 REAL_VALUE_TYPE pten
, *rr
;
2338 bool negative
= (exp
< 0);
2344 pten
= *real_digit (1);
2350 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2352 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2355 do_divide (r
, r
, &pten
);
2358 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2360 const REAL_VALUE_TYPE
*
2363 static REAL_VALUE_TYPE value
;
2365 /* Initialize mathematical constants for constant folding builtins.
2366 These constants need to be given to at least 160 bits precision. */
2367 if (value
.cl
== rvc_zero
)
2370 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2371 mpfr_set_ui (m
, 1, GMP_RNDN
);
2372 mpfr_exp (m
, m
, GMP_RNDN
);
2373 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2380 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2382 const REAL_VALUE_TYPE
*
2383 dconst_third_ptr (void)
2385 static REAL_VALUE_TYPE value
;
2387 /* Initialize mathematical constants for constant folding builtins.
2388 These constants need to be given to at least 160 bits precision. */
2389 if (value
.cl
== rvc_zero
)
2391 real_arithmetic (&value
, RDIV_EXPR
, &dconst1
, real_digit (3));
2396 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2398 const REAL_VALUE_TYPE
*
2399 dconst_sqrt2_ptr (void)
2401 static REAL_VALUE_TYPE value
;
2403 /* Initialize mathematical constants for constant folding builtins.
2404 These constants need to be given to at least 160 bits precision. */
2405 if (value
.cl
== rvc_zero
)
2408 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2409 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2410 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2416 /* Fills R with +Inf. */
2419 real_inf (REAL_VALUE_TYPE
*r
)
2424 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2425 we force a QNaN, else we force an SNaN. The string, if not empty,
2426 is parsed as a number and placed in the significand. Return true
2427 if the string was successfully parsed. */
2430 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2431 enum machine_mode mode
)
2433 const struct real_format
*fmt
;
2435 fmt
= REAL_MODE_FORMAT (mode
);
2441 get_canonical_qnan (r
, 0);
2443 get_canonical_snan (r
, 0);
2449 memset (r
, 0, sizeof (*r
));
2452 /* Parse akin to strtol into the significand of R. */
2454 while (ISSPACE (*str
))
2458 else if (*str
== '+')
2463 if (*str
== 'x' || *str
== 'X')
2472 while ((d
= hex_value (*str
)) < base
)
2479 lshift_significand (r
, r
, 3);
2482 lshift_significand (r
, r
, 4);
2485 lshift_significand_1 (&u
, r
);
2486 lshift_significand (r
, r
, 3);
2487 add_significands (r
, r
, &u
);
2495 add_significands (r
, r
, &u
);
2500 /* Must have consumed the entire string for success. */
2504 /* Shift the significand into place such that the bits
2505 are in the most significant bits for the format. */
2506 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2508 /* Our MSB is always unset for NaNs. */
2509 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2511 /* Force quiet or signalling NaN. */
2512 r
->signalling
= !quiet
;
2518 /* Fills R with the largest finite value representable in mode MODE.
2519 If SIGN is nonzero, R is set to the most negative finite value. */
2522 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
2524 const struct real_format
*fmt
;
2527 fmt
= REAL_MODE_FORMAT (mode
);
2529 memset (r
, 0, sizeof (*r
));
2532 decimal_real_maxval (r
, sign
, mode
);
2537 SET_REAL_EXP (r
, fmt
->emax
);
2539 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2540 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2541 clear_significand_below (r
, np2
);
2543 if (fmt
->pnan
< fmt
->p
)
2544 /* This is an IBM extended double format made up of two IEEE
2545 doubles. The value of the long double is the sum of the
2546 values of the two parts. The most significant part is
2547 required to be the value of the long double rounded to the
2548 nearest double. Rounding means we need a slightly smaller
2549 value for LDBL_MAX. */
2550 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2554 /* Fills R with 2**N. */
2557 real_2expN (REAL_VALUE_TYPE
*r
, int n
, enum machine_mode fmode
)
2559 memset (r
, 0, sizeof (*r
));
2564 else if (n
< -MAX_EXP
)
2569 SET_REAL_EXP (r
, n
);
2570 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2572 if (DECIMAL_FLOAT_MODE_P (fmode
))
2573 decimal_real_convert (r
, fmode
, r
);
2578 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2582 bool round_up
= false;
2588 decimal_round_for_format (fmt
, r
);
2591 /* FIXME. We can come here via fp_easy_constant
2592 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2593 investigated whether this convert needs to be here, or
2594 something else is missing. */
2595 decimal_real_convert (r
, DFmode
, r
);
2599 emin2m1
= fmt
->emin
- 1;
2602 np2
= SIGNIFICAND_BITS
- p2
;
2606 get_zero (r
, r
->sign
);
2608 if (!fmt
->has_signed_zero
)
2613 get_inf (r
, r
->sign
);
2618 clear_significand_below (r
, np2
);
2628 /* Check the range of the exponent. If we're out of range,
2629 either underflow or overflow. */
2630 if (REAL_EXP (r
) > emax2
)
2632 else if (REAL_EXP (r
) <= emin2m1
)
2636 if (!fmt
->has_denorm
)
2638 /* Don't underflow completely until we've had a chance to round. */
2639 if (REAL_EXP (r
) < emin2m1
)
2644 diff
= emin2m1
- REAL_EXP (r
) + 1;
2648 /* De-normalize the significand. */
2649 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2650 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2654 if (!fmt
->round_towards_zero
)
2656 /* There are P2 true significand bits, followed by one guard bit,
2657 followed by one sticky bit, followed by stuff. Fold nonzero
2658 stuff into the sticky bit. */
2659 unsigned long sticky
;
2663 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2664 sticky
|= r
->sig
[i
];
2666 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2668 guard
= test_significand_bit (r
, np2
- 1);
2669 lsb
= test_significand_bit (r
, np2
);
2671 /* Round to even. */
2672 round_up
= guard
&& (sticky
|| lsb
);
2679 set_significand_bit (&u
, np2
);
2681 if (add_significands (r
, r
, &u
))
2683 /* Overflow. Means the significand had been all ones, and
2684 is now all zeros. Need to increase the exponent, and
2685 possibly re-normalize it. */
2686 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2687 if (REAL_EXP (r
) > emax2
)
2689 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2693 /* Catch underflow that we deferred until after rounding. */
2694 if (REAL_EXP (r
) <= emin2m1
)
2697 /* Clear out trailing garbage. */
2698 clear_significand_below (r
, np2
);
2701 /* Extend or truncate to a new mode. */
2704 real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2705 const REAL_VALUE_TYPE
*a
)
2707 const struct real_format
*fmt
;
2709 fmt
= REAL_MODE_FORMAT (mode
);
2714 if (a
->decimal
|| fmt
->b
== 10)
2715 decimal_real_convert (r
, mode
, a
);
2717 round_for_format (fmt
, r
);
2719 /* round_for_format de-normalizes denormals. Undo just that part. */
2720 if (r
->cl
== rvc_normal
)
2724 /* Legacy. Likewise, except return the struct directly. */
2727 real_value_truncate (enum machine_mode mode
, REAL_VALUE_TYPE a
)
2730 real_convert (&r
, mode
, &a
);
2734 /* Return true if truncating to MODE is exact. */
2737 exact_real_truncate (enum machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2739 const struct real_format
*fmt
;
2743 fmt
= REAL_MODE_FORMAT (mode
);
2746 /* Don't allow conversion to denormals. */
2747 emin2m1
= fmt
->emin
- 1;
2748 if (REAL_EXP (a
) <= emin2m1
)
2751 /* After conversion to the new mode, the value must be identical. */
2752 real_convert (&t
, mode
, a
);
2753 return real_identical (&t
, a
);
2756 /* Write R to the given target format. Place the words of the result
2757 in target word order in BUF. There are always 32 bits in each
2758 long, no matter the size of the host long.
2760 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2763 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2764 const struct real_format
*fmt
)
2770 round_for_format (fmt
, &r
);
2774 (*fmt
->encode
) (fmt
, buf
, &r
);
2779 /* Similar, but look up the format from MODE. */
2782 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, enum machine_mode mode
)
2784 const struct real_format
*fmt
;
2786 fmt
= REAL_MODE_FORMAT (mode
);
2789 return real_to_target_fmt (buf
, r
, fmt
);
2792 /* Read R from the given target format. Read the words of the result
2793 in target word order in BUF. There are always 32 bits in each
2794 long, no matter the size of the host long. */
2797 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2798 const struct real_format
*fmt
)
2800 (*fmt
->decode
) (fmt
, r
, buf
);
2803 /* Similar, but look up the format from MODE. */
2806 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, enum machine_mode mode
)
2808 const struct real_format
*fmt
;
2810 fmt
= REAL_MODE_FORMAT (mode
);
2813 (*fmt
->decode
) (fmt
, r
, buf
);
2816 /* Return the number of bits of the largest binary value that the
2817 significand of MODE will hold. */
2818 /* ??? Legacy. Should get access to real_format directly. */
2821 significand_size (enum machine_mode mode
)
2823 const struct real_format
*fmt
;
2825 fmt
= REAL_MODE_FORMAT (mode
);
2831 /* Return the size in bits of the largest binary value that can be
2832 held by the decimal coefficient for this mode. This is one more
2833 than the number of bits required to hold the largest coefficient
2835 double log2_10
= 3.3219281;
2836 return fmt
->p
* log2_10
;
2841 /* Return a hash value for the given real value. */
2842 /* ??? The "unsigned int" return value is intended to be hashval_t,
2843 but I didn't want to pull hashtab.h into real.h. */
2846 real_hash (const REAL_VALUE_TYPE
*r
)
2851 h
= r
->cl
| (r
->sign
<< 2);
2859 h
|= REAL_EXP (r
) << 3;
2864 h
^= (unsigned int)-1;
2873 if (sizeof (unsigned long) > sizeof (unsigned int))
2874 for (i
= 0; i
< SIGSZ
; ++i
)
2876 unsigned long s
= r
->sig
[i
];
2877 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2880 for (i
= 0; i
< SIGSZ
; ++i
)
2886 /* IEEE single-precision format. */
2888 static void encode_ieee_single (const struct real_format
*fmt
,
2889 long *, const REAL_VALUE_TYPE
*);
2890 static void decode_ieee_single (const struct real_format
*,
2891 REAL_VALUE_TYPE
*, const long *);
2894 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2895 const REAL_VALUE_TYPE
*r
)
2897 unsigned long image
, sig
, exp
;
2898 unsigned long sign
= r
->sign
;
2899 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2902 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2913 image
|= 0x7fffffff;
2920 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2921 if (r
->signalling
== fmt
->qnan_msb_set
)
2932 image
|= 0x7fffffff;
2936 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2937 whereas the intermediate representation is 0.F x 2**exp.
2938 Which means we're off by one. */
2942 exp
= REAL_EXP (r
) + 127 - 1;
2955 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2958 unsigned long image
= buf
[0] & 0xffffffff;
2959 bool sign
= (image
>> 31) & 1;
2960 int exp
= (image
>> 23) & 0xff;
2962 memset (r
, 0, sizeof (*r
));
2963 image
<<= HOST_BITS_PER_LONG
- 24;
2968 if (image
&& fmt
->has_denorm
)
2972 SET_REAL_EXP (r
, -126);
2973 r
->sig
[SIGSZ
-1] = image
<< 1;
2976 else if (fmt
->has_signed_zero
)
2979 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2985 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2986 ^ fmt
->qnan_msb_set
);
2987 r
->sig
[SIGSZ
-1] = image
;
2999 SET_REAL_EXP (r
, exp
- 127 + 1);
3000 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
3004 const struct real_format ieee_single_format
=
3025 const struct real_format mips_single_format
=
3046 const struct real_format motorola_single_format
=
3067 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3068 single precision with the following differences:
3069 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3071 - NaNs are not supported.
3072 - The range of non-zero numbers in binary is
3073 (001)[1.]000...000 to (255)[1.]111...111.
3074 - Denormals can be represented, but are treated as +0.0 when
3075 used as an operand and are never generated as a result.
3076 - -0.0 can be represented, but a zero result is always +0.0.
3077 - the only supported rounding mode is trunction (towards zero). */
3078 const struct real_format spu_single_format
=
3099 /* IEEE double-precision format. */
3101 static void encode_ieee_double (const struct real_format
*fmt
,
3102 long *, const REAL_VALUE_TYPE
*);
3103 static void decode_ieee_double (const struct real_format
*,
3104 REAL_VALUE_TYPE
*, const long *);
3107 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3108 const REAL_VALUE_TYPE
*r
)
3110 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3111 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3113 image_hi
= r
->sign
<< 31;
3116 if (HOST_BITS_PER_LONG
== 64)
3118 sig_hi
= r
->sig
[SIGSZ
-1];
3119 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3120 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3124 sig_hi
= r
->sig
[SIGSZ
-1];
3125 sig_lo
= r
->sig
[SIGSZ
-2];
3126 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3127 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3137 image_hi
|= 2047 << 20;
3140 image_hi
|= 0x7fffffff;
3141 image_lo
= 0xffffffff;
3150 if (fmt
->canonical_nan_lsbs_set
)
3152 sig_hi
= (1 << 19) - 1;
3153 sig_lo
= 0xffffffff;
3161 if (r
->signalling
== fmt
->qnan_msb_set
)
3162 sig_hi
&= ~(1 << 19);
3165 if (sig_hi
== 0 && sig_lo
== 0)
3168 image_hi
|= 2047 << 20;
3174 image_hi
|= 0x7fffffff;
3175 image_lo
= 0xffffffff;
3180 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3181 whereas the intermediate representation is 0.F x 2**exp.
3182 Which means we're off by one. */
3186 exp
= REAL_EXP (r
) + 1023 - 1;
3187 image_hi
|= exp
<< 20;
3196 if (FLOAT_WORDS_BIG_ENDIAN
)
3197 buf
[0] = image_hi
, buf
[1] = image_lo
;
3199 buf
[0] = image_lo
, buf
[1] = image_hi
;
3203 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3206 unsigned long image_hi
, image_lo
;
3210 if (FLOAT_WORDS_BIG_ENDIAN
)
3211 image_hi
= buf
[0], image_lo
= buf
[1];
3213 image_lo
= buf
[0], image_hi
= buf
[1];
3214 image_lo
&= 0xffffffff;
3215 image_hi
&= 0xffffffff;
3217 sign
= (image_hi
>> 31) & 1;
3218 exp
= (image_hi
>> 20) & 0x7ff;
3220 memset (r
, 0, sizeof (*r
));
3222 image_hi
<<= 32 - 21;
3223 image_hi
|= image_lo
>> 21;
3224 image_hi
&= 0x7fffffff;
3225 image_lo
<<= 32 - 21;
3229 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3233 SET_REAL_EXP (r
, -1022);
3234 if (HOST_BITS_PER_LONG
== 32)
3236 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3238 r
->sig
[SIGSZ
-1] = image_hi
;
3239 r
->sig
[SIGSZ
-2] = image_lo
;
3243 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3244 r
->sig
[SIGSZ
-1] = image_hi
;
3248 else if (fmt
->has_signed_zero
)
3251 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3253 if (image_hi
|| image_lo
)
3257 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3258 if (HOST_BITS_PER_LONG
== 32)
3260 r
->sig
[SIGSZ
-1] = image_hi
;
3261 r
->sig
[SIGSZ
-2] = image_lo
;
3264 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3276 SET_REAL_EXP (r
, exp
- 1023 + 1);
3277 if (HOST_BITS_PER_LONG
== 32)
3279 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3280 r
->sig
[SIGSZ
-2] = image_lo
;
3283 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3287 const struct real_format ieee_double_format
=
3308 const struct real_format mips_double_format
=
3329 const struct real_format motorola_double_format
=
3350 /* IEEE extended real format. This comes in three flavors: Intel's as
3351 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3352 12- and 16-byte images may be big- or little endian; Motorola's is
3353 always big endian. */
3355 /* Helper subroutine which converts from the internal format to the
3356 12-byte little-endian Intel format. Functions below adjust this
3357 for the other possible formats. */
3359 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3360 const REAL_VALUE_TYPE
*r
)
3362 unsigned long image_hi
, sig_hi
, sig_lo
;
3363 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3365 image_hi
= r
->sign
<< 15;
3366 sig_hi
= sig_lo
= 0;
3378 /* Intel requires the explicit integer bit to be set, otherwise
3379 it considers the value a "pseudo-infinity". Motorola docs
3380 say it doesn't care. */
3381 sig_hi
= 0x80000000;
3386 sig_lo
= sig_hi
= 0xffffffff;
3396 if (fmt
->canonical_nan_lsbs_set
)
3398 sig_hi
= (1 << 30) - 1;
3399 sig_lo
= 0xffffffff;
3402 else if (HOST_BITS_PER_LONG
== 32)
3404 sig_hi
= r
->sig
[SIGSZ
-1];
3405 sig_lo
= r
->sig
[SIGSZ
-2];
3409 sig_lo
= r
->sig
[SIGSZ
-1];
3410 sig_hi
= sig_lo
>> 31 >> 1;
3411 sig_lo
&= 0xffffffff;
3413 if (r
->signalling
== fmt
->qnan_msb_set
)
3414 sig_hi
&= ~(1 << 30);
3417 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3420 /* Intel requires the explicit integer bit to be set, otherwise
3421 it considers the value a "pseudo-nan". Motorola docs say it
3423 sig_hi
|= 0x80000000;
3428 sig_lo
= sig_hi
= 0xffffffff;
3434 int exp
= REAL_EXP (r
);
3436 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3437 whereas the intermediate representation is 0.F x 2**exp.
3438 Which means we're off by one.
3440 Except for Motorola, which consider exp=0 and explicit
3441 integer bit set to continue to be normalized. In theory
3442 this discrepancy has been taken care of by the difference
3443 in fmt->emin in round_for_format. */
3450 gcc_assert (exp
>= 0);
3454 if (HOST_BITS_PER_LONG
== 32)
3456 sig_hi
= r
->sig
[SIGSZ
-1];
3457 sig_lo
= r
->sig
[SIGSZ
-2];
3461 sig_lo
= r
->sig
[SIGSZ
-1];
3462 sig_hi
= sig_lo
>> 31 >> 1;
3463 sig_lo
&= 0xffffffff;
3472 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3475 /* Convert from the internal format to the 12-byte Motorola format
3476 for an IEEE extended real. */
3478 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3479 const REAL_VALUE_TYPE
*r
)
3482 encode_ieee_extended (fmt
, intermed
, r
);
3484 /* Motorola chips are assumed always to be big-endian. Also, the
3485 padding in a Motorola extended real goes between the exponent and
3486 the mantissa. At this point the mantissa is entirely within
3487 elements 0 and 1 of intermed, and the exponent entirely within
3488 element 2, so all we have to do is swap the order around, and
3489 shift element 2 left 16 bits. */
3490 buf
[0] = intermed
[2] << 16;
3491 buf
[1] = intermed
[1];
3492 buf
[2] = intermed
[0];
3495 /* Convert from the internal format to the 12-byte Intel format for
3496 an IEEE extended real. */
3498 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3499 const REAL_VALUE_TYPE
*r
)
3501 if (FLOAT_WORDS_BIG_ENDIAN
)
3503 /* All the padding in an Intel-format extended real goes at the high
3504 end, which in this case is after the mantissa, not the exponent.
3505 Therefore we must shift everything down 16 bits. */
3507 encode_ieee_extended (fmt
, intermed
, r
);
3508 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3509 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3510 buf
[2] = (intermed
[0] << 16);
3513 /* encode_ieee_extended produces what we want directly. */
3514 encode_ieee_extended (fmt
, buf
, r
);
3517 /* Convert from the internal format to the 16-byte Intel format for
3518 an IEEE extended real. */
3520 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3521 const REAL_VALUE_TYPE
*r
)
3523 /* All the padding in an Intel-format extended real goes at the high end. */
3524 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3528 /* As above, we have a helper function which converts from 12-byte
3529 little-endian Intel format to internal format. Functions below
3530 adjust for the other possible formats. */
3532 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3535 unsigned long image_hi
, sig_hi
, sig_lo
;
3539 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3540 sig_lo
&= 0xffffffff;
3541 sig_hi
&= 0xffffffff;
3542 image_hi
&= 0xffffffff;
3544 sign
= (image_hi
>> 15) & 1;
3545 exp
= image_hi
& 0x7fff;
3547 memset (r
, 0, sizeof (*r
));
3551 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3556 /* When the IEEE format contains a hidden bit, we know that
3557 it's zero at this point, and so shift up the significand
3558 and decrease the exponent to match. In this case, Motorola
3559 defines the explicit integer bit to be valid, so we don't
3560 know whether the msb is set or not. */
3561 SET_REAL_EXP (r
, fmt
->emin
);
3562 if (HOST_BITS_PER_LONG
== 32)
3564 r
->sig
[SIGSZ
-1] = sig_hi
;
3565 r
->sig
[SIGSZ
-2] = sig_lo
;
3568 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3572 else if (fmt
->has_signed_zero
)
3575 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3577 /* See above re "pseudo-infinities" and "pseudo-nans".
3578 Short summary is that the MSB will likely always be
3579 set, and that we don't care about it. */
3580 sig_hi
&= 0x7fffffff;
3582 if (sig_hi
|| sig_lo
)
3586 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3587 if (HOST_BITS_PER_LONG
== 32)
3589 r
->sig
[SIGSZ
-1] = sig_hi
;
3590 r
->sig
[SIGSZ
-2] = sig_lo
;
3593 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3605 SET_REAL_EXP (r
, exp
- 16383 + 1);
3606 if (HOST_BITS_PER_LONG
== 32)
3608 r
->sig
[SIGSZ
-1] = sig_hi
;
3609 r
->sig
[SIGSZ
-2] = sig_lo
;
3612 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3616 /* Convert from the internal format to the 12-byte Motorola format
3617 for an IEEE extended real. */
3619 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3624 /* Motorola chips are assumed always to be big-endian. Also, the
3625 padding in a Motorola extended real goes between the exponent and
3626 the mantissa; remove it. */
3627 intermed
[0] = buf
[2];
3628 intermed
[1] = buf
[1];
3629 intermed
[2] = (unsigned long)buf
[0] >> 16;
3631 decode_ieee_extended (fmt
, r
, intermed
);
3634 /* Convert from the internal format to the 12-byte Intel format for
3635 an IEEE extended real. */
3637 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3640 if (FLOAT_WORDS_BIG_ENDIAN
)
3642 /* All the padding in an Intel-format extended real goes at the high
3643 end, which in this case is after the mantissa, not the exponent.
3644 Therefore we must shift everything up 16 bits. */
3647 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3648 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3649 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3651 decode_ieee_extended (fmt
, r
, intermed
);
3654 /* decode_ieee_extended produces what we want directly. */
3655 decode_ieee_extended (fmt
, r
, buf
);
3658 /* Convert from the internal format to the 16-byte Intel format for
3659 an IEEE extended real. */
3661 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3664 /* All the padding in an Intel-format extended real goes at the high end. */
3665 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3668 const struct real_format ieee_extended_motorola_format
=
3670 encode_ieee_extended_motorola
,
3671 decode_ieee_extended_motorola
,
3689 const struct real_format ieee_extended_intel_96_format
=
3691 encode_ieee_extended_intel_96
,
3692 decode_ieee_extended_intel_96
,
3710 const struct real_format ieee_extended_intel_128_format
=
3712 encode_ieee_extended_intel_128
,
3713 decode_ieee_extended_intel_128
,
3731 /* The following caters to i386 systems that set the rounding precision
3732 to 53 bits instead of 64, e.g. FreeBSD. */
3733 const struct real_format ieee_extended_intel_96_round_53_format
=
3735 encode_ieee_extended_intel_96
,
3736 decode_ieee_extended_intel_96
,
3754 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3755 numbers whose sum is equal to the extended precision value. The number
3756 with greater magnitude is first. This format has the same magnitude
3757 range as an IEEE double precision value, but effectively 106 bits of
3758 significand precision. Infinity and NaN are represented by their IEEE
3759 double precision value stored in the first number, the second number is
3760 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3762 static void encode_ibm_extended (const struct real_format
*fmt
,
3763 long *, const REAL_VALUE_TYPE
*);
3764 static void decode_ibm_extended (const struct real_format
*,
3765 REAL_VALUE_TYPE
*, const long *);
3768 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3769 const REAL_VALUE_TYPE
*r
)
3771 REAL_VALUE_TYPE u
, normr
, v
;
3772 const struct real_format
*base_fmt
;
3774 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3776 /* Renormalize R before doing any arithmetic on it. */
3778 if (normr
.cl
== rvc_normal
)
3781 /* u = IEEE double precision portion of significand. */
3783 round_for_format (base_fmt
, &u
);
3784 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3786 if (u
.cl
== rvc_normal
)
3788 do_add (&v
, &normr
, &u
, 1);
3789 /* Call round_for_format since we might need to denormalize. */
3790 round_for_format (base_fmt
, &v
);
3791 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3795 /* Inf, NaN, 0 are all representable as doubles, so the
3796 least-significant part can be 0.0. */
3803 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3806 REAL_VALUE_TYPE u
, v
;
3807 const struct real_format
*base_fmt
;
3809 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3810 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3812 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3814 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3815 do_add (r
, &u
, &v
, 0);
3821 const struct real_format ibm_extended_format
=
3823 encode_ibm_extended
,
3824 decode_ibm_extended
,
3842 const struct real_format mips_extended_format
=
3844 encode_ibm_extended
,
3845 decode_ibm_extended
,
3864 /* IEEE quad precision format. */
3866 static void encode_ieee_quad (const struct real_format
*fmt
,
3867 long *, const REAL_VALUE_TYPE
*);
3868 static void decode_ieee_quad (const struct real_format
*,
3869 REAL_VALUE_TYPE
*, const long *);
3872 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3873 const REAL_VALUE_TYPE
*r
)
3875 unsigned long image3
, image2
, image1
, image0
, exp
;
3876 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3879 image3
= r
->sign
<< 31;
3884 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3893 image3
|= 32767 << 16;
3896 image3
|= 0x7fffffff;
3897 image2
= 0xffffffff;
3898 image1
= 0xffffffff;
3899 image0
= 0xffffffff;
3906 image3
|= 32767 << 16;
3910 if (fmt
->canonical_nan_lsbs_set
)
3913 image2
= image1
= image0
= 0xffffffff;
3916 else if (HOST_BITS_PER_LONG
== 32)
3921 image3
|= u
.sig
[3] & 0xffff;
3926 image1
= image0
>> 31 >> 1;
3928 image3
|= (image2
>> 31 >> 1) & 0xffff;
3929 image0
&= 0xffffffff;
3930 image2
&= 0xffffffff;
3932 if (r
->signalling
== fmt
->qnan_msb_set
)
3936 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3941 image3
|= 0x7fffffff;
3942 image2
= 0xffffffff;
3943 image1
= 0xffffffff;
3944 image0
= 0xffffffff;
3949 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3950 whereas the intermediate representation is 0.F x 2**exp.
3951 Which means we're off by one. */
3955 exp
= REAL_EXP (r
) + 16383 - 1;
3956 image3
|= exp
<< 16;
3958 if (HOST_BITS_PER_LONG
== 32)
3963 image3
|= u
.sig
[3] & 0xffff;
3968 image1
= image0
>> 31 >> 1;
3970 image3
|= (image2
>> 31 >> 1) & 0xffff;
3971 image0
&= 0xffffffff;
3972 image2
&= 0xffffffff;
3980 if (FLOAT_WORDS_BIG_ENDIAN
)
3997 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4000 unsigned long image3
, image2
, image1
, image0
;
4004 if (FLOAT_WORDS_BIG_ENDIAN
)
4018 image0
&= 0xffffffff;
4019 image1
&= 0xffffffff;
4020 image2
&= 0xffffffff;
4022 sign
= (image3
>> 31) & 1;
4023 exp
= (image3
>> 16) & 0x7fff;
4026 memset (r
, 0, sizeof (*r
));
4030 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4035 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4036 if (HOST_BITS_PER_LONG
== 32)
4045 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4046 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4051 else if (fmt
->has_signed_zero
)
4054 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4056 if (image3
| image2
| image1
| image0
)
4060 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4062 if (HOST_BITS_PER_LONG
== 32)
4071 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4072 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4074 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4086 SET_REAL_EXP (r
, exp
- 16383 + 1);
4088 if (HOST_BITS_PER_LONG
== 32)
4097 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4098 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4100 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4101 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4105 const struct real_format ieee_quad_format
=
4126 const struct real_format mips_quad_format
=
4147 /* Descriptions of VAX floating point formats can be found beginning at
4149 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4151 The thing to remember is that they're almost IEEE, except for word
4152 order, exponent bias, and the lack of infinities, nans, and denormals.
4154 We don't implement the H_floating format here, simply because neither
4155 the VAX or Alpha ports use it. */
4157 static void encode_vax_f (const struct real_format
*fmt
,
4158 long *, const REAL_VALUE_TYPE
*);
4159 static void decode_vax_f (const struct real_format
*,
4160 REAL_VALUE_TYPE
*, const long *);
4161 static void encode_vax_d (const struct real_format
*fmt
,
4162 long *, const REAL_VALUE_TYPE
*);
4163 static void decode_vax_d (const struct real_format
*,
4164 REAL_VALUE_TYPE
*, const long *);
4165 static void encode_vax_g (const struct real_format
*fmt
,
4166 long *, const REAL_VALUE_TYPE
*);
4167 static void decode_vax_g (const struct real_format
*,
4168 REAL_VALUE_TYPE
*, const long *);
4171 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4172 const REAL_VALUE_TYPE
*r
)
4174 unsigned long sign
, exp
, sig
, image
;
4176 sign
= r
->sign
<< 15;
4186 image
= 0xffff7fff | sign
;
4190 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4191 exp
= REAL_EXP (r
) + 128;
4193 image
= (sig
<< 16) & 0xffff0000;
4207 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4208 REAL_VALUE_TYPE
*r
, const long *buf
)
4210 unsigned long image
= buf
[0] & 0xffffffff;
4211 int exp
= (image
>> 7) & 0xff;
4213 memset (r
, 0, sizeof (*r
));
4218 r
->sign
= (image
>> 15) & 1;
4219 SET_REAL_EXP (r
, exp
- 128);
4221 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4222 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4227 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4228 const REAL_VALUE_TYPE
*r
)
4230 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4235 image0
= image1
= 0;
4240 image0
= 0xffff7fff | sign
;
4241 image1
= 0xffffffff;
4245 /* Extract the significand into straight hi:lo. */
4246 if (HOST_BITS_PER_LONG
== 64)
4248 image0
= r
->sig
[SIGSZ
-1];
4249 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4250 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4254 image0
= r
->sig
[SIGSZ
-1];
4255 image1
= r
->sig
[SIGSZ
-2];
4256 image1
= (image0
<< 24) | (image1
>> 8);
4257 image0
= (image0
>> 8) & 0xffffff;
4260 /* Rearrange the half-words of the significand to match the
4262 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4263 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4265 /* Add the sign and exponent. */
4267 image0
|= (REAL_EXP (r
) + 128) << 7;
4274 if (FLOAT_WORDS_BIG_ENDIAN
)
4275 buf
[0] = image1
, buf
[1] = image0
;
4277 buf
[0] = image0
, buf
[1] = image1
;
4281 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4282 REAL_VALUE_TYPE
*r
, const long *buf
)
4284 unsigned long image0
, image1
;
4287 if (FLOAT_WORDS_BIG_ENDIAN
)
4288 image1
= buf
[0], image0
= buf
[1];
4290 image0
= buf
[0], image1
= buf
[1];
4291 image0
&= 0xffffffff;
4292 image1
&= 0xffffffff;
4294 exp
= (image0
>> 7) & 0xff;
4296 memset (r
, 0, sizeof (*r
));
4301 r
->sign
= (image0
>> 15) & 1;
4302 SET_REAL_EXP (r
, exp
- 128);
4304 /* Rearrange the half-words of the external format into
4305 proper ascending order. */
4306 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4307 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4309 if (HOST_BITS_PER_LONG
== 64)
4311 image0
= (image0
<< 31 << 1) | image1
;
4314 r
->sig
[SIGSZ
-1] = image0
;
4318 r
->sig
[SIGSZ
-1] = image0
;
4319 r
->sig
[SIGSZ
-2] = image1
;
4320 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4321 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4327 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4328 const REAL_VALUE_TYPE
*r
)
4330 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4335 image0
= image1
= 0;
4340 image0
= 0xffff7fff | sign
;
4341 image1
= 0xffffffff;
4345 /* Extract the significand into straight hi:lo. */
4346 if (HOST_BITS_PER_LONG
== 64)
4348 image0
= r
->sig
[SIGSZ
-1];
4349 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4350 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4354 image0
= r
->sig
[SIGSZ
-1];
4355 image1
= r
->sig
[SIGSZ
-2];
4356 image1
= (image0
<< 21) | (image1
>> 11);
4357 image0
= (image0
>> 11) & 0xfffff;
4360 /* Rearrange the half-words of the significand to match the
4362 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4363 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4365 /* Add the sign and exponent. */
4367 image0
|= (REAL_EXP (r
) + 1024) << 4;
4374 if (FLOAT_WORDS_BIG_ENDIAN
)
4375 buf
[0] = image1
, buf
[1] = image0
;
4377 buf
[0] = image0
, buf
[1] = image1
;
4381 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4382 REAL_VALUE_TYPE
*r
, const long *buf
)
4384 unsigned long image0
, image1
;
4387 if (FLOAT_WORDS_BIG_ENDIAN
)
4388 image1
= buf
[0], image0
= buf
[1];
4390 image0
= buf
[0], image1
= buf
[1];
4391 image0
&= 0xffffffff;
4392 image1
&= 0xffffffff;
4394 exp
= (image0
>> 4) & 0x7ff;
4396 memset (r
, 0, sizeof (*r
));
4401 r
->sign
= (image0
>> 15) & 1;
4402 SET_REAL_EXP (r
, exp
- 1024);
4404 /* Rearrange the half-words of the external format into
4405 proper ascending order. */
4406 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4407 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4409 if (HOST_BITS_PER_LONG
== 64)
4411 image0
= (image0
<< 31 << 1) | image1
;
4414 r
->sig
[SIGSZ
-1] = image0
;
4418 r
->sig
[SIGSZ
-1] = image0
;
4419 r
->sig
[SIGSZ
-2] = image1
;
4420 lshift_significand (r
, r
, 64 - 53);
4421 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4426 const struct real_format vax_f_format
=
4447 const struct real_format vax_d_format
=
4468 const struct real_format vax_g_format
=
4489 /* Encode real R into a single precision DFP value in BUF. */
4491 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4492 long *buf ATTRIBUTE_UNUSED
,
4493 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4495 encode_decimal32 (fmt
, buf
, r
);
4498 /* Decode a single precision DFP value in BUF into a real R. */
4500 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4501 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4502 const long *buf ATTRIBUTE_UNUSED
)
4504 decode_decimal32 (fmt
, r
, buf
);
4507 /* Encode real R into a double precision DFP value in BUF. */
4509 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4510 long *buf ATTRIBUTE_UNUSED
,
4511 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4513 encode_decimal64 (fmt
, buf
, r
);
4516 /* Decode a double precision DFP value in BUF into a real R. */
4518 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4519 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4520 const long *buf ATTRIBUTE_UNUSED
)
4522 decode_decimal64 (fmt
, r
, buf
);
4525 /* Encode real R into a quad precision DFP value in BUF. */
4527 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4528 long *buf ATTRIBUTE_UNUSED
,
4529 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4531 encode_decimal128 (fmt
, buf
, r
);
4534 /* Decode a quad precision DFP value in BUF into a real R. */
4536 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4537 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4538 const long *buf ATTRIBUTE_UNUSED
)
4540 decode_decimal128 (fmt
, r
, buf
);
4543 /* Single precision decimal floating point (IEEE 754). */
4544 const struct real_format decimal_single_format
=
4546 encode_decimal_single
,
4547 decode_decimal_single
,
4565 /* Double precision decimal floating point (IEEE 754). */
4566 const struct real_format decimal_double_format
=
4568 encode_decimal_double
,
4569 decode_decimal_double
,
4587 /* Quad precision decimal floating point (IEEE 754). */
4588 const struct real_format decimal_quad_format
=
4590 encode_decimal_quad
,
4591 decode_decimal_quad
,
4609 /* Encode half-precision floats. This routine is used both for the IEEE
4610 ARM alternative encodings. */
4612 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4613 const REAL_VALUE_TYPE
*r
)
4615 unsigned long image
, sig
, exp
;
4616 unsigned long sign
= r
->sign
;
4617 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4620 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4638 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4639 if (r
->signalling
== fmt
->qnan_msb_set
)
4654 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4655 whereas the intermediate representation is 0.F x 2**exp.
4656 Which means we're off by one. */
4660 exp
= REAL_EXP (r
) + 15 - 1;
4672 /* Decode half-precision floats. This routine is used both for the IEEE
4673 ARM alternative encodings. */
4675 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4678 unsigned long image
= buf
[0] & 0xffff;
4679 bool sign
= (image
>> 15) & 1;
4680 int exp
= (image
>> 10) & 0x1f;
4682 memset (r
, 0, sizeof (*r
));
4683 image
<<= HOST_BITS_PER_LONG
- 11;
4688 if (image
&& fmt
->has_denorm
)
4692 SET_REAL_EXP (r
, -14);
4693 r
->sig
[SIGSZ
-1] = image
<< 1;
4696 else if (fmt
->has_signed_zero
)
4699 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4705 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4706 ^ fmt
->qnan_msb_set
);
4707 r
->sig
[SIGSZ
-1] = image
;
4719 SET_REAL_EXP (r
, exp
- 15 + 1);
4720 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4724 /* Half-precision format, as specified in IEEE 754R. */
4725 const struct real_format ieee_half_format
=
4746 /* ARM's alternative half-precision format, similar to IEEE but with
4747 no reserved exponent value for NaNs and infinities; rather, it just
4748 extends the range of exponents by one. */
4749 const struct real_format arm_half_format
=
4770 /* A synthetic "format" for internal arithmetic. It's the size of the
4771 internal significand minus the two bits needed for proper rounding.
4772 The encode and decode routines exist only to satisfy our paranoia
4775 static void encode_internal (const struct real_format
*fmt
,
4776 long *, const REAL_VALUE_TYPE
*);
4777 static void decode_internal (const struct real_format
*,
4778 REAL_VALUE_TYPE
*, const long *);
4781 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4782 const REAL_VALUE_TYPE
*r
)
4784 memcpy (buf
, r
, sizeof (*r
));
4788 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4789 REAL_VALUE_TYPE
*r
, const long *buf
)
4791 memcpy (r
, buf
, sizeof (*r
));
4794 const struct real_format real_internal_format
=
4799 SIGNIFICAND_BITS
- 2,
4800 SIGNIFICAND_BITS
- 2,
4815 /* Calculate X raised to the integer exponent N in mode MODE and store
4816 the result in R. Return true if the result may be inexact due to
4817 loss of precision. The algorithm is the classic "left-to-right binary
4818 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4819 Algorithms", "The Art of Computer Programming", Volume 2. */
4822 real_powi (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4823 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4825 unsigned HOST_WIDE_INT bit
;
4827 bool inexact
= false;
4839 /* Don't worry about overflow, from now on n is unsigned. */
4847 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4848 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4852 inexact
|= do_multiply (&t
, &t
, &t
);
4854 inexact
|= do_multiply (&t
, &t
, x
);
4862 inexact
|= do_divide (&t
, &dconst1
, &t
);
4864 real_convert (r
, mode
, &t
);
4868 /* Round X to the nearest integer not larger in absolute value, i.e.
4869 towards zero, placing the result in R in mode MODE. */
4872 real_trunc (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4873 const REAL_VALUE_TYPE
*x
)
4875 do_fix_trunc (r
, x
);
4876 if (mode
!= VOIDmode
)
4877 real_convert (r
, mode
, r
);
4880 /* Round X to the largest integer not greater in value, i.e. round
4881 down, placing the result in R in mode MODE. */
4884 real_floor (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4885 const REAL_VALUE_TYPE
*x
)
4889 do_fix_trunc (&t
, x
);
4890 if (! real_identical (&t
, x
) && x
->sign
)
4891 do_add (&t
, &t
, &dconstm1
, 0);
4892 if (mode
!= VOIDmode
)
4893 real_convert (r
, mode
, &t
);
4898 /* Round X to the smallest integer not less then argument, i.e. round
4899 up, placing the result in R in mode MODE. */
4902 real_ceil (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4903 const REAL_VALUE_TYPE
*x
)
4907 do_fix_trunc (&t
, x
);
4908 if (! real_identical (&t
, x
) && ! x
->sign
)
4909 do_add (&t
, &t
, &dconst1
, 0);
4910 if (mode
!= VOIDmode
)
4911 real_convert (r
, mode
, &t
);
4916 /* Round X to the nearest integer, but round halfway cases away from
4920 real_round (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4921 const REAL_VALUE_TYPE
*x
)
4923 do_add (r
, x
, &dconsthalf
, x
->sign
);
4924 do_fix_trunc (r
, r
);
4925 if (mode
!= VOIDmode
)
4926 real_convert (r
, mode
, r
);
4929 /* Set the sign of R to the sign of X. */
4932 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4937 /* Check whether the real constant value given is an integer. */
4940 real_isinteger (const REAL_VALUE_TYPE
*c
, enum machine_mode mode
)
4942 REAL_VALUE_TYPE cint
;
4944 real_trunc (&cint
, mode
, c
);
4945 return real_identical (c
, &cint
);
4948 /* Write into BUF the maximum representable finite floating-point
4949 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4950 float string. LEN is the size of BUF, and the buffer must be large
4951 enough to contain the resulting string. */
4954 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
4959 strcpy (buf
, "0x0.");
4961 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
4964 *p
++ = "08ce"[n
- i
];
4965 sprintf (p
, "p%d", fmt
->emax
);
4966 if (fmt
->pnan
< fmt
->p
)
4968 /* This is an IBM extended double format made up of two IEEE
4969 doubles. The value of the long double is the sum of the
4970 values of the two parts. The most significant part is
4971 required to be the value of the long double rounded to the
4972 nearest double. Rounding means we need a slightly smaller
4973 value for LDBL_MAX. */
4974 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
4977 gcc_assert (strlen (buf
) < len
);