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"
30 #include "diagnostic-core.h"
37 /* The floating point model used internally is not exactly IEEE 754
38 compliant, and close to the description in the ISO C99 standard,
39 section 5.2.4.2.2 Characteristics of floating types.
43 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
47 b = base or radix, here always 2
49 p = precision (the number of base-b digits in the significand)
50 f_k = the digits of the significand.
52 We differ from typical IEEE 754 encodings in that the entire
53 significand is fractional. Normalized significands are in the
56 A requirement of the model is that P be larger than the largest
57 supported target floating-point type by at least 2 bits. This gives
58 us proper rounding when we truncate to the target type. In addition,
59 E must be large enough to hold the smallest supported denormal number
62 Both of these requirements are easily satisfied. The largest target
63 significand is 113 bits; we store at least 160. The smallest
64 denormal number fits in 17 exponent bits; we store 26. */
67 /* Used to classify two numbers simultaneously. */
68 #define CLASS2(A, B) ((A) << 2 | (B))
70 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
71 #error "Some constant folding done by hand to avoid shift count warnings"
74 static void get_zero (REAL_VALUE_TYPE
*, int);
75 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
76 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
77 static void get_inf (REAL_VALUE_TYPE
*, int);
78 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
79 const REAL_VALUE_TYPE
*, unsigned int);
80 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
82 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
84 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
85 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
86 const REAL_VALUE_TYPE
*);
87 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
88 const REAL_VALUE_TYPE
*, int);
89 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
90 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
91 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
92 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
93 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
94 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
95 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
96 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
97 const REAL_VALUE_TYPE
*);
98 static void normalize (REAL_VALUE_TYPE
*);
100 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
101 const REAL_VALUE_TYPE
*, int);
102 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
103 const REAL_VALUE_TYPE
*);
104 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
105 const REAL_VALUE_TYPE
*);
106 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
107 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
109 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, REAL_VALUE_TYPE
*);
110 static void decimal_from_integer (REAL_VALUE_TYPE
*);
111 static void decimal_integer_string (char *, const REAL_VALUE_TYPE
*,
114 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
115 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
116 static const REAL_VALUE_TYPE
* real_digit (int);
117 static void times_pten (REAL_VALUE_TYPE
*, int);
119 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
121 /* Initialize R with a positive zero. */
124 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
126 memset (r
, 0, sizeof (*r
));
130 /* Initialize R with the canonical quiet NaN. */
133 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
135 memset (r
, 0, sizeof (*r
));
142 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
144 memset (r
, 0, sizeof (*r
));
152 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
154 memset (r
, 0, sizeof (*r
));
160 /* Right-shift the significand of A by N bits; put the result in the
161 significand of R. If any one bits are shifted out, return true. */
164 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
167 unsigned long sticky
= 0;
168 unsigned int i
, ofs
= 0;
170 if (n
>= HOST_BITS_PER_LONG
)
172 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
174 n
&= HOST_BITS_PER_LONG
- 1;
179 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
180 for (i
= 0; i
< SIGSZ
; ++i
)
183 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
184 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
185 << (HOST_BITS_PER_LONG
- n
)));
190 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
191 r
->sig
[i
] = a
->sig
[ofs
+ i
];
192 for (; i
< SIGSZ
; ++i
)
199 /* Right-shift the significand of A by N bits; put the result in the
203 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
206 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
208 n
&= HOST_BITS_PER_LONG
- 1;
211 for (i
= 0; i
< SIGSZ
; ++i
)
214 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
215 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
216 << (HOST_BITS_PER_LONG
- n
)));
221 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
222 r
->sig
[i
] = a
->sig
[ofs
+ i
];
223 for (; i
< SIGSZ
; ++i
)
228 /* Left-shift the significand of A by N bits; put the result in the
232 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
235 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
237 n
&= HOST_BITS_PER_LONG
- 1;
240 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
241 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
242 for (; i
< SIGSZ
; ++i
)
243 r
->sig
[SIGSZ
-1-i
] = 0;
246 for (i
= 0; i
< SIGSZ
; ++i
)
249 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
250 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
251 >> (HOST_BITS_PER_LONG
- n
)));
255 /* Likewise, but N is specialized to 1. */
258 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
262 for (i
= SIGSZ
- 1; i
> 0; --i
)
263 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
264 r
->sig
[0] = a
->sig
[0] << 1;
267 /* Add the significands of A and B, placing the result in R. Return
268 true if there was carry out of the most significant word. */
271 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
272 const REAL_VALUE_TYPE
*b
)
277 for (i
= 0; i
< SIGSZ
; ++i
)
279 unsigned long ai
= a
->sig
[i
];
280 unsigned long ri
= ai
+ b
->sig
[i
];
296 /* Subtract the significands of A and B, placing the result in R. CARRY is
297 true if there's a borrow incoming to the least significant word.
298 Return true if there was borrow out of the most significant word. */
301 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
302 const REAL_VALUE_TYPE
*b
, int carry
)
306 for (i
= 0; i
< SIGSZ
; ++i
)
308 unsigned long ai
= a
->sig
[i
];
309 unsigned long ri
= ai
- b
->sig
[i
];
325 /* Negate the significand A, placing the result in R. */
328 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
333 for (i
= 0; i
< SIGSZ
; ++i
)
335 unsigned long ri
, ai
= a
->sig
[i
];
354 /* Compare significands. Return tri-state vs zero. */
357 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
361 for (i
= SIGSZ
- 1; i
>= 0; --i
)
363 unsigned long ai
= a
->sig
[i
];
364 unsigned long bi
= b
->sig
[i
];
375 /* Return true if A is nonzero. */
378 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
382 for (i
= SIGSZ
- 1; i
>= 0; --i
)
389 /* Set bit N of the significand of R. */
392 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
394 r
->sig
[n
/ HOST_BITS_PER_LONG
]
395 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
398 /* Clear bit N of the significand of R. */
401 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
403 r
->sig
[n
/ HOST_BITS_PER_LONG
]
404 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
407 /* Test bit N of the significand of R. */
410 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
412 /* ??? Compiler bug here if we return this expression directly.
413 The conversion to bool strips the "&1" and we wind up testing
414 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
415 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
419 /* Clear bits 0..N-1 of the significand of R. */
422 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
424 int i
, w
= n
/ HOST_BITS_PER_LONG
;
426 for (i
= 0; i
< w
; ++i
)
429 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
432 /* Divide the significands of A and B, placing the result in R. Return
433 true if the division was inexact. */
436 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
437 const REAL_VALUE_TYPE
*b
)
440 int i
, bit
= SIGNIFICAND_BITS
- 1;
441 unsigned long msb
, inexact
;
444 memset (r
->sig
, 0, sizeof (r
->sig
));
450 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
451 lshift_significand_1 (&u
, &u
);
453 if (msb
|| cmp_significands (&u
, b
) >= 0)
455 sub_significands (&u
, &u
, b
, 0);
456 set_significand_bit (r
, bit
);
461 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
467 /* Adjust the exponent and significand of R such that the most
468 significant bit is set. We underflow to zero and overflow to
469 infinity here, without denormals. (The intermediate representation
470 exponent is large enough to handle target denormals normalized.) */
473 normalize (REAL_VALUE_TYPE
*r
)
481 /* Find the first word that is nonzero. */
482 for (i
= SIGSZ
- 1; i
>= 0; i
--)
484 shift
+= HOST_BITS_PER_LONG
;
488 /* Zero significand flushes to zero. */
496 /* Find the first bit that is nonzero. */
498 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
504 exp
= REAL_EXP (r
) - shift
;
506 get_inf (r
, r
->sign
);
507 else if (exp
< -MAX_EXP
)
508 get_zero (r
, r
->sign
);
511 SET_REAL_EXP (r
, exp
);
512 lshift_significand (r
, r
, shift
);
517 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
518 result may be inexact due to a loss of precision. */
521 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
522 const REAL_VALUE_TYPE
*b
, int subtract_p
)
526 bool inexact
= false;
528 /* Determine if we need to add or subtract. */
530 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
532 switch (CLASS2 (a
->cl
, b
->cl
))
534 case CLASS2 (rvc_zero
, rvc_zero
):
535 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
536 get_zero (r
, sign
& !subtract_p
);
539 case CLASS2 (rvc_zero
, rvc_normal
):
540 case CLASS2 (rvc_zero
, rvc_inf
):
541 case CLASS2 (rvc_zero
, rvc_nan
):
543 case CLASS2 (rvc_normal
, rvc_nan
):
544 case CLASS2 (rvc_inf
, rvc_nan
):
545 case CLASS2 (rvc_nan
, rvc_nan
):
546 /* ANY + NaN = NaN. */
547 case CLASS2 (rvc_normal
, rvc_inf
):
550 r
->sign
= sign
^ subtract_p
;
553 case CLASS2 (rvc_normal
, rvc_zero
):
554 case CLASS2 (rvc_inf
, rvc_zero
):
555 case CLASS2 (rvc_nan
, rvc_zero
):
557 case CLASS2 (rvc_nan
, rvc_normal
):
558 case CLASS2 (rvc_nan
, rvc_inf
):
559 /* NaN + ANY = NaN. */
560 case CLASS2 (rvc_inf
, rvc_normal
):
565 case CLASS2 (rvc_inf
, rvc_inf
):
567 /* Inf - Inf = NaN. */
568 get_canonical_qnan (r
, 0);
570 /* Inf + Inf = Inf. */
574 case CLASS2 (rvc_normal
, rvc_normal
):
581 /* Swap the arguments such that A has the larger exponent. */
582 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
585 const REAL_VALUE_TYPE
*t
;
592 /* If the exponents are not identical, we need to shift the
593 significand of B down. */
596 /* If the exponents are too far apart, the significands
597 do not overlap, which makes the subtraction a noop. */
598 if (dexp
>= SIGNIFICAND_BITS
)
605 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
611 if (sub_significands (r
, a
, b
, inexact
))
613 /* We got a borrow out of the subtraction. That means that
614 A and B had the same exponent, and B had the larger
615 significand. We need to swap the sign and negate the
618 neg_significand (r
, r
);
623 if (add_significands (r
, a
, b
))
625 /* We got carry out of the addition. This means we need to
626 shift the significand back down one bit and increase the
628 inexact
|= sticky_rshift_significand (r
, r
, 1);
629 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
640 SET_REAL_EXP (r
, exp
);
641 /* Zero out the remaining fields. */
646 /* Re-normalize the result. */
649 /* Special case: if the subtraction results in zero, the result
651 if (r
->cl
== rvc_zero
)
654 r
->sig
[0] |= inexact
;
659 /* Calculate R = A * B. Return true if the result may be inexact. */
662 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
663 const REAL_VALUE_TYPE
*b
)
665 REAL_VALUE_TYPE u
, t
, *rr
;
666 unsigned int i
, j
, k
;
667 int sign
= a
->sign
^ b
->sign
;
668 bool inexact
= false;
670 switch (CLASS2 (a
->cl
, b
->cl
))
672 case CLASS2 (rvc_zero
, rvc_zero
):
673 case CLASS2 (rvc_zero
, rvc_normal
):
674 case CLASS2 (rvc_normal
, rvc_zero
):
675 /* +-0 * ANY = 0 with appropriate sign. */
679 case CLASS2 (rvc_zero
, rvc_nan
):
680 case CLASS2 (rvc_normal
, rvc_nan
):
681 case CLASS2 (rvc_inf
, rvc_nan
):
682 case CLASS2 (rvc_nan
, rvc_nan
):
683 /* ANY * NaN = NaN. */
688 case CLASS2 (rvc_nan
, rvc_zero
):
689 case CLASS2 (rvc_nan
, rvc_normal
):
690 case CLASS2 (rvc_nan
, rvc_inf
):
691 /* NaN * ANY = NaN. */
696 case CLASS2 (rvc_zero
, rvc_inf
):
697 case CLASS2 (rvc_inf
, rvc_zero
):
699 get_canonical_qnan (r
, sign
);
702 case CLASS2 (rvc_inf
, rvc_inf
):
703 case CLASS2 (rvc_normal
, rvc_inf
):
704 case CLASS2 (rvc_inf
, rvc_normal
):
705 /* Inf * Inf = Inf, R * Inf = Inf */
709 case CLASS2 (rvc_normal
, rvc_normal
):
716 if (r
== a
|| r
== b
)
722 /* Collect all the partial products. Since we don't have sure access
723 to a widening multiply, we split each long into two half-words.
725 Consider the long-hand form of a four half-word multiplication:
735 We construct partial products of the widened half-word products
736 that are known to not overlap, e.g. DF+DH. Each such partial
737 product is given its proper exponent, which allows us to sum them
738 and obtain the finished product. */
740 for (i
= 0; i
< SIGSZ
* 2; ++i
)
742 unsigned long ai
= a
->sig
[i
/ 2];
744 ai
>>= HOST_BITS_PER_LONG
/ 2;
746 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
751 for (j
= 0; j
< 2; ++j
)
753 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
754 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
763 /* Would underflow to zero, which we shouldn't bother adding. */
768 memset (&u
, 0, sizeof (u
));
770 SET_REAL_EXP (&u
, exp
);
772 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
774 unsigned long bi
= b
->sig
[k
/ 2];
776 bi
>>= HOST_BITS_PER_LONG
/ 2;
778 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
780 u
.sig
[k
/ 2] = ai
* bi
;
784 inexact
|= do_add (rr
, rr
, &u
, 0);
795 /* Calculate R = A / B. Return true if the result may be inexact. */
798 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
799 const REAL_VALUE_TYPE
*b
)
801 int exp
, sign
= a
->sign
^ b
->sign
;
802 REAL_VALUE_TYPE t
, *rr
;
805 switch (CLASS2 (a
->cl
, b
->cl
))
807 case CLASS2 (rvc_zero
, rvc_zero
):
809 case CLASS2 (rvc_inf
, rvc_inf
):
810 /* Inf / Inf = NaN. */
811 get_canonical_qnan (r
, sign
);
814 case CLASS2 (rvc_zero
, rvc_normal
):
815 case CLASS2 (rvc_zero
, rvc_inf
):
817 case CLASS2 (rvc_normal
, rvc_inf
):
822 case CLASS2 (rvc_normal
, rvc_zero
):
824 case CLASS2 (rvc_inf
, rvc_zero
):
829 case CLASS2 (rvc_zero
, rvc_nan
):
830 case CLASS2 (rvc_normal
, rvc_nan
):
831 case CLASS2 (rvc_inf
, rvc_nan
):
832 case CLASS2 (rvc_nan
, rvc_nan
):
833 /* ANY / NaN = NaN. */
838 case CLASS2 (rvc_nan
, rvc_zero
):
839 case CLASS2 (rvc_nan
, rvc_normal
):
840 case CLASS2 (rvc_nan
, rvc_inf
):
841 /* NaN / ANY = NaN. */
846 case CLASS2 (rvc_inf
, rvc_normal
):
851 case CLASS2 (rvc_normal
, rvc_normal
):
858 if (r
== a
|| r
== b
)
863 /* Make sure all fields in the result are initialized. */
868 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
879 SET_REAL_EXP (rr
, exp
);
881 inexact
= div_significands (rr
, a
, b
);
883 /* Re-normalize the result. */
885 rr
->sig
[0] |= inexact
;
893 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
894 one of the two operands is a NaN. */
897 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
902 switch (CLASS2 (a
->cl
, b
->cl
))
904 case CLASS2 (rvc_zero
, rvc_zero
):
905 /* Sign of zero doesn't matter for compares. */
908 case CLASS2 (rvc_normal
, rvc_zero
):
909 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
911 return decimal_do_compare (a
, b
, nan_result
);
913 case CLASS2 (rvc_inf
, rvc_zero
):
914 case CLASS2 (rvc_inf
, rvc_normal
):
915 return (a
->sign
? -1 : 1);
917 case CLASS2 (rvc_inf
, rvc_inf
):
918 return -a
->sign
- -b
->sign
;
920 case CLASS2 (rvc_zero
, rvc_normal
):
921 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
923 return decimal_do_compare (a
, b
, nan_result
);
925 case CLASS2 (rvc_zero
, rvc_inf
):
926 case CLASS2 (rvc_normal
, rvc_inf
):
927 return (b
->sign
? 1 : -1);
929 case CLASS2 (rvc_zero
, rvc_nan
):
930 case CLASS2 (rvc_normal
, rvc_nan
):
931 case CLASS2 (rvc_inf
, rvc_nan
):
932 case CLASS2 (rvc_nan
, rvc_nan
):
933 case CLASS2 (rvc_nan
, rvc_zero
):
934 case CLASS2 (rvc_nan
, rvc_normal
):
935 case CLASS2 (rvc_nan
, rvc_inf
):
938 case CLASS2 (rvc_normal
, rvc_normal
):
945 if (a
->sign
!= b
->sign
)
946 return -a
->sign
- -b
->sign
;
948 if (a
->decimal
|| b
->decimal
)
949 return decimal_do_compare (a
, b
, nan_result
);
951 if (REAL_EXP (a
) > REAL_EXP (b
))
953 else if (REAL_EXP (a
) < REAL_EXP (b
))
956 ret
= cmp_significands (a
, b
);
958 return (a
->sign
? -ret
: ret
);
961 /* Return A truncated to an integral value toward zero. */
964 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
978 decimal_do_fix_trunc (r
, a
);
981 if (REAL_EXP (r
) <= 0)
982 get_zero (r
, r
->sign
);
983 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
984 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
992 /* Perform the binary or unary operation described by CODE.
993 For a unary operation, leave OP1 NULL. This function returns
994 true if the result may be inexact due to loss of precision. */
997 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
998 const REAL_VALUE_TYPE
*op1
)
1000 enum tree_code code
= (enum tree_code
) icode
;
1002 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1003 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1008 /* Clear any padding areas in *r if it isn't equal to one of the
1009 operands so that we can later do bitwise comparisons later on. */
1010 if (r
!= op0
&& r
!= op1
)
1011 memset (r
, '\0', sizeof (*r
));
1012 return do_add (r
, op0
, op1
, 0);
1015 if (r
!= op0
&& r
!= op1
)
1016 memset (r
, '\0', sizeof (*r
));
1017 return do_add (r
, op0
, op1
, 1);
1020 if (r
!= op0
&& r
!= op1
)
1021 memset (r
, '\0', sizeof (*r
));
1022 return do_multiply (r
, op0
, op1
);
1025 if (r
!= op0
&& r
!= op1
)
1026 memset (r
, '\0', sizeof (*r
));
1027 return do_divide (r
, op0
, op1
);
1030 if (op1
->cl
== rvc_nan
)
1032 else if (do_compare (op0
, op1
, -1) < 0)
1039 if (op1
->cl
== rvc_nan
)
1041 else if (do_compare (op0
, op1
, 1) < 0)
1057 case FIX_TRUNC_EXPR
:
1058 do_fix_trunc (r
, op0
);
1068 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1071 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1076 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1079 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1084 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1085 const REAL_VALUE_TYPE
*op1
)
1087 enum tree_code code
= (enum tree_code
) icode
;
1092 return do_compare (op0
, op1
, 1) < 0;
1094 return do_compare (op0
, op1
, 1) <= 0;
1096 return do_compare (op0
, op1
, -1) > 0;
1098 return do_compare (op0
, op1
, -1) >= 0;
1100 return do_compare (op0
, op1
, -1) == 0;
1102 return do_compare (op0
, op1
, -1) != 0;
1103 case UNORDERED_EXPR
:
1104 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1106 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1108 return do_compare (op0
, op1
, -1) < 0;
1110 return do_compare (op0
, op1
, -1) <= 0;
1112 return do_compare (op0
, op1
, 1) > 0;
1114 return do_compare (op0
, op1
, 1) >= 0;
1116 return do_compare (op0
, op1
, 0) == 0;
1118 return do_compare (op0
, op1
, 0) != 0;
1125 /* Return floor log2(R). */
1128 real_exponent (const REAL_VALUE_TYPE
*r
)
1136 return (unsigned int)-1 >> 1;
1138 return REAL_EXP (r
);
1144 /* R = OP0 * 2**EXP. */
1147 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1158 exp
+= REAL_EXP (op0
);
1160 get_inf (r
, r
->sign
);
1161 else if (exp
< -MAX_EXP
)
1162 get_zero (r
, r
->sign
);
1164 SET_REAL_EXP (r
, exp
);
1172 /* Determine whether a floating-point value X is infinite. */
1175 real_isinf (const REAL_VALUE_TYPE
*r
)
1177 return (r
->cl
== rvc_inf
);
1180 /* Determine whether a floating-point value X is a NaN. */
1183 real_isnan (const REAL_VALUE_TYPE
*r
)
1185 return (r
->cl
== rvc_nan
);
1188 /* Determine whether a floating-point value X is finite. */
1191 real_isfinite (const REAL_VALUE_TYPE
*r
)
1193 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1196 /* Determine whether a floating-point value X is negative. */
1199 real_isneg (const REAL_VALUE_TYPE
*r
)
1204 /* Determine whether a floating-point value X is minus zero. */
1207 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1209 return r
->sign
&& r
->cl
== rvc_zero
;
1212 /* Compare two floating-point objects for bitwise identity. */
1215 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1221 if (a
->sign
!= b
->sign
)
1231 if (a
->decimal
!= b
->decimal
)
1233 if (REAL_EXP (a
) != REAL_EXP (b
))
1238 if (a
->signalling
!= b
->signalling
)
1240 /* The significand is ignored for canonical NaNs. */
1241 if (a
->canonical
|| b
->canonical
)
1242 return a
->canonical
== b
->canonical
;
1249 for (i
= 0; i
< SIGSZ
; ++i
)
1250 if (a
->sig
[i
] != b
->sig
[i
])
1256 /* Try to change R into its exact multiplicative inverse in machine
1257 mode MODE. Return true if successful. */
1260 exact_real_inverse (machine_mode mode
, REAL_VALUE_TYPE
*r
)
1262 const REAL_VALUE_TYPE
*one
= real_digit (1);
1266 if (r
->cl
!= rvc_normal
)
1269 /* Check for a power of two: all significand bits zero except the MSB. */
1270 for (i
= 0; i
< SIGSZ
-1; ++i
)
1273 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1276 /* Find the inverse and truncate to the required mode. */
1277 do_divide (&u
, one
, r
);
1278 real_convert (&u
, mode
, &u
);
1280 /* The rounding may have overflowed. */
1281 if (u
.cl
!= rvc_normal
)
1283 for (i
= 0; i
< SIGSZ
-1; ++i
)
1286 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1293 /* Return true if arithmetic on values in IMODE that were promoted
1294 from values in TMODE is equivalent to direct arithmetic on values
1298 real_can_shorten_arithmetic (machine_mode imode
, machine_mode tmode
)
1300 const struct real_format
*tfmt
, *ifmt
;
1301 tfmt
= REAL_MODE_FORMAT (tmode
);
1302 ifmt
= REAL_MODE_FORMAT (imode
);
1303 /* These conditions are conservative rather than trying to catch the
1304 exact boundary conditions; the main case to allow is IEEE float
1306 return (ifmt
->b
== tfmt
->b
1307 && ifmt
->p
> 2 * tfmt
->p
1308 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1309 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1310 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1311 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1312 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1313 && (ifmt
->has_sign_dependent_rounding
1314 == tfmt
->has_sign_dependent_rounding
)
1315 && ifmt
->has_nans
>= tfmt
->has_nans
1316 && ifmt
->has_inf
>= tfmt
->has_inf
1317 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1318 && !MODE_COMPOSITE_P (tmode
)
1319 && !MODE_COMPOSITE_P (imode
));
1322 /* Render R as an integer. */
1325 real_to_integer (const REAL_VALUE_TYPE
*r
)
1327 unsigned HOST_WIDE_INT i
;
1338 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1345 return decimal_real_to_integer (r
);
1347 if (REAL_EXP (r
) <= 0)
1349 /* Only force overflow for unsigned overflow. Signed overflow is
1350 undefined, so it doesn't matter what we return, and some callers
1351 expect to be able to use this routine for both signed and
1352 unsigned conversions. */
1353 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1356 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1357 i
= r
->sig
[SIGSZ
-1];
1360 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1361 i
= r
->sig
[SIGSZ
-1];
1362 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1363 i
|= r
->sig
[SIGSZ
-2];
1366 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1377 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1378 be represented in precision, *FAIL is set to TRUE. */
1381 real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
1383 HOST_WIDE_INT val
[2 * WIDE_INT_MAX_ELTS
];
1392 return wi::zero (precision
);
1400 return wi::set_bit_in_zero (precision
- 1, precision
);
1402 return ~wi::set_bit_in_zero (precision
- 1, precision
);
1406 return decimal_real_to_integer (r
, fail
, precision
);
1411 /* Only force overflow for unsigned overflow. Signed overflow is
1412 undefined, so it doesn't matter what we return, and some callers
1413 expect to be able to use this routine for both signed and
1414 unsigned conversions. */
1415 if (exp
> precision
)
1418 /* Put the significand into a wide_int that has precision W, which
1419 is the smallest HWI-multiple that has at least PRECISION bits.
1420 This ensures that the top bit of the significand is in the
1421 top bit of the wide_int. */
1422 words
= (precision
+ HOST_BITS_PER_WIDE_INT
- 1) / HOST_BITS_PER_WIDE_INT
;
1423 w
= words
* HOST_BITS_PER_WIDE_INT
;
1425 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1426 for (int i
= 0; i
< words
; i
++)
1428 int j
= SIGSZ
- words
+ i
;
1429 val
[i
] = (j
< 0) ? 0 : r
->sig
[j
];
1432 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1433 for (int i
= 0; i
< words
; i
++)
1435 int j
= SIGSZ
- (words
* 2) + (i
* 2);
1442 val
[i
] |= (unsigned HOST_WIDE_INT
) r
->sig
[j
] << HOST_BITS_PER_LONG
;
1445 /* Shift the value into place and truncate to the desired precision. */
1446 result
= wide_int::from_array (val
, words
, w
);
1447 result
= wi::lrshift (result
, w
- exp
);
1448 result
= wide_int::from (result
, precision
, UNSIGNED
);
1460 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1461 of NUM / DEN. Return the quotient and place the remainder in NUM.
1462 It is expected that NUM / DEN are close enough that the quotient is
1465 static unsigned long
1466 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1468 unsigned long q
, msb
;
1469 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1478 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1480 lshift_significand_1 (num
, num
);
1482 if (msb
|| cmp_significands (num
, den
) >= 0)
1484 sub_significands (num
, num
, den
, 0);
1488 while (--expn
>= expd
);
1490 SET_REAL_EXP (num
, expd
);
1496 /* Render R as a decimal floating point constant. Emit DIGITS significant
1497 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1498 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1499 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1500 to a string that, when parsed back in mode MODE, yields the same value. */
1502 #define M_LOG10_2 0.30102999566398119521
1505 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1506 size_t buf_size
, size_t digits
,
1507 int crop_trailing_zeros
, machine_mode mode
)
1509 const struct real_format
*fmt
= NULL
;
1510 const REAL_VALUE_TYPE
*one
, *ten
;
1511 REAL_VALUE_TYPE r
, pten
, u
, v
;
1512 int dec_exp
, cmp_one
, digit
;
1514 char *p
, *first
, *last
;
1518 if (mode
!= VOIDmode
)
1520 fmt
= REAL_MODE_FORMAT (mode
);
1528 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1533 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1536 /* ??? Print the significand as well, if not canonical? */
1537 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1538 (r_orig
->signalling
? 'S' : 'Q'));
1546 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1550 /* Bound the number of digits printed by the size of the representation. */
1551 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1552 if (digits
== 0 || digits
> max_digits
)
1553 digits
= max_digits
;
1555 /* Estimate the decimal exponent, and compute the length of the string it
1556 will print as. Be conservative and add one to account for possible
1557 overflow or rounding error. */
1558 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1559 for (max_digits
= 1; dec_exp
; max_digits
++)
1562 /* Bound the number of digits printed by the size of the output buffer. */
1563 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1564 gcc_assert (max_digits
<= buf_size
);
1565 if (digits
> max_digits
)
1566 digits
= max_digits
;
1568 one
= real_digit (1);
1569 ten
= ten_to_ptwo (0);
1577 cmp_one
= do_compare (&r
, one
, 0);
1582 /* Number is greater than one. Convert significand to an integer
1583 and strip trailing decimal zeros. */
1586 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1588 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1589 m
= floor_log2 (max_digits
);
1591 /* Iterate over the bits of the possible powers of 10 that might
1592 be present in U and eliminate them. That is, if we find that
1593 10**2**M divides U evenly, keep the division and increase
1599 do_divide (&t
, &u
, ten_to_ptwo (m
));
1600 do_fix_trunc (&v
, &t
);
1601 if (cmp_significands (&v
, &t
) == 0)
1609 /* Revert the scaling to integer that we performed earlier. */
1610 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1611 - (SIGNIFICAND_BITS
- 1));
1614 /* Find power of 10. Do this by dividing out 10**2**M when
1615 this is larger than the current remainder. Fill PTEN with
1616 the power of 10 that we compute. */
1617 if (REAL_EXP (&r
) > 0)
1619 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1622 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1623 if (do_compare (&u
, ptentwo
, 0) >= 0)
1625 do_divide (&u
, &u
, ptentwo
);
1626 do_multiply (&pten
, &pten
, ptentwo
);
1633 /* We managed to divide off enough tens in the above reduction
1634 loop that we've now got a negative exponent. Fall into the
1635 less-than-one code to compute the proper value for PTEN. */
1642 /* Number is less than one. Pad significand with leading
1648 /* Stop if we'd shift bits off the bottom. */
1652 do_multiply (&u
, &v
, ten
);
1654 /* Stop if we're now >= 1. */
1655 if (REAL_EXP (&u
) > 0)
1663 /* Find power of 10. Do this by multiplying in P=10**2**M when
1664 the current remainder is smaller than 1/P. Fill PTEN with the
1665 power of 10 that we compute. */
1666 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1669 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1670 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1672 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1674 do_multiply (&v
, &v
, ptentwo
);
1675 do_multiply (&pten
, &pten
, ptentwo
);
1681 /* Invert the positive power of 10 that we've collected so far. */
1682 do_divide (&pten
, one
, &pten
);
1690 /* At this point, PTEN should contain the nearest power of 10 smaller
1691 than R, such that this division produces the first digit.
1693 Using a divide-step primitive that returns the complete integral
1694 remainder avoids the rounding error that would be produced if
1695 we were to use do_divide here and then simply multiply by 10 for
1696 each subsequent digit. */
1698 digit
= rtd_divmod (&r
, &pten
);
1700 /* Be prepared for error in that division via underflow ... */
1701 if (digit
== 0 && cmp_significand_0 (&r
))
1703 /* Multiply by 10 and try again. */
1704 do_multiply (&r
, &r
, ten
);
1705 digit
= rtd_divmod (&r
, &pten
);
1707 gcc_assert (digit
!= 0);
1710 /* ... or overflow. */
1720 gcc_assert (digit
<= 10);
1724 /* Generate subsequent digits. */
1725 while (--digits
> 0)
1727 do_multiply (&r
, &r
, ten
);
1728 digit
= rtd_divmod (&r
, &pten
);
1733 /* Generate one more digit with which to do rounding. */
1734 do_multiply (&r
, &r
, ten
);
1735 digit
= rtd_divmod (&r
, &pten
);
1737 /* Round the result. */
1738 if (fmt
&& fmt
->round_towards_zero
)
1740 /* If the format uses round towards zero when parsing the string
1741 back in, we need to always round away from zero here. */
1742 if (cmp_significand_0 (&r
))
1744 round_up
= digit
> 0;
1750 /* Round to nearest. If R is nonzero there are additional
1751 nonzero digits to be extracted. */
1752 if (cmp_significand_0 (&r
))
1754 /* Round to even. */
1755 else if ((p
[-1] - '0') & 1)
1759 round_up
= digit
> 5;
1776 /* Carry out of the first digit. This means we had all 9's and
1777 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1785 /* Insert the decimal point. */
1786 first
[0] = first
[1];
1789 /* If requested, drop trailing zeros. Never crop past "1.0". */
1790 if (crop_trailing_zeros
)
1791 while (last
> first
+ 3 && last
[-1] == '0')
1794 /* Append the exponent. */
1795 sprintf (last
, "e%+d", dec_exp
);
1797 #ifdef ENABLE_CHECKING
1798 /* Verify that we can read the original value back in. */
1799 if (mode
!= VOIDmode
)
1801 real_from_string (&r
, str
);
1802 real_convert (&r
, mode
, &r
);
1803 gcc_assert (real_identical (&r
, r_orig
));
1808 /* Likewise, except always uses round-to-nearest. */
1811 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1812 size_t digits
, int crop_trailing_zeros
)
1814 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1815 digits
, crop_trailing_zeros
, VOIDmode
);
1818 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1819 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1820 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1821 strip trailing zeros. */
1824 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1825 size_t digits
, int crop_trailing_zeros
)
1827 int i
, j
, exp
= REAL_EXP (r
);
1840 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1843 /* ??? Print the significand as well, if not canonical? */
1844 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1845 (r
->signalling
? 'S' : 'Q'));
1853 /* Hexadecimal format for decimal floats is not interesting. */
1854 strcpy (str
, "N/A");
1859 digits
= SIGNIFICAND_BITS
/ 4;
1861 /* Bound the number of digits printed by the size of the output buffer. */
1863 sprintf (exp_buf
, "p%+d", exp
);
1864 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1865 gcc_assert (max_digits
<= buf_size
);
1866 if (digits
> max_digits
)
1867 digits
= max_digits
;
1878 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1879 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1881 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1887 if (crop_trailing_zeros
)
1888 while (p
> first
+ 1 && p
[-1] == '0')
1891 sprintf (p
, "p%+d", exp
);
1894 /* Initialize R from a decimal or hexadecimal string. The string is
1895 assumed to have been syntax checked already. Return -1 if the
1896 value underflows, +1 if overflows, and 0 otherwise. */
1899 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1911 else if (*str
== '+')
1914 if (!strncmp (str
, "QNaN", 4))
1916 get_canonical_qnan (r
, sign
);
1919 else if (!strncmp (str
, "SNaN", 4))
1921 get_canonical_snan (r
, sign
);
1924 else if (!strncmp (str
, "Inf", 3))
1930 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1932 /* Hexadecimal floating point. */
1933 int pos
= SIGNIFICAND_BITS
- 4, d
;
1941 d
= hex_value (*str
);
1946 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1947 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1951 /* Ensure correct rounding by setting last bit if there is
1952 a subsequent nonzero digit. */
1960 if (pos
== SIGNIFICAND_BITS
- 4)
1967 d
= hex_value (*str
);
1972 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1973 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1977 /* Ensure correct rounding by setting last bit if there is
1978 a subsequent nonzero digit. */
1984 /* If the mantissa is zero, ignore the exponent. */
1985 if (!cmp_significand_0 (r
))
1988 if (*str
== 'p' || *str
== 'P')
1990 bool exp_neg
= false;
1998 else if (*str
== '+')
2002 while (ISDIGIT (*str
))
2008 /* Overflowed the exponent. */
2023 SET_REAL_EXP (r
, exp
);
2029 /* Decimal floating point. */
2030 const char *cstr
= str
;
2034 while (*cstr
== '0')
2039 while (*cstr
== '0')
2043 /* If the mantissa is zero, ignore the exponent. */
2044 if (!ISDIGIT (*cstr
))
2047 /* Nonzero value, possibly overflowing or underflowing. */
2048 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2049 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, GMP_RNDZ
);
2050 /* The result should never be a NaN, and because the rounding is
2051 toward zero should never be an infinity. */
2052 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2053 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2058 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2065 real_from_mpfr (r
, m
, NULL_TREE
, GMP_RNDZ
);
2066 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2067 because the hex digits used in real_from_mpfr did not
2068 start with a digit 8 to f, but the exponent bounds above
2069 should have avoided underflow or overflow. */
2070 gcc_assert (r
->cl
== rvc_normal
);
2071 /* Set a sticky bit if mpfr_strtofr was inexact. */
2072 r
->sig
[0] |= inexact
;
2093 /* Legacy. Similar, but return the result directly. */
2096 real_from_string2 (const char *s
, machine_mode mode
)
2100 real_from_string (&r
, s
);
2101 if (mode
!= VOIDmode
)
2102 real_convert (&r
, mode
, &r
);
2107 /* Initialize R from string S and desired MODE. */
2110 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, machine_mode mode
)
2112 if (DECIMAL_FLOAT_MODE_P (mode
))
2113 decimal_real_from_string (r
, s
);
2115 real_from_string (r
, s
);
2117 if (mode
!= VOIDmode
)
2118 real_convert (r
, mode
, r
);
2121 /* Initialize R from the wide_int VAL_IN. The MODE is not VOIDmode,*/
2124 real_from_integer (REAL_VALUE_TYPE
*r
, machine_mode mode
,
2125 const wide_int_ref
&val_in
, signop sgn
)
2131 unsigned int len
= val_in
.get_precision ();
2133 int maxbitlen
= MAX_BITSIZE_MODE_ANY_INT
+ HOST_BITS_PER_WIDE_INT
;
2134 const unsigned int realmax
= (SIGNIFICAND_BITS
/ HOST_BITS_PER_WIDE_INT
2135 * HOST_BITS_PER_WIDE_INT
);
2137 memset (r
, 0, sizeof (*r
));
2139 r
->sign
= wi::neg_p (val_in
, sgn
);
2141 /* We have to ensure we can negate the largest negative number. */
2142 wide_int val
= wide_int::from (val_in
, maxbitlen
, sgn
);
2147 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2148 won't work with precisions that are not a multiple of
2149 HOST_BITS_PER_WIDE_INT. */
2150 len
+= HOST_BITS_PER_WIDE_INT
- 1;
2152 /* Ensure we can represent the largest negative number. */
2155 len
= len
/HOST_BITS_PER_WIDE_INT
* HOST_BITS_PER_WIDE_INT
;
2157 /* Cap the size to the size allowed by real.h. */
2160 HOST_WIDE_INT cnt_l_z
;
2161 cnt_l_z
= wi::clz (val
);
2163 if (maxbitlen
- cnt_l_z
> realmax
)
2165 e
= maxbitlen
- cnt_l_z
- realmax
;
2167 /* This value is too large, we must shift it right to
2168 preserve all the bits we can, and then bump the
2169 exponent up by that amount. */
2170 val
= wi::lrshift (val
, e
);
2175 /* Clear out top bits so elt will work with precisions that aren't
2176 a multiple of HOST_BITS_PER_WIDE_INT. */
2177 val
= wide_int::from (val
, len
, sgn
);
2178 len
= len
/ HOST_BITS_PER_WIDE_INT
;
2180 SET_REAL_EXP (r
, len
* HOST_BITS_PER_WIDE_INT
+ e
);
2183 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2184 for (i
= len
- 1; i
>= 0; i
--)
2186 r
->sig
[j
--] = val
.elt (i
);
2192 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2193 for (i
= len
- 1; i
>= 0; i
--)
2195 HOST_WIDE_INT e
= val
.elt (i
);
2196 r
->sig
[j
--] = e
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2208 if (DECIMAL_FLOAT_MODE_P (mode
))
2209 decimal_from_integer (r
);
2210 else if (mode
!= VOIDmode
)
2211 real_convert (r
, mode
, r
);
2214 /* Render R, an integral value, as a floating point constant with no
2215 specified exponent. */
2218 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2221 int dec_exp
, digit
, digits
;
2222 REAL_VALUE_TYPE r
, pten
;
2228 if (r
.cl
== rvc_zero
)
2237 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2238 digits
= dec_exp
+ 1;
2239 gcc_assert ((digits
+ 2) < (int)buf_size
);
2241 pten
= *real_digit (1);
2242 times_pten (&pten
, dec_exp
);
2248 digit
= rtd_divmod (&r
, &pten
);
2249 gcc_assert (digit
>= 0 && digit
<= 9);
2251 while (--digits
> 0)
2254 digit
= rtd_divmod (&r
, &pten
);
2261 /* Convert a real with an integral value to decimal float. */
2264 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2268 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2269 decimal_real_from_string (r
, str
);
2272 /* Returns 10**2**N. */
2274 static const REAL_VALUE_TYPE
*
2277 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2279 gcc_assert (n
>= 0);
2280 gcc_assert (n
< EXP_BITS
);
2282 if (tens
[n
].cl
== rvc_zero
)
2284 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2286 HOST_WIDE_INT t
= 10;
2289 for (i
= 0; i
< n
; ++i
)
2292 real_from_integer (&tens
[n
], VOIDmode
, t
, UNSIGNED
);
2296 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2297 do_multiply (&tens
[n
], t
, t
);
2304 /* Returns 10**(-2**N). */
2306 static const REAL_VALUE_TYPE
*
2307 ten_to_mptwo (int n
)
2309 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2311 gcc_assert (n
>= 0);
2312 gcc_assert (n
< EXP_BITS
);
2314 if (tens
[n
].cl
== rvc_zero
)
2315 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2322 static const REAL_VALUE_TYPE
*
2325 static REAL_VALUE_TYPE num
[10];
2327 gcc_assert (n
>= 0);
2328 gcc_assert (n
<= 9);
2330 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2331 real_from_integer (&num
[n
], VOIDmode
, n
, UNSIGNED
);
2336 /* Multiply R by 10**EXP. */
2339 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2341 REAL_VALUE_TYPE pten
, *rr
;
2342 bool negative
= (exp
< 0);
2348 pten
= *real_digit (1);
2354 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2356 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2359 do_divide (r
, r
, &pten
);
2362 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2364 const REAL_VALUE_TYPE
*
2367 static REAL_VALUE_TYPE value
;
2369 /* Initialize mathematical constants for constant folding builtins.
2370 These constants need to be given to at least 160 bits precision. */
2371 if (value
.cl
== rvc_zero
)
2374 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2375 mpfr_set_ui (m
, 1, GMP_RNDN
);
2376 mpfr_exp (m
, m
, GMP_RNDN
);
2377 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2384 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2386 const REAL_VALUE_TYPE
*
2387 dconst_third_ptr (void)
2389 static REAL_VALUE_TYPE value
;
2391 /* Initialize mathematical constants for constant folding builtins.
2392 These constants need to be given to at least 160 bits precision. */
2393 if (value
.cl
== rvc_zero
)
2395 real_arithmetic (&value
, RDIV_EXPR
, &dconst1
, real_digit (3));
2400 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2402 const REAL_VALUE_TYPE
*
2403 dconst_sqrt2_ptr (void)
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 precision. */
2409 if (value
.cl
== rvc_zero
)
2412 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2413 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2414 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2420 /* Fills R with +Inf. */
2423 real_inf (REAL_VALUE_TYPE
*r
)
2428 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2429 we force a QNaN, else we force an SNaN. The string, if not empty,
2430 is parsed as a number and placed in the significand. Return true
2431 if the string was successfully parsed. */
2434 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2437 const struct real_format
*fmt
;
2439 fmt
= REAL_MODE_FORMAT (mode
);
2445 get_canonical_qnan (r
, 0);
2447 get_canonical_snan (r
, 0);
2453 memset (r
, 0, sizeof (*r
));
2456 /* Parse akin to strtol into the significand of R. */
2458 while (ISSPACE (*str
))
2462 else if (*str
== '+')
2467 if (*str
== 'x' || *str
== 'X')
2476 while ((d
= hex_value (*str
)) < base
)
2483 lshift_significand (r
, r
, 3);
2486 lshift_significand (r
, r
, 4);
2489 lshift_significand_1 (&u
, r
);
2490 lshift_significand (r
, r
, 3);
2491 add_significands (r
, r
, &u
);
2499 add_significands (r
, r
, &u
);
2504 /* Must have consumed the entire string for success. */
2508 /* Shift the significand into place such that the bits
2509 are in the most significant bits for the format. */
2510 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2512 /* Our MSB is always unset for NaNs. */
2513 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2515 /* Force quiet or signalling NaN. */
2516 r
->signalling
= !quiet
;
2522 /* Fills R with the largest finite value representable in mode MODE.
2523 If SIGN is nonzero, R is set to the most negative finite value. */
2526 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
2528 const struct real_format
*fmt
;
2531 fmt
= REAL_MODE_FORMAT (mode
);
2533 memset (r
, 0, sizeof (*r
));
2536 decimal_real_maxval (r
, sign
, mode
);
2541 SET_REAL_EXP (r
, fmt
->emax
);
2543 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2544 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2545 clear_significand_below (r
, np2
);
2547 if (fmt
->pnan
< fmt
->p
)
2548 /* This is an IBM extended double format made up of two IEEE
2549 doubles. The value of the long double is the sum of the
2550 values of the two parts. The most significant part is
2551 required to be the value of the long double rounded to the
2552 nearest double. Rounding means we need a slightly smaller
2553 value for LDBL_MAX. */
2554 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2558 /* Fills R with 2**N. */
2561 real_2expN (REAL_VALUE_TYPE
*r
, int n
, machine_mode fmode
)
2563 memset (r
, 0, sizeof (*r
));
2568 else if (n
< -MAX_EXP
)
2573 SET_REAL_EXP (r
, n
);
2574 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2576 if (DECIMAL_FLOAT_MODE_P (fmode
))
2577 decimal_real_convert (r
, fmode
, r
);
2582 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2586 bool round_up
= false;
2592 decimal_round_for_format (fmt
, r
);
2595 /* FIXME. We can come here via fp_easy_constant
2596 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2597 investigated whether this convert needs to be here, or
2598 something else is missing. */
2599 decimal_real_convert (r
, DFmode
, r
);
2603 emin2m1
= fmt
->emin
- 1;
2606 np2
= SIGNIFICAND_BITS
- p2
;
2610 get_zero (r
, r
->sign
);
2612 if (!fmt
->has_signed_zero
)
2617 get_inf (r
, r
->sign
);
2622 clear_significand_below (r
, np2
);
2632 /* Check the range of the exponent. If we're out of range,
2633 either underflow or overflow. */
2634 if (REAL_EXP (r
) > emax2
)
2636 else if (REAL_EXP (r
) <= emin2m1
)
2640 if (!fmt
->has_denorm
)
2642 /* Don't underflow completely until we've had a chance to round. */
2643 if (REAL_EXP (r
) < emin2m1
)
2648 diff
= emin2m1
- REAL_EXP (r
) + 1;
2652 /* De-normalize the significand. */
2653 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2654 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2658 if (!fmt
->round_towards_zero
)
2660 /* There are P2 true significand bits, followed by one guard bit,
2661 followed by one sticky bit, followed by stuff. Fold nonzero
2662 stuff into the sticky bit. */
2663 unsigned long sticky
;
2667 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2668 sticky
|= r
->sig
[i
];
2670 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2672 guard
= test_significand_bit (r
, np2
- 1);
2673 lsb
= test_significand_bit (r
, np2
);
2675 /* Round to even. */
2676 round_up
= guard
&& (sticky
|| lsb
);
2683 set_significand_bit (&u
, np2
);
2685 if (add_significands (r
, r
, &u
))
2687 /* Overflow. Means the significand had been all ones, and
2688 is now all zeros. Need to increase the exponent, and
2689 possibly re-normalize it. */
2690 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2691 if (REAL_EXP (r
) > emax2
)
2693 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2697 /* Catch underflow that we deferred until after rounding. */
2698 if (REAL_EXP (r
) <= emin2m1
)
2701 /* Clear out trailing garbage. */
2702 clear_significand_below (r
, np2
);
2705 /* Extend or truncate to a new mode. */
2708 real_convert (REAL_VALUE_TYPE
*r
, machine_mode mode
,
2709 const REAL_VALUE_TYPE
*a
)
2711 const struct real_format
*fmt
;
2713 fmt
= REAL_MODE_FORMAT (mode
);
2718 if (a
->decimal
|| fmt
->b
== 10)
2719 decimal_real_convert (r
, mode
, a
);
2721 round_for_format (fmt
, r
);
2723 /* round_for_format de-normalizes denormals. Undo just that part. */
2724 if (r
->cl
== rvc_normal
)
2728 /* Legacy. Likewise, except return the struct directly. */
2731 real_value_truncate (machine_mode mode
, REAL_VALUE_TYPE a
)
2734 real_convert (&r
, mode
, &a
);
2738 /* Return true if truncating to MODE is exact. */
2741 exact_real_truncate (machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2743 const struct real_format
*fmt
;
2747 fmt
= REAL_MODE_FORMAT (mode
);
2750 /* Don't allow conversion to denormals. */
2751 emin2m1
= fmt
->emin
- 1;
2752 if (REAL_EXP (a
) <= emin2m1
)
2755 /* After conversion to the new mode, the value must be identical. */
2756 real_convert (&t
, mode
, a
);
2757 return real_identical (&t
, a
);
2760 /* Write R to the given target format. Place the words of the result
2761 in target word order in BUF. There are always 32 bits in each
2762 long, no matter the size of the host long.
2764 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2767 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2768 const struct real_format
*fmt
)
2774 round_for_format (fmt
, &r
);
2778 (*fmt
->encode
) (fmt
, buf
, &r
);
2783 /* Similar, but look up the format from MODE. */
2786 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, machine_mode mode
)
2788 const struct real_format
*fmt
;
2790 fmt
= REAL_MODE_FORMAT (mode
);
2793 return real_to_target_fmt (buf
, r
, fmt
);
2796 /* Read R from the given target format. Read the words of the result
2797 in target word order in BUF. There are always 32 bits in each
2798 long, no matter the size of the host long. */
2801 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2802 const struct real_format
*fmt
)
2804 (*fmt
->decode
) (fmt
, r
, buf
);
2807 /* Similar, but look up the format from MODE. */
2810 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, machine_mode mode
)
2812 const struct real_format
*fmt
;
2814 fmt
= REAL_MODE_FORMAT (mode
);
2817 (*fmt
->decode
) (fmt
, r
, buf
);
2820 /* Return the number of bits of the largest binary value that the
2821 significand of MODE will hold. */
2822 /* ??? Legacy. Should get access to real_format directly. */
2825 significand_size (machine_mode mode
)
2827 const struct real_format
*fmt
;
2829 fmt
= REAL_MODE_FORMAT (mode
);
2835 /* Return the size in bits of the largest binary value that can be
2836 held by the decimal coefficient for this mode. This is one more
2837 than the number of bits required to hold the largest coefficient
2839 double log2_10
= 3.3219281;
2840 return fmt
->p
* log2_10
;
2845 /* Return a hash value for the given real value. */
2846 /* ??? The "unsigned int" return value is intended to be hashval_t,
2847 but I didn't want to pull hashtab.h into real.h. */
2850 real_hash (const REAL_VALUE_TYPE
*r
)
2855 h
= r
->cl
| (r
->sign
<< 2);
2863 h
|= REAL_EXP (r
) << 3;
2868 h
^= (unsigned int)-1;
2877 if (sizeof (unsigned long) > sizeof (unsigned int))
2878 for (i
= 0; i
< SIGSZ
; ++i
)
2880 unsigned long s
= r
->sig
[i
];
2881 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2884 for (i
= 0; i
< SIGSZ
; ++i
)
2890 /* IEEE single-precision format. */
2892 static void encode_ieee_single (const struct real_format
*fmt
,
2893 long *, const REAL_VALUE_TYPE
*);
2894 static void decode_ieee_single (const struct real_format
*,
2895 REAL_VALUE_TYPE
*, const long *);
2898 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2899 const REAL_VALUE_TYPE
*r
)
2901 unsigned long image
, sig
, exp
;
2902 unsigned long sign
= r
->sign
;
2903 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2906 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2917 image
|= 0x7fffffff;
2924 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2925 if (r
->signalling
== fmt
->qnan_msb_set
)
2936 image
|= 0x7fffffff;
2940 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2941 whereas the intermediate representation is 0.F x 2**exp.
2942 Which means we're off by one. */
2946 exp
= REAL_EXP (r
) + 127 - 1;
2959 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2962 unsigned long image
= buf
[0] & 0xffffffff;
2963 bool sign
= (image
>> 31) & 1;
2964 int exp
= (image
>> 23) & 0xff;
2966 memset (r
, 0, sizeof (*r
));
2967 image
<<= HOST_BITS_PER_LONG
- 24;
2972 if (image
&& fmt
->has_denorm
)
2976 SET_REAL_EXP (r
, -126);
2977 r
->sig
[SIGSZ
-1] = image
<< 1;
2980 else if (fmt
->has_signed_zero
)
2983 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2989 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2990 ^ fmt
->qnan_msb_set
);
2991 r
->sig
[SIGSZ
-1] = image
;
3003 SET_REAL_EXP (r
, exp
- 127 + 1);
3004 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
3008 const struct real_format ieee_single_format
=
3030 const struct real_format mips_single_format
=
3052 const struct real_format motorola_single_format
=
3074 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3075 single precision with the following differences:
3076 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3078 - NaNs are not supported.
3079 - The range of non-zero numbers in binary is
3080 (001)[1.]000...000 to (255)[1.]111...111.
3081 - Denormals can be represented, but are treated as +0.0 when
3082 used as an operand and are never generated as a result.
3083 - -0.0 can be represented, but a zero result is always +0.0.
3084 - the only supported rounding mode is trunction (towards zero). */
3085 const struct real_format spu_single_format
=
3107 /* IEEE double-precision format. */
3109 static void encode_ieee_double (const struct real_format
*fmt
,
3110 long *, const REAL_VALUE_TYPE
*);
3111 static void decode_ieee_double (const struct real_format
*,
3112 REAL_VALUE_TYPE
*, const long *);
3115 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3116 const REAL_VALUE_TYPE
*r
)
3118 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3119 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3121 image_hi
= r
->sign
<< 31;
3124 if (HOST_BITS_PER_LONG
== 64)
3126 sig_hi
= r
->sig
[SIGSZ
-1];
3127 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3128 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3132 sig_hi
= r
->sig
[SIGSZ
-1];
3133 sig_lo
= r
->sig
[SIGSZ
-2];
3134 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3135 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3145 image_hi
|= 2047 << 20;
3148 image_hi
|= 0x7fffffff;
3149 image_lo
= 0xffffffff;
3158 if (fmt
->canonical_nan_lsbs_set
)
3160 sig_hi
= (1 << 19) - 1;
3161 sig_lo
= 0xffffffff;
3169 if (r
->signalling
== fmt
->qnan_msb_set
)
3170 sig_hi
&= ~(1 << 19);
3173 if (sig_hi
== 0 && sig_lo
== 0)
3176 image_hi
|= 2047 << 20;
3182 image_hi
|= 0x7fffffff;
3183 image_lo
= 0xffffffff;
3188 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3189 whereas the intermediate representation is 0.F x 2**exp.
3190 Which means we're off by one. */
3194 exp
= REAL_EXP (r
) + 1023 - 1;
3195 image_hi
|= exp
<< 20;
3204 if (FLOAT_WORDS_BIG_ENDIAN
)
3205 buf
[0] = image_hi
, buf
[1] = image_lo
;
3207 buf
[0] = image_lo
, buf
[1] = image_hi
;
3211 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3214 unsigned long image_hi
, image_lo
;
3218 if (FLOAT_WORDS_BIG_ENDIAN
)
3219 image_hi
= buf
[0], image_lo
= buf
[1];
3221 image_lo
= buf
[0], image_hi
= buf
[1];
3222 image_lo
&= 0xffffffff;
3223 image_hi
&= 0xffffffff;
3225 sign
= (image_hi
>> 31) & 1;
3226 exp
= (image_hi
>> 20) & 0x7ff;
3228 memset (r
, 0, sizeof (*r
));
3230 image_hi
<<= 32 - 21;
3231 image_hi
|= image_lo
>> 21;
3232 image_hi
&= 0x7fffffff;
3233 image_lo
<<= 32 - 21;
3237 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3241 SET_REAL_EXP (r
, -1022);
3242 if (HOST_BITS_PER_LONG
== 32)
3244 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3246 r
->sig
[SIGSZ
-1] = image_hi
;
3247 r
->sig
[SIGSZ
-2] = image_lo
;
3251 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3252 r
->sig
[SIGSZ
-1] = image_hi
;
3256 else if (fmt
->has_signed_zero
)
3259 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3261 if (image_hi
|| image_lo
)
3265 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3266 if (HOST_BITS_PER_LONG
== 32)
3268 r
->sig
[SIGSZ
-1] = image_hi
;
3269 r
->sig
[SIGSZ
-2] = image_lo
;
3272 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3284 SET_REAL_EXP (r
, exp
- 1023 + 1);
3285 if (HOST_BITS_PER_LONG
== 32)
3287 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3288 r
->sig
[SIGSZ
-2] = image_lo
;
3291 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3295 const struct real_format ieee_double_format
=
3317 const struct real_format mips_double_format
=
3339 const struct real_format motorola_double_format
=
3361 /* IEEE extended real format. This comes in three flavors: Intel's as
3362 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3363 12- and 16-byte images may be big- or little endian; Motorola's is
3364 always big endian. */
3366 /* Helper subroutine which converts from the internal format to the
3367 12-byte little-endian Intel format. Functions below adjust this
3368 for the other possible formats. */
3370 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3371 const REAL_VALUE_TYPE
*r
)
3373 unsigned long image_hi
, sig_hi
, sig_lo
;
3374 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3376 image_hi
= r
->sign
<< 15;
3377 sig_hi
= sig_lo
= 0;
3389 /* Intel requires the explicit integer bit to be set, otherwise
3390 it considers the value a "pseudo-infinity". Motorola docs
3391 say it doesn't care. */
3392 sig_hi
= 0x80000000;
3397 sig_lo
= sig_hi
= 0xffffffff;
3407 if (fmt
->canonical_nan_lsbs_set
)
3409 sig_hi
= (1 << 30) - 1;
3410 sig_lo
= 0xffffffff;
3413 else if (HOST_BITS_PER_LONG
== 32)
3415 sig_hi
= r
->sig
[SIGSZ
-1];
3416 sig_lo
= r
->sig
[SIGSZ
-2];
3420 sig_lo
= r
->sig
[SIGSZ
-1];
3421 sig_hi
= sig_lo
>> 31 >> 1;
3422 sig_lo
&= 0xffffffff;
3424 if (r
->signalling
== fmt
->qnan_msb_set
)
3425 sig_hi
&= ~(1 << 30);
3428 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3431 /* Intel requires the explicit integer bit to be set, otherwise
3432 it considers the value a "pseudo-nan". Motorola docs say it
3434 sig_hi
|= 0x80000000;
3439 sig_lo
= sig_hi
= 0xffffffff;
3445 int exp
= REAL_EXP (r
);
3447 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3448 whereas the intermediate representation is 0.F x 2**exp.
3449 Which means we're off by one.
3451 Except for Motorola, which consider exp=0 and explicit
3452 integer bit set to continue to be normalized. In theory
3453 this discrepancy has been taken care of by the difference
3454 in fmt->emin in round_for_format. */
3461 gcc_assert (exp
>= 0);
3465 if (HOST_BITS_PER_LONG
== 32)
3467 sig_hi
= r
->sig
[SIGSZ
-1];
3468 sig_lo
= r
->sig
[SIGSZ
-2];
3472 sig_lo
= r
->sig
[SIGSZ
-1];
3473 sig_hi
= sig_lo
>> 31 >> 1;
3474 sig_lo
&= 0xffffffff;
3483 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3486 /* Convert from the internal format to the 12-byte Motorola format
3487 for an IEEE extended real. */
3489 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3490 const REAL_VALUE_TYPE
*r
)
3493 encode_ieee_extended (fmt
, intermed
, r
);
3495 if (r
->cl
== rvc_inf
)
3496 /* For infinity clear the explicit integer bit again, so that the
3497 format matches the canonical infinity generated by the FPU. */
3500 /* Motorola chips are assumed always to be big-endian. Also, the
3501 padding in a Motorola extended real goes between the exponent and
3502 the mantissa. At this point the mantissa is entirely within
3503 elements 0 and 1 of intermed, and the exponent entirely within
3504 element 2, so all we have to do is swap the order around, and
3505 shift element 2 left 16 bits. */
3506 buf
[0] = intermed
[2] << 16;
3507 buf
[1] = intermed
[1];
3508 buf
[2] = intermed
[0];
3511 /* Convert from the internal format to the 12-byte Intel format for
3512 an IEEE extended real. */
3514 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3515 const REAL_VALUE_TYPE
*r
)
3517 if (FLOAT_WORDS_BIG_ENDIAN
)
3519 /* All the padding in an Intel-format extended real goes at the high
3520 end, which in this case is after the mantissa, not the exponent.
3521 Therefore we must shift everything down 16 bits. */
3523 encode_ieee_extended (fmt
, intermed
, r
);
3524 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3525 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3526 buf
[2] = (intermed
[0] << 16);
3529 /* encode_ieee_extended produces what we want directly. */
3530 encode_ieee_extended (fmt
, buf
, r
);
3533 /* Convert from the internal format to the 16-byte Intel format for
3534 an IEEE extended real. */
3536 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3537 const REAL_VALUE_TYPE
*r
)
3539 /* All the padding in an Intel-format extended real goes at the high end. */
3540 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3544 /* As above, we have a helper function which converts from 12-byte
3545 little-endian Intel format to internal format. Functions below
3546 adjust for the other possible formats. */
3548 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3551 unsigned long image_hi
, sig_hi
, sig_lo
;
3555 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3556 sig_lo
&= 0xffffffff;
3557 sig_hi
&= 0xffffffff;
3558 image_hi
&= 0xffffffff;
3560 sign
= (image_hi
>> 15) & 1;
3561 exp
= image_hi
& 0x7fff;
3563 memset (r
, 0, sizeof (*r
));
3567 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3572 /* When the IEEE format contains a hidden bit, we know that
3573 it's zero at this point, and so shift up the significand
3574 and decrease the exponent to match. In this case, Motorola
3575 defines the explicit integer bit to be valid, so we don't
3576 know whether the msb is set or not. */
3577 SET_REAL_EXP (r
, fmt
->emin
);
3578 if (HOST_BITS_PER_LONG
== 32)
3580 r
->sig
[SIGSZ
-1] = sig_hi
;
3581 r
->sig
[SIGSZ
-2] = sig_lo
;
3584 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3588 else if (fmt
->has_signed_zero
)
3591 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3593 /* See above re "pseudo-infinities" and "pseudo-nans".
3594 Short summary is that the MSB will likely always be
3595 set, and that we don't care about it. */
3596 sig_hi
&= 0x7fffffff;
3598 if (sig_hi
|| sig_lo
)
3602 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3603 if (HOST_BITS_PER_LONG
== 32)
3605 r
->sig
[SIGSZ
-1] = sig_hi
;
3606 r
->sig
[SIGSZ
-2] = sig_lo
;
3609 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3621 SET_REAL_EXP (r
, exp
- 16383 + 1);
3622 if (HOST_BITS_PER_LONG
== 32)
3624 r
->sig
[SIGSZ
-1] = sig_hi
;
3625 r
->sig
[SIGSZ
-2] = sig_lo
;
3628 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3632 /* Convert from the internal format to the 12-byte Motorola format
3633 for an IEEE extended real. */
3635 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3640 /* Motorola chips are assumed always to be big-endian. Also, the
3641 padding in a Motorola extended real goes between the exponent and
3642 the mantissa; remove it. */
3643 intermed
[0] = buf
[2];
3644 intermed
[1] = buf
[1];
3645 intermed
[2] = (unsigned long)buf
[0] >> 16;
3647 decode_ieee_extended (fmt
, r
, intermed
);
3650 /* Convert from the internal format to the 12-byte Intel format for
3651 an IEEE extended real. */
3653 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3656 if (FLOAT_WORDS_BIG_ENDIAN
)
3658 /* All the padding in an Intel-format extended real goes at the high
3659 end, which in this case is after the mantissa, not the exponent.
3660 Therefore we must shift everything up 16 bits. */
3663 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3664 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3665 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3667 decode_ieee_extended (fmt
, r
, intermed
);
3670 /* decode_ieee_extended produces what we want directly. */
3671 decode_ieee_extended (fmt
, r
, buf
);
3674 /* Convert from the internal format to the 16-byte Intel format for
3675 an IEEE extended real. */
3677 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3680 /* All the padding in an Intel-format extended real goes at the high end. */
3681 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3684 const struct real_format ieee_extended_motorola_format
=
3686 encode_ieee_extended_motorola
,
3687 decode_ieee_extended_motorola
,
3703 "ieee_extended_motorola"
3706 const struct real_format ieee_extended_intel_96_format
=
3708 encode_ieee_extended_intel_96
,
3709 decode_ieee_extended_intel_96
,
3725 "ieee_extended_intel_96"
3728 const struct real_format ieee_extended_intel_128_format
=
3730 encode_ieee_extended_intel_128
,
3731 decode_ieee_extended_intel_128
,
3747 "ieee_extended_intel_128"
3750 /* The following caters to i386 systems that set the rounding precision
3751 to 53 bits instead of 64, e.g. FreeBSD. */
3752 const struct real_format ieee_extended_intel_96_round_53_format
=
3754 encode_ieee_extended_intel_96
,
3755 decode_ieee_extended_intel_96
,
3771 "ieee_extended_intel_96_round_53"
3774 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3775 numbers whose sum is equal to the extended precision value. The number
3776 with greater magnitude is first. This format has the same magnitude
3777 range as an IEEE double precision value, but effectively 106 bits of
3778 significand precision. Infinity and NaN are represented by their IEEE
3779 double precision value stored in the first number, the second number is
3780 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3782 static void encode_ibm_extended (const struct real_format
*fmt
,
3783 long *, const REAL_VALUE_TYPE
*);
3784 static void decode_ibm_extended (const struct real_format
*,
3785 REAL_VALUE_TYPE
*, const long *);
3788 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3789 const REAL_VALUE_TYPE
*r
)
3791 REAL_VALUE_TYPE u
, normr
, v
;
3792 const struct real_format
*base_fmt
;
3794 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3796 /* Renormalize R before doing any arithmetic on it. */
3798 if (normr
.cl
== rvc_normal
)
3801 /* u = IEEE double precision portion of significand. */
3803 round_for_format (base_fmt
, &u
);
3804 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3806 if (u
.cl
== rvc_normal
)
3808 do_add (&v
, &normr
, &u
, 1);
3809 /* Call round_for_format since we might need to denormalize. */
3810 round_for_format (base_fmt
, &v
);
3811 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3815 /* Inf, NaN, 0 are all representable as doubles, so the
3816 least-significant part can be 0.0. */
3823 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3826 REAL_VALUE_TYPE u
, v
;
3827 const struct real_format
*base_fmt
;
3829 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3830 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3832 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3834 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3835 do_add (r
, &u
, &v
, 0);
3841 const struct real_format ibm_extended_format
=
3843 encode_ibm_extended
,
3844 decode_ibm_extended
,
3863 const struct real_format mips_extended_format
=
3865 encode_ibm_extended
,
3866 decode_ibm_extended
,
3886 /* IEEE quad precision format. */
3888 static void encode_ieee_quad (const struct real_format
*fmt
,
3889 long *, const REAL_VALUE_TYPE
*);
3890 static void decode_ieee_quad (const struct real_format
*,
3891 REAL_VALUE_TYPE
*, const long *);
3894 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3895 const REAL_VALUE_TYPE
*r
)
3897 unsigned long image3
, image2
, image1
, image0
, exp
;
3898 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3901 image3
= r
->sign
<< 31;
3906 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3915 image3
|= 32767 << 16;
3918 image3
|= 0x7fffffff;
3919 image2
= 0xffffffff;
3920 image1
= 0xffffffff;
3921 image0
= 0xffffffff;
3928 image3
|= 32767 << 16;
3932 if (fmt
->canonical_nan_lsbs_set
)
3935 image2
= image1
= image0
= 0xffffffff;
3938 else if (HOST_BITS_PER_LONG
== 32)
3943 image3
|= u
.sig
[3] & 0xffff;
3948 image1
= image0
>> 31 >> 1;
3950 image3
|= (image2
>> 31 >> 1) & 0xffff;
3951 image0
&= 0xffffffff;
3952 image2
&= 0xffffffff;
3954 if (r
->signalling
== fmt
->qnan_msb_set
)
3958 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3963 image3
|= 0x7fffffff;
3964 image2
= 0xffffffff;
3965 image1
= 0xffffffff;
3966 image0
= 0xffffffff;
3971 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3972 whereas the intermediate representation is 0.F x 2**exp.
3973 Which means we're off by one. */
3977 exp
= REAL_EXP (r
) + 16383 - 1;
3978 image3
|= exp
<< 16;
3980 if (HOST_BITS_PER_LONG
== 32)
3985 image3
|= u
.sig
[3] & 0xffff;
3990 image1
= image0
>> 31 >> 1;
3992 image3
|= (image2
>> 31 >> 1) & 0xffff;
3993 image0
&= 0xffffffff;
3994 image2
&= 0xffffffff;
4002 if (FLOAT_WORDS_BIG_ENDIAN
)
4019 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4022 unsigned long image3
, image2
, image1
, image0
;
4026 if (FLOAT_WORDS_BIG_ENDIAN
)
4040 image0
&= 0xffffffff;
4041 image1
&= 0xffffffff;
4042 image2
&= 0xffffffff;
4044 sign
= (image3
>> 31) & 1;
4045 exp
= (image3
>> 16) & 0x7fff;
4048 memset (r
, 0, sizeof (*r
));
4052 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4057 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4058 if (HOST_BITS_PER_LONG
== 32)
4067 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4068 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4073 else if (fmt
->has_signed_zero
)
4076 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4078 if (image3
| image2
| image1
| image0
)
4082 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4084 if (HOST_BITS_PER_LONG
== 32)
4093 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4094 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4096 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4108 SET_REAL_EXP (r
, exp
- 16383 + 1);
4110 if (HOST_BITS_PER_LONG
== 32)
4119 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4120 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4122 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4123 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4127 const struct real_format ieee_quad_format
=
4149 const struct real_format mips_quad_format
=
4171 /* Descriptions of VAX floating point formats can be found beginning at
4173 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4175 The thing to remember is that they're almost IEEE, except for word
4176 order, exponent bias, and the lack of infinities, nans, and denormals.
4178 We don't implement the H_floating format here, simply because neither
4179 the VAX or Alpha ports use it. */
4181 static void encode_vax_f (const struct real_format
*fmt
,
4182 long *, const REAL_VALUE_TYPE
*);
4183 static void decode_vax_f (const struct real_format
*,
4184 REAL_VALUE_TYPE
*, const long *);
4185 static void encode_vax_d (const struct real_format
*fmt
,
4186 long *, const REAL_VALUE_TYPE
*);
4187 static void decode_vax_d (const struct real_format
*,
4188 REAL_VALUE_TYPE
*, const long *);
4189 static void encode_vax_g (const struct real_format
*fmt
,
4190 long *, const REAL_VALUE_TYPE
*);
4191 static void decode_vax_g (const struct real_format
*,
4192 REAL_VALUE_TYPE
*, const long *);
4195 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4196 const REAL_VALUE_TYPE
*r
)
4198 unsigned long sign
, exp
, sig
, image
;
4200 sign
= r
->sign
<< 15;
4210 image
= 0xffff7fff | sign
;
4214 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4215 exp
= REAL_EXP (r
) + 128;
4217 image
= (sig
<< 16) & 0xffff0000;
4231 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4232 REAL_VALUE_TYPE
*r
, const long *buf
)
4234 unsigned long image
= buf
[0] & 0xffffffff;
4235 int exp
= (image
>> 7) & 0xff;
4237 memset (r
, 0, sizeof (*r
));
4242 r
->sign
= (image
>> 15) & 1;
4243 SET_REAL_EXP (r
, exp
- 128);
4245 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4246 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4251 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4252 const REAL_VALUE_TYPE
*r
)
4254 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4259 image0
= image1
= 0;
4264 image0
= 0xffff7fff | sign
;
4265 image1
= 0xffffffff;
4269 /* Extract the significand into straight hi:lo. */
4270 if (HOST_BITS_PER_LONG
== 64)
4272 image0
= r
->sig
[SIGSZ
-1];
4273 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4274 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4278 image0
= r
->sig
[SIGSZ
-1];
4279 image1
= r
->sig
[SIGSZ
-2];
4280 image1
= (image0
<< 24) | (image1
>> 8);
4281 image0
= (image0
>> 8) & 0xffffff;
4284 /* Rearrange the half-words of the significand to match the
4286 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4287 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4289 /* Add the sign and exponent. */
4291 image0
|= (REAL_EXP (r
) + 128) << 7;
4298 if (FLOAT_WORDS_BIG_ENDIAN
)
4299 buf
[0] = image1
, buf
[1] = image0
;
4301 buf
[0] = image0
, buf
[1] = image1
;
4305 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4306 REAL_VALUE_TYPE
*r
, const long *buf
)
4308 unsigned long image0
, image1
;
4311 if (FLOAT_WORDS_BIG_ENDIAN
)
4312 image1
= buf
[0], image0
= buf
[1];
4314 image0
= buf
[0], image1
= buf
[1];
4315 image0
&= 0xffffffff;
4316 image1
&= 0xffffffff;
4318 exp
= (image0
>> 7) & 0xff;
4320 memset (r
, 0, sizeof (*r
));
4325 r
->sign
= (image0
>> 15) & 1;
4326 SET_REAL_EXP (r
, exp
- 128);
4328 /* Rearrange the half-words of the external format into
4329 proper ascending order. */
4330 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4331 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4333 if (HOST_BITS_PER_LONG
== 64)
4335 image0
= (image0
<< 31 << 1) | image1
;
4338 r
->sig
[SIGSZ
-1] = image0
;
4342 r
->sig
[SIGSZ
-1] = image0
;
4343 r
->sig
[SIGSZ
-2] = image1
;
4344 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4345 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4351 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4352 const REAL_VALUE_TYPE
*r
)
4354 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4359 image0
= image1
= 0;
4364 image0
= 0xffff7fff | sign
;
4365 image1
= 0xffffffff;
4369 /* Extract the significand into straight hi:lo. */
4370 if (HOST_BITS_PER_LONG
== 64)
4372 image0
= r
->sig
[SIGSZ
-1];
4373 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4374 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4378 image0
= r
->sig
[SIGSZ
-1];
4379 image1
= r
->sig
[SIGSZ
-2];
4380 image1
= (image0
<< 21) | (image1
>> 11);
4381 image0
= (image0
>> 11) & 0xfffff;
4384 /* Rearrange the half-words of the significand to match the
4386 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4387 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4389 /* Add the sign and exponent. */
4391 image0
|= (REAL_EXP (r
) + 1024) << 4;
4398 if (FLOAT_WORDS_BIG_ENDIAN
)
4399 buf
[0] = image1
, buf
[1] = image0
;
4401 buf
[0] = image0
, buf
[1] = image1
;
4405 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4406 REAL_VALUE_TYPE
*r
, const long *buf
)
4408 unsigned long image0
, image1
;
4411 if (FLOAT_WORDS_BIG_ENDIAN
)
4412 image1
= buf
[0], image0
= buf
[1];
4414 image0
= buf
[0], image1
= buf
[1];
4415 image0
&= 0xffffffff;
4416 image1
&= 0xffffffff;
4418 exp
= (image0
>> 4) & 0x7ff;
4420 memset (r
, 0, sizeof (*r
));
4425 r
->sign
= (image0
>> 15) & 1;
4426 SET_REAL_EXP (r
, exp
- 1024);
4428 /* Rearrange the half-words of the external format into
4429 proper ascending order. */
4430 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4431 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4433 if (HOST_BITS_PER_LONG
== 64)
4435 image0
= (image0
<< 31 << 1) | image1
;
4438 r
->sig
[SIGSZ
-1] = image0
;
4442 r
->sig
[SIGSZ
-1] = image0
;
4443 r
->sig
[SIGSZ
-2] = image1
;
4444 lshift_significand (r
, r
, 64 - 53);
4445 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4450 const struct real_format vax_f_format
=
4472 const struct real_format vax_d_format
=
4494 const struct real_format vax_g_format
=
4516 /* Encode real R into a single precision DFP value in BUF. */
4518 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4519 long *buf ATTRIBUTE_UNUSED
,
4520 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4522 encode_decimal32 (fmt
, buf
, r
);
4525 /* Decode a single precision DFP value in BUF into a real R. */
4527 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4528 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4529 const long *buf ATTRIBUTE_UNUSED
)
4531 decode_decimal32 (fmt
, r
, buf
);
4534 /* Encode real R into a double precision DFP value in BUF. */
4536 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4537 long *buf ATTRIBUTE_UNUSED
,
4538 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4540 encode_decimal64 (fmt
, buf
, r
);
4543 /* Decode a double precision DFP value in BUF into a real R. */
4545 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4546 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4547 const long *buf ATTRIBUTE_UNUSED
)
4549 decode_decimal64 (fmt
, r
, buf
);
4552 /* Encode real R into a quad precision DFP value in BUF. */
4554 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4555 long *buf ATTRIBUTE_UNUSED
,
4556 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4558 encode_decimal128 (fmt
, buf
, r
);
4561 /* Decode a quad precision DFP value in BUF into a real R. */
4563 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4564 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4565 const long *buf ATTRIBUTE_UNUSED
)
4567 decode_decimal128 (fmt
, r
, buf
);
4570 /* Single precision decimal floating point (IEEE 754). */
4571 const struct real_format decimal_single_format
=
4573 encode_decimal_single
,
4574 decode_decimal_single
,
4593 /* Double precision decimal floating point (IEEE 754). */
4594 const struct real_format decimal_double_format
=
4596 encode_decimal_double
,
4597 decode_decimal_double
,
4616 /* Quad precision decimal floating point (IEEE 754). */
4617 const struct real_format decimal_quad_format
=
4619 encode_decimal_quad
,
4620 decode_decimal_quad
,
4639 /* Encode half-precision floats. This routine is used both for the IEEE
4640 ARM alternative encodings. */
4642 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4643 const REAL_VALUE_TYPE
*r
)
4645 unsigned long image
, sig
, exp
;
4646 unsigned long sign
= r
->sign
;
4647 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4650 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4668 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4669 if (r
->signalling
== fmt
->qnan_msb_set
)
4684 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4685 whereas the intermediate representation is 0.F x 2**exp.
4686 Which means we're off by one. */
4690 exp
= REAL_EXP (r
) + 15 - 1;
4702 /* Decode half-precision floats. This routine is used both for the IEEE
4703 ARM alternative encodings. */
4705 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4708 unsigned long image
= buf
[0] & 0xffff;
4709 bool sign
= (image
>> 15) & 1;
4710 int exp
= (image
>> 10) & 0x1f;
4712 memset (r
, 0, sizeof (*r
));
4713 image
<<= HOST_BITS_PER_LONG
- 11;
4718 if (image
&& fmt
->has_denorm
)
4722 SET_REAL_EXP (r
, -14);
4723 r
->sig
[SIGSZ
-1] = image
<< 1;
4726 else if (fmt
->has_signed_zero
)
4729 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4735 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4736 ^ fmt
->qnan_msb_set
);
4737 r
->sig
[SIGSZ
-1] = image
;
4749 SET_REAL_EXP (r
, exp
- 15 + 1);
4750 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4754 /* Half-precision format, as specified in IEEE 754R. */
4755 const struct real_format ieee_half_format
=
4777 /* ARM's alternative half-precision format, similar to IEEE but with
4778 no reserved exponent value for NaNs and infinities; rather, it just
4779 extends the range of exponents by one. */
4780 const struct real_format arm_half_format
=
4802 /* A synthetic "format" for internal arithmetic. It's the size of the
4803 internal significand minus the two bits needed for proper rounding.
4804 The encode and decode routines exist only to satisfy our paranoia
4807 static void encode_internal (const struct real_format
*fmt
,
4808 long *, const REAL_VALUE_TYPE
*);
4809 static void decode_internal (const struct real_format
*,
4810 REAL_VALUE_TYPE
*, const long *);
4813 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4814 const REAL_VALUE_TYPE
*r
)
4816 memcpy (buf
, r
, sizeof (*r
));
4820 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4821 REAL_VALUE_TYPE
*r
, const long *buf
)
4823 memcpy (r
, buf
, sizeof (*r
));
4826 const struct real_format real_internal_format
=
4831 SIGNIFICAND_BITS
- 2,
4832 SIGNIFICAND_BITS
- 2,
4848 /* Calculate X raised to the integer exponent N in mode MODE and store
4849 the result in R. Return true if the result may be inexact due to
4850 loss of precision. The algorithm is the classic "left-to-right binary
4851 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4852 Algorithms", "The Art of Computer Programming", Volume 2. */
4855 real_powi (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4856 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4858 unsigned HOST_WIDE_INT bit
;
4860 bool inexact
= false;
4872 /* Don't worry about overflow, from now on n is unsigned. */
4880 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4881 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4885 inexact
|= do_multiply (&t
, &t
, &t
);
4887 inexact
|= do_multiply (&t
, &t
, x
);
4895 inexact
|= do_divide (&t
, &dconst1
, &t
);
4897 real_convert (r
, mode
, &t
);
4901 /* Round X to the nearest integer not larger in absolute value, i.e.
4902 towards zero, placing the result in R in mode MODE. */
4905 real_trunc (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4906 const REAL_VALUE_TYPE
*x
)
4908 do_fix_trunc (r
, x
);
4909 if (mode
!= VOIDmode
)
4910 real_convert (r
, mode
, r
);
4913 /* Round X to the largest integer not greater in value, i.e. round
4914 down, placing the result in R in mode MODE. */
4917 real_floor (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4918 const REAL_VALUE_TYPE
*x
)
4922 do_fix_trunc (&t
, x
);
4923 if (! real_identical (&t
, x
) && x
->sign
)
4924 do_add (&t
, &t
, &dconstm1
, 0);
4925 if (mode
!= VOIDmode
)
4926 real_convert (r
, mode
, &t
);
4931 /* Round X to the smallest integer not less then argument, i.e. round
4932 up, placing the result in R in mode MODE. */
4935 real_ceil (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4936 const REAL_VALUE_TYPE
*x
)
4940 do_fix_trunc (&t
, x
);
4941 if (! real_identical (&t
, x
) && ! x
->sign
)
4942 do_add (&t
, &t
, &dconst1
, 0);
4943 if (mode
!= VOIDmode
)
4944 real_convert (r
, mode
, &t
);
4949 /* Round X to the nearest integer, but round halfway cases away from
4953 real_round (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4954 const REAL_VALUE_TYPE
*x
)
4956 do_add (r
, x
, &dconsthalf
, x
->sign
);
4957 do_fix_trunc (r
, r
);
4958 if (mode
!= VOIDmode
)
4959 real_convert (r
, mode
, r
);
4962 /* Set the sign of R to the sign of X. */
4965 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4970 /* Check whether the real constant value given is an integer. */
4973 real_isinteger (const REAL_VALUE_TYPE
*c
, machine_mode mode
)
4975 REAL_VALUE_TYPE cint
;
4977 real_trunc (&cint
, mode
, c
);
4978 return real_identical (c
, &cint
);
4981 /* Write into BUF the maximum representable finite floating-point
4982 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4983 float string. LEN is the size of BUF, and the buffer must be large
4984 enough to contain the resulting string. */
4987 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
4992 strcpy (buf
, "0x0.");
4994 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
4997 *p
++ = "08ce"[n
- i
];
4998 sprintf (p
, "p%d", fmt
->emax
);
4999 if (fmt
->pnan
< fmt
->p
)
5001 /* This is an IBM extended double format made up of two IEEE
5002 doubles. The value of the long double is the sum of the
5003 values of the two parts. The most significant part is
5004 required to be the value of the long double rounded to the
5005 nearest double. Rounding means we need a slightly smaller
5006 value for LDBL_MAX. */
5007 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
5010 gcc_assert (strlen (buf
) < len
);
5013 /* True if mode M has a NaN representation and
5014 the treatment of NaN operands is important. */
5017 HONOR_NANS (machine_mode m
)
5019 return MODE_HAS_NANS (m
) && !flag_finite_math_only
;
5023 HONOR_NANS (const_tree t
)
5025 return HONOR_NANS (element_mode (t
));
5029 HONOR_NANS (const_rtx x
)
5031 return HONOR_NANS (GET_MODE (x
));
5034 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5037 HONOR_SNANS (machine_mode m
)
5039 return flag_signaling_nans
&& HONOR_NANS (m
);
5043 HONOR_SNANS (const_tree t
)
5045 return HONOR_SNANS (element_mode (t
));
5049 HONOR_SNANS (const_rtx x
)
5051 return HONOR_SNANS (GET_MODE (x
));
5054 /* As for HONOR_NANS, but true if the mode can represent infinity and
5055 the treatment of infinite values is important. */
5058 HONOR_INFINITIES (machine_mode m
)
5060 return MODE_HAS_INFINITIES (m
) && !flag_finite_math_only
;
5064 HONOR_INFINITIES (const_tree t
)
5066 return HONOR_INFINITIES (element_mode (t
));
5070 HONOR_INFINITIES (const_rtx x
)
5072 return HONOR_INFINITIES (GET_MODE (x
));
5075 /* Like HONOR_NANS, but true if the given mode distinguishes between
5076 positive and negative zero, and the sign of zero is important. */
5079 HONOR_SIGNED_ZEROS (machine_mode m
)
5081 return MODE_HAS_SIGNED_ZEROS (m
) && flag_signed_zeros
;
5085 HONOR_SIGNED_ZEROS (const_tree t
)
5087 return HONOR_SIGNED_ZEROS (element_mode (t
));
5091 HONOR_SIGNED_ZEROS (const_rtx x
)
5093 return HONOR_SIGNED_ZEROS (GET_MODE (x
));
5096 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5097 and the rounding mode is important. */
5100 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m
)
5102 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m
) && flag_rounding_math
;
5106 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t
)
5108 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t
));
5112 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x
)
5114 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x
));