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"
31 /* The floating point model used internally is not exactly IEEE 754
32 compliant, and close to the description in the ISO C99 standard,
33 section 5.2.4.2.2 Characteristics of floating types.
37 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
41 b = base or radix, here always 2
43 p = precision (the number of base-b digits in the significand)
44 f_k = the digits of the significand.
46 We differ from typical IEEE 754 encodings in that the entire
47 significand is fractional. Normalized significands are in the
50 A requirement of the model is that P be larger than the largest
51 supported target floating-point type by at least 2 bits. This gives
52 us proper rounding when we truncate to the target type. In addition,
53 E must be large enough to hold the smallest supported denormal number
56 Both of these requirements are easily satisfied. The largest target
57 significand is 113 bits; we store at least 160. The smallest
58 denormal number fits in 17 exponent bits; we store 26. */
61 /* Used to classify two numbers simultaneously. */
62 #define CLASS2(A, B) ((A) << 2 | (B))
64 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
65 #error "Some constant folding done by hand to avoid shift count warnings"
68 static void get_zero (REAL_VALUE_TYPE
*, int);
69 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
70 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
71 static void get_inf (REAL_VALUE_TYPE
*, int);
72 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
73 const REAL_VALUE_TYPE
*, unsigned int);
74 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
76 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
78 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
79 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
80 const REAL_VALUE_TYPE
*);
81 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
82 const REAL_VALUE_TYPE
*, int);
83 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
84 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
85 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
86 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
87 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
88 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
89 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
90 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
91 const REAL_VALUE_TYPE
*);
92 static void normalize (REAL_VALUE_TYPE
*);
94 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
95 const REAL_VALUE_TYPE
*, int);
96 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
97 const REAL_VALUE_TYPE
*);
98 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
99 const REAL_VALUE_TYPE
*);
100 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
101 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
103 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, REAL_VALUE_TYPE
*);
104 static void decimal_from_integer (REAL_VALUE_TYPE
*);
105 static void decimal_integer_string (char *, const REAL_VALUE_TYPE
*,
108 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
109 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
110 static const REAL_VALUE_TYPE
* real_digit (int);
111 static void times_pten (REAL_VALUE_TYPE
*, int);
113 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
115 /* Initialize R with a positive zero. */
118 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
120 memset (r
, 0, sizeof (*r
));
124 /* Initialize R with the canonical quiet NaN. */
127 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
129 memset (r
, 0, sizeof (*r
));
136 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
138 memset (r
, 0, sizeof (*r
));
146 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
148 memset (r
, 0, sizeof (*r
));
154 /* Right-shift the significand of A by N bits; put the result in the
155 significand of R. If any one bits are shifted out, return true. */
158 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
161 unsigned long sticky
= 0;
162 unsigned int i
, ofs
= 0;
164 if (n
>= HOST_BITS_PER_LONG
)
166 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
168 n
&= HOST_BITS_PER_LONG
- 1;
173 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
174 for (i
= 0; i
< SIGSZ
; ++i
)
177 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
178 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
179 << (HOST_BITS_PER_LONG
- n
)));
184 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
185 r
->sig
[i
] = a
->sig
[ofs
+ i
];
186 for (; i
< SIGSZ
; ++i
)
193 /* Right-shift the significand of A by N bits; put the result in the
197 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
200 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
202 n
&= HOST_BITS_PER_LONG
- 1;
205 for (i
= 0; i
< SIGSZ
; ++i
)
208 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
209 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
210 << (HOST_BITS_PER_LONG
- n
)));
215 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
216 r
->sig
[i
] = a
->sig
[ofs
+ i
];
217 for (; i
< SIGSZ
; ++i
)
222 /* Left-shift the significand of A by N bits; put the result in the
226 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
229 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
231 n
&= HOST_BITS_PER_LONG
- 1;
234 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
235 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
236 for (; i
< SIGSZ
; ++i
)
237 r
->sig
[SIGSZ
-1-i
] = 0;
240 for (i
= 0; i
< SIGSZ
; ++i
)
243 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
244 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
245 >> (HOST_BITS_PER_LONG
- n
)));
249 /* Likewise, but N is specialized to 1. */
252 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
256 for (i
= SIGSZ
- 1; i
> 0; --i
)
257 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
258 r
->sig
[0] = a
->sig
[0] << 1;
261 /* Add the significands of A and B, placing the result in R. Return
262 true if there was carry out of the most significant word. */
265 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
266 const REAL_VALUE_TYPE
*b
)
271 for (i
= 0; i
< SIGSZ
; ++i
)
273 unsigned long ai
= a
->sig
[i
];
274 unsigned long ri
= ai
+ b
->sig
[i
];
290 /* Subtract the significands of A and B, placing the result in R. CARRY is
291 true if there's a borrow incoming to the least significant word.
292 Return true if there was borrow out of the most significant word. */
295 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
296 const REAL_VALUE_TYPE
*b
, int carry
)
300 for (i
= 0; i
< SIGSZ
; ++i
)
302 unsigned long ai
= a
->sig
[i
];
303 unsigned long ri
= ai
- b
->sig
[i
];
319 /* Negate the significand A, placing the result in R. */
322 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
327 for (i
= 0; i
< SIGSZ
; ++i
)
329 unsigned long ri
, ai
= a
->sig
[i
];
348 /* Compare significands. Return tri-state vs zero. */
351 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
355 for (i
= SIGSZ
- 1; i
>= 0; --i
)
357 unsigned long ai
= a
->sig
[i
];
358 unsigned long bi
= b
->sig
[i
];
369 /* Return true if A is nonzero. */
372 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
376 for (i
= SIGSZ
- 1; i
>= 0; --i
)
383 /* Set bit N of the significand of R. */
386 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
388 r
->sig
[n
/ HOST_BITS_PER_LONG
]
389 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
392 /* Clear bit N of the significand of R. */
395 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
397 r
->sig
[n
/ HOST_BITS_PER_LONG
]
398 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
401 /* Test bit N of the significand of R. */
404 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
406 /* ??? Compiler bug here if we return this expression directly.
407 The conversion to bool strips the "&1" and we wind up testing
408 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
409 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
413 /* Clear bits 0..N-1 of the significand of R. */
416 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
418 int i
, w
= n
/ HOST_BITS_PER_LONG
;
420 for (i
= 0; i
< w
; ++i
)
423 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
426 /* Divide the significands of A and B, placing the result in R. Return
427 true if the division was inexact. */
430 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
431 const REAL_VALUE_TYPE
*b
)
434 int i
, bit
= SIGNIFICAND_BITS
- 1;
435 unsigned long msb
, inexact
;
438 memset (r
->sig
, 0, sizeof (r
->sig
));
444 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
445 lshift_significand_1 (&u
, &u
);
447 if (msb
|| cmp_significands (&u
, b
) >= 0)
449 sub_significands (&u
, &u
, b
, 0);
450 set_significand_bit (r
, bit
);
455 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
461 /* Adjust the exponent and significand of R such that the most
462 significant bit is set. We underflow to zero and overflow to
463 infinity here, without denormals. (The intermediate representation
464 exponent is large enough to handle target denormals normalized.) */
467 normalize (REAL_VALUE_TYPE
*r
)
475 /* Find the first word that is nonzero. */
476 for (i
= SIGSZ
- 1; i
>= 0; i
--)
478 shift
+= HOST_BITS_PER_LONG
;
482 /* Zero significand flushes to zero. */
490 /* Find the first bit that is nonzero. */
492 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
498 exp
= REAL_EXP (r
) - shift
;
500 get_inf (r
, r
->sign
);
501 else if (exp
< -MAX_EXP
)
502 get_zero (r
, r
->sign
);
505 SET_REAL_EXP (r
, exp
);
506 lshift_significand (r
, r
, shift
);
511 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
512 result may be inexact due to a loss of precision. */
515 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
516 const REAL_VALUE_TYPE
*b
, int subtract_p
)
520 bool inexact
= false;
522 /* Determine if we need to add or subtract. */
524 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
526 switch (CLASS2 (a
->cl
, b
->cl
))
528 case CLASS2 (rvc_zero
, rvc_zero
):
529 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
530 get_zero (r
, sign
& !subtract_p
);
533 case CLASS2 (rvc_zero
, rvc_normal
):
534 case CLASS2 (rvc_zero
, rvc_inf
):
535 case CLASS2 (rvc_zero
, rvc_nan
):
537 case CLASS2 (rvc_normal
, rvc_nan
):
538 case CLASS2 (rvc_inf
, rvc_nan
):
539 case CLASS2 (rvc_nan
, rvc_nan
):
540 /* ANY + NaN = NaN. */
541 case CLASS2 (rvc_normal
, rvc_inf
):
544 r
->sign
= sign
^ subtract_p
;
547 case CLASS2 (rvc_normal
, rvc_zero
):
548 case CLASS2 (rvc_inf
, rvc_zero
):
549 case CLASS2 (rvc_nan
, rvc_zero
):
551 case CLASS2 (rvc_nan
, rvc_normal
):
552 case CLASS2 (rvc_nan
, rvc_inf
):
553 /* NaN + ANY = NaN. */
554 case CLASS2 (rvc_inf
, rvc_normal
):
559 case CLASS2 (rvc_inf
, rvc_inf
):
561 /* Inf - Inf = NaN. */
562 get_canonical_qnan (r
, 0);
564 /* Inf + Inf = Inf. */
568 case CLASS2 (rvc_normal
, rvc_normal
):
575 /* Swap the arguments such that A has the larger exponent. */
576 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
579 const REAL_VALUE_TYPE
*t
;
586 /* If the exponents are not identical, we need to shift the
587 significand of B down. */
590 /* If the exponents are too far apart, the significands
591 do not overlap, which makes the subtraction a noop. */
592 if (dexp
>= SIGNIFICAND_BITS
)
599 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
605 if (sub_significands (r
, a
, b
, inexact
))
607 /* We got a borrow out of the subtraction. That means that
608 A and B had the same exponent, and B had the larger
609 significand. We need to swap the sign and negate the
612 neg_significand (r
, r
);
617 if (add_significands (r
, a
, b
))
619 /* We got carry out of the addition. This means we need to
620 shift the significand back down one bit and increase the
622 inexact
|= sticky_rshift_significand (r
, r
, 1);
623 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
634 SET_REAL_EXP (r
, exp
);
635 /* Zero out the remaining fields. */
640 /* Re-normalize the result. */
643 /* Special case: if the subtraction results in zero, the result
645 if (r
->cl
== rvc_zero
)
648 r
->sig
[0] |= inexact
;
653 /* Calculate R = A * B. Return true if the result may be inexact. */
656 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
657 const REAL_VALUE_TYPE
*b
)
659 REAL_VALUE_TYPE u
, t
, *rr
;
660 unsigned int i
, j
, k
;
661 int sign
= a
->sign
^ b
->sign
;
662 bool inexact
= false;
664 switch (CLASS2 (a
->cl
, b
->cl
))
666 case CLASS2 (rvc_zero
, rvc_zero
):
667 case CLASS2 (rvc_zero
, rvc_normal
):
668 case CLASS2 (rvc_normal
, rvc_zero
):
669 /* +-0 * ANY = 0 with appropriate sign. */
673 case CLASS2 (rvc_zero
, rvc_nan
):
674 case CLASS2 (rvc_normal
, rvc_nan
):
675 case CLASS2 (rvc_inf
, rvc_nan
):
676 case CLASS2 (rvc_nan
, rvc_nan
):
677 /* ANY * NaN = NaN. */
682 case CLASS2 (rvc_nan
, rvc_zero
):
683 case CLASS2 (rvc_nan
, rvc_normal
):
684 case CLASS2 (rvc_nan
, rvc_inf
):
685 /* NaN * ANY = NaN. */
690 case CLASS2 (rvc_zero
, rvc_inf
):
691 case CLASS2 (rvc_inf
, rvc_zero
):
693 get_canonical_qnan (r
, sign
);
696 case CLASS2 (rvc_inf
, rvc_inf
):
697 case CLASS2 (rvc_normal
, rvc_inf
):
698 case CLASS2 (rvc_inf
, rvc_normal
):
699 /* Inf * Inf = Inf, R * Inf = Inf */
703 case CLASS2 (rvc_normal
, rvc_normal
):
710 if (r
== a
|| r
== b
)
716 /* Collect all the partial products. Since we don't have sure access
717 to a widening multiply, we split each long into two half-words.
719 Consider the long-hand form of a four half-word multiplication:
729 We construct partial products of the widened half-word products
730 that are known to not overlap, e.g. DF+DH. Each such partial
731 product is given its proper exponent, which allows us to sum them
732 and obtain the finished product. */
734 for (i
= 0; i
< SIGSZ
* 2; ++i
)
736 unsigned long ai
= a
->sig
[i
/ 2];
738 ai
>>= HOST_BITS_PER_LONG
/ 2;
740 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
745 for (j
= 0; j
< 2; ++j
)
747 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
748 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
757 /* Would underflow to zero, which we shouldn't bother adding. */
762 memset (&u
, 0, sizeof (u
));
764 SET_REAL_EXP (&u
, exp
);
766 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
768 unsigned long bi
= b
->sig
[k
/ 2];
770 bi
>>= HOST_BITS_PER_LONG
/ 2;
772 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
774 u
.sig
[k
/ 2] = ai
* bi
;
778 inexact
|= do_add (rr
, rr
, &u
, 0);
789 /* Calculate R = A / B. Return true if the result may be inexact. */
792 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
793 const REAL_VALUE_TYPE
*b
)
795 int exp
, sign
= a
->sign
^ b
->sign
;
796 REAL_VALUE_TYPE t
, *rr
;
799 switch (CLASS2 (a
->cl
, b
->cl
))
801 case CLASS2 (rvc_zero
, rvc_zero
):
803 case CLASS2 (rvc_inf
, rvc_inf
):
804 /* Inf / Inf = NaN. */
805 get_canonical_qnan (r
, sign
);
808 case CLASS2 (rvc_zero
, rvc_normal
):
809 case CLASS2 (rvc_zero
, rvc_inf
):
811 case CLASS2 (rvc_normal
, rvc_inf
):
816 case CLASS2 (rvc_normal
, rvc_zero
):
818 case CLASS2 (rvc_inf
, rvc_zero
):
823 case CLASS2 (rvc_zero
, rvc_nan
):
824 case CLASS2 (rvc_normal
, rvc_nan
):
825 case CLASS2 (rvc_inf
, rvc_nan
):
826 case CLASS2 (rvc_nan
, rvc_nan
):
827 /* ANY / NaN = NaN. */
832 case CLASS2 (rvc_nan
, rvc_zero
):
833 case CLASS2 (rvc_nan
, rvc_normal
):
834 case CLASS2 (rvc_nan
, rvc_inf
):
835 /* NaN / ANY = NaN. */
840 case CLASS2 (rvc_inf
, rvc_normal
):
845 case CLASS2 (rvc_normal
, rvc_normal
):
852 if (r
== a
|| r
== b
)
857 /* Make sure all fields in the result are initialized. */
862 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
873 SET_REAL_EXP (rr
, exp
);
875 inexact
= div_significands (rr
, a
, b
);
877 /* Re-normalize the result. */
879 rr
->sig
[0] |= inexact
;
887 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
888 one of the two operands is a NaN. */
891 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
896 switch (CLASS2 (a
->cl
, b
->cl
))
898 case CLASS2 (rvc_zero
, rvc_zero
):
899 /* Sign of zero doesn't matter for compares. */
902 case CLASS2 (rvc_normal
, rvc_zero
):
903 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
905 return decimal_do_compare (a
, b
, nan_result
);
907 case CLASS2 (rvc_inf
, rvc_zero
):
908 case CLASS2 (rvc_inf
, rvc_normal
):
909 return (a
->sign
? -1 : 1);
911 case CLASS2 (rvc_inf
, rvc_inf
):
912 return -a
->sign
- -b
->sign
;
914 case CLASS2 (rvc_zero
, rvc_normal
):
915 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
917 return decimal_do_compare (a
, b
, nan_result
);
919 case CLASS2 (rvc_zero
, rvc_inf
):
920 case CLASS2 (rvc_normal
, rvc_inf
):
921 return (b
->sign
? 1 : -1);
923 case CLASS2 (rvc_zero
, rvc_nan
):
924 case CLASS2 (rvc_normal
, rvc_nan
):
925 case CLASS2 (rvc_inf
, rvc_nan
):
926 case CLASS2 (rvc_nan
, rvc_nan
):
927 case CLASS2 (rvc_nan
, rvc_zero
):
928 case CLASS2 (rvc_nan
, rvc_normal
):
929 case CLASS2 (rvc_nan
, rvc_inf
):
932 case CLASS2 (rvc_normal
, rvc_normal
):
939 if (a
->sign
!= b
->sign
)
940 return -a
->sign
- -b
->sign
;
942 if (a
->decimal
|| b
->decimal
)
943 return decimal_do_compare (a
, b
, nan_result
);
945 if (REAL_EXP (a
) > REAL_EXP (b
))
947 else if (REAL_EXP (a
) < REAL_EXP (b
))
950 ret
= cmp_significands (a
, b
);
952 return (a
->sign
? -ret
: ret
);
955 /* Return A truncated to an integral value toward zero. */
958 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
972 decimal_do_fix_trunc (r
, a
);
975 if (REAL_EXP (r
) <= 0)
976 get_zero (r
, r
->sign
);
977 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
978 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
986 /* Perform the binary or unary operation described by CODE.
987 For a unary operation, leave OP1 NULL. This function returns
988 true if the result may be inexact due to loss of precision. */
991 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
992 const REAL_VALUE_TYPE
*op1
)
994 enum tree_code code
= (enum tree_code
) icode
;
996 if (op0
->decimal
|| (op1
&& op1
->decimal
))
997 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1002 /* Clear any padding areas in *r if it isn't equal to one of the
1003 operands so that we can later do bitwise comparisons later on. */
1004 if (r
!= op0
&& r
!= op1
)
1005 memset (r
, '\0', sizeof (*r
));
1006 return do_add (r
, op0
, op1
, 0);
1009 if (r
!= op0
&& r
!= op1
)
1010 memset (r
, '\0', sizeof (*r
));
1011 return do_add (r
, op0
, op1
, 1);
1014 if (r
!= op0
&& r
!= op1
)
1015 memset (r
, '\0', sizeof (*r
));
1016 return do_multiply (r
, op0
, op1
);
1019 if (r
!= op0
&& r
!= op1
)
1020 memset (r
, '\0', sizeof (*r
));
1021 return do_divide (r
, op0
, op1
);
1024 if (op1
->cl
== rvc_nan
)
1026 else if (do_compare (op0
, op1
, -1) < 0)
1033 if (op1
->cl
== rvc_nan
)
1035 else if (do_compare (op0
, op1
, 1) < 0)
1051 case FIX_TRUNC_EXPR
:
1052 do_fix_trunc (r
, op0
);
1062 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1065 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1070 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1073 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1077 /* Return whether OP0 == OP1. */
1080 real_equal (const REAL_VALUE_TYPE
*op0
, const REAL_VALUE_TYPE
*op1
)
1082 return do_compare (op0
, op1
, -1) == 0;
1085 /* Return whether OP0 < OP1. */
1088 real_less (const REAL_VALUE_TYPE
*op0
, const REAL_VALUE_TYPE
*op1
)
1090 return do_compare (op0
, op1
, 1) < 0;
1094 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1095 const REAL_VALUE_TYPE
*op1
)
1097 enum tree_code code
= (enum tree_code
) icode
;
1102 return real_less (op0
, op1
);
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 real_equal (op0
, op1
);
1112 return do_compare (op0
, op1
, -1) != 0;
1113 case UNORDERED_EXPR
:
1114 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1116 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
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
, 1) >= 0;
1126 return do_compare (op0
, op1
, 0) == 0;
1128 return do_compare (op0
, op1
, 0) != 0;
1135 /* Return floor log2(R). */
1138 real_exponent (const REAL_VALUE_TYPE
*r
)
1146 return (unsigned int)-1 >> 1;
1148 return REAL_EXP (r
);
1154 /* R = OP0 * 2**EXP. */
1157 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1168 exp
+= REAL_EXP (op0
);
1170 get_inf (r
, r
->sign
);
1171 else if (exp
< -MAX_EXP
)
1172 get_zero (r
, r
->sign
);
1174 SET_REAL_EXP (r
, exp
);
1182 /* Determine whether a floating-point value X is infinite. */
1185 real_isinf (const REAL_VALUE_TYPE
*r
)
1187 return (r
->cl
== rvc_inf
);
1190 /* Determine whether a floating-point value X is a NaN. */
1193 real_isnan (const REAL_VALUE_TYPE
*r
)
1195 return (r
->cl
== rvc_nan
);
1198 /* Determine whether a floating-point value X is a signaling NaN. */
1199 bool real_issignaling_nan (const REAL_VALUE_TYPE
*r
)
1201 return real_isnan (r
) && r
->signalling
;
1204 /* Determine whether a floating-point value X is finite. */
1207 real_isfinite (const REAL_VALUE_TYPE
*r
)
1209 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1212 /* Determine whether a floating-point value X is negative. */
1215 real_isneg (const REAL_VALUE_TYPE
*r
)
1220 /* Determine whether a floating-point value X is minus zero. */
1223 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1225 return r
->sign
&& r
->cl
== rvc_zero
;
1228 /* Compare two floating-point objects for bitwise identity. */
1231 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1237 if (a
->sign
!= b
->sign
)
1247 if (a
->decimal
!= b
->decimal
)
1249 if (REAL_EXP (a
) != REAL_EXP (b
))
1254 if (a
->signalling
!= b
->signalling
)
1256 /* The significand is ignored for canonical NaNs. */
1257 if (a
->canonical
|| b
->canonical
)
1258 return a
->canonical
== b
->canonical
;
1265 for (i
= 0; i
< SIGSZ
; ++i
)
1266 if (a
->sig
[i
] != b
->sig
[i
])
1272 /* Try to change R into its exact multiplicative inverse in format FMT.
1273 Return true if successful. */
1276 exact_real_inverse (format_helper fmt
, REAL_VALUE_TYPE
*r
)
1278 const REAL_VALUE_TYPE
*one
= real_digit (1);
1282 if (r
->cl
!= rvc_normal
)
1285 /* Check for a power of two: all significand bits zero except the MSB. */
1286 for (i
= 0; i
< SIGSZ
-1; ++i
)
1289 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1292 /* Find the inverse and truncate to the required format. */
1293 do_divide (&u
, one
, r
);
1294 real_convert (&u
, fmt
, &u
);
1296 /* The rounding may have overflowed. */
1297 if (u
.cl
!= rvc_normal
)
1299 for (i
= 0; i
< SIGSZ
-1; ++i
)
1302 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1309 /* Return true if arithmetic on values in IMODE that were promoted
1310 from values in TMODE is equivalent to direct arithmetic on values
1314 real_can_shorten_arithmetic (machine_mode imode
, machine_mode tmode
)
1316 const struct real_format
*tfmt
, *ifmt
;
1317 tfmt
= REAL_MODE_FORMAT (tmode
);
1318 ifmt
= REAL_MODE_FORMAT (imode
);
1319 /* These conditions are conservative rather than trying to catch the
1320 exact boundary conditions; the main case to allow is IEEE float
1322 return (ifmt
->b
== tfmt
->b
1323 && ifmt
->p
> 2 * tfmt
->p
1324 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1325 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1326 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1327 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1328 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1329 && (ifmt
->has_sign_dependent_rounding
1330 == tfmt
->has_sign_dependent_rounding
)
1331 && ifmt
->has_nans
>= tfmt
->has_nans
1332 && ifmt
->has_inf
>= tfmt
->has_inf
1333 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1334 && !MODE_COMPOSITE_P (tmode
)
1335 && !MODE_COMPOSITE_P (imode
));
1338 /* Render R as an integer. */
1341 real_to_integer (const REAL_VALUE_TYPE
*r
)
1343 unsigned HOST_WIDE_INT i
;
1354 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1361 return decimal_real_to_integer (r
);
1363 if (REAL_EXP (r
) <= 0)
1365 /* Only force overflow for unsigned overflow. Signed overflow is
1366 undefined, so it doesn't matter what we return, and some callers
1367 expect to be able to use this routine for both signed and
1368 unsigned conversions. */
1369 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1372 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1373 i
= r
->sig
[SIGSZ
-1];
1376 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1377 i
= r
->sig
[SIGSZ
-1];
1378 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1379 i
|= r
->sig
[SIGSZ
-2];
1382 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1393 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1394 be represented in precision, *FAIL is set to TRUE. */
1397 real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
1399 HOST_WIDE_INT val
[2 * WIDE_INT_MAX_ELTS
];
1408 return wi::zero (precision
);
1416 return wi::set_bit_in_zero (precision
- 1, precision
);
1418 return ~wi::set_bit_in_zero (precision
- 1, precision
);
1422 return decimal_real_to_integer (r
, fail
, precision
);
1427 /* Only force overflow for unsigned overflow. Signed overflow is
1428 undefined, so it doesn't matter what we return, and some callers
1429 expect to be able to use this routine for both signed and
1430 unsigned conversions. */
1431 if (exp
> precision
)
1434 /* Put the significand into a wide_int that has precision W, which
1435 is the smallest HWI-multiple that has at least PRECISION bits.
1436 This ensures that the top bit of the significand is in the
1437 top bit of the wide_int. */
1438 words
= (precision
+ HOST_BITS_PER_WIDE_INT
- 1) / HOST_BITS_PER_WIDE_INT
;
1439 w
= words
* HOST_BITS_PER_WIDE_INT
;
1441 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1442 for (int i
= 0; i
< words
; i
++)
1444 int j
= SIGSZ
- words
+ i
;
1445 val
[i
] = (j
< 0) ? 0 : r
->sig
[j
];
1448 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1449 for (int i
= 0; i
< words
; i
++)
1451 int j
= SIGSZ
- (words
* 2) + (i
* 2);
1458 val
[i
] |= (unsigned HOST_WIDE_INT
) r
->sig
[j
] << HOST_BITS_PER_LONG
;
1461 /* Shift the value into place and truncate to the desired precision. */
1462 result
= wide_int::from_array (val
, words
, w
);
1463 result
= wi::lrshift (result
, w
- exp
);
1464 result
= wide_int::from (result
, precision
, UNSIGNED
);
1476 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1477 of NUM / DEN. Return the quotient and place the remainder in NUM.
1478 It is expected that NUM / DEN are close enough that the quotient is
1481 static unsigned long
1482 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1484 unsigned long q
, msb
;
1485 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1494 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1496 lshift_significand_1 (num
, num
);
1498 if (msb
|| cmp_significands (num
, den
) >= 0)
1500 sub_significands (num
, num
, den
, 0);
1504 while (--expn
>= expd
);
1506 SET_REAL_EXP (num
, expd
);
1512 /* Render R as a decimal floating point constant. Emit DIGITS significant
1513 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1514 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1515 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1516 to a string that, when parsed back in mode MODE, yields the same value. */
1518 #define M_LOG10_2 0.30102999566398119521
1521 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1522 size_t buf_size
, size_t digits
,
1523 int crop_trailing_zeros
, machine_mode mode
)
1525 const struct real_format
*fmt
= NULL
;
1526 const REAL_VALUE_TYPE
*one
, *ten
;
1527 REAL_VALUE_TYPE r
, pten
, u
, v
;
1528 int dec_exp
, cmp_one
, digit
;
1530 char *p
, *first
, *last
;
1534 if (mode
!= VOIDmode
)
1536 fmt
= REAL_MODE_FORMAT (mode
);
1544 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1549 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1552 /* ??? Print the significand as well, if not canonical? */
1553 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1554 (r_orig
->signalling
? 'S' : 'Q'));
1562 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1566 /* Bound the number of digits printed by the size of the representation. */
1567 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1568 if (digits
== 0 || digits
> max_digits
)
1569 digits
= max_digits
;
1571 /* Estimate the decimal exponent, and compute the length of the string it
1572 will print as. Be conservative and add one to account for possible
1573 overflow or rounding error. */
1574 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1575 for (max_digits
= 1; dec_exp
; max_digits
++)
1578 /* Bound the number of digits printed by the size of the output buffer. */
1579 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1580 gcc_assert (max_digits
<= buf_size
);
1581 if (digits
> max_digits
)
1582 digits
= max_digits
;
1584 one
= real_digit (1);
1585 ten
= ten_to_ptwo (0);
1593 cmp_one
= do_compare (&r
, one
, 0);
1598 /* Number is greater than one. Convert significand to an integer
1599 and strip trailing decimal zeros. */
1602 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1604 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1605 m
= floor_log2 (max_digits
);
1607 /* Iterate over the bits of the possible powers of 10 that might
1608 be present in U and eliminate them. That is, if we find that
1609 10**2**M divides U evenly, keep the division and increase
1615 do_divide (&t
, &u
, ten_to_ptwo (m
));
1616 do_fix_trunc (&v
, &t
);
1617 if (cmp_significands (&v
, &t
) == 0)
1625 /* Revert the scaling to integer that we performed earlier. */
1626 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1627 - (SIGNIFICAND_BITS
- 1));
1630 /* Find power of 10. Do this by dividing out 10**2**M when
1631 this is larger than the current remainder. Fill PTEN with
1632 the power of 10 that we compute. */
1633 if (REAL_EXP (&r
) > 0)
1635 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1638 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1639 if (do_compare (&u
, ptentwo
, 0) >= 0)
1641 do_divide (&u
, &u
, ptentwo
);
1642 do_multiply (&pten
, &pten
, ptentwo
);
1649 /* We managed to divide off enough tens in the above reduction
1650 loop that we've now got a negative exponent. Fall into the
1651 less-than-one code to compute the proper value for PTEN. */
1658 /* Number is less than one. Pad significand with leading
1664 /* Stop if we'd shift bits off the bottom. */
1668 do_multiply (&u
, &v
, ten
);
1670 /* Stop if we're now >= 1. */
1671 if (REAL_EXP (&u
) > 0)
1679 /* Find power of 10. Do this by multiplying in P=10**2**M when
1680 the current remainder is smaller than 1/P. Fill PTEN with the
1681 power of 10 that we compute. */
1682 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1685 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1686 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1688 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1690 do_multiply (&v
, &v
, ptentwo
);
1691 do_multiply (&pten
, &pten
, ptentwo
);
1697 /* Invert the positive power of 10 that we've collected so far. */
1698 do_divide (&pten
, one
, &pten
);
1706 /* At this point, PTEN should contain the nearest power of 10 smaller
1707 than R, such that this division produces the first digit.
1709 Using a divide-step primitive that returns the complete integral
1710 remainder avoids the rounding error that would be produced if
1711 we were to use do_divide here and then simply multiply by 10 for
1712 each subsequent digit. */
1714 digit
= rtd_divmod (&r
, &pten
);
1716 /* Be prepared for error in that division via underflow ... */
1717 if (digit
== 0 && cmp_significand_0 (&r
))
1719 /* Multiply by 10 and try again. */
1720 do_multiply (&r
, &r
, ten
);
1721 digit
= rtd_divmod (&r
, &pten
);
1723 gcc_assert (digit
!= 0);
1726 /* ... or overflow. */
1736 gcc_assert (digit
<= 10);
1740 /* Generate subsequent digits. */
1741 while (--digits
> 0)
1743 do_multiply (&r
, &r
, ten
);
1744 digit
= rtd_divmod (&r
, &pten
);
1749 /* Generate one more digit with which to do rounding. */
1750 do_multiply (&r
, &r
, ten
);
1751 digit
= rtd_divmod (&r
, &pten
);
1753 /* Round the result. */
1754 if (fmt
&& fmt
->round_towards_zero
)
1756 /* If the format uses round towards zero when parsing the string
1757 back in, we need to always round away from zero here. */
1758 if (cmp_significand_0 (&r
))
1760 round_up
= digit
> 0;
1766 /* Round to nearest. If R is nonzero there are additional
1767 nonzero digits to be extracted. */
1768 if (cmp_significand_0 (&r
))
1770 /* Round to even. */
1771 else if ((p
[-1] - '0') & 1)
1775 round_up
= digit
> 5;
1792 /* Carry out of the first digit. This means we had all 9's and
1793 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1801 /* Insert the decimal point. */
1802 first
[0] = first
[1];
1805 /* If requested, drop trailing zeros. Never crop past "1.0". */
1806 if (crop_trailing_zeros
)
1807 while (last
> first
+ 3 && last
[-1] == '0')
1810 /* Append the exponent. */
1811 sprintf (last
, "e%+d", dec_exp
);
1813 /* Verify that we can read the original value back in. */
1814 if (flag_checking
&& mode
!= VOIDmode
)
1816 real_from_string (&r
, str
);
1817 real_convert (&r
, mode
, &r
);
1818 gcc_assert (real_identical (&r
, r_orig
));
1822 /* Likewise, except always uses round-to-nearest. */
1825 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1826 size_t digits
, int crop_trailing_zeros
)
1828 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1829 digits
, crop_trailing_zeros
, VOIDmode
);
1832 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1833 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1834 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1835 strip trailing zeros. */
1838 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1839 size_t digits
, int crop_trailing_zeros
)
1841 int i
, j
, exp
= REAL_EXP (r
);
1854 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1857 /* ??? Print the significand as well, if not canonical? */
1858 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1859 (r
->signalling
? 'S' : 'Q'));
1867 /* Hexadecimal format for decimal floats is not interesting. */
1868 strcpy (str
, "N/A");
1873 digits
= SIGNIFICAND_BITS
/ 4;
1875 /* Bound the number of digits printed by the size of the output buffer. */
1877 sprintf (exp_buf
, "p%+d", exp
);
1878 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1879 gcc_assert (max_digits
<= buf_size
);
1880 if (digits
> max_digits
)
1881 digits
= max_digits
;
1892 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1893 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1895 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1901 if (crop_trailing_zeros
)
1902 while (p
> first
+ 1 && p
[-1] == '0')
1905 sprintf (p
, "p%+d", exp
);
1908 /* Initialize R from a decimal or hexadecimal string. The string is
1909 assumed to have been syntax checked already. Return -1 if the
1910 value underflows, +1 if overflows, and 0 otherwise. */
1913 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1925 else if (*str
== '+')
1928 if (!strncmp (str
, "QNaN", 4))
1930 get_canonical_qnan (r
, sign
);
1933 else if (!strncmp (str
, "SNaN", 4))
1935 get_canonical_snan (r
, sign
);
1938 else if (!strncmp (str
, "Inf", 3))
1944 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1946 /* Hexadecimal floating point. */
1947 int pos
= SIGNIFICAND_BITS
- 4, d
;
1955 d
= hex_value (*str
);
1960 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1961 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1965 /* Ensure correct rounding by setting last bit if there is
1966 a subsequent nonzero digit. */
1974 if (pos
== SIGNIFICAND_BITS
- 4)
1981 d
= hex_value (*str
);
1986 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1987 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1991 /* Ensure correct rounding by setting last bit if there is
1992 a subsequent nonzero digit. */
1998 /* If the mantissa is zero, ignore the exponent. */
1999 if (!cmp_significand_0 (r
))
2002 if (*str
== 'p' || *str
== 'P')
2004 bool exp_neg
= false;
2012 else if (*str
== '+')
2016 while (ISDIGIT (*str
))
2022 /* Overflowed the exponent. */
2037 SET_REAL_EXP (r
, exp
);
2043 /* Decimal floating point. */
2044 const char *cstr
= str
;
2048 while (*cstr
== '0')
2053 while (*cstr
== '0')
2057 /* If the mantissa is zero, ignore the exponent. */
2058 if (!ISDIGIT (*cstr
))
2061 /* Nonzero value, possibly overflowing or underflowing. */
2062 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2063 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, GMP_RNDZ
);
2064 /* The result should never be a NaN, and because the rounding is
2065 toward zero should never be an infinity. */
2066 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2067 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2072 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2079 real_from_mpfr (r
, m
, NULL_TREE
, GMP_RNDZ
);
2080 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2081 because the hex digits used in real_from_mpfr did not
2082 start with a digit 8 to f, but the exponent bounds above
2083 should have avoided underflow or overflow. */
2084 gcc_assert (r
->cl
== rvc_normal
);
2085 /* Set a sticky bit if mpfr_strtofr was inexact. */
2086 r
->sig
[0] |= inexact
;
2107 /* Legacy. Similar, but return the result directly. */
2110 real_from_string2 (const char *s
, format_helper fmt
)
2114 real_from_string (&r
, s
);
2116 real_convert (&r
, fmt
, &r
);
2121 /* Initialize R from string S and desired format FMT. */
2124 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, format_helper fmt
)
2126 if (fmt
.decimal_p ())
2127 decimal_real_from_string (r
, s
);
2129 real_from_string (r
, s
);
2132 real_convert (r
, fmt
, r
);
2135 /* Initialize R from the wide_int VAL_IN. Round it to format FMT if
2139 real_from_integer (REAL_VALUE_TYPE
*r
, format_helper fmt
,
2140 const wide_int_ref
&val_in
, signop sgn
)
2146 unsigned int len
= val_in
.get_precision ();
2148 int maxbitlen
= MAX_BITSIZE_MODE_ANY_INT
+ HOST_BITS_PER_WIDE_INT
;
2149 const unsigned int realmax
= (SIGNIFICAND_BITS
/ HOST_BITS_PER_WIDE_INT
2150 * HOST_BITS_PER_WIDE_INT
);
2152 memset (r
, 0, sizeof (*r
));
2154 r
->sign
= wi::neg_p (val_in
, sgn
);
2156 /* We have to ensure we can negate the largest negative number. */
2157 wide_int val
= wide_int::from (val_in
, maxbitlen
, sgn
);
2162 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2163 won't work with precisions that are not a multiple of
2164 HOST_BITS_PER_WIDE_INT. */
2165 len
+= HOST_BITS_PER_WIDE_INT
- 1;
2167 /* Ensure we can represent the largest negative number. */
2170 len
= len
/HOST_BITS_PER_WIDE_INT
* HOST_BITS_PER_WIDE_INT
;
2172 /* Cap the size to the size allowed by real.h. */
2175 HOST_WIDE_INT cnt_l_z
;
2176 cnt_l_z
= wi::clz (val
);
2178 if (maxbitlen
- cnt_l_z
> realmax
)
2180 e
= maxbitlen
- cnt_l_z
- realmax
;
2182 /* This value is too large, we must shift it right to
2183 preserve all the bits we can, and then bump the
2184 exponent up by that amount. */
2185 val
= wi::lrshift (val
, e
);
2190 /* Clear out top bits so elt will work with precisions that aren't
2191 a multiple of HOST_BITS_PER_WIDE_INT. */
2192 val
= wide_int::from (val
, len
, sgn
);
2193 len
= len
/ HOST_BITS_PER_WIDE_INT
;
2195 SET_REAL_EXP (r
, len
* HOST_BITS_PER_WIDE_INT
+ e
);
2198 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2199 for (i
= len
- 1; i
>= 0; i
--)
2201 r
->sig
[j
--] = val
.elt (i
);
2207 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2208 for (i
= len
- 1; i
>= 0; i
--)
2210 HOST_WIDE_INT e
= val
.elt (i
);
2211 r
->sig
[j
--] = e
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2223 if (fmt
.decimal_p ())
2224 decimal_from_integer (r
);
2226 real_convert (r
, fmt
, r
);
2229 /* Render R, an integral value, as a floating point constant with no
2230 specified exponent. */
2233 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2236 int dec_exp
, digit
, digits
;
2237 REAL_VALUE_TYPE r
, pten
;
2243 if (r
.cl
== rvc_zero
)
2252 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2253 digits
= dec_exp
+ 1;
2254 gcc_assert ((digits
+ 2) < (int)buf_size
);
2256 pten
= *real_digit (1);
2257 times_pten (&pten
, dec_exp
);
2263 digit
= rtd_divmod (&r
, &pten
);
2264 gcc_assert (digit
>= 0 && digit
<= 9);
2266 while (--digits
> 0)
2269 digit
= rtd_divmod (&r
, &pten
);
2276 /* Convert a real with an integral value to decimal float. */
2279 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2283 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2284 decimal_real_from_string (r
, str
);
2287 /* Returns 10**2**N. */
2289 static const REAL_VALUE_TYPE
*
2292 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2294 gcc_assert (n
>= 0);
2295 gcc_assert (n
< EXP_BITS
);
2297 if (tens
[n
].cl
== rvc_zero
)
2299 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2301 HOST_WIDE_INT t
= 10;
2304 for (i
= 0; i
< n
; ++i
)
2307 real_from_integer (&tens
[n
], VOIDmode
, t
, UNSIGNED
);
2311 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2312 do_multiply (&tens
[n
], t
, t
);
2319 /* Returns 10**(-2**N). */
2321 static const REAL_VALUE_TYPE
*
2322 ten_to_mptwo (int n
)
2324 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2326 gcc_assert (n
>= 0);
2327 gcc_assert (n
< EXP_BITS
);
2329 if (tens
[n
].cl
== rvc_zero
)
2330 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2337 static const REAL_VALUE_TYPE
*
2340 static REAL_VALUE_TYPE num
[10];
2342 gcc_assert (n
>= 0);
2343 gcc_assert (n
<= 9);
2345 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2346 real_from_integer (&num
[n
], VOIDmode
, n
, UNSIGNED
);
2351 /* Multiply R by 10**EXP. */
2354 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2356 REAL_VALUE_TYPE pten
, *rr
;
2357 bool negative
= (exp
< 0);
2363 pten
= *real_digit (1);
2369 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2371 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2374 do_divide (r
, r
, &pten
);
2377 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2379 const REAL_VALUE_TYPE
*
2382 static REAL_VALUE_TYPE value
;
2384 /* Initialize mathematical constants for constant folding builtins.
2385 These constants need to be given to at least 160 bits precision. */
2386 if (value
.cl
== rvc_zero
)
2389 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2390 mpfr_set_ui (m
, 1, GMP_RNDN
);
2391 mpfr_exp (m
, m
, GMP_RNDN
);
2392 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2399 /* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2401 #define CACHED_FRACTION(NAME, N) \
2402 const REAL_VALUE_TYPE * \
2405 static REAL_VALUE_TYPE value; \
2407 /* Initialize mathematical constants for constant folding builtins. \
2408 These constants need to be given to at least 160 bits \
2410 if (value.cl == rvc_zero) \
2411 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2415 CACHED_FRACTION (dconst_third_ptr
, 3)
2416 CACHED_FRACTION (dconst_quarter_ptr
, 4)
2417 CACHED_FRACTION (dconst_sixth_ptr
, 6)
2418 CACHED_FRACTION (dconst_ninth_ptr
, 9)
2420 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2422 const REAL_VALUE_TYPE
*
2423 dconst_sqrt2_ptr (void)
2425 static REAL_VALUE_TYPE value
;
2427 /* Initialize mathematical constants for constant folding builtins.
2428 These constants need to be given to at least 160 bits precision. */
2429 if (value
.cl
== rvc_zero
)
2432 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2433 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2434 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2440 /* Fills R with +Inf. */
2443 real_inf (REAL_VALUE_TYPE
*r
)
2448 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2449 we force a QNaN, else we force an SNaN. The string, if not empty,
2450 is parsed as a number and placed in the significand. Return true
2451 if the string was successfully parsed. */
2454 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2460 get_canonical_qnan (r
, 0);
2462 get_canonical_snan (r
, 0);
2468 memset (r
, 0, sizeof (*r
));
2471 /* Parse akin to strtol into the significand of R. */
2473 while (ISSPACE (*str
))
2477 else if (*str
== '+')
2482 if (*str
== 'x' || *str
== 'X')
2491 while ((d
= hex_value (*str
)) < base
)
2498 lshift_significand (r
, r
, 3);
2501 lshift_significand (r
, r
, 4);
2504 lshift_significand_1 (&u
, r
);
2505 lshift_significand (r
, r
, 3);
2506 add_significands (r
, r
, &u
);
2514 add_significands (r
, r
, &u
);
2519 /* Must have consumed the entire string for success. */
2523 /* Shift the significand into place such that the bits
2524 are in the most significant bits for the format. */
2525 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2527 /* Our MSB is always unset for NaNs. */
2528 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2530 /* Force quiet or signalling NaN. */
2531 r
->signalling
= !quiet
;
2537 /* Fills R with the largest finite value representable in mode MODE.
2538 If SIGN is nonzero, R is set to the most negative finite value. */
2541 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
2543 const struct real_format
*fmt
;
2546 fmt
= REAL_MODE_FORMAT (mode
);
2548 memset (r
, 0, sizeof (*r
));
2551 decimal_real_maxval (r
, sign
, mode
);
2556 SET_REAL_EXP (r
, fmt
->emax
);
2558 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2559 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2560 clear_significand_below (r
, np2
);
2562 if (fmt
->pnan
< fmt
->p
)
2563 /* This is an IBM extended double format made up of two IEEE
2564 doubles. The value of the long double is the sum of the
2565 values of the two parts. The most significant part is
2566 required to be the value of the long double rounded to the
2567 nearest double. Rounding means we need a slightly smaller
2568 value for LDBL_MAX. */
2569 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2573 /* Fills R with 2**N. */
2576 real_2expN (REAL_VALUE_TYPE
*r
, int n
, format_helper fmt
)
2578 memset (r
, 0, sizeof (*r
));
2583 else if (n
< -MAX_EXP
)
2588 SET_REAL_EXP (r
, n
);
2589 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2591 if (fmt
.decimal_p ())
2592 decimal_real_convert (r
, fmt
, r
);
2597 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2601 bool round_up
= false;
2607 decimal_round_for_format (fmt
, r
);
2610 /* FIXME. We can come here via fp_easy_constant
2611 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2612 investigated whether this convert needs to be here, or
2613 something else is missing. */
2614 decimal_real_convert (r
, REAL_MODE_FORMAT (DFmode
), r
);
2618 emin2m1
= fmt
->emin
- 1;
2621 np2
= SIGNIFICAND_BITS
- p2
;
2625 get_zero (r
, r
->sign
);
2627 if (!fmt
->has_signed_zero
)
2632 get_inf (r
, r
->sign
);
2637 clear_significand_below (r
, np2
);
2647 /* Check the range of the exponent. If we're out of range,
2648 either underflow or overflow. */
2649 if (REAL_EXP (r
) > emax2
)
2651 else if (REAL_EXP (r
) <= emin2m1
)
2655 if (!fmt
->has_denorm
)
2657 /* Don't underflow completely until we've had a chance to round. */
2658 if (REAL_EXP (r
) < emin2m1
)
2663 diff
= emin2m1
- REAL_EXP (r
) + 1;
2667 /* De-normalize the significand. */
2668 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2669 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2673 if (!fmt
->round_towards_zero
)
2675 /* There are P2 true significand bits, followed by one guard bit,
2676 followed by one sticky bit, followed by stuff. Fold nonzero
2677 stuff into the sticky bit. */
2678 unsigned long sticky
;
2682 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2683 sticky
|= r
->sig
[i
];
2685 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2687 guard
= test_significand_bit (r
, np2
- 1);
2688 lsb
= test_significand_bit (r
, np2
);
2690 /* Round to even. */
2691 round_up
= guard
&& (sticky
|| lsb
);
2698 set_significand_bit (&u
, np2
);
2700 if (add_significands (r
, r
, &u
))
2702 /* Overflow. Means the significand had been all ones, and
2703 is now all zeros. Need to increase the exponent, and
2704 possibly re-normalize it. */
2705 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2706 if (REAL_EXP (r
) > emax2
)
2708 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2712 /* Catch underflow that we deferred until after rounding. */
2713 if (REAL_EXP (r
) <= emin2m1
)
2716 /* Clear out trailing garbage. */
2717 clear_significand_below (r
, np2
);
2720 /* Extend or truncate to a new format. */
2723 real_convert (REAL_VALUE_TYPE
*r
, format_helper fmt
,
2724 const REAL_VALUE_TYPE
*a
)
2728 if (a
->decimal
|| fmt
->b
== 10)
2729 decimal_real_convert (r
, fmt
, a
);
2731 round_for_format (fmt
, r
);
2733 /* round_for_format de-normalizes denormals. Undo just that part. */
2734 if (r
->cl
== rvc_normal
)
2738 /* Legacy. Likewise, except return the struct directly. */
2741 real_value_truncate (format_helper fmt
, REAL_VALUE_TYPE a
)
2744 real_convert (&r
, fmt
, &a
);
2748 /* Return true if truncating to FMT is exact. */
2751 exact_real_truncate (format_helper fmt
, const REAL_VALUE_TYPE
*a
)
2756 /* Don't allow conversion to denormals. */
2757 emin2m1
= fmt
->emin
- 1;
2758 if (REAL_EXP (a
) <= emin2m1
)
2761 /* After conversion to the new format, the value must be identical. */
2762 real_convert (&t
, fmt
, a
);
2763 return real_identical (&t
, a
);
2766 /* Write R to the given target format. Place the words of the result
2767 in target word order in BUF. There are always 32 bits in each
2768 long, no matter the size of the host long.
2770 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2773 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2780 round_for_format (fmt
, &r
);
2784 (*fmt
->encode
) (fmt
, buf
, &r
);
2789 /* Read R from the given target format. Read the words of the result
2790 in target word order in BUF. There are always 32 bits in each
2791 long, no matter the size of the host long. */
2794 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, format_helper fmt
)
2796 (*fmt
->decode
) (fmt
, r
, buf
);
2799 /* Return the number of bits of the largest binary value that the
2800 significand of FMT will hold. */
2801 /* ??? Legacy. Should get access to real_format directly. */
2804 significand_size (format_helper fmt
)
2811 /* Return the size in bits of the largest binary value that can be
2812 held by the decimal coefficient for this format. This is one more
2813 than the number of bits required to hold the largest coefficient
2815 double log2_10
= 3.3219281;
2816 return fmt
->p
* log2_10
;
2821 /* Return a hash value for the given real value. */
2822 /* ??? The "unsigned int" return value is intended to be hashval_t,
2823 but I didn't want to pull hashtab.h into real.h. */
2826 real_hash (const REAL_VALUE_TYPE
*r
)
2831 h
= r
->cl
| (r
->sign
<< 2);
2839 h
|= REAL_EXP (r
) << 3;
2844 h
^= (unsigned int)-1;
2853 if (sizeof (unsigned long) > sizeof (unsigned int))
2854 for (i
= 0; i
< SIGSZ
; ++i
)
2856 unsigned long s
= r
->sig
[i
];
2857 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2860 for (i
= 0; i
< SIGSZ
; ++i
)
2866 /* IEEE single-precision format. */
2868 static void encode_ieee_single (const struct real_format
*fmt
,
2869 long *, const REAL_VALUE_TYPE
*);
2870 static void decode_ieee_single (const struct real_format
*,
2871 REAL_VALUE_TYPE
*, const long *);
2874 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2875 const REAL_VALUE_TYPE
*r
)
2877 unsigned long image
, sig
, exp
;
2878 unsigned long sign
= r
->sign
;
2879 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2882 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2893 image
|= 0x7fffffff;
2900 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2901 if (r
->signalling
== fmt
->qnan_msb_set
)
2912 image
|= 0x7fffffff;
2916 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2917 whereas the intermediate representation is 0.F x 2**exp.
2918 Which means we're off by one. */
2922 exp
= REAL_EXP (r
) + 127 - 1;
2935 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2938 unsigned long image
= buf
[0] & 0xffffffff;
2939 bool sign
= (image
>> 31) & 1;
2940 int exp
= (image
>> 23) & 0xff;
2942 memset (r
, 0, sizeof (*r
));
2943 image
<<= HOST_BITS_PER_LONG
- 24;
2948 if (image
&& fmt
->has_denorm
)
2952 SET_REAL_EXP (r
, -126);
2953 r
->sig
[SIGSZ
-1] = image
<< 1;
2956 else if (fmt
->has_signed_zero
)
2959 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2965 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2966 ^ fmt
->qnan_msb_set
);
2967 r
->sig
[SIGSZ
-1] = image
;
2979 SET_REAL_EXP (r
, exp
- 127 + 1);
2980 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2984 const struct real_format ieee_single_format
=
3006 const struct real_format mips_single_format
=
3028 const struct real_format motorola_single_format
=
3050 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3051 single precision with the following differences:
3052 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3054 - NaNs are not supported.
3055 - The range of non-zero numbers in binary is
3056 (001)[1.]000...000 to (255)[1.]111...111.
3057 - Denormals can be represented, but are treated as +0.0 when
3058 used as an operand and are never generated as a result.
3059 - -0.0 can be represented, but a zero result is always +0.0.
3060 - the only supported rounding mode is trunction (towards zero). */
3061 const struct real_format spu_single_format
=
3083 /* IEEE double-precision format. */
3085 static void encode_ieee_double (const struct real_format
*fmt
,
3086 long *, const REAL_VALUE_TYPE
*);
3087 static void decode_ieee_double (const struct real_format
*,
3088 REAL_VALUE_TYPE
*, const long *);
3091 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3092 const REAL_VALUE_TYPE
*r
)
3094 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3095 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3097 image_hi
= r
->sign
<< 31;
3100 if (HOST_BITS_PER_LONG
== 64)
3102 sig_hi
= r
->sig
[SIGSZ
-1];
3103 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3104 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3108 sig_hi
= r
->sig
[SIGSZ
-1];
3109 sig_lo
= r
->sig
[SIGSZ
-2];
3110 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3111 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3121 image_hi
|= 2047 << 20;
3124 image_hi
|= 0x7fffffff;
3125 image_lo
= 0xffffffff;
3134 if (fmt
->canonical_nan_lsbs_set
)
3136 sig_hi
= (1 << 19) - 1;
3137 sig_lo
= 0xffffffff;
3145 if (r
->signalling
== fmt
->qnan_msb_set
)
3146 sig_hi
&= ~(1 << 19);
3149 if (sig_hi
== 0 && sig_lo
== 0)
3152 image_hi
|= 2047 << 20;
3158 image_hi
|= 0x7fffffff;
3159 image_lo
= 0xffffffff;
3164 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3165 whereas the intermediate representation is 0.F x 2**exp.
3166 Which means we're off by one. */
3170 exp
= REAL_EXP (r
) + 1023 - 1;
3171 image_hi
|= exp
<< 20;
3180 if (FLOAT_WORDS_BIG_ENDIAN
)
3181 buf
[0] = image_hi
, buf
[1] = image_lo
;
3183 buf
[0] = image_lo
, buf
[1] = image_hi
;
3187 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3190 unsigned long image_hi
, image_lo
;
3194 if (FLOAT_WORDS_BIG_ENDIAN
)
3195 image_hi
= buf
[0], image_lo
= buf
[1];
3197 image_lo
= buf
[0], image_hi
= buf
[1];
3198 image_lo
&= 0xffffffff;
3199 image_hi
&= 0xffffffff;
3201 sign
= (image_hi
>> 31) & 1;
3202 exp
= (image_hi
>> 20) & 0x7ff;
3204 memset (r
, 0, sizeof (*r
));
3206 image_hi
<<= 32 - 21;
3207 image_hi
|= image_lo
>> 21;
3208 image_hi
&= 0x7fffffff;
3209 image_lo
<<= 32 - 21;
3213 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3217 SET_REAL_EXP (r
, -1022);
3218 if (HOST_BITS_PER_LONG
== 32)
3220 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3222 r
->sig
[SIGSZ
-1] = image_hi
;
3223 r
->sig
[SIGSZ
-2] = image_lo
;
3227 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3228 r
->sig
[SIGSZ
-1] = image_hi
;
3232 else if (fmt
->has_signed_zero
)
3235 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3237 if (image_hi
|| image_lo
)
3241 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3242 if (HOST_BITS_PER_LONG
== 32)
3244 r
->sig
[SIGSZ
-1] = image_hi
;
3245 r
->sig
[SIGSZ
-2] = image_lo
;
3248 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3260 SET_REAL_EXP (r
, exp
- 1023 + 1);
3261 if (HOST_BITS_PER_LONG
== 32)
3263 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3264 r
->sig
[SIGSZ
-2] = image_lo
;
3267 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3271 const struct real_format ieee_double_format
=
3293 const struct real_format mips_double_format
=
3315 const struct real_format motorola_double_format
=
3337 /* IEEE extended real format. This comes in three flavors: Intel's as
3338 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3339 12- and 16-byte images may be big- or little endian; Motorola's is
3340 always big endian. */
3342 /* Helper subroutine which converts from the internal format to the
3343 12-byte little-endian Intel format. Functions below adjust this
3344 for the other possible formats. */
3346 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3347 const REAL_VALUE_TYPE
*r
)
3349 unsigned long image_hi
, sig_hi
, sig_lo
;
3350 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3352 image_hi
= r
->sign
<< 15;
3353 sig_hi
= sig_lo
= 0;
3365 /* Intel requires the explicit integer bit to be set, otherwise
3366 it considers the value a "pseudo-infinity". Motorola docs
3367 say it doesn't care. */
3368 sig_hi
= 0x80000000;
3373 sig_lo
= sig_hi
= 0xffffffff;
3383 if (fmt
->canonical_nan_lsbs_set
)
3385 sig_hi
= (1 << 30) - 1;
3386 sig_lo
= 0xffffffff;
3389 else if (HOST_BITS_PER_LONG
== 32)
3391 sig_hi
= r
->sig
[SIGSZ
-1];
3392 sig_lo
= r
->sig
[SIGSZ
-2];
3396 sig_lo
= r
->sig
[SIGSZ
-1];
3397 sig_hi
= sig_lo
>> 31 >> 1;
3398 sig_lo
&= 0xffffffff;
3400 if (r
->signalling
== fmt
->qnan_msb_set
)
3401 sig_hi
&= ~(1 << 30);
3404 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3407 /* Intel requires the explicit integer bit to be set, otherwise
3408 it considers the value a "pseudo-nan". Motorola docs say it
3410 sig_hi
|= 0x80000000;
3415 sig_lo
= sig_hi
= 0xffffffff;
3421 int exp
= REAL_EXP (r
);
3423 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3424 whereas the intermediate representation is 0.F x 2**exp.
3425 Which means we're off by one.
3427 Except for Motorola, which consider exp=0 and explicit
3428 integer bit set to continue to be normalized. In theory
3429 this discrepancy has been taken care of by the difference
3430 in fmt->emin in round_for_format. */
3437 gcc_assert (exp
>= 0);
3441 if (HOST_BITS_PER_LONG
== 32)
3443 sig_hi
= r
->sig
[SIGSZ
-1];
3444 sig_lo
= r
->sig
[SIGSZ
-2];
3448 sig_lo
= r
->sig
[SIGSZ
-1];
3449 sig_hi
= sig_lo
>> 31 >> 1;
3450 sig_lo
&= 0xffffffff;
3459 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3462 /* Convert from the internal format to the 12-byte Motorola format
3463 for an IEEE extended real. */
3465 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3466 const REAL_VALUE_TYPE
*r
)
3469 encode_ieee_extended (fmt
, intermed
, r
);
3471 if (r
->cl
== rvc_inf
)
3472 /* For infinity clear the explicit integer bit again, so that the
3473 format matches the canonical infinity generated by the FPU. */
3476 /* Motorola chips are assumed always to be big-endian. Also, the
3477 padding in a Motorola extended real goes between the exponent and
3478 the mantissa. At this point the mantissa is entirely within
3479 elements 0 and 1 of intermed, and the exponent entirely within
3480 element 2, so all we have to do is swap the order around, and
3481 shift element 2 left 16 bits. */
3482 buf
[0] = intermed
[2] << 16;
3483 buf
[1] = intermed
[1];
3484 buf
[2] = intermed
[0];
3487 /* Convert from the internal format to the 12-byte Intel format for
3488 an IEEE extended real. */
3490 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3491 const REAL_VALUE_TYPE
*r
)
3493 if (FLOAT_WORDS_BIG_ENDIAN
)
3495 /* All the padding in an Intel-format extended real goes at the high
3496 end, which in this case is after the mantissa, not the exponent.
3497 Therefore we must shift everything down 16 bits. */
3499 encode_ieee_extended (fmt
, intermed
, r
);
3500 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3501 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3502 buf
[2] = (intermed
[0] << 16);
3505 /* encode_ieee_extended produces what we want directly. */
3506 encode_ieee_extended (fmt
, buf
, r
);
3509 /* Convert from the internal format to the 16-byte Intel format for
3510 an IEEE extended real. */
3512 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3513 const REAL_VALUE_TYPE
*r
)
3515 /* All the padding in an Intel-format extended real goes at the high end. */
3516 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3520 /* As above, we have a helper function which converts from 12-byte
3521 little-endian Intel format to internal format. Functions below
3522 adjust for the other possible formats. */
3524 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3527 unsigned long image_hi
, sig_hi
, sig_lo
;
3531 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3532 sig_lo
&= 0xffffffff;
3533 sig_hi
&= 0xffffffff;
3534 image_hi
&= 0xffffffff;
3536 sign
= (image_hi
>> 15) & 1;
3537 exp
= image_hi
& 0x7fff;
3539 memset (r
, 0, sizeof (*r
));
3543 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3548 /* When the IEEE format contains a hidden bit, we know that
3549 it's zero at this point, and so shift up the significand
3550 and decrease the exponent to match. In this case, Motorola
3551 defines the explicit integer bit to be valid, so we don't
3552 know whether the msb is set or not. */
3553 SET_REAL_EXP (r
, fmt
->emin
);
3554 if (HOST_BITS_PER_LONG
== 32)
3556 r
->sig
[SIGSZ
-1] = sig_hi
;
3557 r
->sig
[SIGSZ
-2] = sig_lo
;
3560 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3564 else if (fmt
->has_signed_zero
)
3567 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3569 /* See above re "pseudo-infinities" and "pseudo-nans".
3570 Short summary is that the MSB will likely always be
3571 set, and that we don't care about it. */
3572 sig_hi
&= 0x7fffffff;
3574 if (sig_hi
|| sig_lo
)
3578 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
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
;
3597 SET_REAL_EXP (r
, exp
- 16383 + 1);
3598 if (HOST_BITS_PER_LONG
== 32)
3600 r
->sig
[SIGSZ
-1] = sig_hi
;
3601 r
->sig
[SIGSZ
-2] = sig_lo
;
3604 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3608 /* Convert from the internal format to the 12-byte Motorola format
3609 for an IEEE extended real. */
3611 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3616 /* Motorola chips are assumed always to be big-endian. Also, the
3617 padding in a Motorola extended real goes between the exponent and
3618 the mantissa; remove it. */
3619 intermed
[0] = buf
[2];
3620 intermed
[1] = buf
[1];
3621 intermed
[2] = (unsigned long)buf
[0] >> 16;
3623 decode_ieee_extended (fmt
, r
, intermed
);
3626 /* Convert from the internal format to the 12-byte Intel format for
3627 an IEEE extended real. */
3629 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3632 if (FLOAT_WORDS_BIG_ENDIAN
)
3634 /* All the padding in an Intel-format extended real goes at the high
3635 end, which in this case is after the mantissa, not the exponent.
3636 Therefore we must shift everything up 16 bits. */
3639 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3640 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3641 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3643 decode_ieee_extended (fmt
, r
, intermed
);
3646 /* decode_ieee_extended produces what we want directly. */
3647 decode_ieee_extended (fmt
, r
, buf
);
3650 /* Convert from the internal format to the 16-byte Intel format for
3651 an IEEE extended real. */
3653 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3656 /* All the padding in an Intel-format extended real goes at the high end. */
3657 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3660 const struct real_format ieee_extended_motorola_format
=
3662 encode_ieee_extended_motorola
,
3663 decode_ieee_extended_motorola
,
3679 "ieee_extended_motorola"
3682 const struct real_format ieee_extended_intel_96_format
=
3684 encode_ieee_extended_intel_96
,
3685 decode_ieee_extended_intel_96
,
3701 "ieee_extended_intel_96"
3704 const struct real_format ieee_extended_intel_128_format
=
3706 encode_ieee_extended_intel_128
,
3707 decode_ieee_extended_intel_128
,
3723 "ieee_extended_intel_128"
3726 /* The following caters to i386 systems that set the rounding precision
3727 to 53 bits instead of 64, e.g. FreeBSD. */
3728 const struct real_format ieee_extended_intel_96_round_53_format
=
3730 encode_ieee_extended_intel_96
,
3731 decode_ieee_extended_intel_96
,
3747 "ieee_extended_intel_96_round_53"
3750 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3751 numbers whose sum is equal to the extended precision value. The number
3752 with greater magnitude is first. This format has the same magnitude
3753 range as an IEEE double precision value, but effectively 106 bits of
3754 significand precision. Infinity and NaN are represented by their IEEE
3755 double precision value stored in the first number, the second number is
3756 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3758 static void encode_ibm_extended (const struct real_format
*fmt
,
3759 long *, const REAL_VALUE_TYPE
*);
3760 static void decode_ibm_extended (const struct real_format
*,
3761 REAL_VALUE_TYPE
*, const long *);
3764 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3765 const REAL_VALUE_TYPE
*r
)
3767 REAL_VALUE_TYPE u
, normr
, v
;
3768 const struct real_format
*base_fmt
;
3770 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3772 /* Renormalize R before doing any arithmetic on it. */
3774 if (normr
.cl
== rvc_normal
)
3777 /* u = IEEE double precision portion of significand. */
3779 round_for_format (base_fmt
, &u
);
3780 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3782 if (u
.cl
== rvc_normal
)
3784 do_add (&v
, &normr
, &u
, 1);
3785 /* Call round_for_format since we might need to denormalize. */
3786 round_for_format (base_fmt
, &v
);
3787 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3791 /* Inf, NaN, 0 are all representable as doubles, so the
3792 least-significant part can be 0.0. */
3799 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3802 REAL_VALUE_TYPE u
, v
;
3803 const struct real_format
*base_fmt
;
3805 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3806 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3808 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3810 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3811 do_add (r
, &u
, &v
, 0);
3817 const struct real_format ibm_extended_format
=
3819 encode_ibm_extended
,
3820 decode_ibm_extended
,
3839 const struct real_format mips_extended_format
=
3841 encode_ibm_extended
,
3842 decode_ibm_extended
,
3862 /* IEEE quad precision format. */
3864 static void encode_ieee_quad (const struct real_format
*fmt
,
3865 long *, const REAL_VALUE_TYPE
*);
3866 static void decode_ieee_quad (const struct real_format
*,
3867 REAL_VALUE_TYPE
*, const long *);
3870 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3871 const REAL_VALUE_TYPE
*r
)
3873 unsigned long image3
, image2
, image1
, image0
, exp
;
3874 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3877 image3
= r
->sign
<< 31;
3882 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3891 image3
|= 32767 << 16;
3894 image3
|= 0x7fffffff;
3895 image2
= 0xffffffff;
3896 image1
= 0xffffffff;
3897 image0
= 0xffffffff;
3904 image3
|= 32767 << 16;
3908 if (fmt
->canonical_nan_lsbs_set
)
3911 image2
= image1
= image0
= 0xffffffff;
3914 else if (HOST_BITS_PER_LONG
== 32)
3919 image3
|= u
.sig
[3] & 0xffff;
3924 image1
= image0
>> 31 >> 1;
3926 image3
|= (image2
>> 31 >> 1) & 0xffff;
3927 image0
&= 0xffffffff;
3928 image2
&= 0xffffffff;
3930 if (r
->signalling
== fmt
->qnan_msb_set
)
3934 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3939 image3
|= 0x7fffffff;
3940 image2
= 0xffffffff;
3941 image1
= 0xffffffff;
3942 image0
= 0xffffffff;
3947 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3948 whereas the intermediate representation is 0.F x 2**exp.
3949 Which means we're off by one. */
3953 exp
= REAL_EXP (r
) + 16383 - 1;
3954 image3
|= exp
<< 16;
3956 if (HOST_BITS_PER_LONG
== 32)
3961 image3
|= u
.sig
[3] & 0xffff;
3966 image1
= image0
>> 31 >> 1;
3968 image3
|= (image2
>> 31 >> 1) & 0xffff;
3969 image0
&= 0xffffffff;
3970 image2
&= 0xffffffff;
3978 if (FLOAT_WORDS_BIG_ENDIAN
)
3995 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3998 unsigned long image3
, image2
, image1
, image0
;
4002 if (FLOAT_WORDS_BIG_ENDIAN
)
4016 image0
&= 0xffffffff;
4017 image1
&= 0xffffffff;
4018 image2
&= 0xffffffff;
4020 sign
= (image3
>> 31) & 1;
4021 exp
= (image3
>> 16) & 0x7fff;
4024 memset (r
, 0, sizeof (*r
));
4028 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4033 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4034 if (HOST_BITS_PER_LONG
== 32)
4043 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4044 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4049 else if (fmt
->has_signed_zero
)
4052 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4054 if (image3
| image2
| image1
| image0
)
4058 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4060 if (HOST_BITS_PER_LONG
== 32)
4069 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4070 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4072 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4084 SET_REAL_EXP (r
, exp
- 16383 + 1);
4086 if (HOST_BITS_PER_LONG
== 32)
4095 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4096 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4098 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4099 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4103 const struct real_format ieee_quad_format
=
4125 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
=
4448 const struct real_format vax_d_format
=
4470 const struct real_format vax_g_format
=
4492 /* Encode real R into a single precision DFP value in BUF. */
4494 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4495 long *buf ATTRIBUTE_UNUSED
,
4496 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4498 encode_decimal32 (fmt
, buf
, r
);
4501 /* Decode a single precision DFP value in BUF into a real R. */
4503 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4504 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4505 const long *buf ATTRIBUTE_UNUSED
)
4507 decode_decimal32 (fmt
, r
, buf
);
4510 /* Encode real R into a double precision DFP value in BUF. */
4512 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4513 long *buf ATTRIBUTE_UNUSED
,
4514 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4516 encode_decimal64 (fmt
, buf
, r
);
4519 /* Decode a double precision DFP value in BUF into a real R. */
4521 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4522 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4523 const long *buf ATTRIBUTE_UNUSED
)
4525 decode_decimal64 (fmt
, r
, buf
);
4528 /* Encode real R into a quad precision DFP value in BUF. */
4530 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4531 long *buf ATTRIBUTE_UNUSED
,
4532 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4534 encode_decimal128 (fmt
, buf
, r
);
4537 /* Decode a quad precision DFP value in BUF into a real R. */
4539 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4540 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4541 const long *buf ATTRIBUTE_UNUSED
)
4543 decode_decimal128 (fmt
, r
, buf
);
4546 /* Single precision decimal floating point (IEEE 754). */
4547 const struct real_format decimal_single_format
=
4549 encode_decimal_single
,
4550 decode_decimal_single
,
4569 /* Double precision decimal floating point (IEEE 754). */
4570 const struct real_format decimal_double_format
=
4572 encode_decimal_double
,
4573 decode_decimal_double
,
4592 /* Quad precision decimal floating point (IEEE 754). */
4593 const struct real_format decimal_quad_format
=
4595 encode_decimal_quad
,
4596 decode_decimal_quad
,
4615 /* Encode half-precision floats. This routine is used both for the IEEE
4616 ARM alternative encodings. */
4618 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4619 const REAL_VALUE_TYPE
*r
)
4621 unsigned long image
, sig
, exp
;
4622 unsigned long sign
= r
->sign
;
4623 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4626 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4644 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4645 if (r
->signalling
== fmt
->qnan_msb_set
)
4660 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4661 whereas the intermediate representation is 0.F x 2**exp.
4662 Which means we're off by one. */
4666 exp
= REAL_EXP (r
) + 15 - 1;
4678 /* Decode half-precision floats. This routine is used both for the IEEE
4679 ARM alternative encodings. */
4681 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4684 unsigned long image
= buf
[0] & 0xffff;
4685 bool sign
= (image
>> 15) & 1;
4686 int exp
= (image
>> 10) & 0x1f;
4688 memset (r
, 0, sizeof (*r
));
4689 image
<<= HOST_BITS_PER_LONG
- 11;
4694 if (image
&& fmt
->has_denorm
)
4698 SET_REAL_EXP (r
, -14);
4699 r
->sig
[SIGSZ
-1] = image
<< 1;
4702 else if (fmt
->has_signed_zero
)
4705 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4711 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4712 ^ fmt
->qnan_msb_set
);
4713 r
->sig
[SIGSZ
-1] = image
;
4725 SET_REAL_EXP (r
, exp
- 15 + 1);
4726 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4730 /* Half-precision format, as specified in IEEE 754R. */
4731 const struct real_format ieee_half_format
=
4753 /* ARM's alternative half-precision format, similar to IEEE but with
4754 no reserved exponent value for NaNs and infinities; rather, it just
4755 extends the range of exponents by one. */
4756 const struct real_format arm_half_format
=
4778 /* A synthetic "format" for internal arithmetic. It's the size of the
4779 internal significand minus the two bits needed for proper rounding.
4780 The encode and decode routines exist only to satisfy our paranoia
4783 static void encode_internal (const struct real_format
*fmt
,
4784 long *, const REAL_VALUE_TYPE
*);
4785 static void decode_internal (const struct real_format
*,
4786 REAL_VALUE_TYPE
*, const long *);
4789 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4790 const REAL_VALUE_TYPE
*r
)
4792 memcpy (buf
, r
, sizeof (*r
));
4796 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4797 REAL_VALUE_TYPE
*r
, const long *buf
)
4799 memcpy (r
, buf
, sizeof (*r
));
4802 const struct real_format real_internal_format
=
4807 SIGNIFICAND_BITS
- 2,
4808 SIGNIFICAND_BITS
- 2,
4824 /* Calculate X raised to the integer exponent N in format FMT and store
4825 the result in R. Return true if the result may be inexact due to
4826 loss of precision. The algorithm is the classic "left-to-right binary
4827 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4828 Algorithms", "The Art of Computer Programming", Volume 2. */
4831 real_powi (REAL_VALUE_TYPE
*r
, format_helper fmt
,
4832 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4834 unsigned HOST_WIDE_INT bit
;
4836 bool inexact
= false;
4848 /* Don't worry about overflow, from now on n is unsigned. */
4856 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4857 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4861 inexact
|= do_multiply (&t
, &t
, &t
);
4863 inexact
|= do_multiply (&t
, &t
, x
);
4871 inexact
|= do_divide (&t
, &dconst1
, &t
);
4873 real_convert (r
, fmt
, &t
);
4877 /* Round X to the nearest integer not larger in absolute value, i.e.
4878 towards zero, placing the result in R in format FMT. */
4881 real_trunc (REAL_VALUE_TYPE
*r
, format_helper fmt
,
4882 const REAL_VALUE_TYPE
*x
)
4884 do_fix_trunc (r
, x
);
4886 real_convert (r
, fmt
, r
);
4889 /* Round X to the largest integer not greater in value, i.e. round
4890 down, placing the result in R in format FMT. */
4893 real_floor (REAL_VALUE_TYPE
*r
, format_helper fmt
,
4894 const REAL_VALUE_TYPE
*x
)
4898 do_fix_trunc (&t
, x
);
4899 if (! real_identical (&t
, x
) && x
->sign
)
4900 do_add (&t
, &t
, &dconstm1
, 0);
4902 real_convert (r
, fmt
, &t
);
4907 /* Round X to the smallest integer not less then argument, i.e. round
4908 up, placing the result in R in format FMT. */
4911 real_ceil (REAL_VALUE_TYPE
*r
, format_helper fmt
,
4912 const REAL_VALUE_TYPE
*x
)
4916 do_fix_trunc (&t
, x
);
4917 if (! real_identical (&t
, x
) && ! x
->sign
)
4918 do_add (&t
, &t
, &dconst1
, 0);
4920 real_convert (r
, fmt
, &t
);
4925 /* Round X to the nearest integer, but round halfway cases away from
4929 real_round (REAL_VALUE_TYPE
*r
, format_helper fmt
,
4930 const REAL_VALUE_TYPE
*x
)
4932 do_add (r
, x
, &dconsthalf
, x
->sign
);
4933 do_fix_trunc (r
, r
);
4935 real_convert (r
, fmt
, r
);
4938 /* Set the sign of R to the sign of X. */
4941 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4946 /* Check whether the real constant value given is an integer. */
4949 real_isinteger (const REAL_VALUE_TYPE
*c
, format_helper fmt
)
4951 REAL_VALUE_TYPE cint
;
4953 real_trunc (&cint
, fmt
, c
);
4954 return real_identical (c
, &cint
);
4957 /* Check whether C is an integer that fits in a HOST_WIDE_INT,
4958 storing it in *INT_OUT if so. */
4961 real_isinteger (const REAL_VALUE_TYPE
*c
, HOST_WIDE_INT
*int_out
)
4963 REAL_VALUE_TYPE cint
;
4965 HOST_WIDE_INT n
= real_to_integer (c
);
4966 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
4967 if (real_identical (c
, &cint
))
4975 /* Write into BUF the maximum representable finite floating-point
4976 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4977 float string. LEN is the size of BUF, and the buffer must be large
4978 enough to contain the resulting string. */
4981 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
4986 strcpy (buf
, "0x0.");
4988 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
4991 *p
++ = "08ce"[n
- i
];
4992 sprintf (p
, "p%d", fmt
->emax
);
4993 if (fmt
->pnan
< fmt
->p
)
4995 /* This is an IBM extended double format made up of two IEEE
4996 doubles. The value of the long double is the sum of the
4997 values of the two parts. The most significant part is
4998 required to be the value of the long double rounded to the
4999 nearest double. Rounding means we need a slightly smaller
5000 value for LDBL_MAX. */
5001 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
5004 gcc_assert (strlen (buf
) < len
);
5007 /* True if mode M has a NaN representation and
5008 the treatment of NaN operands is important. */
5011 HONOR_NANS (machine_mode m
)
5013 return MODE_HAS_NANS (m
) && !flag_finite_math_only
;
5017 HONOR_NANS (const_tree t
)
5019 return HONOR_NANS (element_mode (t
));
5023 HONOR_NANS (const_rtx x
)
5025 return HONOR_NANS (GET_MODE (x
));
5028 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5031 HONOR_SNANS (machine_mode m
)
5033 return flag_signaling_nans
&& HONOR_NANS (m
);
5037 HONOR_SNANS (const_tree t
)
5039 return HONOR_SNANS (element_mode (t
));
5043 HONOR_SNANS (const_rtx x
)
5045 return HONOR_SNANS (GET_MODE (x
));
5048 /* As for HONOR_NANS, but true if the mode can represent infinity and
5049 the treatment of infinite values is important. */
5052 HONOR_INFINITIES (machine_mode m
)
5054 return MODE_HAS_INFINITIES (m
) && !flag_finite_math_only
;
5058 HONOR_INFINITIES (const_tree t
)
5060 return HONOR_INFINITIES (element_mode (t
));
5064 HONOR_INFINITIES (const_rtx x
)
5066 return HONOR_INFINITIES (GET_MODE (x
));
5069 /* Like HONOR_NANS, but true if the given mode distinguishes between
5070 positive and negative zero, and the sign of zero is important. */
5073 HONOR_SIGNED_ZEROS (machine_mode m
)
5075 return MODE_HAS_SIGNED_ZEROS (m
) && flag_signed_zeros
;
5079 HONOR_SIGNED_ZEROS (const_tree t
)
5081 return HONOR_SIGNED_ZEROS (element_mode (t
));
5085 HONOR_SIGNED_ZEROS (const_rtx x
)
5087 return HONOR_SIGNED_ZEROS (GET_MODE (x
));
5090 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5091 and the rounding mode is important. */
5094 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m
)
5096 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m
) && flag_rounding_math
;
5100 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t
)
5102 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t
));
5106 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x
)
5108 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x
));