1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2002, 2003, 2004, 2005, 2007, 2008 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_inf
, rvc_zero
):
909 case CLASS2 (rvc_inf
, rvc_normal
):
910 case CLASS2 (rvc_normal
, rvc_zero
):
911 return (a
->sign
? -1 : 1);
913 case CLASS2 (rvc_inf
, rvc_inf
):
914 return -a
->sign
- -b
->sign
;
916 case CLASS2 (rvc_zero
, rvc_normal
):
917 case CLASS2 (rvc_zero
, rvc_inf
):
918 case CLASS2 (rvc_normal
, rvc_inf
):
919 return (b
->sign
? 1 : -1);
921 case CLASS2 (rvc_zero
, rvc_nan
):
922 case CLASS2 (rvc_normal
, rvc_nan
):
923 case CLASS2 (rvc_inf
, rvc_nan
):
924 case CLASS2 (rvc_nan
, rvc_nan
):
925 case CLASS2 (rvc_nan
, rvc_zero
):
926 case CLASS2 (rvc_nan
, rvc_normal
):
927 case CLASS2 (rvc_nan
, rvc_inf
):
930 case CLASS2 (rvc_normal
, rvc_normal
):
937 if (a
->sign
!= b
->sign
)
938 return -a
->sign
- -b
->sign
;
940 if (a
->decimal
|| b
->decimal
)
941 return decimal_do_compare (a
, b
, nan_result
);
943 if (REAL_EXP (a
) > REAL_EXP (b
))
945 else if (REAL_EXP (a
) < REAL_EXP (b
))
948 ret
= cmp_significands (a
, b
);
950 return (a
->sign
? -ret
: ret
);
953 /* Return A truncated to an integral value toward zero. */
956 do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
970 decimal_do_fix_trunc (r
, a
);
973 if (REAL_EXP (r
) <= 0)
974 get_zero (r
, r
->sign
);
975 else if (REAL_EXP (r
) < SIGNIFICAND_BITS
)
976 clear_significand_below (r
, SIGNIFICAND_BITS
- REAL_EXP (r
));
984 /* Perform the binary or unary operation described by CODE.
985 For a unary operation, leave OP1 NULL. This function returns
986 true if the result may be inexact due to loss of precision. */
989 real_arithmetic (REAL_VALUE_TYPE
*r
, int icode
, const REAL_VALUE_TYPE
*op0
,
990 const REAL_VALUE_TYPE
*op1
)
992 enum tree_code code
= icode
;
994 if (op0
->decimal
|| (op1
&& op1
->decimal
))
995 return decimal_real_arithmetic (r
, icode
, op0
, op1
);
1000 return do_add (r
, op0
, op1
, 0);
1003 return do_add (r
, op0
, op1
, 1);
1006 return do_multiply (r
, op0
, op1
);
1009 return do_divide (r
, op0
, op1
);
1012 if (op1
->cl
== rvc_nan
)
1014 else if (do_compare (op0
, op1
, -1) < 0)
1021 if (op1
->cl
== rvc_nan
)
1023 else if (do_compare (op0
, op1
, 1) < 0)
1039 case FIX_TRUNC_EXPR
:
1040 do_fix_trunc (r
, op0
);
1049 /* Legacy. Similar, but return the result directly. */
1052 real_arithmetic2 (int icode
, const REAL_VALUE_TYPE
*op0
,
1053 const REAL_VALUE_TYPE
*op1
)
1056 real_arithmetic (&r
, icode
, op0
, op1
);
1061 real_compare (int icode
, const REAL_VALUE_TYPE
*op0
,
1062 const REAL_VALUE_TYPE
*op1
)
1064 enum tree_code code
= icode
;
1069 return do_compare (op0
, op1
, 1) < 0;
1071 return do_compare (op0
, op1
, 1) <= 0;
1073 return do_compare (op0
, op1
, -1) > 0;
1075 return do_compare (op0
, op1
, -1) >= 0;
1077 return do_compare (op0
, op1
, -1) == 0;
1079 return do_compare (op0
, op1
, -1) != 0;
1080 case UNORDERED_EXPR
:
1081 return op0
->cl
== rvc_nan
|| op1
->cl
== rvc_nan
;
1083 return op0
->cl
!= rvc_nan
&& op1
->cl
!= rvc_nan
;
1085 return do_compare (op0
, op1
, -1) < 0;
1087 return do_compare (op0
, op1
, -1) <= 0;
1089 return do_compare (op0
, op1
, 1) > 0;
1091 return do_compare (op0
, op1
, 1) >= 0;
1093 return do_compare (op0
, op1
, 0) == 0;
1095 return do_compare (op0
, op1
, 0) != 0;
1102 /* Return floor log2(R). */
1105 real_exponent (const REAL_VALUE_TYPE
*r
)
1113 return (unsigned int)-1 >> 1;
1115 return REAL_EXP (r
);
1121 /* R = OP0 * 2**EXP. */
1124 real_ldexp (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
, int exp
)
1135 exp
+= REAL_EXP (op0
);
1137 get_inf (r
, r
->sign
);
1138 else if (exp
< -MAX_EXP
)
1139 get_zero (r
, r
->sign
);
1141 SET_REAL_EXP (r
, exp
);
1149 /* Determine whether a floating-point value X is infinite. */
1152 real_isinf (const REAL_VALUE_TYPE
*r
)
1154 return (r
->cl
== rvc_inf
);
1157 /* Determine whether a floating-point value X is a NaN. */
1160 real_isnan (const REAL_VALUE_TYPE
*r
)
1162 return (r
->cl
== rvc_nan
);
1165 /* Determine whether a floating-point value X is finite. */
1168 real_isfinite (const REAL_VALUE_TYPE
*r
)
1170 return (r
->cl
!= rvc_nan
) && (r
->cl
!= rvc_inf
);
1173 /* Determine whether a floating-point value X is negative. */
1176 real_isneg (const REAL_VALUE_TYPE
*r
)
1181 /* Determine whether a floating-point value X is minus zero. */
1184 real_isnegzero (const REAL_VALUE_TYPE
*r
)
1186 return r
->sign
&& r
->cl
== rvc_zero
;
1189 /* Compare two floating-point objects for bitwise identity. */
1192 real_identical (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
)
1198 if (a
->sign
!= b
->sign
)
1208 if (a
->decimal
!= b
->decimal
)
1210 if (REAL_EXP (a
) != REAL_EXP (b
))
1215 if (a
->signalling
!= b
->signalling
)
1217 /* The significand is ignored for canonical NaNs. */
1218 if (a
->canonical
|| b
->canonical
)
1219 return a
->canonical
== b
->canonical
;
1226 for (i
= 0; i
< SIGSZ
; ++i
)
1227 if (a
->sig
[i
] != b
->sig
[i
])
1233 /* Try to change R into its exact multiplicative inverse in machine
1234 mode MODE. Return true if successful. */
1237 exact_real_inverse (enum machine_mode mode
, REAL_VALUE_TYPE
*r
)
1239 const REAL_VALUE_TYPE
*one
= real_digit (1);
1243 if (r
->cl
!= rvc_normal
)
1246 /* Check for a power of two: all significand bits zero except the MSB. */
1247 for (i
= 0; i
< SIGSZ
-1; ++i
)
1250 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1253 /* Find the inverse and truncate to the required mode. */
1254 do_divide (&u
, one
, r
);
1255 real_convert (&u
, mode
, &u
);
1257 /* The rounding may have overflowed. */
1258 if (u
.cl
!= rvc_normal
)
1260 for (i
= 0; i
< SIGSZ
-1; ++i
)
1263 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1270 /* Render R as an integer. */
1273 real_to_integer (const REAL_VALUE_TYPE
*r
)
1275 unsigned HOST_WIDE_INT i
;
1286 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1293 return decimal_real_to_integer (r
);
1295 if (REAL_EXP (r
) <= 0)
1297 /* Only force overflow for unsigned overflow. Signed overflow is
1298 undefined, so it doesn't matter what we return, and some callers
1299 expect to be able to use this routine for both signed and
1300 unsigned conversions. */
1301 if (REAL_EXP (r
) > HOST_BITS_PER_WIDE_INT
)
1304 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1305 i
= r
->sig
[SIGSZ
-1];
1308 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2 * HOST_BITS_PER_LONG
);
1309 i
= r
->sig
[SIGSZ
-1];
1310 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1311 i
|= r
->sig
[SIGSZ
-2];
1314 i
>>= HOST_BITS_PER_WIDE_INT
- REAL_EXP (r
);
1325 /* Likewise, but to an integer pair, HI+LOW. */
1328 real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
1329 const REAL_VALUE_TYPE
*r
)
1332 HOST_WIDE_INT low
, high
;
1345 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1358 decimal_real_to_integer2 (plow
, phigh
, r
);
1365 /* Only force overflow for unsigned overflow. Signed overflow is
1366 undefined, so it doesn't matter what we return, and some callers
1367 expect to be able to use this routine for both signed and
1368 unsigned conversions. */
1369 if (exp
> 2*HOST_BITS_PER_WIDE_INT
)
1372 rshift_significand (&t
, r
, 2*HOST_BITS_PER_WIDE_INT
- exp
);
1373 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1375 high
= t
.sig
[SIGSZ
-1];
1376 low
= t
.sig
[SIGSZ
-2];
1380 gcc_assert (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
);
1381 high
= t
.sig
[SIGSZ
-1];
1382 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1383 high
|= t
.sig
[SIGSZ
-2];
1385 low
= t
.sig
[SIGSZ
-3];
1386 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1387 low
|= t
.sig
[SIGSZ
-4];
1395 low
= -low
, high
= ~high
;
1407 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1408 of NUM / DEN. Return the quotient and place the remainder in NUM.
1409 It is expected that NUM / DEN are close enough that the quotient is
1412 static unsigned long
1413 rtd_divmod (REAL_VALUE_TYPE
*num
, REAL_VALUE_TYPE
*den
)
1415 unsigned long q
, msb
;
1416 int expn
= REAL_EXP (num
), expd
= REAL_EXP (den
);
1425 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1427 lshift_significand_1 (num
, num
);
1429 if (msb
|| cmp_significands (num
, den
) >= 0)
1431 sub_significands (num
, num
, den
, 0);
1435 while (--expn
>= expd
);
1437 SET_REAL_EXP (num
, expd
);
1443 /* Render R as a decimal floating point constant. Emit DIGITS significant
1444 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1445 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1448 #define M_LOG10_2 0.30102999566398119521
1451 real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
, size_t buf_size
,
1452 size_t digits
, int crop_trailing_zeros
)
1454 const REAL_VALUE_TYPE
*one
, *ten
;
1455 REAL_VALUE_TYPE r
, pten
, u
, v
;
1456 int dec_exp
, cmp_one
, digit
;
1458 char *p
, *first
, *last
;
1465 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1470 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1473 /* ??? Print the significand as well, if not canonical? */
1474 sprintf (str
, "%c%cNaN", (r_orig
->sign
? '-' : '+'),
1475 (r_orig
->signalling
? 'S' : 'Q'));
1483 decimal_real_to_decimal (str
, &r
, buf_size
, digits
, crop_trailing_zeros
);
1487 /* Bound the number of digits printed by the size of the representation. */
1488 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1489 if (digits
== 0 || digits
> max_digits
)
1490 digits
= max_digits
;
1492 /* Estimate the decimal exponent, and compute the length of the string it
1493 will print as. Be conservative and add one to account for possible
1494 overflow or rounding error. */
1495 dec_exp
= REAL_EXP (&r
) * M_LOG10_2
;
1496 for (max_digits
= 1; dec_exp
; max_digits
++)
1499 /* Bound the number of digits printed by the size of the output buffer. */
1500 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1501 gcc_assert (max_digits
<= buf_size
);
1502 if (digits
> max_digits
)
1503 digits
= max_digits
;
1505 one
= real_digit (1);
1506 ten
= ten_to_ptwo (0);
1514 cmp_one
= do_compare (&r
, one
, 0);
1519 /* Number is greater than one. Convert significand to an integer
1520 and strip trailing decimal zeros. */
1523 SET_REAL_EXP (&u
, SIGNIFICAND_BITS
- 1);
1525 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1526 m
= floor_log2 (max_digits
);
1528 /* Iterate over the bits of the possible powers of 10 that might
1529 be present in U and eliminate them. That is, if we find that
1530 10**2**M divides U evenly, keep the division and increase
1536 do_divide (&t
, &u
, ten_to_ptwo (m
));
1537 do_fix_trunc (&v
, &t
);
1538 if (cmp_significands (&v
, &t
) == 0)
1546 /* Revert the scaling to integer that we performed earlier. */
1547 SET_REAL_EXP (&u
, REAL_EXP (&u
) + REAL_EXP (&r
)
1548 - (SIGNIFICAND_BITS
- 1));
1551 /* Find power of 10. Do this by dividing out 10**2**M when
1552 this is larger than the current remainder. Fill PTEN with
1553 the power of 10 that we compute. */
1554 if (REAL_EXP (&r
) > 0)
1556 m
= floor_log2 ((int)(REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1559 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1560 if (do_compare (&u
, ptentwo
, 0) >= 0)
1562 do_divide (&u
, &u
, ptentwo
);
1563 do_multiply (&pten
, &pten
, ptentwo
);
1570 /* We managed to divide off enough tens in the above reduction
1571 loop that we've now got a negative exponent. Fall into the
1572 less-than-one code to compute the proper value for PTEN. */
1579 /* Number is less than one. Pad significand with leading
1585 /* Stop if we'd shift bits off the bottom. */
1589 do_multiply (&u
, &v
, ten
);
1591 /* Stop if we're now >= 1. */
1592 if (REAL_EXP (&u
) > 0)
1600 /* Find power of 10. Do this by multiplying in P=10**2**M when
1601 the current remainder is smaller than 1/P. Fill PTEN with the
1602 power of 10 that we compute. */
1603 m
= floor_log2 ((int)(-REAL_EXP (&r
) * M_LOG10_2
)) + 1;
1606 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1607 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1609 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1611 do_multiply (&v
, &v
, ptentwo
);
1612 do_multiply (&pten
, &pten
, ptentwo
);
1618 /* Invert the positive power of 10 that we've collected so far. */
1619 do_divide (&pten
, one
, &pten
);
1627 /* At this point, PTEN should contain the nearest power of 10 smaller
1628 than R, such that this division produces the first digit.
1630 Using a divide-step primitive that returns the complete integral
1631 remainder avoids the rounding error that would be produced if
1632 we were to use do_divide here and then simply multiply by 10 for
1633 each subsequent digit. */
1635 digit
= rtd_divmod (&r
, &pten
);
1637 /* Be prepared for error in that division via underflow ... */
1638 if (digit
== 0 && cmp_significand_0 (&r
))
1640 /* Multiply by 10 and try again. */
1641 do_multiply (&r
, &r
, ten
);
1642 digit
= rtd_divmod (&r
, &pten
);
1644 gcc_assert (digit
!= 0);
1647 /* ... or overflow. */
1657 gcc_assert (digit
<= 10);
1661 /* Generate subsequent digits. */
1662 while (--digits
> 0)
1664 do_multiply (&r
, &r
, ten
);
1665 digit
= rtd_divmod (&r
, &pten
);
1670 /* Generate one more digit with which to do rounding. */
1671 do_multiply (&r
, &r
, ten
);
1672 digit
= rtd_divmod (&r
, &pten
);
1674 /* Round the result. */
1677 /* Round to nearest. If R is nonzero there are additional
1678 nonzero digits to be extracted. */
1679 if (cmp_significand_0 (&r
))
1681 /* Round to even. */
1682 else if ((p
[-1] - '0') & 1)
1699 /* Carry out of the first digit. This means we had all 9's and
1700 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1708 /* Insert the decimal point. */
1709 first
[0] = first
[1];
1712 /* If requested, drop trailing zeros. Never crop past "1.0". */
1713 if (crop_trailing_zeros
)
1714 while (last
> first
+ 3 && last
[-1] == '0')
1717 /* Append the exponent. */
1718 sprintf (last
, "e%+d", dec_exp
);
1721 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1722 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1723 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1724 strip trailing zeros. */
1727 real_to_hexadecimal (char *str
, const REAL_VALUE_TYPE
*r
, size_t buf_size
,
1728 size_t digits
, int crop_trailing_zeros
)
1730 int i
, j
, exp
= REAL_EXP (r
);
1743 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1746 /* ??? Print the significand as well, if not canonical? */
1747 sprintf (str
, "%c%cNaN", (r
->sign
? '-' : '+'),
1748 (r
->signalling
? 'S' : 'Q'));
1756 /* Hexadecimal format for decimal floats is not interesting. */
1757 strcpy (str
, "N/A");
1762 digits
= SIGNIFICAND_BITS
/ 4;
1764 /* Bound the number of digits printed by the size of the output buffer. */
1766 sprintf (exp_buf
, "p%+d", exp
);
1767 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1768 gcc_assert (max_digits
<= buf_size
);
1769 if (digits
> max_digits
)
1770 digits
= max_digits
;
1781 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1782 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1784 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1790 if (crop_trailing_zeros
)
1791 while (p
> first
+ 1 && p
[-1] == '0')
1794 sprintf (p
, "p%+d", exp
);
1797 /* Initialize R from a decimal or hexadecimal string. The string is
1798 assumed to have been syntax checked already. Return -1 if the
1799 value underflows, +1 if overflows, and 0 otherwise. */
1802 real_from_string (REAL_VALUE_TYPE
*r
, const char *str
)
1814 else if (*str
== '+')
1817 if (!strncmp (str
, "QNaN", 4))
1819 get_canonical_qnan (r
, sign
);
1822 else if (!strncmp (str
, "SNaN", 4))
1824 get_canonical_snan (r
, sign
);
1827 else if (!strncmp (str
, "Inf", 3))
1833 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
1835 /* Hexadecimal floating point. */
1836 int pos
= SIGNIFICAND_BITS
- 4, d
;
1844 d
= hex_value (*str
);
1849 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1850 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1854 /* Ensure correct rounding by setting last bit if there is
1855 a subsequent nonzero digit. */
1863 if (pos
== SIGNIFICAND_BITS
- 4)
1870 d
= hex_value (*str
);
1875 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1876 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1880 /* Ensure correct rounding by setting last bit if there is
1881 a subsequent nonzero digit. */
1887 /* If the mantissa is zero, ignore the exponent. */
1888 if (!cmp_significand_0 (r
))
1891 if (*str
== 'p' || *str
== 'P')
1893 bool exp_neg
= false;
1901 else if (*str
== '+')
1905 while (ISDIGIT (*str
))
1911 /* Overflowed the exponent. */
1926 SET_REAL_EXP (r
, exp
);
1932 /* Decimal floating point. */
1933 const REAL_VALUE_TYPE
*ten
= ten_to_ptwo (0);
1938 while (ISDIGIT (*str
))
1941 do_multiply (r
, r
, ten
);
1943 do_add (r
, r
, real_digit (d
), 0);
1948 if (r
->cl
== rvc_zero
)
1953 while (ISDIGIT (*str
))
1956 do_multiply (r
, r
, ten
);
1958 do_add (r
, r
, real_digit (d
), 0);
1963 /* If the mantissa is zero, ignore the exponent. */
1964 if (r
->cl
== rvc_zero
)
1967 if (*str
== 'e' || *str
== 'E')
1969 bool exp_neg
= false;
1977 else if (*str
== '+')
1981 while (ISDIGIT (*str
))
1987 /* Overflowed the exponent. */
2001 times_pten (r
, exp
);
2020 /* Legacy. Similar, but return the result directly. */
2023 real_from_string2 (const char *s
, enum machine_mode mode
)
2027 real_from_string (&r
, s
);
2028 if (mode
!= VOIDmode
)
2029 real_convert (&r
, mode
, &r
);
2034 /* Initialize R from string S and desired MODE. */
2037 real_from_string3 (REAL_VALUE_TYPE
*r
, const char *s
, enum machine_mode mode
)
2039 if (DECIMAL_FLOAT_MODE_P (mode
))
2040 decimal_real_from_string (r
, s
);
2042 real_from_string (r
, s
);
2044 if (mode
!= VOIDmode
)
2045 real_convert (r
, mode
, r
);
2048 /* Initialize R from the integer pair HIGH+LOW. */
2051 real_from_integer (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2052 unsigned HOST_WIDE_INT low
, HOST_WIDE_INT high
,
2055 if (low
== 0 && high
== 0)
2059 memset (r
, 0, sizeof (*r
));
2061 r
->sign
= high
< 0 && !unsigned_p
;
2062 SET_REAL_EXP (r
, 2 * HOST_BITS_PER_WIDE_INT
);
2073 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2075 r
->sig
[SIGSZ
-1] = high
;
2076 r
->sig
[SIGSZ
-2] = low
;
2080 gcc_assert (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
);
2081 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2082 r
->sig
[SIGSZ
-2] = high
;
2083 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2084 r
->sig
[SIGSZ
-4] = low
;
2090 if (mode
!= VOIDmode
)
2091 real_convert (r
, mode
, r
);
2094 /* Returns 10**2**N. */
2096 static const REAL_VALUE_TYPE
*
2099 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2101 gcc_assert (n
>= 0);
2102 gcc_assert (n
< EXP_BITS
);
2104 if (tens
[n
].cl
== rvc_zero
)
2106 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2108 HOST_WIDE_INT t
= 10;
2111 for (i
= 0; i
< n
; ++i
)
2114 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2118 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2119 do_multiply (&tens
[n
], t
, t
);
2126 /* Returns 10**(-2**N). */
2128 static const REAL_VALUE_TYPE
*
2129 ten_to_mptwo (int n
)
2131 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2133 gcc_assert (n
>= 0);
2134 gcc_assert (n
< EXP_BITS
);
2136 if (tens
[n
].cl
== rvc_zero
)
2137 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2144 static const REAL_VALUE_TYPE
*
2147 static REAL_VALUE_TYPE num
[10];
2149 gcc_assert (n
>= 0);
2150 gcc_assert (n
<= 9);
2152 if (n
> 0 && num
[n
].cl
== rvc_zero
)
2153 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2158 /* Multiply R by 10**EXP. */
2161 times_pten (REAL_VALUE_TYPE
*r
, int exp
)
2163 REAL_VALUE_TYPE pten
, *rr
;
2164 bool negative
= (exp
< 0);
2170 pten
= *real_digit (1);
2176 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2178 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2181 do_divide (r
, r
, &pten
);
2184 /* Returns the special REAL_VALUE_TYPE enumerated by E. */
2186 const REAL_VALUE_TYPE
*
2187 get_real_const (enum real_value_const e
)
2189 static REAL_VALUE_TYPE value
[rv_max
];
2191 gcc_assert (e
< rv_max
);
2193 /* Initialize mathematical constants for constant folding builtins.
2194 These constants need to be given to at least 160 bits precision. */
2195 if (value
[e
].cl
== rvc_zero
)
2201 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2202 mpfr_set_ui (m
, 1, GMP_RNDN
);
2203 mpfr_exp (m
, m
, GMP_RNDN
);
2204 real_from_mpfr (&value
[e
], m
, NULL_TREE
, GMP_RNDN
);
2209 real_arithmetic (&value
[e
], RDIV_EXPR
, &dconst1
, real_digit (3));
2214 mpfr_init2 (m
, SIGNIFICAND_BITS
);
2215 mpfr_sqrt_ui (m
, 2, GMP_RNDN
);
2216 real_from_mpfr (&value
[e
], m
, NULL_TREE
, GMP_RNDN
);
2227 /* Fills R with +Inf. */
2230 real_inf (REAL_VALUE_TYPE
*r
)
2235 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2236 we force a QNaN, else we force an SNaN. The string, if not empty,
2237 is parsed as a number and placed in the significand. Return true
2238 if the string was successfully parsed. */
2241 real_nan (REAL_VALUE_TYPE
*r
, const char *str
, int quiet
,
2242 enum machine_mode mode
)
2244 const struct real_format
*fmt
;
2246 fmt
= REAL_MODE_FORMAT (mode
);
2252 get_canonical_qnan (r
, 0);
2254 get_canonical_snan (r
, 0);
2260 memset (r
, 0, sizeof (*r
));
2263 /* Parse akin to strtol into the significand of R. */
2265 while (ISSPACE (*str
))
2269 else if (*str
== '+')
2274 if (*str
== 'x' || *str
== 'X')
2283 while ((d
= hex_value (*str
)) < base
)
2290 lshift_significand (r
, r
, 3);
2293 lshift_significand (r
, r
, 4);
2296 lshift_significand_1 (&u
, r
);
2297 lshift_significand (r
, r
, 3);
2298 add_significands (r
, r
, &u
);
2306 add_significands (r
, r
, &u
);
2311 /* Must have consumed the entire string for success. */
2315 /* Shift the significand into place such that the bits
2316 are in the most significant bits for the format. */
2317 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2319 /* Our MSB is always unset for NaNs. */
2320 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2322 /* Force quiet or signalling NaN. */
2323 r
->signalling
= !quiet
;
2329 /* Fills R with the largest finite value representable in mode MODE.
2330 If SIGN is nonzero, R is set to the most negative finite value. */
2333 real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
2335 const struct real_format
*fmt
;
2338 fmt
= REAL_MODE_FORMAT (mode
);
2340 memset (r
, 0, sizeof (*r
));
2343 decimal_real_maxval (r
, sign
, mode
);
2348 SET_REAL_EXP (r
, fmt
->emax
);
2350 np2
= SIGNIFICAND_BITS
- fmt
->p
;
2351 memset (r
->sig
, -1, SIGSZ
* sizeof (unsigned long));
2352 clear_significand_below (r
, np2
);
2354 if (fmt
->pnan
< fmt
->p
)
2355 /* This is an IBM extended double format made up of two IEEE
2356 doubles. The value of the long double is the sum of the
2357 values of the two parts. The most significant part is
2358 required to be the value of the long double rounded to the
2359 nearest double. Rounding means we need a slightly smaller
2360 value for LDBL_MAX. */
2361 clear_significand_bit (r
, SIGNIFICAND_BITS
- fmt
->pnan
);
2365 /* Fills R with 2**N. */
2368 real_2expN (REAL_VALUE_TYPE
*r
, int n
, enum machine_mode fmode
)
2370 memset (r
, 0, sizeof (*r
));
2375 else if (n
< -MAX_EXP
)
2380 SET_REAL_EXP (r
, n
);
2381 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2383 if (DECIMAL_FLOAT_MODE_P (fmode
))
2384 decimal_real_convert (r
, fmode
, r
);
2389 round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
2392 unsigned long sticky
;
2400 decimal_round_for_format (fmt
, r
);
2403 /* FIXME. We can come here via fp_easy_constant
2404 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2405 investigated whether this convert needs to be here, or
2406 something else is missing. */
2407 decimal_real_convert (r
, DFmode
, r
);
2411 emin2m1
= fmt
->emin
- 1;
2414 np2
= SIGNIFICAND_BITS
- p2
;
2418 get_zero (r
, r
->sign
);
2420 if (!fmt
->has_signed_zero
)
2425 get_inf (r
, r
->sign
);
2430 clear_significand_below (r
, np2
);
2440 /* Check the range of the exponent. If we're out of range,
2441 either underflow or overflow. */
2442 if (REAL_EXP (r
) > emax2
)
2444 else if (REAL_EXP (r
) <= emin2m1
)
2448 if (!fmt
->has_denorm
)
2450 /* Don't underflow completely until we've had a chance to round. */
2451 if (REAL_EXP (r
) < emin2m1
)
2456 diff
= emin2m1
- REAL_EXP (r
) + 1;
2460 /* De-normalize the significand. */
2461 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2462 SET_REAL_EXP (r
, REAL_EXP (r
) + diff
);
2466 /* There are P2 true significand bits, followed by one guard bit,
2467 followed by one sticky bit, followed by stuff. Fold nonzero
2468 stuff into the sticky bit. */
2471 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2472 sticky
|= r
->sig
[i
];
2474 r
->sig
[w
] & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2476 guard
= test_significand_bit (r
, np2
- 1);
2477 lsb
= test_significand_bit (r
, np2
);
2479 /* Round to even. */
2480 if (guard
&& (sticky
|| lsb
))
2484 set_significand_bit (&u
, np2
);
2486 if (add_significands (r
, r
, &u
))
2488 /* Overflow. Means the significand had been all ones, and
2489 is now all zeros. Need to increase the exponent, and
2490 possibly re-normalize it. */
2491 SET_REAL_EXP (r
, REAL_EXP (r
) + 1);
2492 if (REAL_EXP (r
) > emax2
)
2494 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2498 /* Catch underflow that we deferred until after rounding. */
2499 if (REAL_EXP (r
) <= emin2m1
)
2502 /* Clear out trailing garbage. */
2503 clear_significand_below (r
, np2
);
2506 /* Extend or truncate to a new mode. */
2509 real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
2510 const REAL_VALUE_TYPE
*a
)
2512 const struct real_format
*fmt
;
2514 fmt
= REAL_MODE_FORMAT (mode
);
2519 if (a
->decimal
|| fmt
->b
== 10)
2520 decimal_real_convert (r
, mode
, a
);
2522 round_for_format (fmt
, r
);
2524 /* round_for_format de-normalizes denormals. Undo just that part. */
2525 if (r
->cl
== rvc_normal
)
2529 /* Legacy. Likewise, except return the struct directly. */
2532 real_value_truncate (enum machine_mode mode
, REAL_VALUE_TYPE a
)
2535 real_convert (&r
, mode
, &a
);
2539 /* Return true if truncating to MODE is exact. */
2542 exact_real_truncate (enum machine_mode mode
, const REAL_VALUE_TYPE
*a
)
2544 const struct real_format
*fmt
;
2548 fmt
= REAL_MODE_FORMAT (mode
);
2551 /* Don't allow conversion to denormals. */
2552 emin2m1
= fmt
->emin
- 1;
2553 if (REAL_EXP (a
) <= emin2m1
)
2556 /* After conversion to the new mode, the value must be identical. */
2557 real_convert (&t
, mode
, a
);
2558 return real_identical (&t
, a
);
2561 /* Write R to the given target format. Place the words of the result
2562 in target word order in BUF. There are always 32 bits in each
2563 long, no matter the size of the host long.
2565 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2568 real_to_target_fmt (long *buf
, const REAL_VALUE_TYPE
*r_orig
,
2569 const struct real_format
*fmt
)
2575 round_for_format (fmt
, &r
);
2579 (*fmt
->encode
) (fmt
, buf
, &r
);
2584 /* Similar, but look up the format from MODE. */
2587 real_to_target (long *buf
, const REAL_VALUE_TYPE
*r
, enum machine_mode mode
)
2589 const struct real_format
*fmt
;
2591 fmt
= REAL_MODE_FORMAT (mode
);
2594 return real_to_target_fmt (buf
, r
, fmt
);
2597 /* Read R from the given target format. Read the words of the result
2598 in target word order in BUF. There are always 32 bits in each
2599 long, no matter the size of the host long. */
2602 real_from_target_fmt (REAL_VALUE_TYPE
*r
, const long *buf
,
2603 const struct real_format
*fmt
)
2605 (*fmt
->decode
) (fmt
, r
, buf
);
2608 /* Similar, but look up the format from MODE. */
2611 real_from_target (REAL_VALUE_TYPE
*r
, const long *buf
, enum machine_mode mode
)
2613 const struct real_format
*fmt
;
2615 fmt
= REAL_MODE_FORMAT (mode
);
2618 (*fmt
->decode
) (fmt
, r
, buf
);
2621 /* Return the number of bits of the largest binary value that the
2622 significand of MODE will hold. */
2623 /* ??? Legacy. Should get access to real_format directly. */
2626 significand_size (enum machine_mode mode
)
2628 const struct real_format
*fmt
;
2630 fmt
= REAL_MODE_FORMAT (mode
);
2636 /* Return the size in bits of the largest binary value that can be
2637 held by the decimal coefficient for this mode. This is one more
2638 than the number of bits required to hold the largest coefficient
2640 double log2_10
= 3.3219281;
2641 return fmt
->p
* log2_10
;
2646 /* Return a hash value for the given real value. */
2647 /* ??? The "unsigned int" return value is intended to be hashval_t,
2648 but I didn't want to pull hashtab.h into real.h. */
2651 real_hash (const REAL_VALUE_TYPE
*r
)
2656 h
= r
->cl
| (r
->sign
<< 2);
2664 h
|= REAL_EXP (r
) << 3;
2669 h
^= (unsigned int)-1;
2678 if (sizeof(unsigned long) > sizeof(unsigned int))
2679 for (i
= 0; i
< SIGSZ
; ++i
)
2681 unsigned long s
= r
->sig
[i
];
2682 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2685 for (i
= 0; i
< SIGSZ
; ++i
)
2691 /* IEEE single-precision format. */
2693 static void encode_ieee_single (const struct real_format
*fmt
,
2694 long *, const REAL_VALUE_TYPE
*);
2695 static void decode_ieee_single (const struct real_format
*,
2696 REAL_VALUE_TYPE
*, const long *);
2699 encode_ieee_single (const struct real_format
*fmt
, long *buf
,
2700 const REAL_VALUE_TYPE
*r
)
2702 unsigned long image
, sig
, exp
;
2703 unsigned long sign
= r
->sign
;
2704 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2707 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2718 image
|= 0x7fffffff;
2725 sig
= (fmt
->canonical_nan_lsbs_set
? (1 << 22) - 1 : 0);
2726 if (r
->signalling
== fmt
->qnan_msb_set
)
2737 image
|= 0x7fffffff;
2741 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2742 whereas the intermediate representation is 0.F x 2**exp.
2743 Which means we're off by one. */
2747 exp
= REAL_EXP (r
) + 127 - 1;
2760 decode_ieee_single (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2763 unsigned long image
= buf
[0] & 0xffffffff;
2764 bool sign
= (image
>> 31) & 1;
2765 int exp
= (image
>> 23) & 0xff;
2767 memset (r
, 0, sizeof (*r
));
2768 image
<<= HOST_BITS_PER_LONG
- 24;
2773 if (image
&& fmt
->has_denorm
)
2777 SET_REAL_EXP (r
, -126);
2778 r
->sig
[SIGSZ
-1] = image
<< 1;
2781 else if (fmt
->has_signed_zero
)
2784 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2790 r
->signalling
= (((image
>> (HOST_BITS_PER_LONG
- 2)) & 1)
2791 ^ fmt
->qnan_msb_set
);
2792 r
->sig
[SIGSZ
-1] = image
;
2804 SET_REAL_EXP (r
, exp
- 127 + 1);
2805 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2809 const struct real_format ieee_single_format
=
2828 const struct real_format mips_single_format
=
2847 const struct real_format motorola_single_format
=
2866 /* IEEE double-precision format. */
2868 static void encode_ieee_double (const struct real_format
*fmt
,
2869 long *, const REAL_VALUE_TYPE
*);
2870 static void decode_ieee_double (const struct real_format
*,
2871 REAL_VALUE_TYPE
*, const long *);
2874 encode_ieee_double (const struct real_format
*fmt
, long *buf
,
2875 const REAL_VALUE_TYPE
*r
)
2877 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
2878 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2880 image_hi
= r
->sign
<< 31;
2883 if (HOST_BITS_PER_LONG
== 64)
2885 sig_hi
= r
->sig
[SIGSZ
-1];
2886 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
2887 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
2891 sig_hi
= r
->sig
[SIGSZ
-1];
2892 sig_lo
= r
->sig
[SIGSZ
-2];
2893 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
2894 sig_hi
= (sig_hi
>> 11) & 0xfffff;
2904 image_hi
|= 2047 << 20;
2907 image_hi
|= 0x7fffffff;
2908 image_lo
= 0xffffffff;
2917 if (fmt
->canonical_nan_lsbs_set
)
2919 sig_hi
= (1 << 19) - 1;
2920 sig_lo
= 0xffffffff;
2928 if (r
->signalling
== fmt
->qnan_msb_set
)
2929 sig_hi
&= ~(1 << 19);
2932 if (sig_hi
== 0 && sig_lo
== 0)
2935 image_hi
|= 2047 << 20;
2941 image_hi
|= 0x7fffffff;
2942 image_lo
= 0xffffffff;
2947 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2948 whereas the intermediate representation is 0.F x 2**exp.
2949 Which means we're off by one. */
2953 exp
= REAL_EXP (r
) + 1023 - 1;
2954 image_hi
|= exp
<< 20;
2963 if (FLOAT_WORDS_BIG_ENDIAN
)
2964 buf
[0] = image_hi
, buf
[1] = image_lo
;
2966 buf
[0] = image_lo
, buf
[1] = image_hi
;
2970 decode_ieee_double (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
2973 unsigned long image_hi
, image_lo
;
2977 if (FLOAT_WORDS_BIG_ENDIAN
)
2978 image_hi
= buf
[0], image_lo
= buf
[1];
2980 image_lo
= buf
[0], image_hi
= buf
[1];
2981 image_lo
&= 0xffffffff;
2982 image_hi
&= 0xffffffff;
2984 sign
= (image_hi
>> 31) & 1;
2985 exp
= (image_hi
>> 20) & 0x7ff;
2987 memset (r
, 0, sizeof (*r
));
2989 image_hi
<<= 32 - 21;
2990 image_hi
|= image_lo
>> 21;
2991 image_hi
&= 0x7fffffff;
2992 image_lo
<<= 32 - 21;
2996 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
3000 SET_REAL_EXP (r
, -1022);
3001 if (HOST_BITS_PER_LONG
== 32)
3003 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
3005 r
->sig
[SIGSZ
-1] = image_hi
;
3006 r
->sig
[SIGSZ
-2] = image_lo
;
3010 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
3011 r
->sig
[SIGSZ
-1] = image_hi
;
3015 else if (fmt
->has_signed_zero
)
3018 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
3020 if (image_hi
|| image_lo
)
3024 r
->signalling
= ((image_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3025 if (HOST_BITS_PER_LONG
== 32)
3027 r
->sig
[SIGSZ
-1] = image_hi
;
3028 r
->sig
[SIGSZ
-2] = image_lo
;
3031 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
3043 SET_REAL_EXP (r
, exp
- 1023 + 1);
3044 if (HOST_BITS_PER_LONG
== 32)
3046 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
3047 r
->sig
[SIGSZ
-2] = image_lo
;
3050 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
3054 const struct real_format ieee_double_format
=
3073 const struct real_format mips_double_format
=
3092 const struct real_format motorola_double_format
=
3111 /* IEEE extended real format. This comes in three flavors: Intel's as
3112 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3113 12- and 16-byte images may be big- or little endian; Motorola's is
3114 always big endian. */
3116 /* Helper subroutine which converts from the internal format to the
3117 12-byte little-endian Intel format. Functions below adjust this
3118 for the other possible formats. */
3120 encode_ieee_extended (const struct real_format
*fmt
, long *buf
,
3121 const REAL_VALUE_TYPE
*r
)
3123 unsigned long image_hi
, sig_hi
, sig_lo
;
3124 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3126 image_hi
= r
->sign
<< 15;
3127 sig_hi
= sig_lo
= 0;
3139 /* Intel requires the explicit integer bit to be set, otherwise
3140 it considers the value a "pseudo-infinity". Motorola docs
3141 say it doesn't care. */
3142 sig_hi
= 0x80000000;
3147 sig_lo
= sig_hi
= 0xffffffff;
3157 if (fmt
->canonical_nan_lsbs_set
)
3159 sig_hi
= (1 << 30) - 1;
3160 sig_lo
= 0xffffffff;
3163 else if (HOST_BITS_PER_LONG
== 32)
3165 sig_hi
= r
->sig
[SIGSZ
-1];
3166 sig_lo
= r
->sig
[SIGSZ
-2];
3170 sig_lo
= r
->sig
[SIGSZ
-1];
3171 sig_hi
= sig_lo
>> 31 >> 1;
3172 sig_lo
&= 0xffffffff;
3174 if (r
->signalling
== fmt
->qnan_msb_set
)
3175 sig_hi
&= ~(1 << 30);
3178 if ((sig_hi
& 0x7fffffff) == 0 && sig_lo
== 0)
3181 /* Intel requires the explicit integer bit to be set, otherwise
3182 it considers the value a "pseudo-nan". Motorola docs say it
3184 sig_hi
|= 0x80000000;
3189 sig_lo
= sig_hi
= 0xffffffff;
3195 int exp
= REAL_EXP (r
);
3197 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3198 whereas the intermediate representation is 0.F x 2**exp.
3199 Which means we're off by one.
3201 Except for Motorola, which consider exp=0 and explicit
3202 integer bit set to continue to be normalized. In theory
3203 this discrepancy has been taken care of by the difference
3204 in fmt->emin in round_for_format. */
3211 gcc_assert (exp
>= 0);
3215 if (HOST_BITS_PER_LONG
== 32)
3217 sig_hi
= r
->sig
[SIGSZ
-1];
3218 sig_lo
= r
->sig
[SIGSZ
-2];
3222 sig_lo
= r
->sig
[SIGSZ
-1];
3223 sig_hi
= sig_lo
>> 31 >> 1;
3224 sig_lo
&= 0xffffffff;
3233 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3236 /* Convert from the internal format to the 12-byte Motorola format
3237 for an IEEE extended real. */
3239 encode_ieee_extended_motorola (const struct real_format
*fmt
, long *buf
,
3240 const REAL_VALUE_TYPE
*r
)
3243 encode_ieee_extended (fmt
, intermed
, r
);
3245 /* Motorola chips are assumed always to be big-endian. Also, the
3246 padding in a Motorola extended real goes between the exponent and
3247 the mantissa. At this point the mantissa is entirely within
3248 elements 0 and 1 of intermed, and the exponent entirely within
3249 element 2, so all we have to do is swap the order around, and
3250 shift element 2 left 16 bits. */
3251 buf
[0] = intermed
[2] << 16;
3252 buf
[1] = intermed
[1];
3253 buf
[2] = intermed
[0];
3256 /* Convert from the internal format to the 12-byte Intel format for
3257 an IEEE extended real. */
3259 encode_ieee_extended_intel_96 (const struct real_format
*fmt
, long *buf
,
3260 const REAL_VALUE_TYPE
*r
)
3262 if (FLOAT_WORDS_BIG_ENDIAN
)
3264 /* All the padding in an Intel-format extended real goes at the high
3265 end, which in this case is after the mantissa, not the exponent.
3266 Therefore we must shift everything down 16 bits. */
3268 encode_ieee_extended (fmt
, intermed
, r
);
3269 buf
[0] = ((intermed
[2] << 16) | ((unsigned long)(intermed
[1] & 0xFFFF0000) >> 16));
3270 buf
[1] = ((intermed
[1] << 16) | ((unsigned long)(intermed
[0] & 0xFFFF0000) >> 16));
3271 buf
[2] = (intermed
[0] << 16);
3274 /* encode_ieee_extended produces what we want directly. */
3275 encode_ieee_extended (fmt
, buf
, r
);
3278 /* Convert from the internal format to the 16-byte Intel format for
3279 an IEEE extended real. */
3281 encode_ieee_extended_intel_128 (const struct real_format
*fmt
, long *buf
,
3282 const REAL_VALUE_TYPE
*r
)
3284 /* All the padding in an Intel-format extended real goes at the high end. */
3285 encode_ieee_extended_intel_96 (fmt
, buf
, r
);
3289 /* As above, we have a helper function which converts from 12-byte
3290 little-endian Intel format to internal format. Functions below
3291 adjust for the other possible formats. */
3293 decode_ieee_extended (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3296 unsigned long image_hi
, sig_hi
, sig_lo
;
3300 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3301 sig_lo
&= 0xffffffff;
3302 sig_hi
&= 0xffffffff;
3303 image_hi
&= 0xffffffff;
3305 sign
= (image_hi
>> 15) & 1;
3306 exp
= image_hi
& 0x7fff;
3308 memset (r
, 0, sizeof (*r
));
3312 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3317 /* When the IEEE format contains a hidden bit, we know that
3318 it's zero at this point, and so shift up the significand
3319 and decrease the exponent to match. In this case, Motorola
3320 defines the explicit integer bit to be valid, so we don't
3321 know whether the msb is set or not. */
3322 SET_REAL_EXP (r
, fmt
->emin
);
3323 if (HOST_BITS_PER_LONG
== 32)
3325 r
->sig
[SIGSZ
-1] = sig_hi
;
3326 r
->sig
[SIGSZ
-2] = sig_lo
;
3329 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3333 else if (fmt
->has_signed_zero
)
3336 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3338 /* See above re "pseudo-infinities" and "pseudo-nans".
3339 Short summary is that the MSB will likely always be
3340 set, and that we don't care about it. */
3341 sig_hi
&= 0x7fffffff;
3343 if (sig_hi
|| sig_lo
)
3347 r
->signalling
= ((sig_hi
>> 30) & 1) ^ fmt
->qnan_msb_set
;
3348 if (HOST_BITS_PER_LONG
== 32)
3350 r
->sig
[SIGSZ
-1] = sig_hi
;
3351 r
->sig
[SIGSZ
-2] = sig_lo
;
3354 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3366 SET_REAL_EXP (r
, exp
- 16383 + 1);
3367 if (HOST_BITS_PER_LONG
== 32)
3369 r
->sig
[SIGSZ
-1] = sig_hi
;
3370 r
->sig
[SIGSZ
-2] = sig_lo
;
3373 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3377 /* Convert from the internal format to the 12-byte Motorola format
3378 for an IEEE extended real. */
3380 decode_ieee_extended_motorola (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3385 /* Motorola chips are assumed always to be big-endian. Also, the
3386 padding in a Motorola extended real goes between the exponent and
3387 the mantissa; remove it. */
3388 intermed
[0] = buf
[2];
3389 intermed
[1] = buf
[1];
3390 intermed
[2] = (unsigned long)buf
[0] >> 16;
3392 decode_ieee_extended (fmt
, r
, intermed
);
3395 /* Convert from the internal format to the 12-byte Intel format for
3396 an IEEE extended real. */
3398 decode_ieee_extended_intel_96 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3401 if (FLOAT_WORDS_BIG_ENDIAN
)
3403 /* All the padding in an Intel-format extended real goes at the high
3404 end, which in this case is after the mantissa, not the exponent.
3405 Therefore we must shift everything up 16 bits. */
3408 intermed
[0] = (((unsigned long)buf
[2] >> 16) | (buf
[1] << 16));
3409 intermed
[1] = (((unsigned long)buf
[1] >> 16) | (buf
[0] << 16));
3410 intermed
[2] = ((unsigned long)buf
[0] >> 16);
3412 decode_ieee_extended (fmt
, r
, intermed
);
3415 /* decode_ieee_extended produces what we want directly. */
3416 decode_ieee_extended (fmt
, r
, buf
);
3419 /* Convert from the internal format to the 16-byte Intel format for
3420 an IEEE extended real. */
3422 decode_ieee_extended_intel_128 (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3425 /* All the padding in an Intel-format extended real goes at the high end. */
3426 decode_ieee_extended_intel_96 (fmt
, r
, buf
);
3429 const struct real_format ieee_extended_motorola_format
=
3431 encode_ieee_extended_motorola
,
3432 decode_ieee_extended_motorola
,
3448 const struct real_format ieee_extended_intel_96_format
=
3450 encode_ieee_extended_intel_96
,
3451 decode_ieee_extended_intel_96
,
3467 const struct real_format ieee_extended_intel_128_format
=
3469 encode_ieee_extended_intel_128
,
3470 decode_ieee_extended_intel_128
,
3486 /* The following caters to i386 systems that set the rounding precision
3487 to 53 bits instead of 64, e.g. FreeBSD. */
3488 const struct real_format ieee_extended_intel_96_round_53_format
=
3490 encode_ieee_extended_intel_96
,
3491 decode_ieee_extended_intel_96
,
3507 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3508 numbers whose sum is equal to the extended precision value. The number
3509 with greater magnitude is first. This format has the same magnitude
3510 range as an IEEE double precision value, but effectively 106 bits of
3511 significand precision. Infinity and NaN are represented by their IEEE
3512 double precision value stored in the first number, the second number is
3513 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3515 static void encode_ibm_extended (const struct real_format
*fmt
,
3516 long *, const REAL_VALUE_TYPE
*);
3517 static void decode_ibm_extended (const struct real_format
*,
3518 REAL_VALUE_TYPE
*, const long *);
3521 encode_ibm_extended (const struct real_format
*fmt
, long *buf
,
3522 const REAL_VALUE_TYPE
*r
)
3524 REAL_VALUE_TYPE u
, normr
, v
;
3525 const struct real_format
*base_fmt
;
3527 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3529 /* Renormalize R before doing any arithmetic on it. */
3531 if (normr
.cl
== rvc_normal
)
3534 /* u = IEEE double precision portion of significand. */
3536 round_for_format (base_fmt
, &u
);
3537 encode_ieee_double (base_fmt
, &buf
[0], &u
);
3539 if (u
.cl
== rvc_normal
)
3541 do_add (&v
, &normr
, &u
, 1);
3542 /* Call round_for_format since we might need to denormalize. */
3543 round_for_format (base_fmt
, &v
);
3544 encode_ieee_double (base_fmt
, &buf
[2], &v
);
3548 /* Inf, NaN, 0 are all representable as doubles, so the
3549 least-significant part can be 0.0. */
3556 decode_ibm_extended (const struct real_format
*fmt ATTRIBUTE_UNUSED
, REAL_VALUE_TYPE
*r
,
3559 REAL_VALUE_TYPE u
, v
;
3560 const struct real_format
*base_fmt
;
3562 base_fmt
= fmt
->qnan_msb_set
? &ieee_double_format
: &mips_double_format
;
3563 decode_ieee_double (base_fmt
, &u
, &buf
[0]);
3565 if (u
.cl
!= rvc_zero
&& u
.cl
!= rvc_inf
&& u
.cl
!= rvc_nan
)
3567 decode_ieee_double (base_fmt
, &v
, &buf
[2]);
3568 do_add (r
, &u
, &v
, 0);
3574 const struct real_format ibm_extended_format
=
3576 encode_ibm_extended
,
3577 decode_ibm_extended
,
3593 const struct real_format mips_extended_format
=
3595 encode_ibm_extended
,
3596 decode_ibm_extended
,
3613 /* IEEE quad precision format. */
3615 static void encode_ieee_quad (const struct real_format
*fmt
,
3616 long *, const REAL_VALUE_TYPE
*);
3617 static void decode_ieee_quad (const struct real_format
*,
3618 REAL_VALUE_TYPE
*, const long *);
3621 encode_ieee_quad (const struct real_format
*fmt
, long *buf
,
3622 const REAL_VALUE_TYPE
*r
)
3624 unsigned long image3
, image2
, image1
, image0
, exp
;
3625 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3628 image3
= r
->sign
<< 31;
3633 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3642 image3
|= 32767 << 16;
3645 image3
|= 0x7fffffff;
3646 image2
= 0xffffffff;
3647 image1
= 0xffffffff;
3648 image0
= 0xffffffff;
3655 image3
|= 32767 << 16;
3659 if (fmt
->canonical_nan_lsbs_set
)
3662 image2
= image1
= image0
= 0xffffffff;
3665 else if (HOST_BITS_PER_LONG
== 32)
3670 image3
|= u
.sig
[3] & 0xffff;
3675 image1
= image0
>> 31 >> 1;
3677 image3
|= (image2
>> 31 >> 1) & 0xffff;
3678 image0
&= 0xffffffff;
3679 image2
&= 0xffffffff;
3681 if (r
->signalling
== fmt
->qnan_msb_set
)
3685 if (((image3
& 0xffff) | image2
| image1
| image0
) == 0)
3690 image3
|= 0x7fffffff;
3691 image2
= 0xffffffff;
3692 image1
= 0xffffffff;
3693 image0
= 0xffffffff;
3698 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3699 whereas the intermediate representation is 0.F x 2**exp.
3700 Which means we're off by one. */
3704 exp
= REAL_EXP (r
) + 16383 - 1;
3705 image3
|= exp
<< 16;
3707 if (HOST_BITS_PER_LONG
== 32)
3712 image3
|= u
.sig
[3] & 0xffff;
3717 image1
= image0
>> 31 >> 1;
3719 image3
|= (image2
>> 31 >> 1) & 0xffff;
3720 image0
&= 0xffffffff;
3721 image2
&= 0xffffffff;
3729 if (FLOAT_WORDS_BIG_ENDIAN
)
3746 decode_ieee_quad (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
,
3749 unsigned long image3
, image2
, image1
, image0
;
3753 if (FLOAT_WORDS_BIG_ENDIAN
)
3767 image0
&= 0xffffffff;
3768 image1
&= 0xffffffff;
3769 image2
&= 0xffffffff;
3771 sign
= (image3
>> 31) & 1;
3772 exp
= (image3
>> 16) & 0x7fff;
3775 memset (r
, 0, sizeof (*r
));
3779 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
3784 SET_REAL_EXP (r
, -16382 + (SIGNIFICAND_BITS
- 112));
3785 if (HOST_BITS_PER_LONG
== 32)
3794 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3795 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3800 else if (fmt
->has_signed_zero
)
3803 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3805 if (image3
| image2
| image1
| image0
)
3809 r
->signalling
= ((image3
>> 15) & 1) ^ fmt
->qnan_msb_set
;
3811 if (HOST_BITS_PER_LONG
== 32)
3820 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3821 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3823 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3835 SET_REAL_EXP (r
, exp
- 16383 + 1);
3837 if (HOST_BITS_PER_LONG
== 32)
3846 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3847 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3849 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3850 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3854 const struct real_format ieee_quad_format
=
3873 const struct real_format mips_quad_format
=
3892 /* Descriptions of VAX floating point formats can be found beginning at
3894 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
3896 The thing to remember is that they're almost IEEE, except for word
3897 order, exponent bias, and the lack of infinities, nans, and denormals.
3899 We don't implement the H_floating format here, simply because neither
3900 the VAX or Alpha ports use it. */
3902 static void encode_vax_f (const struct real_format
*fmt
,
3903 long *, const REAL_VALUE_TYPE
*);
3904 static void decode_vax_f (const struct real_format
*,
3905 REAL_VALUE_TYPE
*, const long *);
3906 static void encode_vax_d (const struct real_format
*fmt
,
3907 long *, const REAL_VALUE_TYPE
*);
3908 static void decode_vax_d (const struct real_format
*,
3909 REAL_VALUE_TYPE
*, const long *);
3910 static void encode_vax_g (const struct real_format
*fmt
,
3911 long *, const REAL_VALUE_TYPE
*);
3912 static void decode_vax_g (const struct real_format
*,
3913 REAL_VALUE_TYPE
*, const long *);
3916 encode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
3917 const REAL_VALUE_TYPE
*r
)
3919 unsigned long sign
, exp
, sig
, image
;
3921 sign
= r
->sign
<< 15;
3931 image
= 0xffff7fff | sign
;
3935 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
3936 exp
= REAL_EXP (r
) + 128;
3938 image
= (sig
<< 16) & 0xffff0000;
3952 decode_vax_f (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
3953 REAL_VALUE_TYPE
*r
, const long *buf
)
3955 unsigned long image
= buf
[0] & 0xffffffff;
3956 int exp
= (image
>> 7) & 0xff;
3958 memset (r
, 0, sizeof (*r
));
3963 r
->sign
= (image
>> 15) & 1;
3964 SET_REAL_EXP (r
, exp
- 128);
3966 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
3967 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
3972 encode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
3973 const REAL_VALUE_TYPE
*r
)
3975 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3980 image0
= image1
= 0;
3985 image0
= 0xffff7fff | sign
;
3986 image1
= 0xffffffff;
3990 /* Extract the significand into straight hi:lo. */
3991 if (HOST_BITS_PER_LONG
== 64)
3993 image0
= r
->sig
[SIGSZ
-1];
3994 image1
= (image0
>> (64 - 56)) & 0xffffffff;
3995 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
3999 image0
= r
->sig
[SIGSZ
-1];
4000 image1
= r
->sig
[SIGSZ
-2];
4001 image1
= (image0
<< 24) | (image1
>> 8);
4002 image0
= (image0
>> 8) & 0xffffff;
4005 /* Rearrange the half-words of the significand to match the
4007 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
4008 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4010 /* Add the sign and exponent. */
4012 image0
|= (REAL_EXP (r
) + 128) << 7;
4019 if (FLOAT_WORDS_BIG_ENDIAN
)
4020 buf
[0] = image1
, buf
[1] = image0
;
4022 buf
[0] = image0
, buf
[1] = image1
;
4026 decode_vax_d (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4027 REAL_VALUE_TYPE
*r
, const long *buf
)
4029 unsigned long image0
, image1
;
4032 if (FLOAT_WORDS_BIG_ENDIAN
)
4033 image1
= buf
[0], image0
= buf
[1];
4035 image0
= buf
[0], image1
= buf
[1];
4036 image0
&= 0xffffffff;
4037 image1
&= 0xffffffff;
4039 exp
= (image0
>> 7) & 0xff;
4041 memset (r
, 0, sizeof (*r
));
4046 r
->sign
= (image0
>> 15) & 1;
4047 SET_REAL_EXP (r
, exp
- 128);
4049 /* Rearrange the half-words of the external format into
4050 proper ascending order. */
4051 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
4052 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4054 if (HOST_BITS_PER_LONG
== 64)
4056 image0
= (image0
<< 31 << 1) | image1
;
4059 r
->sig
[SIGSZ
-1] = image0
;
4063 r
->sig
[SIGSZ
-1] = image0
;
4064 r
->sig
[SIGSZ
-2] = image1
;
4065 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
4066 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4072 encode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4073 const REAL_VALUE_TYPE
*r
)
4075 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
4080 image0
= image1
= 0;
4085 image0
= 0xffff7fff | sign
;
4086 image1
= 0xffffffff;
4090 /* Extract the significand into straight hi:lo. */
4091 if (HOST_BITS_PER_LONG
== 64)
4093 image0
= r
->sig
[SIGSZ
-1];
4094 image1
= (image0
>> (64 - 53)) & 0xffffffff;
4095 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
4099 image0
= r
->sig
[SIGSZ
-1];
4100 image1
= r
->sig
[SIGSZ
-2];
4101 image1
= (image0
<< 21) | (image1
>> 11);
4102 image0
= (image0
>> 11) & 0xfffff;
4105 /* Rearrange the half-words of the significand to match the
4107 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
4108 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
4110 /* Add the sign and exponent. */
4112 image0
|= (REAL_EXP (r
) + 1024) << 4;
4119 if (FLOAT_WORDS_BIG_ENDIAN
)
4120 buf
[0] = image1
, buf
[1] = image0
;
4122 buf
[0] = image0
, buf
[1] = image1
;
4126 decode_vax_g (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4127 REAL_VALUE_TYPE
*r
, const long *buf
)
4129 unsigned long image0
, image1
;
4132 if (FLOAT_WORDS_BIG_ENDIAN
)
4133 image1
= buf
[0], image0
= buf
[1];
4135 image0
= buf
[0], image1
= buf
[1];
4136 image0
&= 0xffffffff;
4137 image1
&= 0xffffffff;
4139 exp
= (image0
>> 4) & 0x7ff;
4141 memset (r
, 0, sizeof (*r
));
4146 r
->sign
= (image0
>> 15) & 1;
4147 SET_REAL_EXP (r
, exp
- 1024);
4149 /* Rearrange the half-words of the external format into
4150 proper ascending order. */
4151 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
4152 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
4154 if (HOST_BITS_PER_LONG
== 64)
4156 image0
= (image0
<< 31 << 1) | image1
;
4159 r
->sig
[SIGSZ
-1] = image0
;
4163 r
->sig
[SIGSZ
-1] = image0
;
4164 r
->sig
[SIGSZ
-2] = image1
;
4165 lshift_significand (r
, r
, 64 - 53);
4166 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
4171 const struct real_format vax_f_format
=
4190 const struct real_format vax_d_format
=
4209 const struct real_format vax_g_format
=
4228 /* Encode real R into a single precision DFP value in BUF. */
4230 encode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4231 long *buf ATTRIBUTE_UNUSED
,
4232 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4234 encode_decimal32 (fmt
, buf
, r
);
4237 /* Decode a single precision DFP value in BUF into a real R. */
4239 decode_decimal_single (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4240 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4241 const long *buf ATTRIBUTE_UNUSED
)
4243 decode_decimal32 (fmt
, r
, buf
);
4246 /* Encode real R into a double precision DFP value in BUF. */
4248 encode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4249 long *buf ATTRIBUTE_UNUSED
,
4250 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4252 encode_decimal64 (fmt
, buf
, r
);
4255 /* Decode a double precision DFP value in BUF into a real R. */
4257 decode_decimal_double (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4258 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4259 const long *buf ATTRIBUTE_UNUSED
)
4261 decode_decimal64 (fmt
, r
, buf
);
4264 /* Encode real R into a quad precision DFP value in BUF. */
4266 encode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4267 long *buf ATTRIBUTE_UNUSED
,
4268 const REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
)
4270 encode_decimal128 (fmt
, buf
, r
);
4273 /* Decode a quad precision DFP value in BUF into a real R. */
4275 decode_decimal_quad (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4276 REAL_VALUE_TYPE
*r ATTRIBUTE_UNUSED
,
4277 const long *buf ATTRIBUTE_UNUSED
)
4279 decode_decimal128 (fmt
, r
, buf
);
4282 /* Single precision decimal floating point (IEEE 754). */
4283 const struct real_format decimal_single_format
=
4285 encode_decimal_single
,
4286 decode_decimal_single
,
4302 /* Double precision decimal floating point (IEEE 754). */
4303 const struct real_format decimal_double_format
=
4305 encode_decimal_double
,
4306 decode_decimal_double
,
4322 /* Quad precision decimal floating point (IEEE 754). */
4323 const struct real_format decimal_quad_format
=
4325 encode_decimal_quad
,
4326 decode_decimal_quad
,
4342 /* A synthetic "format" for internal arithmetic. It's the size of the
4343 internal significand minus the two bits needed for proper rounding.
4344 The encode and decode routines exist only to satisfy our paranoia
4347 static void encode_internal (const struct real_format
*fmt
,
4348 long *, const REAL_VALUE_TYPE
*);
4349 static void decode_internal (const struct real_format
*,
4350 REAL_VALUE_TYPE
*, const long *);
4353 encode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
, long *buf
,
4354 const REAL_VALUE_TYPE
*r
)
4356 memcpy (buf
, r
, sizeof (*r
));
4360 decode_internal (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
4361 REAL_VALUE_TYPE
*r
, const long *buf
)
4363 memcpy (r
, buf
, sizeof (*r
));
4366 const struct real_format real_internal_format
=
4371 SIGNIFICAND_BITS
- 2,
4372 SIGNIFICAND_BITS
- 2,
4385 /* Calculate the square root of X in mode MODE, and store the result
4386 in R. Return TRUE if the operation does not raise an exception.
4387 For details see "High Precision Division and Square Root",
4388 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4389 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4392 real_sqrt (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4393 const REAL_VALUE_TYPE
*x
)
4395 static REAL_VALUE_TYPE halfthree
;
4396 static bool init
= false;
4397 REAL_VALUE_TYPE h
, t
, i
;
4400 /* sqrt(-0.0) is -0.0. */
4401 if (real_isnegzero (x
))
4407 /* Negative arguments return NaN. */
4410 get_canonical_qnan (r
, 0);
4414 /* Infinity and NaN return themselves. */
4415 if (!real_isfinite (x
))
4423 do_add (&halfthree
, &dconst1
, &dconsthalf
, 0);
4427 /* Initial guess for reciprocal sqrt, i. */
4428 exp
= real_exponent (x
);
4429 real_ldexp (&i
, &dconst1
, -exp
/2);
4431 /* Newton's iteration for reciprocal sqrt, i. */
4432 for (iter
= 0; iter
< 16; iter
++)
4434 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4435 do_multiply (&t
, x
, &i
);
4436 do_multiply (&h
, &t
, &i
);
4437 do_multiply (&t
, &h
, &dconsthalf
);
4438 do_add (&h
, &halfthree
, &t
, 1);
4439 do_multiply (&t
, &i
, &h
);
4441 /* Check for early convergence. */
4442 if (iter
>= 6 && real_identical (&i
, &t
))
4445 /* ??? Unroll loop to avoid copying. */
4449 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4450 do_multiply (&t
, x
, &i
);
4451 do_multiply (&h
, &t
, &i
);
4452 do_add (&i
, &dconst1
, &h
, 1);
4453 do_multiply (&h
, &t
, &i
);
4454 do_multiply (&i
, &dconsthalf
, &h
);
4455 do_add (&h
, &t
, &i
, 0);
4457 /* ??? We need a Tuckerman test to get the last bit. */
4459 real_convert (r
, mode
, &h
);
4463 /* Calculate X raised to the integer exponent N in mode MODE and store
4464 the result in R. Return true if the result may be inexact due to
4465 loss of precision. The algorithm is the classic "left-to-right binary
4466 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4467 Algorithms", "The Art of Computer Programming", Volume 2. */
4470 real_powi (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4471 const REAL_VALUE_TYPE
*x
, HOST_WIDE_INT n
)
4473 unsigned HOST_WIDE_INT bit
;
4475 bool inexact
= false;
4487 /* Don't worry about overflow, from now on n is unsigned. */
4495 bit
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
4496 for (i
= 0; i
< HOST_BITS_PER_WIDE_INT
; i
++)
4500 inexact
|= do_multiply (&t
, &t
, &t
);
4502 inexact
|= do_multiply (&t
, &t
, x
);
4510 inexact
|= do_divide (&t
, &dconst1
, &t
);
4512 real_convert (r
, mode
, &t
);
4516 /* Round X to the nearest integer not larger in absolute value, i.e.
4517 towards zero, placing the result in R in mode MODE. */
4520 real_trunc (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4521 const REAL_VALUE_TYPE
*x
)
4523 do_fix_trunc (r
, x
);
4524 if (mode
!= VOIDmode
)
4525 real_convert (r
, mode
, r
);
4528 /* Round X to the largest integer not greater in value, i.e. round
4529 down, placing the result in R in mode MODE. */
4532 real_floor (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4533 const REAL_VALUE_TYPE
*x
)
4537 do_fix_trunc (&t
, x
);
4538 if (! real_identical (&t
, x
) && x
->sign
)
4539 do_add (&t
, &t
, &dconstm1
, 0);
4540 if (mode
!= VOIDmode
)
4541 real_convert (r
, mode
, &t
);
4546 /* Round X to the smallest integer not less then argument, i.e. round
4547 up, placing the result in R in mode MODE. */
4550 real_ceil (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4551 const REAL_VALUE_TYPE
*x
)
4555 do_fix_trunc (&t
, x
);
4556 if (! real_identical (&t
, x
) && ! x
->sign
)
4557 do_add (&t
, &t
, &dconst1
, 0);
4558 if (mode
!= VOIDmode
)
4559 real_convert (r
, mode
, &t
);
4564 /* Round X to the nearest integer, but round halfway cases away from
4568 real_round (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
4569 const REAL_VALUE_TYPE
*x
)
4571 do_add (r
, x
, &dconsthalf
, x
->sign
);
4572 do_fix_trunc (r
, r
);
4573 if (mode
!= VOIDmode
)
4574 real_convert (r
, mode
, r
);
4577 /* Set the sign of R to the sign of X. */
4580 real_copysign (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*x
)
4585 /* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible
4586 for initializing and clearing the MPFR parameter. */
4589 mpfr_from_real (mpfr_ptr m
, const REAL_VALUE_TYPE
*r
, mp_rnd_t rndmode
)
4591 /* We use a string as an intermediate type. */
4595 /* Take care of Infinity and NaN. */
4596 if (r
->cl
== rvc_inf
)
4598 mpfr_set_inf (m
, r
->sign
== 1 ? -1 : 1);
4602 if (r
->cl
== rvc_nan
)
4608 real_to_hexadecimal (buf
, r
, sizeof (buf
), 0, 1);
4609 /* mpfr_set_str() parses hexadecimal floats from strings in the same
4610 format that GCC will output them. Nothing extra is needed. */
4611 ret
= mpfr_set_str (m
, buf
, 16, rndmode
);
4612 gcc_assert (ret
== 0);
4615 /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
4616 mode RNDMODE. TYPE is only relevant if M is a NaN. */
4619 real_from_mpfr (REAL_VALUE_TYPE
*r
, mpfr_srcptr m
, tree type
, mp_rnd_t rndmode
)
4621 /* We use a string as an intermediate type. */
4622 char buf
[128], *rstr
;
4625 /* Take care of Infinity and NaN. */
4629 if (mpfr_sgn (m
) < 0)
4630 *r
= REAL_VALUE_NEGATE (*r
);
4636 real_nan (r
, "", 1, TYPE_MODE (type
));
4640 rstr
= mpfr_get_str (NULL
, &exp
, 16, 0, m
, rndmode
);
4642 /* The additional 12 chars add space for the sprintf below. This
4643 leaves 6 digits for the exponent which is supposedly enough. */
4644 gcc_assert (rstr
!= NULL
&& strlen (rstr
) < sizeof (buf
) - 12);
4646 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
4647 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
4652 sprintf (buf
, "-0x.%sp%d", &rstr
[1], (int) exp
);
4654 sprintf (buf
, "0x.%sp%d", rstr
, (int) exp
);
4656 mpfr_free_str (rstr
);
4658 real_from_string (r
, buf
);
4661 /* Check whether the real constant value given is an integer. */
4664 real_isinteger (const REAL_VALUE_TYPE
*c
, enum machine_mode mode
)
4666 REAL_VALUE_TYPE cint
;
4668 real_trunc (&cint
, mode
, c
);
4669 return real_identical (c
, &cint
);
4672 /* Write into BUF the maximum representable finite floating-point
4673 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4674 float string. LEN is the size of BUF, and the buffer must be large
4675 enough to contain the resulting string. */
4678 get_max_float (const struct real_format
*fmt
, char *buf
, size_t len
)
4683 strcpy (buf
, "0x0.");
4685 for (i
= 0, p
= buf
+ 4; i
+ 3 < n
; i
+= 4)
4688 *p
++ = "08ce"[n
- i
];
4689 sprintf (p
, "p%d", fmt
->emax
);
4690 if (fmt
->pnan
< fmt
->p
)
4692 /* This is an IBM extended double format made up of two IEEE
4693 doubles. The value of the long double is the sum of the
4694 values of the two parts. The most significant part is
4695 required to be the value of the long double rounded to the
4696 nearest double. Rounding means we need a slightly smaller
4697 value for LDBL_MAX. */
4698 buf
[4 + fmt
->pnan
/ 4] = "7bde"[fmt
->pnan
% 4];
4701 gcc_assert (strlen (buf
) < len
);