1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002,
3 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by Stephen L. Moshier (moshier@world.std.com).
5 Re-written by Richard Henderson <rth@redhat.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
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. */
70 /* Used to classify two numbers simultaneously. */
71 #define CLASS2(A, B) ((A) << 2 | (B))
73 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
74 #error "Some constant folding done by hand to avoid shift count warnings"
77 static void get_zero (REAL_VALUE_TYPE
*, int);
78 static void get_canonical_qnan (REAL_VALUE_TYPE
*, int);
79 static void get_canonical_snan (REAL_VALUE_TYPE
*, int);
80 static void get_inf (REAL_VALUE_TYPE
*, int);
81 static bool sticky_rshift_significand (REAL_VALUE_TYPE
*,
82 const REAL_VALUE_TYPE
*, unsigned int);
83 static void rshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
85 static void lshift_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
87 static void lshift_significand_1 (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
88 static bool add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*,
89 const REAL_VALUE_TYPE
*);
90 static bool sub_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
91 const REAL_VALUE_TYPE
*, int);
92 static void neg_significand (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
93 static int cmp_significands (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
94 static int cmp_significand_0 (const REAL_VALUE_TYPE
*);
95 static void set_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
96 static void clear_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
97 static bool test_significand_bit (REAL_VALUE_TYPE
*, unsigned int);
98 static void clear_significand_below (REAL_VALUE_TYPE
*, unsigned int);
99 static bool div_significands (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
100 const REAL_VALUE_TYPE
*);
101 static void normalize (REAL_VALUE_TYPE
*);
103 static bool do_add (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
104 const REAL_VALUE_TYPE
*, int);
105 static bool do_multiply (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
106 const REAL_VALUE_TYPE
*);
107 static bool do_divide (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
108 const REAL_VALUE_TYPE
*);
109 static int do_compare (const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, int);
110 static void do_fix_trunc (REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*);
112 static unsigned long rtd_divmod (REAL_VALUE_TYPE
*, REAL_VALUE_TYPE
*);
114 static const REAL_VALUE_TYPE
* ten_to_ptwo (int);
115 static const REAL_VALUE_TYPE
* ten_to_mptwo (int);
116 static const REAL_VALUE_TYPE
* real_digit (int);
117 static void times_pten (REAL_VALUE_TYPE
*, int);
119 static void round_for_format (const struct real_format
*, REAL_VALUE_TYPE
*);
121 /* Initialize R with a positive zero. */
124 get_zero (REAL_VALUE_TYPE
*r
, int sign
)
126 memset (r
, 0, sizeof (*r
));
130 /* Initialize R with the canonical quiet NaN. */
133 get_canonical_qnan (REAL_VALUE_TYPE
*r
, int sign
)
135 memset (r
, 0, sizeof (*r
));
142 get_canonical_snan (REAL_VALUE_TYPE
*r
, int sign
)
144 memset (r
, 0, sizeof (*r
));
152 get_inf (REAL_VALUE_TYPE
*r
, int sign
)
154 memset (r
, 0, sizeof (*r
));
160 /* Right-shift the significand of A by N bits; put the result in the
161 significand of R. If any one bits are shifted out, return true. */
164 sticky_rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
167 unsigned long sticky
= 0;
168 unsigned int i
, ofs
= 0;
170 if (n
>= HOST_BITS_PER_LONG
)
172 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
174 n
&= HOST_BITS_PER_LONG
- 1;
179 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
180 for (i
= 0; i
< SIGSZ
; ++i
)
183 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
184 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
185 << (HOST_BITS_PER_LONG
- n
)));
190 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
191 r
->sig
[i
] = a
->sig
[ofs
+ i
];
192 for (; i
< SIGSZ
; ++i
)
199 /* Right-shift the significand of A by N bits; put the result in the
203 rshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
206 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
208 n
&= HOST_BITS_PER_LONG
- 1;
211 for (i
= 0; i
< SIGSZ
; ++i
)
214 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
215 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
216 << (HOST_BITS_PER_LONG
- n
)));
221 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
222 r
->sig
[i
] = a
->sig
[ofs
+ i
];
223 for (; i
< SIGSZ
; ++i
)
228 /* Left-shift the significand of A by N bits; put the result in the
232 lshift_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
235 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
237 n
&= HOST_BITS_PER_LONG
- 1;
240 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
241 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
242 for (; i
< SIGSZ
; ++i
)
243 r
->sig
[SIGSZ
-1-i
] = 0;
246 for (i
= 0; i
< SIGSZ
; ++i
)
249 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
250 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
251 >> (HOST_BITS_PER_LONG
- n
)));
255 /* Likewise, but N is specialized to 1. */
258 lshift_significand_1 (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
262 for (i
= SIGSZ
- 1; i
> 0; --i
)
263 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
264 r
->sig
[0] = a
->sig
[0] << 1;
267 /* Add the significands of A and B, placing the result in R. Return
268 true if there was carry out of the most significant word. */
271 add_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
272 const REAL_VALUE_TYPE
*b
)
277 for (i
= 0; i
< SIGSZ
; ++i
)
279 unsigned long ai
= a
->sig
[i
];
280 unsigned long ri
= ai
+ b
->sig
[i
];
296 /* Subtract the significands of A and B, placing the result in R. CARRY is
297 true if there's a borrow incoming to the least significant word.
298 Return true if there was borrow out of the most significant word. */
301 sub_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
302 const REAL_VALUE_TYPE
*b
, int carry
)
306 for (i
= 0; i
< SIGSZ
; ++i
)
308 unsigned long ai
= a
->sig
[i
];
309 unsigned long ri
= ai
- b
->sig
[i
];
325 /* Negate the significand A, placing the result in R. */
328 neg_significand (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
333 for (i
= 0; i
< SIGSZ
; ++i
)
335 unsigned long ri
, ai
= a
->sig
[i
];
354 /* Compare significands. Return tri-state vs zero. */
357 cmp_significands (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
361 for (i
= SIGSZ
- 1; i
>= 0; --i
)
363 unsigned long ai
= a
->sig
[i
];
364 unsigned long bi
= b
->sig
[i
];
375 /* Return true if A is nonzero. */
378 cmp_significand_0 (const REAL_VALUE_TYPE
*a
)
382 for (i
= SIGSZ
- 1; i
>= 0; --i
)
389 /* Set bit N of the significand of R. */
392 set_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
394 r
->sig
[n
/ HOST_BITS_PER_LONG
]
395 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
398 /* Clear bit N of the significand of R. */
401 clear_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
403 r
->sig
[n
/ HOST_BITS_PER_LONG
]
404 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
407 /* Test bit N of the significand of R. */
410 test_significand_bit (REAL_VALUE_TYPE
*r
, unsigned int n
)
412 /* ??? Compiler bug here if we return this expression directly.
413 The conversion to bool strips the "&1" and we wind up testing
414 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
415 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
419 /* Clear bits 0..N-1 of the significand of R. */
422 clear_significand_below (REAL_VALUE_TYPE
*r
, unsigned int n
)
424 int i
, w
= n
/ HOST_BITS_PER_LONG
;
426 for (i
= 0; i
< w
; ++i
)
429 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
432 /* Divide the significands of A and B, placing the result in R. Return
433 true if the division was inexact. */
436 div_significands (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
437 const REAL_VALUE_TYPE
*b
)
440 int i
, bit
= SIGNIFICAND_BITS
- 1;
441 unsigned long msb
, inexact
;
444 memset (r
->sig
, 0, sizeof (r
->sig
));
450 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
451 lshift_significand_1 (&u
, &u
);
453 if (msb
|| cmp_significands (&u
, b
) >= 0)
455 sub_significands (&u
, &u
, b
, 0);
456 set_significand_bit (r
, bit
);
461 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
467 /* Adjust the exponent and significand of R such that the most
468 significant bit is set. We underflow to zero and overflow to
469 infinity here, without denormals. (The intermediate representation
470 exponent is large enough to handle target denormals normalized.) */
473 normalize (REAL_VALUE_TYPE
*r
)
481 /* Find the first word that is nonzero. */
482 for (i
= SIGSZ
- 1; i
>= 0; i
--)
484 shift
+= HOST_BITS_PER_LONG
;
488 /* Zero significand flushes to zero. */
496 /* Find the first bit that is nonzero. */
498 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
504 exp
= REAL_EXP (r
) - shift
;
506 get_inf (r
, r
->sign
);
507 else if (exp
< -MAX_EXP
)
508 get_zero (r
, r
->sign
);
511 SET_REAL_EXP (r
, exp
);
512 lshift_significand (r
, r
, shift
);
517 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
518 result may be inexact due to a loss of precision. */
521 do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
522 const REAL_VALUE_TYPE
*b
, int subtract_p
)
526 bool inexact
= false;
528 /* Determine if we need to add or subtract. */
530 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
532 switch (CLASS2 (a
->cl
, b
->cl
))
534 case CLASS2 (rvc_zero
, rvc_zero
):
535 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
536 get_zero (r
, sign
& !subtract_p
);
539 case CLASS2 (rvc_zero
, rvc_normal
):
540 case CLASS2 (rvc_zero
, rvc_inf
):
541 case CLASS2 (rvc_zero
, rvc_nan
):
543 case CLASS2 (rvc_normal
, rvc_nan
):
544 case CLASS2 (rvc_inf
, rvc_nan
):
545 case CLASS2 (rvc_nan
, rvc_nan
):
546 /* ANY + NaN = NaN. */
547 case CLASS2 (rvc_normal
, rvc_inf
):
550 r
->sign
= sign
^ subtract_p
;
553 case CLASS2 (rvc_normal
, rvc_zero
):
554 case CLASS2 (rvc_inf
, rvc_zero
):
555 case CLASS2 (rvc_nan
, rvc_zero
):
557 case CLASS2 (rvc_nan
, rvc_normal
):
558 case CLASS2 (rvc_nan
, rvc_inf
):
559 /* NaN + ANY = NaN. */
560 case CLASS2 (rvc_inf
, rvc_normal
):
565 case CLASS2 (rvc_inf
, rvc_inf
):
567 /* Inf - Inf = NaN. */
568 get_canonical_qnan (r
, 0);
570 /* Inf + Inf = Inf. */
574 case CLASS2 (rvc_normal
, rvc_normal
):
581 /* Swap the arguments such that A has the larger exponent. */
582 dexp
= REAL_EXP (a
) - REAL_EXP (b
);
585 const REAL_VALUE_TYPE
*t
;
592 /* If the exponents are not identical, we need to shift the
593 significand of B down. */
596 /* If the exponents are too far apart, the significands
597 do not overlap, which makes the subtraction a noop. */
598 if (dexp
>= SIGNIFICAND_BITS
)
605 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
611 if (sub_significands (r
, a
, b
, inexact
))
613 /* We got a borrow out of the subtraction. That means that
614 A and B had the same exponent, and B had the larger
615 significand. We need to swap the sign and negate the
618 neg_significand (r
, r
);
623 if (add_significands (r
, a
, b
))
625 /* We got carry out of the addition. This means we need to
626 shift the significand back down one bit and increase the
628 inexact
|= sticky_rshift_significand (r
, r
, 1);
629 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
640 SET_REAL_EXP (r
, exp
);
641 /* Zero out the remaining fields. */
646 /* Re-normalize the result. */
649 /* Special case: if the subtraction results in zero, the result
651 if (r
->cl
== rvc_zero
)
654 r
->sig
[0] |= inexact
;
659 /* Calculate R = A * B. Return true if the result may be inexact. */
662 do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
663 const REAL_VALUE_TYPE
*b
)
665 REAL_VALUE_TYPE u
, t
, *rr
;
666 unsigned int i
, j
, k
;
667 int sign
= a
->sign
^ b
->sign
;
668 bool inexact
= false;
670 switch (CLASS2 (a
->cl
, b
->cl
))
672 case CLASS2 (rvc_zero
, rvc_zero
):
673 case CLASS2 (rvc_zero
, rvc_normal
):
674 case CLASS2 (rvc_normal
, rvc_zero
):
675 /* +-0 * ANY = 0 with appropriate sign. */
679 case CLASS2 (rvc_zero
, rvc_nan
):
680 case CLASS2 (rvc_normal
, rvc_nan
):
681 case CLASS2 (rvc_inf
, rvc_nan
):
682 case CLASS2 (rvc_nan
, rvc_nan
):
683 /* ANY * NaN = NaN. */
688 case CLASS2 (rvc_nan
, rvc_zero
):
689 case CLASS2 (rvc_nan
, rvc_normal
):
690 case CLASS2 (rvc_nan
, rvc_inf
):
691 /* NaN * ANY = NaN. */
696 case CLASS2 (rvc_zero
, rvc_inf
):
697 case CLASS2 (rvc_inf
, rvc_zero
):
699 get_canonical_qnan (r
, sign
);
702 case CLASS2 (rvc_inf
, rvc_inf
):
703 case CLASS2 (rvc_normal
, rvc_inf
):
704 case CLASS2 (rvc_inf
, rvc_normal
):
705 /* Inf * Inf = Inf, R * Inf = Inf */
709 case CLASS2 (rvc_normal
, rvc_normal
):
716 if (r
== a
|| r
== b
)
722 /* Collect all the partial products. Since we don't have sure access
723 to a widening multiply, we split each long into two half-words.
725 Consider the long-hand form of a four half-word multiplication:
735 We construct partial products of the widened half-word products
736 that are known to not overlap, e.g. DF+DH. Each such partial
737 product is given its proper exponent, which allows us to sum them
738 and obtain the finished product. */
740 for (i
= 0; i
< SIGSZ
* 2; ++i
)
742 unsigned long ai
= a
->sig
[i
/ 2];
744 ai
>>= HOST_BITS_PER_LONG
/ 2;
746 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
751 for (j
= 0; j
< 2; ++j
)
753 int exp
= (REAL_EXP (a
) - (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
754 + (REAL_EXP (b
) - (1-j
)*(HOST_BITS_PER_LONG
/2)));
763 /* Would underflow to zero, which we shouldn't bother adding. */
768 memset (&u
, 0, sizeof (u
));
770 SET_REAL_EXP (&u
, exp
);
772 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
774 unsigned long bi
= b
->sig
[k
/ 2];
776 bi
>>= HOST_BITS_PER_LONG
/ 2;
778 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
780 u
.sig
[k
/ 2] = ai
* bi
;
784 inexact
|= do_add (rr
, rr
, &u
, 0);
795 /* Calculate R = A / B. Return true if the result may be inexact. */
798 do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
,
799 const REAL_VALUE_TYPE
*b
)
801 int exp
, sign
= a
->sign
^ b
->sign
;
802 REAL_VALUE_TYPE t
, *rr
;
805 switch (CLASS2 (a
->cl
, b
->cl
))
807 case CLASS2 (rvc_zero
, rvc_zero
):
809 case CLASS2 (rvc_inf
, rvc_inf
):
810 /* Inf / Inf = NaN. */
811 get_canonical_qnan (r
, sign
);
814 case CLASS2 (rvc_zero
, rvc_normal
):
815 case CLASS2 (rvc_zero
, rvc_inf
):
817 case CLASS2 (rvc_normal
, rvc_inf
):
822 case CLASS2 (rvc_normal
, rvc_zero
):
824 case CLASS2 (rvc_inf
, rvc_zero
):
829 case CLASS2 (rvc_zero
, rvc_nan
):
830 case CLASS2 (rvc_normal
, rvc_nan
):
831 case CLASS2 (rvc_inf
, rvc_nan
):
832 case CLASS2 (rvc_nan
, rvc_nan
):
833 /* ANY / NaN = NaN. */
838 case CLASS2 (rvc_nan
, rvc_zero
):
839 case CLASS2 (rvc_nan
, rvc_normal
):
840 case CLASS2 (rvc_nan
, rvc_inf
):
841 /* NaN / ANY = NaN. */
846 case CLASS2 (rvc_inf
, rvc_normal
):
851 case CLASS2 (rvc_normal
, rvc_normal
):
858 if (r
== a
|| r
== b
)
863 /* Make sure all fields in the result are initialized. */
868 exp
= REAL_EXP (a
) - REAL_EXP (b
) + 1;
879 SET_REAL_EXP (rr
, exp
);
881 inexact
= div_significands (rr
, a
, b
);
883 /* Re-normalize the result. */
885 rr
->sig
[0] |= inexact
;
893 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
894 one of the two operands is a NaN. */
897 do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
902 switch (CLASS2 (a
->cl
, b
->cl
))
904 case CLASS2 (rvc_zero
, rvc_zero
):
905 /* Sign of zero doesn't matter for compares. */
908 case CLASS2 (rvc_normal
, rvc_zero
):
909 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
911 return decimal_do_compare (a
, b
, nan_result
);
913 case CLASS2 (rvc_inf
, rvc_zero
):
914 case CLASS2 (rvc_inf
, rvc_normal
):
915 return (a
->sign
? -1 : 1);
917 case CLASS2 (rvc_inf
, rvc_inf
):
918 return -a
->sign
- -b
->sign
;
920 case CLASS2 (rvc_zero
, rvc_normal
):
921 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
923 return decimal_do_compare (a
, b
, nan_result
);
925 case CLASS2 (rvc_zero
, rvc_inf
):
926 case CLASS2 (rvc_normal
, rvc_inf
):
927 return (b
->sign
? 1 : -1);
929 case CLASS2 (rvc_zero
, rvc_nan
):
930 case CLASS2 (rvc_normal
, rvc_nan
):
931 case CLASS2 (rvc_inf
, rvc_nan
):
932 case CLASS2 (rvc_nan
, rvc_nan
):
933 case CLASS2 (rvc_nan
, rvc_zero
):
934 case CLASS2 (rvc_nan
, rvc_normal
):
935 case CLASS2 (rvc_nan
, rvc_inf
):
938 case CLASS2 (rvc_normal
, rvc_normal
):
945 if (a
->sign
!= b
->sign
)
946 return -a
->sign
- -b
->sign
;
948 if (a
->decimal
|| b
->decimal
)
949 return decimal_do_compare (a
, b
, nan_result
);
951 if (REAL_EXP (a
) > REAL_EXP (b
))
953 else if (REAL_EXP (a
) < REAL_EXP (b
))
956 ret
= cmp_significands (a
, b
);
958 return (a
->sign
? -ret
: ret
);
961 /* Return A truncated to an integral value toward zero. */
964 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
978 decimal_do_fix_trunc (r
, a
);
981 if (REAL_EXP (r
) <= 0)
982 get_zero (r
, r
->sign
);
983 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
984 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
992 /* Perform the binary or unary operation described by CODE.
993 For a unary operation, leave OP1 NULL. This function returns
994 true if the result may be inexact due to loss of precision. */
997 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
998 const REAL_VALUE_TYPE
*op1
)
1000 enum tree_code code
= (enum tree_code
) icode
;
1002 if (op0
->decimal
|| (op1
&& op1
->decimal
))
1003 return decimal_real_arithmetic (r
, code
, op0
, op1
);
1008 return do_add (r
, op0
, op1
, 0);
1011 return do_add (r
, op0
, op1
, 1);
1014 return do_multiply (r
, op0
, op1
);
1017 return do_divide (r
, op0
, op1
);
1020 if (op1
->cl
== rvc_nan
)
1022 else if (do_compare (op0
, op1
, -1) < 0)
1029 if (op1
->cl
== rvc_nan
)
1031 else if (do_compare (op0
, op1
, 1) < 0)
1047 case FIX_TRUNC_EXPR
:
1048 do_fix_trunc (r
, op0
);
1057 /* Legacy. Similar, but return the result directly. */
1060 real_arithmetic2 (int icode
, const REAL_VALUE_TYPE
*op0
,
1061 const REAL_VALUE_TYPE
*op1
)
1064 real_arithmetic (&r
, icode
, op0
, op1
);
1069 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1070 const REAL_VALUE_TYPE
*op1
)
1072 enum tree_code code
= (enum tree_code
) icode
;
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;
1087 return do_compare (op0
, op1
, -1) != 0;
1088 case UNORDERED_EXPR
:
1089 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1091 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
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
, 1) >= 0;
1101 return do_compare (op0
, op1
, 0) == 0;
1103 return do_compare (op0
, op1
, 0) != 0;
1110 /* Return floor log2(R). */
1113 real_exponent (const REAL_VALUE_TYPE
*r
)
1121 return (unsigned int)-1 >> 1;
1123 return REAL_EXP (r
);
1129 /* R = OP0 * 2**EXP. */
1132 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1143 exp
+= REAL_EXP (op0
);
1145 get_inf (r
, r
->sign
);
1146 else if (exp
< -MAX_EXP
)
1147 get_zero (r
, r
->sign
);
1149 SET_REAL_EXP (r
, exp
);
1157 /* Determine whether a floating-point value X is infinite. */
1160 real_isinf (const REAL_VALUE_TYPE
*r
)
1162 return (r
->cl
== rvc_inf
);
1165 /* Determine whether a floating-point value X is a NaN. */
1168 real_isnan (const REAL_VALUE_TYPE
*r
)
1170 return (r
->cl
== rvc_nan
);
1173 /* Determine whether a floating-point value X is finite. */
1176 real_isfinite (const REAL_VALUE_TYPE
*r
)
1178 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1181 /* Determine whether a floating-point value X is negative. */
1184 real_isneg (const REAL_VALUE_TYPE
*r
)
1189 /* Determine whether a floating-point value X is minus zero. */
1192 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1194 return r
->sign
&& r
->cl
== rvc_zero
;
1197 /* Compare two floating-point objects for bitwise identity. */
1200 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1206 if (a
->sign
!= b
->sign
)
1216 if (a
->decimal
!= b
->decimal
)
1218 if (REAL_EXP (a
) != REAL_EXP (b
))
1223 if (a
->signalling
!= b
->signalling
)
1225 /* The significand is ignored for canonical NaNs. */
1226 if (a
->canonical
|| b
->canonical
)
1227 return a
->canonical
== b
->canonical
;
1234 for (i
= 0; i
< SIGSZ
; ++i
)
1235 if (a
->sig
[i
] != b
->sig
[i
])
1241 /* Try to change R into its exact multiplicative inverse in machine
1242 mode MODE. Return true if successful. */
1245 exact_real_inverse (enum machine_mode mode
, REAL_VALUE_TYPE
*r
)
1247 const REAL_VALUE_TYPE
*one
= real_digit (1);
1251 if (r
->cl
!= rvc_normal
)
1254 /* Check for a power of two: all significand bits zero except the MSB. */
1255 for (i
= 0; i
< SIGSZ
-1; ++i
)
1258 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1261 /* Find the inverse and truncate to the required mode. */
1262 do_divide (&u
, one
, r
);
1263 real_convert (&u
, mode
, &u
);
1265 /* The rounding may have overflowed. */
1266 if (u
.cl
!= rvc_normal
)
1268 for (i
= 0; i
< SIGSZ
-1; ++i
)
1271 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1278 /* Return true if arithmetic on values in IMODE that were promoted
1279 from values in TMODE is equivalent to direct arithmetic on values
1283 real_can_shorten_arithmetic (enum machine_mode imode
, enum machine_mode tmode
)
1285 const struct real_format
*tfmt
, *ifmt
;
1286 tfmt
= REAL_MODE_FORMAT (tmode
);
1287 ifmt
= REAL_MODE_FORMAT (imode
);
1288 /* These conditions are conservative rather than trying to catch the
1289 exact boundary conditions; the main case to allow is IEEE float
1291 return (ifmt
->b
== tfmt
->b
1292 && ifmt
->p
> 2 * tfmt
->p
1293 && ifmt
->emin
< 2 * tfmt
->emin
- tfmt
->p
- 2
1294 && ifmt
->emin
< tfmt
->emin
- tfmt
->emax
- tfmt
->p
- 2
1295 && ifmt
->emax
> 2 * tfmt
->emax
+ 2
1296 && ifmt
->emax
> tfmt
->emax
- tfmt
->emin
+ tfmt
->p
+ 2
1297 && ifmt
->round_towards_zero
== tfmt
->round_towards_zero
1298 && (ifmt
->has_sign_dependent_rounding
1299 == tfmt
->has_sign_dependent_rounding
)
1300 && ifmt
->has_nans
>= tfmt
->has_nans
1301 && ifmt
->has_inf
>= tfmt
->has_inf
1302 && ifmt
->has_signed_zero
>= tfmt
->has_signed_zero
1303 && !MODE_COMPOSITE_P (tmode
)
1304 && !MODE_COMPOSITE_P (imode
));
1307 /* Render R as an integer. */
1310 real_to_integer (const REAL_VALUE_TYPE
*r
)
1312 unsigned HOST_WIDE_INT i
;
1323 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1330 return decimal_real_to_integer (r
);
1332 if (REAL_EXP (r
) <= 0)
1334 /* Only force overflow for unsigned overflow. Signed overflow is
1335 undefined, so it doesn't matter what we return, and some callers
1336 expect to be able to use this routine for both signed and
1337 unsigned conversions. */
1338 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1341 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1342 i
= r
->sig
[SIGSZ
-1];
1345 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1346 i
= r
->sig
[SIGSZ
-1];
1347 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1348 i
|= r
->sig
[SIGSZ
-2];
1351 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1362 /* Likewise, but to an integer pair, HI+LOW. */
1365 real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
1366 const REAL_VALUE_TYPE
*r
)
1369 HOST_WIDE_INT low
, high
;
1382 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1395 decimal_real_to_integer2 (plow
, phigh
, r
);
1402 /* Only force overflow for unsigned overflow. Signed overflow is
1403 undefined, so it doesn't matter what we return, and some callers
1404 expect to be able to use this routine for both signed and
1405 unsigned conversions. */
1406 if (exp
> 2*HOST_BITS_PER_WIDE_INT
)
1409 rshift_significand (&t
, r
, 2*HOST_BITS_PER_WIDE_INT
- exp
);
1410 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1412 high
= t
.sig
[SIGSZ
-1];
1413 low
= t
.sig
[SIGSZ
-2];
1417 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
);
1418 high
= t
.sig
[SIGSZ
-1];
1419 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1420 high
|= t
.sig
[SIGSZ
-2];
1422 low
= t
.sig
[SIGSZ
-3];
1423 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1424 low
|= t
.sig
[SIGSZ
-4];
1432 low
= -low
, high
= ~high
;
1444 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1445 of NUM / DEN. Return the quotient and place the remainder in NUM.
1446 It is expected that NUM / DEN are close enough that the quotient is
1449 static unsigned long
1450 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1452 unsigned long q
, msb
;
1453 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1462 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1464 lshift_significand_1 (num
, num
);
1466 if (msb
|| cmp_significands (num
, den
) >= 0)
1468 sub_significands (num
, num
, den
, 0);
1472 while (--expn
>= expd
);
1474 SET_REAL_EXP (num
, expd
);
1480 /* Render R as a decimal floating point constant. Emit DIGITS significant
1481 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1482 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1483 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1484 to a string that, when parsed back in mode MODE, yields the same value. */
1486 #define M_LOG10_2 0.30102999566398119521
1489 real_to_decimal_for_mode (char *str
, const REAL_VALUE_TYPE
*r_orig
,
1490 size_t buf_size
, size_t digits
,
1491 int crop_trailing_zeros
, enum machine_mode mode
)
1493 const struct real_format
*fmt
= NULL
;
1494 const REAL_VALUE_TYPE
*one
, *ten
;
1495 REAL_VALUE_TYPE r
, pten
, u
, v
;
1496 int dec_exp
, cmp_one
, digit
;
1498 char *p
, *first
, *last
;
1502 if (mode
!= VOIDmode
)
1504 fmt
= REAL_MODE_FORMAT (mode
);
1512 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1517 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1520 /* ??? Print the significand as well, if not canonical? */
1521 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1522 (r_orig
->signalling
? 'S' : 'Q'));
1530 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1534 /* Bound the number of digits printed by the size of the representation. */
1535 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1536 if (digits
== 0 || digits
> max_digits
)
1537 digits
= max_digits
;
1539 /* Estimate the decimal exponent, and compute the length of the string it
1540 will print as. Be conservative and add one to account for possible
1541 overflow or rounding error. */
1542 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1543 for (max_digits
= 1; dec_exp
; max_digits
++)
1546 /* Bound the number of digits printed by the size of the output buffer. */
1547 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1548 gcc_assert (max_digits
<= buf_size
);
1549 if (digits
> max_digits
)
1550 digits
= max_digits
;
1552 one
= real_digit (1);
1553 ten
= ten_to_ptwo (0);
1561 cmp_one
= do_compare (&r
, one
, 0);
1566 /* Number is greater than one. Convert significand to an integer
1567 and strip trailing decimal zeros. */
1570 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1572 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1573 m
= floor_log2 (max_digits
);
1575 /* Iterate over the bits of the possible powers of 10 that might
1576 be present in U and eliminate them. That is, if we find that
1577 10**2**M divides U evenly, keep the division and increase
1583 do_divide (&t
, &u
, ten_to_ptwo (m
));
1584 do_fix_trunc (&v
, &t
);
1585 if (cmp_significands (&v
, &t
) == 0)
1593 /* Revert the scaling to integer that we performed earlier. */
1594 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1595 - (SIGNIFICAND_BITS
- 1));
1598 /* Find power of 10. Do this by dividing out 10**2**M when
1599 this is larger than the current remainder. Fill PTEN with
1600 the power of 10 that we compute. */
1601 if (REAL_EXP (&r
) > 0)
1603 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1606 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1607 if (do_compare (&u
, ptentwo
, 0) >= 0)
1609 do_divide (&u
, &u
, ptentwo
);
1610 do_multiply (&pten
, &pten
, ptentwo
);
1617 /* We managed to divide off enough tens in the above reduction
1618 loop that we've now got a negative exponent. Fall into the
1619 less-than-one code to compute the proper value for PTEN. */
1626 /* Number is less than one. Pad significand with leading
1632 /* Stop if we'd shift bits off the bottom. */
1636 do_multiply (&u
, &v
, ten
);
1638 /* Stop if we're now >= 1. */
1639 if (REAL_EXP (&u
) > 0)
1647 /* Find power of 10. Do this by multiplying in P=10**2**M when
1648 the current remainder is smaller than 1/P. Fill PTEN with the
1649 power of 10 that we compute. */
1650 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1653 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1654 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1656 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1658 do_multiply (&v
, &v
, ptentwo
);
1659 do_multiply (&pten
, &pten
, ptentwo
);
1665 /* Invert the positive power of 10 that we've collected so far. */
1666 do_divide (&pten
, one
, &pten
);
1674 /* At this point, PTEN should contain the nearest power of 10 smaller
1675 than R, such that this division produces the first digit.
1677 Using a divide-step primitive that returns the complete integral
1678 remainder avoids the rounding error that would be produced if
1679 we were to use do_divide here and then simply multiply by 10 for
1680 each subsequent digit. */
1682 digit
= rtd_divmod (&r
, &pten
);
1684 /* Be prepared for error in that division via underflow ... */
1685 if (digit
== 0 && cmp_significand_0 (&r
))
1687 /* Multiply by 10 and try again. */
1688 do_multiply (&r
, &r
, ten
);
1689 digit
= rtd_divmod (&r
, &pten
);
1691 gcc_assert (digit
!= 0);
1694 /* ... or overflow. */
1704 gcc_assert (digit
<= 10);
1708 /* Generate subsequent digits. */
1709 while (--digits
> 0)
1711 do_multiply (&r
, &r
, ten
);
1712 digit
= rtd_divmod (&r
, &pten
);
1717 /* Generate one more digit with which to do rounding. */
1718 do_multiply (&r
, &r
, ten
);
1719 digit
= rtd_divmod (&r
, &pten
);
1721 /* Round the result. */
1722 if (fmt
&& fmt
->round_towards_zero
)
1724 /* If the format uses round towards zero when parsing the string
1725 back in, we need to always round away from zero here. */
1726 if (cmp_significand_0 (&r
))
1728 round_up
= digit
> 0;
1734 /* Round to nearest. If R is nonzero there are additional
1735 nonzero digits to be extracted. */
1736 if (cmp_significand_0 (&r
))
1738 /* Round to even. */
1739 else if ((p
[-1] - '0') & 1)
1743 round_up
= digit
> 5;
1760 /* Carry out of the first digit. This means we had all 9's and
1761 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1769 /* Insert the decimal point. */
1770 first
[0] = first
[1];
1773 /* If requested, drop trailing zeros. Never crop past "1.0". */
1774 if (crop_trailing_zeros
)
1775 while (last
> first
+ 3 && last
[-1] == '0')
1778 /* Append the exponent. */
1779 sprintf (last
, "e%+d", dec_exp
);
1781 #ifdef ENABLE_CHECKING
1782 /* Verify that we can read the original value back in. */
1783 if (mode
!= VOIDmode
)
1785 real_from_string (&r
, str
);
1786 real_convert (&r
, mode
, &r
);
1787 gcc_assert (real_identical (&r
, r_orig
));
1792 /* Likewise, except always uses round-to-nearest. */
1795 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1796 size_t digits
, int crop_trailing_zeros
)
1798 real_to_decimal_for_mode (str
, r_orig
, buf_size
,
1799 digits
, crop_trailing_zeros
, VOIDmode
);
1802 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1803 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1804 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1805 strip trailing zeros. */
1808 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1809 size_t digits
, int crop_trailing_zeros
)
1811 int i
, j
, exp
= REAL_EXP (r
);
1824 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1827 /* ??? Print the significand as well, if not canonical? */
1828 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1829 (r
->signalling
? 'S' : 'Q'));
1837 /* Hexadecimal format for decimal floats is not interesting. */
1838 strcpy (str
, "N/A");
1843 digits
= SIGNIFICAND_BITS
/ 4;
1845 /* Bound the number of digits printed by the size of the output buffer. */
1847 sprintf (exp_buf
, "p%+d", exp
);
1848 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1849 gcc_assert (max_digits
<= buf_size
);
1850 if (digits
> max_digits
)
1851 digits
= max_digits
;
1862 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1863 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1865 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1871 if (crop_trailing_zeros
)
1872 while (p
> first
+ 1 && p
[-1] == '0')
1875 sprintf (p
, "p%+d", exp
);
1878 /* Initialize R from a decimal or hexadecimal string. The string is
1879 assumed to have been syntax checked already. Return -1 if the
1880 value underflows, +1 if overflows, and 0 otherwise. */
1883 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1895 else if (*str
== '+')
1898 if (!strncmp (str
, "QNaN", 4))
1900 get_canonical_qnan (r
, sign
);
1903 else if (!strncmp (str
, "SNaN", 4))
1905 get_canonical_snan (r
, sign
);
1908 else if (!strncmp (str
, "Inf", 3))
1914 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1916 /* Hexadecimal floating point. */
1917 int pos
= SIGNIFICAND_BITS
- 4, d
;
1925 d
= hex_value (*str
);
1930 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1931 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1935 /* Ensure correct rounding by setting last bit if there is
1936 a subsequent nonzero digit. */
1944 if (pos
== SIGNIFICAND_BITS
- 4)
1951 d
= hex_value (*str
);
1956 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1957 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1961 /* Ensure correct rounding by setting last bit if there is
1962 a subsequent nonzero digit. */
1968 /* If the mantissa is zero, ignore the exponent. */
1969 if (!cmp_significand_0 (r
))
1972 if (*str
== 'p' || *str
== 'P')
1974 bool exp_neg
= false;
1982 else if (*str
== '+')
1986 while (ISDIGIT (*str
))
1992 /* Overflowed the exponent. */
2007 SET_REAL_EXP (r
, exp
);
2013 /* Decimal floating point. */
2014 const REAL_VALUE_TYPE
*ten
= ten_to_ptwo (0);
2019 while (ISDIGIT (*str
))
2022 do_multiply (r
, r
, ten
);
2024 do_add (r
, r
, real_digit (d
), 0);
2029 if (r
->cl
== rvc_zero
)
2034 while (ISDIGIT (*str
))
2037 do_multiply (r
, r
, ten
);
2039 do_add (r
, r
, real_digit (d
), 0);
2044 /* If the mantissa is zero, ignore the exponent. */
2045 if (r
->cl
== rvc_zero
)
2048 if (*str
== 'e' || *str
== 'E')
2050 bool exp_neg
= false;
2058 else if (*str
== '+')
2062 while (ISDIGIT (*str
))
2068 /* Overflowed the exponent. */
2082 times_pten (r
, exp
);
2101 /* Legacy. Similar, but return the result directly. */
2104 real_from_string2 (const char *s
, enum machine_mode mode
)
2108 real_from_string (&r
, s
);
2109 if (mode
!= VOIDmode
)
2110 real_convert (&r
, mode
, &r
);
2115 /* Initialize R from string S and desired MODE. */
2118 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, enum machine_mode mode
)
2120 if (DECIMAL_FLOAT_MODE_P (mode
))
2121 decimal_real_from_string (r
, s
);
2123 real_from_string (r
, s
);
2125 if (mode
!= VOIDmode
)
2126 real_convert (r
, mode
, r
);
2129 /* Initialize R from the integer pair HIGH+LOW. */
2132 real_from_integer (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2133 unsigned HOST_WIDE_INT low
, HOST_WIDE_INT high
,
2136 if (low
== 0 && high
== 0)
2140 memset (r
, 0, sizeof (*r
));
2142 r
->sign
= high
< 0 && !unsigned_p
;
2143 SET_REAL_EXP (r
, 2 * HOST_BITS_PER_WIDE_INT
);
2154 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2156 r
->sig
[SIGSZ
-1] = high
;
2157 r
->sig
[SIGSZ
-2] = low
;
2161 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2162 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2163 r
->sig
[SIGSZ
-2] = high
;
2164 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2165 r
->sig
[SIGSZ
-4] = low
;
2171 if (mode
!= VOIDmode
)
2172 real_convert (r
, mode
, r
);
2175 /* Returns 10**2**N. */
2177 static const REAL_VALUE_TYPE
*
2180 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2182 gcc_assert (n
>= 0);
2183 gcc_assert (n
< EXP_BITS
);
2185 if (tens
[n
].cl
== rvc_zero
)
2187 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2189 HOST_WIDE_INT t
= 10;
2192 for (i
= 0; i
< n
; ++i
)
2195 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2199 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2200 do_multiply (&tens
[n
], t
, t
);
2207 /* Returns 10**(-2**N). */
2209 static const REAL_VALUE_TYPE
*
2210 ten_to_mptwo (int n
)
2212 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2214 gcc_assert (n
>= 0);
2215 gcc_assert (n
< EXP_BITS
);
2217 if (tens
[n
].cl
== rvc_zero
)
2218 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2225 static const REAL_VALUE_TYPE
*
2228 static REAL_VALUE_TYPE num
[10];
2230 gcc_assert (n
>= 0);
2231 gcc_assert (n
<= 9);
2233 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2234 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2239 /* Multiply R by 10**EXP. */
2242 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2244 REAL_VALUE_TYPE pten
, *rr
;
2245 bool negative
= (exp
< 0);
2251 pten
= *real_digit (1);
2257 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2259 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2262 do_divide (r
, r
, &pten
);
2265 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2267 const REAL_VALUE_TYPE
*
2270 static REAL_VALUE_TYPE value
;
2272 /* Initialize mathematical constants for constant folding builtins.
2273 These constants need to be given to at least 160 bits precision. */
2274 if (value
.cl
== rvc_zero
)
2277 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2278 mpfr_set_ui (m
, 1, GMP_RNDN
);
2279 mpfr_exp (m
, m
, GMP_RNDN
);
2280 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2287 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2289 const REAL_VALUE_TYPE
*
2290 dconst_third_ptr (void)
2292 static REAL_VALUE_TYPE value
;
2294 /* Initialize mathematical constants for constant folding builtins.
2295 These constants need to be given to at least 160 bits precision. */
2296 if (value
.cl
== rvc_zero
)
2298 real_arithmetic (&value
, RDIV_EXPR
, &dconst1
, real_digit (3));
2303 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2305 const REAL_VALUE_TYPE
*
2306 dconst_sqrt2_ptr (void)
2308 static REAL_VALUE_TYPE value
;
2310 /* Initialize mathematical constants for constant folding builtins.
2311 These constants need to be given to at least 160 bits precision. */
2312 if (value
.cl
== rvc_zero
)
2315 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2316 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2317 real_from_mpfr (&value
, m
, NULL_TREE
, GMP_RNDN
);
2323 /* Fills R with +Inf. */
2326 real_inf (REAL_VALUE_TYPE
*r
)
2331 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2332 we force a QNaN, else we force an SNaN. The string, if not empty,
2333 is parsed as a number and placed in the significand. Return true
2334 if the string was successfully parsed. */
2337 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2338 enum machine_mode mode
)
2340 const struct real_format
*fmt
;
2342 fmt
= REAL_MODE_FORMAT (mode
);
2348 get_canonical_qnan (r
, 0);
2350 get_canonical_snan (r
, 0);
2356 memset (r
, 0, sizeof (*r
));
2359 /* Parse akin to strtol into the significand of R. */
2361 while (ISSPACE (*str
))
2365 else if (*str
== '+')
2370 if (*str
== 'x' || *str
== 'X')
2379 while ((d
= hex_value (*str
)) < base
)
2386 lshift_significand (r
, r
, 3);
2389 lshift_significand (r
, r
, 4);
2392 lshift_significand_1 (&u
, r
);
2393 lshift_significand (r
, r
, 3);
2394 add_significands (r
, r
, &u
);
2402 add_significands (r
, r
, &u
);
2407 /* Must have consumed the entire string for success. */
2411 /* Shift the significand into place such that the bits
2412 are in the most significant bits for the format. */
2413 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2415 /* Our MSB is always unset for NaNs. */
2416 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2418 /* Force quiet or signalling NaN. */
2419 r
->signalling
= !quiet
;
2425 /* Fills R with the largest finite value representable in mode MODE.
2426 If SIGN is nonzero, R is set to the most negative finite value. */
2429 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
2431 const struct real_format
*fmt
;
2434 fmt
= REAL_MODE_FORMAT (mode
);
2436 memset (r
, 0, sizeof (*r
));
2439 decimal_real_maxval (r
, sign
, mode
);
2444 SET_REAL_EXP (r
, fmt
->emax
);
2446 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2447 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2448 clear_significand_below (r
, np2
);
2450 if (fmt
->pnan
< fmt
->p
)
2451 /* This is an IBM extended double format made up of two IEEE
2452 doubles. The value of the long double is the sum of the
2453 values of the two parts. The most significant part is
2454 required to be the value of the long double rounded to the
2455 nearest double. Rounding means we need a slightly smaller
2456 value for LDBL_MAX. */
2457 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
- 1);
2461 /* Fills R with 2**N. */
2464 real_2expN (REAL_VALUE_TYPE
*r
, int n
, enum machine_mode fmode
)
2466 memset (r
, 0, sizeof (*r
));
2471 else if (n
< -MAX_EXP
)
2476 SET_REAL_EXP (r
, n
);
2477 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2479 if (DECIMAL_FLOAT_MODE_P (fmode
))
2480 decimal_real_convert (r
, fmode
, r
);
2485 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2489 bool round_up
= false;
2495 decimal_round_for_format (fmt
, r
);
2498 /* FIXME. We can come here via fp_easy_constant
2499 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2500 investigated whether this convert needs to be here, or
2501 something else is missing. */
2502 decimal_real_convert (r
, DFmode
, r
);
2506 emin2m1
= fmt
->emin
- 1;
2509 np2
= SIGNIFICAND_BITS
- p2
;
2513 get_zero (r
, r
->sign
);
2515 if (!fmt
->has_signed_zero
)
2520 get_inf (r
, r
->sign
);
2525 clear_significand_below (r
, np2
);
2535 /* Check the range of the exponent. If we're out of range,
2536 either underflow or overflow. */
2537 if (REAL_EXP (r
) > emax2
)
2539 else if (REAL_EXP (r
) <= emin2m1
)
2543 if (!fmt
->has_denorm
)
2545 /* Don't underflow completely until we've had a chance to round. */
2546 if (REAL_EXP (r
) < emin2m1
)
2551 diff
= emin2m1
- REAL_EXP (r
) + 1;
2555 /* De-normalize the significand. */
2556 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2557 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2561 if (!fmt
->round_towards_zero
)
2563 /* There are P2 true significand bits, followed by one guard bit,
2564 followed by one sticky bit, followed by stuff. Fold nonzero
2565 stuff into the sticky bit. */
2566 unsigned long sticky
;
2570 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2571 sticky
|= r
->sig
[i
];
2573 & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2575 guard
= test_significand_bit (r
, np2
- 1);
2576 lsb
= test_significand_bit (r
, np2
);
2578 /* Round to even. */
2579 round_up
= guard
&& (sticky
|| lsb
);
2586 set_significand_bit (&u
, np2
);
2588 if (add_significands (r
, r
, &u
))
2590 /* Overflow. Means the significand had been all ones, and
2591 is now all zeros. Need to increase the exponent, and
2592 possibly re-normalize it. */
2593 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2594 if (REAL_EXP (r
) > emax2
)
2596 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2600 /* Catch underflow that we deferred until after rounding. */
2601 if (REAL_EXP (r
) <= emin2m1
)
2604 /* Clear out trailing garbage. */
2605 clear_significand_below (r
, np2
);
2608 /* Extend or truncate to a new mode. */
2611 real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2612 const REAL_VALUE_TYPE
*a
)
2614 const struct real_format
*fmt
;
2616 fmt
= REAL_MODE_FORMAT (mode
);
2621 if (a
->decimal
|| fmt
->b
== 10)
2622 decimal_real_convert (r
, mode
, a
);
2624 round_for_format (fmt
, r
);
2626 /* round_for_format de-normalizes denormals. Undo just that part. */
2627 if (r
->cl
== rvc_normal
)
2631 /* Legacy. Likewise, except return the struct directly. */
2634 real_value_truncate (enum machine_mode mode
, REAL_VALUE_TYPE a
)
2637 real_convert (&r
, mode
, &a
);
2641 /* Return true if truncating to MODE is exact. */
2644 exact_real_truncate (enum machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2646 const struct real_format
*fmt
;
2650 fmt
= REAL_MODE_FORMAT (mode
);
2653 /* Don't allow conversion to denormals. */
2654 emin2m1
= fmt
->emin
- 1;
2655 if (REAL_EXP (a
) <= emin2m1
)
2658 /* After conversion to the new mode, the value must be identical. */
2659 real_convert (&t
, mode
, a
);
2660 return real_identical (&t
, a
);
2663 /* Write R to the given target format. Place the words of the result
2664 in target word order in BUF. There are always 32 bits in each
2665 long, no matter the size of the host long.
2667 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2670 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2671 const struct real_format
*fmt
)
2677 round_for_format (fmt
, &r
);
2681 (*fmt
->encode
) (fmt
, buf
, &r
);
2686 /* Similar, but look up the format from MODE. */
2689 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, enum machine_mode mode
)
2691 const struct real_format
*fmt
;
2693 fmt
= REAL_MODE_FORMAT (mode
);
2696 return real_to_target_fmt (buf
, r
, fmt
);
2699 /* Read R from the given target format. Read the words of the result
2700 in target word order in BUF. There are always 32 bits in each
2701 long, no matter the size of the host long. */
2704 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2705 const struct real_format
*fmt
)
2707 (*fmt
->decode
) (fmt
, r
, buf
);
2710 /* Similar, but look up the format from MODE. */
2713 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, enum machine_mode mode
)
2715 const struct real_format
*fmt
;
2717 fmt
= REAL_MODE_FORMAT (mode
);
2720 (*fmt
->decode
) (fmt
, r
, buf
);
2723 /* Return the number of bits of the largest binary value that the
2724 significand of MODE will hold. */
2725 /* ??? Legacy. Should get access to real_format directly. */
2728 significand_size (enum machine_mode mode
)
2730 const struct real_format
*fmt
;
2732 fmt
= REAL_MODE_FORMAT (mode
);
2738 /* Return the size in bits of the largest binary value that can be
2739 held by the decimal coefficient for this mode. This is one more
2740 than the number of bits required to hold the largest coefficient
2742 double log2_10
= 3.3219281;
2743 return fmt
->p
* log2_10
;
2748 /* Return a hash value for the given real value. */
2749 /* ??? The "unsigned int" return value is intended to be hashval_t,
2750 but I didn't want to pull hashtab.h into real.h. */
2753 real_hash (const REAL_VALUE_TYPE
*r
)
2758 h
= r
->cl
| (r
->sign
<< 2);
2766 h
|= REAL_EXP (r
) << 3;
2771 h
^= (unsigned int)-1;
2780 if (sizeof(unsigned long) > sizeof(unsigned int))
2781 for (i
= 0; i
< SIGSZ
; ++i
)
2783 unsigned long s
= r
->sig
[i
];
2784 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2787 for (i
= 0; i
< SIGSZ
; ++i
)
2793 /* IEEE single-precision format. */
2795 static void encode_ieee_single (const struct real_format
*fmt
,
2796 long *, const REAL_VALUE_TYPE
*);
2797 static void decode_ieee_single (const struct real_format
*,
2798 REAL_VALUE_TYPE
*, const long *);
2801 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2802 const REAL_VALUE_TYPE
*r
)
2804 unsigned long image
, sig
, exp
;
2805 unsigned long sign
= r
->sign
;
2806 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2809 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2820 image
|= 0x7fffffff;
2827 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2828 if (r
->signalling
== fmt
->qnan_msb_set
)
2839 image
|= 0x7fffffff;
2843 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2844 whereas the intermediate representation is 0.F x 2**exp.
2845 Which means we're off by one. */
2849 exp
= REAL_EXP (r
) + 127 - 1;
2862 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2865 unsigned long image
= buf
[0] & 0xffffffff;
2866 bool sign
= (image
>> 31) & 1;
2867 int exp
= (image
>> 23) & 0xff;
2869 memset (r
, 0, sizeof (*r
));
2870 image
<<= HOST_BITS_PER_LONG
- 24;
2875 if (image
&& fmt
->has_denorm
)
2879 SET_REAL_EXP (r
, -126);
2880 r
->sig
[SIGSZ
-1] = image
<< 1;
2883 else if (fmt
->has_signed_zero
)
2886 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2892 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2893 ^ fmt
->qnan_msb_set
);
2894 r
->sig
[SIGSZ
-1] = image
;
2906 SET_REAL_EXP (r
, exp
- 127 + 1);
2907 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2911 const struct real_format ieee_single_format
=
2932 const struct real_format mips_single_format
=
2953 const struct real_format motorola_single_format
=
2974 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
2975 single precision with the following differences:
2976 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
2978 - NaNs are not supported.
2979 - The range of non-zero numbers in binary is
2980 (001)[1.]000...000 to (255)[1.]111...111.
2981 - Denormals can be represented, but are treated as +0.0 when
2982 used as an operand and are never generated as a result.
2983 - -0.0 can be represented, but a zero result is always +0.0.
2984 - the only supported rounding mode is trunction (towards zero). */
2985 const struct real_format spu_single_format
=
3006 /* IEEE double-precision format. */
3008 static void encode_ieee_double (const struct real_format
*fmt
,
3009 long *, const REAL_VALUE_TYPE
*);
3010 static void decode_ieee_double (const struct real_format
*,
3011 REAL_VALUE_TYPE
*, const long *);
3014 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
3015 const REAL_VALUE_TYPE
*r
)
3017 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
3018 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3020 image_hi
= r
->sign
<< 31;
3023 if (HOST_BITS_PER_LONG
== 64)
3025 sig_hi
= r
->sig
[SIGSZ
-1];
3026 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
3027 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
3031 sig_hi
= r
->sig
[SIGSZ
-1];
3032 sig_lo
= r
->sig
[SIGSZ
-2];
3033 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
3034 sig_hi
= (sig_hi
>> 11) & 0xfffff;
3044 image_hi
|= 2047 << 20;
3047 image_hi
|= 0x7fffffff;
3048 image_lo
= 0xffffffff;
3057 if (fmt
->canonical_nan_lsbs_set
)
3059 sig_hi
= (1 << 19) - 1;
3060 sig_lo
= 0xffffffff;
3068 if (r
->signalling
== fmt
->qnan_msb_set
)
3069 sig_hi
&= ~(1 << 19);
3072 if (sig_hi
== 0 && sig_lo
== 0)
3075 image_hi
|= 2047 << 20;
3081 image_hi
|= 0x7fffffff;
3082 image_lo
= 0xffffffff;
3087 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3088 whereas the intermediate representation is 0.F x 2**exp.
3089 Which means we're off by one. */
3093 exp
= REAL_EXP (r
) + 1023 - 1;
3094 image_hi
|= exp
<< 20;
3103 if (FLOAT_WORDS_BIG_ENDIAN
)
3104 buf
[0] = image_hi
, buf
[1] = image_lo
;
3106 buf
[0] = image_lo
, buf
[1] = image_hi
;
3110 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3113 unsigned long image_hi
, image_lo
;
3117 if (FLOAT_WORDS_BIG_ENDIAN
)
3118 image_hi
= buf
[0], image_lo
= buf
[1];
3120 image_lo
= buf
[0], image_hi
= buf
[1];
3121 image_lo
&= 0xffffffff;
3122 image_hi
&= 0xffffffff;
3124 sign
= (image_hi
>> 31) & 1;
3125 exp
= (image_hi
>> 20) & 0x7ff;
3127 memset (r
, 0, sizeof (*r
));
3129 image_hi
<<= 32 - 21;
3130 image_hi
|= image_lo
>> 21;
3131 image_hi
&= 0x7fffffff;
3132 image_lo
<<= 32 - 21;
3136 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3140 SET_REAL_EXP (r
, -1022);
3141 if (HOST_BITS_PER_LONG
== 32)
3143 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3145 r
->sig
[SIGSZ
-1] = image_hi
;
3146 r
->sig
[SIGSZ
-2] = image_lo
;
3150 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3151 r
->sig
[SIGSZ
-1] = image_hi
;
3155 else if (fmt
->has_signed_zero
)
3158 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3160 if (image_hi
|| image_lo
)
3164 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3165 if (HOST_BITS_PER_LONG
== 32)
3167 r
->sig
[SIGSZ
-1] = image_hi
;
3168 r
->sig
[SIGSZ
-2] = image_lo
;
3171 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3183 SET_REAL_EXP (r
, exp
- 1023 + 1);
3184 if (HOST_BITS_PER_LONG
== 32)
3186 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3187 r
->sig
[SIGSZ
-2] = image_lo
;
3190 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3194 const struct real_format ieee_double_format
=
3215 const struct real_format mips_double_format
=
3236 const struct real_format motorola_double_format
=
3257 /* IEEE extended real format. This comes in three flavors: Intel's as
3258 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3259 12- and 16-byte images may be big- or little endian; Motorola's is
3260 always big endian. */
3262 /* Helper subroutine which converts from the internal format to the
3263 12-byte little-endian Intel format. Functions below adjust this
3264 for the other possible formats. */
3266 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3267 const REAL_VALUE_TYPE
*r
)
3269 unsigned long image_hi
, sig_hi
, sig_lo
;
3270 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3272 image_hi
= r
->sign
<< 15;
3273 sig_hi
= sig_lo
= 0;
3285 /* Intel requires the explicit integer bit to be set, otherwise
3286 it considers the value a "pseudo-infinity". Motorola docs
3287 say it doesn't care. */
3288 sig_hi
= 0x80000000;
3293 sig_lo
= sig_hi
= 0xffffffff;
3303 if (fmt
->canonical_nan_lsbs_set
)
3305 sig_hi
= (1 << 30) - 1;
3306 sig_lo
= 0xffffffff;
3309 else if (HOST_BITS_PER_LONG
== 32)
3311 sig_hi
= r
->sig
[SIGSZ
-1];
3312 sig_lo
= r
->sig
[SIGSZ
-2];
3316 sig_lo
= r
->sig
[SIGSZ
-1];
3317 sig_hi
= sig_lo
>> 31 >> 1;
3318 sig_lo
&= 0xffffffff;
3320 if (r
->signalling
== fmt
->qnan_msb_set
)
3321 sig_hi
&= ~(1 << 30);
3324 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3327 /* Intel requires the explicit integer bit to be set, otherwise
3328 it considers the value a "pseudo-nan". Motorola docs say it
3330 sig_hi
|= 0x80000000;
3335 sig_lo
= sig_hi
= 0xffffffff;
3341 int exp
= REAL_EXP (r
);
3343 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3344 whereas the intermediate representation is 0.F x 2**exp.
3345 Which means we're off by one.
3347 Except for Motorola, which consider exp=0 and explicit
3348 integer bit set to continue to be normalized. In theory
3349 this discrepancy has been taken care of by the difference
3350 in fmt->emin in round_for_format. */
3357 gcc_assert (exp
>= 0);
3361 if (HOST_BITS_PER_LONG
== 32)
3363 sig_hi
= r
->sig
[SIGSZ
-1];
3364 sig_lo
= r
->sig
[SIGSZ
-2];
3368 sig_lo
= r
->sig
[SIGSZ
-1];
3369 sig_hi
= sig_lo
>> 31 >> 1;
3370 sig_lo
&= 0xffffffff;
3379 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3382 /* Convert from the internal format to the 12-byte Motorola format
3383 for an IEEE extended real. */
3385 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3386 const REAL_VALUE_TYPE
*r
)
3389 encode_ieee_extended (fmt
, intermed
, r
);
3391 /* Motorola chips are assumed always to be big-endian. Also, the
3392 padding in a Motorola extended real goes between the exponent and
3393 the mantissa. At this point the mantissa is entirely within
3394 elements 0 and 1 of intermed, and the exponent entirely within
3395 element 2, so all we have to do is swap the order around, and
3396 shift element 2 left 16 bits. */
3397 buf
[0] = intermed
[2] << 16;
3398 buf
[1] = intermed
[1];
3399 buf
[2] = intermed
[0];
3402 /* Convert from the internal format to the 12-byte Intel format for
3403 an IEEE extended real. */
3405 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3406 const REAL_VALUE_TYPE
*r
)
3408 if (FLOAT_WORDS_BIG_ENDIAN
)
3410 /* All the padding in an Intel-format extended real goes at the high
3411 end, which in this case is after the mantissa, not the exponent.
3412 Therefore we must shift everything down 16 bits. */
3414 encode_ieee_extended (fmt
, intermed
, r
);
3415 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3416 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3417 buf
[2] = (intermed
[0] << 16);
3420 /* encode_ieee_extended produces what we want directly. */
3421 encode_ieee_extended (fmt
, buf
, r
);
3424 /* Convert from the internal format to the 16-byte Intel format for
3425 an IEEE extended real. */
3427 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3428 const REAL_VALUE_TYPE
*r
)
3430 /* All the padding in an Intel-format extended real goes at the high end. */
3431 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3435 /* As above, we have a helper function which converts from 12-byte
3436 little-endian Intel format to internal format. Functions below
3437 adjust for the other possible formats. */
3439 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3442 unsigned long image_hi
, sig_hi
, sig_lo
;
3446 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3447 sig_lo
&= 0xffffffff;
3448 sig_hi
&= 0xffffffff;
3449 image_hi
&= 0xffffffff;
3451 sign
= (image_hi
>> 15) & 1;
3452 exp
= image_hi
& 0x7fff;
3454 memset (r
, 0, sizeof (*r
));
3458 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3463 /* When the IEEE format contains a hidden bit, we know that
3464 it's zero at this point, and so shift up the significand
3465 and decrease the exponent to match. In this case, Motorola
3466 defines the explicit integer bit to be valid, so we don't
3467 know whether the msb is set or not. */
3468 SET_REAL_EXP (r
, fmt
->emin
);
3469 if (HOST_BITS_PER_LONG
== 32)
3471 r
->sig
[SIGSZ
-1] = sig_hi
;
3472 r
->sig
[SIGSZ
-2] = sig_lo
;
3475 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3479 else if (fmt
->has_signed_zero
)
3482 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3484 /* See above re "pseudo-infinities" and "pseudo-nans".
3485 Short summary is that the MSB will likely always be
3486 set, and that we don't care about it. */
3487 sig_hi
&= 0x7fffffff;
3489 if (sig_hi
|| sig_lo
)
3493 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3494 if (HOST_BITS_PER_LONG
== 32)
3496 r
->sig
[SIGSZ
-1] = sig_hi
;
3497 r
->sig
[SIGSZ
-2] = sig_lo
;
3500 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3512 SET_REAL_EXP (r
, exp
- 16383 + 1);
3513 if (HOST_BITS_PER_LONG
== 32)
3515 r
->sig
[SIGSZ
-1] = sig_hi
;
3516 r
->sig
[SIGSZ
-2] = sig_lo
;
3519 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3523 /* Convert from the internal format to the 12-byte Motorola format
3524 for an IEEE extended real. */
3526 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3531 /* Motorola chips are assumed always to be big-endian. Also, the
3532 padding in a Motorola extended real goes between the exponent and
3533 the mantissa; remove it. */
3534 intermed
[0] = buf
[2];
3535 intermed
[1] = buf
[1];
3536 intermed
[2] = (unsigned long)buf
[0] >> 16;
3538 decode_ieee_extended (fmt
, r
, intermed
);
3541 /* Convert from the internal format to the 12-byte Intel format for
3542 an IEEE extended real. */
3544 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3547 if (FLOAT_WORDS_BIG_ENDIAN
)
3549 /* All the padding in an Intel-format extended real goes at the high
3550 end, which in this case is after the mantissa, not the exponent.
3551 Therefore we must shift everything up 16 bits. */
3554 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3555 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3556 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3558 decode_ieee_extended (fmt
, r
, intermed
);
3561 /* decode_ieee_extended produces what we want directly. */
3562 decode_ieee_extended (fmt
, r
, buf
);
3565 /* Convert from the internal format to the 16-byte Intel format for
3566 an IEEE extended real. */
3568 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3571 /* All the padding in an Intel-format extended real goes at the high end. */
3572 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3575 const struct real_format ieee_extended_motorola_format
=
3577 encode_ieee_extended_motorola
,
3578 decode_ieee_extended_motorola
,
3596 const struct real_format ieee_extended_intel_96_format
=
3598 encode_ieee_extended_intel_96
,
3599 decode_ieee_extended_intel_96
,
3617 const struct real_format ieee_extended_intel_128_format
=
3619 encode_ieee_extended_intel_128
,
3620 decode_ieee_extended_intel_128
,
3638 /* The following caters to i386 systems that set the rounding precision
3639 to 53 bits instead of 64, e.g. FreeBSD. */
3640 const struct real_format ieee_extended_intel_96_round_53_format
=
3642 encode_ieee_extended_intel_96
,
3643 decode_ieee_extended_intel_96
,
3661 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3662 numbers whose sum is equal to the extended precision value. The number
3663 with greater magnitude is first. This format has the same magnitude
3664 range as an IEEE double precision value, but effectively 106 bits of
3665 significand precision. Infinity and NaN are represented by their IEEE
3666 double precision value stored in the first number, the second number is
3667 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3669 static void encode_ibm_extended (const struct real_format
*fmt
,
3670 long *, const REAL_VALUE_TYPE
*);
3671 static void decode_ibm_extended (const struct real_format
*,
3672 REAL_VALUE_TYPE
*, const long *);
3675 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3676 const REAL_VALUE_TYPE
*r
)
3678 REAL_VALUE_TYPE u
, normr
, v
;
3679 const struct real_format
*base_fmt
;
3681 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3683 /* Renormalize R before doing any arithmetic on it. */
3685 if (normr
.cl
== rvc_normal
)
3688 /* u = IEEE double precision portion of significand. */
3690 round_for_format (base_fmt
, &u
);
3691 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3693 if (u
.cl
== rvc_normal
)
3695 do_add (&v
, &normr
, &u
, 1);
3696 /* Call round_for_format since we might need to denormalize. */
3697 round_for_format (base_fmt
, &v
);
3698 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3702 /* Inf, NaN, 0 are all representable as doubles, so the
3703 least-significant part can be 0.0. */
3710 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3713 REAL_VALUE_TYPE u
, v
;
3714 const struct real_format
*base_fmt
;
3716 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3717 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3719 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3721 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3722 do_add (r
, &u
, &v
, 0);
3728 const struct real_format ibm_extended_format
=
3730 encode_ibm_extended
,
3731 decode_ibm_extended
,
3749 const struct real_format mips_extended_format
=
3751 encode_ibm_extended
,
3752 decode_ibm_extended
,
3771 /* IEEE quad precision format. */
3773 static void encode_ieee_quad (const struct real_format
*fmt
,
3774 long *, const REAL_VALUE_TYPE
*);
3775 static void decode_ieee_quad (const struct real_format
*,
3776 REAL_VALUE_TYPE
*, const long *);
3779 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3780 const REAL_VALUE_TYPE
*r
)
3782 unsigned long image3
, image2
, image1
, image0
, exp
;
3783 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3786 image3
= r
->sign
<< 31;
3791 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3800 image3
|= 32767 << 16;
3803 image3
|= 0x7fffffff;
3804 image2
= 0xffffffff;
3805 image1
= 0xffffffff;
3806 image0
= 0xffffffff;
3813 image3
|= 32767 << 16;
3817 if (fmt
->canonical_nan_lsbs_set
)
3820 image2
= image1
= image0
= 0xffffffff;
3823 else if (HOST_BITS_PER_LONG
== 32)
3828 image3
|= u
.sig
[3] & 0xffff;
3833 image1
= image0
>> 31 >> 1;
3835 image3
|= (image2
>> 31 >> 1) & 0xffff;
3836 image0
&= 0xffffffff;
3837 image2
&= 0xffffffff;
3839 if (r
->signalling
== fmt
->qnan_msb_set
)
3843 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3848 image3
|= 0x7fffffff;
3849 image2
= 0xffffffff;
3850 image1
= 0xffffffff;
3851 image0
= 0xffffffff;
3856 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3857 whereas the intermediate representation is 0.F x 2**exp.
3858 Which means we're off by one. */
3862 exp
= REAL_EXP (r
) + 16383 - 1;
3863 image3
|= exp
<< 16;
3865 if (HOST_BITS_PER_LONG
== 32)
3870 image3
|= u
.sig
[3] & 0xffff;
3875 image1
= image0
>> 31 >> 1;
3877 image3
|= (image2
>> 31 >> 1) & 0xffff;
3878 image0
&= 0xffffffff;
3879 image2
&= 0xffffffff;
3887 if (FLOAT_WORDS_BIG_ENDIAN
)
3904 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3907 unsigned long image3
, image2
, image1
, image0
;
3911 if (FLOAT_WORDS_BIG_ENDIAN
)
3925 image0
&= 0xffffffff;
3926 image1
&= 0xffffffff;
3927 image2
&= 0xffffffff;
3929 sign
= (image3
>> 31) & 1;
3930 exp
= (image3
>> 16) & 0x7fff;
3933 memset (r
, 0, sizeof (*r
));
3937 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
3942 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
3943 if (HOST_BITS_PER_LONG
== 32)
3952 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3953 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3958 else if (fmt
->has_signed_zero
)
3961 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3963 if (image3
| image2
| image1
| image0
)
3967 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
3969 if (HOST_BITS_PER_LONG
== 32)
3978 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3979 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3981 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3993 SET_REAL_EXP (r
, exp
- 16383 + 1);
3995 if (HOST_BITS_PER_LONG
== 32)
4004 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
4005 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
4007 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
4008 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4012 const struct real_format ieee_quad_format
=
4033 const struct real_format mips_quad_format
=
4054 /* Descriptions of VAX floating point formats can be found beginning at
4056 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4058 The thing to remember is that they're almost IEEE, except for word
4059 order, exponent bias, and the lack of infinities, nans, and denormals.
4061 We don't implement the H_floating format here, simply because neither
4062 the VAX or Alpha ports use it. */
4064 static void encode_vax_f (const struct real_format
*fmt
,
4065 long *, const REAL_VALUE_TYPE
*);
4066 static void decode_vax_f (const struct real_format
*,
4067 REAL_VALUE_TYPE
*, const long *);
4068 static void encode_vax_d (const struct real_format
*fmt
,
4069 long *, const REAL_VALUE_TYPE
*);
4070 static void decode_vax_d (const struct real_format
*,
4071 REAL_VALUE_TYPE
*, const long *);
4072 static void encode_vax_g (const struct real_format
*fmt
,
4073 long *, const REAL_VALUE_TYPE
*);
4074 static void decode_vax_g (const struct real_format
*,
4075 REAL_VALUE_TYPE
*, const long *);
4078 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4079 const REAL_VALUE_TYPE
*r
)
4081 unsigned long sign
, exp
, sig
, image
;
4083 sign
= r
->sign
<< 15;
4093 image
= 0xffff7fff | sign
;
4097 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4098 exp
= REAL_EXP (r
) + 128;
4100 image
= (sig
<< 16) & 0xffff0000;
4114 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4115 REAL_VALUE_TYPE
*r
, const long *buf
)
4117 unsigned long image
= buf
[0] & 0xffffffff;
4118 int exp
= (image
>> 7) & 0xff;
4120 memset (r
, 0, sizeof (*r
));
4125 r
->sign
= (image
>> 15) & 1;
4126 SET_REAL_EXP (r
, exp
- 128);
4128 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
4129 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4134 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4135 const REAL_VALUE_TYPE
*r
)
4137 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4142 image0
= image1
= 0;
4147 image0
= 0xffff7fff | sign
;
4148 image1
= 0xffffffff;
4152 /* Extract the significand into straight hi:lo. */
4153 if (HOST_BITS_PER_LONG
== 64)
4155 image0
= r
->sig
[SIGSZ
-1];
4156 image1
= (image0
>> (64 - 56)) & 0xffffffff;
4157 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
4161 image0
= r
->sig
[SIGSZ
-1];
4162 image1
= r
->sig
[SIGSZ
-2];
4163 image1
= (image0
<< 24) | (image1
>> 8);
4164 image0
= (image0
>> 8) & 0xffffff;
4167 /* Rearrange the half-words of the significand to match the
4169 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4170 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4172 /* Add the sign and exponent. */
4174 image0
|= (REAL_EXP (r
) + 128) << 7;
4181 if (FLOAT_WORDS_BIG_ENDIAN
)
4182 buf
[0] = image1
, buf
[1] = image0
;
4184 buf
[0] = image0
, buf
[1] = image1
;
4188 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4189 REAL_VALUE_TYPE
*r
, const long *buf
)
4191 unsigned long image0
, image1
;
4194 if (FLOAT_WORDS_BIG_ENDIAN
)
4195 image1
= buf
[0], image0
= buf
[1];
4197 image0
= buf
[0], image1
= buf
[1];
4198 image0
&= 0xffffffff;
4199 image1
&= 0xffffffff;
4201 exp
= (image0
>> 7) & 0xff;
4203 memset (r
, 0, sizeof (*r
));
4208 r
->sign
= (image0
>> 15) & 1;
4209 SET_REAL_EXP (r
, exp
- 128);
4211 /* Rearrange the half-words of the external format into
4212 proper ascending order. */
4213 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4214 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4216 if (HOST_BITS_PER_LONG
== 64)
4218 image0
= (image0
<< 31 << 1) | image1
;
4221 r
->sig
[SIGSZ
-1] = image0
;
4225 r
->sig
[SIGSZ
-1] = image0
;
4226 r
->sig
[SIGSZ
-2] = image1
;
4227 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4228 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4234 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4235 const REAL_VALUE_TYPE
*r
)
4237 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4242 image0
= image1
= 0;
4247 image0
= 0xffff7fff | sign
;
4248 image1
= 0xffffffff;
4252 /* Extract the significand into straight hi:lo. */
4253 if (HOST_BITS_PER_LONG
== 64)
4255 image0
= r
->sig
[SIGSZ
-1];
4256 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4257 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4261 image0
= r
->sig
[SIGSZ
-1];
4262 image1
= r
->sig
[SIGSZ
-2];
4263 image1
= (image0
<< 21) | (image1
>> 11);
4264 image0
= (image0
>> 11) & 0xfffff;
4267 /* Rearrange the half-words of the significand to match the
4269 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4270 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4272 /* Add the sign and exponent. */
4274 image0
|= (REAL_EXP (r
) + 1024) << 4;
4281 if (FLOAT_WORDS_BIG_ENDIAN
)
4282 buf
[0] = image1
, buf
[1] = image0
;
4284 buf
[0] = image0
, buf
[1] = image1
;
4288 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4289 REAL_VALUE_TYPE
*r
, const long *buf
)
4291 unsigned long image0
, image1
;
4294 if (FLOAT_WORDS_BIG_ENDIAN
)
4295 image1
= buf
[0], image0
= buf
[1];
4297 image0
= buf
[0], image1
= buf
[1];
4298 image0
&= 0xffffffff;
4299 image1
&= 0xffffffff;
4301 exp
= (image0
>> 4) & 0x7ff;
4303 memset (r
, 0, sizeof (*r
));
4308 r
->sign
= (image0
>> 15) & 1;
4309 SET_REAL_EXP (r
, exp
- 1024);
4311 /* Rearrange the half-words of the external format into
4312 proper ascending order. */
4313 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4314 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4316 if (HOST_BITS_PER_LONG
== 64)
4318 image0
= (image0
<< 31 << 1) | image1
;
4321 r
->sig
[SIGSZ
-1] = image0
;
4325 r
->sig
[SIGSZ
-1] = image0
;
4326 r
->sig
[SIGSZ
-2] = image1
;
4327 lshift_significand (r
, r
, 64 - 53);
4328 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4333 const struct real_format vax_f_format
=
4354 const struct real_format vax_d_format
=
4375 const struct real_format vax_g_format
=
4396 /* Encode real R into a single precision DFP value in BUF. */
4398 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4399 long *buf ATTRIBUTE_UNUSED
,
4400 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4402 encode_decimal32 (fmt
, buf
, r
);
4405 /* Decode a single precision DFP value in BUF into a real R. */
4407 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4408 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4409 const long *buf ATTRIBUTE_UNUSED
)
4411 decode_decimal32 (fmt
, r
, buf
);
4414 /* Encode real R into a double precision DFP value in BUF. */
4416 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4417 long *buf ATTRIBUTE_UNUSED
,
4418 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4420 encode_decimal64 (fmt
, buf
, r
);
4423 /* Decode a double precision DFP value in BUF into a real R. */
4425 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4426 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4427 const long *buf ATTRIBUTE_UNUSED
)
4429 decode_decimal64 (fmt
, r
, buf
);
4432 /* Encode real R into a quad precision DFP value in BUF. */
4434 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4435 long *buf ATTRIBUTE_UNUSED
,
4436 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4438 encode_decimal128 (fmt
, buf
, r
);
4441 /* Decode a quad precision DFP value in BUF into a real R. */
4443 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4444 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4445 const long *buf ATTRIBUTE_UNUSED
)
4447 decode_decimal128 (fmt
, r
, buf
);
4450 /* Single precision decimal floating point (IEEE 754). */
4451 const struct real_format decimal_single_format
=
4453 encode_decimal_single
,
4454 decode_decimal_single
,
4472 /* Double precision decimal floating point (IEEE 754). */
4473 const struct real_format decimal_double_format
=
4475 encode_decimal_double
,
4476 decode_decimal_double
,
4494 /* Quad precision decimal floating point (IEEE 754). */
4495 const struct real_format decimal_quad_format
=
4497 encode_decimal_quad
,
4498 decode_decimal_quad
,
4516 /* A synthetic "format" for internal arithmetic. It's the size of the
4517 internal significand minus the two bits needed for proper rounding.
4518 The encode and decode routines exist only to satisfy our paranoia
4521 static void encode_internal (const struct real_format
*fmt
,
4522 long *, const REAL_VALUE_TYPE
*);
4523 static void decode_internal (const struct real_format
*,
4524 REAL_VALUE_TYPE
*, const long *);
4527 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4528 const REAL_VALUE_TYPE
*r
)
4530 memcpy (buf
, r
, sizeof (*r
));
4534 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4535 REAL_VALUE_TYPE
*r
, const long *buf
)
4537 memcpy (r
, buf
, sizeof (*r
));
4540 const struct real_format real_internal_format
=
4545 SIGNIFICAND_BITS
- 2,
4546 SIGNIFICAND_BITS
- 2,
4561 /* Calculate the square root of X in mode MODE, and store the result
4562 in R. Return TRUE if the operation does not raise an exception.
4563 For details see "High Precision Division and Square Root",
4564 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4565 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4568 real_sqrt (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4569 const REAL_VALUE_TYPE
*x
)
4571 static REAL_VALUE_TYPE halfthree
;
4572 static bool init
= false;
4573 REAL_VALUE_TYPE h
, t
, i
;
4576 /* sqrt(-0.0) is -0.0. */
4577 if (real_isnegzero (x
))
4583 /* Negative arguments return NaN. */
4586 get_canonical_qnan (r
, 0);
4590 /* Infinity and NaN return themselves. */
4591 if (!real_isfinite (x
))
4599 do_add (&halfthree
, &dconst1
, &dconsthalf
, 0);
4603 /* Initial guess for reciprocal sqrt, i. */
4604 exp
= real_exponent (x
);
4605 real_ldexp (&i
, &dconst1
, -exp
/2);
4607 /* Newton's iteration for reciprocal sqrt, i. */
4608 for (iter
= 0; iter
< 16; iter
++)
4610 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4611 do_multiply (&t
, x
, &i
);
4612 do_multiply (&h
, &t
, &i
);
4613 do_multiply (&t
, &h
, &dconsthalf
);
4614 do_add (&h
, &halfthree
, &t
, 1);
4615 do_multiply (&t
, &i
, &h
);
4617 /* Check for early convergence. */
4618 if (iter
>= 6 && real_identical (&i
, &t
))
4621 /* ??? Unroll loop to avoid copying. */
4625 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4626 do_multiply (&t
, x
, &i
);
4627 do_multiply (&h
, &t
, &i
);
4628 do_add (&i
, &dconst1
, &h
, 1);
4629 do_multiply (&h
, &t
, &i
);
4630 do_multiply (&i
, &dconsthalf
, &h
);
4631 do_add (&h
, &t
, &i
, 0);
4633 /* ??? We need a Tuckerman test to get the last bit. */
4635 real_convert (r
, mode
, &h
);
4639 /* Calculate X raised to the integer exponent N in mode MODE and store
4640 the result in R. Return true if the result may be inexact due to
4641 loss of precision. The algorithm is the classic "left-to-right binary
4642 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4643 Algorithms", "The Art of Computer Programming", Volume 2. */
4646 real_powi (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4647 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4649 unsigned HOST_WIDE_INT bit
;
4651 bool inexact
= false;
4663 /* Don't worry about overflow, from now on n is unsigned. */
4671 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4672 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4676 inexact
|= do_multiply (&t
, &t
, &t
);
4678 inexact
|= do_multiply (&t
, &t
, x
);
4686 inexact
|= do_divide (&t
, &dconst1
, &t
);
4688 real_convert (r
, mode
, &t
);
4692 /* Round X to the nearest integer not larger in absolute value, i.e.
4693 towards zero, placing the result in R in mode MODE. */
4696 real_trunc (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4697 const REAL_VALUE_TYPE
*x
)
4699 do_fix_trunc (r
, x
);
4700 if (mode
!= VOIDmode
)
4701 real_convert (r
, mode
, r
);
4704 /* Round X to the largest integer not greater in value, i.e. round
4705 down, placing the result in R in mode MODE. */
4708 real_floor (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4709 const REAL_VALUE_TYPE
*x
)
4713 do_fix_trunc (&t
, x
);
4714 if (! real_identical (&t
, x
) && x
->sign
)
4715 do_add (&t
, &t
, &dconstm1
, 0);
4716 if (mode
!= VOIDmode
)
4717 real_convert (r
, mode
, &t
);
4722 /* Round X to the smallest integer not less then argument, i.e. round
4723 up, placing the result in R in mode MODE. */
4726 real_ceil (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4727 const REAL_VALUE_TYPE
*x
)
4731 do_fix_trunc (&t
, x
);
4732 if (! real_identical (&t
, x
) && ! x
->sign
)
4733 do_add (&t
, &t
, &dconst1
, 0);
4734 if (mode
!= VOIDmode
)
4735 real_convert (r
, mode
, &t
);
4740 /* Round X to the nearest integer, but round halfway cases away from
4744 real_round (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4745 const REAL_VALUE_TYPE
*x
)
4747 do_add (r
, x
, &dconsthalf
, x
->sign
);
4748 do_fix_trunc (r
, r
);
4749 if (mode
!= VOIDmode
)
4750 real_convert (r
, mode
, r
);
4753 /* Set the sign of R to the sign of X. */
4756 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4761 /* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible
4762 for initializing and clearing the MPFR parameter. */
4765 mpfr_from_real (mpfr_ptr m
, const REAL_VALUE_TYPE
*r
, mp_rnd_t rndmode
)
4767 /* We use a string as an intermediate type. */
4771 /* Take care of Infinity and NaN. */
4772 if (r
->cl
== rvc_inf
)
4774 mpfr_set_inf (m
, r
->sign
== 1 ? -1 : 1);
4778 if (r
->cl
== rvc_nan
)
4784 real_to_hexadecimal (buf
, r
, sizeof (buf
), 0, 1);
4785 /* mpfr_set_str() parses hexadecimal floats from strings in the same
4786 format that GCC will output them. Nothing extra is needed. */
4787 ret
= mpfr_set_str (m
, buf
, 16, rndmode
);
4788 gcc_assert (ret
== 0);
4791 /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
4792 mode RNDMODE. TYPE is only relevant if M is a NaN. */
4795 real_from_mpfr (REAL_VALUE_TYPE
*r
, mpfr_srcptr m
, tree type
, mp_rnd_t rndmode
)
4797 /* We use a string as an intermediate type. */
4798 char buf
[128], *rstr
;
4801 /* Take care of Infinity and NaN. */
4805 if (mpfr_sgn (m
) < 0)
4806 *r
= REAL_VALUE_NEGATE (*r
);
4812 real_nan (r
, "", 1, TYPE_MODE (type
));
4816 rstr
= mpfr_get_str (NULL
, &exp
, 16, 0, m
, rndmode
);
4818 /* The additional 12 chars add space for the sprintf below. This
4819 leaves 6 digits for the exponent which is supposedly enough. */
4820 gcc_assert (rstr
!= NULL
&& strlen (rstr
) < sizeof (buf
) - 12);
4822 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
4823 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
4828 sprintf (buf
, "-0x.%sp%d", &rstr
[1], (int) exp
);
4830 sprintf (buf
, "0x.%sp%d", rstr
, (int) exp
);
4832 mpfr_free_str (rstr
);
4834 real_from_string (r
, buf
);
4837 /* Check whether the real constant value given is an integer. */
4840 real_isinteger (const REAL_VALUE_TYPE
*c
, enum machine_mode mode
)
4842 REAL_VALUE_TYPE cint
;
4844 real_trunc (&cint
, mode
, c
);
4845 return real_identical (c
, &cint
);
4848 /* Write into BUF the maximum representable finite floating-point
4849 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4850 float string. LEN is the size of BUF, and the buffer must be large
4851 enough to contain the resulting string. */
4854 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
4859 strcpy (buf
, "0x0.");
4861 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
4864 *p
++ = "08ce"[n
- i
];
4865 sprintf (p
, "p%d", fmt
->emax
);
4866 if (fmt
->pnan
< fmt
->p
)
4868 /* This is an IBM extended double format made up of two IEEE
4869 doubles. The value of the long double is the sum of the
4870 values of the two parts. The most significant part is
4871 required to be the value of the long double rounded to the
4872 nearest double. Rounding means we need a slightly smaller
4873 value for LDBL_MAX. */
4874 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
4877 gcc_assert (strlen (buf
) < len
);