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"
28 #include "diagnostic-core.h"
35 /* The floating point model used internally is not exactly IEEE 754
36 compliant, and close to the description in the ISO C99 standard,
37 section 5.2.4.2.2 Characteristics of floating types.
41 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
45 b = base or radix, here always 2
47 p = precision (the number of base-b digits in the significand)
48 f_k = the digits of the significand.
50 We differ from typical IEEE 754 encodings in that the entire
51 significand is fractional. Normalized significands are in the
54 A requirement of the model is that P be larger than the largest
55 supported target floating-point type by at least 2 bits. This gives
56 us proper rounding when we truncate to the target type. In addition,
57 E must be large enough to hold the smallest supported denormal number
60 Both of these requirements are easily satisfied. The largest target
61 significand is 113 bits; we store at least 160. The smallest
62 denormal number fits in 17 exponent bits; we store 26.
64 Note that the decimal string conversion routines are sensitive to
65 rounding errors. Since the raw arithmetic routines do not themselves
66 have guard digits or rounding, the computation of 10**exp can
67 accumulate more than a few digits of error. The previous incarnation
68 of real.c successfully used a 144-bit fraction; given the current
69 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits. */
72 /* Used to classify two numbers simultaneously. */
73 #define CLASS2(A, B) ((A) << 2 | (B))
75 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
76 #error "Some constant folding done by hand to avoid shift count warnings"
79 static void get_zero (REAL_VALUE_TYPE
*, int);
80 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
81 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
82 static void get_inf (REAL_VALUE_TYPE
*, int);
83 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
84 const REAL_VALUE_TYPE
*, unsigned int);
85 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
87 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
89 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
90 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
91 const REAL_VALUE_TYPE
*);
92 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
93 const REAL_VALUE_TYPE
*, int);
94 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
95 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
96 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
97 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
98 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
99 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
100 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
101 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
102 const REAL_VALUE_TYPE
*);
103 static void normalize (REAL_VALUE_TYPE
*);
105 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
106 const REAL_VALUE_TYPE
*, int);
107 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
108 const REAL_VALUE_TYPE
*);
109 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
110 const REAL_VALUE_TYPE
*);
111 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
112 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
114 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, REAL_VALUE_TYPE
*);
115 static void decimal_from_integer (REAL_VALUE_TYPE
*);
116 static void decimal_integer_string (char *, const REAL_VALUE_TYPE
*,
119 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
120 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
121 static const REAL_VALUE_TYPE
* real_digit (int);
122 static void times_pten (REAL_VALUE_TYPE
*, int);
124 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
126 /* Initialize R with a positive zero. */
129 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
131 memset (r
, 0, sizeof (*r
));
135 /* Initialize R with the canonical quiet NaN. */
138 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
140 memset (r
, 0, sizeof (*r
));
147 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
149 memset (r
, 0, sizeof (*r
));
157 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
159 memset (r
, 0, sizeof (*r
));
165 /* Right-shift the significand of A by N bits; put the result in the
166 significand of R. If any one bits are shifted out, return true. */
169 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
172 unsigned long sticky
= 0;
173 unsigned int i
, ofs
= 0;
175 if (n
>= HOST_BITS_PER_LONG
)
177 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
179 n
&= HOST_BITS_PER_LONG
- 1;
184 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
185 for (i
= 0; i
< SIGSZ
; ++i
)
188 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
189 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
190 << (HOST_BITS_PER_LONG
- n
)));
195 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
196 r
->sig
[i
] = a
->sig
[ofs
+ i
];
197 for (; i
< SIGSZ
; ++i
)
204 /* Right-shift the significand of A by N bits; put the result in the
208 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
211 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
213 n
&= HOST_BITS_PER_LONG
- 1;
216 for (i
= 0; i
< SIGSZ
; ++i
)
219 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
220 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
221 << (HOST_BITS_PER_LONG
- n
)));
226 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
227 r
->sig
[i
] = a
->sig
[ofs
+ i
];
228 for (; i
< SIGSZ
; ++i
)
233 /* Left-shift the significand of A by N bits; put the result in the
237 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
240 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
242 n
&= HOST_BITS_PER_LONG
- 1;
245 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
246 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
247 for (; i
< SIGSZ
; ++i
)
248 r
->sig
[SIGSZ
-1-i
] = 0;
251 for (i
= 0; i
< SIGSZ
; ++i
)
254 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
255 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
256 >> (HOST_BITS_PER_LONG
- n
)));
260 /* Likewise, but N is specialized to 1. */
263 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
267 for (i
= SIGSZ
- 1; i
> 0; --i
)
268 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
269 r
->sig
[0] = a
->sig
[0] << 1;
272 /* Add the significands of A and B, placing the result in R. Return
273 true if there was carry out of the most significant word. */
276 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
277 const REAL_VALUE_TYPE
*b
)
282 for (i
= 0; i
< SIGSZ
; ++i
)
284 unsigned long ai
= a
->sig
[i
];
285 unsigned long ri
= ai
+ b
->sig
[i
];
301 /* Subtract the significands of A and B, placing the result in R. CARRY is
302 true if there's a borrow incoming to the least significant word.
303 Return true if there was borrow out of the most significant word. */
306 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
307 const REAL_VALUE_TYPE
*b
, int carry
)
311 for (i
= 0; i
< SIGSZ
; ++i
)
313 unsigned long ai
= a
->sig
[i
];
314 unsigned long ri
= ai
- b
->sig
[i
];
330 /* Negate the significand A, placing the result in R. */
333 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
338 for (i
= 0; i
< SIGSZ
; ++i
)
340 unsigned long ri
, ai
= a
->sig
[i
];
359 /* Compare significands. Return tri-state vs zero. */
362 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
366 for (i
= SIGSZ
- 1; i
>= 0; --i
)
368 unsigned long ai
= a
->sig
[i
];
369 unsigned long bi
= b
->sig
[i
];
380 /* Return true if A is nonzero. */
383 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
387 for (i
= SIGSZ
- 1; i
>= 0; --i
)
394 /* Set bit N of the significand of R. */
397 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
399 r
->sig
[n
/ HOST_BITS_PER_LONG
]
400 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
403 /* Clear bit N of the significand of R. */
406 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
408 r
->sig
[n
/ HOST_BITS_PER_LONG
]
409 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
412 /* Test bit N of the significand of R. */
415 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
417 /* ??? Compiler bug here if we return this expression directly.
418 The conversion to bool strips the "&1" and we wind up testing
419 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
420 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
424 /* Clear bits 0..N-1 of the significand of R. */
427 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
429 int i
, w
= n
/ HOST_BITS_PER_LONG
;
431 for (i
= 0; i
< w
; ++i
)
434 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
437 /* Divide the significands of A and B, placing the result in R. Return
438 true if the division was inexact. */
441 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
442 const REAL_VALUE_TYPE
*b
)
445 int i
, bit
= SIGNIFICAND_BITS
- 1;
446 unsigned long msb
, inexact
;
449 memset (r
->sig
, 0, sizeof (r
->sig
));
455 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
456 lshift_significand_1 (&u
, &u
);
458 if (msb
|| cmp_significands (&u
, b
) >= 0)
460 sub_significands (&u
, &u
, b
, 0);
461 set_significand_bit (r
, bit
);
466 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
472 /* Adjust the exponent and significand of R such that the most
473 significant bit is set. We underflow to zero and overflow to
474 infinity here, without denormals. (The intermediate representation
475 exponent is large enough to handle target denormals normalized.) */
478 normalize (REAL_VALUE_TYPE
*r
)
486 /* Find the first word that is nonzero. */
487 for (i
= SIGSZ
- 1; i
>= 0; i
--)
489 shift
+= HOST_BITS_PER_LONG
;
493 /* Zero significand flushes to zero. */
501 /* Find the first bit that is nonzero. */
503 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
509 exp
= REAL_EXP (r
) - shift
;
511 get_inf (r
, r
->sign
);
512 else if (exp
< -MAX_EXP
)
513 get_zero (r
, r
->sign
);
516 SET_REAL_EXP (r
, exp
);
517 lshift_significand (r
, r
, shift
);
522 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
523 result may be inexact due to a loss of precision. */
526 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
527 const REAL_VALUE_TYPE
*b
, int subtract_p
)
531 bool inexact
= false;
533 /* Determine if we need to add or subtract. */
535 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
537 switch (CLASS2 (a
->cl
, b
->cl
))
539 case CLASS2 (rvc_zero
, rvc_zero
):
540 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
541 get_zero (r
, sign
& !subtract_p
);
544 case CLASS2 (rvc_zero
, rvc_normal
):
545 case CLASS2 (rvc_zero
, rvc_inf
):
546 case CLASS2 (rvc_zero
, rvc_nan
):
548 case CLASS2 (rvc_normal
, rvc_nan
):
549 case CLASS2 (rvc_inf
, rvc_nan
):
550 case CLASS2 (rvc_nan
, rvc_nan
):
551 /* ANY + NaN = NaN. */
552 case CLASS2 (rvc_normal
, rvc_inf
):
555 r
->sign
= sign
^ subtract_p
;
558 case CLASS2 (rvc_normal
, rvc_zero
):
559 case CLASS2 (rvc_inf
, rvc_zero
):
560 case CLASS2 (rvc_nan
, rvc_zero
):
562 case CLASS2 (rvc_nan
, rvc_normal
):
563 case CLASS2 (rvc_nan
, rvc_inf
):
564 /* NaN + ANY = NaN. */
565 case CLASS2 (rvc_inf
, rvc_normal
):
570 case CLASS2 (rvc_inf
, rvc_inf
):
572 /* Inf - Inf = NaN. */
573 get_canonical_qnan (r
, 0);
575 /* Inf + Inf = Inf. */
579 case CLASS2 (rvc_normal
, rvc_normal
):
586 /* Swap the arguments such that A has the larger exponent. */
587 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
590 const REAL_VALUE_TYPE
*t
;
597 /* If the exponents are not identical, we need to shift the
598 significand of B down. */
601 /* If the exponents are too far apart, the significands
602 do not overlap, which makes the subtraction a noop. */
603 if (dexp
>= SIGNIFICAND_BITS
)
610 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
616 if (sub_significands (r
, a
, b
, inexact
))
618 /* We got a borrow out of the subtraction. That means that
619 A and B had the same exponent, and B had the larger
620 significand. We need to swap the sign and negate the
623 neg_significand (r
, r
);
628 if (add_significands (r
, a
, b
))
630 /* We got carry out of the addition. This means we need to
631 shift the significand back down one bit and increase the
633 inexact
|= sticky_rshift_significand (r
, r
, 1);
634 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
645 SET_REAL_EXP (r
, exp
);
646 /* Zero out the remaining fields. */
651 /* Re-normalize the result. */
654 /* Special case: if the subtraction results in zero, the result
656 if (r
->cl
== rvc_zero
)
659 r
->sig
[0] |= inexact
;
664 /* Calculate R = A * B. Return true if the result may be inexact. */
667 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
668 const REAL_VALUE_TYPE
*b
)
670 REAL_VALUE_TYPE u
, t
, *rr
;
671 unsigned int i
, j
, k
;
672 int sign
= a
->sign
^ b
->sign
;
673 bool inexact
= false;
675 switch (CLASS2 (a
->cl
, b
->cl
))
677 case CLASS2 (rvc_zero
, rvc_zero
):
678 case CLASS2 (rvc_zero
, rvc_normal
):
679 case CLASS2 (rvc_normal
, rvc_zero
):
680 /* +-0 * ANY = 0 with appropriate sign. */
684 case CLASS2 (rvc_zero
, rvc_nan
):
685 case CLASS2 (rvc_normal
, rvc_nan
):
686 case CLASS2 (rvc_inf
, rvc_nan
):
687 case CLASS2 (rvc_nan
, rvc_nan
):
688 /* ANY * NaN = NaN. */
693 case CLASS2 (rvc_nan
, rvc_zero
):
694 case CLASS2 (rvc_nan
, rvc_normal
):
695 case CLASS2 (rvc_nan
, rvc_inf
):
696 /* NaN * ANY = NaN. */
701 case CLASS2 (rvc_zero
, rvc_inf
):
702 case CLASS2 (rvc_inf
, rvc_zero
):
704 get_canonical_qnan (r
, sign
);
707 case CLASS2 (rvc_inf
, rvc_inf
):
708 case CLASS2 (rvc_normal
, rvc_inf
):
709 case CLASS2 (rvc_inf
, rvc_normal
):
710 /* Inf * Inf = Inf, R * Inf = Inf */
714 case CLASS2 (rvc_normal
, rvc_normal
):
721 if (r
== a
|| r
== b
)
727 /* Collect all the partial products. Since we don't have sure access
728 to a widening multiply, we split each long into two half-words.
730 Consider the long-hand form of a four half-word multiplication:
740 We construct partial products of the widened half-word products
741 that are known to not overlap, e.g. DF+DH. Each such partial
742 product is given its proper exponent, which allows us to sum them
743 and obtain the finished product. */
745 for (i
= 0; i
< SIGSZ
* 2; ++i
)
747 unsigned long ai
= a
->sig
[i
/ 2];
749 ai
>>= HOST_BITS_PER_LONG
/ 2;
751 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
756 for (j
= 0; j
< 2; ++j
)
758 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
759 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
768 /* Would underflow to zero, which we shouldn't bother adding. */
773 memset (&u
, 0, sizeof (u
));
775 SET_REAL_EXP (&u
, exp
);
777 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
779 unsigned long bi
= b
->sig
[k
/ 2];
781 bi
>>= HOST_BITS_PER_LONG
/ 2;
783 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
785 u
.sig
[k
/ 2] = ai
* bi
;
789 inexact
|= do_add (rr
, rr
, &u
, 0);
800 /* Calculate R = A / B. Return true if the result may be inexact. */
803 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
804 const REAL_VALUE_TYPE
*b
)
806 int exp
, sign
= a
->sign
^ b
->sign
;
807 REAL_VALUE_TYPE t
, *rr
;
810 switch (CLASS2 (a
->cl
, b
->cl
))
812 case CLASS2 (rvc_zero
, rvc_zero
):
814 case CLASS2 (rvc_inf
, rvc_inf
):
815 /* Inf / Inf = NaN. */
816 get_canonical_qnan (r
, sign
);
819 case CLASS2 (rvc_zero
, rvc_normal
):
820 case CLASS2 (rvc_zero
, rvc_inf
):
822 case CLASS2 (rvc_normal
, rvc_inf
):
827 case CLASS2 (rvc_normal
, rvc_zero
):
829 case CLASS2 (rvc_inf
, rvc_zero
):
834 case CLASS2 (rvc_zero
, rvc_nan
):
835 case CLASS2 (rvc_normal
, rvc_nan
):
836 case CLASS2 (rvc_inf
, rvc_nan
):
837 case CLASS2 (rvc_nan
, rvc_nan
):
838 /* ANY / NaN = NaN. */
843 case CLASS2 (rvc_nan
, rvc_zero
):
844 case CLASS2 (rvc_nan
, rvc_normal
):
845 case CLASS2 (rvc_nan
, rvc_inf
):
846 /* NaN / ANY = NaN. */
851 case CLASS2 (rvc_inf
, rvc_normal
):
856 case CLASS2 (rvc_normal
, rvc_normal
):
863 if (r
== a
|| r
== b
)
868 /* Make sure all fields in the result are initialized. */
873 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
884 SET_REAL_EXP (rr
, exp
);
886 inexact
= div_significands (rr
, a
, b
);
888 /* Re-normalize the result. */
890 rr
->sig
[0] |= inexact
;
898 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
899 one of the two operands is a NaN. */
902 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
907 switch (CLASS2 (a
->cl
, b
->cl
))
909 case CLASS2 (rvc_zero
, rvc_zero
):
910 /* Sign of zero doesn't matter for compares. */
913 case CLASS2 (rvc_normal
, rvc_zero
):
914 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
916 return decimal_do_compare (a
, b
, nan_result
);
918 case CLASS2 (rvc_inf
, rvc_zero
):
919 case CLASS2 (rvc_inf
, rvc_normal
):
920 return (a
->sign
? -1 : 1);
922 case CLASS2 (rvc_inf
, rvc_inf
):
923 return -a
->sign
- -b
->sign
;
925 case CLASS2 (rvc_zero
, rvc_normal
):
926 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
928 return decimal_do_compare (a
, b
, nan_result
);
930 case CLASS2 (rvc_zero
, rvc_inf
):
931 case CLASS2 (rvc_normal
, rvc_inf
):
932 return (b
->sign
? 1 : -1);
934 case CLASS2 (rvc_zero
, rvc_nan
):
935 case CLASS2 (rvc_normal
, rvc_nan
):
936 case CLASS2 (rvc_inf
, rvc_nan
):
937 case CLASS2 (rvc_nan
, rvc_nan
):
938 case CLASS2 (rvc_nan
, rvc_zero
):
939 case CLASS2 (rvc_nan
, rvc_normal
):
940 case CLASS2 (rvc_nan
, rvc_inf
):
943 case CLASS2 (rvc_normal
, rvc_normal
):
950 if (a
->sign
!= b
->sign
)
951 return -a
->sign
- -b
->sign
;
953 if (a
->decimal
|| b
->decimal
)
954 return decimal_do_compare (a
, b
, nan_result
);
956 if (REAL_EXP (a
) > REAL_EXP (b
))
958 else if (REAL_EXP (a
) < REAL_EXP (b
))
961 ret
= cmp_significands (a
, b
);
963 return (a
->sign
? -ret
: ret
);
966 /* Return A truncated to an integral value toward zero. */
969 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
983 decimal_do_fix_trunc (r
, a
);
986 if (REAL_EXP (r
) <= 0)
987 get_zero (r
, r
->sign
);
988 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
989 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
997 /* Perform the binary or unary operation described by CODE.
998 For a unary operation, leave OP1 NULL. This function returns
999 true if the result may be inexact due to loss of precision. */
1002 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
1003 const REAL_VALUE_TYPE
*op1
)
1005 enum tree_code code
= (enum tree_code
) icode
;
1007 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1008 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1013 return do_add (r
, op0
, op1
, 0);
1016 return do_add (r
, op0
, op1
, 1);
1019 return do_multiply (r
, op0
, op1
);
1022 return do_divide (r
, op0
, op1
);
1025 if (op1
->cl
== rvc_nan
)
1027 else if (do_compare (op0
, op1
, -1) < 0)
1034 if (op1
->cl
== rvc_nan
)
1036 else if (do_compare (op0
, op1
, 1) < 0)
1052 case FIX_TRUNC_EXPR
:
1053 do_fix_trunc (r
, op0
);
1063 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1066 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1071 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1074 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1079 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1080 const REAL_VALUE_TYPE
*op1
)
1082 enum tree_code code
= (enum tree_code
) icode
;
1087 return do_compare (op0
, op1
, 1) < 0;
1089 return do_compare (op0
, op1
, 1) <= 0;
1091 return do_compare (op0
, op1
, -1) > 0;
1093 return do_compare (op0
, op1
, -1) >= 0;
1095 return do_compare (op0
, op1
, -1) == 0;
1097 return do_compare (op0
, op1
, -1) != 0;
1098 case UNORDERED_EXPR
:
1099 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1101 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1103 return do_compare (op0
, op1
, -1) < 0;
1105 return do_compare (op0
, op1
, -1) <= 0;
1107 return do_compare (op0
, op1
, 1) > 0;
1109 return do_compare (op0
, op1
, 1) >= 0;
1111 return do_compare (op0
, op1
, 0) == 0;
1113 return do_compare (op0
, op1
, 0) != 0;
1120 /* Return floor log2(R). */
1123 real_exponent (const REAL_VALUE_TYPE
*r
)
1131 return (unsigned int)-1 >> 1;
1133 return REAL_EXP (r
);
1139 /* R = OP0 * 2**EXP. */
1142 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1153 exp
+= REAL_EXP (op0
);
1155 get_inf (r
, r
->sign
);
1156 else if (exp
< -MAX_EXP
)
1157 get_zero (r
, r
->sign
);
1159 SET_REAL_EXP (r
, exp
);
1167 /* Determine whether a floating-point value X is infinite. */
1170 real_isinf (const REAL_VALUE_TYPE
*r
)
1172 return (r
->cl
== rvc_inf
);
1175 /* Determine whether a floating-point value X is a NaN. */
1178 real_isnan (const REAL_VALUE_TYPE
*r
)
1180 return (r
->cl
== rvc_nan
);
1183 /* Determine whether a floating-point value X is finite. */
1186 real_isfinite (const REAL_VALUE_TYPE
*r
)
1188 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1191 /* Determine whether a floating-point value X is negative. */
1194 real_isneg (const REAL_VALUE_TYPE
*r
)
1199 /* Determine whether a floating-point value X is minus zero. */
1202 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1204 return r
->sign
&& r
->cl
== rvc_zero
;
1207 /* Compare two floating-point objects for bitwise identity. */
1210 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1216 if (a
->sign
!= b
->sign
)
1226 if (a
->decimal
!= b
->decimal
)
1228 if (REAL_EXP (a
) != REAL_EXP (b
))
1233 if (a
->signalling
!= b
->signalling
)
1235 /* The significand is ignored for canonical NaNs. */
1236 if (a
->canonical
|| b
->canonical
)
1237 return a
->canonical
== b
->canonical
;
1244 for (i
= 0; i
< SIGSZ
; ++i
)
1245 if (a
->sig
[i
] != b
->sig
[i
])
1251 /* Try to change R into its exact multiplicative inverse in machine
1252 mode MODE. Return true if successful. */
1255 exact_real_inverse (enum machine_mode mode
, REAL_VALUE_TYPE
*r
)
1257 const REAL_VALUE_TYPE
*one
= real_digit (1);
1261 if (r
->cl
!= rvc_normal
)
1264 /* Check for a power of two: all significand bits zero except the MSB. */
1265 for (i
= 0; i
< SIGSZ
-1; ++i
)
1268 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1271 /* Find the inverse and truncate to the required mode. */
1272 do_divide (&u
, one
, r
);
1273 real_convert (&u
, mode
, &u
);
1275 /* The rounding may have overflowed. */
1276 if (u
.cl
!= rvc_normal
)
1278 for (i
= 0; i
< SIGSZ
-1; ++i
)
1281 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1288 /* Return true if arithmetic on values in IMODE that were promoted
1289 from values in TMODE is equivalent to direct arithmetic on values
1293 real_can_shorten_arithmetic (enum machine_mode imode
, enum machine_mode tmode
)
1295 const struct real_format
*tfmt
, *ifmt
;
1296 tfmt
= REAL_MODE_FORMAT (tmode
);
1297 ifmt
= REAL_MODE_FORMAT (imode
);
1298 /* These conditions are conservative rather than trying to catch the
1299 exact boundary conditions; the main case to allow is IEEE float
1301 return (ifmt
->b
== tfmt
->b
1302 && ifmt
->p
> 2 * tfmt
->p
1303 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1304 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1305 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1306 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1307 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1308 && (ifmt
->has_sign_dependent_rounding
1309 == tfmt
->has_sign_dependent_rounding
)
1310 && ifmt
->has_nans
>= tfmt
->has_nans
1311 && ifmt
->has_inf
>= tfmt
->has_inf
1312 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1313 && !MODE_COMPOSITE_P (tmode
)
1314 && !MODE_COMPOSITE_P (imode
));
1317 /* Render R as an integer. */
1320 real_to_integer (const REAL_VALUE_TYPE
*r
)
1322 unsigned HOST_WIDE_INT i
;
1333 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1340 return decimal_real_to_integer (r
);
1342 if (REAL_EXP (r
) <= 0)
1344 /* Only force overflow for unsigned overflow. Signed overflow is
1345 undefined, so it doesn't matter what we return, and some callers
1346 expect to be able to use this routine for both signed and
1347 unsigned conversions. */
1348 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1351 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1352 i
= r
->sig
[SIGSZ
-1];
1355 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1356 i
= r
->sig
[SIGSZ
-1];
1357 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1358 i
|= r
->sig
[SIGSZ
-2];
1361 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1372 /* Likewise, but to an integer pair, HI+LOW. */
1375 real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
1376 const REAL_VALUE_TYPE
*r
)
1379 HOST_WIDE_INT low
, high
;
1392 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1405 decimal_real_to_integer2 (plow
, phigh
, r
);
1412 /* Only force overflow for unsigned overflow. Signed overflow is
1413 undefined, so it doesn't matter what we return, and some callers
1414 expect to be able to use this routine for both signed and
1415 unsigned conversions. */
1416 if (exp
> 2*HOST_BITS_PER_WIDE_INT
)
1419 rshift_significand (&t
, r
, 2*HOST_BITS_PER_WIDE_INT
- exp
);
1420 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1422 high
= t
.sig
[SIGSZ
-1];
1423 low
= t
.sig
[SIGSZ
-2];
1427 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
);
1428 high
= t
.sig
[SIGSZ
-1];
1429 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1430 high
|= t
.sig
[SIGSZ
-2];
1432 low
= t
.sig
[SIGSZ
-3];
1433 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1434 low
|= t
.sig
[SIGSZ
-4];
1442 low
= -low
, high
= ~high
;
1454 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1455 of NUM / DEN. Return the quotient and place the remainder in NUM.
1456 It is expected that NUM / DEN are close enough that the quotient is
1459 static unsigned long
1460 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1462 unsigned long q
, msb
;
1463 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1472 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1474 lshift_significand_1 (num
, num
);
1476 if (msb
|| cmp_significands (num
, den
) >= 0)
1478 sub_significands (num
, num
, den
, 0);
1482 while (--expn
>= expd
);
1484 SET_REAL_EXP (num
, expd
);
1490 /* Render R as a decimal floating point constant. Emit DIGITS significant
1491 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1492 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1493 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1494 to a string that, when parsed back in mode MODE, yields the same value. */
1496 #define M_LOG10_2 0.30102999566398119521
1499 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1500 size_t buf_size
, size_t digits
,
1501 int crop_trailing_zeros
, enum machine_mode mode
)
1503 const struct real_format
*fmt
= NULL
;
1504 const REAL_VALUE_TYPE
*one
, *ten
;
1505 REAL_VALUE_TYPE r
, pten
, u
, v
;
1506 int dec_exp
, cmp_one
, digit
;
1508 char *p
, *first
, *last
;
1512 if (mode
!= VOIDmode
)
1514 fmt
= REAL_MODE_FORMAT (mode
);
1522 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1527 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1530 /* ??? Print the significand as well, if not canonical? */
1531 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1532 (r_orig
->signalling
? 'S' : 'Q'));
1540 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1544 /* Bound the number of digits printed by the size of the representation. */
1545 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1546 if (digits
== 0 || digits
> max_digits
)
1547 digits
= max_digits
;
1549 /* Estimate the decimal exponent, and compute the length of the string it
1550 will print as. Be conservative and add one to account for possible
1551 overflow or rounding error. */
1552 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1553 for (max_digits
= 1; dec_exp
; max_digits
++)
1556 /* Bound the number of digits printed by the size of the output buffer. */
1557 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1558 gcc_assert (max_digits
<= buf_size
);
1559 if (digits
> max_digits
)
1560 digits
= max_digits
;
1562 one
= real_digit (1);
1563 ten
= ten_to_ptwo (0);
1571 cmp_one
= do_compare (&r
, one
, 0);
1576 /* Number is greater than one. Convert significand to an integer
1577 and strip trailing decimal zeros. */
1580 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1582 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1583 m
= floor_log2 (max_digits
);
1585 /* Iterate over the bits of the possible powers of 10 that might
1586 be present in U and eliminate them. That is, if we find that
1587 10**2**M divides U evenly, keep the division and increase
1593 do_divide (&t
, &u
, ten_to_ptwo (m
));
1594 do_fix_trunc (&v
, &t
);
1595 if (cmp_significands (&v
, &t
) == 0)
1603 /* Revert the scaling to integer that we performed earlier. */
1604 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1605 - (SIGNIFICAND_BITS
- 1));
1608 /* Find power of 10. Do this by dividing out 10**2**M when
1609 this is larger than the current remainder. Fill PTEN with
1610 the power of 10 that we compute. */
1611 if (REAL_EXP (&r
) > 0)
1613 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1616 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1617 if (do_compare (&u
, ptentwo
, 0) >= 0)
1619 do_divide (&u
, &u
, ptentwo
);
1620 do_multiply (&pten
, &pten
, ptentwo
);
1627 /* We managed to divide off enough tens in the above reduction
1628 loop that we've now got a negative exponent. Fall into the
1629 less-than-one code to compute the proper value for PTEN. */
1636 /* Number is less than one. Pad significand with leading
1642 /* Stop if we'd shift bits off the bottom. */
1646 do_multiply (&u
, &v
, ten
);
1648 /* Stop if we're now >= 1. */
1649 if (REAL_EXP (&u
) > 0)
1657 /* Find power of 10. Do this by multiplying in P=10**2**M when
1658 the current remainder is smaller than 1/P. Fill PTEN with the
1659 power of 10 that we compute. */
1660 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1663 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1664 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1666 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1668 do_multiply (&v
, &v
, ptentwo
);
1669 do_multiply (&pten
, &pten
, ptentwo
);
1675 /* Invert the positive power of 10 that we've collected so far. */
1676 do_divide (&pten
, one
, &pten
);
1684 /* At this point, PTEN should contain the nearest power of 10 smaller
1685 than R, such that this division produces the first digit.
1687 Using a divide-step primitive that returns the complete integral
1688 remainder avoids the rounding error that would be produced if
1689 we were to use do_divide here and then simply multiply by 10 for
1690 each subsequent digit. */
1692 digit
= rtd_divmod (&r
, &pten
);
1694 /* Be prepared for error in that division via underflow ... */
1695 if (digit
== 0 && cmp_significand_0 (&r
))
1697 /* Multiply by 10 and try again. */
1698 do_multiply (&r
, &r
, ten
);
1699 digit
= rtd_divmod (&r
, &pten
);
1701 gcc_assert (digit
!= 0);
1704 /* ... or overflow. */
1714 gcc_assert (digit
<= 10);
1718 /* Generate subsequent digits. */
1719 while (--digits
> 0)
1721 do_multiply (&r
, &r
, ten
);
1722 digit
= rtd_divmod (&r
, &pten
);
1727 /* Generate one more digit with which to do rounding. */
1728 do_multiply (&r
, &r
, ten
);
1729 digit
= rtd_divmod (&r
, &pten
);
1731 /* Round the result. */
1732 if (fmt
&& fmt
->round_towards_zero
)
1734 /* If the format uses round towards zero when parsing the string
1735 back in, we need to always round away from zero here. */
1736 if (cmp_significand_0 (&r
))
1738 round_up
= digit
> 0;
1744 /* Round to nearest. If R is nonzero there are additional
1745 nonzero digits to be extracted. */
1746 if (cmp_significand_0 (&r
))
1748 /* Round to even. */
1749 else if ((p
[-1] - '0') & 1)
1753 round_up
= digit
> 5;
1770 /* Carry out of the first digit. This means we had all 9's and
1771 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1779 /* Insert the decimal point. */
1780 first
[0] = first
[1];
1783 /* If requested, drop trailing zeros. Never crop past "1.0". */
1784 if (crop_trailing_zeros
)
1785 while (last
> first
+ 3 && last
[-1] == '0')
1788 /* Append the exponent. */
1789 sprintf (last
, "e%+d", dec_exp
);
1791 #ifdef ENABLE_CHECKING
1792 /* Verify that we can read the original value back in. */
1793 if (mode
!= VOIDmode
)
1795 real_from_string (&r
, str
);
1796 real_convert (&r
, mode
, &r
);
1797 gcc_assert (real_identical (&r
, r_orig
));
1802 /* Likewise, except always uses round-to-nearest. */
1805 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1806 size_t digits
, int crop_trailing_zeros
)
1808 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1809 digits
, crop_trailing_zeros
, VOIDmode
);
1812 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1813 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1814 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1815 strip trailing zeros. */
1818 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1819 size_t digits
, int crop_trailing_zeros
)
1821 int i
, j
, exp
= REAL_EXP (r
);
1834 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1837 /* ??? Print the significand as well, if not canonical? */
1838 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1839 (r
->signalling
? 'S' : 'Q'));
1847 /* Hexadecimal format for decimal floats is not interesting. */
1848 strcpy (str
, "N/A");
1853 digits
= SIGNIFICAND_BITS
/ 4;
1855 /* Bound the number of digits printed by the size of the output buffer. */
1857 sprintf (exp_buf
, "p%+d", exp
);
1858 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1859 gcc_assert (max_digits
<= buf_size
);
1860 if (digits
> max_digits
)
1861 digits
= max_digits
;
1872 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1873 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1875 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1881 if (crop_trailing_zeros
)
1882 while (p
> first
+ 1 && p
[-1] == '0')
1885 sprintf (p
, "p%+d", exp
);
1888 /* Initialize R from a decimal or hexadecimal string. The string is
1889 assumed to have been syntax checked already. Return -1 if the
1890 value underflows, +1 if overflows, and 0 otherwise. */
1893 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1905 else if (*str
== '+')
1908 if (!strncmp (str
, "QNaN", 4))
1910 get_canonical_qnan (r
, sign
);
1913 else if (!strncmp (str
, "SNaN", 4))
1915 get_canonical_snan (r
, sign
);
1918 else if (!strncmp (str
, "Inf", 3))
1924 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1926 /* Hexadecimal floating point. */
1927 int pos
= SIGNIFICAND_BITS
- 4, d
;
1935 d
= hex_value (*str
);
1940 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1941 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1945 /* Ensure correct rounding by setting last bit if there is
1946 a subsequent nonzero digit. */
1954 if (pos
== SIGNIFICAND_BITS
- 4)
1961 d
= hex_value (*str
);
1966 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1967 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1971 /* Ensure correct rounding by setting last bit if there is
1972 a subsequent nonzero digit. */
1978 /* If the mantissa is zero, ignore the exponent. */
1979 if (!cmp_significand_0 (r
))
1982 if (*str
== 'p' || *str
== 'P')
1984 bool exp_neg
= false;
1992 else if (*str
== '+')
1996 while (ISDIGIT (*str
))
2002 /* Overflowed the exponent. */
2017 SET_REAL_EXP (r
, exp
);
2023 /* Decimal floating point. */
2024 const REAL_VALUE_TYPE
*ten
= ten_to_ptwo (0);
2029 while (ISDIGIT (*str
))
2032 do_multiply (r
, r
, ten
);
2034 do_add (r
, r
, real_digit (d
), 0);
2039 if (r
->cl
== rvc_zero
)
2044 while (ISDIGIT (*str
))
2047 do_multiply (r
, r
, ten
);
2049 do_add (r
, r
, real_digit (d
), 0);
2054 /* If the mantissa is zero, ignore the exponent. */
2055 if (r
->cl
== rvc_zero
)
2058 if (*str
== 'e' || *str
== 'E')
2060 bool exp_neg
= false;
2068 else if (*str
== '+')
2072 while (ISDIGIT (*str
))
2078 /* Overflowed the exponent. */
2092 times_pten (r
, exp
);
2111 /* Legacy. Similar, but return the result directly. */
2114 real_from_string2 (const char *s
, enum machine_mode mode
)
2118 real_from_string (&r
, s
);
2119 if (mode
!= VOIDmode
)
2120 real_convert (&r
, mode
, &r
);
2125 /* Initialize R from string S and desired MODE. */
2128 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, enum machine_mode mode
)
2130 if (DECIMAL_FLOAT_MODE_P (mode
))
2131 decimal_real_from_string (r
, s
);
2133 real_from_string (r
, s
);
2135 if (mode
!= VOIDmode
)
2136 real_convert (r
, mode
, r
);
2139 /* Initialize R from the integer pair HIGH+LOW. */
2142 real_from_integer (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2143 unsigned HOST_WIDE_INT low
, HOST_WIDE_INT high
,
2146 if (low
== 0 && high
== 0)
2150 memset (r
, 0, sizeof (*r
));
2152 r
->sign
= high
< 0 && !unsigned_p
;
2153 SET_REAL_EXP (r
, 2 * HOST_BITS_PER_WIDE_INT
);
2164 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2166 r
->sig
[SIGSZ
-1] = high
;
2167 r
->sig
[SIGSZ
-2] = low
;
2171 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2172 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2173 r
->sig
[SIGSZ
-2] = high
;
2174 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2175 r
->sig
[SIGSZ
-4] = low
;
2181 if (DECIMAL_FLOAT_MODE_P (mode
))
2182 decimal_from_integer (r
);
2183 else if (mode
!= VOIDmode
)
2184 real_convert (r
, mode
, r
);
2187 /* Render R, an integral value, as a floating point constant with no
2188 specified exponent. */
2191 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2194 int dec_exp
, digit
, digits
;
2195 REAL_VALUE_TYPE r
, pten
;
2201 if (r
.cl
== rvc_zero
)
2210 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2211 digits
= dec_exp
+ 1;
2212 gcc_assert ((digits
+ 2) < (int)buf_size
);
2214 pten
= *real_digit (1);
2215 times_pten (&pten
, dec_exp
);
2221 digit
= rtd_divmod (&r
, &pten
);
2222 gcc_assert (digit
>= 0 && digit
<= 9);
2224 while (--digits
> 0)
2227 digit
= rtd_divmod (&r
, &pten
);
2234 /* Convert a real with an integral value to decimal float. */
2237 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2241 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2242 decimal_real_from_string (r
, str
);
2245 /* Returns 10**2**N. */
2247 static const REAL_VALUE_TYPE
*
2250 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2252 gcc_assert (n
>= 0);
2253 gcc_assert (n
< EXP_BITS
);
2255 if (tens
[n
].cl
== rvc_zero
)
2257 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2259 HOST_WIDE_INT t
= 10;
2262 for (i
= 0; i
< n
; ++i
)
2265 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2269 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2270 do_multiply (&tens
[n
], t
, t
);
2277 /* Returns 10**(-2**N). */
2279 static const REAL_VALUE_TYPE
*
2280 ten_to_mptwo (int n
)
2282 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2284 gcc_assert (n
>= 0);
2285 gcc_assert (n
< EXP_BITS
);
2287 if (tens
[n
].cl
== rvc_zero
)
2288 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2295 static const REAL_VALUE_TYPE
*
2298 static REAL_VALUE_TYPE num
[10];
2300 gcc_assert (n
>= 0);
2301 gcc_assert (n
<= 9);
2303 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2304 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2309 /* Multiply R by 10**EXP. */
2312 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2314 REAL_VALUE_TYPE pten
, *rr
;
2315 bool negative
= (exp
< 0);
2321 pten
= *real_digit (1);
2327 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2329 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2332 do_divide (r
, r
, &pten
);
2335 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2337 const REAL_VALUE_TYPE
*
2340 static REAL_VALUE_TYPE value
;
2342 /* Initialize mathematical constants for constant folding builtins.
2343 These constants need to be given to at least 160 bits precision. */
2344 if (value
.cl
== rvc_zero
)
2347 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2348 mpfr_set_ui (m
, 1, GMP_RNDN
);
2349 mpfr_exp (m
, m
, GMP_RNDN
);
2350 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2357 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2359 const REAL_VALUE_TYPE
*
2360 dconst_third_ptr (void)
2362 static REAL_VALUE_TYPE value
;
2364 /* Initialize mathematical constants for constant folding builtins.
2365 These constants need to be given to at least 160 bits precision. */
2366 if (value
.cl
== rvc_zero
)
2368 real_arithmetic (&value
, RDIV_EXPR
, &dconst1
, real_digit (3));
2373 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2375 const REAL_VALUE_TYPE
*
2376 dconst_sqrt2_ptr (void)
2378 static REAL_VALUE_TYPE value
;
2380 /* Initialize mathematical constants for constant folding builtins.
2381 These constants need to be given to at least 160 bits precision. */
2382 if (value
.cl
== rvc_zero
)
2385 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2386 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2387 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2393 /* Fills R with +Inf. */
2396 real_inf (REAL_VALUE_TYPE
*r
)
2401 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2402 we force a QNaN, else we force an SNaN. The string, if not empty,
2403 is parsed as a number and placed in the significand. Return true
2404 if the string was successfully parsed. */
2407 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2408 enum machine_mode mode
)
2410 const struct real_format
*fmt
;
2412 fmt
= REAL_MODE_FORMAT (mode
);
2418 get_canonical_qnan (r
, 0);
2420 get_canonical_snan (r
, 0);
2426 memset (r
, 0, sizeof (*r
));
2429 /* Parse akin to strtol into the significand of R. */
2431 while (ISSPACE (*str
))
2435 else if (*str
== '+')
2440 if (*str
== 'x' || *str
== 'X')
2449 while ((d
= hex_value (*str
)) < base
)
2456 lshift_significand (r
, r
, 3);
2459 lshift_significand (r
, r
, 4);
2462 lshift_significand_1 (&u
, r
);
2463 lshift_significand (r
, r
, 3);
2464 add_significands (r
, r
, &u
);
2472 add_significands (r
, r
, &u
);
2477 /* Must have consumed the entire string for success. */
2481 /* Shift the significand into place such that the bits
2482 are in the most significant bits for the format. */
2483 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2485 /* Our MSB is always unset for NaNs. */
2486 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2488 /* Force quiet or signalling NaN. */
2489 r
->signalling
= !quiet
;
2495 /* Fills R with the largest finite value representable in mode MODE.
2496 If SIGN is nonzero, R is set to the most negative finite value. */
2499 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
2501 const struct real_format
*fmt
;
2504 fmt
= REAL_MODE_FORMAT (mode
);
2506 memset (r
, 0, sizeof (*r
));
2509 decimal_real_maxval (r
, sign
, mode
);
2514 SET_REAL_EXP (r
, fmt
->emax
);
2516 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2517 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2518 clear_significand_below (r
, np2
);
2520 if (fmt
->pnan
< fmt
->p
)
2521 /* This is an IBM extended double format made up of two IEEE
2522 doubles. The value of the long double is the sum of the
2523 values of the two parts. The most significant part is
2524 required to be the value of the long double rounded to the
2525 nearest double. Rounding means we need a slightly smaller
2526 value for LDBL_MAX. */
2527 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2531 /* Fills R with 2**N. */
2534 real_2expN (REAL_VALUE_TYPE
*r
, int n
, enum machine_mode fmode
)
2536 memset (r
, 0, sizeof (*r
));
2541 else if (n
< -MAX_EXP
)
2546 SET_REAL_EXP (r
, n
);
2547 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2549 if (DECIMAL_FLOAT_MODE_P (fmode
))
2550 decimal_real_convert (r
, fmode
, r
);
2555 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2559 bool round_up
= false;
2565 decimal_round_for_format (fmt
, r
);
2568 /* FIXME. We can come here via fp_easy_constant
2569 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2570 investigated whether this convert needs to be here, or
2571 something else is missing. */
2572 decimal_real_convert (r
, DFmode
, r
);
2576 emin2m1
= fmt
->emin
- 1;
2579 np2
= SIGNIFICAND_BITS
- p2
;
2583 get_zero (r
, r
->sign
);
2585 if (!fmt
->has_signed_zero
)
2590 get_inf (r
, r
->sign
);
2595 clear_significand_below (r
, np2
);
2605 /* Check the range of the exponent. If we're out of range,
2606 either underflow or overflow. */
2607 if (REAL_EXP (r
) > emax2
)
2609 else if (REAL_EXP (r
) <= emin2m1
)
2613 if (!fmt
->has_denorm
)
2615 /* Don't underflow completely until we've had a chance to round. */
2616 if (REAL_EXP (r
) < emin2m1
)
2621 diff
= emin2m1
- REAL_EXP (r
) + 1;
2625 /* De-normalize the significand. */
2626 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2627 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2631 if (!fmt
->round_towards_zero
)
2633 /* There are P2 true significand bits, followed by one guard bit,
2634 followed by one sticky bit, followed by stuff. Fold nonzero
2635 stuff into the sticky bit. */
2636 unsigned long sticky
;
2640 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2641 sticky
|= r
->sig
[i
];
2643 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2645 guard
= test_significand_bit (r
, np2
- 1);
2646 lsb
= test_significand_bit (r
, np2
);
2648 /* Round to even. */
2649 round_up
= guard
&& (sticky
|| lsb
);
2656 set_significand_bit (&u
, np2
);
2658 if (add_significands (r
, r
, &u
))
2660 /* Overflow. Means the significand had been all ones, and
2661 is now all zeros. Need to increase the exponent, and
2662 possibly re-normalize it. */
2663 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2664 if (REAL_EXP (r
) > emax2
)
2666 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2670 /* Catch underflow that we deferred until after rounding. */
2671 if (REAL_EXP (r
) <= emin2m1
)
2674 /* Clear out trailing garbage. */
2675 clear_significand_below (r
, np2
);
2678 /* Extend or truncate to a new mode. */
2681 real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2682 const REAL_VALUE_TYPE
*a
)
2684 const struct real_format
*fmt
;
2686 fmt
= REAL_MODE_FORMAT (mode
);
2691 if (a
->decimal
|| fmt
->b
== 10)
2692 decimal_real_convert (r
, mode
, a
);
2694 round_for_format (fmt
, r
);
2696 /* round_for_format de-normalizes denormals. Undo just that part. */
2697 if (r
->cl
== rvc_normal
)
2701 /* Legacy. Likewise, except return the struct directly. */
2704 real_value_truncate (enum machine_mode mode
, REAL_VALUE_TYPE a
)
2707 real_convert (&r
, mode
, &a
);
2711 /* Return true if truncating to MODE is exact. */
2714 exact_real_truncate (enum machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2716 const struct real_format
*fmt
;
2720 fmt
= REAL_MODE_FORMAT (mode
);
2723 /* Don't allow conversion to denormals. */
2724 emin2m1
= fmt
->emin
- 1;
2725 if (REAL_EXP (a
) <= emin2m1
)
2728 /* After conversion to the new mode, the value must be identical. */
2729 real_convert (&t
, mode
, a
);
2730 return real_identical (&t
, a
);
2733 /* Write R to the given target format. Place the words of the result
2734 in target word order in BUF. There are always 32 bits in each
2735 long, no matter the size of the host long.
2737 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2740 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2741 const struct real_format
*fmt
)
2747 round_for_format (fmt
, &r
);
2751 (*fmt
->encode
) (fmt
, buf
, &r
);
2756 /* Similar, but look up the format from MODE. */
2759 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, enum machine_mode mode
)
2761 const struct real_format
*fmt
;
2763 fmt
= REAL_MODE_FORMAT (mode
);
2766 return real_to_target_fmt (buf
, r
, fmt
);
2769 /* Read R from the given target format. Read the words of the result
2770 in target word order in BUF. There are always 32 bits in each
2771 long, no matter the size of the host long. */
2774 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2775 const struct real_format
*fmt
)
2777 (*fmt
->decode
) (fmt
, r
, buf
);
2780 /* Similar, but look up the format from MODE. */
2783 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, enum machine_mode mode
)
2785 const struct real_format
*fmt
;
2787 fmt
= REAL_MODE_FORMAT (mode
);
2790 (*fmt
->decode
) (fmt
, r
, buf
);
2793 /* Return the number of bits of the largest binary value that the
2794 significand of MODE will hold. */
2795 /* ??? Legacy. Should get access to real_format directly. */
2798 significand_size (enum machine_mode mode
)
2800 const struct real_format
*fmt
;
2802 fmt
= REAL_MODE_FORMAT (mode
);
2808 /* Return the size in bits of the largest binary value that can be
2809 held by the decimal coefficient for this mode. This is one more
2810 than the number of bits required to hold the largest coefficient
2812 double log2_10
= 3.3219281;
2813 return fmt
->p
* log2_10
;
2818 /* Return a hash value for the given real value. */
2819 /* ??? The "unsigned int" return value is intended to be hashval_t,
2820 but I didn't want to pull hashtab.h into real.h. */
2823 real_hash (const REAL_VALUE_TYPE
*r
)
2828 h
= r
->cl
| (r
->sign
<< 2);
2836 h
|= REAL_EXP (r
) << 3;
2841 h
^= (unsigned int)-1;
2850 if (sizeof(unsigned long) > sizeof(unsigned int))
2851 for (i
= 0; i
< SIGSZ
; ++i
)
2853 unsigned long s
= r
->sig
[i
];
2854 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2857 for (i
= 0; i
< SIGSZ
; ++i
)
2863 /* IEEE single-precision format. */
2865 static void encode_ieee_single (const struct real_format
*fmt
,
2866 long *, const REAL_VALUE_TYPE
*);
2867 static void decode_ieee_single (const struct real_format
*,
2868 REAL_VALUE_TYPE
*, const long *);
2871 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2872 const REAL_VALUE_TYPE
*r
)
2874 unsigned long image
, sig
, exp
;
2875 unsigned long sign
= r
->sign
;
2876 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2879 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2890 image
|= 0x7fffffff;
2897 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2898 if (r
->signalling
== fmt
->qnan_msb_set
)
2909 image
|= 0x7fffffff;
2913 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2914 whereas the intermediate representation is 0.F x 2**exp.
2915 Which means we're off by one. */
2919 exp
= REAL_EXP (r
) + 127 - 1;
2932 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2935 unsigned long image
= buf
[0] & 0xffffffff;
2936 bool sign
= (image
>> 31) & 1;
2937 int exp
= (image
>> 23) & 0xff;
2939 memset (r
, 0, sizeof (*r
));
2940 image
<<= HOST_BITS_PER_LONG
- 24;
2945 if (image
&& fmt
->has_denorm
)
2949 SET_REAL_EXP (r
, -126);
2950 r
->sig
[SIGSZ
-1] = image
<< 1;
2953 else if (fmt
->has_signed_zero
)
2956 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2962 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2963 ^ fmt
->qnan_msb_set
);
2964 r
->sig
[SIGSZ
-1] = image
;
2976 SET_REAL_EXP (r
, exp
- 127 + 1);
2977 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2981 const struct real_format ieee_single_format
=
3002 const struct real_format mips_single_format
=
3023 const struct real_format motorola_single_format
=
3044 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3045 single precision with the following differences:
3046 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3048 - NaNs are not supported.
3049 - The range of non-zero numbers in binary is
3050 (001)[1.]000...000 to (255)[1.]111...111.
3051 - Denormals can be represented, but are treated as +0.0 when
3052 used as an operand and are never generated as a result.
3053 - -0.0 can be represented, but a zero result is always +0.0.
3054 - the only supported rounding mode is trunction (towards zero). */
3055 const struct real_format spu_single_format
=
3076 /* IEEE double-precision format. */
3078 static void encode_ieee_double (const struct real_format
*fmt
,
3079 long *, const REAL_VALUE_TYPE
*);
3080 static void decode_ieee_double (const struct real_format
*,
3081 REAL_VALUE_TYPE
*, const long *);
3084 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3085 const REAL_VALUE_TYPE
*r
)
3087 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3088 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3090 image_hi
= r
->sign
<< 31;
3093 if (HOST_BITS_PER_LONG
== 64)
3095 sig_hi
= r
->sig
[SIGSZ
-1];
3096 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3097 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3101 sig_hi
= r
->sig
[SIGSZ
-1];
3102 sig_lo
= r
->sig
[SIGSZ
-2];
3103 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3104 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3114 image_hi
|= 2047 << 20;
3117 image_hi
|= 0x7fffffff;
3118 image_lo
= 0xffffffff;
3127 if (fmt
->canonical_nan_lsbs_set
)
3129 sig_hi
= (1 << 19) - 1;
3130 sig_lo
= 0xffffffff;
3138 if (r
->signalling
== fmt
->qnan_msb_set
)
3139 sig_hi
&= ~(1 << 19);
3142 if (sig_hi
== 0 && sig_lo
== 0)
3145 image_hi
|= 2047 << 20;
3151 image_hi
|= 0x7fffffff;
3152 image_lo
= 0xffffffff;
3157 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3158 whereas the intermediate representation is 0.F x 2**exp.
3159 Which means we're off by one. */
3163 exp
= REAL_EXP (r
) + 1023 - 1;
3164 image_hi
|= exp
<< 20;
3173 if (FLOAT_WORDS_BIG_ENDIAN
)
3174 buf
[0] = image_hi
, buf
[1] = image_lo
;
3176 buf
[0] = image_lo
, buf
[1] = image_hi
;
3180 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3183 unsigned long image_hi
, image_lo
;
3187 if (FLOAT_WORDS_BIG_ENDIAN
)
3188 image_hi
= buf
[0], image_lo
= buf
[1];
3190 image_lo
= buf
[0], image_hi
= buf
[1];
3191 image_lo
&= 0xffffffff;
3192 image_hi
&= 0xffffffff;
3194 sign
= (image_hi
>> 31) & 1;
3195 exp
= (image_hi
>> 20) & 0x7ff;
3197 memset (r
, 0, sizeof (*r
));
3199 image_hi
<<= 32 - 21;
3200 image_hi
|= image_lo
>> 21;
3201 image_hi
&= 0x7fffffff;
3202 image_lo
<<= 32 - 21;
3206 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3210 SET_REAL_EXP (r
, -1022);
3211 if (HOST_BITS_PER_LONG
== 32)
3213 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3215 r
->sig
[SIGSZ
-1] = image_hi
;
3216 r
->sig
[SIGSZ
-2] = image_lo
;
3220 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3221 r
->sig
[SIGSZ
-1] = image_hi
;
3225 else if (fmt
->has_signed_zero
)
3228 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3230 if (image_hi
|| image_lo
)
3234 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3235 if (HOST_BITS_PER_LONG
== 32)
3237 r
->sig
[SIGSZ
-1] = image_hi
;
3238 r
->sig
[SIGSZ
-2] = image_lo
;
3241 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3253 SET_REAL_EXP (r
, exp
- 1023 + 1);
3254 if (HOST_BITS_PER_LONG
== 32)
3256 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3257 r
->sig
[SIGSZ
-2] = image_lo
;
3260 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3264 const struct real_format ieee_double_format
=
3285 const struct real_format mips_double_format
=
3306 const struct real_format motorola_double_format
=
3327 /* IEEE extended real format. This comes in three flavors: Intel's as
3328 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3329 12- and 16-byte images may be big- or little endian; Motorola's is
3330 always big endian. */
3332 /* Helper subroutine which converts from the internal format to the
3333 12-byte little-endian Intel format. Functions below adjust this
3334 for the other possible formats. */
3336 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3337 const REAL_VALUE_TYPE
*r
)
3339 unsigned long image_hi
, sig_hi
, sig_lo
;
3340 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3342 image_hi
= r
->sign
<< 15;
3343 sig_hi
= sig_lo
= 0;
3355 /* Intel requires the explicit integer bit to be set, otherwise
3356 it considers the value a "pseudo-infinity". Motorola docs
3357 say it doesn't care. */
3358 sig_hi
= 0x80000000;
3363 sig_lo
= sig_hi
= 0xffffffff;
3373 if (fmt
->canonical_nan_lsbs_set
)
3375 sig_hi
= (1 << 30) - 1;
3376 sig_lo
= 0xffffffff;
3379 else if (HOST_BITS_PER_LONG
== 32)
3381 sig_hi
= r
->sig
[SIGSZ
-1];
3382 sig_lo
= r
->sig
[SIGSZ
-2];
3386 sig_lo
= r
->sig
[SIGSZ
-1];
3387 sig_hi
= sig_lo
>> 31 >> 1;
3388 sig_lo
&= 0xffffffff;
3390 if (r
->signalling
== fmt
->qnan_msb_set
)
3391 sig_hi
&= ~(1 << 30);
3394 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3397 /* Intel requires the explicit integer bit to be set, otherwise
3398 it considers the value a "pseudo-nan". Motorola docs say it
3400 sig_hi
|= 0x80000000;
3405 sig_lo
= sig_hi
= 0xffffffff;
3411 int exp
= REAL_EXP (r
);
3413 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3414 whereas the intermediate representation is 0.F x 2**exp.
3415 Which means we're off by one.
3417 Except for Motorola, which consider exp=0 and explicit
3418 integer bit set to continue to be normalized. In theory
3419 this discrepancy has been taken care of by the difference
3420 in fmt->emin in round_for_format. */
3427 gcc_assert (exp
>= 0);
3431 if (HOST_BITS_PER_LONG
== 32)
3433 sig_hi
= r
->sig
[SIGSZ
-1];
3434 sig_lo
= r
->sig
[SIGSZ
-2];
3438 sig_lo
= r
->sig
[SIGSZ
-1];
3439 sig_hi
= sig_lo
>> 31 >> 1;
3440 sig_lo
&= 0xffffffff;
3449 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3452 /* Convert from the internal format to the 12-byte Motorola format
3453 for an IEEE extended real. */
3455 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3456 const REAL_VALUE_TYPE
*r
)
3459 encode_ieee_extended (fmt
, intermed
, r
);
3461 /* Motorola chips are assumed always to be big-endian. Also, the
3462 padding in a Motorola extended real goes between the exponent and
3463 the mantissa. At this point the mantissa is entirely within
3464 elements 0 and 1 of intermed, and the exponent entirely within
3465 element 2, so all we have to do is swap the order around, and
3466 shift element 2 left 16 bits. */
3467 buf
[0] = intermed
[2] << 16;
3468 buf
[1] = intermed
[1];
3469 buf
[2] = intermed
[0];
3472 /* Convert from the internal format to the 12-byte Intel format for
3473 an IEEE extended real. */
3475 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3476 const REAL_VALUE_TYPE
*r
)
3478 if (FLOAT_WORDS_BIG_ENDIAN
)
3480 /* All the padding in an Intel-format extended real goes at the high
3481 end, which in this case is after the mantissa, not the exponent.
3482 Therefore we must shift everything down 16 bits. */
3484 encode_ieee_extended (fmt
, intermed
, r
);
3485 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3486 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3487 buf
[2] = (intermed
[0] << 16);
3490 /* encode_ieee_extended produces what we want directly. */
3491 encode_ieee_extended (fmt
, buf
, r
);
3494 /* Convert from the internal format to the 16-byte Intel format for
3495 an IEEE extended real. */
3497 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3498 const REAL_VALUE_TYPE
*r
)
3500 /* All the padding in an Intel-format extended real goes at the high end. */
3501 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3505 /* As above, we have a helper function which converts from 12-byte
3506 little-endian Intel format to internal format. Functions below
3507 adjust for the other possible formats. */
3509 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3512 unsigned long image_hi
, sig_hi
, sig_lo
;
3516 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3517 sig_lo
&= 0xffffffff;
3518 sig_hi
&= 0xffffffff;
3519 image_hi
&= 0xffffffff;
3521 sign
= (image_hi
>> 15) & 1;
3522 exp
= image_hi
& 0x7fff;
3524 memset (r
, 0, sizeof (*r
));
3528 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3533 /* When the IEEE format contains a hidden bit, we know that
3534 it's zero at this point, and so shift up the significand
3535 and decrease the exponent to match. In this case, Motorola
3536 defines the explicit integer bit to be valid, so we don't
3537 know whether the msb is set or not. */
3538 SET_REAL_EXP (r
, fmt
->emin
);
3539 if (HOST_BITS_PER_LONG
== 32)
3541 r
->sig
[SIGSZ
-1] = sig_hi
;
3542 r
->sig
[SIGSZ
-2] = sig_lo
;
3545 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3549 else if (fmt
->has_signed_zero
)
3552 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3554 /* See above re "pseudo-infinities" and "pseudo-nans".
3555 Short summary is that the MSB will likely always be
3556 set, and that we don't care about it. */
3557 sig_hi
&= 0x7fffffff;
3559 if (sig_hi
|| sig_lo
)
3563 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3564 if (HOST_BITS_PER_LONG
== 32)
3566 r
->sig
[SIGSZ
-1] = sig_hi
;
3567 r
->sig
[SIGSZ
-2] = sig_lo
;
3570 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3582 SET_REAL_EXP (r
, exp
- 16383 + 1);
3583 if (HOST_BITS_PER_LONG
== 32)
3585 r
->sig
[SIGSZ
-1] = sig_hi
;
3586 r
->sig
[SIGSZ
-2] = sig_lo
;
3589 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3593 /* Convert from the internal format to the 12-byte Motorola format
3594 for an IEEE extended real. */
3596 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3601 /* Motorola chips are assumed always to be big-endian. Also, the
3602 padding in a Motorola extended real goes between the exponent and
3603 the mantissa; remove it. */
3604 intermed
[0] = buf
[2];
3605 intermed
[1] = buf
[1];
3606 intermed
[2] = (unsigned long)buf
[0] >> 16;
3608 decode_ieee_extended (fmt
, r
, intermed
);
3611 /* Convert from the internal format to the 12-byte Intel format for
3612 an IEEE extended real. */
3614 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3617 if (FLOAT_WORDS_BIG_ENDIAN
)
3619 /* All the padding in an Intel-format extended real goes at the high
3620 end, which in this case is after the mantissa, not the exponent.
3621 Therefore we must shift everything up 16 bits. */
3624 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3625 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3626 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3628 decode_ieee_extended (fmt
, r
, intermed
);
3631 /* decode_ieee_extended produces what we want directly. */
3632 decode_ieee_extended (fmt
, r
, buf
);
3635 /* Convert from the internal format to the 16-byte Intel format for
3636 an IEEE extended real. */
3638 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3641 /* All the padding in an Intel-format extended real goes at the high end. */
3642 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3645 const struct real_format ieee_extended_motorola_format
=
3647 encode_ieee_extended_motorola
,
3648 decode_ieee_extended_motorola
,
3666 const struct real_format ieee_extended_intel_96_format
=
3668 encode_ieee_extended_intel_96
,
3669 decode_ieee_extended_intel_96
,
3687 const struct real_format ieee_extended_intel_128_format
=
3689 encode_ieee_extended_intel_128
,
3690 decode_ieee_extended_intel_128
,
3708 /* The following caters to i386 systems that set the rounding precision
3709 to 53 bits instead of 64, e.g. FreeBSD. */
3710 const struct real_format ieee_extended_intel_96_round_53_format
=
3712 encode_ieee_extended_intel_96
,
3713 decode_ieee_extended_intel_96
,
3731 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3732 numbers whose sum is equal to the extended precision value. The number
3733 with greater magnitude is first. This format has the same magnitude
3734 range as an IEEE double precision value, but effectively 106 bits of
3735 significand precision. Infinity and NaN are represented by their IEEE
3736 double precision value stored in the first number, the second number is
3737 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3739 static void encode_ibm_extended (const struct real_format
*fmt
,
3740 long *, const REAL_VALUE_TYPE
*);
3741 static void decode_ibm_extended (const struct real_format
*,
3742 REAL_VALUE_TYPE
*, const long *);
3745 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3746 const REAL_VALUE_TYPE
*r
)
3748 REAL_VALUE_TYPE u
, normr
, v
;
3749 const struct real_format
*base_fmt
;
3751 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3753 /* Renormalize R before doing any arithmetic on it. */
3755 if (normr
.cl
== rvc_normal
)
3758 /* u = IEEE double precision portion of significand. */
3760 round_for_format (base_fmt
, &u
);
3761 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3763 if (u
.cl
== rvc_normal
)
3765 do_add (&v
, &normr
, &u
, 1);
3766 /* Call round_for_format since we might need to denormalize. */
3767 round_for_format (base_fmt
, &v
);
3768 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3772 /* Inf, NaN, 0 are all representable as doubles, so the
3773 least-significant part can be 0.0. */
3780 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3783 REAL_VALUE_TYPE u
, v
;
3784 const struct real_format
*base_fmt
;
3786 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3787 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3789 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3791 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3792 do_add (r
, &u
, &v
, 0);
3798 const struct real_format ibm_extended_format
=
3800 encode_ibm_extended
,
3801 decode_ibm_extended
,
3819 const struct real_format mips_extended_format
=
3821 encode_ibm_extended
,
3822 decode_ibm_extended
,
3841 /* IEEE quad precision format. */
3843 static void encode_ieee_quad (const struct real_format
*fmt
,
3844 long *, const REAL_VALUE_TYPE
*);
3845 static void decode_ieee_quad (const struct real_format
*,
3846 REAL_VALUE_TYPE
*, const long *);
3849 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3850 const REAL_VALUE_TYPE
*r
)
3852 unsigned long image3
, image2
, image1
, image0
, exp
;
3853 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3856 image3
= r
->sign
<< 31;
3861 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3870 image3
|= 32767 << 16;
3873 image3
|= 0x7fffffff;
3874 image2
= 0xffffffff;
3875 image1
= 0xffffffff;
3876 image0
= 0xffffffff;
3883 image3
|= 32767 << 16;
3887 if (fmt
->canonical_nan_lsbs_set
)
3890 image2
= image1
= image0
= 0xffffffff;
3893 else if (HOST_BITS_PER_LONG
== 32)
3898 image3
|= u
.sig
[3] & 0xffff;
3903 image1
= image0
>> 31 >> 1;
3905 image3
|= (image2
>> 31 >> 1) & 0xffff;
3906 image0
&= 0xffffffff;
3907 image2
&= 0xffffffff;
3909 if (r
->signalling
== fmt
->qnan_msb_set
)
3913 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3918 image3
|= 0x7fffffff;
3919 image2
= 0xffffffff;
3920 image1
= 0xffffffff;
3921 image0
= 0xffffffff;
3926 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3927 whereas the intermediate representation is 0.F x 2**exp.
3928 Which means we're off by one. */
3932 exp
= REAL_EXP (r
) + 16383 - 1;
3933 image3
|= exp
<< 16;
3935 if (HOST_BITS_PER_LONG
== 32)
3940 image3
|= u
.sig
[3] & 0xffff;
3945 image1
= image0
>> 31 >> 1;
3947 image3
|= (image2
>> 31 >> 1) & 0xffff;
3948 image0
&= 0xffffffff;
3949 image2
&= 0xffffffff;
3957 if (FLOAT_WORDS_BIG_ENDIAN
)
3974 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3977 unsigned long image3
, image2
, image1
, image0
;
3981 if (FLOAT_WORDS_BIG_ENDIAN
)
3995 image0
&= 0xffffffff;
3996 image1
&= 0xffffffff;
3997 image2
&= 0xffffffff;
3999 sign
= (image3
>> 31) & 1;
4000 exp
= (image3
>> 16) & 0x7fff;
4003 memset (r
, 0, sizeof (*r
));
4007 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4012 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4013 if (HOST_BITS_PER_LONG
== 32)
4022 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4023 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4028 else if (fmt
->has_signed_zero
)
4031 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4033 if (image3
| image2
| image1
| image0
)
4037 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4039 if (HOST_BITS_PER_LONG
== 32)
4048 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4049 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4051 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4063 SET_REAL_EXP (r
, exp
- 16383 + 1);
4065 if (HOST_BITS_PER_LONG
== 32)
4074 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4075 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4077 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4078 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4082 const struct real_format ieee_quad_format
=
4103 const struct real_format mips_quad_format
=
4124 /* Descriptions of VAX floating point formats can be found beginning at
4126 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4128 The thing to remember is that they're almost IEEE, except for word
4129 order, exponent bias, and the lack of infinities, nans, and denormals.
4131 We don't implement the H_floating format here, simply because neither
4132 the VAX or Alpha ports use it. */
4134 static void encode_vax_f (const struct real_format
*fmt
,
4135 long *, const REAL_VALUE_TYPE
*);
4136 static void decode_vax_f (const struct real_format
*,
4137 REAL_VALUE_TYPE
*, const long *);
4138 static void encode_vax_d (const struct real_format
*fmt
,
4139 long *, const REAL_VALUE_TYPE
*);
4140 static void decode_vax_d (const struct real_format
*,
4141 REAL_VALUE_TYPE
*, const long *);
4142 static void encode_vax_g (const struct real_format
*fmt
,
4143 long *, const REAL_VALUE_TYPE
*);
4144 static void decode_vax_g (const struct real_format
*,
4145 REAL_VALUE_TYPE
*, const long *);
4148 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4149 const REAL_VALUE_TYPE
*r
)
4151 unsigned long sign
, exp
, sig
, image
;
4153 sign
= r
->sign
<< 15;
4163 image
= 0xffff7fff | sign
;
4167 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4168 exp
= REAL_EXP (r
) + 128;
4170 image
= (sig
<< 16) & 0xffff0000;
4184 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4185 REAL_VALUE_TYPE
*r
, const long *buf
)
4187 unsigned long image
= buf
[0] & 0xffffffff;
4188 int exp
= (image
>> 7) & 0xff;
4190 memset (r
, 0, sizeof (*r
));
4195 r
->sign
= (image
>> 15) & 1;
4196 SET_REAL_EXP (r
, exp
- 128);
4198 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4199 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4204 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4205 const REAL_VALUE_TYPE
*r
)
4207 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4212 image0
= image1
= 0;
4217 image0
= 0xffff7fff | sign
;
4218 image1
= 0xffffffff;
4222 /* Extract the significand into straight hi:lo. */
4223 if (HOST_BITS_PER_LONG
== 64)
4225 image0
= r
->sig
[SIGSZ
-1];
4226 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4227 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4231 image0
= r
->sig
[SIGSZ
-1];
4232 image1
= r
->sig
[SIGSZ
-2];
4233 image1
= (image0
<< 24) | (image1
>> 8);
4234 image0
= (image0
>> 8) & 0xffffff;
4237 /* Rearrange the half-words of the significand to match the
4239 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4240 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4242 /* Add the sign and exponent. */
4244 image0
|= (REAL_EXP (r
) + 128) << 7;
4251 if (FLOAT_WORDS_BIG_ENDIAN
)
4252 buf
[0] = image1
, buf
[1] = image0
;
4254 buf
[0] = image0
, buf
[1] = image1
;
4258 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4259 REAL_VALUE_TYPE
*r
, const long *buf
)
4261 unsigned long image0
, image1
;
4264 if (FLOAT_WORDS_BIG_ENDIAN
)
4265 image1
= buf
[0], image0
= buf
[1];
4267 image0
= buf
[0], image1
= buf
[1];
4268 image0
&= 0xffffffff;
4269 image1
&= 0xffffffff;
4271 exp
= (image0
>> 7) & 0xff;
4273 memset (r
, 0, sizeof (*r
));
4278 r
->sign
= (image0
>> 15) & 1;
4279 SET_REAL_EXP (r
, exp
- 128);
4281 /* Rearrange the half-words of the external format into
4282 proper ascending order. */
4283 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4284 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4286 if (HOST_BITS_PER_LONG
== 64)
4288 image0
= (image0
<< 31 << 1) | image1
;
4291 r
->sig
[SIGSZ
-1] = image0
;
4295 r
->sig
[SIGSZ
-1] = image0
;
4296 r
->sig
[SIGSZ
-2] = image1
;
4297 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4298 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4304 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4305 const REAL_VALUE_TYPE
*r
)
4307 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4312 image0
= image1
= 0;
4317 image0
= 0xffff7fff | sign
;
4318 image1
= 0xffffffff;
4322 /* Extract the significand into straight hi:lo. */
4323 if (HOST_BITS_PER_LONG
== 64)
4325 image0
= r
->sig
[SIGSZ
-1];
4326 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4327 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4331 image0
= r
->sig
[SIGSZ
-1];
4332 image1
= r
->sig
[SIGSZ
-2];
4333 image1
= (image0
<< 21) | (image1
>> 11);
4334 image0
= (image0
>> 11) & 0xfffff;
4337 /* Rearrange the half-words of the significand to match the
4339 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4340 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4342 /* Add the sign and exponent. */
4344 image0
|= (REAL_EXP (r
) + 1024) << 4;
4351 if (FLOAT_WORDS_BIG_ENDIAN
)
4352 buf
[0] = image1
, buf
[1] = image0
;
4354 buf
[0] = image0
, buf
[1] = image1
;
4358 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4359 REAL_VALUE_TYPE
*r
, const long *buf
)
4361 unsigned long image0
, image1
;
4364 if (FLOAT_WORDS_BIG_ENDIAN
)
4365 image1
= buf
[0], image0
= buf
[1];
4367 image0
= buf
[0], image1
= buf
[1];
4368 image0
&= 0xffffffff;
4369 image1
&= 0xffffffff;
4371 exp
= (image0
>> 4) & 0x7ff;
4373 memset (r
, 0, sizeof (*r
));
4378 r
->sign
= (image0
>> 15) & 1;
4379 SET_REAL_EXP (r
, exp
- 1024);
4381 /* Rearrange the half-words of the external format into
4382 proper ascending order. */
4383 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4384 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4386 if (HOST_BITS_PER_LONG
== 64)
4388 image0
= (image0
<< 31 << 1) | image1
;
4391 r
->sig
[SIGSZ
-1] = image0
;
4395 r
->sig
[SIGSZ
-1] = image0
;
4396 r
->sig
[SIGSZ
-2] = image1
;
4397 lshift_significand (r
, r
, 64 - 53);
4398 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4403 const struct real_format vax_f_format
=
4424 const struct real_format vax_d_format
=
4445 const struct real_format vax_g_format
=
4466 /* Encode real R into a single precision DFP value in BUF. */
4468 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4469 long *buf ATTRIBUTE_UNUSED
,
4470 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4472 encode_decimal32 (fmt
, buf
, r
);
4475 /* Decode a single precision DFP value in BUF into a real R. */
4477 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4478 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4479 const long *buf ATTRIBUTE_UNUSED
)
4481 decode_decimal32 (fmt
, r
, buf
);
4484 /* Encode real R into a double precision DFP value in BUF. */
4486 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4487 long *buf ATTRIBUTE_UNUSED
,
4488 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4490 encode_decimal64 (fmt
, buf
, r
);
4493 /* Decode a double precision DFP value in BUF into a real R. */
4495 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4496 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4497 const long *buf ATTRIBUTE_UNUSED
)
4499 decode_decimal64 (fmt
, r
, buf
);
4502 /* Encode real R into a quad precision DFP value in BUF. */
4504 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4505 long *buf ATTRIBUTE_UNUSED
,
4506 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4508 encode_decimal128 (fmt
, buf
, r
);
4511 /* Decode a quad precision DFP value in BUF into a real R. */
4513 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4514 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4515 const long *buf ATTRIBUTE_UNUSED
)
4517 decode_decimal128 (fmt
, r
, buf
);
4520 /* Single precision decimal floating point (IEEE 754). */
4521 const struct real_format decimal_single_format
=
4523 encode_decimal_single
,
4524 decode_decimal_single
,
4542 /* Double precision decimal floating point (IEEE 754). */
4543 const struct real_format decimal_double_format
=
4545 encode_decimal_double
,
4546 decode_decimal_double
,
4564 /* Quad precision decimal floating point (IEEE 754). */
4565 const struct real_format decimal_quad_format
=
4567 encode_decimal_quad
,
4568 decode_decimal_quad
,
4586 /* Encode half-precision floats. This routine is used both for the IEEE
4587 ARM alternative encodings. */
4589 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4590 const REAL_VALUE_TYPE
*r
)
4592 unsigned long image
, sig
, exp
;
4593 unsigned long sign
= r
->sign
;
4594 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4597 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4615 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4616 if (r
->signalling
== fmt
->qnan_msb_set
)
4631 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4632 whereas the intermediate representation is 0.F x 2**exp.
4633 Which means we're off by one. */
4637 exp
= REAL_EXP (r
) + 15 - 1;
4649 /* Decode half-precision floats. This routine is used both for the IEEE
4650 ARM alternative encodings. */
4652 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4655 unsigned long image
= buf
[0] & 0xffff;
4656 bool sign
= (image
>> 15) & 1;
4657 int exp
= (image
>> 10) & 0x1f;
4659 memset (r
, 0, sizeof (*r
));
4660 image
<<= HOST_BITS_PER_LONG
- 11;
4665 if (image
&& fmt
->has_denorm
)
4669 SET_REAL_EXP (r
, -14);
4670 r
->sig
[SIGSZ
-1] = image
<< 1;
4673 else if (fmt
->has_signed_zero
)
4676 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4682 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4683 ^ fmt
->qnan_msb_set
);
4684 r
->sig
[SIGSZ
-1] = image
;
4696 SET_REAL_EXP (r
, exp
- 15 + 1);
4697 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4701 /* Half-precision format, as specified in IEEE 754R. */
4702 const struct real_format ieee_half_format
=
4723 /* ARM's alternative half-precision format, similar to IEEE but with
4724 no reserved exponent value for NaNs and infinities; rather, it just
4725 extends the range of exponents by one. */
4726 const struct real_format arm_half_format
=
4747 /* A synthetic "format" for internal arithmetic. It's the size of the
4748 internal significand minus the two bits needed for proper rounding.
4749 The encode and decode routines exist only to satisfy our paranoia
4752 static void encode_internal (const struct real_format
*fmt
,
4753 long *, const REAL_VALUE_TYPE
*);
4754 static void decode_internal (const struct real_format
*,
4755 REAL_VALUE_TYPE
*, const long *);
4758 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4759 const REAL_VALUE_TYPE
*r
)
4761 memcpy (buf
, r
, sizeof (*r
));
4765 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4766 REAL_VALUE_TYPE
*r
, const long *buf
)
4768 memcpy (r
, buf
, sizeof (*r
));
4771 const struct real_format real_internal_format
=
4776 SIGNIFICAND_BITS
- 2,
4777 SIGNIFICAND_BITS
- 2,
4792 /* Calculate the square root of X in mode MODE, and store the result
4793 in R. Return TRUE if the operation does not raise an exception.
4794 For details see "High Precision Division and Square Root",
4795 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4796 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4799 real_sqrt (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4800 const REAL_VALUE_TYPE
*x
)
4802 static REAL_VALUE_TYPE halfthree
;
4803 static bool init
= false;
4804 REAL_VALUE_TYPE h
, t
, i
;
4807 /* sqrt(-0.0) is -0.0. */
4808 if (real_isnegzero (x
))
4814 /* Negative arguments return NaN. */
4817 get_canonical_qnan (r
, 0);
4821 /* Infinity and NaN return themselves. */
4822 if (!real_isfinite (x
))
4830 do_add (&halfthree
, &dconst1
, &dconsthalf
, 0);
4834 /* Initial guess for reciprocal sqrt, i. */
4835 exp
= real_exponent (x
);
4836 real_ldexp (&i
, &dconst1
, -exp
/2);
4838 /* Newton's iteration for reciprocal sqrt, i. */
4839 for (iter
= 0; iter
< 16; iter
++)
4841 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4842 do_multiply (&t
, x
, &i
);
4843 do_multiply (&h
, &t
, &i
);
4844 do_multiply (&t
, &h
, &dconsthalf
);
4845 do_add (&h
, &halfthree
, &t
, 1);
4846 do_multiply (&t
, &i
, &h
);
4848 /* Check for early convergence. */
4849 if (iter
>= 6 && real_identical (&i
, &t
))
4852 /* ??? Unroll loop to avoid copying. */
4856 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4857 do_multiply (&t
, x
, &i
);
4858 do_multiply (&h
, &t
, &i
);
4859 do_add (&i
, &dconst1
, &h
, 1);
4860 do_multiply (&h
, &t
, &i
);
4861 do_multiply (&i
, &dconsthalf
, &h
);
4862 do_add (&h
, &t
, &i
, 0);
4864 /* ??? We need a Tuckerman test to get the last bit. */
4866 real_convert (r
, mode
, &h
);
4870 /* Calculate X raised to the integer exponent N in mode MODE and store
4871 the result in R. Return true if the result may be inexact due to
4872 loss of precision. The algorithm is the classic "left-to-right binary
4873 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4874 Algorithms", "The Art of Computer Programming", Volume 2. */
4877 real_powi (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4878 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4880 unsigned HOST_WIDE_INT bit
;
4882 bool inexact
= false;
4894 /* Don't worry about overflow, from now on n is unsigned. */
4902 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4903 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4907 inexact
|= do_multiply (&t
, &t
, &t
);
4909 inexact
|= do_multiply (&t
, &t
, x
);
4917 inexact
|= do_divide (&t
, &dconst1
, &t
);
4919 real_convert (r
, mode
, &t
);
4923 /* Round X to the nearest integer not larger in absolute value, i.e.
4924 towards zero, placing the result in R in mode MODE. */
4927 real_trunc (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4928 const REAL_VALUE_TYPE
*x
)
4930 do_fix_trunc (r
, x
);
4931 if (mode
!= VOIDmode
)
4932 real_convert (r
, mode
, r
);
4935 /* Round X to the largest integer not greater in value, i.e. round
4936 down, placing the result in R in mode MODE. */
4939 real_floor (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4940 const REAL_VALUE_TYPE
*x
)
4944 do_fix_trunc (&t
, x
);
4945 if (! real_identical (&t
, x
) && x
->sign
)
4946 do_add (&t
, &t
, &dconstm1
, 0);
4947 if (mode
!= VOIDmode
)
4948 real_convert (r
, mode
, &t
);
4953 /* Round X to the smallest integer not less then argument, i.e. round
4954 up, placing the result in R in mode MODE. */
4957 real_ceil (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4958 const REAL_VALUE_TYPE
*x
)
4962 do_fix_trunc (&t
, x
);
4963 if (! real_identical (&t
, x
) && ! x
->sign
)
4964 do_add (&t
, &t
, &dconst1
, 0);
4965 if (mode
!= VOIDmode
)
4966 real_convert (r
, mode
, &t
);
4971 /* Round X to the nearest integer, but round halfway cases away from
4975 real_round (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4976 const REAL_VALUE_TYPE
*x
)
4978 do_add (r
, x
, &dconsthalf
, x
->sign
);
4979 do_fix_trunc (r
, r
);
4980 if (mode
!= VOIDmode
)
4981 real_convert (r
, mode
, r
);
4984 /* Set the sign of R to the sign of X. */
4987 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4992 /* Check whether the real constant value given is an integer. */
4995 real_isinteger (const REAL_VALUE_TYPE
*c
, enum machine_mode mode
)
4997 REAL_VALUE_TYPE cint
;
4999 real_trunc (&cint
, mode
, c
);
5000 return real_identical (c
, &cint
);
5003 /* Write into BUF the maximum representable finite floating-point
5004 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5005 float string. LEN is the size of BUF, and the buffer must be large
5006 enough to contain the resulting string. */
5009 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
5014 strcpy (buf
, "0x0.");
5016 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
5019 *p
++ = "08ce"[n
- i
];
5020 sprintf (p
, "p%d", fmt
->emax
);
5021 if (fmt
->pnan
< fmt
->p
)
5023 /* This is an IBM extended double format made up of two IEEE
5024 doubles. The value of the long double is the sum of the
5025 values of the two parts. The most significant part is
5026 required to be the value of the long double rounded to the
5027 nearest double. Rounding means we need a slightly smaller
5028 value for LDBL_MAX. */
5029 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
5032 gcc_assert (strlen (buf
) < len
);