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, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Stephen L. Moshier (moshier@world.std.com).
6 Re-written by Richard Henderson <rth@redhat.com>
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
26 #include "coretypes.h"
29 #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 /* Clear any padding areas in *r if it isn't equal to one of the
1014 operands so that we can later do bitwise comparisons later on. */
1015 if (r
!= op0
&& r
!= op1
)
1016 memset (r
, '\0', sizeof (*r
));
1017 return do_add (r
, op0
, op1
, 0);
1020 if (r
!= op0
&& r
!= op1
)
1021 memset (r
, '\0', sizeof (*r
));
1022 return do_add (r
, op0
, op1
, 1);
1025 if (r
!= op0
&& r
!= op1
)
1026 memset (r
, '\0', sizeof (*r
));
1027 return do_multiply (r
, op0
, op1
);
1030 if (r
!= op0
&& r
!= op1
)
1031 memset (r
, '\0', sizeof (*r
));
1032 return do_divide (r
, op0
, op1
);
1035 if (op1
->cl
== rvc_nan
)
1037 else if (do_compare (op0
, op1
, -1) < 0)
1044 if (op1
->cl
== rvc_nan
)
1046 else if (do_compare (op0
, op1
, 1) < 0)
1062 case FIX_TRUNC_EXPR
:
1063 do_fix_trunc (r
, op0
);
1073 real_value_negate (const REAL_VALUE_TYPE
*op0
)
1076 real_arithmetic (&r
, NEGATE_EXPR
, op0
, NULL
);
1081 real_value_abs (const REAL_VALUE_TYPE
*op0
)
1084 real_arithmetic (&r
, ABS_EXPR
, op0
, NULL
);
1089 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1090 const REAL_VALUE_TYPE
*op1
)
1092 enum tree_code code
= (enum tree_code
) icode
;
1097 return do_compare (op0
, op1
, 1) < 0;
1099 return do_compare (op0
, op1
, 1) <= 0;
1101 return do_compare (op0
, op1
, -1) > 0;
1103 return do_compare (op0
, op1
, -1) >= 0;
1105 return do_compare (op0
, op1
, -1) == 0;
1107 return do_compare (op0
, op1
, -1) != 0;
1108 case UNORDERED_EXPR
:
1109 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1111 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1113 return do_compare (op0
, op1
, -1) < 0;
1115 return do_compare (op0
, op1
, -1) <= 0;
1117 return do_compare (op0
, op1
, 1) > 0;
1119 return do_compare (op0
, op1
, 1) >= 0;
1121 return do_compare (op0
, op1
, 0) == 0;
1123 return do_compare (op0
, op1
, 0) != 0;
1130 /* Return floor log2(R). */
1133 real_exponent (const REAL_VALUE_TYPE
*r
)
1141 return (unsigned int)-1 >> 1;
1143 return REAL_EXP (r
);
1149 /* R = OP0 * 2**EXP. */
1152 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1163 exp
+= REAL_EXP (op0
);
1165 get_inf (r
, r
->sign
);
1166 else if (exp
< -MAX_EXP
)
1167 get_zero (r
, r
->sign
);
1169 SET_REAL_EXP (r
, exp
);
1177 /* Determine whether a floating-point value X is infinite. */
1180 real_isinf (const REAL_VALUE_TYPE
*r
)
1182 return (r
->cl
== rvc_inf
);
1185 /* Determine whether a floating-point value X is a NaN. */
1188 real_isnan (const REAL_VALUE_TYPE
*r
)
1190 return (r
->cl
== rvc_nan
);
1193 /* Determine whether a floating-point value X is finite. */
1196 real_isfinite (const REAL_VALUE_TYPE
*r
)
1198 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1201 /* Determine whether a floating-point value X is negative. */
1204 real_isneg (const REAL_VALUE_TYPE
*r
)
1209 /* Determine whether a floating-point value X is minus zero. */
1212 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1214 return r
->sign
&& r
->cl
== rvc_zero
;
1217 /* Compare two floating-point objects for bitwise identity. */
1220 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1226 if (a
->sign
!= b
->sign
)
1236 if (a
->decimal
!= b
->decimal
)
1238 if (REAL_EXP (a
) != REAL_EXP (b
))
1243 if (a
->signalling
!= b
->signalling
)
1245 /* The significand is ignored for canonical NaNs. */
1246 if (a
->canonical
|| b
->canonical
)
1247 return a
->canonical
== b
->canonical
;
1254 for (i
= 0; i
< SIGSZ
; ++i
)
1255 if (a
->sig
[i
] != b
->sig
[i
])
1261 /* Try to change R into its exact multiplicative inverse in machine
1262 mode MODE. Return true if successful. */
1265 exact_real_inverse (enum machine_mode mode
, REAL_VALUE_TYPE
*r
)
1267 const REAL_VALUE_TYPE
*one
= real_digit (1);
1271 if (r
->cl
!= rvc_normal
)
1274 /* Check for a power of two: all significand bits zero except the MSB. */
1275 for (i
= 0; i
< SIGSZ
-1; ++i
)
1278 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1281 /* Find the inverse and truncate to the required mode. */
1282 do_divide (&u
, one
, r
);
1283 real_convert (&u
, mode
, &u
);
1285 /* The rounding may have overflowed. */
1286 if (u
.cl
!= rvc_normal
)
1288 for (i
= 0; i
< SIGSZ
-1; ++i
)
1291 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1298 /* Return true if arithmetic on values in IMODE that were promoted
1299 from values in TMODE is equivalent to direct arithmetic on values
1303 real_can_shorten_arithmetic (enum machine_mode imode
, enum machine_mode tmode
)
1305 const struct real_format
*tfmt
, *ifmt
;
1306 tfmt
= REAL_MODE_FORMAT (tmode
);
1307 ifmt
= REAL_MODE_FORMAT (imode
);
1308 /* These conditions are conservative rather than trying to catch the
1309 exact boundary conditions; the main case to allow is IEEE float
1311 return (ifmt
->b
== tfmt
->b
1312 && ifmt
->p
> 2 * tfmt
->p
1313 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1314 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1315 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1316 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1317 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1318 && (ifmt
->has_sign_dependent_rounding
1319 == tfmt
->has_sign_dependent_rounding
)
1320 && ifmt
->has_nans
>= tfmt
->has_nans
1321 && ifmt
->has_inf
>= tfmt
->has_inf
1322 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1323 && !MODE_COMPOSITE_P (tmode
)
1324 && !MODE_COMPOSITE_P (imode
));
1327 /* Render R as an integer. */
1330 real_to_integer (const REAL_VALUE_TYPE
*r
)
1332 unsigned HOST_WIDE_INT i
;
1343 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1350 return decimal_real_to_integer (r
);
1352 if (REAL_EXP (r
) <= 0)
1354 /* Only force overflow for unsigned overflow. Signed overflow is
1355 undefined, so it doesn't matter what we return, and some callers
1356 expect to be able to use this routine for both signed and
1357 unsigned conversions. */
1358 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1361 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1362 i
= r
->sig
[SIGSZ
-1];
1365 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1366 i
= r
->sig
[SIGSZ
-1];
1367 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1368 i
|= r
->sig
[SIGSZ
-2];
1371 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1382 /* Likewise, but to an integer pair, HI+LOW. */
1385 real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
1386 const REAL_VALUE_TYPE
*r
)
1389 HOST_WIDE_INT low
, high
;
1402 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1415 decimal_real_to_integer2 (plow
, phigh
, r
);
1422 /* Only force overflow for unsigned overflow. Signed overflow is
1423 undefined, so it doesn't matter what we return, and some callers
1424 expect to be able to use this routine for both signed and
1425 unsigned conversions. */
1426 if (exp
> HOST_BITS_PER_DOUBLE_INT
)
1429 rshift_significand (&t
, r
, HOST_BITS_PER_DOUBLE_INT
- exp
);
1430 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1432 high
= t
.sig
[SIGSZ
-1];
1433 low
= t
.sig
[SIGSZ
-2];
1437 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
);
1438 high
= t
.sig
[SIGSZ
-1];
1439 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1440 high
|= t
.sig
[SIGSZ
-2];
1442 low
= t
.sig
[SIGSZ
-3];
1443 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1444 low
|= t
.sig
[SIGSZ
-4];
1452 low
= -low
, high
= ~high
;
1464 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1465 of NUM / DEN. Return the quotient and place the remainder in NUM.
1466 It is expected that NUM / DEN are close enough that the quotient is
1469 static unsigned long
1470 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1472 unsigned long q
, msb
;
1473 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1482 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1484 lshift_significand_1 (num
, num
);
1486 if (msb
|| cmp_significands (num
, den
) >= 0)
1488 sub_significands (num
, num
, den
, 0);
1492 while (--expn
>= expd
);
1494 SET_REAL_EXP (num
, expd
);
1500 /* Render R as a decimal floating point constant. Emit DIGITS significant
1501 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1502 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1503 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1504 to a string that, when parsed back in mode MODE, yields the same value. */
1506 #define M_LOG10_2 0.30102999566398119521
1509 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1510 size_t buf_size
, size_t digits
,
1511 int crop_trailing_zeros
, enum machine_mode mode
)
1513 const struct real_format
*fmt
= NULL
;
1514 const REAL_VALUE_TYPE
*one
, *ten
;
1515 REAL_VALUE_TYPE r
, pten
, u
, v
;
1516 int dec_exp
, cmp_one
, digit
;
1518 char *p
, *first
, *last
;
1522 if (mode
!= VOIDmode
)
1524 fmt
= REAL_MODE_FORMAT (mode
);
1532 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1537 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1540 /* ??? Print the significand as well, if not canonical? */
1541 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1542 (r_orig
->signalling
? 'S' : 'Q'));
1550 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1554 /* Bound the number of digits printed by the size of the representation. */
1555 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1556 if (digits
== 0 || digits
> max_digits
)
1557 digits
= max_digits
;
1559 /* Estimate the decimal exponent, and compute the length of the string it
1560 will print as. Be conservative and add one to account for possible
1561 overflow or rounding error. */
1562 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1563 for (max_digits
= 1; dec_exp
; max_digits
++)
1566 /* Bound the number of digits printed by the size of the output buffer. */
1567 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1568 gcc_assert (max_digits
<= buf_size
);
1569 if (digits
> max_digits
)
1570 digits
= max_digits
;
1572 one
= real_digit (1);
1573 ten
= ten_to_ptwo (0);
1581 cmp_one
= do_compare (&r
, one
, 0);
1586 /* Number is greater than one. Convert significand to an integer
1587 and strip trailing decimal zeros. */
1590 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1592 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1593 m
= floor_log2 (max_digits
);
1595 /* Iterate over the bits of the possible powers of 10 that might
1596 be present in U and eliminate them. That is, if we find that
1597 10**2**M divides U evenly, keep the division and increase
1603 do_divide (&t
, &u
, ten_to_ptwo (m
));
1604 do_fix_trunc (&v
, &t
);
1605 if (cmp_significands (&v
, &t
) == 0)
1613 /* Revert the scaling to integer that we performed earlier. */
1614 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1615 - (SIGNIFICAND_BITS
- 1));
1618 /* Find power of 10. Do this by dividing out 10**2**M when
1619 this is larger than the current remainder. Fill PTEN with
1620 the power of 10 that we compute. */
1621 if (REAL_EXP (&r
) > 0)
1623 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1626 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1627 if (do_compare (&u
, ptentwo
, 0) >= 0)
1629 do_divide (&u
, &u
, ptentwo
);
1630 do_multiply (&pten
, &pten
, ptentwo
);
1637 /* We managed to divide off enough tens in the above reduction
1638 loop that we've now got a negative exponent. Fall into the
1639 less-than-one code to compute the proper value for PTEN. */
1646 /* Number is less than one. Pad significand with leading
1652 /* Stop if we'd shift bits off the bottom. */
1656 do_multiply (&u
, &v
, ten
);
1658 /* Stop if we're now >= 1. */
1659 if (REAL_EXP (&u
) > 0)
1667 /* Find power of 10. Do this by multiplying in P=10**2**M when
1668 the current remainder is smaller than 1/P. Fill PTEN with the
1669 power of 10 that we compute. */
1670 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1673 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1674 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1676 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1678 do_multiply (&v
, &v
, ptentwo
);
1679 do_multiply (&pten
, &pten
, ptentwo
);
1685 /* Invert the positive power of 10 that we've collected so far. */
1686 do_divide (&pten
, one
, &pten
);
1694 /* At this point, PTEN should contain the nearest power of 10 smaller
1695 than R, such that this division produces the first digit.
1697 Using a divide-step primitive that returns the complete integral
1698 remainder avoids the rounding error that would be produced if
1699 we were to use do_divide here and then simply multiply by 10 for
1700 each subsequent digit. */
1702 digit
= rtd_divmod (&r
, &pten
);
1704 /* Be prepared for error in that division via underflow ... */
1705 if (digit
== 0 && cmp_significand_0 (&r
))
1707 /* Multiply by 10 and try again. */
1708 do_multiply (&r
, &r
, ten
);
1709 digit
= rtd_divmod (&r
, &pten
);
1711 gcc_assert (digit
!= 0);
1714 /* ... or overflow. */
1724 gcc_assert (digit
<= 10);
1728 /* Generate subsequent digits. */
1729 while (--digits
> 0)
1731 do_multiply (&r
, &r
, ten
);
1732 digit
= rtd_divmod (&r
, &pten
);
1737 /* Generate one more digit with which to do rounding. */
1738 do_multiply (&r
, &r
, ten
);
1739 digit
= rtd_divmod (&r
, &pten
);
1741 /* Round the result. */
1742 if (fmt
&& fmt
->round_towards_zero
)
1744 /* If the format uses round towards zero when parsing the string
1745 back in, we need to always round away from zero here. */
1746 if (cmp_significand_0 (&r
))
1748 round_up
= digit
> 0;
1754 /* Round to nearest. If R is nonzero there are additional
1755 nonzero digits to be extracted. */
1756 if (cmp_significand_0 (&r
))
1758 /* Round to even. */
1759 else if ((p
[-1] - '0') & 1)
1763 round_up
= digit
> 5;
1780 /* Carry out of the first digit. This means we had all 9's and
1781 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1789 /* Insert the decimal point. */
1790 first
[0] = first
[1];
1793 /* If requested, drop trailing zeros. Never crop past "1.0". */
1794 if (crop_trailing_zeros
)
1795 while (last
> first
+ 3 && last
[-1] == '0')
1798 /* Append the exponent. */
1799 sprintf (last
, "e%+d", dec_exp
);
1801 #ifdef ENABLE_CHECKING
1802 /* Verify that we can read the original value back in. */
1803 if (mode
!= VOIDmode
)
1805 real_from_string (&r
, str
);
1806 real_convert (&r
, mode
, &r
);
1807 gcc_assert (real_identical (&r
, r_orig
));
1812 /* Likewise, except always uses round-to-nearest. */
1815 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1816 size_t digits
, int crop_trailing_zeros
)
1818 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1819 digits
, crop_trailing_zeros
, VOIDmode
);
1822 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1823 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1824 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1825 strip trailing zeros. */
1828 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1829 size_t digits
, int crop_trailing_zeros
)
1831 int i
, j
, exp
= REAL_EXP (r
);
1844 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1847 /* ??? Print the significand as well, if not canonical? */
1848 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1849 (r
->signalling
? 'S' : 'Q'));
1857 /* Hexadecimal format for decimal floats is not interesting. */
1858 strcpy (str
, "N/A");
1863 digits
= SIGNIFICAND_BITS
/ 4;
1865 /* Bound the number of digits printed by the size of the output buffer. */
1867 sprintf (exp_buf
, "p%+d", exp
);
1868 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1869 gcc_assert (max_digits
<= buf_size
);
1870 if (digits
> max_digits
)
1871 digits
= max_digits
;
1882 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1883 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1885 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1891 if (crop_trailing_zeros
)
1892 while (p
> first
+ 1 && p
[-1] == '0')
1895 sprintf (p
, "p%+d", exp
);
1898 /* Initialize R from a decimal or hexadecimal string. The string is
1899 assumed to have been syntax checked already. Return -1 if the
1900 value underflows, +1 if overflows, and 0 otherwise. */
1903 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1915 else if (*str
== '+')
1918 if (!strncmp (str
, "QNaN", 4))
1920 get_canonical_qnan (r
, sign
);
1923 else if (!strncmp (str
, "SNaN", 4))
1925 get_canonical_snan (r
, sign
);
1928 else if (!strncmp (str
, "Inf", 3))
1934 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1936 /* Hexadecimal floating point. */
1937 int pos
= SIGNIFICAND_BITS
- 4, d
;
1945 d
= hex_value (*str
);
1950 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1951 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1955 /* Ensure correct rounding by setting last bit if there is
1956 a subsequent nonzero digit. */
1964 if (pos
== SIGNIFICAND_BITS
- 4)
1971 d
= hex_value (*str
);
1976 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1977 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1981 /* Ensure correct rounding by setting last bit if there is
1982 a subsequent nonzero digit. */
1988 /* If the mantissa is zero, ignore the exponent. */
1989 if (!cmp_significand_0 (r
))
1992 if (*str
== 'p' || *str
== 'P')
1994 bool exp_neg
= false;
2002 else if (*str
== '+')
2006 while (ISDIGIT (*str
))
2012 /* Overflowed the exponent. */
2027 SET_REAL_EXP (r
, exp
);
2033 /* Decimal floating point. */
2034 const REAL_VALUE_TYPE
*ten
= ten_to_ptwo (0);
2039 while (ISDIGIT (*str
))
2042 do_multiply (r
, r
, ten
);
2044 do_add (r
, r
, real_digit (d
), 0);
2049 if (r
->cl
== rvc_zero
)
2054 while (ISDIGIT (*str
))
2057 do_multiply (r
, r
, ten
);
2059 do_add (r
, r
, real_digit (d
), 0);
2064 /* If the mantissa is zero, ignore the exponent. */
2065 if (r
->cl
== rvc_zero
)
2068 if (*str
== 'e' || *str
== 'E')
2070 bool exp_neg
= false;
2078 else if (*str
== '+')
2082 while (ISDIGIT (*str
))
2088 /* Overflowed the exponent. */
2102 times_pten (r
, exp
);
2121 /* Legacy. Similar, but return the result directly. */
2124 real_from_string2 (const char *s
, enum machine_mode mode
)
2128 real_from_string (&r
, s
);
2129 if (mode
!= VOIDmode
)
2130 real_convert (&r
, mode
, &r
);
2135 /* Initialize R from string S and desired MODE. */
2138 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, enum machine_mode mode
)
2140 if (DECIMAL_FLOAT_MODE_P (mode
))
2141 decimal_real_from_string (r
, s
);
2143 real_from_string (r
, s
);
2145 if (mode
!= VOIDmode
)
2146 real_convert (r
, mode
, r
);
2149 /* Initialize R from the integer pair HIGH+LOW. */
2152 real_from_integer (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2153 unsigned HOST_WIDE_INT low
, HOST_WIDE_INT high
,
2156 if (low
== 0 && high
== 0)
2160 memset (r
, 0, sizeof (*r
));
2162 r
->sign
= high
< 0 && !unsigned_p
;
2163 SET_REAL_EXP (r
, HOST_BITS_PER_DOUBLE_INT
);
2174 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2176 r
->sig
[SIGSZ
-1] = high
;
2177 r
->sig
[SIGSZ
-2] = low
;
2181 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2182 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2183 r
->sig
[SIGSZ
-2] = high
;
2184 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2185 r
->sig
[SIGSZ
-4] = low
;
2191 if (DECIMAL_FLOAT_MODE_P (mode
))
2192 decimal_from_integer (r
);
2193 else if (mode
!= VOIDmode
)
2194 real_convert (r
, mode
, r
);
2197 /* Render R, an integral value, as a floating point constant with no
2198 specified exponent. */
2201 decimal_integer_string (char *str
, const REAL_VALUE_TYPE
*r_orig
,
2204 int dec_exp
, digit
, digits
;
2205 REAL_VALUE_TYPE r
, pten
;
2211 if (r
.cl
== rvc_zero
)
2220 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
2221 digits
= dec_exp
+ 1;
2222 gcc_assert ((digits
+ 2) < (int)buf_size
);
2224 pten
= *real_digit (1);
2225 times_pten (&pten
, dec_exp
);
2231 digit
= rtd_divmod (&r
, &pten
);
2232 gcc_assert (digit
>= 0 && digit
<= 9);
2234 while (--digits
> 0)
2237 digit
= rtd_divmod (&r
, &pten
);
2244 /* Convert a real with an integral value to decimal float. */
2247 decimal_from_integer (REAL_VALUE_TYPE
*r
)
2251 decimal_integer_string (str
, r
, sizeof (str
) - 1);
2252 decimal_real_from_string (r
, str
);
2255 /* Returns 10**2**N. */
2257 static const REAL_VALUE_TYPE
*
2260 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2262 gcc_assert (n
>= 0);
2263 gcc_assert (n
< EXP_BITS
);
2265 if (tens
[n
].cl
== rvc_zero
)
2267 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2269 HOST_WIDE_INT t
= 10;
2272 for (i
= 0; i
< n
; ++i
)
2275 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2279 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2280 do_multiply (&tens
[n
], t
, t
);
2287 /* Returns 10**(-2**N). */
2289 static const REAL_VALUE_TYPE
*
2290 ten_to_mptwo (int n
)
2292 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2294 gcc_assert (n
>= 0);
2295 gcc_assert (n
< EXP_BITS
);
2297 if (tens
[n
].cl
== rvc_zero
)
2298 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2305 static const REAL_VALUE_TYPE
*
2308 static REAL_VALUE_TYPE num
[10];
2310 gcc_assert (n
>= 0);
2311 gcc_assert (n
<= 9);
2313 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2314 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2319 /* Multiply R by 10**EXP. */
2322 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2324 REAL_VALUE_TYPE pten
, *rr
;
2325 bool negative
= (exp
< 0);
2331 pten
= *real_digit (1);
2337 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2339 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2342 do_divide (r
, r
, &pten
);
2345 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2347 const REAL_VALUE_TYPE
*
2350 static REAL_VALUE_TYPE value
;
2352 /* Initialize mathematical constants for constant folding builtins.
2353 These constants need to be given to at least 160 bits precision. */
2354 if (value
.cl
== rvc_zero
)
2357 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2358 mpfr_set_ui (m
, 1, GMP_RNDN
);
2359 mpfr_exp (m
, m
, GMP_RNDN
);
2360 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2367 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2369 const REAL_VALUE_TYPE
*
2370 dconst_third_ptr (void)
2372 static REAL_VALUE_TYPE value
;
2374 /* Initialize mathematical constants for constant folding builtins.
2375 These constants need to be given to at least 160 bits precision. */
2376 if (value
.cl
== rvc_zero
)
2378 real_arithmetic (&value
, RDIV_EXPR
, &dconst1
, real_digit (3));
2383 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2385 const REAL_VALUE_TYPE
*
2386 dconst_sqrt2_ptr (void)
2388 static REAL_VALUE_TYPE value
;
2390 /* Initialize mathematical constants for constant folding builtins.
2391 These constants need to be given to at least 160 bits precision. */
2392 if (value
.cl
== rvc_zero
)
2395 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2396 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2397 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2403 /* Fills R with +Inf. */
2406 real_inf (REAL_VALUE_TYPE
*r
)
2411 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2412 we force a QNaN, else we force an SNaN. The string, if not empty,
2413 is parsed as a number and placed in the significand. Return true
2414 if the string was successfully parsed. */
2417 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2418 enum machine_mode mode
)
2420 const struct real_format
*fmt
;
2422 fmt
= REAL_MODE_FORMAT (mode
);
2428 get_canonical_qnan (r
, 0);
2430 get_canonical_snan (r
, 0);
2436 memset (r
, 0, sizeof (*r
));
2439 /* Parse akin to strtol into the significand of R. */
2441 while (ISSPACE (*str
))
2445 else if (*str
== '+')
2450 if (*str
== 'x' || *str
== 'X')
2459 while ((d
= hex_value (*str
)) < base
)
2466 lshift_significand (r
, r
, 3);
2469 lshift_significand (r
, r
, 4);
2472 lshift_significand_1 (&u
, r
);
2473 lshift_significand (r
, r
, 3);
2474 add_significands (r
, r
, &u
);
2482 add_significands (r
, r
, &u
);
2487 /* Must have consumed the entire string for success. */
2491 /* Shift the significand into place such that the bits
2492 are in the most significant bits for the format. */
2493 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2495 /* Our MSB is always unset for NaNs. */
2496 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2498 /* Force quiet or signalling NaN. */
2499 r
->signalling
= !quiet
;
2505 /* Fills R with the largest finite value representable in mode MODE.
2506 If SIGN is nonzero, R is set to the most negative finite value. */
2509 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
2511 const struct real_format
*fmt
;
2514 fmt
= REAL_MODE_FORMAT (mode
);
2516 memset (r
, 0, sizeof (*r
));
2519 decimal_real_maxval (r
, sign
, mode
);
2524 SET_REAL_EXP (r
, fmt
->emax
);
2526 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2527 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2528 clear_significand_below (r
, np2
);
2530 if (fmt
->pnan
< fmt
->p
)
2531 /* This is an IBM extended double format made up of two IEEE
2532 doubles. The value of the long double is the sum of the
2533 values of the two parts. The most significant part is
2534 required to be the value of the long double rounded to the
2535 nearest double. Rounding means we need a slightly smaller
2536 value for LDBL_MAX. */
2537 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2541 /* Fills R with 2**N. */
2544 real_2expN (REAL_VALUE_TYPE
*r
, int n
, enum machine_mode fmode
)
2546 memset (r
, 0, sizeof (*r
));
2551 else if (n
< -MAX_EXP
)
2556 SET_REAL_EXP (r
, n
);
2557 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2559 if (DECIMAL_FLOAT_MODE_P (fmode
))
2560 decimal_real_convert (r
, fmode
, r
);
2565 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2569 bool round_up
= false;
2575 decimal_round_for_format (fmt
, r
);
2578 /* FIXME. We can come here via fp_easy_constant
2579 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2580 investigated whether this convert needs to be here, or
2581 something else is missing. */
2582 decimal_real_convert (r
, DFmode
, r
);
2586 emin2m1
= fmt
->emin
- 1;
2589 np2
= SIGNIFICAND_BITS
- p2
;
2593 get_zero (r
, r
->sign
);
2595 if (!fmt
->has_signed_zero
)
2600 get_inf (r
, r
->sign
);
2605 clear_significand_below (r
, np2
);
2615 /* Check the range of the exponent. If we're out of range,
2616 either underflow or overflow. */
2617 if (REAL_EXP (r
) > emax2
)
2619 else if (REAL_EXP (r
) <= emin2m1
)
2623 if (!fmt
->has_denorm
)
2625 /* Don't underflow completely until we've had a chance to round. */
2626 if (REAL_EXP (r
) < emin2m1
)
2631 diff
= emin2m1
- REAL_EXP (r
) + 1;
2635 /* De-normalize the significand. */
2636 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2637 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2641 if (!fmt
->round_towards_zero
)
2643 /* There are P2 true significand bits, followed by one guard bit,
2644 followed by one sticky bit, followed by stuff. Fold nonzero
2645 stuff into the sticky bit. */
2646 unsigned long sticky
;
2650 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2651 sticky
|= r
->sig
[i
];
2653 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2655 guard
= test_significand_bit (r
, np2
- 1);
2656 lsb
= test_significand_bit (r
, np2
);
2658 /* Round to even. */
2659 round_up
= guard
&& (sticky
|| lsb
);
2666 set_significand_bit (&u
, np2
);
2668 if (add_significands (r
, r
, &u
))
2670 /* Overflow. Means the significand had been all ones, and
2671 is now all zeros. Need to increase the exponent, and
2672 possibly re-normalize it. */
2673 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2674 if (REAL_EXP (r
) > emax2
)
2676 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2680 /* Catch underflow that we deferred until after rounding. */
2681 if (REAL_EXP (r
) <= emin2m1
)
2684 /* Clear out trailing garbage. */
2685 clear_significand_below (r
, np2
);
2688 /* Extend or truncate to a new mode. */
2691 real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2692 const REAL_VALUE_TYPE
*a
)
2694 const struct real_format
*fmt
;
2696 fmt
= REAL_MODE_FORMAT (mode
);
2701 if (a
->decimal
|| fmt
->b
== 10)
2702 decimal_real_convert (r
, mode
, a
);
2704 round_for_format (fmt
, r
);
2706 /* round_for_format de-normalizes denormals. Undo just that part. */
2707 if (r
->cl
== rvc_normal
)
2711 /* Legacy. Likewise, except return the struct directly. */
2714 real_value_truncate (enum machine_mode mode
, REAL_VALUE_TYPE a
)
2717 real_convert (&r
, mode
, &a
);
2721 /* Return true if truncating to MODE is exact. */
2724 exact_real_truncate (enum machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2726 const struct real_format
*fmt
;
2730 fmt
= REAL_MODE_FORMAT (mode
);
2733 /* Don't allow conversion to denormals. */
2734 emin2m1
= fmt
->emin
- 1;
2735 if (REAL_EXP (a
) <= emin2m1
)
2738 /* After conversion to the new mode, the value must be identical. */
2739 real_convert (&t
, mode
, a
);
2740 return real_identical (&t
, a
);
2743 /* Write R to the given target format. Place the words of the result
2744 in target word order in BUF. There are always 32 bits in each
2745 long, no matter the size of the host long.
2747 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2750 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2751 const struct real_format
*fmt
)
2757 round_for_format (fmt
, &r
);
2761 (*fmt
->encode
) (fmt
, buf
, &r
);
2766 /* Similar, but look up the format from MODE. */
2769 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, enum machine_mode mode
)
2771 const struct real_format
*fmt
;
2773 fmt
= REAL_MODE_FORMAT (mode
);
2776 return real_to_target_fmt (buf
, r
, fmt
);
2779 /* Read R from the given target format. Read the words of the result
2780 in target word order in BUF. There are always 32 bits in each
2781 long, no matter the size of the host long. */
2784 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2785 const struct real_format
*fmt
)
2787 (*fmt
->decode
) (fmt
, r
, buf
);
2790 /* Similar, but look up the format from MODE. */
2793 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, enum machine_mode mode
)
2795 const struct real_format
*fmt
;
2797 fmt
= REAL_MODE_FORMAT (mode
);
2800 (*fmt
->decode
) (fmt
, r
, buf
);
2803 /* Return the number of bits of the largest binary value that the
2804 significand of MODE will hold. */
2805 /* ??? Legacy. Should get access to real_format directly. */
2808 significand_size (enum machine_mode mode
)
2810 const struct real_format
*fmt
;
2812 fmt
= REAL_MODE_FORMAT (mode
);
2818 /* Return the size in bits of the largest binary value that can be
2819 held by the decimal coefficient for this mode. This is one more
2820 than the number of bits required to hold the largest coefficient
2822 double log2_10
= 3.3219281;
2823 return fmt
->p
* log2_10
;
2828 /* Return a hash value for the given real value. */
2829 /* ??? The "unsigned int" return value is intended to be hashval_t,
2830 but I didn't want to pull hashtab.h into real.h. */
2833 real_hash (const REAL_VALUE_TYPE
*r
)
2838 h
= r
->cl
| (r
->sign
<< 2);
2846 h
|= REAL_EXP (r
) << 3;
2851 h
^= (unsigned int)-1;
2860 if (sizeof(unsigned long) > sizeof(unsigned int))
2861 for (i
= 0; i
< SIGSZ
; ++i
)
2863 unsigned long s
= r
->sig
[i
];
2864 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2867 for (i
= 0; i
< SIGSZ
; ++i
)
2873 /* IEEE single-precision format. */
2875 static void encode_ieee_single (const struct real_format
*fmt
,
2876 long *, const REAL_VALUE_TYPE
*);
2877 static void decode_ieee_single (const struct real_format
*,
2878 REAL_VALUE_TYPE
*, const long *);
2881 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2882 const REAL_VALUE_TYPE
*r
)
2884 unsigned long image
, sig
, exp
;
2885 unsigned long sign
= r
->sign
;
2886 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2889 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2900 image
|= 0x7fffffff;
2907 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2908 if (r
->signalling
== fmt
->qnan_msb_set
)
2919 image
|= 0x7fffffff;
2923 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2924 whereas the intermediate representation is 0.F x 2**exp.
2925 Which means we're off by one. */
2929 exp
= REAL_EXP (r
) + 127 - 1;
2942 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2945 unsigned long image
= buf
[0] & 0xffffffff;
2946 bool sign
= (image
>> 31) & 1;
2947 int exp
= (image
>> 23) & 0xff;
2949 memset (r
, 0, sizeof (*r
));
2950 image
<<= HOST_BITS_PER_LONG
- 24;
2955 if (image
&& fmt
->has_denorm
)
2959 SET_REAL_EXP (r
, -126);
2960 r
->sig
[SIGSZ
-1] = image
<< 1;
2963 else if (fmt
->has_signed_zero
)
2966 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2972 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2973 ^ fmt
->qnan_msb_set
);
2974 r
->sig
[SIGSZ
-1] = image
;
2986 SET_REAL_EXP (r
, exp
- 127 + 1);
2987 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2991 const struct real_format ieee_single_format
=
3012 const struct real_format mips_single_format
=
3033 const struct real_format motorola_single_format
=
3054 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3055 single precision with the following differences:
3056 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3058 - NaNs are not supported.
3059 - The range of non-zero numbers in binary is
3060 (001)[1.]000...000 to (255)[1.]111...111.
3061 - Denormals can be represented, but are treated as +0.0 when
3062 used as an operand and are never generated as a result.
3063 - -0.0 can be represented, but a zero result is always +0.0.
3064 - the only supported rounding mode is trunction (towards zero). */
3065 const struct real_format spu_single_format
=
3086 /* IEEE double-precision format. */
3088 static void encode_ieee_double (const struct real_format
*fmt
,
3089 long *, const REAL_VALUE_TYPE
*);
3090 static void decode_ieee_double (const struct real_format
*,
3091 REAL_VALUE_TYPE
*, const long *);
3094 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3095 const REAL_VALUE_TYPE
*r
)
3097 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3098 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3100 image_hi
= r
->sign
<< 31;
3103 if (HOST_BITS_PER_LONG
== 64)
3105 sig_hi
= r
->sig
[SIGSZ
-1];
3106 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3107 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3111 sig_hi
= r
->sig
[SIGSZ
-1];
3112 sig_lo
= r
->sig
[SIGSZ
-2];
3113 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3114 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3124 image_hi
|= 2047 << 20;
3127 image_hi
|= 0x7fffffff;
3128 image_lo
= 0xffffffff;
3137 if (fmt
->canonical_nan_lsbs_set
)
3139 sig_hi
= (1 << 19) - 1;
3140 sig_lo
= 0xffffffff;
3148 if (r
->signalling
== fmt
->qnan_msb_set
)
3149 sig_hi
&= ~(1 << 19);
3152 if (sig_hi
== 0 && sig_lo
== 0)
3155 image_hi
|= 2047 << 20;
3161 image_hi
|= 0x7fffffff;
3162 image_lo
= 0xffffffff;
3167 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3168 whereas the intermediate representation is 0.F x 2**exp.
3169 Which means we're off by one. */
3173 exp
= REAL_EXP (r
) + 1023 - 1;
3174 image_hi
|= exp
<< 20;
3183 if (FLOAT_WORDS_BIG_ENDIAN
)
3184 buf
[0] = image_hi
, buf
[1] = image_lo
;
3186 buf
[0] = image_lo
, buf
[1] = image_hi
;
3190 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3193 unsigned long image_hi
, image_lo
;
3197 if (FLOAT_WORDS_BIG_ENDIAN
)
3198 image_hi
= buf
[0], image_lo
= buf
[1];
3200 image_lo
= buf
[0], image_hi
= buf
[1];
3201 image_lo
&= 0xffffffff;
3202 image_hi
&= 0xffffffff;
3204 sign
= (image_hi
>> 31) & 1;
3205 exp
= (image_hi
>> 20) & 0x7ff;
3207 memset (r
, 0, sizeof (*r
));
3209 image_hi
<<= 32 - 21;
3210 image_hi
|= image_lo
>> 21;
3211 image_hi
&= 0x7fffffff;
3212 image_lo
<<= 32 - 21;
3216 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3220 SET_REAL_EXP (r
, -1022);
3221 if (HOST_BITS_PER_LONG
== 32)
3223 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3225 r
->sig
[SIGSZ
-1] = image_hi
;
3226 r
->sig
[SIGSZ
-2] = image_lo
;
3230 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3231 r
->sig
[SIGSZ
-1] = image_hi
;
3235 else if (fmt
->has_signed_zero
)
3238 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3240 if (image_hi
|| image_lo
)
3244 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3245 if (HOST_BITS_PER_LONG
== 32)
3247 r
->sig
[SIGSZ
-1] = image_hi
;
3248 r
->sig
[SIGSZ
-2] = image_lo
;
3251 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3263 SET_REAL_EXP (r
, exp
- 1023 + 1);
3264 if (HOST_BITS_PER_LONG
== 32)
3266 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3267 r
->sig
[SIGSZ
-2] = image_lo
;
3270 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3274 const struct real_format ieee_double_format
=
3295 const struct real_format mips_double_format
=
3316 const struct real_format motorola_double_format
=
3337 /* IEEE extended real format. This comes in three flavors: Intel's as
3338 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3339 12- and 16-byte images may be big- or little endian; Motorola's is
3340 always big endian. */
3342 /* Helper subroutine which converts from the internal format to the
3343 12-byte little-endian Intel format. Functions below adjust this
3344 for the other possible formats. */
3346 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3347 const REAL_VALUE_TYPE
*r
)
3349 unsigned long image_hi
, sig_hi
, sig_lo
;
3350 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3352 image_hi
= r
->sign
<< 15;
3353 sig_hi
= sig_lo
= 0;
3365 /* Intel requires the explicit integer bit to be set, otherwise
3366 it considers the value a "pseudo-infinity". Motorola docs
3367 say it doesn't care. */
3368 sig_hi
= 0x80000000;
3373 sig_lo
= sig_hi
= 0xffffffff;
3383 if (fmt
->canonical_nan_lsbs_set
)
3385 sig_hi
= (1 << 30) - 1;
3386 sig_lo
= 0xffffffff;
3389 else if (HOST_BITS_PER_LONG
== 32)
3391 sig_hi
= r
->sig
[SIGSZ
-1];
3392 sig_lo
= r
->sig
[SIGSZ
-2];
3396 sig_lo
= r
->sig
[SIGSZ
-1];
3397 sig_hi
= sig_lo
>> 31 >> 1;
3398 sig_lo
&= 0xffffffff;
3400 if (r
->signalling
== fmt
->qnan_msb_set
)
3401 sig_hi
&= ~(1 << 30);
3404 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3407 /* Intel requires the explicit integer bit to be set, otherwise
3408 it considers the value a "pseudo-nan". Motorola docs say it
3410 sig_hi
|= 0x80000000;
3415 sig_lo
= sig_hi
= 0xffffffff;
3421 int exp
= REAL_EXP (r
);
3423 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3424 whereas the intermediate representation is 0.F x 2**exp.
3425 Which means we're off by one.
3427 Except for Motorola, which consider exp=0 and explicit
3428 integer bit set to continue to be normalized. In theory
3429 this discrepancy has been taken care of by the difference
3430 in fmt->emin in round_for_format. */
3437 gcc_assert (exp
>= 0);
3441 if (HOST_BITS_PER_LONG
== 32)
3443 sig_hi
= r
->sig
[SIGSZ
-1];
3444 sig_lo
= r
->sig
[SIGSZ
-2];
3448 sig_lo
= r
->sig
[SIGSZ
-1];
3449 sig_hi
= sig_lo
>> 31 >> 1;
3450 sig_lo
&= 0xffffffff;
3459 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3462 /* Convert from the internal format to the 12-byte Motorola format
3463 for an IEEE extended real. */
3465 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3466 const REAL_VALUE_TYPE
*r
)
3469 encode_ieee_extended (fmt
, intermed
, r
);
3471 /* Motorola chips are assumed always to be big-endian. Also, the
3472 padding in a Motorola extended real goes between the exponent and
3473 the mantissa. At this point the mantissa is entirely within
3474 elements 0 and 1 of intermed, and the exponent entirely within
3475 element 2, so all we have to do is swap the order around, and
3476 shift element 2 left 16 bits. */
3477 buf
[0] = intermed
[2] << 16;
3478 buf
[1] = intermed
[1];
3479 buf
[2] = intermed
[0];
3482 /* Convert from the internal format to the 12-byte Intel format for
3483 an IEEE extended real. */
3485 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3486 const REAL_VALUE_TYPE
*r
)
3488 if (FLOAT_WORDS_BIG_ENDIAN
)
3490 /* All the padding in an Intel-format extended real goes at the high
3491 end, which in this case is after the mantissa, not the exponent.
3492 Therefore we must shift everything down 16 bits. */
3494 encode_ieee_extended (fmt
, intermed
, r
);
3495 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3496 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3497 buf
[2] = (intermed
[0] << 16);
3500 /* encode_ieee_extended produces what we want directly. */
3501 encode_ieee_extended (fmt
, buf
, r
);
3504 /* Convert from the internal format to the 16-byte Intel format for
3505 an IEEE extended real. */
3507 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3508 const REAL_VALUE_TYPE
*r
)
3510 /* All the padding in an Intel-format extended real goes at the high end. */
3511 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3515 /* As above, we have a helper function which converts from 12-byte
3516 little-endian Intel format to internal format. Functions below
3517 adjust for the other possible formats. */
3519 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3522 unsigned long image_hi
, sig_hi
, sig_lo
;
3526 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3527 sig_lo
&= 0xffffffff;
3528 sig_hi
&= 0xffffffff;
3529 image_hi
&= 0xffffffff;
3531 sign
= (image_hi
>> 15) & 1;
3532 exp
= image_hi
& 0x7fff;
3534 memset (r
, 0, sizeof (*r
));
3538 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3543 /* When the IEEE format contains a hidden bit, we know that
3544 it's zero at this point, and so shift up the significand
3545 and decrease the exponent to match. In this case, Motorola
3546 defines the explicit integer bit to be valid, so we don't
3547 know whether the msb is set or not. */
3548 SET_REAL_EXP (r
, fmt
->emin
);
3549 if (HOST_BITS_PER_LONG
== 32)
3551 r
->sig
[SIGSZ
-1] = sig_hi
;
3552 r
->sig
[SIGSZ
-2] = sig_lo
;
3555 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3559 else if (fmt
->has_signed_zero
)
3562 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3564 /* See above re "pseudo-infinities" and "pseudo-nans".
3565 Short summary is that the MSB will likely always be
3566 set, and that we don't care about it. */
3567 sig_hi
&= 0x7fffffff;
3569 if (sig_hi
|| sig_lo
)
3573 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3574 if (HOST_BITS_PER_LONG
== 32)
3576 r
->sig
[SIGSZ
-1] = sig_hi
;
3577 r
->sig
[SIGSZ
-2] = sig_lo
;
3580 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3592 SET_REAL_EXP (r
, exp
- 16383 + 1);
3593 if (HOST_BITS_PER_LONG
== 32)
3595 r
->sig
[SIGSZ
-1] = sig_hi
;
3596 r
->sig
[SIGSZ
-2] = sig_lo
;
3599 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3603 /* Convert from the internal format to the 12-byte Motorola format
3604 for an IEEE extended real. */
3606 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3611 /* Motorola chips are assumed always to be big-endian. Also, the
3612 padding in a Motorola extended real goes between the exponent and
3613 the mantissa; remove it. */
3614 intermed
[0] = buf
[2];
3615 intermed
[1] = buf
[1];
3616 intermed
[2] = (unsigned long)buf
[0] >> 16;
3618 decode_ieee_extended (fmt
, r
, intermed
);
3621 /* Convert from the internal format to the 12-byte Intel format for
3622 an IEEE extended real. */
3624 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3627 if (FLOAT_WORDS_BIG_ENDIAN
)
3629 /* All the padding in an Intel-format extended real goes at the high
3630 end, which in this case is after the mantissa, not the exponent.
3631 Therefore we must shift everything up 16 bits. */
3634 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3635 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3636 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3638 decode_ieee_extended (fmt
, r
, intermed
);
3641 /* decode_ieee_extended produces what we want directly. */
3642 decode_ieee_extended (fmt
, r
, buf
);
3645 /* Convert from the internal format to the 16-byte Intel format for
3646 an IEEE extended real. */
3648 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3651 /* All the padding in an Intel-format extended real goes at the high end. */
3652 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3655 const struct real_format ieee_extended_motorola_format
=
3657 encode_ieee_extended_motorola
,
3658 decode_ieee_extended_motorola
,
3676 const struct real_format ieee_extended_intel_96_format
=
3678 encode_ieee_extended_intel_96
,
3679 decode_ieee_extended_intel_96
,
3697 const struct real_format ieee_extended_intel_128_format
=
3699 encode_ieee_extended_intel_128
,
3700 decode_ieee_extended_intel_128
,
3718 /* The following caters to i386 systems that set the rounding precision
3719 to 53 bits instead of 64, e.g. FreeBSD. */
3720 const struct real_format ieee_extended_intel_96_round_53_format
=
3722 encode_ieee_extended_intel_96
,
3723 decode_ieee_extended_intel_96
,
3741 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3742 numbers whose sum is equal to the extended precision value. The number
3743 with greater magnitude is first. This format has the same magnitude
3744 range as an IEEE double precision value, but effectively 106 bits of
3745 significand precision. Infinity and NaN are represented by their IEEE
3746 double precision value stored in the first number, the second number is
3747 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3749 static void encode_ibm_extended (const struct real_format
*fmt
,
3750 long *, const REAL_VALUE_TYPE
*);
3751 static void decode_ibm_extended (const struct real_format
*,
3752 REAL_VALUE_TYPE
*, const long *);
3755 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3756 const REAL_VALUE_TYPE
*r
)
3758 REAL_VALUE_TYPE u
, normr
, v
;
3759 const struct real_format
*base_fmt
;
3761 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3763 /* Renormalize R before doing any arithmetic on it. */
3765 if (normr
.cl
== rvc_normal
)
3768 /* u = IEEE double precision portion of significand. */
3770 round_for_format (base_fmt
, &u
);
3771 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3773 if (u
.cl
== rvc_normal
)
3775 do_add (&v
, &normr
, &u
, 1);
3776 /* Call round_for_format since we might need to denormalize. */
3777 round_for_format (base_fmt
, &v
);
3778 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3782 /* Inf, NaN, 0 are all representable as doubles, so the
3783 least-significant part can be 0.0. */
3790 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3793 REAL_VALUE_TYPE u
, v
;
3794 const struct real_format
*base_fmt
;
3796 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3797 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3799 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3801 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3802 do_add (r
, &u
, &v
, 0);
3808 const struct real_format ibm_extended_format
=
3810 encode_ibm_extended
,
3811 decode_ibm_extended
,
3829 const struct real_format mips_extended_format
=
3831 encode_ibm_extended
,
3832 decode_ibm_extended
,
3851 /* IEEE quad precision format. */
3853 static void encode_ieee_quad (const struct real_format
*fmt
,
3854 long *, const REAL_VALUE_TYPE
*);
3855 static void decode_ieee_quad (const struct real_format
*,
3856 REAL_VALUE_TYPE
*, const long *);
3859 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3860 const REAL_VALUE_TYPE
*r
)
3862 unsigned long image3
, image2
, image1
, image0
, exp
;
3863 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3866 image3
= r
->sign
<< 31;
3871 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3880 image3
|= 32767 << 16;
3883 image3
|= 0x7fffffff;
3884 image2
= 0xffffffff;
3885 image1
= 0xffffffff;
3886 image0
= 0xffffffff;
3893 image3
|= 32767 << 16;
3897 if (fmt
->canonical_nan_lsbs_set
)
3900 image2
= image1
= image0
= 0xffffffff;
3903 else if (HOST_BITS_PER_LONG
== 32)
3908 image3
|= u
.sig
[3] & 0xffff;
3913 image1
= image0
>> 31 >> 1;
3915 image3
|= (image2
>> 31 >> 1) & 0xffff;
3916 image0
&= 0xffffffff;
3917 image2
&= 0xffffffff;
3919 if (r
->signalling
== fmt
->qnan_msb_set
)
3923 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3928 image3
|= 0x7fffffff;
3929 image2
= 0xffffffff;
3930 image1
= 0xffffffff;
3931 image0
= 0xffffffff;
3936 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3937 whereas the intermediate representation is 0.F x 2**exp.
3938 Which means we're off by one. */
3942 exp
= REAL_EXP (r
) + 16383 - 1;
3943 image3
|= exp
<< 16;
3945 if (HOST_BITS_PER_LONG
== 32)
3950 image3
|= u
.sig
[3] & 0xffff;
3955 image1
= image0
>> 31 >> 1;
3957 image3
|= (image2
>> 31 >> 1) & 0xffff;
3958 image0
&= 0xffffffff;
3959 image2
&= 0xffffffff;
3967 if (FLOAT_WORDS_BIG_ENDIAN
)
3984 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3987 unsigned long image3
, image2
, image1
, image0
;
3991 if (FLOAT_WORDS_BIG_ENDIAN
)
4005 image0
&= 0xffffffff;
4006 image1
&= 0xffffffff;
4007 image2
&= 0xffffffff;
4009 sign
= (image3
>> 31) & 1;
4010 exp
= (image3
>> 16) & 0x7fff;
4013 memset (r
, 0, sizeof (*r
));
4017 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
4022 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
4023 if (HOST_BITS_PER_LONG
== 32)
4032 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4033 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4038 else if (fmt
->has_signed_zero
)
4041 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
4043 if (image3
| image2
| image1
| image0
)
4047 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
4049 if (HOST_BITS_PER_LONG
== 32)
4058 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4059 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4061 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4073 SET_REAL_EXP (r
, exp
- 16383 + 1);
4075 if (HOST_BITS_PER_LONG
== 32)
4084 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4085 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4087 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4088 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4092 const struct real_format ieee_quad_format
=
4113 const struct real_format mips_quad_format
=
4134 /* Descriptions of VAX floating point formats can be found beginning at
4136 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4138 The thing to remember is that they're almost IEEE, except for word
4139 order, exponent bias, and the lack of infinities, nans, and denormals.
4141 We don't implement the H_floating format here, simply because neither
4142 the VAX or Alpha ports use it. */
4144 static void encode_vax_f (const struct real_format
*fmt
,
4145 long *, const REAL_VALUE_TYPE
*);
4146 static void decode_vax_f (const struct real_format
*,
4147 REAL_VALUE_TYPE
*, const long *);
4148 static void encode_vax_d (const struct real_format
*fmt
,
4149 long *, const REAL_VALUE_TYPE
*);
4150 static void decode_vax_d (const struct real_format
*,
4151 REAL_VALUE_TYPE
*, const long *);
4152 static void encode_vax_g (const struct real_format
*fmt
,
4153 long *, const REAL_VALUE_TYPE
*);
4154 static void decode_vax_g (const struct real_format
*,
4155 REAL_VALUE_TYPE
*, const long *);
4158 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4159 const REAL_VALUE_TYPE
*r
)
4161 unsigned long sign
, exp
, sig
, image
;
4163 sign
= r
->sign
<< 15;
4173 image
= 0xffff7fff | sign
;
4177 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4178 exp
= REAL_EXP (r
) + 128;
4180 image
= (sig
<< 16) & 0xffff0000;
4194 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4195 REAL_VALUE_TYPE
*r
, const long *buf
)
4197 unsigned long image
= buf
[0] & 0xffffffff;
4198 int exp
= (image
>> 7) & 0xff;
4200 memset (r
, 0, sizeof (*r
));
4205 r
->sign
= (image
>> 15) & 1;
4206 SET_REAL_EXP (r
, exp
- 128);
4208 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4209 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4214 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4215 const REAL_VALUE_TYPE
*r
)
4217 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4222 image0
= image1
= 0;
4227 image0
= 0xffff7fff | sign
;
4228 image1
= 0xffffffff;
4232 /* Extract the significand into straight hi:lo. */
4233 if (HOST_BITS_PER_LONG
== 64)
4235 image0
= r
->sig
[SIGSZ
-1];
4236 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4237 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4241 image0
= r
->sig
[SIGSZ
-1];
4242 image1
= r
->sig
[SIGSZ
-2];
4243 image1
= (image0
<< 24) | (image1
>> 8);
4244 image0
= (image0
>> 8) & 0xffffff;
4247 /* Rearrange the half-words of the significand to match the
4249 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4250 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4252 /* Add the sign and exponent. */
4254 image0
|= (REAL_EXP (r
) + 128) << 7;
4261 if (FLOAT_WORDS_BIG_ENDIAN
)
4262 buf
[0] = image1
, buf
[1] = image0
;
4264 buf
[0] = image0
, buf
[1] = image1
;
4268 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4269 REAL_VALUE_TYPE
*r
, const long *buf
)
4271 unsigned long image0
, image1
;
4274 if (FLOAT_WORDS_BIG_ENDIAN
)
4275 image1
= buf
[0], image0
= buf
[1];
4277 image0
= buf
[0], image1
= buf
[1];
4278 image0
&= 0xffffffff;
4279 image1
&= 0xffffffff;
4281 exp
= (image0
>> 7) & 0xff;
4283 memset (r
, 0, sizeof (*r
));
4288 r
->sign
= (image0
>> 15) & 1;
4289 SET_REAL_EXP (r
, exp
- 128);
4291 /* Rearrange the half-words of the external format into
4292 proper ascending order. */
4293 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4294 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4296 if (HOST_BITS_PER_LONG
== 64)
4298 image0
= (image0
<< 31 << 1) | image1
;
4301 r
->sig
[SIGSZ
-1] = image0
;
4305 r
->sig
[SIGSZ
-1] = image0
;
4306 r
->sig
[SIGSZ
-2] = image1
;
4307 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4308 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4314 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4315 const REAL_VALUE_TYPE
*r
)
4317 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4322 image0
= image1
= 0;
4327 image0
= 0xffff7fff | sign
;
4328 image1
= 0xffffffff;
4332 /* Extract the significand into straight hi:lo. */
4333 if (HOST_BITS_PER_LONG
== 64)
4335 image0
= r
->sig
[SIGSZ
-1];
4336 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4337 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4341 image0
= r
->sig
[SIGSZ
-1];
4342 image1
= r
->sig
[SIGSZ
-2];
4343 image1
= (image0
<< 21) | (image1
>> 11);
4344 image0
= (image0
>> 11) & 0xfffff;
4347 /* Rearrange the half-words of the significand to match the
4349 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4350 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4352 /* Add the sign and exponent. */
4354 image0
|= (REAL_EXP (r
) + 1024) << 4;
4361 if (FLOAT_WORDS_BIG_ENDIAN
)
4362 buf
[0] = image1
, buf
[1] = image0
;
4364 buf
[0] = image0
, buf
[1] = image1
;
4368 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4369 REAL_VALUE_TYPE
*r
, const long *buf
)
4371 unsigned long image0
, image1
;
4374 if (FLOAT_WORDS_BIG_ENDIAN
)
4375 image1
= buf
[0], image0
= buf
[1];
4377 image0
= buf
[0], image1
= buf
[1];
4378 image0
&= 0xffffffff;
4379 image1
&= 0xffffffff;
4381 exp
= (image0
>> 4) & 0x7ff;
4383 memset (r
, 0, sizeof (*r
));
4388 r
->sign
= (image0
>> 15) & 1;
4389 SET_REAL_EXP (r
, exp
- 1024);
4391 /* Rearrange the half-words of the external format into
4392 proper ascending order. */
4393 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4394 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4396 if (HOST_BITS_PER_LONG
== 64)
4398 image0
= (image0
<< 31 << 1) | image1
;
4401 r
->sig
[SIGSZ
-1] = image0
;
4405 r
->sig
[SIGSZ
-1] = image0
;
4406 r
->sig
[SIGSZ
-2] = image1
;
4407 lshift_significand (r
, r
, 64 - 53);
4408 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4413 const struct real_format vax_f_format
=
4434 const struct real_format vax_d_format
=
4455 const struct real_format vax_g_format
=
4476 /* Encode real R into a single precision DFP value in BUF. */
4478 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4479 long *buf ATTRIBUTE_UNUSED
,
4480 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4482 encode_decimal32 (fmt
, buf
, r
);
4485 /* Decode a single precision DFP value in BUF into a real R. */
4487 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4488 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4489 const long *buf ATTRIBUTE_UNUSED
)
4491 decode_decimal32 (fmt
, r
, buf
);
4494 /* Encode real R into a double precision DFP value in BUF. */
4496 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4497 long *buf ATTRIBUTE_UNUSED
,
4498 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4500 encode_decimal64 (fmt
, buf
, r
);
4503 /* Decode a double precision DFP value in BUF into a real R. */
4505 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4506 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4507 const long *buf ATTRIBUTE_UNUSED
)
4509 decode_decimal64 (fmt
, r
, buf
);
4512 /* Encode real R into a quad precision DFP value in BUF. */
4514 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4515 long *buf ATTRIBUTE_UNUSED
,
4516 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4518 encode_decimal128 (fmt
, buf
, r
);
4521 /* Decode a quad precision DFP value in BUF into a real R. */
4523 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4524 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4525 const long *buf ATTRIBUTE_UNUSED
)
4527 decode_decimal128 (fmt
, r
, buf
);
4530 /* Single precision decimal floating point (IEEE 754). */
4531 const struct real_format decimal_single_format
=
4533 encode_decimal_single
,
4534 decode_decimal_single
,
4552 /* Double precision decimal floating point (IEEE 754). */
4553 const struct real_format decimal_double_format
=
4555 encode_decimal_double
,
4556 decode_decimal_double
,
4574 /* Quad precision decimal floating point (IEEE 754). */
4575 const struct real_format decimal_quad_format
=
4577 encode_decimal_quad
,
4578 decode_decimal_quad
,
4596 /* Encode half-precision floats. This routine is used both for the IEEE
4597 ARM alternative encodings. */
4599 encode_ieee_half (const struct real_format
*fmt
, long *buf
,
4600 const REAL_VALUE_TYPE
*r
)
4602 unsigned long image
, sig
, exp
;
4603 unsigned long sign
= r
->sign
;
4604 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
4607 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 11)) & 0x3ff;
4625 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 9) - 1 : 0);
4626 if (r
->signalling
== fmt
->qnan_msb_set
)
4641 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4642 whereas the intermediate representation is 0.F x 2**exp.
4643 Which means we're off by one. */
4647 exp
= REAL_EXP (r
) + 15 - 1;
4659 /* Decode half-precision floats. This routine is used both for the IEEE
4660 ARM alternative encodings. */
4662 decode_ieee_half (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
4665 unsigned long image
= buf
[0] & 0xffff;
4666 bool sign
= (image
>> 15) & 1;
4667 int exp
= (image
>> 10) & 0x1f;
4669 memset (r
, 0, sizeof (*r
));
4670 image
<<= HOST_BITS_PER_LONG
- 11;
4675 if (image
&& fmt
->has_denorm
)
4679 SET_REAL_EXP (r
, -14);
4680 r
->sig
[SIGSZ
-1] = image
<< 1;
4683 else if (fmt
->has_signed_zero
)
4686 else if (exp
== 31 && (fmt
->has_nans
|| fmt
->has_inf
))
4692 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
4693 ^ fmt
->qnan_msb_set
);
4694 r
->sig
[SIGSZ
-1] = image
;
4706 SET_REAL_EXP (r
, exp
- 15 + 1);
4707 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
4711 /* Half-precision format, as specified in IEEE 754R. */
4712 const struct real_format ieee_half_format
=
4733 /* ARM's alternative half-precision format, similar to IEEE but with
4734 no reserved exponent value for NaNs and infinities; rather, it just
4735 extends the range of exponents by one. */
4736 const struct real_format arm_half_format
=
4757 /* A synthetic "format" for internal arithmetic. It's the size of the
4758 internal significand minus the two bits needed for proper rounding.
4759 The encode and decode routines exist only to satisfy our paranoia
4762 static void encode_internal (const struct real_format
*fmt
,
4763 long *, const REAL_VALUE_TYPE
*);
4764 static void decode_internal (const struct real_format
*,
4765 REAL_VALUE_TYPE
*, const long *);
4768 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4769 const REAL_VALUE_TYPE
*r
)
4771 memcpy (buf
, r
, sizeof (*r
));
4775 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4776 REAL_VALUE_TYPE
*r
, const long *buf
)
4778 memcpy (r
, buf
, sizeof (*r
));
4781 const struct real_format real_internal_format
=
4786 SIGNIFICAND_BITS
- 2,
4787 SIGNIFICAND_BITS
- 2,
4802 /* Calculate the square root of X in mode MODE, and store the result
4803 in R. Return TRUE if the operation does not raise an exception.
4804 For details see "High Precision Division and Square Root",
4805 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4806 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4809 real_sqrt (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4810 const REAL_VALUE_TYPE
*x
)
4812 static REAL_VALUE_TYPE halfthree
;
4813 static bool init
= false;
4814 REAL_VALUE_TYPE h
, t
, i
;
4817 /* sqrt(-0.0) is -0.0. */
4818 if (real_isnegzero (x
))
4824 /* Negative arguments return NaN. */
4827 get_canonical_qnan (r
, 0);
4831 /* Infinity and NaN return themselves. */
4832 if (!real_isfinite (x
))
4840 do_add (&halfthree
, &dconst1
, &dconsthalf
, 0);
4844 /* Initial guess for reciprocal sqrt, i. */
4845 exp
= real_exponent (x
);
4846 real_ldexp (&i
, &dconst1
, -exp
/2);
4848 /* Newton's iteration for reciprocal sqrt, i. */
4849 for (iter
= 0; iter
< 16; iter
++)
4851 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4852 do_multiply (&t
, x
, &i
);
4853 do_multiply (&h
, &t
, &i
);
4854 do_multiply (&t
, &h
, &dconsthalf
);
4855 do_add (&h
, &halfthree
, &t
, 1);
4856 do_multiply (&t
, &i
, &h
);
4858 /* Check for early convergence. */
4859 if (iter
>= 6 && real_identical (&i
, &t
))
4862 /* ??? Unroll loop to avoid copying. */
4866 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4867 do_multiply (&t
, x
, &i
);
4868 do_multiply (&h
, &t
, &i
);
4869 do_add (&i
, &dconst1
, &h
, 1);
4870 do_multiply (&h
, &t
, &i
);
4871 do_multiply (&i
, &dconsthalf
, &h
);
4872 do_add (&h
, &t
, &i
, 0);
4874 /* ??? We need a Tuckerman test to get the last bit. */
4876 real_convert (r
, mode
, &h
);
4880 /* Calculate X raised to the integer exponent N in mode MODE and store
4881 the result in R. Return true if the result may be inexact due to
4882 loss of precision. The algorithm is the classic "left-to-right binary
4883 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4884 Algorithms", "The Art of Computer Programming", Volume 2. */
4887 real_powi (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4888 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4890 unsigned HOST_WIDE_INT bit
;
4892 bool inexact
= false;
4904 /* Don't worry about overflow, from now on n is unsigned. */
4912 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4913 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4917 inexact
|= do_multiply (&t
, &t
, &t
);
4919 inexact
|= do_multiply (&t
, &t
, x
);
4927 inexact
|= do_divide (&t
, &dconst1
, &t
);
4929 real_convert (r
, mode
, &t
);
4933 /* Round X to the nearest integer not larger in absolute value, i.e.
4934 towards zero, placing the result in R in mode MODE. */
4937 real_trunc (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4938 const REAL_VALUE_TYPE
*x
)
4940 do_fix_trunc (r
, x
);
4941 if (mode
!= VOIDmode
)
4942 real_convert (r
, mode
, r
);
4945 /* Round X to the largest integer not greater in value, i.e. round
4946 down, placing the result in R in mode MODE. */
4949 real_floor (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4950 const REAL_VALUE_TYPE
*x
)
4954 do_fix_trunc (&t
, x
);
4955 if (! real_identical (&t
, x
) && x
->sign
)
4956 do_add (&t
, &t
, &dconstm1
, 0);
4957 if (mode
!= VOIDmode
)
4958 real_convert (r
, mode
, &t
);
4963 /* Round X to the smallest integer not less then argument, i.e. round
4964 up, placing the result in R in mode MODE. */
4967 real_ceil (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4968 const REAL_VALUE_TYPE
*x
)
4972 do_fix_trunc (&t
, x
);
4973 if (! real_identical (&t
, x
) && ! x
->sign
)
4974 do_add (&t
, &t
, &dconst1
, 0);
4975 if (mode
!= VOIDmode
)
4976 real_convert (r
, mode
, &t
);
4981 /* Round X to the nearest integer, but round halfway cases away from
4985 real_round (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4986 const REAL_VALUE_TYPE
*x
)
4988 do_add (r
, x
, &dconsthalf
, x
->sign
);
4989 do_fix_trunc (r
, r
);
4990 if (mode
!= VOIDmode
)
4991 real_convert (r
, mode
, r
);
4994 /* Set the sign of R to the sign of X. */
4997 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
5002 /* Check whether the real constant value given is an integer. */
5005 real_isinteger (const REAL_VALUE_TYPE
*c
, enum machine_mode mode
)
5007 REAL_VALUE_TYPE cint
;
5009 real_trunc (&cint
, mode
, c
);
5010 return real_identical (c
, &cint
);
5013 /* Write into BUF the maximum representable finite floating-point
5014 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5015 float string. LEN is the size of BUF, and the buffer must be large
5016 enough to contain the resulting string. */
5019 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
5024 strcpy (buf
, "0x0.");
5026 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
5029 *p
++ = "08ce"[n
- i
];
5030 sprintf (p
, "p%d", fmt
->emax
);
5031 if (fmt
->pnan
< fmt
->p
)
5033 /* This is an IBM extended double format made up of two IEEE
5034 doubles. The value of the long double is the sum of the
5035 values of the two parts. The most significant part is
5036 required to be the value of the long double rounded to the
5037 nearest double. Rounding means we need a slightly smaller
5038 value for LDBL_MAX. */
5039 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
5042 gcc_assert (strlen (buf
) < len
);