1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002,
3 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by Stephen L. Moshier (moshier@world.std.com).
5 Re-written by Richard Henderson <rth@redhat.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
34 /* The floating point model used internally is not exactly IEEE 754
35 compliant, and close to the description in the ISO C99 standard,
36 section 5.2.4.2.2 Characteristics of floating types.
40 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
44 b = base or radix, here always 2
46 p = precision (the number of base-b digits in the significand)
47 f_k = the digits of the significand.
49 We differ from typical IEEE 754 encodings in that the entire
50 significand is fractional. Normalized significands are in the
53 A requirement of the model is that P be larger than the largest
54 supported target floating-point type by at least 2 bits. This gives
55 us proper rounding when we truncate to the target type. In addition,
56 E must be large enough to hold the smallest supported denormal number
59 Both of these requirements are easily satisfied. The largest target
60 significand is 113 bits; we store at least 160. The smallest
61 denormal number fits in 17 exponent bits; we store 26.
63 Note that the decimal string conversion routines are sensitive to
64 rounding errors. Since the raw arithmetic routines do not themselves
65 have guard digits or rounding, the computation of 10**exp can
66 accumulate more than a few digits of error. The previous incarnation
67 of real.c successfully used a 144-bit fraction; given the current
68 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits. */
71 /* Used to classify two numbers simultaneously. */
72 #define CLASS2(A, B) ((A) << 2 | (B))
74 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
75 #error "Some constant folding done by hand to avoid shift count warnings"
78 static void get_zero (REAL_VALUE_TYPE
*, int);
79 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
80 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
81 static void get_inf (REAL_VALUE_TYPE
*, int);
82 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
83 const REAL_VALUE_TYPE
*, unsigned int);
84 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
86 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
88 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
89 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
90 const REAL_VALUE_TYPE
*);
91 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
92 const REAL_VALUE_TYPE
*, int);
93 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
94 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
95 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
96 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
97 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
98 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
99 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
100 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
101 const REAL_VALUE_TYPE
*);
102 static void normalize (REAL_VALUE_TYPE
*);
104 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
105 const REAL_VALUE_TYPE
*, int);
106 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
107 const REAL_VALUE_TYPE
*);
108 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
109 const REAL_VALUE_TYPE
*);
110 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
111 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
113 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, REAL_VALUE_TYPE
*);
114 static void decimal_from_integer (REAL_VALUE_TYPE
*);
115 static void decimal_integer_string (char *, const REAL_VALUE_TYPE
*,
118 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
119 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
120 static const REAL_VALUE_TYPE
* real_digit (int);
121 static void times_pten (REAL_VALUE_TYPE
*, int);
123 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
125 /* Initialize R with a positive zero. */
128 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
130 memset (r
, 0, sizeof (*r
));
134 /* Initialize R with the canonical quiet NaN. */
137 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
139 memset (r
, 0, sizeof (*r
));
146 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
148 memset (r
, 0, sizeof (*r
));
156 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
158 memset (r
, 0, sizeof (*r
));
164 /* Right-shift the significand of A by N bits; put the result in the
165 significand of R. If any one bits are shifted out, return true. */
168 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
171 unsigned long sticky
= 0;
172 unsigned int i
, ofs
= 0;
174 if (n
>= HOST_BITS_PER_LONG
)
176 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
178 n
&= HOST_BITS_PER_LONG
- 1;
183 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
184 for (i
= 0; i
< SIGSZ
; ++i
)
187 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
188 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
189 << (HOST_BITS_PER_LONG
- n
)));
194 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
195 r
->sig
[i
] = a
->sig
[ofs
+ i
];
196 for (; i
< SIGSZ
; ++i
)
203 /* Right-shift the significand of A by N bits; put the result in the
207 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
210 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
212 n
&= HOST_BITS_PER_LONG
- 1;
215 for (i
= 0; i
< SIGSZ
; ++i
)
218 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
219 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
220 << (HOST_BITS_PER_LONG
- n
)));
225 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
226 r
->sig
[i
] = a
->sig
[ofs
+ i
];
227 for (; i
< SIGSZ
; ++i
)
232 /* Left-shift the significand of A by N bits; put the result in the
236 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
239 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
241 n
&= HOST_BITS_PER_LONG
- 1;
244 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
245 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
246 for (; i
< SIGSZ
; ++i
)
247 r
->sig
[SIGSZ
-1-i
] = 0;
250 for (i
= 0; i
< SIGSZ
; ++i
)
253 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
254 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
255 >> (HOST_BITS_PER_LONG
- n
)));
259 /* Likewise, but N is specialized to 1. */
262 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
266 for (i
= SIGSZ
- 1; i
> 0; --i
)
267 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
268 r
->sig
[0] = a
->sig
[0] << 1;
271 /* Add the significands of A and B, placing the result in R. Return
272 true if there was carry out of the most significant word. */
275 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
276 const REAL_VALUE_TYPE
*b
)
281 for (i
= 0; i
< SIGSZ
; ++i
)
283 unsigned long ai
= a
->sig
[i
];
284 unsigned long ri
= ai
+ b
->sig
[i
];
300 /* Subtract the significands of A and B, placing the result in R. CARRY is
301 true if there's a borrow incoming to the least significant word.
302 Return true if there was borrow out of the most significant word. */
305 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
306 const REAL_VALUE_TYPE
*b
, int carry
)
310 for (i
= 0; i
< SIGSZ
; ++i
)
312 unsigned long ai
= a
->sig
[i
];
313 unsigned long ri
= ai
- b
->sig
[i
];
329 /* Negate the significand A, placing the result in R. */
332 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
337 for (i
= 0; i
< SIGSZ
; ++i
)
339 unsigned long ri
, ai
= a
->sig
[i
];
358 /* Compare significands. Return tri-state vs zero. */
361 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
365 for (i
= SIGSZ
- 1; i
>= 0; --i
)
367 unsigned long ai
= a
->sig
[i
];
368 unsigned long bi
= b
->sig
[i
];
379 /* Return true if A is nonzero. */
382 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
386 for (i
= SIGSZ
- 1; i
>= 0; --i
)
393 /* Set bit N of the significand of R. */
396 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
398 r
->sig
[n
/ HOST_BITS_PER_LONG
]
399 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
402 /* Clear bit N of the significand of R. */
405 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
407 r
->sig
[n
/ HOST_BITS_PER_LONG
]
408 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
411 /* Test bit N of the significand of R. */
414 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
416 /* ??? Compiler bug here if we return this expression directly.
417 The conversion to bool strips the "&1" and we wind up testing
418 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
419 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
423 /* Clear bits 0..N-1 of the significand of R. */
426 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
428 int i
, w
= n
/ HOST_BITS_PER_LONG
;
430 for (i
= 0; i
< w
; ++i
)
433 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
436 /* Divide the significands of A and B, placing the result in R. Return
437 true if the division was inexact. */
440 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
441 const REAL_VALUE_TYPE
*b
)
444 int i
, bit
= SIGNIFICAND_BITS
- 1;
445 unsigned long msb
, inexact
;
448 memset (r
->sig
, 0, sizeof (r
->sig
));
454 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
455 lshift_significand_1 (&u
, &u
);
457 if (msb
|| cmp_significands (&u
, b
) >= 0)
459 sub_significands (&u
, &u
, b
, 0);
460 set_significand_bit (r
, bit
);
465 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
471 /* Adjust the exponent and significand of R such that the most
472 significant bit is set. We underflow to zero and overflow to
473 infinity here, without denormals. (The intermediate representation
474 exponent is large enough to handle target denormals normalized.) */
477 normalize (REAL_VALUE_TYPE
*r
)
485 /* Find the first word that is nonzero. */
486 for (i
= SIGSZ
- 1; i
>= 0; i
--)
488 shift
+= HOST_BITS_PER_LONG
;
492 /* Zero significand flushes to zero. */
500 /* Find the first bit that is nonzero. */
502 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
508 exp
= REAL_EXP (r
) - shift
;
510 get_inf (r
, r
->sign
);
511 else if (exp
< -MAX_EXP
)
512 get_zero (r
, r
->sign
);
515 SET_REAL_EXP (r
, exp
);
516 lshift_significand (r
, r
, shift
);
521 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
522 result may be inexact due to a loss of precision. */
525 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
526 const REAL_VALUE_TYPE
*b
, int subtract_p
)
530 bool inexact
= false;
532 /* Determine if we need to add or subtract. */
534 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
536 switch (CLASS2 (a
->cl
, b
->cl
))
538 case CLASS2 (rvc_zero
, rvc_zero
):
539 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
540 get_zero (r
, sign
& !subtract_p
);
543 case CLASS2 (rvc_zero
, rvc_normal
):
544 case CLASS2 (rvc_zero
, rvc_inf
):
545 case CLASS2 (rvc_zero
, rvc_nan
):
547 case CLASS2 (rvc_normal
, rvc_nan
):
548 case CLASS2 (rvc_inf
, rvc_nan
):
549 case CLASS2 (rvc_nan
, rvc_nan
):
550 /* ANY + NaN = NaN. */
551 case CLASS2 (rvc_normal
, rvc_inf
):
554 r
->sign
= sign
^ subtract_p
;
557 case CLASS2 (rvc_normal
, rvc_zero
):
558 case CLASS2 (rvc_inf
, rvc_zero
):
559 case CLASS2 (rvc_nan
, rvc_zero
):
561 case CLASS2 (rvc_nan
, rvc_normal
):
562 case CLASS2 (rvc_nan
, rvc_inf
):
563 /* NaN + ANY = NaN. */
564 case CLASS2 (rvc_inf
, rvc_normal
):
569 case CLASS2 (rvc_inf
, rvc_inf
):
571 /* Inf - Inf = NaN. */
572 get_canonical_qnan (r
, 0);
574 /* Inf + Inf = Inf. */
578 case CLASS2 (rvc_normal
, rvc_normal
):
585 /* Swap the arguments such that A has the larger exponent. */
586 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
589 const REAL_VALUE_TYPE
*t
;
596 /* If the exponents are not identical, we need to shift the
597 significand of B down. */
600 /* If the exponents are too far apart, the significands
601 do not overlap, which makes the subtraction a noop. */
602 if (dexp
>= SIGNIFICAND_BITS
)
609 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
615 if (sub_significands (r
, a
, b
, inexact
))
617 /* We got a borrow out of the subtraction. That means that
618 A and B had the same exponent, and B had the larger
619 significand. We need to swap the sign and negate the
622 neg_significand (r
, r
);
627 if (add_significands (r
, a
, b
))
629 /* We got carry out of the addition. This means we need to
630 shift the significand back down one bit and increase the
632 inexact
|= sticky_rshift_significand (r
, r
, 1);
633 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
644 SET_REAL_EXP (r
, exp
);
645 /* Zero out the remaining fields. */
650 /* Re-normalize the result. */
653 /* Special case: if the subtraction results in zero, the result
655 if (r
->cl
== rvc_zero
)
658 r
->sig
[0] |= inexact
;
663 /* Calculate R = A * B. Return true if the result may be inexact. */
666 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
667 const REAL_VALUE_TYPE
*b
)
669 REAL_VALUE_TYPE u
, t
, *rr
;
670 unsigned int i
, j
, k
;
671 int sign
= a
->sign
^ b
->sign
;
672 bool inexact
= false;
674 switch (CLASS2 (a
->cl
, b
->cl
))
676 case CLASS2 (rvc_zero
, rvc_zero
):
677 case CLASS2 (rvc_zero
, rvc_normal
):
678 case CLASS2 (rvc_normal
, rvc_zero
):
679 /* +-0 * ANY = 0 with appropriate sign. */
683 case CLASS2 (rvc_zero
, rvc_nan
):
684 case CLASS2 (rvc_normal
, rvc_nan
):
685 case CLASS2 (rvc_inf
, rvc_nan
):
686 case CLASS2 (rvc_nan
, rvc_nan
):
687 /* ANY * NaN = NaN. */
692 case CLASS2 (rvc_nan
, rvc_zero
):
693 case CLASS2 (rvc_nan
, rvc_normal
):
694 case CLASS2 (rvc_nan
, rvc_inf
):
695 /* NaN * ANY = NaN. */
700 case CLASS2 (rvc_zero
, rvc_inf
):
701 case CLASS2 (rvc_inf
, rvc_zero
):
703 get_canonical_qnan (r
, sign
);
706 case CLASS2 (rvc_inf
, rvc_inf
):
707 case CLASS2 (rvc_normal
, rvc_inf
):
708 case CLASS2 (rvc_inf
, rvc_normal
):
709 /* Inf * Inf = Inf, R * Inf = Inf */
713 case CLASS2 (rvc_normal
, rvc_normal
):
720 if (r
== a
|| r
== b
)
726 /* Collect all the partial products. Since we don't have sure access
727 to a widening multiply, we split each long into two half-words.
729 Consider the long-hand form of a four half-word multiplication:
739 We construct partial products of the widened half-word products
740 that are known to not overlap, e.g. DF+DH. Each such partial
741 product is given its proper exponent, which allows us to sum them
742 and obtain the finished product. */
744 for (i
= 0; i
< SIGSZ
* 2; ++i
)
746 unsigned long ai
= a
->sig
[i
/ 2];
748 ai
>>= HOST_BITS_PER_LONG
/ 2;
750 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
755 for (j
= 0; j
< 2; ++j
)
757 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
758 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
767 /* Would underflow to zero, which we shouldn't bother adding. */
772 memset (&u
, 0, sizeof (u
));
774 SET_REAL_EXP (&u
, exp
);
776 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
778 unsigned long bi
= b
->sig
[k
/ 2];
780 bi
>>= HOST_BITS_PER_LONG
/ 2;
782 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
784 u
.sig
[k
/ 2] = ai
* bi
;
788 inexact
|= do_add (rr
, rr
, &u
, 0);
799 /* Calculate R = A / B. Return true if the result may be inexact. */
802 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
803 const REAL_VALUE_TYPE
*b
)
805 int exp
, sign
= a
->sign
^ b
->sign
;
806 REAL_VALUE_TYPE t
, *rr
;
809 switch (CLASS2 (a
->cl
, b
->cl
))
811 case CLASS2 (rvc_zero
, rvc_zero
):
813 case CLASS2 (rvc_inf
, rvc_inf
):
814 /* Inf / Inf = NaN. */
815 get_canonical_qnan (r
, sign
);
818 case CLASS2 (rvc_zero
, rvc_normal
):
819 case CLASS2 (rvc_zero
, rvc_inf
):
821 case CLASS2 (rvc_normal
, rvc_inf
):
826 case CLASS2 (rvc_normal
, rvc_zero
):
828 case CLASS2 (rvc_inf
, rvc_zero
):
833 case CLASS2 (rvc_zero
, rvc_nan
):
834 case CLASS2 (rvc_normal
, rvc_nan
):
835 case CLASS2 (rvc_inf
, rvc_nan
):
836 case CLASS2 (rvc_nan
, rvc_nan
):
837 /* ANY / NaN = NaN. */
842 case CLASS2 (rvc_nan
, rvc_zero
):
843 case CLASS2 (rvc_nan
, rvc_normal
):
844 case CLASS2 (rvc_nan
, rvc_inf
):
845 /* NaN / ANY = NaN. */
850 case CLASS2 (rvc_inf
, rvc_normal
):
855 case CLASS2 (rvc_normal
, rvc_normal
):
862 if (r
== a
|| r
== b
)
867 /* Make sure all fields in the result are initialized. */
872 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
883 SET_REAL_EXP (rr
, exp
);
885 inexact
= div_significands (rr
, a
, b
);
887 /* Re-normalize the result. */
889 rr
->sig
[0] |= inexact
;
897 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
898 one of the two operands is a NaN. */
901 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
906 switch (CLASS2 (a
->cl
, b
->cl
))
908 case CLASS2 (rvc_zero
, rvc_zero
):
909 /* Sign of zero doesn't matter for compares. */
912 case CLASS2 (rvc_normal
, rvc_zero
):
913 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
915 return decimal_do_compare (a
, b
, nan_result
);
917 case CLASS2 (rvc_inf
, rvc_zero
):
918 case CLASS2 (rvc_inf
, rvc_normal
):
919 return (a
->sign
? -1 : 1);
921 case CLASS2 (rvc_inf
, rvc_inf
):
922 return -a
->sign
- -b
->sign
;
924 case CLASS2 (rvc_zero
, rvc_normal
):
925 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
927 return decimal_do_compare (a
, b
, nan_result
);
929 case CLASS2 (rvc_zero
, rvc_inf
):
930 case CLASS2 (rvc_normal
, rvc_inf
):
931 return (b
->sign
? 1 : -1);
933 case CLASS2 (rvc_zero
, rvc_nan
):
934 case CLASS2 (rvc_normal
, rvc_nan
):
935 case CLASS2 (rvc_inf
, rvc_nan
):
936 case CLASS2 (rvc_nan
, rvc_nan
):
937 case CLASS2 (rvc_nan
, rvc_zero
):
938 case CLASS2 (rvc_nan
, rvc_normal
):
939 case CLASS2 (rvc_nan
, rvc_inf
):
942 case CLASS2 (rvc_normal
, rvc_normal
):
949 if (a
->sign
!= b
->sign
)
950 return -a
->sign
- -b
->sign
;
952 if (a
->decimal
|| b
->decimal
)
953 return decimal_do_compare (a
, b
, nan_result
);
955 if (REAL_EXP (a
) > REAL_EXP (b
))
957 else if (REAL_EXP (a
) < REAL_EXP (b
))
960 ret
= cmp_significands (a
, b
);
962 return (a
->sign
? -ret
: ret
);
965 /* Return A truncated to an integral value toward zero. */
968 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
982 decimal_do_fix_trunc (r
, a
);
985 if (REAL_EXP (r
) <= 0)
986 get_zero (r
, r
->sign
);
987 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
988 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
996 /* Perform the binary or unary operation described by CODE.
997 For a unary operation, leave OP1 NULL. This function returns
998 true if the result may be inexact due to loss of precision. */
1001 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
1002 const REAL_VALUE_TYPE
*op1
)
1004 enum tree_code code
= (enum tree_code
) icode
;
1006 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1007 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1012 return do_add (r
, op0
, op1
, 0);
1015 return do_add (r
, op0
, op1
, 1);
1018 return do_multiply (r
, op0
, op1
);
1021 return do_divide (r
, op0
, op1
);
1024 if (op1
->cl
== rvc_nan
)
1026 else if (do_compare (op0
, op1
, -1) < 0)
1033 if (op1
->cl
== rvc_nan
)
1035 else if (do_compare (op0
, op1
, 1) < 0)
1051 case FIX_TRUNC_EXPR
:
1052 do_fix_trunc (r
, op0
);
1062 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1065 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1070 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1073 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1078 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1079 const REAL_VALUE_TYPE
*op1
)
1081 enum tree_code code
= (enum tree_code
) icode
;
1086 return do_compare (op0
, op1
, 1) < 0;
1088 return do_compare (op0
, op1
, 1) <= 0;
1090 return do_compare (op0
, op1
, -1) > 0;
1092 return do_compare (op0
, op1
, -1) >= 0;
1094 return do_compare (op0
, op1
, -1) == 0;
1096 return do_compare (op0
, op1
, -1) != 0;
1097 case UNORDERED_EXPR
:
1098 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1100 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1102 return do_compare (op0
, op1
, -1) < 0;
1104 return do_compare (op0
, op1
, -1) <= 0;
1106 return do_compare (op0
, op1
, 1) > 0;
1108 return do_compare (op0
, op1
, 1) >= 0;
1110 return do_compare (op0
, op1
, 0) == 0;
1112 return do_compare (op0
, op1
, 0) != 0;
1119 /* Return floor log2(R). */
1122 real_exponent (const REAL_VALUE_TYPE
*r
)
1130 return (unsigned int)-1 >> 1;
1132 return REAL_EXP (r
);
1138 /* R = OP0 * 2**EXP. */
1141 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1152 exp
+= REAL_EXP (op0
);
1154 get_inf (r
, r
->sign
);
1155 else if (exp
< -MAX_EXP
)
1156 get_zero (r
, r
->sign
);
1158 SET_REAL_EXP (r
, exp
);
1166 /* Determine whether a floating-point value X is infinite. */
1169 real_isinf (const REAL_VALUE_TYPE
*r
)
1171 return (r
->cl
== rvc_inf
);
1174 /* Determine whether a floating-point value X is a NaN. */
1177 real_isnan (const REAL_VALUE_TYPE
*r
)
1179 return (r
->cl
== rvc_nan
);
1182 /* Determine whether a floating-point value X is finite. */
1185 real_isfinite (const REAL_VALUE_TYPE
*r
)
1187 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1190 /* Determine whether a floating-point value X is negative. */
1193 real_isneg (const REAL_VALUE_TYPE
*r
)
1198 /* Determine whether a floating-point value X is minus zero. */
1201 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1203 return r
->sign
&& r
->cl
== rvc_zero
;
1206 /* Compare two floating-point objects for bitwise identity. */
1209 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1215 if (a
->sign
!= b
->sign
)
1225 if (a
->decimal
!= b
->decimal
)
1227 if (REAL_EXP (a
) != REAL_EXP (b
))
1232 if (a
->signalling
!= b
->signalling
)
1234 /* The significand is ignored for canonical NaNs. */
1235 if (a
->canonical
|| b
->canonical
)
1236 return a
->canonical
== b
->canonical
;
1243 for (i
= 0; i
< SIGSZ
; ++i
)
1244 if (a
->sig
[i
] != b
->sig
[i
])
1250 /* Try to change R into its exact multiplicative inverse in machine
1251 mode MODE. Return true if successful. */
1254 exact_real_inverse (enum machine_mode mode
, REAL_VALUE_TYPE
*r
)
1256 const REAL_VALUE_TYPE
*one
= real_digit (1);
1260 if (r
->cl
!= rvc_normal
)
1263 /* Check for a power of two: all significand bits zero except the MSB. */
1264 for (i
= 0; i
< SIGSZ
-1; ++i
)
1267 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1270 /* Find the inverse and truncate to the required mode. */
1271 do_divide (&u
, one
, r
);
1272 real_convert (&u
, mode
, &u
);
1274 /* The rounding may have overflowed. */
1275 if (u
.cl
!= rvc_normal
)
1277 for (i
= 0; i
< SIGSZ
-1; ++i
)
1280 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1287 /* Return true if arithmetic on values in IMODE that were promoted
1288 from values in TMODE is equivalent to direct arithmetic on values
1292 real_can_shorten_arithmetic (enum machine_mode imode
, enum machine_mode tmode
)
1294 const struct real_format
*tfmt
, *ifmt
;
1295 tfmt
= REAL_MODE_FORMAT (tmode
);
1296 ifmt
= REAL_MODE_FORMAT (imode
);
1297 /* These conditions are conservative rather than trying to catch the
1298 exact boundary conditions; the main case to allow is IEEE float
1300 return (ifmt
->b
== tfmt
->b
1301 && ifmt
->p
> 2 * tfmt
->p
1302 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1303 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1304 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1305 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1306 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1307 && (ifmt
->has_sign_dependent_rounding
1308 == tfmt
->has_sign_dependent_rounding
)
1309 && ifmt
->has_nans
>= tfmt
->has_nans
1310 && ifmt
->has_inf
>= tfmt
->has_inf
1311 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1312 && !MODE_COMPOSITE_P (tmode
)
1313 && !MODE_COMPOSITE_P (imode
));
1316 /* Render R as an integer. */
1319 real_to_integer (const REAL_VALUE_TYPE
*r
)
1321 unsigned HOST_WIDE_INT i
;
1332 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1339 return decimal_real_to_integer (r
);
1341 if (REAL_EXP (r
) <= 0)
1343 /* Only force overflow for unsigned overflow. Signed overflow is
1344 undefined, so it doesn't matter what we return, and some callers
1345 expect to be able to use this routine for both signed and
1346 unsigned conversions. */
1347 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1350 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1351 i
= r
->sig
[SIGSZ
-1];
1354 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1355 i
= r
->sig
[SIGSZ
-1];
1356 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1357 i
|= r
->sig
[SIGSZ
-2];
1360 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1371 /* Likewise, but to an integer pair, HI+LOW. */
1374 real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
1375 const REAL_VALUE_TYPE
*r
)
1378 HOST_WIDE_INT low
, high
;
1391 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1404 decimal_real_to_integer2 (plow
, phigh
, r
);
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
> 2*HOST_BITS_PER_WIDE_INT
)
1418 rshift_significand (&t
, r
, 2*HOST_BITS_PER_WIDE_INT
- exp
);
1419 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1421 high
= t
.sig
[SIGSZ
-1];
1422 low
= t
.sig
[SIGSZ
-2];
1426 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
);
1427 high
= t
.sig
[SIGSZ
-1];
1428 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1429 high
|= t
.sig
[SIGSZ
-2];
1431 low
= t
.sig
[SIGSZ
-3];
1432 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1433 low
|= t
.sig
[SIGSZ
-4];
1441 low
= -low
, high
= ~high
;
1453 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1454 of NUM / DEN. Return the quotient and place the remainder in NUM.
1455 It is expected that NUM / DEN are close enough that the quotient is
1458 static unsigned long
1459 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1461 unsigned long q
, msb
;
1462 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1471 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1473 lshift_significand_1 (num
, num
);
1475 if (msb
|| cmp_significands (num
, den
) >= 0)
1477 sub_significands (num
, num
, den
, 0);
1481 while (--expn
>= expd
);
1483 SET_REAL_EXP (num
, expd
);
1489 /* Render R as a decimal floating point constant. Emit DIGITS significant
1490 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1491 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1492 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1493 to a string that, when parsed back in mode MODE, yields the same value. */
1495 #define M_LOG10_2 0.30102999566398119521
1498 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1499 size_t buf_size
, size_t digits
,
1500 int crop_trailing_zeros
, enum machine_mode mode
)
1502 const struct real_format
*fmt
= NULL
;
1503 const REAL_VALUE_TYPE
*one
, *ten
;
1504 REAL_VALUE_TYPE r
, pten
, u
, v
;
1505 int dec_exp
, cmp_one
, digit
;
1507 char *p
, *first
, *last
;
1511 if (mode
!= VOIDmode
)
1513 fmt
= REAL_MODE_FORMAT (mode
);
1521 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1526 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1529 /* ??? Print the significand as well, if not canonical? */
1530 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1531 (r_orig
->signalling
? 'S' : 'Q'));
1539 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1543 /* Bound the number of digits printed by the size of the representation. */
1544 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1545 if (digits
== 0 || digits
> max_digits
)
1546 digits
= max_digits
;
1548 /* Estimate the decimal exponent, and compute the length of the string it
1549 will print as. Be conservative and add one to account for possible
1550 overflow or rounding error. */
1551 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1552 for (max_digits
= 1; dec_exp
; max_digits
++)
1555 /* Bound the number of digits printed by the size of the output buffer. */
1556 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1557 gcc_assert (max_digits
<= buf_size
);
1558 if (digits
> max_digits
)
1559 digits
= max_digits
;
1561 one
= real_digit (1);
1562 ten
= ten_to_ptwo (0);
1570 cmp_one
= do_compare (&r
, one
, 0);
1575 /* Number is greater than one. Convert significand to an integer
1576 and strip trailing decimal zeros. */
1579 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1581 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1582 m
= floor_log2 (max_digits
);
1584 /* Iterate over the bits of the possible powers of 10 that might
1585 be present in U and eliminate them. That is, if we find that
1586 10**2**M divides U evenly, keep the division and increase
1592 do_divide (&t
, &u
, ten_to_ptwo (m
));
1593 do_fix_trunc (&v
, &t
);
1594 if (cmp_significands (&v
, &t
) == 0)
1602 /* Revert the scaling to integer that we performed earlier. */
1603 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1604 - (SIGNIFICAND_BITS
- 1));
1607 /* Find power of 10. Do this by dividing out 10**2**M when
1608 this is larger than the current remainder. Fill PTEN with
1609 the power of 10 that we compute. */
1610 if (REAL_EXP (&r
) > 0)
1612 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1615 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1616 if (do_compare (&u
, ptentwo
, 0) >= 0)
1618 do_divide (&u
, &u
, ptentwo
);
1619 do_multiply (&pten
, &pten
, ptentwo
);
1626 /* We managed to divide off enough tens in the above reduction
1627 loop that we've now got a negative exponent. Fall into the
1628 less-than-one code to compute the proper value for PTEN. */
1635 /* Number is less than one. Pad significand with leading
1641 /* Stop if we'd shift bits off the bottom. */
1645 do_multiply (&u
, &v
, ten
);
1647 /* Stop if we're now >= 1. */
1648 if (REAL_EXP (&u
) > 0)
1656 /* Find power of 10. Do this by multiplying in P=10**2**M when
1657 the current remainder is smaller than 1/P. Fill PTEN with the
1658 power of 10 that we compute. */
1659 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1662 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1663 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1665 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1667 do_multiply (&v
, &v
, ptentwo
);
1668 do_multiply (&pten
, &pten
, ptentwo
);
1674 /* Invert the positive power of 10 that we've collected so far. */
1675 do_divide (&pten
, one
, &pten
);
1683 /* At this point, PTEN should contain the nearest power of 10 smaller
1684 than R, such that this division produces the first digit.
1686 Using a divide-step primitive that returns the complete integral
1687 remainder avoids the rounding error that would be produced if
1688 we were to use do_divide here and then simply multiply by 10 for
1689 each subsequent digit. */
1691 digit
= rtd_divmod (&r
, &pten
);
1693 /* Be prepared for error in that division via underflow ... */
1694 if (digit
== 0 && cmp_significand_0 (&r
))
1696 /* Multiply by 10 and try again. */
1697 do_multiply (&r
, &r
, ten
);
1698 digit
= rtd_divmod (&r
, &pten
);
1700 gcc_assert (digit
!= 0);
1703 /* ... or overflow. */
1713 gcc_assert (digit
<= 10);
1717 /* Generate subsequent digits. */
1718 while (--digits
> 0)
1720 do_multiply (&r
, &r
, ten
);
1721 digit
= rtd_divmod (&r
, &pten
);
1726 /* Generate one more digit with which to do rounding. */
1727 do_multiply (&r
, &r
, ten
);
1728 digit
= rtd_divmod (&r
, &pten
);
1730 /* Round the result. */
1731 if (fmt
&& fmt
->round_towards_zero
)
1733 /* If the format uses round towards zero when parsing the string
1734 back in, we need to always round away from zero here. */
1735 if (cmp_significand_0 (&r
))
1737 round_up
= digit
> 0;
1743 /* Round to nearest. If R is nonzero there are additional
1744 nonzero digits to be extracted. */
1745 if (cmp_significand_0 (&r
))
1747 /* Round to even. */
1748 else if ((p
[-1] - '0') & 1)
1752 round_up
= digit
> 5;
1769 /* Carry out of the first digit. This means we had all 9's and
1770 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1778 /* Insert the decimal point. */
1779 first
[0] = first
[1];
1782 /* If requested, drop trailing zeros. Never crop past "1.0". */
1783 if (crop_trailing_zeros
)
1784 while (last
> first
+ 3 && last
[-1] == '0')
1787 /* Append the exponent. */
1788 sprintf (last
, "e%+d", dec_exp
);
1790 #ifdef ENABLE_CHECKING
1791 /* Verify that we can read the original value back in. */
1792 if (mode
!= VOIDmode
)
1794 real_from_string (&r
, str
);
1795 real_convert (&r
, mode
, &r
);
1796 gcc_assert (real_identical (&r
, r_orig
));
1801 /* Likewise, except always uses round-to-nearest. */
1804 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1805 size_t digits
, int crop_trailing_zeros
)
1807 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1808 digits
, crop_trailing_zeros
, VOIDmode
);
1811 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1812 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1813 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1814 strip trailing zeros. */
1817 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1818 size_t digits
, int crop_trailing_zeros
)
1820 int i
, j
, exp
= REAL_EXP (r
);
1833 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1836 /* ??? Print the significand as well, if not canonical? */
1837 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1838 (r
->signalling
? 'S' : 'Q'));
1846 /* Hexadecimal format for decimal floats is not interesting. */
1847 strcpy (str
, "N/A");
1852 digits
= SIGNIFICAND_BITS
/ 4;
1854 /* Bound the number of digits printed by the size of the output buffer. */
1856 sprintf (exp_buf
, "p%+d", exp
);
1857 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1858 gcc_assert (max_digits
<= buf_size
);
1859 if (digits
> max_digits
)
1860 digits
= max_digits
;
1871 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1872 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1874 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1880 if (crop_trailing_zeros
)
1881 while (p
> first
+ 1 && p
[-1] == '0')
1884 sprintf (p
, "p%+d", exp
);
1887 /* Initialize R from a decimal or hexadecimal string. The string is
1888 assumed to have been syntax checked already. Return -1 if the
1889 value underflows, +1 if overflows, and 0 otherwise. */
1892 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1904 else if (*str
== '+')
1907 if (!strncmp (str
, "QNaN", 4))
1909 get_canonical_qnan (r
, sign
);
1912 else if (!strncmp (str
, "SNaN", 4))
1914 get_canonical_snan (r
, sign
);
1917 else if (!strncmp (str
, "Inf", 3))
1923 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1925 /* Hexadecimal floating point. */
1926 int pos
= SIGNIFICAND_BITS
- 4, d
;
1934 d
= hex_value (*str
);
1939 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1940 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1944 /* Ensure correct rounding by setting last bit if there is
1945 a subsequent nonzero digit. */
1953 if (pos
== SIGNIFICAND_BITS
- 4)
1960 d
= hex_value (*str
);
1965 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1966 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1970 /* Ensure correct rounding by setting last bit if there is
1971 a subsequent nonzero digit. */
1977 /* If the mantissa is zero, ignore the exponent. */
1978 if (!cmp_significand_0 (r
))
1981 if (*str
== 'p' || *str
== 'P')
1983 bool exp_neg
= false;
1991 else if (*str
== '+')
1995 while (ISDIGIT (*str
))
2001 /* Overflowed the exponent. */
2016 SET_REAL_EXP (r
, exp
);
2022 /* Decimal floating point. */
2023 const REAL_VALUE_TYPE
*ten
= ten_to_ptwo (0);
2028 while (ISDIGIT (*str
))
2031 do_multiply (r
, r
, ten
);
2033 do_add (r
, r
, real_digit (d
), 0);
2038 if (r
->cl
== rvc_zero
)
2043 while (ISDIGIT (*str
))
2046 do_multiply (r
, r
, ten
);
2048 do_add (r
, r
, real_digit (d
), 0);
2053 /* If the mantissa is zero, ignore the exponent. */
2054 if (r
->cl
== rvc_zero
)
2057 if (*str
== 'e' || *str
== 'E')
2059 bool exp_neg
= false;
2067 else if (*str
== '+')
2071 while (ISDIGIT (*str
))
2077 /* Overflowed the exponent. */
2091 times_pten (r
, exp
);
2110 /* Legacy. Similar, but return the result directly. */
2113 real_from_string2 (const char *s
, enum machine_mode mode
)
2117 real_from_string (&r
, s
);
2118 if (mode
!= VOIDmode
)
2119 real_convert (&r
, mode
, &r
);
2124 /* Initialize R from string S and desired MODE. */
2127 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, enum machine_mode mode
)
2129 if (DECIMAL_FLOAT_MODE_P (mode
))
2130 decimal_real_from_string (r
, s
);
2132 real_from_string (r
, s
);
2134 if (mode
!= VOIDmode
)
2135 real_convert (r
, mode
, r
);
2138 /* Initialize R from the integer pair HIGH+LOW. */
2141 real_from_integer (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2142 unsigned HOST_WIDE_INT low
, HOST_WIDE_INT high
,
2145 if (low
== 0 && high
== 0)
2149 memset (r
, 0, sizeof (*r
));
2151 r
->sign
= high
< 0 && !unsigned_p
;
2152 SET_REAL_EXP (r
, 2 * HOST_BITS_PER_WIDE_INT
);
2163 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2165 r
->sig
[SIGSZ
-1] = high
;
2166 r
->sig
[SIGSZ
-2] = low
;
2170 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2171 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2172 r
->sig
[SIGSZ
-2] = high
;
2173 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2174 r
->sig
[SIGSZ
-4] = low
;
2180 if (DECIMAL_FLOAT_MODE_P (mode
))
2181 decimal_from_integer (r
);
2182 else if (mode
!= VOIDmode
)
2183 real_convert (r
, mode
, r
);
2186 /* Render R, an integral value, as a floating point constant with no
2187 specified exponent. */
2190 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2193 int dec_exp
, digit
, digits
;
2194 REAL_VALUE_TYPE r
, pten
;
2200 if (r
.cl
== rvc_zero
)
2209 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2210 digits
= dec_exp
+ 1;
2211 gcc_assert ((digits
+ 2) < (int)buf_size
);
2213 pten
= *real_digit (1);
2214 times_pten (&pten
, dec_exp
);
2220 digit
= rtd_divmod (&r
, &pten
);
2221 gcc_assert (digit
>= 0 && digit
<= 9);
2223 while (--digits
> 0)
2226 digit
= rtd_divmod (&r
, &pten
);
2233 /* Convert a real with an integral value to decimal float. */
2236 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2240 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2241 decimal_real_from_string (r
, str
);
2244 /* Returns 10**2**N. */
2246 static const REAL_VALUE_TYPE
*
2249 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2251 gcc_assert (n
>= 0);
2252 gcc_assert (n
< EXP_BITS
);
2254 if (tens
[n
].cl
== rvc_zero
)
2256 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2258 HOST_WIDE_INT t
= 10;
2261 for (i
= 0; i
< n
; ++i
)
2264 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2268 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2269 do_multiply (&tens
[n
], t
, t
);
2276 /* Returns 10**(-2**N). */
2278 static const REAL_VALUE_TYPE
*
2279 ten_to_mptwo (int n
)
2281 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2283 gcc_assert (n
>= 0);
2284 gcc_assert (n
< EXP_BITS
);
2286 if (tens
[n
].cl
== rvc_zero
)
2287 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2294 static const REAL_VALUE_TYPE
*
2297 static REAL_VALUE_TYPE num
[10];
2299 gcc_assert (n
>= 0);
2300 gcc_assert (n
<= 9);
2302 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2303 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2308 /* Multiply R by 10**EXP. */
2311 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2313 REAL_VALUE_TYPE pten
, *rr
;
2314 bool negative
= (exp
< 0);
2320 pten
= *real_digit (1);
2326 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2328 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2331 do_divide (r
, r
, &pten
);
2334 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2336 const REAL_VALUE_TYPE
*
2339 static REAL_VALUE_TYPE value
;
2341 /* Initialize mathematical constants for constant folding builtins.
2342 These constants need to be given to at least 160 bits precision. */
2343 if (value
.cl
== rvc_zero
)
2346 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2347 mpfr_set_ui (m
, 1, GMP_RNDN
);
2348 mpfr_exp (m
, m
, GMP_RNDN
);
2349 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2356 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2358 const REAL_VALUE_TYPE
*
2359 dconst_third_ptr (void)
2361 static REAL_VALUE_TYPE value
;
2363 /* Initialize mathematical constants for constant folding builtins.
2364 These constants need to be given to at least 160 bits precision. */
2365 if (value
.cl
== rvc_zero
)
2367 real_arithmetic (&value
, RDIV_EXPR
, &dconst1
, real_digit (3));
2372 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2374 const REAL_VALUE_TYPE
*
2375 dconst_sqrt2_ptr (void)
2377 static REAL_VALUE_TYPE value
;
2379 /* Initialize mathematical constants for constant folding builtins.
2380 These constants need to be given to at least 160 bits precision. */
2381 if (value
.cl
== rvc_zero
)
2384 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2385 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2386 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2392 /* Fills R with +Inf. */
2395 real_inf (REAL_VALUE_TYPE
*r
)
2400 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2401 we force a QNaN, else we force an SNaN. The string, if not empty,
2402 is parsed as a number and placed in the significand. Return true
2403 if the string was successfully parsed. */
2406 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2407 enum machine_mode mode
)
2409 const struct real_format
*fmt
;
2411 fmt
= REAL_MODE_FORMAT (mode
);
2417 get_canonical_qnan (r
, 0);
2419 get_canonical_snan (r
, 0);
2425 memset (r
, 0, sizeof (*r
));
2428 /* Parse akin to strtol into the significand of R. */
2430 while (ISSPACE (*str
))
2434 else if (*str
== '+')
2439 if (*str
== 'x' || *str
== 'X')
2448 while ((d
= hex_value (*str
)) < base
)
2455 lshift_significand (r
, r
, 3);
2458 lshift_significand (r
, r
, 4);
2461 lshift_significand_1 (&u
, r
);
2462 lshift_significand (r
, r
, 3);
2463 add_significands (r
, r
, &u
);
2471 add_significands (r
, r
, &u
);
2476 /* Must have consumed the entire string for success. */
2480 /* Shift the significand into place such that the bits
2481 are in the most significant bits for the format. */
2482 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2484 /* Our MSB is always unset for NaNs. */
2485 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2487 /* Force quiet or signalling NaN. */
2488 r
->signalling
= !quiet
;
2494 /* Fills R with the largest finite value representable in mode MODE.
2495 If SIGN is nonzero, R is set to the most negative finite value. */
2498 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
2500 const struct real_format
*fmt
;
2503 fmt
= REAL_MODE_FORMAT (mode
);
2505 memset (r
, 0, sizeof (*r
));
2508 decimal_real_maxval (r
, sign
, mode
);
2513 SET_REAL_EXP (r
, fmt
->emax
);
2515 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2516 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2517 clear_significand_below (r
, np2
);
2519 if (fmt
->pnan
< fmt
->p
)
2520 /* This is an IBM extended double format made up of two IEEE
2521 doubles. The value of the long double is the sum of the
2522 values of the two parts. The most significant part is
2523 required to be the value of the long double rounded to the
2524 nearest double. Rounding means we need a slightly smaller
2525 value for LDBL_MAX. */
2526 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2530 /* Fills R with 2**N. */
2533 real_2expN (REAL_VALUE_TYPE
*r
, int n
, enum machine_mode fmode
)
2535 memset (r
, 0, sizeof (*r
));
2540 else if (n
< -MAX_EXP
)
2545 SET_REAL_EXP (r
, n
);
2546 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2548 if (DECIMAL_FLOAT_MODE_P (fmode
))
2549 decimal_real_convert (r
, fmode
, r
);
2554 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2558 bool round_up
= false;
2564 decimal_round_for_format (fmt
, r
);
2567 /* FIXME. We can come here via fp_easy_constant
2568 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2569 investigated whether this convert needs to be here, or
2570 something else is missing. */
2571 decimal_real_convert (r
, DFmode
, r
);
2575 emin2m1
= fmt
->emin
- 1;
2578 np2
= SIGNIFICAND_BITS
- p2
;
2582 get_zero (r
, r
->sign
);
2584 if (!fmt
->has_signed_zero
)
2589 get_inf (r
, r
->sign
);
2594 clear_significand_below (r
, np2
);
2604 /* Check the range of the exponent. If we're out of range,
2605 either underflow or overflow. */
2606 if (REAL_EXP (r
) > emax2
)
2608 else if (REAL_EXP (r
) <= emin2m1
)
2612 if (!fmt
->has_denorm
)
2614 /* Don't underflow completely until we've had a chance to round. */
2615 if (REAL_EXP (r
) < emin2m1
)
2620 diff
= emin2m1
- REAL_EXP (r
) + 1;
2624 /* De-normalize the significand. */
2625 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2626 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2630 if (!fmt
->round_towards_zero
)
2632 /* There are P2 true significand bits, followed by one guard bit,
2633 followed by one sticky bit, followed by stuff. Fold nonzero
2634 stuff into the sticky bit. */
2635 unsigned long sticky
;
2639 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2640 sticky
|= r
->sig
[i
];
2642 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2644 guard
= test_significand_bit (r
, np2
- 1);
2645 lsb
= test_significand_bit (r
, np2
);
2647 /* Round to even. */
2648 round_up
= guard
&& (sticky
|| lsb
);
2655 set_significand_bit (&u
, np2
);
2657 if (add_significands (r
, r
, &u
))
2659 /* Overflow. Means the significand had been all ones, and
2660 is now all zeros. Need to increase the exponent, and
2661 possibly re-normalize it. */
2662 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2663 if (REAL_EXP (r
) > emax2
)
2665 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2669 /* Catch underflow that we deferred until after rounding. */
2670 if (REAL_EXP (r
) <= emin2m1
)
2673 /* Clear out trailing garbage. */
2674 clear_significand_below (r
, np2
);
2677 /* Extend or truncate to a new mode. */
2680 real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2681 const REAL_VALUE_TYPE
*a
)
2683 const struct real_format
*fmt
;
2685 fmt
= REAL_MODE_FORMAT (mode
);
2690 if (a
->decimal
|| fmt
->b
== 10)
2691 decimal_real_convert (r
, mode
, a
);
2693 round_for_format (fmt
, r
);
2695 /* round_for_format de-normalizes denormals. Undo just that part. */
2696 if (r
->cl
== rvc_normal
)
2700 /* Legacy. Likewise, except return the struct directly. */
2703 real_value_truncate (enum machine_mode mode
, REAL_VALUE_TYPE a
)
2706 real_convert (&r
, mode
, &a
);
2710 /* Return true if truncating to MODE is exact. */
2713 exact_real_truncate (enum machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2715 const struct real_format
*fmt
;
2719 fmt
= REAL_MODE_FORMAT (mode
);
2722 /* Don't allow conversion to denormals. */
2723 emin2m1
= fmt
->emin
- 1;
2724 if (REAL_EXP (a
) <= emin2m1
)
2727 /* After conversion to the new mode, the value must be identical. */
2728 real_convert (&t
, mode
, a
);
2729 return real_identical (&t
, a
);
2732 /* Write R to the given target format. Place the words of the result
2733 in target word order in BUF. There are always 32 bits in each
2734 long, no matter the size of the host long.
2736 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2739 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2740 const struct real_format
*fmt
)
2746 round_for_format (fmt
, &r
);
2750 (*fmt
->encode
) (fmt
, buf
, &r
);
2755 /* Similar, but look up the format from MODE. */
2758 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, enum machine_mode mode
)
2760 const struct real_format
*fmt
;
2762 fmt
= REAL_MODE_FORMAT (mode
);
2765 return real_to_target_fmt (buf
, r
, fmt
);
2768 /* Read R from the given target format. Read the words of the result
2769 in target word order in BUF. There are always 32 bits in each
2770 long, no matter the size of the host long. */
2773 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2774 const struct real_format
*fmt
)
2776 (*fmt
->decode
) (fmt
, r
, buf
);
2779 /* Similar, but look up the format from MODE. */
2782 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, enum machine_mode mode
)
2784 const struct real_format
*fmt
;
2786 fmt
= REAL_MODE_FORMAT (mode
);
2789 (*fmt
->decode
) (fmt
, r
, buf
);
2792 /* Return the number of bits of the largest binary value that the
2793 significand of MODE will hold. */
2794 /* ??? Legacy. Should get access to real_format directly. */
2797 significand_size (enum machine_mode mode
)
2799 const struct real_format
*fmt
;
2801 fmt
= REAL_MODE_FORMAT (mode
);
2807 /* Return the size in bits of the largest binary value that can be
2808 held by the decimal coefficient for this mode. This is one more
2809 than the number of bits required to hold the largest coefficient
2811 double log2_10
= 3.3219281;
2812 return fmt
->p
* log2_10
;
2817 /* Return a hash value for the given real value. */
2818 /* ??? The "unsigned int" return value is intended to be hashval_t,
2819 but I didn't want to pull hashtab.h into real.h. */
2822 real_hash (const REAL_VALUE_TYPE
*r
)
2827 h
= r
->cl
| (r
->sign
<< 2);
2835 h
|= REAL_EXP (r
) << 3;
2840 h
^= (unsigned int)-1;
2849 if (sizeof(unsigned long) > sizeof(unsigned int))
2850 for (i
= 0; i
< SIGSZ
; ++i
)
2852 unsigned long s
= r
->sig
[i
];
2853 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2856 for (i
= 0; i
< SIGSZ
; ++i
)
2862 /* IEEE single-precision format. */
2864 static void encode_ieee_single (const struct real_format
*fmt
,
2865 long *, const REAL_VALUE_TYPE
*);
2866 static void decode_ieee_single (const struct real_format
*,
2867 REAL_VALUE_TYPE
*, const long *);
2870 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2871 const REAL_VALUE_TYPE
*r
)
2873 unsigned long image
, sig
, exp
;
2874 unsigned long sign
= r
->sign
;
2875 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2878 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2889 image
|= 0x7fffffff;
2896 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2897 if (r
->signalling
== fmt
->qnan_msb_set
)
2908 image
|= 0x7fffffff;
2912 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2913 whereas the intermediate representation is 0.F x 2**exp.
2914 Which means we're off by one. */
2918 exp
= REAL_EXP (r
) + 127 - 1;
2931 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2934 unsigned long image
= buf
[0] & 0xffffffff;
2935 bool sign
= (image
>> 31) & 1;
2936 int exp
= (image
>> 23) & 0xff;
2938 memset (r
, 0, sizeof (*r
));
2939 image
<<= HOST_BITS_PER_LONG
- 24;
2944 if (image
&& fmt
->has_denorm
)
2948 SET_REAL_EXP (r
, -126);
2949 r
->sig
[SIGSZ
-1] = image
<< 1;
2952 else if (fmt
->has_signed_zero
)
2955 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2961 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2962 ^ fmt
->qnan_msb_set
);
2963 r
->sig
[SIGSZ
-1] = image
;
2975 SET_REAL_EXP (r
, exp
- 127 + 1);
2976 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2980 const struct real_format ieee_single_format
=
3001 const struct real_format mips_single_format
=
3022 const struct real_format motorola_single_format
=
3043 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3044 single precision with the following differences:
3045 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3047 - NaNs are not supported.
3048 - The range of non-zero numbers in binary is
3049 (001)[1.]000...000 to (255)[1.]111...111.
3050 - Denormals can be represented, but are treated as +0.0 when
3051 used as an operand and are never generated as a result.
3052 - -0.0 can be represented, but a zero result is always +0.0.
3053 - the only supported rounding mode is trunction (towards zero). */
3054 const struct real_format spu_single_format
=
3075 /* IEEE double-precision format. */
3077 static void encode_ieee_double (const struct real_format
*fmt
,
3078 long *, const REAL_VALUE_TYPE
*);
3079 static void decode_ieee_double (const struct real_format
*,
3080 REAL_VALUE_TYPE
*, const long *);
3083 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3084 const REAL_VALUE_TYPE
*r
)
3086 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3087 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3089 image_hi
= r
->sign
<< 31;
3092 if (HOST_BITS_PER_LONG
== 64)
3094 sig_hi
= r
->sig
[SIGSZ
-1];
3095 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3096 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3100 sig_hi
= r
->sig
[SIGSZ
-1];
3101 sig_lo
= r
->sig
[SIGSZ
-2];
3102 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3103 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3113 image_hi
|= 2047 << 20;
3116 image_hi
|= 0x7fffffff;
3117 image_lo
= 0xffffffff;
3126 if (fmt
->canonical_nan_lsbs_set
)
3128 sig_hi
= (1 << 19) - 1;
3129 sig_lo
= 0xffffffff;
3137 if (r
->signalling
== fmt
->qnan_msb_set
)
3138 sig_hi
&= ~(1 << 19);
3141 if (sig_hi
== 0 && sig_lo
== 0)
3144 image_hi
|= 2047 << 20;
3150 image_hi
|= 0x7fffffff;
3151 image_lo
= 0xffffffff;
3156 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3157 whereas the intermediate representation is 0.F x 2**exp.
3158 Which means we're off by one. */
3162 exp
= REAL_EXP (r
) + 1023 - 1;
3163 image_hi
|= exp
<< 20;
3172 if (FLOAT_WORDS_BIG_ENDIAN
)
3173 buf
[0] = image_hi
, buf
[1] = image_lo
;
3175 buf
[0] = image_lo
, buf
[1] = image_hi
;
3179 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3182 unsigned long image_hi
, image_lo
;
3186 if (FLOAT_WORDS_BIG_ENDIAN
)
3187 image_hi
= buf
[0], image_lo
= buf
[1];
3189 image_lo
= buf
[0], image_hi
= buf
[1];
3190 image_lo
&= 0xffffffff;
3191 image_hi
&= 0xffffffff;
3193 sign
= (image_hi
>> 31) & 1;
3194 exp
= (image_hi
>> 20) & 0x7ff;
3196 memset (r
, 0, sizeof (*r
));
3198 image_hi
<<= 32 - 21;
3199 image_hi
|= image_lo
>> 21;
3200 image_hi
&= 0x7fffffff;
3201 image_lo
<<= 32 - 21;
3205 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3209 SET_REAL_EXP (r
, -1022);
3210 if (HOST_BITS_PER_LONG
== 32)
3212 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3214 r
->sig
[SIGSZ
-1] = image_hi
;
3215 r
->sig
[SIGSZ
-2] = image_lo
;
3219 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3220 r
->sig
[SIGSZ
-1] = image_hi
;
3224 else if (fmt
->has_signed_zero
)
3227 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3229 if (image_hi
|| image_lo
)
3233 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3234 if (HOST_BITS_PER_LONG
== 32)
3236 r
->sig
[SIGSZ
-1] = image_hi
;
3237 r
->sig
[SIGSZ
-2] = image_lo
;
3240 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3252 SET_REAL_EXP (r
, exp
- 1023 + 1);
3253 if (HOST_BITS_PER_LONG
== 32)
3255 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3256 r
->sig
[SIGSZ
-2] = image_lo
;
3259 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3263 const struct real_format ieee_double_format
=
3284 const struct real_format mips_double_format
=
3305 const struct real_format motorola_double_format
=
3326 /* IEEE extended real format. This comes in three flavors: Intel's as
3327 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3328 12- and 16-byte images may be big- or little endian; Motorola's is
3329 always big endian. */
3331 /* Helper subroutine which converts from the internal format to the
3332 12-byte little-endian Intel format. Functions below adjust this
3333 for the other possible formats. */
3335 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3336 const REAL_VALUE_TYPE
*r
)
3338 unsigned long image_hi
, sig_hi
, sig_lo
;
3339 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3341 image_hi
= r
->sign
<< 15;
3342 sig_hi
= sig_lo
= 0;
3354 /* Intel requires the explicit integer bit to be set, otherwise
3355 it considers the value a "pseudo-infinity". Motorola docs
3356 say it doesn't care. */
3357 sig_hi
= 0x80000000;
3362 sig_lo
= sig_hi
= 0xffffffff;
3372 if (fmt
->canonical_nan_lsbs_set
)
3374 sig_hi
= (1 << 30) - 1;
3375 sig_lo
= 0xffffffff;
3378 else if (HOST_BITS_PER_LONG
== 32)
3380 sig_hi
= r
->sig
[SIGSZ
-1];
3381 sig_lo
= r
->sig
[SIGSZ
-2];
3385 sig_lo
= r
->sig
[SIGSZ
-1];
3386 sig_hi
= sig_lo
>> 31 >> 1;
3387 sig_lo
&= 0xffffffff;
3389 if (r
->signalling
== fmt
->qnan_msb_set
)
3390 sig_hi
&= ~(1 << 30);
3393 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3396 /* Intel requires the explicit integer bit to be set, otherwise
3397 it considers the value a "pseudo-nan". Motorola docs say it
3399 sig_hi
|= 0x80000000;
3404 sig_lo
= sig_hi
= 0xffffffff;
3410 int exp
= REAL_EXP (r
);
3412 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3413 whereas the intermediate representation is 0.F x 2**exp.
3414 Which means we're off by one.
3416 Except for Motorola, which consider exp=0 and explicit
3417 integer bit set to continue to be normalized. In theory
3418 this discrepancy has been taken care of by the difference
3419 in fmt->emin in round_for_format. */
3426 gcc_assert (exp
>= 0);
3430 if (HOST_BITS_PER_LONG
== 32)
3432 sig_hi
= r
->sig
[SIGSZ
-1];
3433 sig_lo
= r
->sig
[SIGSZ
-2];
3437 sig_lo
= r
->sig
[SIGSZ
-1];
3438 sig_hi
= sig_lo
>> 31 >> 1;
3439 sig_lo
&= 0xffffffff;
3448 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3451 /* Convert from the internal format to the 12-byte Motorola format
3452 for an IEEE extended real. */
3454 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3455 const REAL_VALUE_TYPE
*r
)
3458 encode_ieee_extended (fmt
, intermed
, r
);
3460 /* Motorola chips are assumed always to be big-endian. Also, the
3461 padding in a Motorola extended real goes between the exponent and
3462 the mantissa. At this point the mantissa is entirely within
3463 elements 0 and 1 of intermed, and the exponent entirely within
3464 element 2, so all we have to do is swap the order around, and
3465 shift element 2 left 16 bits. */
3466 buf
[0] = intermed
[2] << 16;
3467 buf
[1] = intermed
[1];
3468 buf
[2] = intermed
[0];
3471 /* Convert from the internal format to the 12-byte Intel format for
3472 an IEEE extended real. */
3474 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3475 const REAL_VALUE_TYPE
*r
)
3477 if (FLOAT_WORDS_BIG_ENDIAN
)
3479 /* All the padding in an Intel-format extended real goes at the high
3480 end, which in this case is after the mantissa, not the exponent.
3481 Therefore we must shift everything down 16 bits. */
3483 encode_ieee_extended (fmt
, intermed
, r
);
3484 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3485 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3486 buf
[2] = (intermed
[0] << 16);
3489 /* encode_ieee_extended produces what we want directly. */
3490 encode_ieee_extended (fmt
, buf
, r
);
3493 /* Convert from the internal format to the 16-byte Intel format for
3494 an IEEE extended real. */
3496 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3497 const REAL_VALUE_TYPE
*r
)
3499 /* All the padding in an Intel-format extended real goes at the high end. */
3500 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3504 /* As above, we have a helper function which converts from 12-byte
3505 little-endian Intel format to internal format. Functions below
3506 adjust for the other possible formats. */
3508 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3511 unsigned long image_hi
, sig_hi
, sig_lo
;
3515 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3516 sig_lo
&= 0xffffffff;
3517 sig_hi
&= 0xffffffff;
3518 image_hi
&= 0xffffffff;
3520 sign
= (image_hi
>> 15) & 1;
3521 exp
= image_hi
& 0x7fff;
3523 memset (r
, 0, sizeof (*r
));
3527 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3532 /* When the IEEE format contains a hidden bit, we know that
3533 it's zero at this point, and so shift up the significand
3534 and decrease the exponent to match. In this case, Motorola
3535 defines the explicit integer bit to be valid, so we don't
3536 know whether the msb is set or not. */
3537 SET_REAL_EXP (r
, fmt
->emin
);
3538 if (HOST_BITS_PER_LONG
== 32)
3540 r
->sig
[SIGSZ
-1] = sig_hi
;
3541 r
->sig
[SIGSZ
-2] = sig_lo
;
3544 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3548 else if (fmt
->has_signed_zero
)
3551 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3553 /* See above re "pseudo-infinities" and "pseudo-nans".
3554 Short summary is that the MSB will likely always be
3555 set, and that we don't care about it. */
3556 sig_hi
&= 0x7fffffff;
3558 if (sig_hi
|| sig_lo
)
3562 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3563 if (HOST_BITS_PER_LONG
== 32)
3565 r
->sig
[SIGSZ
-1] = sig_hi
;
3566 r
->sig
[SIGSZ
-2] = sig_lo
;
3569 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3581 SET_REAL_EXP (r
, exp
- 16383 + 1);
3582 if (HOST_BITS_PER_LONG
== 32)
3584 r
->sig
[SIGSZ
-1] = sig_hi
;
3585 r
->sig
[SIGSZ
-2] = sig_lo
;
3588 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3592 /* Convert from the internal format to the 12-byte Motorola format
3593 for an IEEE extended real. */
3595 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3600 /* Motorola chips are assumed always to be big-endian. Also, the
3601 padding in a Motorola extended real goes between the exponent and
3602 the mantissa; remove it. */
3603 intermed
[0] = buf
[2];
3604 intermed
[1] = buf
[1];
3605 intermed
[2] = (unsigned long)buf
[0] >> 16;
3607 decode_ieee_extended (fmt
, r
, intermed
);
3610 /* Convert from the internal format to the 12-byte Intel format for
3611 an IEEE extended real. */
3613 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3616 if (FLOAT_WORDS_BIG_ENDIAN
)
3618 /* All the padding in an Intel-format extended real goes at the high
3619 end, which in this case is after the mantissa, not the exponent.
3620 Therefore we must shift everything up 16 bits. */
3623 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3624 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3625 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3627 decode_ieee_extended (fmt
, r
, intermed
);
3630 /* decode_ieee_extended produces what we want directly. */
3631 decode_ieee_extended (fmt
, r
, buf
);
3634 /* Convert from the internal format to the 16-byte Intel format for
3635 an IEEE extended real. */
3637 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3640 /* All the padding in an Intel-format extended real goes at the high end. */
3641 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3644 const struct real_format ieee_extended_motorola_format
=
3646 encode_ieee_extended_motorola
,
3647 decode_ieee_extended_motorola
,
3665 const struct real_format ieee_extended_intel_96_format
=
3667 encode_ieee_extended_intel_96
,
3668 decode_ieee_extended_intel_96
,
3686 const struct real_format ieee_extended_intel_128_format
=
3688 encode_ieee_extended_intel_128
,
3689 decode_ieee_extended_intel_128
,
3707 /* The following caters to i386 systems that set the rounding precision
3708 to 53 bits instead of 64, e.g. FreeBSD. */
3709 const struct real_format ieee_extended_intel_96_round_53_format
=
3711 encode_ieee_extended_intel_96
,
3712 decode_ieee_extended_intel_96
,
3730 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3731 numbers whose sum is equal to the extended precision value. The number
3732 with greater magnitude is first. This format has the same magnitude
3733 range as an IEEE double precision value, but effectively 106 bits of
3734 significand precision. Infinity and NaN are represented by their IEEE
3735 double precision value stored in the first number, the second number is
3736 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3738 static void encode_ibm_extended (const struct real_format
*fmt
,
3739 long *, const REAL_VALUE_TYPE
*);
3740 static void decode_ibm_extended (const struct real_format
*,
3741 REAL_VALUE_TYPE
*, const long *);
3744 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3745 const REAL_VALUE_TYPE
*r
)
3747 REAL_VALUE_TYPE u
, normr
, v
;
3748 const struct real_format
*base_fmt
;
3750 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3752 /* Renormalize R before doing any arithmetic on it. */
3754 if (normr
.cl
== rvc_normal
)
3757 /* u = IEEE double precision portion of significand. */
3759 round_for_format (base_fmt
, &u
);
3760 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3762 if (u
.cl
== rvc_normal
)
3764 do_add (&v
, &normr
, &u
, 1);
3765 /* Call round_for_format since we might need to denormalize. */
3766 round_for_format (base_fmt
, &v
);
3767 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3771 /* Inf, NaN, 0 are all representable as doubles, so the
3772 least-significant part can be 0.0. */
3779 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3782 REAL_VALUE_TYPE u
, v
;
3783 const struct real_format
*base_fmt
;
3785 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3786 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3788 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3790 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3791 do_add (r
, &u
, &v
, 0);
3797 const struct real_format ibm_extended_format
=
3799 encode_ibm_extended
,
3800 decode_ibm_extended
,
3818 const struct real_format mips_extended_format
=
3820 encode_ibm_extended
,
3821 decode_ibm_extended
,
3840 /* IEEE quad precision format. */
3842 static void encode_ieee_quad (const struct real_format
*fmt
,
3843 long *, const REAL_VALUE_TYPE
*);
3844 static void decode_ieee_quad (const struct real_format
*,
3845 REAL_VALUE_TYPE
*, const long *);
3848 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3849 const REAL_VALUE_TYPE
*r
)
3851 unsigned long image3
, image2
, image1
, image0
, exp
;
3852 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3855 image3
= r
->sign
<< 31;
3860 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3869 image3
|= 32767 << 16;
3872 image3
|= 0x7fffffff;
3873 image2
= 0xffffffff;
3874 image1
= 0xffffffff;
3875 image0
= 0xffffffff;
3882 image3
|= 32767 << 16;
3886 if (fmt
->canonical_nan_lsbs_set
)
3889 image2
= image1
= image0
= 0xffffffff;
3892 else if (HOST_BITS_PER_LONG
== 32)
3897 image3
|= u
.sig
[3] & 0xffff;
3902 image1
= image0
>> 31 >> 1;
3904 image3
|= (image2
>> 31 >> 1) & 0xffff;
3905 image0
&= 0xffffffff;
3906 image2
&= 0xffffffff;
3908 if (r
->signalling
== fmt
->qnan_msb_set
)
3912 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3917 image3
|= 0x7fffffff;
3918 image2
= 0xffffffff;
3919 image1
= 0xffffffff;
3920 image0
= 0xffffffff;
3925 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3926 whereas the intermediate representation is 0.F x 2**exp.
3927 Which means we're off by one. */
3931 exp
= REAL_EXP (r
) + 16383 - 1;
3932 image3
|= exp
<< 16;
3934 if (HOST_BITS_PER_LONG
== 32)
3939 image3
|= u
.sig
[3] & 0xffff;
3944 image1
= image0
>> 31 >> 1;
3946 image3
|= (image2
>> 31 >> 1) & 0xffff;
3947 image0
&= 0xffffffff;
3948 image2
&= 0xffffffff;
3956 if (FLOAT_WORDS_BIG_ENDIAN
)
3973 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3976 unsigned long image3
, image2
, image1
, image0
;
3980 if (FLOAT_WORDS_BIG_ENDIAN
)
3994 image0
&= 0xffffffff;
3995 image1
&= 0xffffffff;
3996 image2
&= 0xffffffff;
3998 sign
= (image3
>> 31) & 1;
3999 exp
= (image3
>> 16) & 0x7fff;
4002 memset (r
, 0, sizeof (*r
));
4006 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4011 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4012 if (HOST_BITS_PER_LONG
== 32)
4021 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4022 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4027 else if (fmt
->has_signed_zero
)
4030 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4032 if (image3
| image2
| image1
| image0
)
4036 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4038 if (HOST_BITS_PER_LONG
== 32)
4047 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4048 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4050 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4062 SET_REAL_EXP (r
, exp
- 16383 + 1);
4064 if (HOST_BITS_PER_LONG
== 32)
4073 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4074 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4076 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4077 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4081 const struct real_format ieee_quad_format
=
4102 const struct real_format mips_quad_format
=
4123 /* Descriptions of VAX floating point formats can be found beginning at
4125 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4127 The thing to remember is that they're almost IEEE, except for word
4128 order, exponent bias, and the lack of infinities, nans, and denormals.
4130 We don't implement the H_floating format here, simply because neither
4131 the VAX or Alpha ports use it. */
4133 static void encode_vax_f (const struct real_format
*fmt
,
4134 long *, const REAL_VALUE_TYPE
*);
4135 static void decode_vax_f (const struct real_format
*,
4136 REAL_VALUE_TYPE
*, const long *);
4137 static void encode_vax_d (const struct real_format
*fmt
,
4138 long *, const REAL_VALUE_TYPE
*);
4139 static void decode_vax_d (const struct real_format
*,
4140 REAL_VALUE_TYPE
*, const long *);
4141 static void encode_vax_g (const struct real_format
*fmt
,
4142 long *, const REAL_VALUE_TYPE
*);
4143 static void decode_vax_g (const struct real_format
*,
4144 REAL_VALUE_TYPE
*, const long *);
4147 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4148 const REAL_VALUE_TYPE
*r
)
4150 unsigned long sign
, exp
, sig
, image
;
4152 sign
= r
->sign
<< 15;
4162 image
= 0xffff7fff | sign
;
4166 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4167 exp
= REAL_EXP (r
) + 128;
4169 image
= (sig
<< 16) & 0xffff0000;
4183 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4184 REAL_VALUE_TYPE
*r
, const long *buf
)
4186 unsigned long image
= buf
[0] & 0xffffffff;
4187 int exp
= (image
>> 7) & 0xff;
4189 memset (r
, 0, sizeof (*r
));
4194 r
->sign
= (image
>> 15) & 1;
4195 SET_REAL_EXP (r
, exp
- 128);
4197 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4198 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4203 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4204 const REAL_VALUE_TYPE
*r
)
4206 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4211 image0
= image1
= 0;
4216 image0
= 0xffff7fff | sign
;
4217 image1
= 0xffffffff;
4221 /* Extract the significand into straight hi:lo. */
4222 if (HOST_BITS_PER_LONG
== 64)
4224 image0
= r
->sig
[SIGSZ
-1];
4225 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4226 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4230 image0
= r
->sig
[SIGSZ
-1];
4231 image1
= r
->sig
[SIGSZ
-2];
4232 image1
= (image0
<< 24) | (image1
>> 8);
4233 image0
= (image0
>> 8) & 0xffffff;
4236 /* Rearrange the half-words of the significand to match the
4238 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4239 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4241 /* Add the sign and exponent. */
4243 image0
|= (REAL_EXP (r
) + 128) << 7;
4250 if (FLOAT_WORDS_BIG_ENDIAN
)
4251 buf
[0] = image1
, buf
[1] = image0
;
4253 buf
[0] = image0
, buf
[1] = image1
;
4257 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4258 REAL_VALUE_TYPE
*r
, const long *buf
)
4260 unsigned long image0
, image1
;
4263 if (FLOAT_WORDS_BIG_ENDIAN
)
4264 image1
= buf
[0], image0
= buf
[1];
4266 image0
= buf
[0], image1
= buf
[1];
4267 image0
&= 0xffffffff;
4268 image1
&= 0xffffffff;
4270 exp
= (image0
>> 7) & 0xff;
4272 memset (r
, 0, sizeof (*r
));
4277 r
->sign
= (image0
>> 15) & 1;
4278 SET_REAL_EXP (r
, exp
- 128);
4280 /* Rearrange the half-words of the external format into
4281 proper ascending order. */
4282 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4283 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4285 if (HOST_BITS_PER_LONG
== 64)
4287 image0
= (image0
<< 31 << 1) | image1
;
4290 r
->sig
[SIGSZ
-1] = image0
;
4294 r
->sig
[SIGSZ
-1] = image0
;
4295 r
->sig
[SIGSZ
-2] = image1
;
4296 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4297 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4303 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4304 const REAL_VALUE_TYPE
*r
)
4306 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4311 image0
= image1
= 0;
4316 image0
= 0xffff7fff | sign
;
4317 image1
= 0xffffffff;
4321 /* Extract the significand into straight hi:lo. */
4322 if (HOST_BITS_PER_LONG
== 64)
4324 image0
= r
->sig
[SIGSZ
-1];
4325 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4326 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4330 image0
= r
->sig
[SIGSZ
-1];
4331 image1
= r
->sig
[SIGSZ
-2];
4332 image1
= (image0
<< 21) | (image1
>> 11);
4333 image0
= (image0
>> 11) & 0xfffff;
4336 /* Rearrange the half-words of the significand to match the
4338 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4339 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4341 /* Add the sign and exponent. */
4343 image0
|= (REAL_EXP (r
) + 1024) << 4;
4350 if (FLOAT_WORDS_BIG_ENDIAN
)
4351 buf
[0] = image1
, buf
[1] = image0
;
4353 buf
[0] = image0
, buf
[1] = image1
;
4357 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4358 REAL_VALUE_TYPE
*r
, const long *buf
)
4360 unsigned long image0
, image1
;
4363 if (FLOAT_WORDS_BIG_ENDIAN
)
4364 image1
= buf
[0], image0
= buf
[1];
4366 image0
= buf
[0], image1
= buf
[1];
4367 image0
&= 0xffffffff;
4368 image1
&= 0xffffffff;
4370 exp
= (image0
>> 4) & 0x7ff;
4372 memset (r
, 0, sizeof (*r
));
4377 r
->sign
= (image0
>> 15) & 1;
4378 SET_REAL_EXP (r
, exp
- 1024);
4380 /* Rearrange the half-words of the external format into
4381 proper ascending order. */
4382 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4383 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4385 if (HOST_BITS_PER_LONG
== 64)
4387 image0
= (image0
<< 31 << 1) | image1
;
4390 r
->sig
[SIGSZ
-1] = image0
;
4394 r
->sig
[SIGSZ
-1] = image0
;
4395 r
->sig
[SIGSZ
-2] = image1
;
4396 lshift_significand (r
, r
, 64 - 53);
4397 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4402 const struct real_format vax_f_format
=
4423 const struct real_format vax_d_format
=
4444 const struct real_format vax_g_format
=
4465 /* Encode real R into a single precision DFP value in BUF. */
4467 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4468 long *buf ATTRIBUTE_UNUSED
,
4469 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4471 encode_decimal32 (fmt
, buf
, r
);
4474 /* Decode a single precision DFP value in BUF into a real R. */
4476 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4477 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4478 const long *buf ATTRIBUTE_UNUSED
)
4480 decode_decimal32 (fmt
, r
, buf
);
4483 /* Encode real R into a double precision DFP value in BUF. */
4485 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4486 long *buf ATTRIBUTE_UNUSED
,
4487 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4489 encode_decimal64 (fmt
, buf
, r
);
4492 /* Decode a double precision DFP value in BUF into a real R. */
4494 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4495 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4496 const long *buf ATTRIBUTE_UNUSED
)
4498 decode_decimal64 (fmt
, r
, buf
);
4501 /* Encode real R into a quad precision DFP value in BUF. */
4503 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4504 long *buf ATTRIBUTE_UNUSED
,
4505 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4507 encode_decimal128 (fmt
, buf
, r
);
4510 /* Decode a quad precision DFP value in BUF into a real R. */
4512 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4513 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4514 const long *buf ATTRIBUTE_UNUSED
)
4516 decode_decimal128 (fmt
, r
, buf
);
4519 /* Single precision decimal floating point (IEEE 754). */
4520 const struct real_format decimal_single_format
=
4522 encode_decimal_single
,
4523 decode_decimal_single
,
4541 /* Double precision decimal floating point (IEEE 754). */
4542 const struct real_format decimal_double_format
=
4544 encode_decimal_double
,
4545 decode_decimal_double
,
4563 /* Quad precision decimal floating point (IEEE 754). */
4564 const struct real_format decimal_quad_format
=
4566 encode_decimal_quad
,
4567 decode_decimal_quad
,
4585 /* Encode half-precision floats. This routine is used both for the IEEE
4586 ARM alternative encodings. */
4588 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4589 const REAL_VALUE_TYPE
*r
)
4591 unsigned long image
, sig
, exp
;
4592 unsigned long sign
= r
->sign
;
4593 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4596 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4614 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4615 if (r
->signalling
== fmt
->qnan_msb_set
)
4630 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4631 whereas the intermediate representation is 0.F x 2**exp.
4632 Which means we're off by one. */
4636 exp
= REAL_EXP (r
) + 15 - 1;
4648 /* Decode half-precision floats. This routine is used both for the IEEE
4649 ARM alternative encodings. */
4651 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4654 unsigned long image
= buf
[0] & 0xffff;
4655 bool sign
= (image
>> 15) & 1;
4656 int exp
= (image
>> 10) & 0x1f;
4658 memset (r
, 0, sizeof (*r
));
4659 image
<<= HOST_BITS_PER_LONG
- 11;
4664 if (image
&& fmt
->has_denorm
)
4668 SET_REAL_EXP (r
, -14);
4669 r
->sig
[SIGSZ
-1] = image
<< 1;
4672 else if (fmt
->has_signed_zero
)
4675 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4681 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4682 ^ fmt
->qnan_msb_set
);
4683 r
->sig
[SIGSZ
-1] = image
;
4695 SET_REAL_EXP (r
, exp
- 15 + 1);
4696 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4700 /* Half-precision format, as specified in IEEE 754R. */
4701 const struct real_format ieee_half_format
=
4722 /* ARM's alternative half-precision format, similar to IEEE but with
4723 no reserved exponent value for NaNs and infinities; rather, it just
4724 extends the range of exponents by one. */
4725 const struct real_format arm_half_format
=
4746 /* A synthetic "format" for internal arithmetic. It's the size of the
4747 internal significand minus the two bits needed for proper rounding.
4748 The encode and decode routines exist only to satisfy our paranoia
4751 static void encode_internal (const struct real_format
*fmt
,
4752 long *, const REAL_VALUE_TYPE
*);
4753 static void decode_internal (const struct real_format
*,
4754 REAL_VALUE_TYPE
*, const long *);
4757 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4758 const REAL_VALUE_TYPE
*r
)
4760 memcpy (buf
, r
, sizeof (*r
));
4764 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4765 REAL_VALUE_TYPE
*r
, const long *buf
)
4767 memcpy (r
, buf
, sizeof (*r
));
4770 const struct real_format real_internal_format
=
4775 SIGNIFICAND_BITS
- 2,
4776 SIGNIFICAND_BITS
- 2,
4791 /* Calculate the square root of X in mode MODE, and store the result
4792 in R. Return TRUE if the operation does not raise an exception.
4793 For details see "High Precision Division and Square Root",
4794 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4795 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4798 real_sqrt (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4799 const REAL_VALUE_TYPE
*x
)
4801 static REAL_VALUE_TYPE halfthree
;
4802 static bool init
= false;
4803 REAL_VALUE_TYPE h
, t
, i
;
4806 /* sqrt(-0.0) is -0.0. */
4807 if (real_isnegzero (x
))
4813 /* Negative arguments return NaN. */
4816 get_canonical_qnan (r
, 0);
4820 /* Infinity and NaN return themselves. */
4821 if (!real_isfinite (x
))
4829 do_add (&halfthree
, &dconst1
, &dconsthalf
, 0);
4833 /* Initial guess for reciprocal sqrt, i. */
4834 exp
= real_exponent (x
);
4835 real_ldexp (&i
, &dconst1
, -exp
/2);
4837 /* Newton's iteration for reciprocal sqrt, i. */
4838 for (iter
= 0; iter
< 16; iter
++)
4840 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4841 do_multiply (&t
, x
, &i
);
4842 do_multiply (&h
, &t
, &i
);
4843 do_multiply (&t
, &h
, &dconsthalf
);
4844 do_add (&h
, &halfthree
, &t
, 1);
4845 do_multiply (&t
, &i
, &h
);
4847 /* Check for early convergence. */
4848 if (iter
>= 6 && real_identical (&i
, &t
))
4851 /* ??? Unroll loop to avoid copying. */
4855 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4856 do_multiply (&t
, x
, &i
);
4857 do_multiply (&h
, &t
, &i
);
4858 do_add (&i
, &dconst1
, &h
, 1);
4859 do_multiply (&h
, &t
, &i
);
4860 do_multiply (&i
, &dconsthalf
, &h
);
4861 do_add (&h
, &t
, &i
, 0);
4863 /* ??? We need a Tuckerman test to get the last bit. */
4865 real_convert (r
, mode
, &h
);
4869 /* Calculate X raised to the integer exponent N in mode MODE and store
4870 the result in R. Return true if the result may be inexact due to
4871 loss of precision. The algorithm is the classic "left-to-right binary
4872 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4873 Algorithms", "The Art of Computer Programming", Volume 2. */
4876 real_powi (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4877 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4879 unsigned HOST_WIDE_INT bit
;
4881 bool inexact
= false;
4893 /* Don't worry about overflow, from now on n is unsigned. */
4901 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4902 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4906 inexact
|= do_multiply (&t
, &t
, &t
);
4908 inexact
|= do_multiply (&t
, &t
, x
);
4916 inexact
|= do_divide (&t
, &dconst1
, &t
);
4918 real_convert (r
, mode
, &t
);
4922 /* Round X to the nearest integer not larger in absolute value, i.e.
4923 towards zero, placing the result in R in mode MODE. */
4926 real_trunc (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4927 const REAL_VALUE_TYPE
*x
)
4929 do_fix_trunc (r
, x
);
4930 if (mode
!= VOIDmode
)
4931 real_convert (r
, mode
, r
);
4934 /* Round X to the largest integer not greater in value, i.e. round
4935 down, placing the result in R in mode MODE. */
4938 real_floor (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4939 const REAL_VALUE_TYPE
*x
)
4943 do_fix_trunc (&t
, x
);
4944 if (! real_identical (&t
, x
) && x
->sign
)
4945 do_add (&t
, &t
, &dconstm1
, 0);
4946 if (mode
!= VOIDmode
)
4947 real_convert (r
, mode
, &t
);
4952 /* Round X to the smallest integer not less then argument, i.e. round
4953 up, placing the result in R in mode MODE. */
4956 real_ceil (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4957 const REAL_VALUE_TYPE
*x
)
4961 do_fix_trunc (&t
, x
);
4962 if (! real_identical (&t
, x
) && ! x
->sign
)
4963 do_add (&t
, &t
, &dconst1
, 0);
4964 if (mode
!= VOIDmode
)
4965 real_convert (r
, mode
, &t
);
4970 /* Round X to the nearest integer, but round halfway cases away from
4974 real_round (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4975 const REAL_VALUE_TYPE
*x
)
4977 do_add (r
, x
, &dconsthalf
, x
->sign
);
4978 do_fix_trunc (r
, r
);
4979 if (mode
!= VOIDmode
)
4980 real_convert (r
, mode
, r
);
4983 /* Set the sign of R to the sign of X. */
4986 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4991 /* Check whether the real constant value given is an integer. */
4994 real_isinteger (const REAL_VALUE_TYPE
*c
, enum machine_mode mode
)
4996 REAL_VALUE_TYPE cint
;
4998 real_trunc (&cint
, mode
, c
);
4999 return real_identical (c
, &cint
);
5002 /* Write into BUF the maximum representable finite floating-point
5003 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5004 float string. LEN is the size of BUF, and the buffer must be large
5005 enough to contain the resulting string. */
5008 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
5013 strcpy (buf
, "0x0.");
5015 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
5018 *p
++ = "08ce"[n
- i
];
5019 sprintf (p
, "p%d", fmt
->emax
);
5020 if (fmt
->pnan
< fmt
->p
)
5022 /* This is an IBM extended double format made up of two IEEE
5023 doubles. The value of the long double is the sum of the
5024 values of the two parts. The most significant part is
5025 required to be the value of the long double rounded to the
5026 nearest double. Rounding means we need a slightly smaller
5027 value for LDBL_MAX. */
5028 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
5031 gcc_assert (strlen (buf
) < len
);