1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2002, 2003, 2004, 2005 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 2, 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 COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
26 #include "coretypes.h"
34 /* The floating point model used internally is not exactly IEEE 754
35 compliant, and close to the description in the ISO C99 standard,
36 section 5.2.4.2.2 Characteristics of floating types.
40 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
44 b = base or radix, here always 2
46 p = precision (the number of base-b digits in the significand)
47 f_k = the digits of the significand.
49 We differ from typical IEEE 754 encodings in that the entire
50 significand is fractional. Normalized significands are in the
53 A requirement of the model is that P be larger than the largest
54 supported target floating-point type by at least 2 bits. This gives
55 us proper rounding when we truncate to the target type. In addition,
56 E must be large enough to hold the smallest supported denormal number
59 Both of these requirements are easily satisfied. The largest target
60 significand is 113 bits; we store at least 160. The smallest
61 denormal number fits in 17 exponent bits; we store 27.
63 Note that the decimal string conversion routines are sensitive to
64 rounding errors. Since the raw arithmetic routines do not themselves
65 have guard digits or rounding, the computation of 10**exp can
66 accumulate more than a few digits of error. The previous incarnation
67 of real.c successfully used a 144-bit fraction; given the current
68 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits.
70 Target floating point models that use base 16 instead of base 2
71 (i.e. IBM 370), are handled during round_for_format, in which we
72 canonicalize the exponent to be a multiple of 4 (log2(16)), and
73 adjust the significand to match. */
76 /* Used to classify two numbers simultaneously. */
77 #define CLASS2(A, B) ((A) << 2 | (B))
79 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
80 #error "Some constant folding done by hand to avoid shift count warnings"
83 static void get_zero (REAL_VALUE_TYPE
*, int);
84 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
85 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
86 static void get_inf (REAL_VALUE_TYPE
*, int);
87 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
88 const REAL_VALUE_TYPE
*, unsigned int);
89 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
91 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
93 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
94 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
95 const REAL_VALUE_TYPE
*);
96 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
97 const REAL_VALUE_TYPE
*, int);
98 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
99 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
100 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
101 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
102 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
103 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
104 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
105 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
106 const REAL_VALUE_TYPE
*);
107 static void normalize (REAL_VALUE_TYPE
*);
109 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
110 const REAL_VALUE_TYPE
*, int);
111 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
112 const REAL_VALUE_TYPE
*);
113 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
114 const REAL_VALUE_TYPE
*);
115 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
116 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
118 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, REAL_VALUE_TYPE
*);
120 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
121 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
122 static const REAL_VALUE_TYPE
* real_digit (int);
123 static void times_pten (REAL_VALUE_TYPE
*, int);
125 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
127 /* Initialize R with a positive zero. */
130 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
132 memset (r
, 0, sizeof (*r
));
136 /* Initialize R with the canonical quiet NaN. */
139 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
141 memset (r
, 0, sizeof (*r
));
148 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
150 memset (r
, 0, sizeof (*r
));
158 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
160 memset (r
, 0, sizeof (*r
));
166 /* Right-shift the significand of A by N bits; put the result in the
167 significand of R. If any one bits are shifted out, return true. */
170 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
173 unsigned long sticky
= 0;
174 unsigned int i
, ofs
= 0;
176 if (n
>= HOST_BITS_PER_LONG
)
178 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
180 n
&= HOST_BITS_PER_LONG
- 1;
185 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
186 for (i
= 0; i
< SIGSZ
; ++i
)
189 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
190 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
191 << (HOST_BITS_PER_LONG
- n
)));
196 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
197 r
->sig
[i
] = a
->sig
[ofs
+ i
];
198 for (; i
< SIGSZ
; ++i
)
205 /* Right-shift the significand of A by N bits; put the result in the
209 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
212 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
214 n
&= HOST_BITS_PER_LONG
- 1;
217 for (i
= 0; i
< SIGSZ
; ++i
)
220 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
221 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
222 << (HOST_BITS_PER_LONG
- n
)));
227 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
228 r
->sig
[i
] = a
->sig
[ofs
+ i
];
229 for (; i
< SIGSZ
; ++i
)
234 /* Left-shift the significand of A by N bits; put the result in the
238 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
241 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
243 n
&= HOST_BITS_PER_LONG
- 1;
246 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
247 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
248 for (; i
< SIGSZ
; ++i
)
249 r
->sig
[SIGSZ
-1-i
] = 0;
252 for (i
= 0; i
< SIGSZ
; ++i
)
255 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
256 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
257 >> (HOST_BITS_PER_LONG
- n
)));
261 /* Likewise, but N is specialized to 1. */
264 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
268 for (i
= SIGSZ
- 1; i
> 0; --i
)
269 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
270 r
->sig
[0] = a
->sig
[0] << 1;
273 /* Add the significands of A and B, placing the result in R. Return
274 true if there was carry out of the most significant word. */
277 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
278 const REAL_VALUE_TYPE
*b
)
283 for (i
= 0; i
< SIGSZ
; ++i
)
285 unsigned long ai
= a
->sig
[i
];
286 unsigned long ri
= ai
+ b
->sig
[i
];
302 /* Subtract the significands of A and B, placing the result in R. CARRY is
303 true if there's a borrow incoming to the least significant word.
304 Return true if there was borrow out of the most significant word. */
307 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
308 const REAL_VALUE_TYPE
*b
, int carry
)
312 for (i
= 0; i
< SIGSZ
; ++i
)
314 unsigned long ai
= a
->sig
[i
];
315 unsigned long ri
= ai
- b
->sig
[i
];
331 /* Negate the significand A, placing the result in R. */
334 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
339 for (i
= 0; i
< SIGSZ
; ++i
)
341 unsigned long ri
, ai
= a
->sig
[i
];
360 /* Compare significands. Return tri-state vs zero. */
363 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
367 for (i
= SIGSZ
- 1; i
>= 0; --i
)
369 unsigned long ai
= a
->sig
[i
];
370 unsigned long bi
= b
->sig
[i
];
381 /* Return true if A is nonzero. */
384 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
388 for (i
= SIGSZ
- 1; i
>= 0; --i
)
395 /* Set bit N of the significand of R. */
398 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
400 r
->sig
[n
/ HOST_BITS_PER_LONG
]
401 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
404 /* Clear bit N of the significand of R. */
407 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
409 r
->sig
[n
/ HOST_BITS_PER_LONG
]
410 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
413 /* Test bit N of the significand of R. */
416 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
418 /* ??? Compiler bug here if we return this expression directly.
419 The conversion to bool strips the "&1" and we wind up testing
420 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
421 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
425 /* Clear bits 0..N-1 of the significand of R. */
428 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
430 int i
, w
= n
/ HOST_BITS_PER_LONG
;
432 for (i
= 0; i
< w
; ++i
)
435 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
438 /* Divide the significands of A and B, placing the result in R. Return
439 true if the division was inexact. */
442 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
443 const REAL_VALUE_TYPE
*b
)
446 int i
, bit
= SIGNIFICAND_BITS
- 1;
447 unsigned long msb
, inexact
;
450 memset (r
->sig
, 0, sizeof (r
->sig
));
456 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
457 lshift_significand_1 (&u
, &u
);
459 if (msb
|| cmp_significands (&u
, b
) >= 0)
461 sub_significands (&u
, &u
, b
, 0);
462 set_significand_bit (r
, bit
);
467 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
473 /* Adjust the exponent and significand of R such that the most
474 significant bit is set. We underflow to zero and overflow to
475 infinity here, without denormals. (The intermediate representation
476 exponent is large enough to handle target denormals normalized.) */
479 normalize (REAL_VALUE_TYPE
*r
)
487 /* Find the first word that is nonzero. */
488 for (i
= SIGSZ
- 1; i
>= 0; i
--)
490 shift
+= HOST_BITS_PER_LONG
;
494 /* Zero significand flushes to zero. */
502 /* Find the first bit that is nonzero. */
504 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
510 exp
= REAL_EXP (r
) - shift
;
512 get_inf (r
, r
->sign
);
513 else if (exp
< -MAX_EXP
)
514 get_zero (r
, r
->sign
);
517 SET_REAL_EXP (r
, exp
);
518 lshift_significand (r
, r
, shift
);
523 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
524 result may be inexact due to a loss of precision. */
527 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
528 const REAL_VALUE_TYPE
*b
, int subtract_p
)
532 bool inexact
= false;
534 /* Determine if we need to add or subtract. */
536 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
538 switch (CLASS2 (a
->cl
, b
->cl
))
540 case CLASS2 (rvc_zero
, rvc_zero
):
541 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
542 get_zero (r
, sign
& !subtract_p
);
545 case CLASS2 (rvc_zero
, rvc_normal
):
546 case CLASS2 (rvc_zero
, rvc_inf
):
547 case CLASS2 (rvc_zero
, rvc_nan
):
549 case CLASS2 (rvc_normal
, rvc_nan
):
550 case CLASS2 (rvc_inf
, rvc_nan
):
551 case CLASS2 (rvc_nan
, rvc_nan
):
552 /* ANY + NaN = NaN. */
553 case CLASS2 (rvc_normal
, rvc_inf
):
556 r
->sign
= sign
^ subtract_p
;
559 case CLASS2 (rvc_normal
, rvc_zero
):
560 case CLASS2 (rvc_inf
, rvc_zero
):
561 case CLASS2 (rvc_nan
, rvc_zero
):
563 case CLASS2 (rvc_nan
, rvc_normal
):
564 case CLASS2 (rvc_nan
, rvc_inf
):
565 /* NaN + ANY = NaN. */
566 case CLASS2 (rvc_inf
, rvc_normal
):
571 case CLASS2 (rvc_inf
, rvc_inf
):
573 /* Inf - Inf = NaN. */
574 get_canonical_qnan (r
, 0);
576 /* Inf + Inf = Inf. */
580 case CLASS2 (rvc_normal
, rvc_normal
):
587 /* Swap the arguments such that A has the larger exponent. */
588 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
591 const REAL_VALUE_TYPE
*t
;
598 /* If the exponents are not identical, we need to shift the
599 significand of B down. */
602 /* If the exponents are too far apart, the significands
603 do not overlap, which makes the subtraction a noop. */
604 if (dexp
>= SIGNIFICAND_BITS
)
611 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
617 if (sub_significands (r
, a
, b
, inexact
))
619 /* We got a borrow out of the subtraction. That means that
620 A and B had the same exponent, and B had the larger
621 significand. We need to swap the sign and negate the
624 neg_significand (r
, r
);
629 if (add_significands (r
, a
, b
))
631 /* We got carry out of the addition. This means we need to
632 shift the significand back down one bit and increase the
634 inexact
|= sticky_rshift_significand (r
, r
, 1);
635 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
646 SET_REAL_EXP (r
, exp
);
647 /* Zero out the remaining fields. */
652 /* Re-normalize the result. */
655 /* Special case: if the subtraction results in zero, the result
657 if (r
->cl
== rvc_zero
)
660 r
->sig
[0] |= inexact
;
665 /* Calculate R = A * B. Return true if the result may be inexact. */
668 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
669 const REAL_VALUE_TYPE
*b
)
671 REAL_VALUE_TYPE u
, t
, *rr
;
672 unsigned int i
, j
, k
;
673 int sign
= a
->sign
^ b
->sign
;
674 bool inexact
= false;
676 switch (CLASS2 (a
->cl
, b
->cl
))
678 case CLASS2 (rvc_zero
, rvc_zero
):
679 case CLASS2 (rvc_zero
, rvc_normal
):
680 case CLASS2 (rvc_normal
, rvc_zero
):
681 /* +-0 * ANY = 0 with appropriate sign. */
685 case CLASS2 (rvc_zero
, rvc_nan
):
686 case CLASS2 (rvc_normal
, rvc_nan
):
687 case CLASS2 (rvc_inf
, rvc_nan
):
688 case CLASS2 (rvc_nan
, rvc_nan
):
689 /* ANY * NaN = NaN. */
694 case CLASS2 (rvc_nan
, rvc_zero
):
695 case CLASS2 (rvc_nan
, rvc_normal
):
696 case CLASS2 (rvc_nan
, rvc_inf
):
697 /* NaN * ANY = NaN. */
702 case CLASS2 (rvc_zero
, rvc_inf
):
703 case CLASS2 (rvc_inf
, rvc_zero
):
705 get_canonical_qnan (r
, sign
);
708 case CLASS2 (rvc_inf
, rvc_inf
):
709 case CLASS2 (rvc_normal
, rvc_inf
):
710 case CLASS2 (rvc_inf
, rvc_normal
):
711 /* Inf * Inf = Inf, R * Inf = Inf */
715 case CLASS2 (rvc_normal
, rvc_normal
):
722 if (r
== a
|| r
== b
)
728 /* Collect all the partial products. Since we don't have sure access
729 to a widening multiply, we split each long into two half-words.
731 Consider the long-hand form of a four half-word multiplication:
741 We construct partial products of the widened half-word products
742 that are known to not overlap, e.g. DF+DH. Each such partial
743 product is given its proper exponent, which allows us to sum them
744 and obtain the finished product. */
746 for (i
= 0; i
< SIGSZ
* 2; ++i
)
748 unsigned long ai
= a
->sig
[i
/ 2];
750 ai
>>= HOST_BITS_PER_LONG
/ 2;
752 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
757 for (j
= 0; j
< 2; ++j
)
759 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
760 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
769 /* Would underflow to zero, which we shouldn't bother adding. */
774 memset (&u
, 0, sizeof (u
));
776 SET_REAL_EXP (&u
, exp
);
778 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
780 unsigned long bi
= b
->sig
[k
/ 2];
782 bi
>>= HOST_BITS_PER_LONG
/ 2;
784 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
786 u
.sig
[k
/ 2] = ai
* bi
;
790 inexact
|= do_add (rr
, rr
, &u
, 0);
801 /* Calculate R = A / B. Return true if the result may be inexact. */
804 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
805 const REAL_VALUE_TYPE
*b
)
807 int exp
, sign
= a
->sign
^ b
->sign
;
808 REAL_VALUE_TYPE t
, *rr
;
811 switch (CLASS2 (a
->cl
, b
->cl
))
813 case CLASS2 (rvc_zero
, rvc_zero
):
815 case CLASS2 (rvc_inf
, rvc_inf
):
816 /* Inf / Inf = NaN. */
817 get_canonical_qnan (r
, sign
);
820 case CLASS2 (rvc_zero
, rvc_normal
):
821 case CLASS2 (rvc_zero
, rvc_inf
):
823 case CLASS2 (rvc_normal
, rvc_inf
):
828 case CLASS2 (rvc_normal
, rvc_zero
):
830 case CLASS2 (rvc_inf
, rvc_zero
):
835 case CLASS2 (rvc_zero
, rvc_nan
):
836 case CLASS2 (rvc_normal
, rvc_nan
):
837 case CLASS2 (rvc_inf
, rvc_nan
):
838 case CLASS2 (rvc_nan
, rvc_nan
):
839 /* ANY / NaN = NaN. */
844 case CLASS2 (rvc_nan
, rvc_zero
):
845 case CLASS2 (rvc_nan
, rvc_normal
):
846 case CLASS2 (rvc_nan
, rvc_inf
):
847 /* NaN / ANY = NaN. */
852 case CLASS2 (rvc_inf
, rvc_normal
):
857 case CLASS2 (rvc_normal
, rvc_normal
):
864 if (r
== a
|| r
== b
)
869 /* Make sure all fields in the result are initialized. */
874 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
885 SET_REAL_EXP (rr
, exp
);
887 inexact
= div_significands (rr
, a
, b
);
889 /* Re-normalize the result. */
891 rr
->sig
[0] |= inexact
;
899 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
900 one of the two operands is a NaN. */
903 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
908 switch (CLASS2 (a
->cl
, b
->cl
))
910 case CLASS2 (rvc_zero
, rvc_zero
):
911 /* Sign of zero doesn't matter for compares. */
914 case CLASS2 (rvc_inf
, rvc_zero
):
915 case CLASS2 (rvc_inf
, rvc_normal
):
916 case CLASS2 (rvc_normal
, rvc_zero
):
917 return (a
->sign
? -1 : 1);
919 case CLASS2 (rvc_inf
, rvc_inf
):
920 return -a
->sign
- -b
->sign
;
922 case CLASS2 (rvc_zero
, rvc_normal
):
923 case CLASS2 (rvc_zero
, rvc_inf
):
924 case CLASS2 (rvc_normal
, rvc_inf
):
925 return (b
->sign
? 1 : -1);
927 case CLASS2 (rvc_zero
, rvc_nan
):
928 case CLASS2 (rvc_normal
, rvc_nan
):
929 case CLASS2 (rvc_inf
, rvc_nan
):
930 case CLASS2 (rvc_nan
, rvc_nan
):
931 case CLASS2 (rvc_nan
, rvc_zero
):
932 case CLASS2 (rvc_nan
, rvc_normal
):
933 case CLASS2 (rvc_nan
, rvc_inf
):
936 case CLASS2 (rvc_normal
, rvc_normal
):
943 if (a
->sign
!= b
->sign
)
944 return -a
->sign
- -b
->sign
;
946 if (a
->decimal
|| b
->decimal
)
947 return decimal_do_compare (a
, b
, nan_result
);
949 if (REAL_EXP (a
) > REAL_EXP (b
))
951 else if (REAL_EXP (a
) < REAL_EXP (b
))
954 ret
= cmp_significands (a
, b
);
956 return (a
->sign
? -ret
: ret
);
959 /* Return A truncated to an integral value toward zero. */
962 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
976 decimal_do_fix_trunc (r
, a
);
979 if (REAL_EXP (r
) <= 0)
980 get_zero (r
, r
->sign
);
981 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
982 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
990 /* Perform the binary or unary operation described by CODE.
991 For a unary operation, leave OP1 NULL. This function returns
992 true if the result may be inexact due to loss of precision. */
995 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
996 const REAL_VALUE_TYPE
*op1
)
998 enum tree_code code
= icode
;
1000 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1001 return decimal_real_arithmetic (r
, icode
, op0
, op1
);
1006 return do_add (r
, op0
, op1
, 0);
1009 return do_add (r
, op0
, op1
, 1);
1012 return do_multiply (r
, op0
, op1
);
1015 return do_divide (r
, op0
, op1
);
1018 if (op1
->cl
== rvc_nan
)
1020 else if (do_compare (op0
, op1
, -1) < 0)
1027 if (op1
->cl
== rvc_nan
)
1029 else if (do_compare (op0
, op1
, 1) < 0)
1045 case FIX_TRUNC_EXPR
:
1046 do_fix_trunc (r
, op0
);
1055 /* Legacy. Similar, but return the result directly. */
1058 real_arithmetic2 (int icode
, const REAL_VALUE_TYPE
*op0
,
1059 const REAL_VALUE_TYPE
*op1
)
1062 real_arithmetic (&r
, icode
, op0
, op1
);
1067 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1068 const REAL_VALUE_TYPE
*op1
)
1070 enum tree_code code
= icode
;
1075 return do_compare (op0
, op1
, 1) < 0;
1077 return do_compare (op0
, op1
, 1) <= 0;
1079 return do_compare (op0
, op1
, -1) > 0;
1081 return do_compare (op0
, op1
, -1) >= 0;
1083 return do_compare (op0
, op1
, -1) == 0;
1085 return do_compare (op0
, op1
, -1) != 0;
1086 case UNORDERED_EXPR
:
1087 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1089 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1091 return do_compare (op0
, op1
, -1) < 0;
1093 return do_compare (op0
, op1
, -1) <= 0;
1095 return do_compare (op0
, op1
, 1) > 0;
1097 return do_compare (op0
, op1
, 1) >= 0;
1099 return do_compare (op0
, op1
, 0) == 0;
1101 return do_compare (op0
, op1
, 0) != 0;
1108 /* Return floor log2(R). */
1111 real_exponent (const REAL_VALUE_TYPE
*r
)
1119 return (unsigned int)-1 >> 1;
1121 return REAL_EXP (r
);
1127 /* R = OP0 * 2**EXP. */
1130 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1141 exp
+= REAL_EXP (op0
);
1143 get_inf (r
, r
->sign
);
1144 else if (exp
< -MAX_EXP
)
1145 get_zero (r
, r
->sign
);
1147 SET_REAL_EXP (r
, exp
);
1155 /* Determine whether a floating-point value X is infinite. */
1158 real_isinf (const REAL_VALUE_TYPE
*r
)
1160 return (r
->cl
== rvc_inf
);
1163 /* Determine whether a floating-point value X is a NaN. */
1166 real_isnan (const REAL_VALUE_TYPE
*r
)
1168 return (r
->cl
== rvc_nan
);
1171 /* Determine whether a floating-point value X is negative. */
1174 real_isneg (const REAL_VALUE_TYPE
*r
)
1179 /* Determine whether a floating-point value X is minus zero. */
1182 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1184 return r
->sign
&& r
->cl
== rvc_zero
;
1187 /* Compare two floating-point objects for bitwise identity. */
1190 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1196 if (a
->sign
!= b
->sign
)
1206 if (a
->decimal
!= b
->decimal
)
1208 if (REAL_EXP (a
) != REAL_EXP (b
))
1213 if (a
->signalling
!= b
->signalling
)
1215 /* The significand is ignored for canonical NaNs. */
1216 if (a
->canonical
|| b
->canonical
)
1217 return a
->canonical
== b
->canonical
;
1224 for (i
= 0; i
< SIGSZ
; ++i
)
1225 if (a
->sig
[i
] != b
->sig
[i
])
1231 /* Try to change R into its exact multiplicative inverse in machine
1232 mode MODE. Return true if successful. */
1235 exact_real_inverse (enum machine_mode mode
, REAL_VALUE_TYPE
*r
)
1237 const REAL_VALUE_TYPE
*one
= real_digit (1);
1241 if (r
->cl
!= rvc_normal
)
1244 /* Check for a power of two: all significand bits zero except the MSB. */
1245 for (i
= 0; i
< SIGSZ
-1; ++i
)
1248 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1251 /* Find the inverse and truncate to the required mode. */
1252 do_divide (&u
, one
, r
);
1253 real_convert (&u
, mode
, &u
);
1255 /* The rounding may have overflowed. */
1256 if (u
.cl
!= rvc_normal
)
1258 for (i
= 0; i
< SIGSZ
-1; ++i
)
1261 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1268 /* Render R as an integer. */
1271 real_to_integer (const REAL_VALUE_TYPE
*r
)
1273 unsigned HOST_WIDE_INT i
;
1284 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1291 return decimal_real_to_integer (r
);
1293 if (REAL_EXP (r
) <= 0)
1295 /* Only force overflow for unsigned overflow. Signed overflow is
1296 undefined, so it doesn't matter what we return, and some callers
1297 expect to be able to use this routine for both signed and
1298 unsigned conversions. */
1299 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1302 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1303 i
= r
->sig
[SIGSZ
-1];
1306 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1307 i
= r
->sig
[SIGSZ
-1];
1308 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1309 i
|= r
->sig
[SIGSZ
-2];
1312 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1323 /* Likewise, but to an integer pair, HI+LOW. */
1326 real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
1327 const REAL_VALUE_TYPE
*r
)
1330 HOST_WIDE_INT low
, high
;
1343 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1356 decimal_real_to_integer2 (plow
, phigh
, r
);
1363 /* Only force overflow for unsigned overflow. Signed overflow is
1364 undefined, so it doesn't matter what we return, and some callers
1365 expect to be able to use this routine for both signed and
1366 unsigned conversions. */
1367 if (exp
> 2*HOST_BITS_PER_WIDE_INT
)
1370 rshift_significand (&t
, r
, 2*HOST_BITS_PER_WIDE_INT
- exp
);
1371 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1373 high
= t
.sig
[SIGSZ
-1];
1374 low
= t
.sig
[SIGSZ
-2];
1378 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
);
1379 high
= t
.sig
[SIGSZ
-1];
1380 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1381 high
|= t
.sig
[SIGSZ
-2];
1383 low
= t
.sig
[SIGSZ
-3];
1384 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1385 low
|= t
.sig
[SIGSZ
-4];
1393 low
= -low
, high
= ~high
;
1405 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1406 of NUM / DEN. Return the quotient and place the remainder in NUM.
1407 It is expected that NUM / DEN are close enough that the quotient is
1410 static unsigned long
1411 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1413 unsigned long q
, msb
;
1414 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1423 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1425 lshift_significand_1 (num
, num
);
1427 if (msb
|| cmp_significands (num
, den
) >= 0)
1429 sub_significands (num
, num
, den
, 0);
1433 while (--expn
>= expd
);
1435 SET_REAL_EXP (num
, expd
);
1441 /* Render R as a decimal floating point constant. Emit DIGITS significant
1442 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1443 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1446 #define M_LOG10_2 0.30102999566398119521
1449 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1450 size_t digits
, int crop_trailing_zeros
)
1452 const REAL_VALUE_TYPE
*one
, *ten
;
1453 REAL_VALUE_TYPE r
, pten
, u
, v
;
1454 int dec_exp
, cmp_one
, digit
;
1456 char *p
, *first
, *last
;
1463 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1468 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1471 /* ??? Print the significand as well, if not canonical? */
1472 strcpy (str
, (r
.sign
? "-NaN" : "+NaN"));
1480 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1484 /* Bound the number of digits printed by the size of the representation. */
1485 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1486 if (digits
== 0 || digits
> max_digits
)
1487 digits
= max_digits
;
1489 /* Estimate the decimal exponent, and compute the length of the string it
1490 will print as. Be conservative and add one to account for possible
1491 overflow or rounding error. */
1492 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1493 for (max_digits
= 1; dec_exp
; max_digits
++)
1496 /* Bound the number of digits printed by the size of the output buffer. */
1497 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1498 gcc_assert (max_digits
<= buf_size
);
1499 if (digits
> max_digits
)
1500 digits
= max_digits
;
1502 one
= real_digit (1);
1503 ten
= ten_to_ptwo (0);
1511 cmp_one
= do_compare (&r
, one
, 0);
1516 /* Number is greater than one. Convert significand to an integer
1517 and strip trailing decimal zeros. */
1520 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1522 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1523 m
= floor_log2 (max_digits
);
1525 /* Iterate over the bits of the possible powers of 10 that might
1526 be present in U and eliminate them. That is, if we find that
1527 10**2**M divides U evenly, keep the division and increase
1533 do_divide (&t
, &u
, ten_to_ptwo (m
));
1534 do_fix_trunc (&v
, &t
);
1535 if (cmp_significands (&v
, &t
) == 0)
1543 /* Revert the scaling to integer that we performed earlier. */
1544 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1545 - (SIGNIFICAND_BITS
- 1));
1548 /* Find power of 10. Do this by dividing out 10**2**M when
1549 this is larger than the current remainder. Fill PTEN with
1550 the power of 10 that we compute. */
1551 if (REAL_EXP (&r
) > 0)
1553 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1556 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1557 if (do_compare (&u
, ptentwo
, 0) >= 0)
1559 do_divide (&u
, &u
, ptentwo
);
1560 do_multiply (&pten
, &pten
, ptentwo
);
1567 /* We managed to divide off enough tens in the above reduction
1568 loop that we've now got a negative exponent. Fall into the
1569 less-than-one code to compute the proper value for PTEN. */
1576 /* Number is less than one. Pad significand with leading
1582 /* Stop if we'd shift bits off the bottom. */
1586 do_multiply (&u
, &v
, ten
);
1588 /* Stop if we're now >= 1. */
1589 if (REAL_EXP (&u
) > 0)
1597 /* Find power of 10. Do this by multiplying in P=10**2**M when
1598 the current remainder is smaller than 1/P. Fill PTEN with the
1599 power of 10 that we compute. */
1600 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1603 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1604 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1606 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1608 do_multiply (&v
, &v
, ptentwo
);
1609 do_multiply (&pten
, &pten
, ptentwo
);
1615 /* Invert the positive power of 10 that we've collected so far. */
1616 do_divide (&pten
, one
, &pten
);
1624 /* At this point, PTEN should contain the nearest power of 10 smaller
1625 than R, such that this division produces the first digit.
1627 Using a divide-step primitive that returns the complete integral
1628 remainder avoids the rounding error that would be produced if
1629 we were to use do_divide here and then simply multiply by 10 for
1630 each subsequent digit. */
1632 digit
= rtd_divmod (&r
, &pten
);
1634 /* Be prepared for error in that division via underflow ... */
1635 if (digit
== 0 && cmp_significand_0 (&r
))
1637 /* Multiply by 10 and try again. */
1638 do_multiply (&r
, &r
, ten
);
1639 digit
= rtd_divmod (&r
, &pten
);
1641 gcc_assert (digit
!= 0);
1644 /* ... or overflow. */
1654 gcc_assert (digit
<= 10);
1658 /* Generate subsequent digits. */
1659 while (--digits
> 0)
1661 do_multiply (&r
, &r
, ten
);
1662 digit
= rtd_divmod (&r
, &pten
);
1667 /* Generate one more digit with which to do rounding. */
1668 do_multiply (&r
, &r
, ten
);
1669 digit
= rtd_divmod (&r
, &pten
);
1671 /* Round the result. */
1674 /* Round to nearest. If R is nonzero there are additional
1675 nonzero digits to be extracted. */
1676 if (cmp_significand_0 (&r
))
1678 /* Round to even. */
1679 else if ((p
[-1] - '0') & 1)
1696 /* Carry out of the first digit. This means we had all 9's and
1697 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1705 /* Insert the decimal point. */
1706 first
[0] = first
[1];
1709 /* If requested, drop trailing zeros. Never crop past "1.0". */
1710 if (crop_trailing_zeros
)
1711 while (last
> first
+ 3 && last
[-1] == '0')
1714 /* Append the exponent. */
1715 sprintf (last
, "e%+d", dec_exp
);
1718 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1719 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1720 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1721 strip trailing zeros. */
1724 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1725 size_t digits
, int crop_trailing_zeros
)
1727 int i
, j
, exp
= REAL_EXP (r
);
1740 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1743 /* ??? Print the significand as well, if not canonical? */
1744 strcpy (str
, (r
->sign
? "-NaN" : "+NaN"));
1752 /* Hexadecimal format for decimal floats is not interesting. */
1753 strcpy (str
, "N/A");
1758 digits
= SIGNIFICAND_BITS
/ 4;
1760 /* Bound the number of digits printed by the size of the output buffer. */
1762 sprintf (exp_buf
, "p%+d", exp
);
1763 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1764 gcc_assert (max_digits
<= buf_size
);
1765 if (digits
> max_digits
)
1766 digits
= max_digits
;
1777 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1778 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1780 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1786 if (crop_trailing_zeros
)
1787 while (p
> first
+ 1 && p
[-1] == '0')
1790 sprintf (p
, "p%+d", exp
);
1793 /* Initialize R from a decimal or hexadecimal string. The string is
1794 assumed to have been syntax checked already. */
1797 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1809 else if (*str
== '+')
1812 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1814 /* Hexadecimal floating point. */
1815 int pos
= SIGNIFICAND_BITS
- 4, d
;
1823 d
= hex_value (*str
);
1828 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1829 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1833 /* Ensure correct rounding by setting last bit if there is
1834 a subsequent nonzero digit. */
1842 if (pos
== SIGNIFICAND_BITS
- 4)
1849 d
= hex_value (*str
);
1854 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1855 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1859 /* Ensure correct rounding by setting last bit if there is
1860 a subsequent nonzero digit. */
1866 /* If the mantissa is zero, ignore the exponent. */
1867 if (!cmp_significand_0 (r
))
1870 if (*str
== 'p' || *str
== 'P')
1872 bool exp_neg
= false;
1880 else if (*str
== '+')
1884 while (ISDIGIT (*str
))
1890 /* Overflowed the exponent. */
1905 SET_REAL_EXP (r
, exp
);
1911 /* Decimal floating point. */
1912 const REAL_VALUE_TYPE
*ten
= ten_to_ptwo (0);
1917 while (ISDIGIT (*str
))
1920 do_multiply (r
, r
, ten
);
1922 do_add (r
, r
, real_digit (d
), 0);
1927 if (r
->cl
== rvc_zero
)
1932 while (ISDIGIT (*str
))
1935 do_multiply (r
, r
, ten
);
1937 do_add (r
, r
, real_digit (d
), 0);
1942 /* If the mantissa is zero, ignore the exponent. */
1943 if (r
->cl
== rvc_zero
)
1946 if (*str
== 'e' || *str
== 'E')
1948 bool exp_neg
= false;
1956 else if (*str
== '+')
1960 while (ISDIGIT (*str
))
1966 /* Overflowed the exponent. */
1980 times_pten (r
, exp
);
1995 /* Legacy. Similar, but return the result directly. */
1998 real_from_string2 (const char *s
, enum machine_mode mode
)
2002 real_from_string (&r
, s
);
2003 if (mode
!= VOIDmode
)
2004 real_convert (&r
, mode
, &r
);
2009 /* Initialize R from string S and desired MODE. */
2012 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, enum machine_mode mode
)
2014 if (DECIMAL_FLOAT_MODE_P (mode
))
2015 decimal_real_from_string (r
, s
);
2017 real_from_string (r
, s
);
2019 if (mode
!= VOIDmode
)
2020 real_convert (r
, mode
, r
);
2023 /* Initialize R from the integer pair HIGH+LOW. */
2026 real_from_integer (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2027 unsigned HOST_WIDE_INT low
, HOST_WIDE_INT high
,
2030 if (low
== 0 && high
== 0)
2034 memset (r
, 0, sizeof (*r
));
2036 r
->sign
= high
< 0 && !unsigned_p
;
2037 SET_REAL_EXP (r
, 2 * HOST_BITS_PER_WIDE_INT
);
2048 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2050 r
->sig
[SIGSZ
-1] = high
;
2051 r
->sig
[SIGSZ
-2] = low
;
2055 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2056 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2057 r
->sig
[SIGSZ
-2] = high
;
2058 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2059 r
->sig
[SIGSZ
-4] = low
;
2065 if (mode
!= VOIDmode
)
2066 real_convert (r
, mode
, r
);
2069 /* Returns 10**2**N. */
2071 static const REAL_VALUE_TYPE
*
2074 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2076 gcc_assert (n
>= 0);
2077 gcc_assert (n
< EXP_BITS
);
2079 if (tens
[n
].cl
== rvc_zero
)
2081 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2083 HOST_WIDE_INT t
= 10;
2086 for (i
= 0; i
< n
; ++i
)
2089 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2093 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2094 do_multiply (&tens
[n
], t
, t
);
2101 /* Returns 10**(-2**N). */
2103 static const REAL_VALUE_TYPE
*
2104 ten_to_mptwo (int n
)
2106 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2108 gcc_assert (n
>= 0);
2109 gcc_assert (n
< EXP_BITS
);
2111 if (tens
[n
].cl
== rvc_zero
)
2112 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2119 static const REAL_VALUE_TYPE
*
2122 static REAL_VALUE_TYPE num
[10];
2124 gcc_assert (n
>= 0);
2125 gcc_assert (n
<= 9);
2127 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2128 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2133 /* Multiply R by 10**EXP. */
2136 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2138 REAL_VALUE_TYPE pten
, *rr
;
2139 bool negative
= (exp
< 0);
2145 pten
= *real_digit (1);
2151 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2153 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2156 do_divide (r
, r
, &pten
);
2159 /* Fills R with +Inf. */
2162 real_inf (REAL_VALUE_TYPE
*r
)
2167 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2168 we force a QNaN, else we force an SNaN. The string, if not empty,
2169 is parsed as a number and placed in the significand. Return true
2170 if the string was successfully parsed. */
2173 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2174 enum machine_mode mode
)
2176 const struct real_format
*fmt
;
2178 fmt
= REAL_MODE_FORMAT (mode
);
2184 get_canonical_qnan (r
, 0);
2186 get_canonical_snan (r
, 0);
2192 memset (r
, 0, sizeof (*r
));
2195 /* Parse akin to strtol into the significand of R. */
2197 while (ISSPACE (*str
))
2201 else if (*str
== '+')
2206 if (*str
== 'x' || *str
== 'X')
2215 while ((d
= hex_value (*str
)) < base
)
2222 lshift_significand (r
, r
, 3);
2225 lshift_significand (r
, r
, 4);
2228 lshift_significand_1 (&u
, r
);
2229 lshift_significand (r
, r
, 3);
2230 add_significands (r
, r
, &u
);
2238 add_significands (r
, r
, &u
);
2243 /* Must have consumed the entire string for success. */
2247 /* Shift the significand into place such that the bits
2248 are in the most significant bits for the format. */
2249 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2251 /* Our MSB is always unset for NaNs. */
2252 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2254 /* Force quiet or signalling NaN. */
2255 r
->signalling
= !quiet
;
2261 /* Fills R with the largest finite value representable in mode MODE.
2262 If SIGN is nonzero, R is set to the most negative finite value. */
2265 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
2267 const struct real_format
*fmt
;
2270 fmt
= REAL_MODE_FORMAT (mode
);
2272 memset (r
, 0, sizeof (*r
));
2275 decimal_real_maxval (r
, sign
, mode
);
2280 SET_REAL_EXP (r
, fmt
->emax
* fmt
->log2_b
);
2282 np2
= SIGNIFICAND_BITS
- fmt
->p
* fmt
->log2_b
;
2283 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2284 clear_significand_below (r
, np2
);
2286 if (fmt
->pnan
< fmt
->p
)
2287 /* This is an IBM extended double format made up of two IEEE
2288 doubles. The value of the long double is the sum of the
2289 values of the two parts. The most significant part is
2290 required to be the value of the long double rounded to the
2291 nearest double. Rounding means we need a slightly smaller
2292 value for LDBL_MAX. */
2293 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2297 /* Fills R with 2**N. */
2300 real_2expN (REAL_VALUE_TYPE
*r
, int n
)
2302 memset (r
, 0, sizeof (*r
));
2307 else if (n
< -MAX_EXP
)
2312 SET_REAL_EXP (r
, n
);
2313 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2319 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2322 unsigned long sticky
;
2330 decimal_round_for_format (fmt
, r
);
2333 /* FIXME. We can come here via fp_easy_constant
2334 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2335 investigated whether this convert needs to be here, or
2336 something else is missing. */
2337 decimal_real_convert (r
, DFmode
, r
);
2340 p2
= fmt
->p
* fmt
->log2_b
;
2341 emin2m1
= (fmt
->emin
- 1) * fmt
->log2_b
;
2342 emax2
= fmt
->emax
* fmt
->log2_b
;
2344 np2
= SIGNIFICAND_BITS
- p2
;
2348 get_zero (r
, r
->sign
);
2350 if (!fmt
->has_signed_zero
)
2355 get_inf (r
, r
->sign
);
2360 clear_significand_below (r
, np2
);
2370 /* If we're not base2, normalize the exponent to a multiple of
2372 if (fmt
->log2_b
!= 1)
2376 gcc_assert (fmt
->b
!= 10);
2377 shift
= REAL_EXP (r
) & (fmt
->log2_b
- 1);
2380 shift
= fmt
->log2_b
- shift
;
2381 r
->sig
[0] |= sticky_rshift_significand (r
, r
, shift
);
2382 SET_REAL_EXP (r
, REAL_EXP (r
) + shift
);
2386 /* Check the range of the exponent. If we're out of range,
2387 either underflow or overflow. */
2388 if (REAL_EXP (r
) > emax2
)
2390 else if (REAL_EXP (r
) <= emin2m1
)
2394 if (!fmt
->has_denorm
)
2396 /* Don't underflow completely until we've had a chance to round. */
2397 if (REAL_EXP (r
) < emin2m1
)
2402 diff
= emin2m1
- REAL_EXP (r
) + 1;
2406 /* De-normalize the significand. */
2407 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2408 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2412 /* There are P2 true significand bits, followed by one guard bit,
2413 followed by one sticky bit, followed by stuff. Fold nonzero
2414 stuff into the sticky bit. */
2417 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2418 sticky
|= r
->sig
[i
];
2420 r
->sig
[w
] & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2422 guard
= test_significand_bit (r
, np2
- 1);
2423 lsb
= test_significand_bit (r
, np2
);
2425 /* Round to even. */
2426 if (guard
&& (sticky
|| lsb
))
2430 set_significand_bit (&u
, np2
);
2432 if (add_significands (r
, r
, &u
))
2434 /* Overflow. Means the significand had been all ones, and
2435 is now all zeros. Need to increase the exponent, and
2436 possibly re-normalize it. */
2437 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2438 if (REAL_EXP (r
) > emax2
)
2440 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2442 if (fmt
->log2_b
!= 1)
2444 int shift
= REAL_EXP (r
) & (fmt
->log2_b
- 1);
2447 shift
= fmt
->log2_b
- shift
;
2448 rshift_significand (r
, r
, shift
);
2449 SET_REAL_EXP (r
, REAL_EXP (r
) + shift
);
2450 if (REAL_EXP (r
) > emax2
)
2457 /* Catch underflow that we deferred until after rounding. */
2458 if (REAL_EXP (r
) <= emin2m1
)
2461 /* Clear out trailing garbage. */
2462 clear_significand_below (r
, np2
);
2465 /* Extend or truncate to a new mode. */
2468 real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2469 const REAL_VALUE_TYPE
*a
)
2471 const struct real_format
*fmt
;
2473 fmt
= REAL_MODE_FORMAT (mode
);
2478 if (a
->decimal
|| fmt
->b
== 10)
2479 decimal_real_convert (r
, mode
, a
);
2481 round_for_format (fmt
, r
);
2483 /* round_for_format de-normalizes denormals. Undo just that part. */
2484 if (r
->cl
== rvc_normal
)
2488 /* Legacy. Likewise, except return the struct directly. */
2491 real_value_truncate (enum machine_mode mode
, REAL_VALUE_TYPE a
)
2494 real_convert (&r
, mode
, &a
);
2498 /* Return true if truncating to MODE is exact. */
2501 exact_real_truncate (enum machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2503 const struct real_format
*fmt
;
2507 fmt
= REAL_MODE_FORMAT (mode
);
2510 /* Don't allow conversion to denormals. */
2511 emin2m1
= (fmt
->emin
- 1) * fmt
->log2_b
;
2512 if (REAL_EXP (a
) <= emin2m1
)
2515 /* After conversion to the new mode, the value must be identical. */
2516 real_convert (&t
, mode
, a
);
2517 return real_identical (&t
, a
);
2520 /* Write R to the given target format. Place the words of the result
2521 in target word order in BUF. There are always 32 bits in each
2522 long, no matter the size of the host long.
2524 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2527 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2528 const struct real_format
*fmt
)
2534 round_for_format (fmt
, &r
);
2538 (*fmt
->encode
) (fmt
, buf
, &r
);
2543 /* Similar, but look up the format from MODE. */
2546 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, enum machine_mode mode
)
2548 const struct real_format
*fmt
;
2550 fmt
= REAL_MODE_FORMAT (mode
);
2553 return real_to_target_fmt (buf
, r
, fmt
);
2556 /* Read R from the given target format. Read the words of the result
2557 in target word order in BUF. There are always 32 bits in each
2558 long, no matter the size of the host long. */
2561 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2562 const struct real_format
*fmt
)
2564 (*fmt
->decode
) (fmt
, r
, buf
);
2567 /* Similar, but look up the format from MODE. */
2570 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, enum machine_mode mode
)
2572 const struct real_format
*fmt
;
2574 fmt
= REAL_MODE_FORMAT (mode
);
2577 (*fmt
->decode
) (fmt
, r
, buf
);
2580 /* Return the number of bits of the largest binary value that the
2581 significand of MODE will hold. */
2582 /* ??? Legacy. Should get access to real_format directly. */
2585 significand_size (enum machine_mode mode
)
2587 const struct real_format
*fmt
;
2589 fmt
= REAL_MODE_FORMAT (mode
);
2595 /* Return the size in bits of the largest binary value that can be
2596 held by the decimal coefficient for this mode. This is one more
2597 than the number of bits required to hold the largest coefficient
2599 double log2_10
= 3.3219281;
2600 return fmt
->p
* log2_10
;
2602 return fmt
->p
* fmt
->log2_b
;
2605 /* Return a hash value for the given real value. */
2606 /* ??? The "unsigned int" return value is intended to be hashval_t,
2607 but I didn't want to pull hashtab.h into real.h. */
2610 real_hash (const REAL_VALUE_TYPE
*r
)
2615 h
= r
->cl
| (r
->sign
<< 2);
2623 h
|= REAL_EXP (r
) << 3;
2628 h
^= (unsigned int)-1;
2637 if (sizeof(unsigned long) > sizeof(unsigned int))
2638 for (i
= 0; i
< SIGSZ
; ++i
)
2640 unsigned long s
= r
->sig
[i
];
2641 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2644 for (i
= 0; i
< SIGSZ
; ++i
)
2650 /* IEEE single-precision format. */
2652 static void encode_ieee_single (const struct real_format
*fmt
,
2653 long *, const REAL_VALUE_TYPE
*);
2654 static void decode_ieee_single (const struct real_format
*,
2655 REAL_VALUE_TYPE
*, const long *);
2658 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2659 const REAL_VALUE_TYPE
*r
)
2661 unsigned long image
, sig
, exp
;
2662 unsigned long sign
= r
->sign
;
2663 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2666 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2677 image
|= 0x7fffffff;
2684 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2685 if (r
->signalling
== fmt
->qnan_msb_set
)
2696 image
|= 0x7fffffff;
2700 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2701 whereas the intermediate representation is 0.F x 2**exp.
2702 Which means we're off by one. */
2706 exp
= REAL_EXP (r
) + 127 - 1;
2719 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2722 unsigned long image
= buf
[0] & 0xffffffff;
2723 bool sign
= (image
>> 31) & 1;
2724 int exp
= (image
>> 23) & 0xff;
2726 memset (r
, 0, sizeof (*r
));
2727 image
<<= HOST_BITS_PER_LONG
- 24;
2732 if (image
&& fmt
->has_denorm
)
2736 SET_REAL_EXP (r
, -126);
2737 r
->sig
[SIGSZ
-1] = image
<< 1;
2740 else if (fmt
->has_signed_zero
)
2743 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2749 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2750 ^ fmt
->qnan_msb_set
);
2751 r
->sig
[SIGSZ
-1] = image
;
2763 SET_REAL_EXP (r
, exp
- 127 + 1);
2764 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2768 const struct real_format ieee_single_format
=
2788 const struct real_format mips_single_format
=
2808 const struct real_format coldfire_single_format
=
2828 /* IEEE double-precision format. */
2830 static void encode_ieee_double (const struct real_format
*fmt
,
2831 long *, const REAL_VALUE_TYPE
*);
2832 static void decode_ieee_double (const struct real_format
*,
2833 REAL_VALUE_TYPE
*, const long *);
2836 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
2837 const REAL_VALUE_TYPE
*r
)
2839 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
2840 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2842 image_hi
= r
->sign
<< 31;
2845 if (HOST_BITS_PER_LONG
== 64)
2847 sig_hi
= r
->sig
[SIGSZ
-1];
2848 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
2849 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
2853 sig_hi
= r
->sig
[SIGSZ
-1];
2854 sig_lo
= r
->sig
[SIGSZ
-2];
2855 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
2856 sig_hi
= (sig_hi
>> 11) & 0xfffff;
2866 image_hi
|= 2047 << 20;
2869 image_hi
|= 0x7fffffff;
2870 image_lo
= 0xffffffff;
2879 if (fmt
->canonical_nan_lsbs_set
)
2881 sig_hi
= (1 << 19) - 1;
2882 sig_lo
= 0xffffffff;
2890 if (r
->signalling
== fmt
->qnan_msb_set
)
2891 sig_hi
&= ~(1 << 19);
2894 if (sig_hi
== 0 && sig_lo
== 0)
2897 image_hi
|= 2047 << 20;
2903 image_hi
|= 0x7fffffff;
2904 image_lo
= 0xffffffff;
2909 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2910 whereas the intermediate representation is 0.F x 2**exp.
2911 Which means we're off by one. */
2915 exp
= REAL_EXP (r
) + 1023 - 1;
2916 image_hi
|= exp
<< 20;
2925 if (FLOAT_WORDS_BIG_ENDIAN
)
2926 buf
[0] = image_hi
, buf
[1] = image_lo
;
2928 buf
[0] = image_lo
, buf
[1] = image_hi
;
2932 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2935 unsigned long image_hi
, image_lo
;
2939 if (FLOAT_WORDS_BIG_ENDIAN
)
2940 image_hi
= buf
[0], image_lo
= buf
[1];
2942 image_lo
= buf
[0], image_hi
= buf
[1];
2943 image_lo
&= 0xffffffff;
2944 image_hi
&= 0xffffffff;
2946 sign
= (image_hi
>> 31) & 1;
2947 exp
= (image_hi
>> 20) & 0x7ff;
2949 memset (r
, 0, sizeof (*r
));
2951 image_hi
<<= 32 - 21;
2952 image_hi
|= image_lo
>> 21;
2953 image_hi
&= 0x7fffffff;
2954 image_lo
<<= 32 - 21;
2958 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
2962 SET_REAL_EXP (r
, -1022);
2963 if (HOST_BITS_PER_LONG
== 32)
2965 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
2967 r
->sig
[SIGSZ
-1] = image_hi
;
2968 r
->sig
[SIGSZ
-2] = image_lo
;
2972 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
2973 r
->sig
[SIGSZ
-1] = image_hi
;
2977 else if (fmt
->has_signed_zero
)
2980 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
2982 if (image_hi
|| image_lo
)
2986 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
2987 if (HOST_BITS_PER_LONG
== 32)
2989 r
->sig
[SIGSZ
-1] = image_hi
;
2990 r
->sig
[SIGSZ
-2] = image_lo
;
2993 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3005 SET_REAL_EXP (r
, exp
- 1023 + 1);
3006 if (HOST_BITS_PER_LONG
== 32)
3008 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3009 r
->sig
[SIGSZ
-2] = image_lo
;
3012 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3016 const struct real_format ieee_double_format
=
3036 const struct real_format mips_double_format
=
3056 const struct real_format coldfire_double_format
=
3076 /* IEEE extended real format. This comes in three flavors: Intel's as
3077 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3078 12- and 16-byte images may be big- or little endian; Motorola's is
3079 always big endian. */
3081 /* Helper subroutine which converts from the internal format to the
3082 12-byte little-endian Intel format. Functions below adjust this
3083 for the other possible formats. */
3085 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3086 const REAL_VALUE_TYPE
*r
)
3088 unsigned long image_hi
, sig_hi
, sig_lo
;
3089 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3091 image_hi
= r
->sign
<< 15;
3092 sig_hi
= sig_lo
= 0;
3104 /* Intel requires the explicit integer bit to be set, otherwise
3105 it considers the value a "pseudo-infinity". Motorola docs
3106 say it doesn't care. */
3107 sig_hi
= 0x80000000;
3112 sig_lo
= sig_hi
= 0xffffffff;
3120 if (HOST_BITS_PER_LONG
== 32)
3122 sig_hi
= r
->sig
[SIGSZ
-1];
3123 sig_lo
= r
->sig
[SIGSZ
-2];
3127 sig_lo
= r
->sig
[SIGSZ
-1];
3128 sig_hi
= sig_lo
>> 31 >> 1;
3129 sig_lo
&= 0xffffffff;
3131 if (r
->signalling
== fmt
->qnan_msb_set
)
3132 sig_hi
&= ~(1 << 30);
3135 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3138 /* Intel requires the explicit integer bit to be set, otherwise
3139 it considers the value a "pseudo-nan". Motorola docs say it
3141 sig_hi
|= 0x80000000;
3146 sig_lo
= sig_hi
= 0xffffffff;
3152 int exp
= REAL_EXP (r
);
3154 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3155 whereas the intermediate representation is 0.F x 2**exp.
3156 Which means we're off by one.
3158 Except for Motorola, which consider exp=0 and explicit
3159 integer bit set to continue to be normalized. In theory
3160 this discrepancy has been taken care of by the difference
3161 in fmt->emin in round_for_format. */
3168 gcc_assert (exp
>= 0);
3172 if (HOST_BITS_PER_LONG
== 32)
3174 sig_hi
= r
->sig
[SIGSZ
-1];
3175 sig_lo
= r
->sig
[SIGSZ
-2];
3179 sig_lo
= r
->sig
[SIGSZ
-1];
3180 sig_hi
= sig_lo
>> 31 >> 1;
3181 sig_lo
&= 0xffffffff;
3190 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3193 /* Convert from the internal format to the 12-byte Motorola format
3194 for an IEEE extended real. */
3196 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3197 const REAL_VALUE_TYPE
*r
)
3200 encode_ieee_extended (fmt
, intermed
, r
);
3202 /* Motorola chips are assumed always to be big-endian. Also, the
3203 padding in a Motorola extended real goes between the exponent and
3204 the mantissa. At this point the mantissa is entirely within
3205 elements 0 and 1 of intermed, and the exponent entirely within
3206 element 2, so all we have to do is swap the order around, and
3207 shift element 2 left 16 bits. */
3208 buf
[0] = intermed
[2] << 16;
3209 buf
[1] = intermed
[1];
3210 buf
[2] = intermed
[0];
3213 /* Convert from the internal format to the 12-byte Intel format for
3214 an IEEE extended real. */
3216 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3217 const REAL_VALUE_TYPE
*r
)
3219 if (FLOAT_WORDS_BIG_ENDIAN
)
3221 /* All the padding in an Intel-format extended real goes at the high
3222 end, which in this case is after the mantissa, not the exponent.
3223 Therefore we must shift everything down 16 bits. */
3225 encode_ieee_extended (fmt
, intermed
, r
);
3226 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3227 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3228 buf
[2] = (intermed
[0] << 16);
3231 /* encode_ieee_extended produces what we want directly. */
3232 encode_ieee_extended (fmt
, buf
, r
);
3235 /* Convert from the internal format to the 16-byte Intel format for
3236 an IEEE extended real. */
3238 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3239 const REAL_VALUE_TYPE
*r
)
3241 /* All the padding in an Intel-format extended real goes at the high end. */
3242 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3246 /* As above, we have a helper function which converts from 12-byte
3247 little-endian Intel format to internal format. Functions below
3248 adjust for the other possible formats. */
3250 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3253 unsigned long image_hi
, sig_hi
, sig_lo
;
3257 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3258 sig_lo
&= 0xffffffff;
3259 sig_hi
&= 0xffffffff;
3260 image_hi
&= 0xffffffff;
3262 sign
= (image_hi
>> 15) & 1;
3263 exp
= image_hi
& 0x7fff;
3265 memset (r
, 0, sizeof (*r
));
3269 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3274 /* When the IEEE format contains a hidden bit, we know that
3275 it's zero at this point, and so shift up the significand
3276 and decrease the exponent to match. In this case, Motorola
3277 defines the explicit integer bit to be valid, so we don't
3278 know whether the msb is set or not. */
3279 SET_REAL_EXP (r
, fmt
->emin
);
3280 if (HOST_BITS_PER_LONG
== 32)
3282 r
->sig
[SIGSZ
-1] = sig_hi
;
3283 r
->sig
[SIGSZ
-2] = sig_lo
;
3286 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3290 else if (fmt
->has_signed_zero
)
3293 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3295 /* See above re "pseudo-infinities" and "pseudo-nans".
3296 Short summary is that the MSB will likely always be
3297 set, and that we don't care about it. */
3298 sig_hi
&= 0x7fffffff;
3300 if (sig_hi
|| sig_lo
)
3304 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3305 if (HOST_BITS_PER_LONG
== 32)
3307 r
->sig
[SIGSZ
-1] = sig_hi
;
3308 r
->sig
[SIGSZ
-2] = sig_lo
;
3311 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3323 SET_REAL_EXP (r
, exp
- 16383 + 1);
3324 if (HOST_BITS_PER_LONG
== 32)
3326 r
->sig
[SIGSZ
-1] = sig_hi
;
3327 r
->sig
[SIGSZ
-2] = sig_lo
;
3330 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3334 /* Convert from the internal format to the 12-byte Motorola format
3335 for an IEEE extended real. */
3337 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3342 /* Motorola chips are assumed always to be big-endian. Also, the
3343 padding in a Motorola extended real goes between the exponent and
3344 the mantissa; remove it. */
3345 intermed
[0] = buf
[2];
3346 intermed
[1] = buf
[1];
3347 intermed
[2] = (unsigned long)buf
[0] >> 16;
3349 decode_ieee_extended (fmt
, r
, intermed
);
3352 /* Convert from the internal format to the 12-byte Intel format for
3353 an IEEE extended real. */
3355 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3358 if (FLOAT_WORDS_BIG_ENDIAN
)
3360 /* All the padding in an Intel-format extended real goes at the high
3361 end, which in this case is after the mantissa, not the exponent.
3362 Therefore we must shift everything up 16 bits. */
3365 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3366 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3367 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3369 decode_ieee_extended (fmt
, r
, intermed
);
3372 /* decode_ieee_extended produces what we want directly. */
3373 decode_ieee_extended (fmt
, r
, buf
);
3376 /* Convert from the internal format to the 16-byte Intel format for
3377 an IEEE extended real. */
3379 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3382 /* All the padding in an Intel-format extended real goes at the high end. */
3383 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3386 const struct real_format ieee_extended_motorola_format
=
3388 encode_ieee_extended_motorola
,
3389 decode_ieee_extended_motorola
,
3406 const struct real_format ieee_extended_intel_96_format
=
3408 encode_ieee_extended_intel_96
,
3409 decode_ieee_extended_intel_96
,
3426 const struct real_format ieee_extended_intel_128_format
=
3428 encode_ieee_extended_intel_128
,
3429 decode_ieee_extended_intel_128
,
3446 /* The following caters to i386 systems that set the rounding precision
3447 to 53 bits instead of 64, e.g. FreeBSD. */
3448 const struct real_format ieee_extended_intel_96_round_53_format
=
3450 encode_ieee_extended_intel_96
,
3451 decode_ieee_extended_intel_96
,
3468 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3469 numbers whose sum is equal to the extended precision value. The number
3470 with greater magnitude is first. This format has the same magnitude
3471 range as an IEEE double precision value, but effectively 106 bits of
3472 significand precision. Infinity and NaN are represented by their IEEE
3473 double precision value stored in the first number, the second number is
3474 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3476 static void encode_ibm_extended (const struct real_format
*fmt
,
3477 long *, const REAL_VALUE_TYPE
*);
3478 static void decode_ibm_extended (const struct real_format
*,
3479 REAL_VALUE_TYPE
*, const long *);
3482 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3483 const REAL_VALUE_TYPE
*r
)
3485 REAL_VALUE_TYPE u
, normr
, v
;
3486 const struct real_format
*base_fmt
;
3488 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3490 /* Renormlize R before doing any arithmetic on it. */
3492 if (normr
.cl
== rvc_normal
)
3495 /* u = IEEE double precision portion of significand. */
3497 round_for_format (base_fmt
, &u
);
3498 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3500 if (u
.cl
== rvc_normal
)
3502 do_add (&v
, &normr
, &u
, 1);
3503 /* Call round_for_format since we might need to denormalize. */
3504 round_for_format (base_fmt
, &v
);
3505 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3509 /* Inf, NaN, 0 are all representable as doubles, so the
3510 least-significant part can be 0.0. */
3517 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3520 REAL_VALUE_TYPE u
, v
;
3521 const struct real_format
*base_fmt
;
3523 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3524 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3526 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3528 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3529 do_add (r
, &u
, &v
, 0);
3535 const struct real_format ibm_extended_format
=
3537 encode_ibm_extended
,
3538 decode_ibm_extended
,
3555 const struct real_format mips_extended_format
=
3557 encode_ibm_extended
,
3558 decode_ibm_extended
,
3576 /* IEEE quad precision format. */
3578 static void encode_ieee_quad (const struct real_format
*fmt
,
3579 long *, const REAL_VALUE_TYPE
*);
3580 static void decode_ieee_quad (const struct real_format
*,
3581 REAL_VALUE_TYPE
*, const long *);
3584 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3585 const REAL_VALUE_TYPE
*r
)
3587 unsigned long image3
, image2
, image1
, image0
, exp
;
3588 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3591 image3
= r
->sign
<< 31;
3596 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3605 image3
|= 32767 << 16;
3608 image3
|= 0x7fffffff;
3609 image2
= 0xffffffff;
3610 image1
= 0xffffffff;
3611 image0
= 0xffffffff;
3618 image3
|= 32767 << 16;
3622 if (fmt
->canonical_nan_lsbs_set
)
3625 image2
= image1
= image0
= 0xffffffff;
3628 else if (HOST_BITS_PER_LONG
== 32)
3633 image3
|= u
.sig
[3] & 0xffff;
3638 image1
= image0
>> 31 >> 1;
3640 image3
|= (image2
>> 31 >> 1) & 0xffff;
3641 image0
&= 0xffffffff;
3642 image2
&= 0xffffffff;
3644 if (r
->signalling
== fmt
->qnan_msb_set
)
3648 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3653 image3
|= 0x7fffffff;
3654 image2
= 0xffffffff;
3655 image1
= 0xffffffff;
3656 image0
= 0xffffffff;
3661 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3662 whereas the intermediate representation is 0.F x 2**exp.
3663 Which means we're off by one. */
3667 exp
= REAL_EXP (r
) + 16383 - 1;
3668 image3
|= exp
<< 16;
3670 if (HOST_BITS_PER_LONG
== 32)
3675 image3
|= u
.sig
[3] & 0xffff;
3680 image1
= image0
>> 31 >> 1;
3682 image3
|= (image2
>> 31 >> 1) & 0xffff;
3683 image0
&= 0xffffffff;
3684 image2
&= 0xffffffff;
3692 if (FLOAT_WORDS_BIG_ENDIAN
)
3709 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3712 unsigned long image3
, image2
, image1
, image0
;
3716 if (FLOAT_WORDS_BIG_ENDIAN
)
3730 image0
&= 0xffffffff;
3731 image1
&= 0xffffffff;
3732 image2
&= 0xffffffff;
3734 sign
= (image3
>> 31) & 1;
3735 exp
= (image3
>> 16) & 0x7fff;
3738 memset (r
, 0, sizeof (*r
));
3742 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
3747 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
3748 if (HOST_BITS_PER_LONG
== 32)
3757 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3758 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3763 else if (fmt
->has_signed_zero
)
3766 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3768 if (image3
| image2
| image1
| image0
)
3772 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
3774 if (HOST_BITS_PER_LONG
== 32)
3783 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3784 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3786 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3798 SET_REAL_EXP (r
, exp
- 16383 + 1);
3800 if (HOST_BITS_PER_LONG
== 32)
3809 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3810 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3812 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3813 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3817 const struct real_format ieee_quad_format
=
3837 const struct real_format mips_quad_format
=
3857 /* Descriptions of VAX floating point formats can be found beginning at
3859 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
3861 The thing to remember is that they're almost IEEE, except for word
3862 order, exponent bias, and the lack of infinities, nans, and denormals.
3864 We don't implement the H_floating format here, simply because neither
3865 the VAX or Alpha ports use it. */
3867 static void encode_vax_f (const struct real_format
*fmt
,
3868 long *, const REAL_VALUE_TYPE
*);
3869 static void decode_vax_f (const struct real_format
*,
3870 REAL_VALUE_TYPE
*, const long *);
3871 static void encode_vax_d (const struct real_format
*fmt
,
3872 long *, const REAL_VALUE_TYPE
*);
3873 static void decode_vax_d (const struct real_format
*,
3874 REAL_VALUE_TYPE
*, const long *);
3875 static void encode_vax_g (const struct real_format
*fmt
,
3876 long *, const REAL_VALUE_TYPE
*);
3877 static void decode_vax_g (const struct real_format
*,
3878 REAL_VALUE_TYPE
*, const long *);
3881 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
3882 const REAL_VALUE_TYPE
*r
)
3884 unsigned long sign
, exp
, sig
, image
;
3886 sign
= r
->sign
<< 15;
3896 image
= 0xffff7fff | sign
;
3900 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
3901 exp
= REAL_EXP (r
) + 128;
3903 image
= (sig
<< 16) & 0xffff0000;
3917 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
3918 REAL_VALUE_TYPE
*r
, const long *buf
)
3920 unsigned long image
= buf
[0] & 0xffffffff;
3921 int exp
= (image
>> 7) & 0xff;
3923 memset (r
, 0, sizeof (*r
));
3928 r
->sign
= (image
>> 15) & 1;
3929 SET_REAL_EXP (r
, exp
- 128);
3931 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
3932 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
3937 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
3938 const REAL_VALUE_TYPE
*r
)
3940 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3945 image0
= image1
= 0;
3950 image0
= 0xffff7fff | sign
;
3951 image1
= 0xffffffff;
3955 /* Extract the significand into straight hi:lo. */
3956 if (HOST_BITS_PER_LONG
== 64)
3958 image0
= r
->sig
[SIGSZ
-1];
3959 image1
= (image0
>> (64 - 56)) & 0xffffffff;
3960 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
3964 image0
= r
->sig
[SIGSZ
-1];
3965 image1
= r
->sig
[SIGSZ
-2];
3966 image1
= (image0
<< 24) | (image1
>> 8);
3967 image0
= (image0
>> 8) & 0xffffff;
3970 /* Rearrange the half-words of the significand to match the
3972 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
3973 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
3975 /* Add the sign and exponent. */
3977 image0
|= (REAL_EXP (r
) + 128) << 7;
3984 if (FLOAT_WORDS_BIG_ENDIAN
)
3985 buf
[0] = image1
, buf
[1] = image0
;
3987 buf
[0] = image0
, buf
[1] = image1
;
3991 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
3992 REAL_VALUE_TYPE
*r
, const long *buf
)
3994 unsigned long image0
, image1
;
3997 if (FLOAT_WORDS_BIG_ENDIAN
)
3998 image1
= buf
[0], image0
= buf
[1];
4000 image0
= buf
[0], image1
= buf
[1];
4001 image0
&= 0xffffffff;
4002 image1
&= 0xffffffff;
4004 exp
= (image0
>> 7) & 0xff;
4006 memset (r
, 0, sizeof (*r
));
4011 r
->sign
= (image0
>> 15) & 1;
4012 SET_REAL_EXP (r
, exp
- 128);
4014 /* Rearrange the half-words of the external format into
4015 proper ascending order. */
4016 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4017 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4019 if (HOST_BITS_PER_LONG
== 64)
4021 image0
= (image0
<< 31 << 1) | image1
;
4024 r
->sig
[SIGSZ
-1] = image0
;
4028 r
->sig
[SIGSZ
-1] = image0
;
4029 r
->sig
[SIGSZ
-2] = image1
;
4030 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4031 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4037 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4038 const REAL_VALUE_TYPE
*r
)
4040 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4045 image0
= image1
= 0;
4050 image0
= 0xffff7fff | sign
;
4051 image1
= 0xffffffff;
4055 /* Extract the significand into straight hi:lo. */
4056 if (HOST_BITS_PER_LONG
== 64)
4058 image0
= r
->sig
[SIGSZ
-1];
4059 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4060 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4064 image0
= r
->sig
[SIGSZ
-1];
4065 image1
= r
->sig
[SIGSZ
-2];
4066 image1
= (image0
<< 21) | (image1
>> 11);
4067 image0
= (image0
>> 11) & 0xfffff;
4070 /* Rearrange the half-words of the significand to match the
4072 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4073 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4075 /* Add the sign and exponent. */
4077 image0
|= (REAL_EXP (r
) + 1024) << 4;
4084 if (FLOAT_WORDS_BIG_ENDIAN
)
4085 buf
[0] = image1
, buf
[1] = image0
;
4087 buf
[0] = image0
, buf
[1] = image1
;
4091 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4092 REAL_VALUE_TYPE
*r
, const long *buf
)
4094 unsigned long image0
, image1
;
4097 if (FLOAT_WORDS_BIG_ENDIAN
)
4098 image1
= buf
[0], image0
= buf
[1];
4100 image0
= buf
[0], image1
= buf
[1];
4101 image0
&= 0xffffffff;
4102 image1
&= 0xffffffff;
4104 exp
= (image0
>> 4) & 0x7ff;
4106 memset (r
, 0, sizeof (*r
));
4111 r
->sign
= (image0
>> 15) & 1;
4112 SET_REAL_EXP (r
, exp
- 1024);
4114 /* Rearrange the half-words of the external format into
4115 proper ascending order. */
4116 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4117 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4119 if (HOST_BITS_PER_LONG
== 64)
4121 image0
= (image0
<< 31 << 1) | image1
;
4124 r
->sig
[SIGSZ
-1] = image0
;
4128 r
->sig
[SIGSZ
-1] = image0
;
4129 r
->sig
[SIGSZ
-2] = image1
;
4130 lshift_significand (r
, r
, 64 - 53);
4131 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4136 const struct real_format vax_f_format
=
4156 const struct real_format vax_d_format
=
4176 const struct real_format vax_g_format
=
4196 /* A good reference for these can be found in chapter 9 of
4197 "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
4198 An on-line version can be found here:
4200 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
4203 static void encode_i370_single (const struct real_format
*fmt
,
4204 long *, const REAL_VALUE_TYPE
*);
4205 static void decode_i370_single (const struct real_format
*,
4206 REAL_VALUE_TYPE
*, const long *);
4207 static void encode_i370_double (const struct real_format
*fmt
,
4208 long *, const REAL_VALUE_TYPE
*);
4209 static void decode_i370_double (const struct real_format
*,
4210 REAL_VALUE_TYPE
*, const long *);
4213 encode_i370_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4214 long *buf
, const REAL_VALUE_TYPE
*r
)
4216 unsigned long sign
, exp
, sig
, image
;
4218 sign
= r
->sign
<< 31;
4228 image
= 0x7fffffff | sign
;
4232 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0xffffff;
4233 exp
= ((REAL_EXP (r
) / 4) + 64) << 24;
4234 image
= sign
| exp
| sig
;
4245 decode_i370_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4246 REAL_VALUE_TYPE
*r
, const long *buf
)
4248 unsigned long sign
, sig
, image
= buf
[0];
4251 sign
= (image
>> 31) & 1;
4252 exp
= (image
>> 24) & 0x7f;
4253 sig
= image
& 0xffffff;
4255 memset (r
, 0, sizeof (*r
));
4261 SET_REAL_EXP (r
, (exp
- 64) * 4);
4262 r
->sig
[SIGSZ
-1] = sig
<< (HOST_BITS_PER_LONG
- 24);
4268 encode_i370_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4269 long *buf
, const REAL_VALUE_TYPE
*r
)
4271 unsigned long sign
, exp
, image_hi
, image_lo
;
4273 sign
= r
->sign
<< 31;
4278 image_hi
= image_lo
= 0;
4283 image_hi
= 0x7fffffff | sign
;
4284 image_lo
= 0xffffffff;
4288 if (HOST_BITS_PER_LONG
== 64)
4290 image_hi
= r
->sig
[SIGSZ
-1];
4291 image_lo
= (image_hi
>> (64 - 56)) & 0xffffffff;
4292 image_hi
= (image_hi
>> (64 - 56 + 1) >> 31) & 0xffffff;
4296 image_hi
= r
->sig
[SIGSZ
-1];
4297 image_lo
= r
->sig
[SIGSZ
-2];
4298 image_lo
= (image_lo
>> 8) | (image_hi
<< 24);
4302 exp
= ((REAL_EXP (r
) / 4) + 64) << 24;
4303 image_hi
|= sign
| exp
;
4310 if (FLOAT_WORDS_BIG_ENDIAN
)
4311 buf
[0] = image_hi
, buf
[1] = image_lo
;
4313 buf
[0] = image_lo
, buf
[1] = image_hi
;
4317 decode_i370_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4318 REAL_VALUE_TYPE
*r
, const long *buf
)
4320 unsigned long sign
, image_hi
, image_lo
;
4323 if (FLOAT_WORDS_BIG_ENDIAN
)
4324 image_hi
= buf
[0], image_lo
= buf
[1];
4326 image_lo
= buf
[0], image_hi
= buf
[1];
4328 sign
= (image_hi
>> 31) & 1;
4329 exp
= (image_hi
>> 24) & 0x7f;
4330 image_hi
&= 0xffffff;
4331 image_lo
&= 0xffffffff;
4333 memset (r
, 0, sizeof (*r
));
4335 if (exp
|| image_hi
|| image_lo
)
4339 SET_REAL_EXP (r
, (exp
- 64) * 4 + (SIGNIFICAND_BITS
- 56));
4341 if (HOST_BITS_PER_LONG
== 32)
4343 r
->sig
[0] = image_lo
;
4344 r
->sig
[1] = image_hi
;
4347 r
->sig
[0] = image_lo
| (image_hi
<< 31 << 1);
4353 const struct real_format i370_single_format
=
4367 false, /* ??? The encoding does allow for "unnormals". */
4368 false, /* ??? The encoding does allow for "unnormals". */
4373 const struct real_format i370_double_format
=
4387 false, /* ??? The encoding does allow for "unnormals". */
4388 false, /* ??? The encoding does allow for "unnormals". */
4393 /* Encode real R into a single precision DFP value in BUF. */
4395 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4396 long *buf ATTRIBUTE_UNUSED
,
4397 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4399 encode_decimal32 (fmt
, buf
, r
);
4402 /* Decode a single precision DFP value in BUF into a real R. */
4404 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4405 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4406 const long *buf ATTRIBUTE_UNUSED
)
4408 decode_decimal32 (fmt
, r
, buf
);
4411 /* Encode real R into a double precision DFP value in BUF. */
4413 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4414 long *buf ATTRIBUTE_UNUSED
,
4415 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4417 encode_decimal64 (fmt
, buf
, r
);
4420 /* Decode a double precision DFP value in BUF into a real R. */
4422 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4423 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4424 const long *buf ATTRIBUTE_UNUSED
)
4426 decode_decimal64 (fmt
, r
, buf
);
4429 /* Encode real R into a quad precision DFP value in BUF. */
4431 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4432 long *buf ATTRIBUTE_UNUSED
,
4433 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4435 encode_decimal128 (fmt
, buf
, r
);
4438 /* Decode a quad precision DFP value in BUF into a real R. */
4440 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4441 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4442 const long *buf ATTRIBUTE_UNUSED
)
4444 decode_decimal128 (fmt
, r
, buf
);
4447 /* Single precision decimal floating point (IEEE 754R). */
4448 const struct real_format decimal_single_format
=
4450 encode_decimal_single
,
4451 decode_decimal_single
,
4468 /* Double precision decimal floating point (IEEE 754R). */
4469 const struct real_format decimal_double_format
=
4471 encode_decimal_double
,
4472 decode_decimal_double
,
4489 /* Quad precision decimal floating point (IEEE 754R). */
4490 const struct real_format decimal_quad_format
=
4492 encode_decimal_quad
,
4493 decode_decimal_quad
,
4510 /* The "twos-complement" c4x format is officially defined as
4514 This is rather misleading. One must remember that F is signed.
4515 A better description would be
4517 x = -1**s * ((s + 1 + .f) * 2**e
4519 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4520 that's -1 * (1+1+(-.5)) == -1.5. I think.
4522 The constructions here are taken from Tables 5-1 and 5-2 of the
4523 TMS320C4x User's Guide wherein step-by-step instructions for
4524 conversion from IEEE are presented. That's close enough to our
4525 internal representation so as to make things easy.
4527 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
4529 static void encode_c4x_single (const struct real_format
*fmt
,
4530 long *, const REAL_VALUE_TYPE
*);
4531 static void decode_c4x_single (const struct real_format
*,
4532 REAL_VALUE_TYPE
*, const long *);
4533 static void encode_c4x_extended (const struct real_format
*fmt
,
4534 long *, const REAL_VALUE_TYPE
*);
4535 static void decode_c4x_extended (const struct real_format
*,
4536 REAL_VALUE_TYPE
*, const long *);
4539 encode_c4x_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4540 long *buf
, const REAL_VALUE_TYPE
*r
)
4542 unsigned long image
, exp
, sig
;
4554 sig
= 0x800000 - r
->sign
;
4558 exp
= REAL_EXP (r
) - 1;
4559 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4574 image
= ((exp
& 0xff) << 24) | (sig
& 0xffffff);
4579 decode_c4x_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4580 REAL_VALUE_TYPE
*r
, const long *buf
)
4582 unsigned long image
= buf
[0];
4586 exp
= (((image
>> 24) & 0xff) ^ 0x80) - 0x80;
4587 sf
= ((image
& 0xffffff) ^ 0x800000) - 0x800000;
4589 memset (r
, 0, sizeof (*r
));
4595 sig
= sf
& 0x7fffff;
4604 sig
= (sig
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4606 SET_REAL_EXP (r
, exp
+ 1);
4607 r
->sig
[SIGSZ
-1] = sig
;
4612 encode_c4x_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4613 long *buf
, const REAL_VALUE_TYPE
*r
)
4615 unsigned long exp
, sig
;
4627 sig
= 0x80000000 - r
->sign
;
4631 exp
= REAL_EXP (r
) - 1;
4633 sig
= r
->sig
[SIGSZ
-1];
4634 if (HOST_BITS_PER_LONG
== 64)
4635 sig
= sig
>> 1 >> 31;
4652 exp
= (exp
& 0xff) << 24;
4655 if (FLOAT_WORDS_BIG_ENDIAN
)
4656 buf
[0] = exp
, buf
[1] = sig
;
4658 buf
[0] = sig
, buf
[0] = exp
;
4662 decode_c4x_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4663 REAL_VALUE_TYPE
*r
, const long *buf
)
4668 if (FLOAT_WORDS_BIG_ENDIAN
)
4669 exp
= buf
[0], sf
= buf
[1];
4671 sf
= buf
[0], exp
= buf
[1];
4673 exp
= (((exp
>> 24) & 0xff) & 0x80) - 0x80;
4674 sf
= ((sf
& 0xffffffff) ^ 0x80000000) - 0x80000000;
4676 memset (r
, 0, sizeof (*r
));
4682 sig
= sf
& 0x7fffffff;
4691 if (HOST_BITS_PER_LONG
== 64)
4692 sig
= sig
<< 1 << 31;
4695 SET_REAL_EXP (r
, exp
+ 1);
4696 r
->sig
[SIGSZ
-1] = sig
;
4700 const struct real_format c4x_single_format
=
4720 const struct real_format c4x_extended_format
=
4722 encode_c4x_extended
,
4723 decode_c4x_extended
,
4741 /* A synthetic "format" for internal arithmetic. It's the size of the
4742 internal significand minus the two bits needed for proper rounding.
4743 The encode and decode routines exist only to satisfy our paranoia
4746 static void encode_internal (const struct real_format
*fmt
,
4747 long *, const REAL_VALUE_TYPE
*);
4748 static void decode_internal (const struct real_format
*,
4749 REAL_VALUE_TYPE
*, const long *);
4752 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4753 const REAL_VALUE_TYPE
*r
)
4755 memcpy (buf
, r
, sizeof (*r
));
4759 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4760 REAL_VALUE_TYPE
*r
, const long *buf
)
4762 memcpy (r
, buf
, sizeof (*r
));
4765 const struct real_format real_internal_format
=
4771 SIGNIFICAND_BITS
- 2,
4772 SIGNIFICAND_BITS
- 2,
4785 /* Calculate the square root of X in mode MODE, and store the result
4786 in R. Return TRUE if the operation does not raise an exception.
4787 For details see "High Precision Division and Square Root",
4788 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4789 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4792 real_sqrt (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4793 const REAL_VALUE_TYPE
*x
)
4795 static REAL_VALUE_TYPE halfthree
;
4796 static bool init
= false;
4797 REAL_VALUE_TYPE h
, t
, i
;
4800 /* sqrt(-0.0) is -0.0. */
4801 if (real_isnegzero (x
))
4807 /* Negative arguments return NaN. */
4810 get_canonical_qnan (r
, 0);
4814 /* Infinity and NaN return themselves. */
4815 if (real_isinf (x
) || real_isnan (x
))
4823 do_add (&halfthree
, &dconst1
, &dconsthalf
, 0);
4827 /* Initial guess for reciprocal sqrt, i. */
4828 exp
= real_exponent (x
);
4829 real_ldexp (&i
, &dconst1
, -exp
/2);
4831 /* Newton's iteration for reciprocal sqrt, i. */
4832 for (iter
= 0; iter
< 16; iter
++)
4834 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4835 do_multiply (&t
, x
, &i
);
4836 do_multiply (&h
, &t
, &i
);
4837 do_multiply (&t
, &h
, &dconsthalf
);
4838 do_add (&h
, &halfthree
, &t
, 1);
4839 do_multiply (&t
, &i
, &h
);
4841 /* Check for early convergence. */
4842 if (iter
>= 6 && real_identical (&i
, &t
))
4845 /* ??? Unroll loop to avoid copying. */
4849 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4850 do_multiply (&t
, x
, &i
);
4851 do_multiply (&h
, &t
, &i
);
4852 do_add (&i
, &dconst1
, &h
, 1);
4853 do_multiply (&h
, &t
, &i
);
4854 do_multiply (&i
, &dconsthalf
, &h
);
4855 do_add (&h
, &t
, &i
, 0);
4857 /* ??? We need a Tuckerman test to get the last bit. */
4859 real_convert (r
, mode
, &h
);
4863 /* Calculate X raised to the integer exponent N in mode MODE and store
4864 the result in R. Return true if the result may be inexact due to
4865 loss of precision. The algorithm is the classic "left-to-right binary
4866 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4867 Algorithms", "The Art of Computer Programming", Volume 2. */
4870 real_powi (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4871 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4873 unsigned HOST_WIDE_INT bit
;
4875 bool inexact
= false;
4887 /* Don't worry about overflow, from now on n is unsigned. */
4895 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4896 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4900 inexact
|= do_multiply (&t
, &t
, &t
);
4902 inexact
|= do_multiply (&t
, &t
, x
);
4910 inexact
|= do_divide (&t
, &dconst1
, &t
);
4912 real_convert (r
, mode
, &t
);
4916 /* Round X to the nearest integer not larger in absolute value, i.e.
4917 towards zero, placing the result in R in mode MODE. */
4920 real_trunc (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4921 const REAL_VALUE_TYPE
*x
)
4923 do_fix_trunc (r
, x
);
4924 if (mode
!= VOIDmode
)
4925 real_convert (r
, mode
, r
);
4928 /* Round X to the largest integer not greater in value, i.e. round
4929 down, placing the result in R in mode MODE. */
4932 real_floor (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4933 const REAL_VALUE_TYPE
*x
)
4937 do_fix_trunc (&t
, x
);
4938 if (! real_identical (&t
, x
) && x
->sign
)
4939 do_add (&t
, &t
, &dconstm1
, 0);
4940 if (mode
!= VOIDmode
)
4941 real_convert (r
, mode
, &t
);
4946 /* Round X to the smallest integer not less then argument, i.e. round
4947 up, placing the result in R in mode MODE. */
4950 real_ceil (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4951 const REAL_VALUE_TYPE
*x
)
4955 do_fix_trunc (&t
, x
);
4956 if (! real_identical (&t
, x
) && ! x
->sign
)
4957 do_add (&t
, &t
, &dconst1
, 0);
4958 if (mode
!= VOIDmode
)
4959 real_convert (r
, mode
, &t
);
4964 /* Round X to the nearest integer, but round halfway cases away from
4968 real_round (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4969 const REAL_VALUE_TYPE
*x
)
4971 do_add (r
, x
, &dconsthalf
, x
->sign
);
4972 do_fix_trunc (r
, r
);
4973 if (mode
!= VOIDmode
)
4974 real_convert (r
, mode
, r
);
4977 /* Set the sign of R to the sign of X. */
4980 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4985 /* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible
4986 for initializing and clearing the MPFR parameter. */
4989 mpfr_from_real (mpfr_ptr m
, const REAL_VALUE_TYPE
*r
)
4991 /* We use a string as an intermediate type. */
4995 real_to_hexadecimal (buf
, r
, sizeof (buf
), 0, 1);
4996 /* mpfr_set_str() parses hexadecimal floats from strings in the same
4997 format that GCC will output them. Nothing extra is needed. */
4998 ret
= mpfr_set_str (m
, buf
, 16, GMP_RNDN
);
4999 gcc_assert (ret
== 0);
5002 /* Convert from MPFR to REAL_VALUE_TYPE. */
5005 real_from_mpfr (REAL_VALUE_TYPE
*r
, mpfr_srcptr m
)
5007 /* We use a string as an intermediate type. */
5008 char buf
[128], *rstr
;
5011 rstr
= mpfr_get_str (NULL
, &exp
, 16, 0, m
, GMP_RNDN
);
5013 /* The additional 12 chars add space for the sprintf below. This
5014 leaves 6 digits for the exponent which is supposedly enough. */
5015 gcc_assert (rstr
!= NULL
&& strlen (rstr
) < sizeof (buf
) - 12);
5017 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
5018 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
5023 sprintf (buf
, "-0x.%sp%d", &rstr
[1], (int) exp
);
5025 sprintf (buf
, "0x.%sp%d", rstr
, (int) exp
);
5027 mpfr_free_str (rstr
);
5029 real_from_string (r
, buf
);
5032 /* Check whether the real constant value given is an integer. */
5035 real_isinteger (const REAL_VALUE_TYPE
*c
, enum machine_mode mode
)
5037 REAL_VALUE_TYPE cint
;
5039 real_trunc (&cint
, mode
, c
);
5040 return real_identical (c
, &cint
);