1 /* real.c - software floating point emulation.
2 Copyright (C) 1993-2014 Free Software Foundation, Inc.
3 Contributed by Stephen L. Moshier (moshier@world.std.com).
4 Re-written by Richard Henderson <rth@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
27 #include "diagnostic-core.h"
36 /* The floating point model used internally is not exactly IEEE 754
37 compliant, and close to the description in the ISO C99 standard,
38 section 5.2.4.2.2 Characteristics of floating types.
42 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
46 b = base or radix, here always 2
48 p = precision (the number of base-b digits in the significand)
49 f_k = the digits of the significand.
51 We differ from typical IEEE 754 encodings in that the entire
52 significand is fractional. Normalized significands are in the
55 A requirement of the model is that P be larger than the largest
56 supported target floating-point type by at least 2 bits. This gives
57 us proper rounding when we truncate to the target type. In addition,
58 E must be large enough to hold the smallest supported denormal number
61 Both of these requirements are easily satisfied. The largest target
62 significand is 113 bits; we store at least 160. The smallest
63 denormal number fits in 17 exponent bits; we store 26. */
66 /* Used to classify two numbers simultaneously. */
67 #define CLASS2(A, B) ((A) << 2 | (B))
69 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
70 #error "Some constant folding done by hand to avoid shift count warnings"
73 static void get_zero (REAL_VALUE_TYPE
*, int);
74 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
75 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
76 static void get_inf (REAL_VALUE_TYPE
*, int);
77 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
78 const REAL_VALUE_TYPE
*, unsigned int);
79 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
81 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
83 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
84 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
85 const REAL_VALUE_TYPE
*);
86 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
87 const REAL_VALUE_TYPE
*, int);
88 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
89 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
90 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
91 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
92 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
93 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
94 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
95 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
96 const REAL_VALUE_TYPE
*);
97 static void normalize (REAL_VALUE_TYPE
*);
99 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
100 const REAL_VALUE_TYPE
*, int);
101 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
102 const REAL_VALUE_TYPE
*);
103 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
104 const REAL_VALUE_TYPE
*);
105 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
106 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
108 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, REAL_VALUE_TYPE
*);
109 static void decimal_from_integer (REAL_VALUE_TYPE
*);
110 static void decimal_integer_string (char *, const REAL_VALUE_TYPE
*,
113 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
114 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
115 static const REAL_VALUE_TYPE
* real_digit (int);
116 static void times_pten (REAL_VALUE_TYPE
*, int);
118 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
120 /* Initialize R with a positive zero. */
123 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
125 memset (r
, 0, sizeof (*r
));
129 /* Initialize R with the canonical quiet NaN. */
132 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
134 memset (r
, 0, sizeof (*r
));
141 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
143 memset (r
, 0, sizeof (*r
));
151 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
153 memset (r
, 0, sizeof (*r
));
159 /* Right-shift the significand of A by N bits; put the result in the
160 significand of R. If any one bits are shifted out, return true. */
163 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
166 unsigned long sticky
= 0;
167 unsigned int i
, ofs
= 0;
169 if (n
>= HOST_BITS_PER_LONG
)
171 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
173 n
&= HOST_BITS_PER_LONG
- 1;
178 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
179 for (i
= 0; i
< SIGSZ
; ++i
)
182 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
183 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
184 << (HOST_BITS_PER_LONG
- n
)));
189 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
190 r
->sig
[i
] = a
->sig
[ofs
+ i
];
191 for (; i
< SIGSZ
; ++i
)
198 /* Right-shift the significand of A by N bits; put the result in the
202 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
205 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
207 n
&= HOST_BITS_PER_LONG
- 1;
210 for (i
= 0; i
< SIGSZ
; ++i
)
213 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
214 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
215 << (HOST_BITS_PER_LONG
- n
)));
220 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
221 r
->sig
[i
] = a
->sig
[ofs
+ i
];
222 for (; i
< SIGSZ
; ++i
)
227 /* Left-shift the significand of A by N bits; put the result in the
231 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
234 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
236 n
&= HOST_BITS_PER_LONG
- 1;
239 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
240 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
241 for (; i
< SIGSZ
; ++i
)
242 r
->sig
[SIGSZ
-1-i
] = 0;
245 for (i
= 0; i
< SIGSZ
; ++i
)
248 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
249 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
250 >> (HOST_BITS_PER_LONG
- n
)));
254 /* Likewise, but N is specialized to 1. */
257 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
261 for (i
= SIGSZ
- 1; i
> 0; --i
)
262 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
263 r
->sig
[0] = a
->sig
[0] << 1;
266 /* Add the significands of A and B, placing the result in R. Return
267 true if there was carry out of the most significant word. */
270 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
271 const REAL_VALUE_TYPE
*b
)
276 for (i
= 0; i
< SIGSZ
; ++i
)
278 unsigned long ai
= a
->sig
[i
];
279 unsigned long ri
= ai
+ b
->sig
[i
];
295 /* Subtract the significands of A and B, placing the result in R. CARRY is
296 true if there's a borrow incoming to the least significant word.
297 Return true if there was borrow out of the most significant word. */
300 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
301 const REAL_VALUE_TYPE
*b
, int carry
)
305 for (i
= 0; i
< SIGSZ
; ++i
)
307 unsigned long ai
= a
->sig
[i
];
308 unsigned long ri
= ai
- b
->sig
[i
];
324 /* Negate the significand A, placing the result in R. */
327 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
332 for (i
= 0; i
< SIGSZ
; ++i
)
334 unsigned long ri
, ai
= a
->sig
[i
];
353 /* Compare significands. Return tri-state vs zero. */
356 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
360 for (i
= SIGSZ
- 1; i
>= 0; --i
)
362 unsigned long ai
= a
->sig
[i
];
363 unsigned long bi
= b
->sig
[i
];
374 /* Return true if A is nonzero. */
377 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
381 for (i
= SIGSZ
- 1; i
>= 0; --i
)
388 /* Set bit N of the significand of R. */
391 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
393 r
->sig
[n
/ HOST_BITS_PER_LONG
]
394 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
397 /* Clear bit N of the significand of R. */
400 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
402 r
->sig
[n
/ HOST_BITS_PER_LONG
]
403 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
406 /* Test bit N of the significand of R. */
409 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
411 /* ??? Compiler bug here if we return this expression directly.
412 The conversion to bool strips the "&1" and we wind up testing
413 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
414 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
418 /* Clear bits 0..N-1 of the significand of R. */
421 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
423 int i
, w
= n
/ HOST_BITS_PER_LONG
;
425 for (i
= 0; i
< w
; ++i
)
428 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
431 /* Divide the significands of A and B, placing the result in R. Return
432 true if the division was inexact. */
435 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
436 const REAL_VALUE_TYPE
*b
)
439 int i
, bit
= SIGNIFICAND_BITS
- 1;
440 unsigned long msb
, inexact
;
443 memset (r
->sig
, 0, sizeof (r
->sig
));
449 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
450 lshift_significand_1 (&u
, &u
);
452 if (msb
|| cmp_significands (&u
, b
) >= 0)
454 sub_significands (&u
, &u
, b
, 0);
455 set_significand_bit (r
, bit
);
460 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
466 /* Adjust the exponent and significand of R such that the most
467 significant bit is set. We underflow to zero and overflow to
468 infinity here, without denormals. (The intermediate representation
469 exponent is large enough to handle target denormals normalized.) */
472 normalize (REAL_VALUE_TYPE
*r
)
480 /* Find the first word that is nonzero. */
481 for (i
= SIGSZ
- 1; i
>= 0; i
--)
483 shift
+= HOST_BITS_PER_LONG
;
487 /* Zero significand flushes to zero. */
495 /* Find the first bit that is nonzero. */
497 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
503 exp
= REAL_EXP (r
) - shift
;
505 get_inf (r
, r
->sign
);
506 else if (exp
< -MAX_EXP
)
507 get_zero (r
, r
->sign
);
510 SET_REAL_EXP (r
, exp
);
511 lshift_significand (r
, r
, shift
);
516 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
517 result may be inexact due to a loss of precision. */
520 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
521 const REAL_VALUE_TYPE
*b
, int subtract_p
)
525 bool inexact
= false;
527 /* Determine if we need to add or subtract. */
529 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
531 switch (CLASS2 (a
->cl
, b
->cl
))
533 case CLASS2 (rvc_zero
, rvc_zero
):
534 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
535 get_zero (r
, sign
& !subtract_p
);
538 case CLASS2 (rvc_zero
, rvc_normal
):
539 case CLASS2 (rvc_zero
, rvc_inf
):
540 case CLASS2 (rvc_zero
, rvc_nan
):
542 case CLASS2 (rvc_normal
, rvc_nan
):
543 case CLASS2 (rvc_inf
, rvc_nan
):
544 case CLASS2 (rvc_nan
, rvc_nan
):
545 /* ANY + NaN = NaN. */
546 case CLASS2 (rvc_normal
, rvc_inf
):
549 r
->sign
= sign
^ subtract_p
;
552 case CLASS2 (rvc_normal
, rvc_zero
):
553 case CLASS2 (rvc_inf
, rvc_zero
):
554 case CLASS2 (rvc_nan
, rvc_zero
):
556 case CLASS2 (rvc_nan
, rvc_normal
):
557 case CLASS2 (rvc_nan
, rvc_inf
):
558 /* NaN + ANY = NaN. */
559 case CLASS2 (rvc_inf
, rvc_normal
):
564 case CLASS2 (rvc_inf
, rvc_inf
):
566 /* Inf - Inf = NaN. */
567 get_canonical_qnan (r
, 0);
569 /* Inf + Inf = Inf. */
573 case CLASS2 (rvc_normal
, rvc_normal
):
580 /* Swap the arguments such that A has the larger exponent. */
581 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
584 const REAL_VALUE_TYPE
*t
;
591 /* If the exponents are not identical, we need to shift the
592 significand of B down. */
595 /* If the exponents are too far apart, the significands
596 do not overlap, which makes the subtraction a noop. */
597 if (dexp
>= SIGNIFICAND_BITS
)
604 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
610 if (sub_significands (r
, a
, b
, inexact
))
612 /* We got a borrow out of the subtraction. That means that
613 A and B had the same exponent, and B had the larger
614 significand. We need to swap the sign and negate the
617 neg_significand (r
, r
);
622 if (add_significands (r
, a
, b
))
624 /* We got carry out of the addition. This means we need to
625 shift the significand back down one bit and increase the
627 inexact
|= sticky_rshift_significand (r
, r
, 1);
628 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
639 SET_REAL_EXP (r
, exp
);
640 /* Zero out the remaining fields. */
645 /* Re-normalize the result. */
648 /* Special case: if the subtraction results in zero, the result
650 if (r
->cl
== rvc_zero
)
653 r
->sig
[0] |= inexact
;
658 /* Calculate R = A * B. Return true if the result may be inexact. */
661 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
662 const REAL_VALUE_TYPE
*b
)
664 REAL_VALUE_TYPE u
, t
, *rr
;
665 unsigned int i
, j
, k
;
666 int sign
= a
->sign
^ b
->sign
;
667 bool inexact
= false;
669 switch (CLASS2 (a
->cl
, b
->cl
))
671 case CLASS2 (rvc_zero
, rvc_zero
):
672 case CLASS2 (rvc_zero
, rvc_normal
):
673 case CLASS2 (rvc_normal
, rvc_zero
):
674 /* +-0 * ANY = 0 with appropriate sign. */
678 case CLASS2 (rvc_zero
, rvc_nan
):
679 case CLASS2 (rvc_normal
, rvc_nan
):
680 case CLASS2 (rvc_inf
, rvc_nan
):
681 case CLASS2 (rvc_nan
, rvc_nan
):
682 /* ANY * NaN = NaN. */
687 case CLASS2 (rvc_nan
, rvc_zero
):
688 case CLASS2 (rvc_nan
, rvc_normal
):
689 case CLASS2 (rvc_nan
, rvc_inf
):
690 /* NaN * ANY = NaN. */
695 case CLASS2 (rvc_zero
, rvc_inf
):
696 case CLASS2 (rvc_inf
, rvc_zero
):
698 get_canonical_qnan (r
, sign
);
701 case CLASS2 (rvc_inf
, rvc_inf
):
702 case CLASS2 (rvc_normal
, rvc_inf
):
703 case CLASS2 (rvc_inf
, rvc_normal
):
704 /* Inf * Inf = Inf, R * Inf = Inf */
708 case CLASS2 (rvc_normal
, rvc_normal
):
715 if (r
== a
|| r
== b
)
721 /* Collect all the partial products. Since we don't have sure access
722 to a widening multiply, we split each long into two half-words.
724 Consider the long-hand form of a four half-word multiplication:
734 We construct partial products of the widened half-word products
735 that are known to not overlap, e.g. DF+DH. Each such partial
736 product is given its proper exponent, which allows us to sum them
737 and obtain the finished product. */
739 for (i
= 0; i
< SIGSZ
* 2; ++i
)
741 unsigned long ai
= a
->sig
[i
/ 2];
743 ai
>>= HOST_BITS_PER_LONG
/ 2;
745 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
750 for (j
= 0; j
< 2; ++j
)
752 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
753 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
762 /* Would underflow to zero, which we shouldn't bother adding. */
767 memset (&u
, 0, sizeof (u
));
769 SET_REAL_EXP (&u
, exp
);
771 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
773 unsigned long bi
= b
->sig
[k
/ 2];
775 bi
>>= HOST_BITS_PER_LONG
/ 2;
777 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
779 u
.sig
[k
/ 2] = ai
* bi
;
783 inexact
|= do_add (rr
, rr
, &u
, 0);
794 /* Calculate R = A / B. Return true if the result may be inexact. */
797 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
798 const REAL_VALUE_TYPE
*b
)
800 int exp
, sign
= a
->sign
^ b
->sign
;
801 REAL_VALUE_TYPE t
, *rr
;
804 switch (CLASS2 (a
->cl
, b
->cl
))
806 case CLASS2 (rvc_zero
, rvc_zero
):
808 case CLASS2 (rvc_inf
, rvc_inf
):
809 /* Inf / Inf = NaN. */
810 get_canonical_qnan (r
, sign
);
813 case CLASS2 (rvc_zero
, rvc_normal
):
814 case CLASS2 (rvc_zero
, rvc_inf
):
816 case CLASS2 (rvc_normal
, rvc_inf
):
821 case CLASS2 (rvc_normal
, rvc_zero
):
823 case CLASS2 (rvc_inf
, rvc_zero
):
828 case CLASS2 (rvc_zero
, rvc_nan
):
829 case CLASS2 (rvc_normal
, rvc_nan
):
830 case CLASS2 (rvc_inf
, rvc_nan
):
831 case CLASS2 (rvc_nan
, rvc_nan
):
832 /* ANY / NaN = NaN. */
837 case CLASS2 (rvc_nan
, rvc_zero
):
838 case CLASS2 (rvc_nan
, rvc_normal
):
839 case CLASS2 (rvc_nan
, rvc_inf
):
840 /* NaN / ANY = NaN. */
845 case CLASS2 (rvc_inf
, rvc_normal
):
850 case CLASS2 (rvc_normal
, rvc_normal
):
857 if (r
== a
|| r
== b
)
862 /* Make sure all fields in the result are initialized. */
867 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
878 SET_REAL_EXP (rr
, exp
);
880 inexact
= div_significands (rr
, a
, b
);
882 /* Re-normalize the result. */
884 rr
->sig
[0] |= inexact
;
892 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
893 one of the two operands is a NaN. */
896 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
901 switch (CLASS2 (a
->cl
, b
->cl
))
903 case CLASS2 (rvc_zero
, rvc_zero
):
904 /* Sign of zero doesn't matter for compares. */
907 case CLASS2 (rvc_normal
, rvc_zero
):
908 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
910 return decimal_do_compare (a
, b
, nan_result
);
912 case CLASS2 (rvc_inf
, rvc_zero
):
913 case CLASS2 (rvc_inf
, rvc_normal
):
914 return (a
->sign
? -1 : 1);
916 case CLASS2 (rvc_inf
, rvc_inf
):
917 return -a
->sign
- -b
->sign
;
919 case CLASS2 (rvc_zero
, rvc_normal
):
920 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
922 return decimal_do_compare (a
, b
, nan_result
);
924 case CLASS2 (rvc_zero
, rvc_inf
):
925 case CLASS2 (rvc_normal
, rvc_inf
):
926 return (b
->sign
? 1 : -1);
928 case CLASS2 (rvc_zero
, rvc_nan
):
929 case CLASS2 (rvc_normal
, rvc_nan
):
930 case CLASS2 (rvc_inf
, rvc_nan
):
931 case CLASS2 (rvc_nan
, rvc_nan
):
932 case CLASS2 (rvc_nan
, rvc_zero
):
933 case CLASS2 (rvc_nan
, rvc_normal
):
934 case CLASS2 (rvc_nan
, rvc_inf
):
937 case CLASS2 (rvc_normal
, rvc_normal
):
944 if (a
->sign
!= b
->sign
)
945 return -a
->sign
- -b
->sign
;
947 if (a
->decimal
|| b
->decimal
)
948 return decimal_do_compare (a
, b
, nan_result
);
950 if (REAL_EXP (a
) > REAL_EXP (b
))
952 else if (REAL_EXP (a
) < REAL_EXP (b
))
955 ret
= cmp_significands (a
, b
);
957 return (a
->sign
? -ret
: ret
);
960 /* Return A truncated to an integral value toward zero. */
963 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
977 decimal_do_fix_trunc (r
, a
);
980 if (REAL_EXP (r
) <= 0)
981 get_zero (r
, r
->sign
);
982 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
983 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
991 /* Perform the binary or unary operation described by CODE.
992 For a unary operation, leave OP1 NULL. This function returns
993 true if the result may be inexact due to loss of precision. */
996 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
997 const REAL_VALUE_TYPE
*op1
)
999 enum tree_code code
= (enum tree_code
) icode
;
1001 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1002 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1007 /* Clear any padding areas in *r if it isn't equal to one of the
1008 operands so that we can later do bitwise comparisons later on. */
1009 if (r
!= op0
&& r
!= op1
)
1010 memset (r
, '\0', sizeof (*r
));
1011 return do_add (r
, op0
, op1
, 0);
1014 if (r
!= op0
&& r
!= op1
)
1015 memset (r
, '\0', sizeof (*r
));
1016 return do_add (r
, op0
, op1
, 1);
1019 if (r
!= op0
&& r
!= op1
)
1020 memset (r
, '\0', sizeof (*r
));
1021 return do_multiply (r
, op0
, op1
);
1024 if (r
!= op0
&& r
!= op1
)
1025 memset (r
, '\0', sizeof (*r
));
1026 return do_divide (r
, op0
, op1
);
1029 if (op1
->cl
== rvc_nan
)
1031 else if (do_compare (op0
, op1
, -1) < 0)
1038 if (op1
->cl
== rvc_nan
)
1040 else if (do_compare (op0
, op1
, 1) < 0)
1056 case FIX_TRUNC_EXPR
:
1057 do_fix_trunc (r
, op0
);
1067 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1070 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1075 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1078 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1083 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1084 const REAL_VALUE_TYPE
*op1
)
1086 enum tree_code code
= (enum tree_code
) icode
;
1091 return do_compare (op0
, op1
, 1) < 0;
1093 return do_compare (op0
, op1
, 1) <= 0;
1095 return do_compare (op0
, op1
, -1) > 0;
1097 return do_compare (op0
, op1
, -1) >= 0;
1099 return do_compare (op0
, op1
, -1) == 0;
1101 return do_compare (op0
, op1
, -1) != 0;
1102 case UNORDERED_EXPR
:
1103 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1105 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1107 return do_compare (op0
, op1
, -1) < 0;
1109 return do_compare (op0
, op1
, -1) <= 0;
1111 return do_compare (op0
, op1
, 1) > 0;
1113 return do_compare (op0
, op1
, 1) >= 0;
1115 return do_compare (op0
, op1
, 0) == 0;
1117 return do_compare (op0
, op1
, 0) != 0;
1124 /* Return floor log2(R). */
1127 real_exponent (const REAL_VALUE_TYPE
*r
)
1135 return (unsigned int)-1 >> 1;
1137 return REAL_EXP (r
);
1143 /* R = OP0 * 2**EXP. */
1146 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1157 exp
+= REAL_EXP (op0
);
1159 get_inf (r
, r
->sign
);
1160 else if (exp
< -MAX_EXP
)
1161 get_zero (r
, r
->sign
);
1163 SET_REAL_EXP (r
, exp
);
1171 /* Determine whether a floating-point value X is infinite. */
1174 real_isinf (const REAL_VALUE_TYPE
*r
)
1176 return (r
->cl
== rvc_inf
);
1179 /* Determine whether a floating-point value X is a NaN. */
1182 real_isnan (const REAL_VALUE_TYPE
*r
)
1184 return (r
->cl
== rvc_nan
);
1187 /* Determine whether a floating-point value X is finite. */
1190 real_isfinite (const REAL_VALUE_TYPE
*r
)
1192 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1195 /* Determine whether a floating-point value X is negative. */
1198 real_isneg (const REAL_VALUE_TYPE
*r
)
1203 /* Determine whether a floating-point value X is minus zero. */
1206 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1208 return r
->sign
&& r
->cl
== rvc_zero
;
1211 /* Compare two floating-point objects for bitwise identity. */
1214 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1220 if (a
->sign
!= b
->sign
)
1230 if (a
->decimal
!= b
->decimal
)
1232 if (REAL_EXP (a
) != REAL_EXP (b
))
1237 if (a
->signalling
!= b
->signalling
)
1239 /* The significand is ignored for canonical NaNs. */
1240 if (a
->canonical
|| b
->canonical
)
1241 return a
->canonical
== b
->canonical
;
1248 for (i
= 0; i
< SIGSZ
; ++i
)
1249 if (a
->sig
[i
] != b
->sig
[i
])
1255 /* Try to change R into its exact multiplicative inverse in machine
1256 mode MODE. Return true if successful. */
1259 exact_real_inverse (machine_mode mode
, REAL_VALUE_TYPE
*r
)
1261 const REAL_VALUE_TYPE
*one
= real_digit (1);
1265 if (r
->cl
!= rvc_normal
)
1268 /* Check for a power of two: all significand bits zero except the MSB. */
1269 for (i
= 0; i
< SIGSZ
-1; ++i
)
1272 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1275 /* Find the inverse and truncate to the required mode. */
1276 do_divide (&u
, one
, r
);
1277 real_convert (&u
, mode
, &u
);
1279 /* The rounding may have overflowed. */
1280 if (u
.cl
!= rvc_normal
)
1282 for (i
= 0; i
< SIGSZ
-1; ++i
)
1285 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1292 /* Return true if arithmetic on values in IMODE that were promoted
1293 from values in TMODE is equivalent to direct arithmetic on values
1297 real_can_shorten_arithmetic (machine_mode imode
, machine_mode tmode
)
1299 const struct real_format
*tfmt
, *ifmt
;
1300 tfmt
= REAL_MODE_FORMAT (tmode
);
1301 ifmt
= REAL_MODE_FORMAT (imode
);
1302 /* These conditions are conservative rather than trying to catch the
1303 exact boundary conditions; the main case to allow is IEEE float
1305 return (ifmt
->b
== tfmt
->b
1306 && ifmt
->p
> 2 * tfmt
->p
1307 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1308 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1309 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1310 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1311 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1312 && (ifmt
->has_sign_dependent_rounding
1313 == tfmt
->has_sign_dependent_rounding
)
1314 && ifmt
->has_nans
>= tfmt
->has_nans
1315 && ifmt
->has_inf
>= tfmt
->has_inf
1316 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1317 && !MODE_COMPOSITE_P (tmode
)
1318 && !MODE_COMPOSITE_P (imode
));
1321 /* Render R as an integer. */
1324 real_to_integer (const REAL_VALUE_TYPE
*r
)
1326 unsigned HOST_WIDE_INT i
;
1337 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1344 return decimal_real_to_integer (r
);
1346 if (REAL_EXP (r
) <= 0)
1348 /* Only force overflow for unsigned overflow. Signed overflow is
1349 undefined, so it doesn't matter what we return, and some callers
1350 expect to be able to use this routine for both signed and
1351 unsigned conversions. */
1352 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1355 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1356 i
= r
->sig
[SIGSZ
-1];
1359 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1360 i
= r
->sig
[SIGSZ
-1];
1361 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1362 i
|= r
->sig
[SIGSZ
-2];
1365 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1376 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1377 be represented in precision, *FAIL is set to TRUE. */
1380 real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
1382 HOST_WIDE_INT val
[2 * WIDE_INT_MAX_ELTS
];
1391 return wi::zero (precision
);
1399 return wi::set_bit_in_zero (precision
- 1, precision
);
1401 return ~wi::set_bit_in_zero (precision
- 1, precision
);
1405 return decimal_real_to_integer (r
, fail
, precision
);
1410 /* Only force overflow for unsigned overflow. Signed overflow is
1411 undefined, so it doesn't matter what we return, and some callers
1412 expect to be able to use this routine for both signed and
1413 unsigned conversions. */
1414 if (exp
> precision
)
1417 /* Put the significand into a wide_int that has precision W, which
1418 is the smallest HWI-multiple that has at least PRECISION bits.
1419 This ensures that the top bit of the significand is in the
1420 top bit of the wide_int. */
1421 words
= (precision
+ HOST_BITS_PER_WIDE_INT
- 1) / HOST_BITS_PER_WIDE_INT
;
1422 w
= words
* HOST_BITS_PER_WIDE_INT
;
1424 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1425 for (int i
= 0; i
< words
; i
++)
1427 int j
= SIGSZ
- words
+ i
;
1428 val
[i
] = (j
< 0) ? 0 : r
->sig
[j
];
1431 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1432 for (int i
= 0; i
< words
; i
++)
1434 int j
= SIGSZ
- (words
* 2) + (i
* 2);
1441 val
[i
] |= (unsigned HOST_WIDE_INT
) r
->sig
[j
] << HOST_BITS_PER_LONG
;
1444 /* Shift the value into place and truncate to the desired precision. */
1445 result
= wide_int::from_array (val
, words
, w
);
1446 result
= wi::lrshift (result
, w
- exp
);
1447 result
= wide_int::from (result
, precision
, UNSIGNED
);
1459 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1460 of NUM / DEN. Return the quotient and place the remainder in NUM.
1461 It is expected that NUM / DEN are close enough that the quotient is
1464 static unsigned long
1465 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1467 unsigned long q
, msb
;
1468 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1477 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1479 lshift_significand_1 (num
, num
);
1481 if (msb
|| cmp_significands (num
, den
) >= 0)
1483 sub_significands (num
, num
, den
, 0);
1487 while (--expn
>= expd
);
1489 SET_REAL_EXP (num
, expd
);
1495 /* Render R as a decimal floating point constant. Emit DIGITS significant
1496 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1497 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1498 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1499 to a string that, when parsed back in mode MODE, yields the same value. */
1501 #define M_LOG10_2 0.30102999566398119521
1504 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1505 size_t buf_size
, size_t digits
,
1506 int crop_trailing_zeros
, machine_mode mode
)
1508 const struct real_format
*fmt
= NULL
;
1509 const REAL_VALUE_TYPE
*one
, *ten
;
1510 REAL_VALUE_TYPE r
, pten
, u
, v
;
1511 int dec_exp
, cmp_one
, digit
;
1513 char *p
, *first
, *last
;
1517 if (mode
!= VOIDmode
)
1519 fmt
= REAL_MODE_FORMAT (mode
);
1527 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1532 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1535 /* ??? Print the significand as well, if not canonical? */
1536 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1537 (r_orig
->signalling
? 'S' : 'Q'));
1545 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1549 /* Bound the number of digits printed by the size of the representation. */
1550 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1551 if (digits
== 0 || digits
> max_digits
)
1552 digits
= max_digits
;
1554 /* Estimate the decimal exponent, and compute the length of the string it
1555 will print as. Be conservative and add one to account for possible
1556 overflow or rounding error. */
1557 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1558 for (max_digits
= 1; dec_exp
; max_digits
++)
1561 /* Bound the number of digits printed by the size of the output buffer. */
1562 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1563 gcc_assert (max_digits
<= buf_size
);
1564 if (digits
> max_digits
)
1565 digits
= max_digits
;
1567 one
= real_digit (1);
1568 ten
= ten_to_ptwo (0);
1576 cmp_one
= do_compare (&r
, one
, 0);
1581 /* Number is greater than one. Convert significand to an integer
1582 and strip trailing decimal zeros. */
1585 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1587 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1588 m
= floor_log2 (max_digits
);
1590 /* Iterate over the bits of the possible powers of 10 that might
1591 be present in U and eliminate them. That is, if we find that
1592 10**2**M divides U evenly, keep the division and increase
1598 do_divide (&t
, &u
, ten_to_ptwo (m
));
1599 do_fix_trunc (&v
, &t
);
1600 if (cmp_significands (&v
, &t
) == 0)
1608 /* Revert the scaling to integer that we performed earlier. */
1609 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1610 - (SIGNIFICAND_BITS
- 1));
1613 /* Find power of 10. Do this by dividing out 10**2**M when
1614 this is larger than the current remainder. Fill PTEN with
1615 the power of 10 that we compute. */
1616 if (REAL_EXP (&r
) > 0)
1618 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1621 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1622 if (do_compare (&u
, ptentwo
, 0) >= 0)
1624 do_divide (&u
, &u
, ptentwo
);
1625 do_multiply (&pten
, &pten
, ptentwo
);
1632 /* We managed to divide off enough tens in the above reduction
1633 loop that we've now got a negative exponent. Fall into the
1634 less-than-one code to compute the proper value for PTEN. */
1641 /* Number is less than one. Pad significand with leading
1647 /* Stop if we'd shift bits off the bottom. */
1651 do_multiply (&u
, &v
, ten
);
1653 /* Stop if we're now >= 1. */
1654 if (REAL_EXP (&u
) > 0)
1662 /* Find power of 10. Do this by multiplying in P=10**2**M when
1663 the current remainder is smaller than 1/P. Fill PTEN with the
1664 power of 10 that we compute. */
1665 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1668 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1669 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1671 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1673 do_multiply (&v
, &v
, ptentwo
);
1674 do_multiply (&pten
, &pten
, ptentwo
);
1680 /* Invert the positive power of 10 that we've collected so far. */
1681 do_divide (&pten
, one
, &pten
);
1689 /* At this point, PTEN should contain the nearest power of 10 smaller
1690 than R, such that this division produces the first digit.
1692 Using a divide-step primitive that returns the complete integral
1693 remainder avoids the rounding error that would be produced if
1694 we were to use do_divide here and then simply multiply by 10 for
1695 each subsequent digit. */
1697 digit
= rtd_divmod (&r
, &pten
);
1699 /* Be prepared for error in that division via underflow ... */
1700 if (digit
== 0 && cmp_significand_0 (&r
))
1702 /* Multiply by 10 and try again. */
1703 do_multiply (&r
, &r
, ten
);
1704 digit
= rtd_divmod (&r
, &pten
);
1706 gcc_assert (digit
!= 0);
1709 /* ... or overflow. */
1719 gcc_assert (digit
<= 10);
1723 /* Generate subsequent digits. */
1724 while (--digits
> 0)
1726 do_multiply (&r
, &r
, ten
);
1727 digit
= rtd_divmod (&r
, &pten
);
1732 /* Generate one more digit with which to do rounding. */
1733 do_multiply (&r
, &r
, ten
);
1734 digit
= rtd_divmod (&r
, &pten
);
1736 /* Round the result. */
1737 if (fmt
&& fmt
->round_towards_zero
)
1739 /* If the format uses round towards zero when parsing the string
1740 back in, we need to always round away from zero here. */
1741 if (cmp_significand_0 (&r
))
1743 round_up
= digit
> 0;
1749 /* Round to nearest. If R is nonzero there are additional
1750 nonzero digits to be extracted. */
1751 if (cmp_significand_0 (&r
))
1753 /* Round to even. */
1754 else if ((p
[-1] - '0') & 1)
1758 round_up
= digit
> 5;
1775 /* Carry out of the first digit. This means we had all 9's and
1776 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1784 /* Insert the decimal point. */
1785 first
[0] = first
[1];
1788 /* If requested, drop trailing zeros. Never crop past "1.0". */
1789 if (crop_trailing_zeros
)
1790 while (last
> first
+ 3 && last
[-1] == '0')
1793 /* Append the exponent. */
1794 sprintf (last
, "e%+d", dec_exp
);
1796 #ifdef ENABLE_CHECKING
1797 /* Verify that we can read the original value back in. */
1798 if (mode
!= VOIDmode
)
1800 real_from_string (&r
, str
);
1801 real_convert (&r
, mode
, &r
);
1802 gcc_assert (real_identical (&r
, r_orig
));
1807 /* Likewise, except always uses round-to-nearest. */
1810 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1811 size_t digits
, int crop_trailing_zeros
)
1813 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1814 digits
, crop_trailing_zeros
, VOIDmode
);
1817 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1818 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1819 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1820 strip trailing zeros. */
1823 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1824 size_t digits
, int crop_trailing_zeros
)
1826 int i
, j
, exp
= REAL_EXP (r
);
1839 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1842 /* ??? Print the significand as well, if not canonical? */
1843 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1844 (r
->signalling
? 'S' : 'Q'));
1852 /* Hexadecimal format for decimal floats is not interesting. */
1853 strcpy (str
, "N/A");
1858 digits
= SIGNIFICAND_BITS
/ 4;
1860 /* Bound the number of digits printed by the size of the output buffer. */
1862 sprintf (exp_buf
, "p%+d", exp
);
1863 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1864 gcc_assert (max_digits
<= buf_size
);
1865 if (digits
> max_digits
)
1866 digits
= max_digits
;
1877 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1878 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1880 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1886 if (crop_trailing_zeros
)
1887 while (p
> first
+ 1 && p
[-1] == '0')
1890 sprintf (p
, "p%+d", exp
);
1893 /* Initialize R from a decimal or hexadecimal string. The string is
1894 assumed to have been syntax checked already. Return -1 if the
1895 value underflows, +1 if overflows, and 0 otherwise. */
1898 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1910 else if (*str
== '+')
1913 if (!strncmp (str
, "QNaN", 4))
1915 get_canonical_qnan (r
, sign
);
1918 else if (!strncmp (str
, "SNaN", 4))
1920 get_canonical_snan (r
, sign
);
1923 else if (!strncmp (str
, "Inf", 3))
1929 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1931 /* Hexadecimal floating point. */
1932 int pos
= SIGNIFICAND_BITS
- 4, d
;
1940 d
= hex_value (*str
);
1945 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1946 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1950 /* Ensure correct rounding by setting last bit if there is
1951 a subsequent nonzero digit. */
1959 if (pos
== SIGNIFICAND_BITS
- 4)
1966 d
= hex_value (*str
);
1971 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1972 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1976 /* Ensure correct rounding by setting last bit if there is
1977 a subsequent nonzero digit. */
1983 /* If the mantissa is zero, ignore the exponent. */
1984 if (!cmp_significand_0 (r
))
1987 if (*str
== 'p' || *str
== 'P')
1989 bool exp_neg
= false;
1997 else if (*str
== '+')
2001 while (ISDIGIT (*str
))
2007 /* Overflowed the exponent. */
2022 SET_REAL_EXP (r
, exp
);
2028 /* Decimal floating point. */
2029 const char *cstr
= str
;
2033 while (*cstr
== '0')
2038 while (*cstr
== '0')
2042 /* If the mantissa is zero, ignore the exponent. */
2043 if (!ISDIGIT (*cstr
))
2046 /* Nonzero value, possibly overflowing or underflowing. */
2047 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2048 inexact
= mpfr_strtofr (m
, str
, NULL
, 10, GMP_RNDZ
);
2049 /* The result should never be a NaN, and because the rounding is
2050 toward zero should never be an infinity. */
2051 gcc_assert (!mpfr_nan_p (m
) && !mpfr_inf_p (m
));
2052 if (mpfr_zero_p (m
) || mpfr_get_exp (m
) < -MAX_EXP
+ 4)
2057 else if (mpfr_get_exp (m
) > MAX_EXP
- 4)
2064 real_from_mpfr (r
, m
, NULL_TREE
, GMP_RNDZ
);
2065 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2066 because the hex digits used in real_from_mpfr did not
2067 start with a digit 8 to f, but the exponent bounds above
2068 should have avoided underflow or overflow. */
2069 gcc_assert (r
->cl
= rvc_normal
);
2070 /* Set a sticky bit if mpfr_strtofr was inexact. */
2071 r
->sig
[0] |= inexact
;
2092 /* Legacy. Similar, but return the result directly. */
2095 real_from_string2 (const char *s
, machine_mode mode
)
2099 real_from_string (&r
, s
);
2100 if (mode
!= VOIDmode
)
2101 real_convert (&r
, mode
, &r
);
2106 /* Initialize R from string S and desired MODE. */
2109 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, machine_mode mode
)
2111 if (DECIMAL_FLOAT_MODE_P (mode
))
2112 decimal_real_from_string (r
, s
);
2114 real_from_string (r
, s
);
2116 if (mode
!= VOIDmode
)
2117 real_convert (r
, mode
, r
);
2120 /* Initialize R from the wide_int VAL_IN. The MODE is not VOIDmode,*/
2123 real_from_integer (REAL_VALUE_TYPE
*r
, machine_mode mode
,
2124 const wide_int_ref
&val_in
, signop sgn
)
2130 unsigned int len
= val_in
.get_precision ();
2132 int maxbitlen
= MAX_BITSIZE_MODE_ANY_INT
+ HOST_BITS_PER_WIDE_INT
;
2133 const unsigned int realmax
= (SIGNIFICAND_BITS
/ HOST_BITS_PER_WIDE_INT
2134 * HOST_BITS_PER_WIDE_INT
);
2136 memset (r
, 0, sizeof (*r
));
2138 r
->sign
= wi::neg_p (val_in
, sgn
);
2140 /* We have to ensure we can negate the largest negative number. */
2141 wide_int val
= wide_int::from (val_in
, maxbitlen
, sgn
);
2146 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2147 won't work with precisions that are not a multiple of
2148 HOST_BITS_PER_WIDE_INT. */
2149 len
+= HOST_BITS_PER_WIDE_INT
- 1;
2151 /* Ensure we can represent the largest negative number. */
2154 len
= len
/HOST_BITS_PER_WIDE_INT
* HOST_BITS_PER_WIDE_INT
;
2156 /* Cap the size to the size allowed by real.h. */
2159 HOST_WIDE_INT cnt_l_z
;
2160 cnt_l_z
= wi::clz (val
);
2162 if (maxbitlen
- cnt_l_z
> realmax
)
2164 e
= maxbitlen
- cnt_l_z
- realmax
;
2166 /* This value is too large, we must shift it right to
2167 preserve all the bits we can, and then bump the
2168 exponent up by that amount. */
2169 val
= wi::lrshift (val
, e
);
2174 /* Clear out top bits so elt will work with precisions that aren't
2175 a multiple of HOST_BITS_PER_WIDE_INT. */
2176 val
= wide_int::from (val
, len
, sgn
);
2177 len
= len
/ HOST_BITS_PER_WIDE_INT
;
2179 SET_REAL_EXP (r
, len
* HOST_BITS_PER_WIDE_INT
+ e
);
2182 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2183 for (i
= len
- 1; i
>= 0; i
--)
2185 r
->sig
[j
--] = val
.elt (i
);
2191 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2192 for (i
= len
- 1; i
>= 0; i
--)
2194 HOST_WIDE_INT e
= val
.elt (i
);
2195 r
->sig
[j
--] = e
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2207 if (DECIMAL_FLOAT_MODE_P (mode
))
2208 decimal_from_integer (r
);
2209 else if (mode
!= VOIDmode
)
2210 real_convert (r
, mode
, r
);
2213 /* Render R, an integral value, as a floating point constant with no
2214 specified exponent. */
2217 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2220 int dec_exp
, digit
, digits
;
2221 REAL_VALUE_TYPE r
, pten
;
2227 if (r
.cl
== rvc_zero
)
2236 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2237 digits
= dec_exp
+ 1;
2238 gcc_assert ((digits
+ 2) < (int)buf_size
);
2240 pten
= *real_digit (1);
2241 times_pten (&pten
, dec_exp
);
2247 digit
= rtd_divmod (&r
, &pten
);
2248 gcc_assert (digit
>= 0 && digit
<= 9);
2250 while (--digits
> 0)
2253 digit
= rtd_divmod (&r
, &pten
);
2260 /* Convert a real with an integral value to decimal float. */
2263 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2267 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2268 decimal_real_from_string (r
, str
);
2271 /* Returns 10**2**N. */
2273 static const REAL_VALUE_TYPE
*
2276 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2278 gcc_assert (n
>= 0);
2279 gcc_assert (n
< EXP_BITS
);
2281 if (tens
[n
].cl
== rvc_zero
)
2283 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2285 HOST_WIDE_INT t
= 10;
2288 for (i
= 0; i
< n
; ++i
)
2291 real_from_integer (&tens
[n
], VOIDmode
, t
, UNSIGNED
);
2295 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2296 do_multiply (&tens
[n
], t
, t
);
2303 /* Returns 10**(-2**N). */
2305 static const REAL_VALUE_TYPE
*
2306 ten_to_mptwo (int n
)
2308 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2310 gcc_assert (n
>= 0);
2311 gcc_assert (n
< EXP_BITS
);
2313 if (tens
[n
].cl
== rvc_zero
)
2314 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2321 static const REAL_VALUE_TYPE
*
2324 static REAL_VALUE_TYPE num
[10];
2326 gcc_assert (n
>= 0);
2327 gcc_assert (n
<= 9);
2329 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2330 real_from_integer (&num
[n
], VOIDmode
, n
, UNSIGNED
);
2335 /* Multiply R by 10**EXP. */
2338 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2340 REAL_VALUE_TYPE pten
, *rr
;
2341 bool negative
= (exp
< 0);
2347 pten
= *real_digit (1);
2353 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2355 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2358 do_divide (r
, r
, &pten
);
2361 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2363 const REAL_VALUE_TYPE
*
2366 static REAL_VALUE_TYPE value
;
2368 /* Initialize mathematical constants for constant folding builtins.
2369 These constants need to be given to at least 160 bits precision. */
2370 if (value
.cl
== rvc_zero
)
2373 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2374 mpfr_set_ui (m
, 1, GMP_RNDN
);
2375 mpfr_exp (m
, m
, GMP_RNDN
);
2376 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2383 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2385 const REAL_VALUE_TYPE
*
2386 dconst_third_ptr (void)
2388 static REAL_VALUE_TYPE value
;
2390 /* Initialize mathematical constants for constant folding builtins.
2391 These constants need to be given to at least 160 bits precision. */
2392 if (value
.cl
== rvc_zero
)
2394 real_arithmetic (&value
, RDIV_EXPR
, &dconst1
, real_digit (3));
2399 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2401 const REAL_VALUE_TYPE
*
2402 dconst_sqrt2_ptr (void)
2404 static REAL_VALUE_TYPE value
;
2406 /* Initialize mathematical constants for constant folding builtins.
2407 These constants need to be given to at least 160 bits precision. */
2408 if (value
.cl
== rvc_zero
)
2411 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2412 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2413 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2419 /* Fills R with +Inf. */
2422 real_inf (REAL_VALUE_TYPE
*r
)
2427 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2428 we force a QNaN, else we force an SNaN. The string, if not empty,
2429 is parsed as a number and placed in the significand. Return true
2430 if the string was successfully parsed. */
2433 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2436 const struct real_format
*fmt
;
2438 fmt
= REAL_MODE_FORMAT (mode
);
2444 get_canonical_qnan (r
, 0);
2446 get_canonical_snan (r
, 0);
2452 memset (r
, 0, sizeof (*r
));
2455 /* Parse akin to strtol into the significand of R. */
2457 while (ISSPACE (*str
))
2461 else if (*str
== '+')
2466 if (*str
== 'x' || *str
== 'X')
2475 while ((d
= hex_value (*str
)) < base
)
2482 lshift_significand (r
, r
, 3);
2485 lshift_significand (r
, r
, 4);
2488 lshift_significand_1 (&u
, r
);
2489 lshift_significand (r
, r
, 3);
2490 add_significands (r
, r
, &u
);
2498 add_significands (r
, r
, &u
);
2503 /* Must have consumed the entire string for success. */
2507 /* Shift the significand into place such that the bits
2508 are in the most significant bits for the format. */
2509 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2511 /* Our MSB is always unset for NaNs. */
2512 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2514 /* Force quiet or signalling NaN. */
2515 r
->signalling
= !quiet
;
2521 /* Fills R with the largest finite value representable in mode MODE.
2522 If SIGN is nonzero, R is set to the most negative finite value. */
2525 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
2527 const struct real_format
*fmt
;
2530 fmt
= REAL_MODE_FORMAT (mode
);
2532 memset (r
, 0, sizeof (*r
));
2535 decimal_real_maxval (r
, sign
, mode
);
2540 SET_REAL_EXP (r
, fmt
->emax
);
2542 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2543 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2544 clear_significand_below (r
, np2
);
2546 if (fmt
->pnan
< fmt
->p
)
2547 /* This is an IBM extended double format made up of two IEEE
2548 doubles. The value of the long double is the sum of the
2549 values of the two parts. The most significant part is
2550 required to be the value of the long double rounded to the
2551 nearest double. Rounding means we need a slightly smaller
2552 value for LDBL_MAX. */
2553 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2557 /* Fills R with 2**N. */
2560 real_2expN (REAL_VALUE_TYPE
*r
, int n
, machine_mode fmode
)
2562 memset (r
, 0, sizeof (*r
));
2567 else if (n
< -MAX_EXP
)
2572 SET_REAL_EXP (r
, n
);
2573 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2575 if (DECIMAL_FLOAT_MODE_P (fmode
))
2576 decimal_real_convert (r
, fmode
, r
);
2581 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2585 bool round_up
= false;
2591 decimal_round_for_format (fmt
, r
);
2594 /* FIXME. We can come here via fp_easy_constant
2595 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2596 investigated whether this convert needs to be here, or
2597 something else is missing. */
2598 decimal_real_convert (r
, DFmode
, r
);
2602 emin2m1
= fmt
->emin
- 1;
2605 np2
= SIGNIFICAND_BITS
- p2
;
2609 get_zero (r
, r
->sign
);
2611 if (!fmt
->has_signed_zero
)
2616 get_inf (r
, r
->sign
);
2621 clear_significand_below (r
, np2
);
2631 /* Check the range of the exponent. If we're out of range,
2632 either underflow or overflow. */
2633 if (REAL_EXP (r
) > emax2
)
2635 else if (REAL_EXP (r
) <= emin2m1
)
2639 if (!fmt
->has_denorm
)
2641 /* Don't underflow completely until we've had a chance to round. */
2642 if (REAL_EXP (r
) < emin2m1
)
2647 diff
= emin2m1
- REAL_EXP (r
) + 1;
2651 /* De-normalize the significand. */
2652 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2653 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2657 if (!fmt
->round_towards_zero
)
2659 /* There are P2 true significand bits, followed by one guard bit,
2660 followed by one sticky bit, followed by stuff. Fold nonzero
2661 stuff into the sticky bit. */
2662 unsigned long sticky
;
2666 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2667 sticky
|= r
->sig
[i
];
2669 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2671 guard
= test_significand_bit (r
, np2
- 1);
2672 lsb
= test_significand_bit (r
, np2
);
2674 /* Round to even. */
2675 round_up
= guard
&& (sticky
|| lsb
);
2682 set_significand_bit (&u
, np2
);
2684 if (add_significands (r
, r
, &u
))
2686 /* Overflow. Means the significand had been all ones, and
2687 is now all zeros. Need to increase the exponent, and
2688 possibly re-normalize it. */
2689 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2690 if (REAL_EXP (r
) > emax2
)
2692 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2696 /* Catch underflow that we deferred until after rounding. */
2697 if (REAL_EXP (r
) <= emin2m1
)
2700 /* Clear out trailing garbage. */
2701 clear_significand_below (r
, np2
);
2704 /* Extend or truncate to a new mode. */
2707 real_convert (REAL_VALUE_TYPE
*r
, machine_mode mode
,
2708 const REAL_VALUE_TYPE
*a
)
2710 const struct real_format
*fmt
;
2712 fmt
= REAL_MODE_FORMAT (mode
);
2717 if (a
->decimal
|| fmt
->b
== 10)
2718 decimal_real_convert (r
, mode
, a
);
2720 round_for_format (fmt
, r
);
2722 /* round_for_format de-normalizes denormals. Undo just that part. */
2723 if (r
->cl
== rvc_normal
)
2727 /* Legacy. Likewise, except return the struct directly. */
2730 real_value_truncate (machine_mode mode
, REAL_VALUE_TYPE a
)
2733 real_convert (&r
, mode
, &a
);
2737 /* Return true if truncating to MODE is exact. */
2740 exact_real_truncate (machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2742 const struct real_format
*fmt
;
2746 fmt
= REAL_MODE_FORMAT (mode
);
2749 /* Don't allow conversion to denormals. */
2750 emin2m1
= fmt
->emin
- 1;
2751 if (REAL_EXP (a
) <= emin2m1
)
2754 /* After conversion to the new mode, the value must be identical. */
2755 real_convert (&t
, mode
, a
);
2756 return real_identical (&t
, a
);
2759 /* Write R to the given target format. Place the words of the result
2760 in target word order in BUF. There are always 32 bits in each
2761 long, no matter the size of the host long.
2763 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2766 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2767 const struct real_format
*fmt
)
2773 round_for_format (fmt
, &r
);
2777 (*fmt
->encode
) (fmt
, buf
, &r
);
2782 /* Similar, but look up the format from MODE. */
2785 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, machine_mode mode
)
2787 const struct real_format
*fmt
;
2789 fmt
= REAL_MODE_FORMAT (mode
);
2792 return real_to_target_fmt (buf
, r
, fmt
);
2795 /* Read R from the given target format. Read the words of the result
2796 in target word order in BUF. There are always 32 bits in each
2797 long, no matter the size of the host long. */
2800 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2801 const struct real_format
*fmt
)
2803 (*fmt
->decode
) (fmt
, r
, buf
);
2806 /* Similar, but look up the format from MODE. */
2809 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, machine_mode mode
)
2811 const struct real_format
*fmt
;
2813 fmt
= REAL_MODE_FORMAT (mode
);
2816 (*fmt
->decode
) (fmt
, r
, buf
);
2819 /* Return the number of bits of the largest binary value that the
2820 significand of MODE will hold. */
2821 /* ??? Legacy. Should get access to real_format directly. */
2824 significand_size (machine_mode mode
)
2826 const struct real_format
*fmt
;
2828 fmt
= REAL_MODE_FORMAT (mode
);
2834 /* Return the size in bits of the largest binary value that can be
2835 held by the decimal coefficient for this mode. This is one more
2836 than the number of bits required to hold the largest coefficient
2838 double log2_10
= 3.3219281;
2839 return fmt
->p
* log2_10
;
2844 /* Return a hash value for the given real value. */
2845 /* ??? The "unsigned int" return value is intended to be hashval_t,
2846 but I didn't want to pull hashtab.h into real.h. */
2849 real_hash (const REAL_VALUE_TYPE
*r
)
2854 h
= r
->cl
| (r
->sign
<< 2);
2862 h
|= REAL_EXP (r
) << 3;
2867 h
^= (unsigned int)-1;
2876 if (sizeof (unsigned long) > sizeof (unsigned int))
2877 for (i
= 0; i
< SIGSZ
; ++i
)
2879 unsigned long s
= r
->sig
[i
];
2880 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2883 for (i
= 0; i
< SIGSZ
; ++i
)
2889 /* IEEE single-precision format. */
2891 static void encode_ieee_single (const struct real_format
*fmt
,
2892 long *, const REAL_VALUE_TYPE
*);
2893 static void decode_ieee_single (const struct real_format
*,
2894 REAL_VALUE_TYPE
*, const long *);
2897 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2898 const REAL_VALUE_TYPE
*r
)
2900 unsigned long image
, sig
, exp
;
2901 unsigned long sign
= r
->sign
;
2902 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2905 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2916 image
|= 0x7fffffff;
2923 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2924 if (r
->signalling
== fmt
->qnan_msb_set
)
2935 image
|= 0x7fffffff;
2939 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2940 whereas the intermediate representation is 0.F x 2**exp.
2941 Which means we're off by one. */
2945 exp
= REAL_EXP (r
) + 127 - 1;
2958 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2961 unsigned long image
= buf
[0] & 0xffffffff;
2962 bool sign
= (image
>> 31) & 1;
2963 int exp
= (image
>> 23) & 0xff;
2965 memset (r
, 0, sizeof (*r
));
2966 image
<<= HOST_BITS_PER_LONG
- 24;
2971 if (image
&& fmt
->has_denorm
)
2975 SET_REAL_EXP (r
, -126);
2976 r
->sig
[SIGSZ
-1] = image
<< 1;
2979 else if (fmt
->has_signed_zero
)
2982 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2988 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2989 ^ fmt
->qnan_msb_set
);
2990 r
->sig
[SIGSZ
-1] = image
;
3002 SET_REAL_EXP (r
, exp
- 127 + 1);
3003 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
3007 const struct real_format ieee_single_format
=
3028 const struct real_format mips_single_format
=
3049 const struct real_format motorola_single_format
=
3070 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3071 single precision with the following differences:
3072 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3074 - NaNs are not supported.
3075 - The range of non-zero numbers in binary is
3076 (001)[1.]000...000 to (255)[1.]111...111.
3077 - Denormals can be represented, but are treated as +0.0 when
3078 used as an operand and are never generated as a result.
3079 - -0.0 can be represented, but a zero result is always +0.0.
3080 - the only supported rounding mode is trunction (towards zero). */
3081 const struct real_format spu_single_format
=
3102 /* IEEE double-precision format. */
3104 static void encode_ieee_double (const struct real_format
*fmt
,
3105 long *, const REAL_VALUE_TYPE
*);
3106 static void decode_ieee_double (const struct real_format
*,
3107 REAL_VALUE_TYPE
*, const long *);
3110 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3111 const REAL_VALUE_TYPE
*r
)
3113 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3114 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3116 image_hi
= r
->sign
<< 31;
3119 if (HOST_BITS_PER_LONG
== 64)
3121 sig_hi
= r
->sig
[SIGSZ
-1];
3122 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3123 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3127 sig_hi
= r
->sig
[SIGSZ
-1];
3128 sig_lo
= r
->sig
[SIGSZ
-2];
3129 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3130 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3140 image_hi
|= 2047 << 20;
3143 image_hi
|= 0x7fffffff;
3144 image_lo
= 0xffffffff;
3153 if (fmt
->canonical_nan_lsbs_set
)
3155 sig_hi
= (1 << 19) - 1;
3156 sig_lo
= 0xffffffff;
3164 if (r
->signalling
== fmt
->qnan_msb_set
)
3165 sig_hi
&= ~(1 << 19);
3168 if (sig_hi
== 0 && sig_lo
== 0)
3171 image_hi
|= 2047 << 20;
3177 image_hi
|= 0x7fffffff;
3178 image_lo
= 0xffffffff;
3183 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3184 whereas the intermediate representation is 0.F x 2**exp.
3185 Which means we're off by one. */
3189 exp
= REAL_EXP (r
) + 1023 - 1;
3190 image_hi
|= exp
<< 20;
3199 if (FLOAT_WORDS_BIG_ENDIAN
)
3200 buf
[0] = image_hi
, buf
[1] = image_lo
;
3202 buf
[0] = image_lo
, buf
[1] = image_hi
;
3206 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3209 unsigned long image_hi
, image_lo
;
3213 if (FLOAT_WORDS_BIG_ENDIAN
)
3214 image_hi
= buf
[0], image_lo
= buf
[1];
3216 image_lo
= buf
[0], image_hi
= buf
[1];
3217 image_lo
&= 0xffffffff;
3218 image_hi
&= 0xffffffff;
3220 sign
= (image_hi
>> 31) & 1;
3221 exp
= (image_hi
>> 20) & 0x7ff;
3223 memset (r
, 0, sizeof (*r
));
3225 image_hi
<<= 32 - 21;
3226 image_hi
|= image_lo
>> 21;
3227 image_hi
&= 0x7fffffff;
3228 image_lo
<<= 32 - 21;
3232 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3236 SET_REAL_EXP (r
, -1022);
3237 if (HOST_BITS_PER_LONG
== 32)
3239 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3241 r
->sig
[SIGSZ
-1] = image_hi
;
3242 r
->sig
[SIGSZ
-2] = image_lo
;
3246 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3247 r
->sig
[SIGSZ
-1] = image_hi
;
3251 else if (fmt
->has_signed_zero
)
3254 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3256 if (image_hi
|| image_lo
)
3260 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3261 if (HOST_BITS_PER_LONG
== 32)
3263 r
->sig
[SIGSZ
-1] = image_hi
;
3264 r
->sig
[SIGSZ
-2] = image_lo
;
3267 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3279 SET_REAL_EXP (r
, exp
- 1023 + 1);
3280 if (HOST_BITS_PER_LONG
== 32)
3282 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3283 r
->sig
[SIGSZ
-2] = image_lo
;
3286 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3290 const struct real_format ieee_double_format
=
3311 const struct real_format mips_double_format
=
3332 const struct real_format motorola_double_format
=
3353 /* IEEE extended real format. This comes in three flavors: Intel's as
3354 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3355 12- and 16-byte images may be big- or little endian; Motorola's is
3356 always big endian. */
3358 /* Helper subroutine which converts from the internal format to the
3359 12-byte little-endian Intel format. Functions below adjust this
3360 for the other possible formats. */
3362 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3363 const REAL_VALUE_TYPE
*r
)
3365 unsigned long image_hi
, sig_hi
, sig_lo
;
3366 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3368 image_hi
= r
->sign
<< 15;
3369 sig_hi
= sig_lo
= 0;
3381 /* Intel requires the explicit integer bit to be set, otherwise
3382 it considers the value a "pseudo-infinity". Motorola docs
3383 say it doesn't care. */
3384 sig_hi
= 0x80000000;
3389 sig_lo
= sig_hi
= 0xffffffff;
3399 if (fmt
->canonical_nan_lsbs_set
)
3401 sig_hi
= (1 << 30) - 1;
3402 sig_lo
= 0xffffffff;
3405 else if (HOST_BITS_PER_LONG
== 32)
3407 sig_hi
= r
->sig
[SIGSZ
-1];
3408 sig_lo
= r
->sig
[SIGSZ
-2];
3412 sig_lo
= r
->sig
[SIGSZ
-1];
3413 sig_hi
= sig_lo
>> 31 >> 1;
3414 sig_lo
&= 0xffffffff;
3416 if (r
->signalling
== fmt
->qnan_msb_set
)
3417 sig_hi
&= ~(1 << 30);
3420 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3423 /* Intel requires the explicit integer bit to be set, otherwise
3424 it considers the value a "pseudo-nan". Motorola docs say it
3426 sig_hi
|= 0x80000000;
3431 sig_lo
= sig_hi
= 0xffffffff;
3437 int exp
= REAL_EXP (r
);
3439 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3440 whereas the intermediate representation is 0.F x 2**exp.
3441 Which means we're off by one.
3443 Except for Motorola, which consider exp=0 and explicit
3444 integer bit set to continue to be normalized. In theory
3445 this discrepancy has been taken care of by the difference
3446 in fmt->emin in round_for_format. */
3453 gcc_assert (exp
>= 0);
3457 if (HOST_BITS_PER_LONG
== 32)
3459 sig_hi
= r
->sig
[SIGSZ
-1];
3460 sig_lo
= r
->sig
[SIGSZ
-2];
3464 sig_lo
= r
->sig
[SIGSZ
-1];
3465 sig_hi
= sig_lo
>> 31 >> 1;
3466 sig_lo
&= 0xffffffff;
3475 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3478 /* Convert from the internal format to the 12-byte Motorola format
3479 for an IEEE extended real. */
3481 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3482 const REAL_VALUE_TYPE
*r
)
3485 encode_ieee_extended (fmt
, intermed
, r
);
3487 if (r
->cl
== rvc_inf
)
3488 /* For infinity clear the explicit integer bit again, so that the
3489 format matches the canonical infinity generated by the FPU. */
3492 /* Motorola chips are assumed always to be big-endian. Also, the
3493 padding in a Motorola extended real goes between the exponent and
3494 the mantissa. At this point the mantissa is entirely within
3495 elements 0 and 1 of intermed, and the exponent entirely within
3496 element 2, so all we have to do is swap the order around, and
3497 shift element 2 left 16 bits. */
3498 buf
[0] = intermed
[2] << 16;
3499 buf
[1] = intermed
[1];
3500 buf
[2] = intermed
[0];
3503 /* Convert from the internal format to the 12-byte Intel format for
3504 an IEEE extended real. */
3506 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3507 const REAL_VALUE_TYPE
*r
)
3509 if (FLOAT_WORDS_BIG_ENDIAN
)
3511 /* All the padding in an Intel-format extended real goes at the high
3512 end, which in this case is after the mantissa, not the exponent.
3513 Therefore we must shift everything down 16 bits. */
3515 encode_ieee_extended (fmt
, intermed
, r
);
3516 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3517 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3518 buf
[2] = (intermed
[0] << 16);
3521 /* encode_ieee_extended produces what we want directly. */
3522 encode_ieee_extended (fmt
, buf
, r
);
3525 /* Convert from the internal format to the 16-byte Intel format for
3526 an IEEE extended real. */
3528 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3529 const REAL_VALUE_TYPE
*r
)
3531 /* All the padding in an Intel-format extended real goes at the high end. */
3532 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3536 /* As above, we have a helper function which converts from 12-byte
3537 little-endian Intel format to internal format. Functions below
3538 adjust for the other possible formats. */
3540 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3543 unsigned long image_hi
, sig_hi
, sig_lo
;
3547 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3548 sig_lo
&= 0xffffffff;
3549 sig_hi
&= 0xffffffff;
3550 image_hi
&= 0xffffffff;
3552 sign
= (image_hi
>> 15) & 1;
3553 exp
= image_hi
& 0x7fff;
3555 memset (r
, 0, sizeof (*r
));
3559 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3564 /* When the IEEE format contains a hidden bit, we know that
3565 it's zero at this point, and so shift up the significand
3566 and decrease the exponent to match. In this case, Motorola
3567 defines the explicit integer bit to be valid, so we don't
3568 know whether the msb is set or not. */
3569 SET_REAL_EXP (r
, fmt
->emin
);
3570 if (HOST_BITS_PER_LONG
== 32)
3572 r
->sig
[SIGSZ
-1] = sig_hi
;
3573 r
->sig
[SIGSZ
-2] = sig_lo
;
3576 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3580 else if (fmt
->has_signed_zero
)
3583 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3585 /* See above re "pseudo-infinities" and "pseudo-nans".
3586 Short summary is that the MSB will likely always be
3587 set, and that we don't care about it. */
3588 sig_hi
&= 0x7fffffff;
3590 if (sig_hi
|| sig_lo
)
3594 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3595 if (HOST_BITS_PER_LONG
== 32)
3597 r
->sig
[SIGSZ
-1] = sig_hi
;
3598 r
->sig
[SIGSZ
-2] = sig_lo
;
3601 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3613 SET_REAL_EXP (r
, exp
- 16383 + 1);
3614 if (HOST_BITS_PER_LONG
== 32)
3616 r
->sig
[SIGSZ
-1] = sig_hi
;
3617 r
->sig
[SIGSZ
-2] = sig_lo
;
3620 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3624 /* Convert from the internal format to the 12-byte Motorola format
3625 for an IEEE extended real. */
3627 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3632 /* Motorola chips are assumed always to be big-endian. Also, the
3633 padding in a Motorola extended real goes between the exponent and
3634 the mantissa; remove it. */
3635 intermed
[0] = buf
[2];
3636 intermed
[1] = buf
[1];
3637 intermed
[2] = (unsigned long)buf
[0] >> 16;
3639 decode_ieee_extended (fmt
, r
, intermed
);
3642 /* Convert from the internal format to the 12-byte Intel format for
3643 an IEEE extended real. */
3645 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3648 if (FLOAT_WORDS_BIG_ENDIAN
)
3650 /* All the padding in an Intel-format extended real goes at the high
3651 end, which in this case is after the mantissa, not the exponent.
3652 Therefore we must shift everything up 16 bits. */
3655 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3656 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3657 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3659 decode_ieee_extended (fmt
, r
, intermed
);
3662 /* decode_ieee_extended produces what we want directly. */
3663 decode_ieee_extended (fmt
, r
, buf
);
3666 /* Convert from the internal format to the 16-byte Intel format for
3667 an IEEE extended real. */
3669 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3672 /* All the padding in an Intel-format extended real goes at the high end. */
3673 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3676 const struct real_format ieee_extended_motorola_format
=
3678 encode_ieee_extended_motorola
,
3679 decode_ieee_extended_motorola
,
3697 const struct real_format ieee_extended_intel_96_format
=
3699 encode_ieee_extended_intel_96
,
3700 decode_ieee_extended_intel_96
,
3718 const struct real_format ieee_extended_intel_128_format
=
3720 encode_ieee_extended_intel_128
,
3721 decode_ieee_extended_intel_128
,
3739 /* The following caters to i386 systems that set the rounding precision
3740 to 53 bits instead of 64, e.g. FreeBSD. */
3741 const struct real_format ieee_extended_intel_96_round_53_format
=
3743 encode_ieee_extended_intel_96
,
3744 decode_ieee_extended_intel_96
,
3762 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3763 numbers whose sum is equal to the extended precision value. The number
3764 with greater magnitude is first. This format has the same magnitude
3765 range as an IEEE double precision value, but effectively 106 bits of
3766 significand precision. Infinity and NaN are represented by their IEEE
3767 double precision value stored in the first number, the second number is
3768 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3770 static void encode_ibm_extended (const struct real_format
*fmt
,
3771 long *, const REAL_VALUE_TYPE
*);
3772 static void decode_ibm_extended (const struct real_format
*,
3773 REAL_VALUE_TYPE
*, const long *);
3776 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3777 const REAL_VALUE_TYPE
*r
)
3779 REAL_VALUE_TYPE u
, normr
, v
;
3780 const struct real_format
*base_fmt
;
3782 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3784 /* Renormalize R before doing any arithmetic on it. */
3786 if (normr
.cl
== rvc_normal
)
3789 /* u = IEEE double precision portion of significand. */
3791 round_for_format (base_fmt
, &u
);
3792 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3794 if (u
.cl
== rvc_normal
)
3796 do_add (&v
, &normr
, &u
, 1);
3797 /* Call round_for_format since we might need to denormalize. */
3798 round_for_format (base_fmt
, &v
);
3799 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3803 /* Inf, NaN, 0 are all representable as doubles, so the
3804 least-significant part can be 0.0. */
3811 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3814 REAL_VALUE_TYPE u
, v
;
3815 const struct real_format
*base_fmt
;
3817 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3818 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3820 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3822 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3823 do_add (r
, &u
, &v
, 0);
3829 const struct real_format ibm_extended_format
=
3831 encode_ibm_extended
,
3832 decode_ibm_extended
,
3850 const struct real_format mips_extended_format
=
3852 encode_ibm_extended
,
3853 decode_ibm_extended
,
3872 /* IEEE quad precision format. */
3874 static void encode_ieee_quad (const struct real_format
*fmt
,
3875 long *, const REAL_VALUE_TYPE
*);
3876 static void decode_ieee_quad (const struct real_format
*,
3877 REAL_VALUE_TYPE
*, const long *);
3880 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3881 const REAL_VALUE_TYPE
*r
)
3883 unsigned long image3
, image2
, image1
, image0
, exp
;
3884 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3887 image3
= r
->sign
<< 31;
3892 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3901 image3
|= 32767 << 16;
3904 image3
|= 0x7fffffff;
3905 image2
= 0xffffffff;
3906 image1
= 0xffffffff;
3907 image0
= 0xffffffff;
3914 image3
|= 32767 << 16;
3918 if (fmt
->canonical_nan_lsbs_set
)
3921 image2
= image1
= image0
= 0xffffffff;
3924 else if (HOST_BITS_PER_LONG
== 32)
3929 image3
|= u
.sig
[3] & 0xffff;
3934 image1
= image0
>> 31 >> 1;
3936 image3
|= (image2
>> 31 >> 1) & 0xffff;
3937 image0
&= 0xffffffff;
3938 image2
&= 0xffffffff;
3940 if (r
->signalling
== fmt
->qnan_msb_set
)
3944 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3949 image3
|= 0x7fffffff;
3950 image2
= 0xffffffff;
3951 image1
= 0xffffffff;
3952 image0
= 0xffffffff;
3957 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3958 whereas the intermediate representation is 0.F x 2**exp.
3959 Which means we're off by one. */
3963 exp
= REAL_EXP (r
) + 16383 - 1;
3964 image3
|= exp
<< 16;
3966 if (HOST_BITS_PER_LONG
== 32)
3971 image3
|= u
.sig
[3] & 0xffff;
3976 image1
= image0
>> 31 >> 1;
3978 image3
|= (image2
>> 31 >> 1) & 0xffff;
3979 image0
&= 0xffffffff;
3980 image2
&= 0xffffffff;
3988 if (FLOAT_WORDS_BIG_ENDIAN
)
4005 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4008 unsigned long image3
, image2
, image1
, image0
;
4012 if (FLOAT_WORDS_BIG_ENDIAN
)
4026 image0
&= 0xffffffff;
4027 image1
&= 0xffffffff;
4028 image2
&= 0xffffffff;
4030 sign
= (image3
>> 31) & 1;
4031 exp
= (image3
>> 16) & 0x7fff;
4034 memset (r
, 0, sizeof (*r
));
4038 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4043 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4044 if (HOST_BITS_PER_LONG
== 32)
4053 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4054 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4059 else if (fmt
->has_signed_zero
)
4062 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4064 if (image3
| image2
| image1
| image0
)
4068 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4070 if (HOST_BITS_PER_LONG
== 32)
4079 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4080 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4082 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4094 SET_REAL_EXP (r
, exp
- 16383 + 1);
4096 if (HOST_BITS_PER_LONG
== 32)
4105 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4106 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4108 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4109 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4113 const struct real_format ieee_quad_format
=
4134 const struct real_format mips_quad_format
=
4155 /* Descriptions of VAX floating point formats can be found beginning at
4157 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4159 The thing to remember is that they're almost IEEE, except for word
4160 order, exponent bias, and the lack of infinities, nans, and denormals.
4162 We don't implement the H_floating format here, simply because neither
4163 the VAX or Alpha ports use it. */
4165 static void encode_vax_f (const struct real_format
*fmt
,
4166 long *, const REAL_VALUE_TYPE
*);
4167 static void decode_vax_f (const struct real_format
*,
4168 REAL_VALUE_TYPE
*, const long *);
4169 static void encode_vax_d (const struct real_format
*fmt
,
4170 long *, const REAL_VALUE_TYPE
*);
4171 static void decode_vax_d (const struct real_format
*,
4172 REAL_VALUE_TYPE
*, const long *);
4173 static void encode_vax_g (const struct real_format
*fmt
,
4174 long *, const REAL_VALUE_TYPE
*);
4175 static void decode_vax_g (const struct real_format
*,
4176 REAL_VALUE_TYPE
*, const long *);
4179 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4180 const REAL_VALUE_TYPE
*r
)
4182 unsigned long sign
, exp
, sig
, image
;
4184 sign
= r
->sign
<< 15;
4194 image
= 0xffff7fff | sign
;
4198 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4199 exp
= REAL_EXP (r
) + 128;
4201 image
= (sig
<< 16) & 0xffff0000;
4215 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4216 REAL_VALUE_TYPE
*r
, const long *buf
)
4218 unsigned long image
= buf
[0] & 0xffffffff;
4219 int exp
= (image
>> 7) & 0xff;
4221 memset (r
, 0, sizeof (*r
));
4226 r
->sign
= (image
>> 15) & 1;
4227 SET_REAL_EXP (r
, exp
- 128);
4229 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4230 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4235 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4236 const REAL_VALUE_TYPE
*r
)
4238 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4243 image0
= image1
= 0;
4248 image0
= 0xffff7fff | sign
;
4249 image1
= 0xffffffff;
4253 /* Extract the significand into straight hi:lo. */
4254 if (HOST_BITS_PER_LONG
== 64)
4256 image0
= r
->sig
[SIGSZ
-1];
4257 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4258 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4262 image0
= r
->sig
[SIGSZ
-1];
4263 image1
= r
->sig
[SIGSZ
-2];
4264 image1
= (image0
<< 24) | (image1
>> 8);
4265 image0
= (image0
>> 8) & 0xffffff;
4268 /* Rearrange the half-words of the significand to match the
4270 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4271 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4273 /* Add the sign and exponent. */
4275 image0
|= (REAL_EXP (r
) + 128) << 7;
4282 if (FLOAT_WORDS_BIG_ENDIAN
)
4283 buf
[0] = image1
, buf
[1] = image0
;
4285 buf
[0] = image0
, buf
[1] = image1
;
4289 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4290 REAL_VALUE_TYPE
*r
, const long *buf
)
4292 unsigned long image0
, image1
;
4295 if (FLOAT_WORDS_BIG_ENDIAN
)
4296 image1
= buf
[0], image0
= buf
[1];
4298 image0
= buf
[0], image1
= buf
[1];
4299 image0
&= 0xffffffff;
4300 image1
&= 0xffffffff;
4302 exp
= (image0
>> 7) & 0xff;
4304 memset (r
, 0, sizeof (*r
));
4309 r
->sign
= (image0
>> 15) & 1;
4310 SET_REAL_EXP (r
, exp
- 128);
4312 /* Rearrange the half-words of the external format into
4313 proper ascending order. */
4314 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4315 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4317 if (HOST_BITS_PER_LONG
== 64)
4319 image0
= (image0
<< 31 << 1) | image1
;
4322 r
->sig
[SIGSZ
-1] = image0
;
4326 r
->sig
[SIGSZ
-1] = image0
;
4327 r
->sig
[SIGSZ
-2] = image1
;
4328 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4329 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4335 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4336 const REAL_VALUE_TYPE
*r
)
4338 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4343 image0
= image1
= 0;
4348 image0
= 0xffff7fff | sign
;
4349 image1
= 0xffffffff;
4353 /* Extract the significand into straight hi:lo. */
4354 if (HOST_BITS_PER_LONG
== 64)
4356 image0
= r
->sig
[SIGSZ
-1];
4357 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4358 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4362 image0
= r
->sig
[SIGSZ
-1];
4363 image1
= r
->sig
[SIGSZ
-2];
4364 image1
= (image0
<< 21) | (image1
>> 11);
4365 image0
= (image0
>> 11) & 0xfffff;
4368 /* Rearrange the half-words of the significand to match the
4370 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4371 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4373 /* Add the sign and exponent. */
4375 image0
|= (REAL_EXP (r
) + 1024) << 4;
4382 if (FLOAT_WORDS_BIG_ENDIAN
)
4383 buf
[0] = image1
, buf
[1] = image0
;
4385 buf
[0] = image0
, buf
[1] = image1
;
4389 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4390 REAL_VALUE_TYPE
*r
, const long *buf
)
4392 unsigned long image0
, image1
;
4395 if (FLOAT_WORDS_BIG_ENDIAN
)
4396 image1
= buf
[0], image0
= buf
[1];
4398 image0
= buf
[0], image1
= buf
[1];
4399 image0
&= 0xffffffff;
4400 image1
&= 0xffffffff;
4402 exp
= (image0
>> 4) & 0x7ff;
4404 memset (r
, 0, sizeof (*r
));
4409 r
->sign
= (image0
>> 15) & 1;
4410 SET_REAL_EXP (r
, exp
- 1024);
4412 /* Rearrange the half-words of the external format into
4413 proper ascending order. */
4414 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4415 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4417 if (HOST_BITS_PER_LONG
== 64)
4419 image0
= (image0
<< 31 << 1) | image1
;
4422 r
->sig
[SIGSZ
-1] = image0
;
4426 r
->sig
[SIGSZ
-1] = image0
;
4427 r
->sig
[SIGSZ
-2] = image1
;
4428 lshift_significand (r
, r
, 64 - 53);
4429 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4434 const struct real_format vax_f_format
=
4455 const struct real_format vax_d_format
=
4476 const struct real_format vax_g_format
=
4497 /* Encode real R into a single precision DFP value in BUF. */
4499 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4500 long *buf ATTRIBUTE_UNUSED
,
4501 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4503 encode_decimal32 (fmt
, buf
, r
);
4506 /* Decode a single precision DFP value in BUF into a real R. */
4508 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4509 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4510 const long *buf ATTRIBUTE_UNUSED
)
4512 decode_decimal32 (fmt
, r
, buf
);
4515 /* Encode real R into a double precision DFP value in BUF. */
4517 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4518 long *buf ATTRIBUTE_UNUSED
,
4519 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4521 encode_decimal64 (fmt
, buf
, r
);
4524 /* Decode a double precision DFP value in BUF into a real R. */
4526 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4527 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4528 const long *buf ATTRIBUTE_UNUSED
)
4530 decode_decimal64 (fmt
, r
, buf
);
4533 /* Encode real R into a quad precision DFP value in BUF. */
4535 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4536 long *buf ATTRIBUTE_UNUSED
,
4537 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4539 encode_decimal128 (fmt
, buf
, r
);
4542 /* Decode a quad precision DFP value in BUF into a real R. */
4544 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4545 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4546 const long *buf ATTRIBUTE_UNUSED
)
4548 decode_decimal128 (fmt
, r
, buf
);
4551 /* Single precision decimal floating point (IEEE 754). */
4552 const struct real_format decimal_single_format
=
4554 encode_decimal_single
,
4555 decode_decimal_single
,
4573 /* Double precision decimal floating point (IEEE 754). */
4574 const struct real_format decimal_double_format
=
4576 encode_decimal_double
,
4577 decode_decimal_double
,
4595 /* Quad precision decimal floating point (IEEE 754). */
4596 const struct real_format decimal_quad_format
=
4598 encode_decimal_quad
,
4599 decode_decimal_quad
,
4617 /* Encode half-precision floats. This routine is used both for the IEEE
4618 ARM alternative encodings. */
4620 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4621 const REAL_VALUE_TYPE
*r
)
4623 unsigned long image
, sig
, exp
;
4624 unsigned long sign
= r
->sign
;
4625 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4628 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4646 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4647 if (r
->signalling
== fmt
->qnan_msb_set
)
4662 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4663 whereas the intermediate representation is 0.F x 2**exp.
4664 Which means we're off by one. */
4668 exp
= REAL_EXP (r
) + 15 - 1;
4680 /* Decode half-precision floats. This routine is used both for the IEEE
4681 ARM alternative encodings. */
4683 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4686 unsigned long image
= buf
[0] & 0xffff;
4687 bool sign
= (image
>> 15) & 1;
4688 int exp
= (image
>> 10) & 0x1f;
4690 memset (r
, 0, sizeof (*r
));
4691 image
<<= HOST_BITS_PER_LONG
- 11;
4696 if (image
&& fmt
->has_denorm
)
4700 SET_REAL_EXP (r
, -14);
4701 r
->sig
[SIGSZ
-1] = image
<< 1;
4704 else if (fmt
->has_signed_zero
)
4707 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4713 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4714 ^ fmt
->qnan_msb_set
);
4715 r
->sig
[SIGSZ
-1] = image
;
4727 SET_REAL_EXP (r
, exp
- 15 + 1);
4728 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4732 /* Half-precision format, as specified in IEEE 754R. */
4733 const struct real_format ieee_half_format
=
4754 /* ARM's alternative half-precision format, similar to IEEE but with
4755 no reserved exponent value for NaNs and infinities; rather, it just
4756 extends the range of exponents by one. */
4757 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,
4823 /* Calculate X raised to the integer exponent N in mode MODE and store
4824 the result in R. Return true if the result may be inexact due to
4825 loss of precision. The algorithm is the classic "left-to-right binary
4826 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4827 Algorithms", "The Art of Computer Programming", Volume 2. */
4830 real_powi (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4831 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4833 unsigned HOST_WIDE_INT bit
;
4835 bool inexact
= false;
4847 /* Don't worry about overflow, from now on n is unsigned. */
4855 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4856 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4860 inexact
|= do_multiply (&t
, &t
, &t
);
4862 inexact
|= do_multiply (&t
, &t
, x
);
4870 inexact
|= do_divide (&t
, &dconst1
, &t
);
4872 real_convert (r
, mode
, &t
);
4876 /* Round X to the nearest integer not larger in absolute value, i.e.
4877 towards zero, placing the result in R in mode MODE. */
4880 real_trunc (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4881 const REAL_VALUE_TYPE
*x
)
4883 do_fix_trunc (r
, x
);
4884 if (mode
!= VOIDmode
)
4885 real_convert (r
, mode
, r
);
4888 /* Round X to the largest integer not greater in value, i.e. round
4889 down, placing the result in R in mode MODE. */
4892 real_floor (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4893 const REAL_VALUE_TYPE
*x
)
4897 do_fix_trunc (&t
, x
);
4898 if (! real_identical (&t
, x
) && x
->sign
)
4899 do_add (&t
, &t
, &dconstm1
, 0);
4900 if (mode
!= VOIDmode
)
4901 real_convert (r
, mode
, &t
);
4906 /* Round X to the smallest integer not less then argument, i.e. round
4907 up, placing the result in R in mode MODE. */
4910 real_ceil (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4911 const REAL_VALUE_TYPE
*x
)
4915 do_fix_trunc (&t
, x
);
4916 if (! real_identical (&t
, x
) && ! x
->sign
)
4917 do_add (&t
, &t
, &dconst1
, 0);
4918 if (mode
!= VOIDmode
)
4919 real_convert (r
, mode
, &t
);
4924 /* Round X to the nearest integer, but round halfway cases away from
4928 real_round (REAL_VALUE_TYPE
*r
, machine_mode mode
,
4929 const REAL_VALUE_TYPE
*x
)
4931 do_add (r
, x
, &dconsthalf
, x
->sign
);
4932 do_fix_trunc (r
, r
);
4933 if (mode
!= VOIDmode
)
4934 real_convert (r
, mode
, r
);
4937 /* Set the sign of R to the sign of X. */
4940 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4945 /* Check whether the real constant value given is an integer. */
4948 real_isinteger (const REAL_VALUE_TYPE
*c
, machine_mode mode
)
4950 REAL_VALUE_TYPE cint
;
4952 real_trunc (&cint
, mode
, c
);
4953 return real_identical (c
, &cint
);
4956 /* Write into BUF the maximum representable finite floating-point
4957 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4958 float string. LEN is the size of BUF, and the buffer must be large
4959 enough to contain the resulting string. */
4962 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
4967 strcpy (buf
, "0x0.");
4969 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
4972 *p
++ = "08ce"[n
- i
];
4973 sprintf (p
, "p%d", fmt
->emax
);
4974 if (fmt
->pnan
< fmt
->p
)
4976 /* This is an IBM extended double format made up of two IEEE
4977 doubles. The value of the long double is the sum of the
4978 values of the two parts. The most significant part is
4979 required to be the value of the long double rounded to the
4980 nearest double. Rounding means we need a slightly smaller
4981 value for LDBL_MAX. */
4982 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
4985 gcc_assert (strlen (buf
) < len
);
4988 /* True if mode M has a NaN representation and
4989 the treatment of NaN operands is important. */
4992 HONOR_NANS (machine_mode m
)
4994 return MODE_HAS_NANS (m
) && !flag_finite_math_only
;
4998 HONOR_NANS (const_tree t
)
5000 return HONOR_NANS (element_mode (t
));
5004 HONOR_NANS (const_rtx x
)
5006 return HONOR_NANS (GET_MODE (x
));
5009 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5012 HONOR_SNANS (machine_mode m
)
5014 return flag_signaling_nans
&& HONOR_NANS (m
);
5018 HONOR_SNANS (const_tree t
)
5020 return HONOR_SNANS (element_mode (t
));
5024 HONOR_SNANS (const_rtx x
)
5026 return HONOR_SNANS (GET_MODE (x
));
5029 /* As for HONOR_NANS, but true if the mode can represent infinity and
5030 the treatment of infinite values is important. */
5033 HONOR_INFINITIES (machine_mode m
)
5035 return MODE_HAS_INFINITIES (m
) && !flag_finite_math_only
;
5039 HONOR_INFINITIES (const_tree t
)
5041 return HONOR_INFINITIES (element_mode (t
));
5045 HONOR_INFINITIES (const_rtx x
)
5047 return HONOR_INFINITIES (GET_MODE (x
));
5050 /* Like HONOR_NANS, but true if the given mode distinguishes between
5051 positive and negative zero, and the sign of zero is important. */
5054 HONOR_SIGNED_ZEROS (machine_mode m
)
5056 return MODE_HAS_SIGNED_ZEROS (m
) && flag_signed_zeros
;
5060 HONOR_SIGNED_ZEROS (const_tree t
)
5062 return HONOR_SIGNED_ZEROS (element_mode (t
));
5066 HONOR_SIGNED_ZEROS (const_rtx x
)
5068 return HONOR_SIGNED_ZEROS (GET_MODE (x
));
5071 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5072 and the rounding mode is important. */
5075 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m
)
5077 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m
) && flag_rounding_math
;
5081 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t
)
5083 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t
));
5087 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x
)
5089 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x
));