1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2002, 2003, 2004, 2005, 2007 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"
33 /* The floating point model used internally is not exactly IEEE 754
34 compliant, and close to the description in the ISO C99 standard,
35 section 5.2.4.2.2 Characteristics of floating types.
39 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
43 b = base or radix, here always 2
45 p = precision (the number of base-b digits in the significand)
46 f_k = the digits of the significand.
48 We differ from typical IEEE 754 encodings in that the entire
49 significand is fractional. Normalized significands are in the
52 A requirement of the model is that P be larger than the largest
53 supported target floating-point type by at least 2 bits. This gives
54 us proper rounding when we truncate to the target type. In addition,
55 E must be large enough to hold the smallest supported denormal number
58 Both of these requirements are easily satisfied. The largest target
59 significand is 113 bits; we store at least 160. The smallest
60 denormal number fits in 17 exponent bits; we store 27.
62 Note that the decimal string conversion routines are sensitive to
63 rounding errors. Since the raw arithmetic routines do not themselves
64 have guard digits or rounding, the computation of 10**exp can
65 accumulate more than a few digits of error. The previous incarnation
66 of real.c successfully used a 144-bit fraction; given the current
67 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits.
69 Target floating point models that use base 16 instead of base 2
70 (i.e. IBM 370), are handled during round_for_format, in which we
71 canonicalize the exponent to be a multiple of 4 (log2(16)), and
72 adjust the significand to match. */
75 /* Used to classify two numbers simultaneously. */
76 #define CLASS2(A, B) ((A) << 2 | (B))
78 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
79 #error "Some constant folding done by hand to avoid shift count warnings"
82 static void get_zero (REAL_VALUE_TYPE
*, int);
83 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
84 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
85 static void get_inf (REAL_VALUE_TYPE
*, int);
86 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
87 const REAL_VALUE_TYPE
*, unsigned int);
88 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
90 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
92 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
93 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
94 const REAL_VALUE_TYPE
*);
95 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
96 const REAL_VALUE_TYPE
*, int);
97 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
98 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
99 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
100 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
101 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
102 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
103 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
104 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
105 const REAL_VALUE_TYPE
*);
106 static void normalize (REAL_VALUE_TYPE
*);
108 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
109 const REAL_VALUE_TYPE
*, int);
110 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
111 const REAL_VALUE_TYPE
*);
112 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
113 const REAL_VALUE_TYPE
*);
114 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
115 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
117 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, 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_inf
, rvc_zero
):
914 case CLASS2 (rvc_inf
, rvc_normal
):
915 case CLASS2 (rvc_normal
, rvc_zero
):
916 return (a
->sign
? -1 : 1);
918 case CLASS2 (rvc_inf
, rvc_inf
):
919 return -a
->sign
- -b
->sign
;
921 case CLASS2 (rvc_zero
, rvc_normal
):
922 case CLASS2 (rvc_zero
, rvc_inf
):
923 case CLASS2 (rvc_normal
, rvc_inf
):
924 return (b
->sign
? 1 : -1);
926 case CLASS2 (rvc_zero
, rvc_nan
):
927 case CLASS2 (rvc_normal
, rvc_nan
):
928 case CLASS2 (rvc_inf
, rvc_nan
):
929 case CLASS2 (rvc_nan
, rvc_nan
):
930 case CLASS2 (rvc_nan
, rvc_zero
):
931 case CLASS2 (rvc_nan
, rvc_normal
):
932 case CLASS2 (rvc_nan
, rvc_inf
):
935 case CLASS2 (rvc_normal
, rvc_normal
):
942 if (a
->sign
!= b
->sign
)
943 return -a
->sign
- -b
->sign
;
945 if (a
->decimal
|| b
->decimal
)
946 return decimal_do_compare (a
, b
, nan_result
);
948 if (REAL_EXP (a
) > REAL_EXP (b
))
950 else if (REAL_EXP (a
) < REAL_EXP (b
))
953 ret
= cmp_significands (a
, b
);
955 return (a
->sign
? -ret
: ret
);
958 /* Return A truncated to an integral value toward zero. */
961 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
975 decimal_do_fix_trunc (r
, a
);
978 if (REAL_EXP (r
) <= 0)
979 get_zero (r
, r
->sign
);
980 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
981 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
989 /* Perform the binary or unary operation described by CODE.
990 For a unary operation, leave OP1 NULL. This function returns
991 true if the result may be inexact due to loss of precision. */
994 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
995 const REAL_VALUE_TYPE
*op1
)
997 enum tree_code code
= icode
;
999 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1000 return decimal_real_arithmetic (r
, icode
, op0
, op1
);
1005 return do_add (r
, op0
, op1
, 0);
1008 return do_add (r
, op0
, op1
, 1);
1011 return do_multiply (r
, op0
, op1
);
1014 return do_divide (r
, op0
, op1
);
1017 if (op1
->cl
== rvc_nan
)
1019 else if (do_compare (op0
, op1
, -1) < 0)
1026 if (op1
->cl
== rvc_nan
)
1028 else if (do_compare (op0
, op1
, 1) < 0)
1044 case FIX_TRUNC_EXPR
:
1045 do_fix_trunc (r
, op0
);
1054 /* Legacy. Similar, but return the result directly. */
1057 real_arithmetic2 (int icode
, const REAL_VALUE_TYPE
*op0
,
1058 const REAL_VALUE_TYPE
*op1
)
1061 real_arithmetic (&r
, icode
, op0
, op1
);
1066 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1067 const REAL_VALUE_TYPE
*op1
)
1069 enum tree_code code
= icode
;
1074 return do_compare (op0
, op1
, 1) < 0;
1076 return do_compare (op0
, op1
, 1) <= 0;
1078 return do_compare (op0
, op1
, -1) > 0;
1080 return do_compare (op0
, op1
, -1) >= 0;
1082 return do_compare (op0
, op1
, -1) == 0;
1084 return do_compare (op0
, op1
, -1) != 0;
1085 case UNORDERED_EXPR
:
1086 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1088 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1090 return do_compare (op0
, op1
, -1) < 0;
1092 return do_compare (op0
, op1
, -1) <= 0;
1094 return do_compare (op0
, op1
, 1) > 0;
1096 return do_compare (op0
, op1
, 1) >= 0;
1098 return do_compare (op0
, op1
, 0) == 0;
1100 return do_compare (op0
, op1
, 0) != 0;
1107 /* Return floor log2(R). */
1110 real_exponent (const REAL_VALUE_TYPE
*r
)
1118 return (unsigned int)-1 >> 1;
1120 return REAL_EXP (r
);
1126 /* R = OP0 * 2**EXP. */
1129 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1140 exp
+= REAL_EXP (op0
);
1142 get_inf (r
, r
->sign
);
1143 else if (exp
< -MAX_EXP
)
1144 get_zero (r
, r
->sign
);
1146 SET_REAL_EXP (r
, exp
);
1154 /* Determine whether a floating-point value X is infinite. */
1157 real_isinf (const REAL_VALUE_TYPE
*r
)
1159 return (r
->cl
== rvc_inf
);
1162 /* Determine whether a floating-point value X is a NaN. */
1165 real_isnan (const REAL_VALUE_TYPE
*r
)
1167 return (r
->cl
== rvc_nan
);
1170 /* Determine whether a floating-point value X is negative. */
1173 real_isneg (const REAL_VALUE_TYPE
*r
)
1178 /* Determine whether a floating-point value X is minus zero. */
1181 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1183 return r
->sign
&& r
->cl
== rvc_zero
;
1186 /* Compare two floating-point objects for bitwise identity. */
1189 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1195 if (a
->sign
!= b
->sign
)
1205 if (a
->decimal
!= b
->decimal
)
1207 if (REAL_EXP (a
) != REAL_EXP (b
))
1212 if (a
->signalling
!= b
->signalling
)
1214 /* The significand is ignored for canonical NaNs. */
1215 if (a
->canonical
|| b
->canonical
)
1216 return a
->canonical
== b
->canonical
;
1223 for (i
= 0; i
< SIGSZ
; ++i
)
1224 if (a
->sig
[i
] != b
->sig
[i
])
1230 /* Try to change R into its exact multiplicative inverse in machine
1231 mode MODE. Return true if successful. */
1234 exact_real_inverse (enum machine_mode mode
, REAL_VALUE_TYPE
*r
)
1236 const REAL_VALUE_TYPE
*one
= real_digit (1);
1240 if (r
->cl
!= rvc_normal
)
1243 /* Check for a power of two: all significand bits zero except the MSB. */
1244 for (i
= 0; i
< SIGSZ
-1; ++i
)
1247 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1250 /* Find the inverse and truncate to the required mode. */
1251 do_divide (&u
, one
, r
);
1252 real_convert (&u
, mode
, &u
);
1254 /* The rounding may have overflowed. */
1255 if (u
.cl
!= rvc_normal
)
1257 for (i
= 0; i
< SIGSZ
-1; ++i
)
1260 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1267 /* Render R as an integer. */
1270 real_to_integer (const REAL_VALUE_TYPE
*r
)
1272 unsigned HOST_WIDE_INT i
;
1283 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1290 return decimal_real_to_integer (r
);
1292 if (REAL_EXP (r
) <= 0)
1294 /* Only force overflow for unsigned overflow. Signed overflow is
1295 undefined, so it doesn't matter what we return, and some callers
1296 expect to be able to use this routine for both signed and
1297 unsigned conversions. */
1298 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1301 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1302 i
= r
->sig
[SIGSZ
-1];
1305 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1306 i
= r
->sig
[SIGSZ
-1];
1307 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1308 i
|= r
->sig
[SIGSZ
-2];
1311 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1322 /* Likewise, but to an integer pair, HI+LOW. */
1325 real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
1326 const REAL_VALUE_TYPE
*r
)
1329 HOST_WIDE_INT low
, high
;
1342 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1355 decimal_real_to_integer2 (plow
, phigh
, r
);
1362 /* Only force overflow for unsigned overflow. Signed overflow is
1363 undefined, so it doesn't matter what we return, and some callers
1364 expect to be able to use this routine for both signed and
1365 unsigned conversions. */
1366 if (exp
> 2*HOST_BITS_PER_WIDE_INT
)
1369 rshift_significand (&t
, r
, 2*HOST_BITS_PER_WIDE_INT
- exp
);
1370 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1372 high
= t
.sig
[SIGSZ
-1];
1373 low
= t
.sig
[SIGSZ
-2];
1377 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
);
1378 high
= t
.sig
[SIGSZ
-1];
1379 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1380 high
|= t
.sig
[SIGSZ
-2];
1382 low
= t
.sig
[SIGSZ
-3];
1383 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1384 low
|= t
.sig
[SIGSZ
-4];
1392 low
= -low
, high
= ~high
;
1404 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1405 of NUM / DEN. Return the quotient and place the remainder in NUM.
1406 It is expected that NUM / DEN are close enough that the quotient is
1409 static unsigned long
1410 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1412 unsigned long q
, msb
;
1413 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1422 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1424 lshift_significand_1 (num
, num
);
1426 if (msb
|| cmp_significands (num
, den
) >= 0)
1428 sub_significands (num
, num
, den
, 0);
1432 while (--expn
>= expd
);
1434 SET_REAL_EXP (num
, expd
);
1440 /* Render R as a decimal floating point constant. Emit DIGITS significant
1441 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1442 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1445 #define M_LOG10_2 0.30102999566398119521
1448 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1449 size_t digits
, int crop_trailing_zeros
)
1451 const REAL_VALUE_TYPE
*one
, *ten
;
1452 REAL_VALUE_TYPE r
, pten
, u
, v
;
1453 int dec_exp
, cmp_one
, digit
;
1455 char *p
, *first
, *last
;
1462 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1467 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1470 /* ??? Print the significand as well, if not canonical? */
1471 strcpy (str
, (r
.sign
? "-NaN" : "+NaN"));
1479 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1483 /* Bound the number of digits printed by the size of the representation. */
1484 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1485 if (digits
== 0 || digits
> max_digits
)
1486 digits
= max_digits
;
1488 /* Estimate the decimal exponent, and compute the length of the string it
1489 will print as. Be conservative and add one to account for possible
1490 overflow or rounding error. */
1491 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1492 for (max_digits
= 1; dec_exp
; max_digits
++)
1495 /* Bound the number of digits printed by the size of the output buffer. */
1496 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1497 gcc_assert (max_digits
<= buf_size
);
1498 if (digits
> max_digits
)
1499 digits
= max_digits
;
1501 one
= real_digit (1);
1502 ten
= ten_to_ptwo (0);
1510 cmp_one
= do_compare (&r
, one
, 0);
1515 /* Number is greater than one. Convert significand to an integer
1516 and strip trailing decimal zeros. */
1519 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1521 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1522 m
= floor_log2 (max_digits
);
1524 /* Iterate over the bits of the possible powers of 10 that might
1525 be present in U and eliminate them. That is, if we find that
1526 10**2**M divides U evenly, keep the division and increase
1532 do_divide (&t
, &u
, ten_to_ptwo (m
));
1533 do_fix_trunc (&v
, &t
);
1534 if (cmp_significands (&v
, &t
) == 0)
1542 /* Revert the scaling to integer that we performed earlier. */
1543 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1544 - (SIGNIFICAND_BITS
- 1));
1547 /* Find power of 10. Do this by dividing out 10**2**M when
1548 this is larger than the current remainder. Fill PTEN with
1549 the power of 10 that we compute. */
1550 if (REAL_EXP (&r
) > 0)
1552 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1555 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1556 if (do_compare (&u
, ptentwo
, 0) >= 0)
1558 do_divide (&u
, &u
, ptentwo
);
1559 do_multiply (&pten
, &pten
, ptentwo
);
1566 /* We managed to divide off enough tens in the above reduction
1567 loop that we've now got a negative exponent. Fall into the
1568 less-than-one code to compute the proper value for PTEN. */
1575 /* Number is less than one. Pad significand with leading
1581 /* Stop if we'd shift bits off the bottom. */
1585 do_multiply (&u
, &v
, ten
);
1587 /* Stop if we're now >= 1. */
1588 if (REAL_EXP (&u
) > 0)
1596 /* Find power of 10. Do this by multiplying in P=10**2**M when
1597 the current remainder is smaller than 1/P. Fill PTEN with the
1598 power of 10 that we compute. */
1599 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1602 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1603 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1605 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1607 do_multiply (&v
, &v
, ptentwo
);
1608 do_multiply (&pten
, &pten
, ptentwo
);
1614 /* Invert the positive power of 10 that we've collected so far. */
1615 do_divide (&pten
, one
, &pten
);
1623 /* At this point, PTEN should contain the nearest power of 10 smaller
1624 than R, such that this division produces the first digit.
1626 Using a divide-step primitive that returns the complete integral
1627 remainder avoids the rounding error that would be produced if
1628 we were to use do_divide here and then simply multiply by 10 for
1629 each subsequent digit. */
1631 digit
= rtd_divmod (&r
, &pten
);
1633 /* Be prepared for error in that division via underflow ... */
1634 if (digit
== 0 && cmp_significand_0 (&r
))
1636 /* Multiply by 10 and try again. */
1637 do_multiply (&r
, &r
, ten
);
1638 digit
= rtd_divmod (&r
, &pten
);
1640 gcc_assert (digit
!= 0);
1643 /* ... or overflow. */
1653 gcc_assert (digit
<= 10);
1657 /* Generate subsequent digits. */
1658 while (--digits
> 0)
1660 do_multiply (&r
, &r
, ten
);
1661 digit
= rtd_divmod (&r
, &pten
);
1666 /* Generate one more digit with which to do rounding. */
1667 do_multiply (&r
, &r
, ten
);
1668 digit
= rtd_divmod (&r
, &pten
);
1670 /* Round the result. */
1673 /* Round to nearest. If R is nonzero there are additional
1674 nonzero digits to be extracted. */
1675 if (cmp_significand_0 (&r
))
1677 /* Round to even. */
1678 else if ((p
[-1] - '0') & 1)
1695 /* Carry out of the first digit. This means we had all 9's and
1696 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1704 /* Insert the decimal point. */
1705 first
[0] = first
[1];
1708 /* If requested, drop trailing zeros. Never crop past "1.0". */
1709 if (crop_trailing_zeros
)
1710 while (last
> first
+ 3 && last
[-1] == '0')
1713 /* Append the exponent. */
1714 sprintf (last
, "e%+d", dec_exp
);
1717 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1718 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1719 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1720 strip trailing zeros. */
1723 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1724 size_t digits
, int crop_trailing_zeros
)
1726 int i
, j
, exp
= REAL_EXP (r
);
1739 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1742 /* ??? Print the significand as well, if not canonical? */
1743 strcpy (str
, (r
->sign
? "-NaN" : "+NaN"));
1751 /* Hexadecimal format for decimal floats is not interesting. */
1752 strcpy (str
, "N/A");
1757 digits
= SIGNIFICAND_BITS
/ 4;
1759 /* Bound the number of digits printed by the size of the output buffer. */
1761 sprintf (exp_buf
, "p%+d", exp
);
1762 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1763 gcc_assert (max_digits
<= buf_size
);
1764 if (digits
> max_digits
)
1765 digits
= max_digits
;
1776 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1777 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1779 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1785 if (crop_trailing_zeros
)
1786 while (p
> first
+ 1 && p
[-1] == '0')
1789 sprintf (p
, "p%+d", exp
);
1792 /* Initialize R from a decimal or hexadecimal string. The string is
1793 assumed to have been syntax checked already. */
1796 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1808 else if (*str
== '+')
1811 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1813 /* Hexadecimal floating point. */
1814 int pos
= SIGNIFICAND_BITS
- 4, d
;
1822 d
= hex_value (*str
);
1827 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1828 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1832 /* Ensure correct rounding by setting last bit if there is
1833 a subsequent nonzero digit. */
1841 if (pos
== SIGNIFICAND_BITS
- 4)
1848 d
= hex_value (*str
);
1853 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1854 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1858 /* Ensure correct rounding by setting last bit if there is
1859 a subsequent nonzero digit. */
1865 /* If the mantissa is zero, ignore the exponent. */
1866 if (!cmp_significand_0 (r
))
1869 if (*str
== 'p' || *str
== 'P')
1871 bool exp_neg
= false;
1879 else if (*str
== '+')
1883 while (ISDIGIT (*str
))
1889 /* Overflowed the exponent. */
1904 SET_REAL_EXP (r
, exp
);
1910 /* Decimal floating point. */
1911 const REAL_VALUE_TYPE
*ten
= ten_to_ptwo (0);
1916 while (ISDIGIT (*str
))
1919 do_multiply (r
, r
, ten
);
1921 do_add (r
, r
, real_digit (d
), 0);
1926 if (r
->cl
== rvc_zero
)
1931 while (ISDIGIT (*str
))
1934 do_multiply (r
, r
, ten
);
1936 do_add (r
, r
, real_digit (d
), 0);
1941 /* If the mantissa is zero, ignore the exponent. */
1942 if (r
->cl
== rvc_zero
)
1945 if (*str
== 'e' || *str
== 'E')
1947 bool exp_neg
= false;
1955 else if (*str
== '+')
1959 while (ISDIGIT (*str
))
1965 /* Overflowed the exponent. */
1979 times_pten (r
, exp
);
1994 /* Legacy. Similar, but return the result directly. */
1997 real_from_string2 (const char *s
, enum machine_mode mode
)
2001 real_from_string (&r
, s
);
2002 if (mode
!= VOIDmode
)
2003 real_convert (&r
, mode
, &r
);
2008 /* Initialize R from string S and desired MODE. */
2011 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, enum machine_mode mode
)
2013 if (DECIMAL_FLOAT_MODE_P (mode
))
2014 decimal_real_from_string (r
, s
);
2016 real_from_string (r
, s
);
2018 if (mode
!= VOIDmode
)
2019 real_convert (r
, mode
, r
);
2022 /* Initialize R from the integer pair HIGH+LOW. */
2025 real_from_integer (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2026 unsigned HOST_WIDE_INT low
, HOST_WIDE_INT high
,
2029 if (low
== 0 && high
== 0)
2033 memset (r
, 0, sizeof (*r
));
2035 r
->sign
= high
< 0 && !unsigned_p
;
2036 SET_REAL_EXP (r
, 2 * HOST_BITS_PER_WIDE_INT
);
2047 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2049 r
->sig
[SIGSZ
-1] = high
;
2050 r
->sig
[SIGSZ
-2] = low
;
2054 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2055 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2056 r
->sig
[SIGSZ
-2] = high
;
2057 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2058 r
->sig
[SIGSZ
-4] = low
;
2064 if (mode
!= VOIDmode
)
2065 real_convert (r
, mode
, r
);
2068 /* Returns 10**2**N. */
2070 static const REAL_VALUE_TYPE
*
2073 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2075 gcc_assert (n
>= 0);
2076 gcc_assert (n
< EXP_BITS
);
2078 if (tens
[n
].cl
== rvc_zero
)
2080 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2082 HOST_WIDE_INT t
= 10;
2085 for (i
= 0; i
< n
; ++i
)
2088 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2092 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2093 do_multiply (&tens
[n
], t
, t
);
2100 /* Returns 10**(-2**N). */
2102 static const REAL_VALUE_TYPE
*
2103 ten_to_mptwo (int n
)
2105 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2107 gcc_assert (n
>= 0);
2108 gcc_assert (n
< EXP_BITS
);
2110 if (tens
[n
].cl
== rvc_zero
)
2111 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2118 static const REAL_VALUE_TYPE
*
2121 static REAL_VALUE_TYPE num
[10];
2123 gcc_assert (n
>= 0);
2124 gcc_assert (n
<= 9);
2126 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2127 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2132 /* Multiply R by 10**EXP. */
2135 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2137 REAL_VALUE_TYPE pten
, *rr
;
2138 bool negative
= (exp
< 0);
2144 pten
= *real_digit (1);
2150 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2152 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2155 do_divide (r
, r
, &pten
);
2158 /* Fills R with +Inf. */
2161 real_inf (REAL_VALUE_TYPE
*r
)
2166 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2167 we force a QNaN, else we force an SNaN. The string, if not empty,
2168 is parsed as a number and placed in the significand. Return true
2169 if the string was successfully parsed. */
2172 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2173 enum machine_mode mode
)
2175 const struct real_format
*fmt
;
2177 fmt
= REAL_MODE_FORMAT (mode
);
2183 get_canonical_qnan (r
, 0);
2185 get_canonical_snan (r
, 0);
2191 memset (r
, 0, sizeof (*r
));
2194 /* Parse akin to strtol into the significand of R. */
2196 while (ISSPACE (*str
))
2200 else if (*str
== '+')
2205 if (*str
== 'x' || *str
== 'X')
2214 while ((d
= hex_value (*str
)) < base
)
2221 lshift_significand (r
, r
, 3);
2224 lshift_significand (r
, r
, 4);
2227 lshift_significand_1 (&u
, r
);
2228 lshift_significand (r
, r
, 3);
2229 add_significands (r
, r
, &u
);
2237 add_significands (r
, r
, &u
);
2242 /* Must have consumed the entire string for success. */
2246 /* Shift the significand into place such that the bits
2247 are in the most significant bits for the format. */
2248 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2250 /* Our MSB is always unset for NaNs. */
2251 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2253 /* Force quiet or signalling NaN. */
2254 r
->signalling
= !quiet
;
2260 /* Fills R with the largest finite value representable in mode MODE.
2261 If SIGN is nonzero, R is set to the most negative finite value. */
2264 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
2266 const struct real_format
*fmt
;
2269 fmt
= REAL_MODE_FORMAT (mode
);
2271 memset (r
, 0, sizeof (*r
));
2274 decimal_real_maxval (r
, sign
, mode
);
2279 SET_REAL_EXP (r
, fmt
->emax
* fmt
->log2_b
);
2281 np2
= SIGNIFICAND_BITS
- fmt
->p
* fmt
->log2_b
;
2282 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2283 clear_significand_below (r
, np2
);
2285 if (fmt
->pnan
< fmt
->p
)
2286 /* This is an IBM extended double format made up of two IEEE
2287 doubles. The value of the long double is the sum of the
2288 values of the two parts. The most significant part is
2289 required to be the value of the long double rounded to the
2290 nearest double. Rounding means we need a slightly smaller
2291 value for LDBL_MAX. */
2292 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2296 /* Fills R with 2**N. */
2299 real_2expN (REAL_VALUE_TYPE
*r
, int n
)
2301 memset (r
, 0, sizeof (*r
));
2306 else if (n
< -MAX_EXP
)
2311 SET_REAL_EXP (r
, n
);
2312 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2318 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2321 unsigned long sticky
;
2329 decimal_round_for_format (fmt
, r
);
2332 /* FIXME. We can come here via fp_easy_constant
2333 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2334 investigated whether this convert needs to be here, or
2335 something else is missing. */
2336 decimal_real_convert (r
, DFmode
, r
);
2339 p2
= fmt
->p
* fmt
->log2_b
;
2340 emin2m1
= (fmt
->emin
- 1) * fmt
->log2_b
;
2341 emax2
= fmt
->emax
* fmt
->log2_b
;
2343 np2
= SIGNIFICAND_BITS
- p2
;
2347 get_zero (r
, r
->sign
);
2349 if (!fmt
->has_signed_zero
)
2354 get_inf (r
, r
->sign
);
2359 clear_significand_below (r
, np2
);
2369 /* If we're not base2, normalize the exponent to a multiple of
2371 if (fmt
->log2_b
!= 1)
2375 gcc_assert (fmt
->b
!= 10);
2376 shift
= REAL_EXP (r
) & (fmt
->log2_b
- 1);
2379 shift
= fmt
->log2_b
- shift
;
2380 r
->sig
[0] |= sticky_rshift_significand (r
, r
, shift
);
2381 SET_REAL_EXP (r
, REAL_EXP (r
) + shift
);
2385 /* Check the range of the exponent. If we're out of range,
2386 either underflow or overflow. */
2387 if (REAL_EXP (r
) > emax2
)
2389 else if (REAL_EXP (r
) <= emin2m1
)
2393 if (!fmt
->has_denorm
)
2395 /* Don't underflow completely until we've had a chance to round. */
2396 if (REAL_EXP (r
) < emin2m1
)
2401 diff
= emin2m1
- REAL_EXP (r
) + 1;
2405 /* De-normalize the significand. */
2406 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2407 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2411 /* There are P2 true significand bits, followed by one guard bit,
2412 followed by one sticky bit, followed by stuff. Fold nonzero
2413 stuff into the sticky bit. */
2416 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2417 sticky
|= r
->sig
[i
];
2419 r
->sig
[w
] & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2421 guard
= test_significand_bit (r
, np2
- 1);
2422 lsb
= test_significand_bit (r
, np2
);
2424 /* Round to even. */
2425 if (guard
&& (sticky
|| lsb
))
2429 set_significand_bit (&u
, np2
);
2431 if (add_significands (r
, r
, &u
))
2433 /* Overflow. Means the significand had been all ones, and
2434 is now all zeros. Need to increase the exponent, and
2435 possibly re-normalize it. */
2436 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2437 if (REAL_EXP (r
) > emax2
)
2439 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2441 if (fmt
->log2_b
!= 1)
2443 int shift
= REAL_EXP (r
) & (fmt
->log2_b
- 1);
2446 shift
= fmt
->log2_b
- shift
;
2447 rshift_significand (r
, r
, shift
);
2448 SET_REAL_EXP (r
, REAL_EXP (r
) + shift
);
2449 if (REAL_EXP (r
) > emax2
)
2456 /* Catch underflow that we deferred until after rounding. */
2457 if (REAL_EXP (r
) <= emin2m1
)
2460 /* Clear out trailing garbage. */
2461 clear_significand_below (r
, np2
);
2464 /* Extend or truncate to a new mode. */
2467 real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2468 const REAL_VALUE_TYPE
*a
)
2470 const struct real_format
*fmt
;
2472 fmt
= REAL_MODE_FORMAT (mode
);
2477 if (a
->decimal
|| fmt
->b
== 10)
2478 decimal_real_convert (r
, mode
, a
);
2480 round_for_format (fmt
, r
);
2482 /* round_for_format de-normalizes denormals. Undo just that part. */
2483 if (r
->cl
== rvc_normal
)
2487 /* Legacy. Likewise, except return the struct directly. */
2490 real_value_truncate (enum machine_mode mode
, REAL_VALUE_TYPE a
)
2493 real_convert (&r
, mode
, &a
);
2497 /* Return true if truncating to MODE is exact. */
2500 exact_real_truncate (enum machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2502 const struct real_format
*fmt
;
2506 fmt
= REAL_MODE_FORMAT (mode
);
2509 /* Don't allow conversion to denormals. */
2510 emin2m1
= (fmt
->emin
- 1) * fmt
->log2_b
;
2511 if (REAL_EXP (a
) <= emin2m1
)
2514 /* After conversion to the new mode, the value must be identical. */
2515 real_convert (&t
, mode
, a
);
2516 return real_identical (&t
, a
);
2519 /* Write R to the given target format. Place the words of the result
2520 in target word order in BUF. There are always 32 bits in each
2521 long, no matter the size of the host long.
2523 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2526 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2527 const struct real_format
*fmt
)
2533 round_for_format (fmt
, &r
);
2537 (*fmt
->encode
) (fmt
, buf
, &r
);
2542 /* Similar, but look up the format from MODE. */
2545 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, enum machine_mode mode
)
2547 const struct real_format
*fmt
;
2549 fmt
= REAL_MODE_FORMAT (mode
);
2552 return real_to_target_fmt (buf
, r
, fmt
);
2555 /* Read R from the given target format. Read the words of the result
2556 in target word order in BUF. There are always 32 bits in each
2557 long, no matter the size of the host long. */
2560 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2561 const struct real_format
*fmt
)
2563 (*fmt
->decode
) (fmt
, r
, buf
);
2566 /* Similar, but look up the format from MODE. */
2569 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, enum machine_mode mode
)
2571 const struct real_format
*fmt
;
2573 fmt
= REAL_MODE_FORMAT (mode
);
2576 (*fmt
->decode
) (fmt
, r
, buf
);
2579 /* Return the number of bits of the largest binary value that the
2580 significand of MODE will hold. */
2581 /* ??? Legacy. Should get access to real_format directly. */
2584 significand_size (enum machine_mode mode
)
2586 const struct real_format
*fmt
;
2588 fmt
= REAL_MODE_FORMAT (mode
);
2594 /* Return the size in bits of the largest binary value that can be
2595 held by the decimal coefficient for this mode. This is one more
2596 than the number of bits required to hold the largest coefficient
2598 double log2_10
= 3.3219281;
2599 return fmt
->p
* log2_10
;
2601 return fmt
->p
* fmt
->log2_b
;
2604 /* Return a hash value for the given real value. */
2605 /* ??? The "unsigned int" return value is intended to be hashval_t,
2606 but I didn't want to pull hashtab.h into real.h. */
2609 real_hash (const REAL_VALUE_TYPE
*r
)
2614 h
= r
->cl
| (r
->sign
<< 2);
2622 h
|= REAL_EXP (r
) << 3;
2627 h
^= (unsigned int)-1;
2636 if (sizeof(unsigned long) > sizeof(unsigned int))
2637 for (i
= 0; i
< SIGSZ
; ++i
)
2639 unsigned long s
= r
->sig
[i
];
2640 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2643 for (i
= 0; i
< SIGSZ
; ++i
)
2649 /* IEEE single-precision format. */
2651 static void encode_ieee_single (const struct real_format
*fmt
,
2652 long *, const REAL_VALUE_TYPE
*);
2653 static void decode_ieee_single (const struct real_format
*,
2654 REAL_VALUE_TYPE
*, const long *);
2657 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2658 const REAL_VALUE_TYPE
*r
)
2660 unsigned long image
, sig
, exp
;
2661 unsigned long sign
= r
->sign
;
2662 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2665 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2676 image
|= 0x7fffffff;
2684 if (r
->signalling
== fmt
->qnan_msb_set
)
2688 /* We overload qnan_msb_set here: it's only clear for
2689 mips_ieee_single, which wants all mantissa bits but the
2690 quiet/signalling one set in canonical NaNs (at least
2692 if (r
->canonical
&& !fmt
->qnan_msb_set
)
2693 sig
|= (1 << 22) - 1;
2701 image
|= 0x7fffffff;
2705 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2706 whereas the intermediate representation is 0.F x 2**exp.
2707 Which means we're off by one. */
2711 exp
= REAL_EXP (r
) + 127 - 1;
2724 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2727 unsigned long image
= buf
[0] & 0xffffffff;
2728 bool sign
= (image
>> 31) & 1;
2729 int exp
= (image
>> 23) & 0xff;
2731 memset (r
, 0, sizeof (*r
));
2732 image
<<= HOST_BITS_PER_LONG
- 24;
2737 if (image
&& fmt
->has_denorm
)
2741 SET_REAL_EXP (r
, -126);
2742 r
->sig
[SIGSZ
-1] = image
<< 1;
2745 else if (fmt
->has_signed_zero
)
2748 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2754 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2755 ^ fmt
->qnan_msb_set
);
2756 r
->sig
[SIGSZ
-1] = image
;
2768 SET_REAL_EXP (r
, exp
- 127 + 1);
2769 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2773 const struct real_format ieee_single_format
=
2792 const struct real_format mips_single_format
=
2812 /* IEEE double-precision format. */
2814 static void encode_ieee_double (const struct real_format
*fmt
,
2815 long *, const REAL_VALUE_TYPE
*);
2816 static void decode_ieee_double (const struct real_format
*,
2817 REAL_VALUE_TYPE
*, const long *);
2820 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
2821 const REAL_VALUE_TYPE
*r
)
2823 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
2824 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2826 image_hi
= r
->sign
<< 31;
2829 if (HOST_BITS_PER_LONG
== 64)
2831 sig_hi
= r
->sig
[SIGSZ
-1];
2832 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
2833 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
2837 sig_hi
= r
->sig
[SIGSZ
-1];
2838 sig_lo
= r
->sig
[SIGSZ
-2];
2839 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
2840 sig_hi
= (sig_hi
>> 11) & 0xfffff;
2850 image_hi
|= 2047 << 20;
2853 image_hi
|= 0x7fffffff;
2854 image_lo
= 0xffffffff;
2862 sig_hi
= sig_lo
= 0;
2863 if (r
->signalling
== fmt
->qnan_msb_set
)
2864 sig_hi
&= ~(1 << 19);
2867 /* We overload qnan_msb_set here: it's only clear for
2868 mips_ieee_single, which wants all mantissa bits but the
2869 quiet/signalling one set in canonical NaNs (at least
2871 if (r
->canonical
&& !fmt
->qnan_msb_set
)
2873 sig_hi
|= (1 << 19) - 1;
2874 sig_lo
= 0xffffffff;
2876 else if (sig_hi
== 0 && sig_lo
== 0)
2879 image_hi
|= 2047 << 20;
2885 image_hi
|= 0x7fffffff;
2886 image_lo
= 0xffffffff;
2891 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2892 whereas the intermediate representation is 0.F x 2**exp.
2893 Which means we're off by one. */
2897 exp
= REAL_EXP (r
) + 1023 - 1;
2898 image_hi
|= exp
<< 20;
2907 if (FLOAT_WORDS_BIG_ENDIAN
)
2908 buf
[0] = image_hi
, buf
[1] = image_lo
;
2910 buf
[0] = image_lo
, buf
[1] = image_hi
;
2914 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2917 unsigned long image_hi
, image_lo
;
2921 if (FLOAT_WORDS_BIG_ENDIAN
)
2922 image_hi
= buf
[0], image_lo
= buf
[1];
2924 image_lo
= buf
[0], image_hi
= buf
[1];
2925 image_lo
&= 0xffffffff;
2926 image_hi
&= 0xffffffff;
2928 sign
= (image_hi
>> 31) & 1;
2929 exp
= (image_hi
>> 20) & 0x7ff;
2931 memset (r
, 0, sizeof (*r
));
2933 image_hi
<<= 32 - 21;
2934 image_hi
|= image_lo
>> 21;
2935 image_hi
&= 0x7fffffff;
2936 image_lo
<<= 32 - 21;
2940 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
2944 SET_REAL_EXP (r
, -1022);
2945 if (HOST_BITS_PER_LONG
== 32)
2947 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
2949 r
->sig
[SIGSZ
-1] = image_hi
;
2950 r
->sig
[SIGSZ
-2] = image_lo
;
2954 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
2955 r
->sig
[SIGSZ
-1] = image_hi
;
2959 else if (fmt
->has_signed_zero
)
2962 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
2964 if (image_hi
|| image_lo
)
2968 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
2969 if (HOST_BITS_PER_LONG
== 32)
2971 r
->sig
[SIGSZ
-1] = image_hi
;
2972 r
->sig
[SIGSZ
-2] = image_lo
;
2975 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
2987 SET_REAL_EXP (r
, exp
- 1023 + 1);
2988 if (HOST_BITS_PER_LONG
== 32)
2990 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
2991 r
->sig
[SIGSZ
-2] = image_lo
;
2994 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
2998 const struct real_format ieee_double_format
=
3017 const struct real_format mips_double_format
=
3037 /* IEEE extended real format. This comes in three flavors: Intel's as
3038 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3039 12- and 16-byte images may be big- or little endian; Motorola's is
3040 always big endian. */
3042 /* Helper subroutine which converts from the internal format to the
3043 12-byte little-endian Intel format. Functions below adjust this
3044 for the other possible formats. */
3046 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3047 const REAL_VALUE_TYPE
*r
)
3049 unsigned long image_hi
, sig_hi
, sig_lo
;
3050 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3052 image_hi
= r
->sign
<< 15;
3053 sig_hi
= sig_lo
= 0;
3065 /* Intel requires the explicit integer bit to be set, otherwise
3066 it considers the value a "pseudo-infinity". Motorola docs
3067 say it doesn't care. */
3068 sig_hi
= 0x80000000;
3073 sig_lo
= sig_hi
= 0xffffffff;
3081 if (HOST_BITS_PER_LONG
== 32)
3083 sig_hi
= r
->sig
[SIGSZ
-1];
3084 sig_lo
= r
->sig
[SIGSZ
-2];
3088 sig_lo
= r
->sig
[SIGSZ
-1];
3089 sig_hi
= sig_lo
>> 31 >> 1;
3090 sig_lo
&= 0xffffffff;
3092 if (r
->signalling
== fmt
->qnan_msb_set
)
3093 sig_hi
&= ~(1 << 30);
3096 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3099 /* Intel requires the explicit integer bit to be set, otherwise
3100 it considers the value a "pseudo-nan". Motorola docs say it
3102 sig_hi
|= 0x80000000;
3107 sig_lo
= sig_hi
= 0xffffffff;
3113 int exp
= REAL_EXP (r
);
3115 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3116 whereas the intermediate representation is 0.F x 2**exp.
3117 Which means we're off by one.
3119 Except for Motorola, which consider exp=0 and explicit
3120 integer bit set to continue to be normalized. In theory
3121 this discrepancy has been taken care of by the difference
3122 in fmt->emin in round_for_format. */
3129 gcc_assert (exp
>= 0);
3133 if (HOST_BITS_PER_LONG
== 32)
3135 sig_hi
= r
->sig
[SIGSZ
-1];
3136 sig_lo
= r
->sig
[SIGSZ
-2];
3140 sig_lo
= r
->sig
[SIGSZ
-1];
3141 sig_hi
= sig_lo
>> 31 >> 1;
3142 sig_lo
&= 0xffffffff;
3151 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3154 /* Convert from the internal format to the 12-byte Motorola format
3155 for an IEEE extended real. */
3157 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3158 const REAL_VALUE_TYPE
*r
)
3161 encode_ieee_extended (fmt
, intermed
, r
);
3163 /* Motorola chips are assumed always to be big-endian. Also, the
3164 padding in a Motorola extended real goes between the exponent and
3165 the mantissa. At this point the mantissa is entirely within
3166 elements 0 and 1 of intermed, and the exponent entirely within
3167 element 2, so all we have to do is swap the order around, and
3168 shift element 2 left 16 bits. */
3169 buf
[0] = intermed
[2] << 16;
3170 buf
[1] = intermed
[1];
3171 buf
[2] = intermed
[0];
3174 /* Convert from the internal format to the 12-byte Intel format for
3175 an IEEE extended real. */
3177 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3178 const REAL_VALUE_TYPE
*r
)
3180 if (FLOAT_WORDS_BIG_ENDIAN
)
3182 /* All the padding in an Intel-format extended real goes at the high
3183 end, which in this case is after the mantissa, not the exponent.
3184 Therefore we must shift everything down 16 bits. */
3186 encode_ieee_extended (fmt
, intermed
, r
);
3187 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3188 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3189 buf
[2] = (intermed
[0] << 16);
3192 /* encode_ieee_extended produces what we want directly. */
3193 encode_ieee_extended (fmt
, buf
, r
);
3196 /* Convert from the internal format to the 16-byte Intel format for
3197 an IEEE extended real. */
3199 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3200 const REAL_VALUE_TYPE
*r
)
3202 /* All the padding in an Intel-format extended real goes at the high end. */
3203 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3207 /* As above, we have a helper function which converts from 12-byte
3208 little-endian Intel format to internal format. Functions below
3209 adjust for the other possible formats. */
3211 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3214 unsigned long image_hi
, sig_hi
, sig_lo
;
3218 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3219 sig_lo
&= 0xffffffff;
3220 sig_hi
&= 0xffffffff;
3221 image_hi
&= 0xffffffff;
3223 sign
= (image_hi
>> 15) & 1;
3224 exp
= image_hi
& 0x7fff;
3226 memset (r
, 0, sizeof (*r
));
3230 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3235 /* When the IEEE format contains a hidden bit, we know that
3236 it's zero at this point, and so shift up the significand
3237 and decrease the exponent to match. In this case, Motorola
3238 defines the explicit integer bit to be valid, so we don't
3239 know whether the msb is set or not. */
3240 SET_REAL_EXP (r
, fmt
->emin
);
3241 if (HOST_BITS_PER_LONG
== 32)
3243 r
->sig
[SIGSZ
-1] = sig_hi
;
3244 r
->sig
[SIGSZ
-2] = sig_lo
;
3247 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3251 else if (fmt
->has_signed_zero
)
3254 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3256 /* See above re "pseudo-infinities" and "pseudo-nans".
3257 Short summary is that the MSB will likely always be
3258 set, and that we don't care about it. */
3259 sig_hi
&= 0x7fffffff;
3261 if (sig_hi
|| sig_lo
)
3265 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3266 if (HOST_BITS_PER_LONG
== 32)
3268 r
->sig
[SIGSZ
-1] = sig_hi
;
3269 r
->sig
[SIGSZ
-2] = sig_lo
;
3272 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3284 SET_REAL_EXP (r
, exp
- 16383 + 1);
3285 if (HOST_BITS_PER_LONG
== 32)
3287 r
->sig
[SIGSZ
-1] = sig_hi
;
3288 r
->sig
[SIGSZ
-2] = sig_lo
;
3291 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3295 /* Convert from the internal format to the 12-byte Motorola format
3296 for an IEEE extended real. */
3298 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3303 /* Motorola chips are assumed always to be big-endian. Also, the
3304 padding in a Motorola extended real goes between the exponent and
3305 the mantissa; remove it. */
3306 intermed
[0] = buf
[2];
3307 intermed
[1] = buf
[1];
3308 intermed
[2] = (unsigned long)buf
[0] >> 16;
3310 decode_ieee_extended (fmt
, r
, intermed
);
3313 /* Convert from the internal format to the 12-byte Intel format for
3314 an IEEE extended real. */
3316 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3319 if (FLOAT_WORDS_BIG_ENDIAN
)
3321 /* All the padding in an Intel-format extended real goes at the high
3322 end, which in this case is after the mantissa, not the exponent.
3323 Therefore we must shift everything up 16 bits. */
3326 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3327 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3328 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3330 decode_ieee_extended (fmt
, r
, intermed
);
3333 /* decode_ieee_extended produces what we want directly. */
3334 decode_ieee_extended (fmt
, r
, buf
);
3337 /* Convert from the internal format to the 16-byte Intel format for
3338 an IEEE extended real. */
3340 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3343 /* All the padding in an Intel-format extended real goes at the high end. */
3344 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3347 const struct real_format ieee_extended_motorola_format
=
3349 encode_ieee_extended_motorola
,
3350 decode_ieee_extended_motorola
,
3366 const struct real_format ieee_extended_intel_96_format
=
3368 encode_ieee_extended_intel_96
,
3369 decode_ieee_extended_intel_96
,
3385 const struct real_format ieee_extended_intel_128_format
=
3387 encode_ieee_extended_intel_128
,
3388 decode_ieee_extended_intel_128
,
3404 /* The following caters to i386 systems that set the rounding precision
3405 to 53 bits instead of 64, e.g. FreeBSD. */
3406 const struct real_format ieee_extended_intel_96_round_53_format
=
3408 encode_ieee_extended_intel_96
,
3409 decode_ieee_extended_intel_96
,
3425 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3426 numbers whose sum is equal to the extended precision value. The number
3427 with greater magnitude is first. This format has the same magnitude
3428 range as an IEEE double precision value, but effectively 106 bits of
3429 significand precision. Infinity and NaN are represented by their IEEE
3430 double precision value stored in the first number, the second number is
3431 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3433 static void encode_ibm_extended (const struct real_format
*fmt
,
3434 long *, const REAL_VALUE_TYPE
*);
3435 static void decode_ibm_extended (const struct real_format
*,
3436 REAL_VALUE_TYPE
*, const long *);
3439 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3440 const REAL_VALUE_TYPE
*r
)
3442 REAL_VALUE_TYPE u
, normr
, v
;
3443 const struct real_format
*base_fmt
;
3445 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3447 /* Renormlize R before doing any arithmetic on it. */
3449 if (normr
.cl
== rvc_normal
)
3452 /* u = IEEE double precision portion of significand. */
3454 round_for_format (base_fmt
, &u
);
3455 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3457 if (u
.cl
== rvc_normal
)
3459 do_add (&v
, &normr
, &u
, 1);
3460 /* Call round_for_format since we might need to denormalize. */
3461 round_for_format (base_fmt
, &v
);
3462 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3466 /* Inf, NaN, 0 are all representable as doubles, so the
3467 least-significant part can be 0.0. */
3474 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3477 REAL_VALUE_TYPE u
, v
;
3478 const struct real_format
*base_fmt
;
3480 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3481 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3483 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3485 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3486 do_add (r
, &u
, &v
, 0);
3492 const struct real_format ibm_extended_format
=
3494 encode_ibm_extended
,
3495 decode_ibm_extended
,
3511 const struct real_format mips_extended_format
=
3513 encode_ibm_extended
,
3514 decode_ibm_extended
,
3531 /* IEEE quad precision format. */
3533 static void encode_ieee_quad (const struct real_format
*fmt
,
3534 long *, const REAL_VALUE_TYPE
*);
3535 static void decode_ieee_quad (const struct real_format
*,
3536 REAL_VALUE_TYPE
*, const long *);
3539 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3540 const REAL_VALUE_TYPE
*r
)
3542 unsigned long image3
, image2
, image1
, image0
, exp
;
3543 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3546 image3
= r
->sign
<< 31;
3551 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3560 image3
|= 32767 << 16;
3563 image3
|= 0x7fffffff;
3564 image2
= 0xffffffff;
3565 image1
= 0xffffffff;
3566 image0
= 0xffffffff;
3573 image3
|= 32767 << 16;
3577 /* Don't use bits from the significand. The
3578 initialization above is right. */
3580 else if (HOST_BITS_PER_LONG
== 32)
3585 image3
|= u
.sig
[3] & 0xffff;
3590 image1
= image0
>> 31 >> 1;
3592 image3
|= (image2
>> 31 >> 1) & 0xffff;
3593 image0
&= 0xffffffff;
3594 image2
&= 0xffffffff;
3596 if (r
->signalling
== fmt
->qnan_msb_set
)
3600 /* We overload qnan_msb_set here: it's only clear for
3601 mips_ieee_single, which wants all mantissa bits but the
3602 quiet/signalling one set in canonical NaNs (at least
3604 if (r
->canonical
&& !fmt
->qnan_msb_set
)
3607 image2
= image1
= image0
= 0xffffffff;
3609 else if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3614 image3
|= 0x7fffffff;
3615 image2
= 0xffffffff;
3616 image1
= 0xffffffff;
3617 image0
= 0xffffffff;
3622 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3623 whereas the intermediate representation is 0.F x 2**exp.
3624 Which means we're off by one. */
3628 exp
= REAL_EXP (r
) + 16383 - 1;
3629 image3
|= exp
<< 16;
3631 if (HOST_BITS_PER_LONG
== 32)
3636 image3
|= u
.sig
[3] & 0xffff;
3641 image1
= image0
>> 31 >> 1;
3643 image3
|= (image2
>> 31 >> 1) & 0xffff;
3644 image0
&= 0xffffffff;
3645 image2
&= 0xffffffff;
3653 if (FLOAT_WORDS_BIG_ENDIAN
)
3670 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3673 unsigned long image3
, image2
, image1
, image0
;
3677 if (FLOAT_WORDS_BIG_ENDIAN
)
3691 image0
&= 0xffffffff;
3692 image1
&= 0xffffffff;
3693 image2
&= 0xffffffff;
3695 sign
= (image3
>> 31) & 1;
3696 exp
= (image3
>> 16) & 0x7fff;
3699 memset (r
, 0, sizeof (*r
));
3703 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
3708 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
3709 if (HOST_BITS_PER_LONG
== 32)
3718 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3719 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3724 else if (fmt
->has_signed_zero
)
3727 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3729 if (image3
| image2
| image1
| image0
)
3733 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
3735 if (HOST_BITS_PER_LONG
== 32)
3744 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3745 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3747 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3759 SET_REAL_EXP (r
, exp
- 16383 + 1);
3761 if (HOST_BITS_PER_LONG
== 32)
3770 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3771 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3773 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3774 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3778 const struct real_format ieee_quad_format
=
3797 const struct real_format mips_quad_format
=
3816 /* Descriptions of VAX floating point formats can be found beginning at
3818 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
3820 The thing to remember is that they're almost IEEE, except for word
3821 order, exponent bias, and the lack of infinities, nans, and denormals.
3823 We don't implement the H_floating format here, simply because neither
3824 the VAX or Alpha ports use it. */
3826 static void encode_vax_f (const struct real_format
*fmt
,
3827 long *, const REAL_VALUE_TYPE
*);
3828 static void decode_vax_f (const struct real_format
*,
3829 REAL_VALUE_TYPE
*, const long *);
3830 static void encode_vax_d (const struct real_format
*fmt
,
3831 long *, const REAL_VALUE_TYPE
*);
3832 static void decode_vax_d (const struct real_format
*,
3833 REAL_VALUE_TYPE
*, const long *);
3834 static void encode_vax_g (const struct real_format
*fmt
,
3835 long *, const REAL_VALUE_TYPE
*);
3836 static void decode_vax_g (const struct real_format
*,
3837 REAL_VALUE_TYPE
*, const long *);
3840 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
3841 const REAL_VALUE_TYPE
*r
)
3843 unsigned long sign
, exp
, sig
, image
;
3845 sign
= r
->sign
<< 15;
3855 image
= 0xffff7fff | sign
;
3859 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
3860 exp
= REAL_EXP (r
) + 128;
3862 image
= (sig
<< 16) & 0xffff0000;
3876 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
3877 REAL_VALUE_TYPE
*r
, const long *buf
)
3879 unsigned long image
= buf
[0] & 0xffffffff;
3880 int exp
= (image
>> 7) & 0xff;
3882 memset (r
, 0, sizeof (*r
));
3887 r
->sign
= (image
>> 15) & 1;
3888 SET_REAL_EXP (r
, exp
- 128);
3890 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
3891 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
3896 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
3897 const REAL_VALUE_TYPE
*r
)
3899 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3904 image0
= image1
= 0;
3909 image0
= 0xffff7fff | sign
;
3910 image1
= 0xffffffff;
3914 /* Extract the significand into straight hi:lo. */
3915 if (HOST_BITS_PER_LONG
== 64)
3917 image0
= r
->sig
[SIGSZ
-1];
3918 image1
= (image0
>> (64 - 56)) & 0xffffffff;
3919 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
3923 image0
= r
->sig
[SIGSZ
-1];
3924 image1
= r
->sig
[SIGSZ
-2];
3925 image1
= (image0
<< 24) | (image1
>> 8);
3926 image0
= (image0
>> 8) & 0xffffff;
3929 /* Rearrange the half-words of the significand to match the
3931 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
3932 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
3934 /* Add the sign and exponent. */
3936 image0
|= (REAL_EXP (r
) + 128) << 7;
3943 if (FLOAT_WORDS_BIG_ENDIAN
)
3944 buf
[0] = image1
, buf
[1] = image0
;
3946 buf
[0] = image0
, buf
[1] = image1
;
3950 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
3951 REAL_VALUE_TYPE
*r
, const long *buf
)
3953 unsigned long image0
, image1
;
3956 if (FLOAT_WORDS_BIG_ENDIAN
)
3957 image1
= buf
[0], image0
= buf
[1];
3959 image0
= buf
[0], image1
= buf
[1];
3960 image0
&= 0xffffffff;
3961 image1
&= 0xffffffff;
3963 exp
= (image0
>> 7) & 0xff;
3965 memset (r
, 0, sizeof (*r
));
3970 r
->sign
= (image0
>> 15) & 1;
3971 SET_REAL_EXP (r
, exp
- 128);
3973 /* Rearrange the half-words of the external format into
3974 proper ascending order. */
3975 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
3976 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
3978 if (HOST_BITS_PER_LONG
== 64)
3980 image0
= (image0
<< 31 << 1) | image1
;
3983 r
->sig
[SIGSZ
-1] = image0
;
3987 r
->sig
[SIGSZ
-1] = image0
;
3988 r
->sig
[SIGSZ
-2] = image1
;
3989 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
3990 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3996 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
3997 const REAL_VALUE_TYPE
*r
)
3999 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4004 image0
= image1
= 0;
4009 image0
= 0xffff7fff | sign
;
4010 image1
= 0xffffffff;
4014 /* Extract the significand into straight hi:lo. */
4015 if (HOST_BITS_PER_LONG
== 64)
4017 image0
= r
->sig
[SIGSZ
-1];
4018 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4019 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4023 image0
= r
->sig
[SIGSZ
-1];
4024 image1
= r
->sig
[SIGSZ
-2];
4025 image1
= (image0
<< 21) | (image1
>> 11);
4026 image0
= (image0
>> 11) & 0xfffff;
4029 /* Rearrange the half-words of the significand to match the
4031 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4032 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4034 /* Add the sign and exponent. */
4036 image0
|= (REAL_EXP (r
) + 1024) << 4;
4043 if (FLOAT_WORDS_BIG_ENDIAN
)
4044 buf
[0] = image1
, buf
[1] = image0
;
4046 buf
[0] = image0
, buf
[1] = image1
;
4050 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4051 REAL_VALUE_TYPE
*r
, const long *buf
)
4053 unsigned long image0
, image1
;
4056 if (FLOAT_WORDS_BIG_ENDIAN
)
4057 image1
= buf
[0], image0
= buf
[1];
4059 image0
= buf
[0], image1
= buf
[1];
4060 image0
&= 0xffffffff;
4061 image1
&= 0xffffffff;
4063 exp
= (image0
>> 4) & 0x7ff;
4065 memset (r
, 0, sizeof (*r
));
4070 r
->sign
= (image0
>> 15) & 1;
4071 SET_REAL_EXP (r
, exp
- 1024);
4073 /* Rearrange the half-words of the external format into
4074 proper ascending order. */
4075 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4076 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4078 if (HOST_BITS_PER_LONG
== 64)
4080 image0
= (image0
<< 31 << 1) | image1
;
4083 r
->sig
[SIGSZ
-1] = image0
;
4087 r
->sig
[SIGSZ
-1] = image0
;
4088 r
->sig
[SIGSZ
-2] = image1
;
4089 lshift_significand (r
, r
, 64 - 53);
4090 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4095 const struct real_format vax_f_format
=
4114 const struct real_format vax_d_format
=
4133 const struct real_format vax_g_format
=
4152 /* A good reference for these can be found in chapter 9 of
4153 "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
4154 An on-line version can be found here:
4156 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
4159 static void encode_i370_single (const struct real_format
*fmt
,
4160 long *, const REAL_VALUE_TYPE
*);
4161 static void decode_i370_single (const struct real_format
*,
4162 REAL_VALUE_TYPE
*, const long *);
4163 static void encode_i370_double (const struct real_format
*fmt
,
4164 long *, const REAL_VALUE_TYPE
*);
4165 static void decode_i370_double (const struct real_format
*,
4166 REAL_VALUE_TYPE
*, const long *);
4169 encode_i370_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4170 long *buf
, const REAL_VALUE_TYPE
*r
)
4172 unsigned long sign
, exp
, sig
, image
;
4174 sign
= r
->sign
<< 31;
4184 image
= 0x7fffffff | sign
;
4188 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0xffffff;
4189 exp
= ((REAL_EXP (r
) / 4) + 64) << 24;
4190 image
= sign
| exp
| sig
;
4201 decode_i370_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4202 REAL_VALUE_TYPE
*r
, const long *buf
)
4204 unsigned long sign
, sig
, image
= buf
[0];
4207 sign
= (image
>> 31) & 1;
4208 exp
= (image
>> 24) & 0x7f;
4209 sig
= image
& 0xffffff;
4211 memset (r
, 0, sizeof (*r
));
4217 SET_REAL_EXP (r
, (exp
- 64) * 4);
4218 r
->sig
[SIGSZ
-1] = sig
<< (HOST_BITS_PER_LONG
- 24);
4224 encode_i370_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4225 long *buf
, const REAL_VALUE_TYPE
*r
)
4227 unsigned long sign
, exp
, image_hi
, image_lo
;
4229 sign
= r
->sign
<< 31;
4234 image_hi
= image_lo
= 0;
4239 image_hi
= 0x7fffffff | sign
;
4240 image_lo
= 0xffffffff;
4244 if (HOST_BITS_PER_LONG
== 64)
4246 image_hi
= r
->sig
[SIGSZ
-1];
4247 image_lo
= (image_hi
>> (64 - 56)) & 0xffffffff;
4248 image_hi
= (image_hi
>> (64 - 56 + 1) >> 31) & 0xffffff;
4252 image_hi
= r
->sig
[SIGSZ
-1];
4253 image_lo
= r
->sig
[SIGSZ
-2];
4254 image_lo
= (image_lo
>> 8) | (image_hi
<< 24);
4258 exp
= ((REAL_EXP (r
) / 4) + 64) << 24;
4259 image_hi
|= sign
| exp
;
4266 if (FLOAT_WORDS_BIG_ENDIAN
)
4267 buf
[0] = image_hi
, buf
[1] = image_lo
;
4269 buf
[0] = image_lo
, buf
[1] = image_hi
;
4273 decode_i370_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4274 REAL_VALUE_TYPE
*r
, const long *buf
)
4276 unsigned long sign
, image_hi
, image_lo
;
4279 if (FLOAT_WORDS_BIG_ENDIAN
)
4280 image_hi
= buf
[0], image_lo
= buf
[1];
4282 image_lo
= buf
[0], image_hi
= buf
[1];
4284 sign
= (image_hi
>> 31) & 1;
4285 exp
= (image_hi
>> 24) & 0x7f;
4286 image_hi
&= 0xffffff;
4287 image_lo
&= 0xffffffff;
4289 memset (r
, 0, sizeof (*r
));
4291 if (exp
|| image_hi
|| image_lo
)
4295 SET_REAL_EXP (r
, (exp
- 64) * 4 + (SIGNIFICAND_BITS
- 56));
4297 if (HOST_BITS_PER_LONG
== 32)
4299 r
->sig
[0] = image_lo
;
4300 r
->sig
[1] = image_hi
;
4303 r
->sig
[0] = image_lo
| (image_hi
<< 31 << 1);
4309 const struct real_format i370_single_format
=
4323 false, /* ??? The encoding does allow for "unnormals". */
4324 false, /* ??? The encoding does allow for "unnormals". */
4328 const struct real_format i370_double_format
=
4342 false, /* ??? The encoding does allow for "unnormals". */
4343 false, /* ??? The encoding does allow for "unnormals". */
4347 /* Encode real R into a single precision DFP value in BUF. */
4349 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4350 long *buf ATTRIBUTE_UNUSED
,
4351 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4353 encode_decimal32 (fmt
, buf
, r
);
4356 /* Decode a single precision DFP value in BUF into a real R. */
4358 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4359 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4360 const long *buf ATTRIBUTE_UNUSED
)
4362 decode_decimal32 (fmt
, r
, buf
);
4365 /* Encode real R into a double precision DFP value in BUF. */
4367 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4368 long *buf ATTRIBUTE_UNUSED
,
4369 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4371 encode_decimal64 (fmt
, buf
, r
);
4374 /* Decode a double precision DFP value in BUF into a real R. */
4376 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4377 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4378 const long *buf ATTRIBUTE_UNUSED
)
4380 decode_decimal64 (fmt
, r
, buf
);
4383 /* Encode real R into a quad precision DFP value in BUF. */
4385 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4386 long *buf ATTRIBUTE_UNUSED
,
4387 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4389 encode_decimal128 (fmt
, buf
, r
);
4392 /* Decode a quad precision DFP value in BUF into a real R. */
4394 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4395 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4396 const long *buf ATTRIBUTE_UNUSED
)
4398 decode_decimal128 (fmt
, r
, buf
);
4401 /* Single precision decimal floating point (IEEE 754R). */
4402 const struct real_format decimal_single_format
=
4404 encode_decimal_single
,
4405 decode_decimal_single
,
4421 /* Double precision decimal floating point (IEEE 754R). */
4422 const struct real_format decimal_double_format
=
4424 encode_decimal_double
,
4425 decode_decimal_double
,
4441 /* Quad precision decimal floating point (IEEE 754R). */
4442 const struct real_format decimal_quad_format
=
4444 encode_decimal_quad
,
4445 decode_decimal_quad
,
4461 /* The "twos-complement" c4x format is officially defined as
4465 This is rather misleading. One must remember that F is signed.
4466 A better description would be
4468 x = -1**s * ((s + 1 + .f) * 2**e
4470 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4471 that's -1 * (1+1+(-.5)) == -1.5. I think.
4473 The constructions here are taken from Tables 5-1 and 5-2 of the
4474 TMS320C4x User's Guide wherein step-by-step instructions for
4475 conversion from IEEE are presented. That's close enough to our
4476 internal representation so as to make things easy.
4478 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
4480 static void encode_c4x_single (const struct real_format
*fmt
,
4481 long *, const REAL_VALUE_TYPE
*);
4482 static void decode_c4x_single (const struct real_format
*,
4483 REAL_VALUE_TYPE
*, const long *);
4484 static void encode_c4x_extended (const struct real_format
*fmt
,
4485 long *, const REAL_VALUE_TYPE
*);
4486 static void decode_c4x_extended (const struct real_format
*,
4487 REAL_VALUE_TYPE
*, const long *);
4490 encode_c4x_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4491 long *buf
, const REAL_VALUE_TYPE
*r
)
4493 unsigned long image
, exp
, sig
;
4505 sig
= 0x800000 - r
->sign
;
4509 exp
= REAL_EXP (r
) - 1;
4510 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4525 image
= ((exp
& 0xff) << 24) | (sig
& 0xffffff);
4530 decode_c4x_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4531 REAL_VALUE_TYPE
*r
, const long *buf
)
4533 unsigned long image
= buf
[0];
4537 exp
= (((image
>> 24) & 0xff) ^ 0x80) - 0x80;
4538 sf
= ((image
& 0xffffff) ^ 0x800000) - 0x800000;
4540 memset (r
, 0, sizeof (*r
));
4546 sig
= sf
& 0x7fffff;
4555 sig
= (sig
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4557 SET_REAL_EXP (r
, exp
+ 1);
4558 r
->sig
[SIGSZ
-1] = sig
;
4563 encode_c4x_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4564 long *buf
, const REAL_VALUE_TYPE
*r
)
4566 unsigned long exp
, sig
;
4578 sig
= 0x80000000 - r
->sign
;
4582 exp
= REAL_EXP (r
) - 1;
4584 sig
= r
->sig
[SIGSZ
-1];
4585 if (HOST_BITS_PER_LONG
== 64)
4586 sig
= sig
>> 1 >> 31;
4603 exp
= (exp
& 0xff) << 24;
4606 if (FLOAT_WORDS_BIG_ENDIAN
)
4607 buf
[0] = exp
, buf
[1] = sig
;
4609 buf
[0] = sig
, buf
[0] = exp
;
4613 decode_c4x_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4614 REAL_VALUE_TYPE
*r
, const long *buf
)
4619 if (FLOAT_WORDS_BIG_ENDIAN
)
4620 exp
= buf
[0], sf
= buf
[1];
4622 sf
= buf
[0], exp
= buf
[1];
4624 exp
= (((exp
>> 24) & 0xff) & 0x80) - 0x80;
4625 sf
= ((sf
& 0xffffffff) ^ 0x80000000) - 0x80000000;
4627 memset (r
, 0, sizeof (*r
));
4633 sig
= sf
& 0x7fffffff;
4642 if (HOST_BITS_PER_LONG
== 64)
4643 sig
= sig
<< 1 << 31;
4646 SET_REAL_EXP (r
, exp
+ 1);
4647 r
->sig
[SIGSZ
-1] = sig
;
4651 const struct real_format c4x_single_format
=
4670 const struct real_format c4x_extended_format
=
4672 encode_c4x_extended
,
4673 decode_c4x_extended
,
4690 /* A synthetic "format" for internal arithmetic. It's the size of the
4691 internal significand minus the two bits needed for proper rounding.
4692 The encode and decode routines exist only to satisfy our paranoia
4695 static void encode_internal (const struct real_format
*fmt
,
4696 long *, const REAL_VALUE_TYPE
*);
4697 static void decode_internal (const struct real_format
*,
4698 REAL_VALUE_TYPE
*, const long *);
4701 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4702 const REAL_VALUE_TYPE
*r
)
4704 memcpy (buf
, r
, sizeof (*r
));
4708 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4709 REAL_VALUE_TYPE
*r
, const long *buf
)
4711 memcpy (r
, buf
, sizeof (*r
));
4714 const struct real_format real_internal_format
=
4720 SIGNIFICAND_BITS
- 2,
4721 SIGNIFICAND_BITS
- 2,
4733 /* Calculate the square root of X in mode MODE, and store the result
4734 in R. Return TRUE if the operation does not raise an exception.
4735 For details see "High Precision Division and Square Root",
4736 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4737 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4740 real_sqrt (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4741 const REAL_VALUE_TYPE
*x
)
4743 static REAL_VALUE_TYPE halfthree
;
4744 static bool init
= false;
4745 REAL_VALUE_TYPE h
, t
, i
;
4748 /* sqrt(-0.0) is -0.0. */
4749 if (real_isnegzero (x
))
4755 /* Negative arguments return NaN. */
4758 get_canonical_qnan (r
, 0);
4762 /* Infinity and NaN return themselves. */
4763 if (real_isinf (x
) || real_isnan (x
))
4771 do_add (&halfthree
, &dconst1
, &dconsthalf
, 0);
4775 /* Initial guess for reciprocal sqrt, i. */
4776 exp
= real_exponent (x
);
4777 real_ldexp (&i
, &dconst1
, -exp
/2);
4779 /* Newton's iteration for reciprocal sqrt, i. */
4780 for (iter
= 0; iter
< 16; iter
++)
4782 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4783 do_multiply (&t
, x
, &i
);
4784 do_multiply (&h
, &t
, &i
);
4785 do_multiply (&t
, &h
, &dconsthalf
);
4786 do_add (&h
, &halfthree
, &t
, 1);
4787 do_multiply (&t
, &i
, &h
);
4789 /* Check for early convergence. */
4790 if (iter
>= 6 && real_identical (&i
, &t
))
4793 /* ??? Unroll loop to avoid copying. */
4797 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4798 do_multiply (&t
, x
, &i
);
4799 do_multiply (&h
, &t
, &i
);
4800 do_add (&i
, &dconst1
, &h
, 1);
4801 do_multiply (&h
, &t
, &i
);
4802 do_multiply (&i
, &dconsthalf
, &h
);
4803 do_add (&h
, &t
, &i
, 0);
4805 /* ??? We need a Tuckerman test to get the last bit. */
4807 real_convert (r
, mode
, &h
);
4811 /* Calculate X raised to the integer exponent N in mode MODE and store
4812 the result in R. Return true if the result may be inexact due to
4813 loss of precision. The algorithm is the classic "left-to-right binary
4814 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4815 Algorithms", "The Art of Computer Programming", Volume 2. */
4818 real_powi (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4819 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4821 unsigned HOST_WIDE_INT bit
;
4823 bool inexact
= false;
4835 /* Don't worry about overflow, from now on n is unsigned. */
4843 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4844 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4848 inexact
|= do_multiply (&t
, &t
, &t
);
4850 inexact
|= do_multiply (&t
, &t
, x
);
4858 inexact
|= do_divide (&t
, &dconst1
, &t
);
4860 real_convert (r
, mode
, &t
);
4864 /* Round X to the nearest integer not larger in absolute value, i.e.
4865 towards zero, placing the result in R in mode MODE. */
4868 real_trunc (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4869 const REAL_VALUE_TYPE
*x
)
4871 do_fix_trunc (r
, x
);
4872 if (mode
!= VOIDmode
)
4873 real_convert (r
, mode
, r
);
4876 /* Round X to the largest integer not greater in value, i.e. round
4877 down, placing the result in R in mode MODE. */
4880 real_floor (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4881 const REAL_VALUE_TYPE
*x
)
4885 do_fix_trunc (&t
, x
);
4886 if (! real_identical (&t
, x
) && x
->sign
)
4887 do_add (&t
, &t
, &dconstm1
, 0);
4888 if (mode
!= VOIDmode
)
4889 real_convert (r
, mode
, &t
);
4894 /* Round X to the smallest integer not less then argument, i.e. round
4895 up, placing the result in R in mode MODE. */
4898 real_ceil (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4899 const REAL_VALUE_TYPE
*x
)
4903 do_fix_trunc (&t
, x
);
4904 if (! real_identical (&t
, x
) && ! x
->sign
)
4905 do_add (&t
, &t
, &dconst1
, 0);
4906 if (mode
!= VOIDmode
)
4907 real_convert (r
, mode
, &t
);
4912 /* Round X to the nearest integer, but round halfway cases away from
4916 real_round (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4917 const REAL_VALUE_TYPE
*x
)
4919 do_add (r
, x
, &dconsthalf
, x
->sign
);
4920 do_fix_trunc (r
, r
);
4921 if (mode
!= VOIDmode
)
4922 real_convert (r
, mode
, r
);
4925 /* Set the sign of R to the sign of X. */
4928 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)