1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by Stephen L. Moshier (moshier@world.std.com).
5 Re-written by Richard Henderson <rth@redhat.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #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 C 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 than the
53 largest supported target floating-point type by at least 2 bits.
54 This gives us proper rounding when we truncate to the target type.
55 In addition, E must be large enough to hold the smallest supported
56 denormal number in a normalized form.
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 29.
62 Note that the decimal string conversion routines are sensitive to
63 rounding error. Since the raw arithmetic routines do not themselves
64 have guard digits or rounding, the computation of 10**exp can
65 accumulate more than a few digits of error. The previous incarnation
66 of real.c successfully used a 144 bit fraction; given the current
67 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits.
69 Target floating point models that use base 16 instead of base 2
70 (i.e. IBM 370), are handled during round_for_format, in which we
71 canonicalize the exponent to be a multiple of 4 (log2(16)), and
72 adjust the significand to match. */
75 /* Used to classify two numbers simultaneously. */
76 #define CLASS2(A, B) ((A) << 2 | (B))
78 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
79 #error "Some constant folding done by hand to avoid shift count warnings"
82 static void get_zero
PARAMS ((REAL_VALUE_TYPE
*, int));
83 static void get_canonical_qnan
PARAMS ((REAL_VALUE_TYPE
*, int));
84 static void get_canonical_snan
PARAMS ((REAL_VALUE_TYPE
*, int));
85 static void get_inf
PARAMS ((REAL_VALUE_TYPE
*, int));
86 static bool sticky_rshift_significand
PARAMS ((REAL_VALUE_TYPE
*,
87 const REAL_VALUE_TYPE
*,
89 static void rshift_significand
PARAMS ((REAL_VALUE_TYPE
*,
90 const REAL_VALUE_TYPE
*,
92 static void lshift_significand
PARAMS ((REAL_VALUE_TYPE
*,
93 const REAL_VALUE_TYPE
*,
95 static void lshift_significand_1
PARAMS ((REAL_VALUE_TYPE
*,
96 const REAL_VALUE_TYPE
*));
97 static bool add_significands
PARAMS ((REAL_VALUE_TYPE
*r
,
98 const REAL_VALUE_TYPE
*,
99 const REAL_VALUE_TYPE
*));
100 static bool sub_significands
PARAMS ((REAL_VALUE_TYPE
*,
101 const REAL_VALUE_TYPE
*,
102 const REAL_VALUE_TYPE
*, int));
103 static void neg_significand
PARAMS ((REAL_VALUE_TYPE
*,
104 const REAL_VALUE_TYPE
*));
105 static int cmp_significands
PARAMS ((const REAL_VALUE_TYPE
*,
106 const REAL_VALUE_TYPE
*));
107 static int cmp_significand_0
PARAMS ((const REAL_VALUE_TYPE
*));
108 static void set_significand_bit
PARAMS ((REAL_VALUE_TYPE
*, unsigned int));
109 static void clear_significand_bit
PARAMS ((REAL_VALUE_TYPE
*, unsigned int));
110 static bool test_significand_bit
PARAMS ((REAL_VALUE_TYPE
*, unsigned int));
111 static void clear_significand_below
PARAMS ((REAL_VALUE_TYPE
*,
113 static bool div_significands
PARAMS ((REAL_VALUE_TYPE
*,
114 const REAL_VALUE_TYPE
*,
115 const REAL_VALUE_TYPE
*));
116 static void normalize
PARAMS ((REAL_VALUE_TYPE
*));
118 static void do_add
PARAMS ((REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
119 const REAL_VALUE_TYPE
*, int));
120 static void do_multiply
PARAMS ((REAL_VALUE_TYPE
*,
121 const REAL_VALUE_TYPE
*,
122 const REAL_VALUE_TYPE
*));
123 static void do_divide
PARAMS ((REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
124 const REAL_VALUE_TYPE
*));
125 static int do_compare
PARAMS ((const REAL_VALUE_TYPE
*,
126 const REAL_VALUE_TYPE
*, int));
127 static void do_fix_trunc
PARAMS ((REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*));
129 static unsigned long rtd_divmod
PARAMS ((REAL_VALUE_TYPE
*,
132 static const REAL_VALUE_TYPE
* ten_to_ptwo
PARAMS ((int));
133 static const REAL_VALUE_TYPE
* ten_to_mptwo
PARAMS ((int));
134 static const REAL_VALUE_TYPE
* real_digit
PARAMS ((int));
135 static void times_pten
PARAMS ((REAL_VALUE_TYPE
*, int));
137 static void round_for_format
PARAMS ((const struct real_format
*,
140 /* Initialize R with a positive zero. */
147 memset (r
, 0, sizeof (*r
));
151 /* Initialize R with the canonical quiet NaN. */
154 get_canonical_qnan (r
, sign
)
158 memset (r
, 0, sizeof (*r
));
161 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 1;
165 get_canonical_snan (r
, sign
)
169 memset (r
, 0, sizeof (*r
));
172 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 2;
180 memset (r
, 0, sizeof (*r
));
186 /* Right-shift the significand of A by N bits; put the result in the
187 significand of R. If any one bits are shifted out, return true. */
190 sticky_rshift_significand (r
, a
, n
)
192 const REAL_VALUE_TYPE
*a
;
195 unsigned long sticky
= 0;
196 unsigned int i
, ofs
= 0;
198 if (n
>= HOST_BITS_PER_LONG
)
200 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
202 n
&= HOST_BITS_PER_LONG
- 1;
207 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
208 for (i
= 0; i
< SIGSZ
; ++i
)
211 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
212 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
213 << (HOST_BITS_PER_LONG
- n
)));
218 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
219 r
->sig
[i
] = a
->sig
[ofs
+ i
];
220 for (; i
< SIGSZ
; ++i
)
227 /* Right-shift the significand of A by N bits; put the result in the
231 rshift_significand (r
, a
, n
)
233 const REAL_VALUE_TYPE
*a
;
236 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
238 n
&= HOST_BITS_PER_LONG
- 1;
241 for (i
= 0; i
< SIGSZ
; ++i
)
244 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
245 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
246 << (HOST_BITS_PER_LONG
- n
)));
251 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
252 r
->sig
[i
] = a
->sig
[ofs
+ i
];
253 for (; i
< SIGSZ
; ++i
)
258 /* Left-shift the significand of A by N bits; put the result in the
262 lshift_significand (r
, a
, n
)
264 const REAL_VALUE_TYPE
*a
;
267 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
269 n
&= HOST_BITS_PER_LONG
- 1;
272 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
273 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
274 for (; i
< SIGSZ
; ++i
)
275 r
->sig
[SIGSZ
-1-i
] = 0;
278 for (i
= 0; i
< SIGSZ
; ++i
)
281 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
282 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
283 >> (HOST_BITS_PER_LONG
- n
)));
287 /* Likewise, but N is specialized to 1. */
290 lshift_significand_1 (r
, a
)
292 const REAL_VALUE_TYPE
*a
;
296 for (i
= SIGSZ
- 1; i
> 0; --i
)
297 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
298 r
->sig
[0] = a
->sig
[0] << 1;
301 /* Add the significands of A and B, placing the result in R. Return
302 true if there was carry out of the most significant word. */
305 add_significands (r
, a
, b
)
307 const REAL_VALUE_TYPE
*a
, *b
;
312 for (i
= 0; i
< SIGSZ
; ++i
)
314 unsigned long ai
= a
->sig
[i
];
315 unsigned long ri
= ai
+ b
->sig
[i
];
331 /* Subtract the significands of A and B, placing the result in R. CARRY is
332 true if there's a borrow incoming to the least significant word.
333 Return true if there was borrow out of the most significant word. */
336 sub_significands (r
, a
, b
, carry
)
338 const REAL_VALUE_TYPE
*a
, *b
;
343 for (i
= 0; i
< SIGSZ
; ++i
)
345 unsigned long ai
= a
->sig
[i
];
346 unsigned long ri
= ai
- b
->sig
[i
];
362 /* Negate the significand A, placing the result in R. */
365 neg_significand (r
, a
)
367 const REAL_VALUE_TYPE
*a
;
372 for (i
= 0; i
< SIGSZ
; ++i
)
374 unsigned long ri
, ai
= a
->sig
[i
];
393 /* Compare significands. Return tri-state vs zero. */
396 cmp_significands (a
, b
)
397 const REAL_VALUE_TYPE
*a
, *b
;
401 for (i
= SIGSZ
- 1; i
>= 0; --i
)
403 unsigned long ai
= a
->sig
[i
];
404 unsigned long bi
= b
->sig
[i
];
415 /* Return true if A is nonzero. */
418 cmp_significand_0 (a
)
419 const REAL_VALUE_TYPE
*a
;
423 for (i
= SIGSZ
- 1; i
>= 0; --i
)
430 /* Set bit N of the significand of R. */
433 set_significand_bit (r
, n
)
437 r
->sig
[n
/ HOST_BITS_PER_LONG
]
438 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
441 /* Clear bit N of the significand of R. */
444 clear_significand_bit (r
, n
)
448 r
->sig
[n
/ HOST_BITS_PER_LONG
]
449 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
452 /* Test bit N of the significand of R. */
455 test_significand_bit (r
, n
)
459 /* ??? Compiler bug here if we return this expression directly.
460 The conversion to bool strips the "&1" and we wind up testing
461 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
462 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
466 /* Clear bits 0..N-1 of the significand of R. */
469 clear_significand_below (r
, n
)
473 int i
, w
= n
/ HOST_BITS_PER_LONG
;
475 for (i
= 0; i
< w
; ++i
)
478 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
481 /* Divide the significands of A and B, placing the result in R. Return
482 true if the division was inexact. */
485 div_significands (r
, a
, b
)
487 const REAL_VALUE_TYPE
*a
, *b
;
490 int i
, bit
= SIGNIFICAND_BITS
- 1;
491 unsigned long msb
, inexact
;
494 memset (r
->sig
, 0, sizeof (r
->sig
));
500 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
501 lshift_significand_1 (&u
, &u
);
503 if (msb
|| cmp_significands (&u
, b
) >= 0)
505 sub_significands (&u
, &u
, b
, 0);
506 set_significand_bit (r
, bit
);
511 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
517 /* Adjust the exponent and significand of R such that the most
518 significant bit is set. We underflow to zero and overflow to
519 infinity here, without denormals. (The intermediate representation
520 exponent is large enough to handle target denormals normalized.) */
529 /* Find the first word that is nonzero. */
530 for (i
= SIGSZ
- 1; i
>= 0; i
--)
532 shift
+= HOST_BITS_PER_LONG
;
536 /* Zero significand flushes to zero. */
544 /* Find the first bit that is nonzero. */
546 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
552 exp
= r
->exp
- shift
;
554 get_inf (r
, r
->sign
);
555 else if (exp
< -MAX_EXP
)
556 get_zero (r
, r
->sign
);
560 lshift_significand (r
, r
, shift
);
565 /* Return R = A + (SUBTRACT_P ? -B : B). */
568 do_add (r
, a
, b
, subtract_p
)
570 const REAL_VALUE_TYPE
*a
, *b
;
575 bool inexact
= false;
577 /* Determine if we need to add or subtract. */
579 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
581 switch (CLASS2 (a
->class, b
->class))
583 case CLASS2 (rvc_zero
, rvc_zero
):
584 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
585 get_zero (r
, sign
& !subtract_p
);
588 case CLASS2 (rvc_zero
, rvc_normal
):
589 case CLASS2 (rvc_zero
, rvc_inf
):
590 case CLASS2 (rvc_zero
, rvc_nan
):
592 case CLASS2 (rvc_normal
, rvc_nan
):
593 case CLASS2 (rvc_inf
, rvc_nan
):
594 case CLASS2 (rvc_nan
, rvc_nan
):
595 /* ANY + NaN = NaN. */
596 case CLASS2 (rvc_normal
, rvc_inf
):
599 r
->sign
= sign
^ subtract_p
;
602 case CLASS2 (rvc_normal
, rvc_zero
):
603 case CLASS2 (rvc_inf
, rvc_zero
):
604 case CLASS2 (rvc_nan
, rvc_zero
):
606 case CLASS2 (rvc_nan
, rvc_normal
):
607 case CLASS2 (rvc_nan
, rvc_inf
):
608 /* NaN + ANY = NaN. */
609 case CLASS2 (rvc_inf
, rvc_normal
):
614 case CLASS2 (rvc_inf
, rvc_inf
):
616 /* Inf - Inf = NaN. */
617 get_canonical_qnan (r
, 0);
619 /* Inf + Inf = Inf. */
623 case CLASS2 (rvc_normal
, rvc_normal
):
630 /* Swap the arguments such that A has the larger exponent. */
631 dexp
= a
->exp
- b
->exp
;
634 const REAL_VALUE_TYPE
*t
;
641 /* If the exponents are not identical, we need to shift the
642 significand of B down. */
645 /* If the exponents are too far apart, the significands
646 do not overlap, which makes the subtraction a noop. */
647 if (dexp
>= SIGNIFICAND_BITS
)
654 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
660 if (sub_significands (r
, a
, b
, inexact
))
662 /* We got a borrow out of the subtraction. That means that
663 A and B had the same exponent, and B had the larger
664 significand. We need to swap the sign and negate the
667 neg_significand (r
, r
);
672 if (add_significands (r
, a
, b
))
674 /* We got carry out of the addition. This means we need to
675 shift the significand back down one bit and increase the
677 inexact
|= sticky_rshift_significand (r
, r
, 1);
678 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
687 r
->class = rvc_normal
;
691 /* Re-normalize the result. */
694 /* Special case: if the subtraction results in zero, the result
696 if (r
->class == rvc_zero
)
699 r
->sig
[0] |= inexact
;
702 /* Return R = A * B. */
705 do_multiply (r
, a
, b
)
707 const REAL_VALUE_TYPE
*a
, *b
;
709 REAL_VALUE_TYPE u
, t
, *rr
;
710 unsigned int i
, j
, k
;
711 int sign
= a
->sign
^ b
->sign
;
713 switch (CLASS2 (a
->class, b
->class))
715 case CLASS2 (rvc_zero
, rvc_zero
):
716 case CLASS2 (rvc_zero
, rvc_normal
):
717 case CLASS2 (rvc_normal
, rvc_zero
):
718 /* +-0 * ANY = 0 with appropriate sign. */
722 case CLASS2 (rvc_zero
, rvc_nan
):
723 case CLASS2 (rvc_normal
, rvc_nan
):
724 case CLASS2 (rvc_inf
, rvc_nan
):
725 case CLASS2 (rvc_nan
, rvc_nan
):
726 /* ANY * NaN = NaN. */
731 case CLASS2 (rvc_nan
, rvc_zero
):
732 case CLASS2 (rvc_nan
, rvc_normal
):
733 case CLASS2 (rvc_nan
, rvc_inf
):
734 /* NaN * ANY = NaN. */
739 case CLASS2 (rvc_zero
, rvc_inf
):
740 case CLASS2 (rvc_inf
, rvc_zero
):
742 get_canonical_qnan (r
, sign
);
745 case CLASS2 (rvc_inf
, rvc_inf
):
746 case CLASS2 (rvc_normal
, rvc_inf
):
747 case CLASS2 (rvc_inf
, rvc_normal
):
748 /* Inf * Inf = Inf, R * Inf = Inf */
753 case CLASS2 (rvc_normal
, rvc_normal
):
760 if (r
== a
|| r
== b
)
766 /* Collect all the partial products. Since we don't have sure access
767 to a widening multiply, we split each long into two half-words.
769 Consider the long-hand form of a four half-word multiplication:
779 We construct partial products of the widened half-word products
780 that are known to not overlap, e.g. DF+DH. Each such partial
781 product is given its proper exponent, which allows us to sum them
782 and obtain the finished product. */
784 for (i
= 0; i
< SIGSZ
* 2; ++i
)
786 unsigned long ai
= a
->sig
[i
/ 2];
788 ai
>>= HOST_BITS_PER_LONG
/ 2;
790 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
795 for (j
= 0; j
< 2; ++j
)
797 int exp
= (a
->exp
- (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
798 + (b
->exp
- (1-j
)*(HOST_BITS_PER_LONG
/2)));
803 /* Would underflow to zero, which we shouldn't bother adding. */
806 u
.class = rvc_normal
;
810 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
812 unsigned long bi
= b
->sig
[k
/ 2];
814 bi
>>= HOST_BITS_PER_LONG
/ 2;
816 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
818 u
.sig
[k
/ 2] = ai
* bi
;
822 do_add (rr
, rr
, &u
, 0);
831 /* Return R = A / B. */
836 const REAL_VALUE_TYPE
*a
, *b
;
838 int exp
, sign
= a
->sign
^ b
->sign
;
839 REAL_VALUE_TYPE t
, *rr
;
842 switch (CLASS2 (a
->class, b
->class))
844 case CLASS2 (rvc_zero
, rvc_zero
):
846 case CLASS2 (rvc_inf
, rvc_inf
):
847 /* Inf / Inf = NaN. */
848 get_canonical_qnan (r
, sign
);
851 case CLASS2 (rvc_zero
, rvc_normal
):
852 case CLASS2 (rvc_zero
, rvc_inf
):
854 case CLASS2 (rvc_normal
, rvc_inf
):
860 case CLASS2 (rvc_normal
, rvc_zero
):
862 case CLASS2 (rvc_inf
, rvc_zero
):
867 case CLASS2 (rvc_zero
, rvc_nan
):
868 case CLASS2 (rvc_normal
, rvc_nan
):
869 case CLASS2 (rvc_inf
, rvc_nan
):
870 case CLASS2 (rvc_nan
, rvc_nan
):
871 /* ANY / NaN = NaN. */
876 case CLASS2 (rvc_nan
, rvc_zero
):
877 case CLASS2 (rvc_nan
, rvc_normal
):
878 case CLASS2 (rvc_nan
, rvc_inf
):
879 /* NaN / ANY = NaN. */
884 case CLASS2 (rvc_inf
, rvc_normal
):
890 case CLASS2 (rvc_normal
, rvc_normal
):
897 if (r
== a
|| r
== b
)
902 rr
->class = rvc_normal
;
905 exp
= a
->exp
- b
->exp
+ 1;
912 inexact
= div_significands (rr
, a
, b
);
914 /* Re-normalize the result. */
916 rr
->sig
[0] |= inexact
;
922 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
923 one of the two operands is a NaN. */
926 do_compare (a
, b
, nan_result
)
927 const REAL_VALUE_TYPE
*a
, *b
;
932 switch (CLASS2 (a
->class, b
->class))
934 case CLASS2 (rvc_zero
, rvc_zero
):
935 /* Sign of zero doesn't matter for compares. */
938 case CLASS2 (rvc_inf
, rvc_zero
):
939 case CLASS2 (rvc_inf
, rvc_normal
):
940 case CLASS2 (rvc_normal
, rvc_zero
):
941 return (a
->sign
? -1 : 1);
943 case CLASS2 (rvc_inf
, rvc_inf
):
944 return -a
->sign
- -b
->sign
;
946 case CLASS2 (rvc_zero
, rvc_normal
):
947 case CLASS2 (rvc_zero
, rvc_inf
):
948 case CLASS2 (rvc_normal
, rvc_inf
):
949 return (b
->sign
? 1 : -1);
951 case CLASS2 (rvc_zero
, rvc_nan
):
952 case CLASS2 (rvc_normal
, rvc_nan
):
953 case CLASS2 (rvc_inf
, rvc_nan
):
954 case CLASS2 (rvc_nan
, rvc_nan
):
955 case CLASS2 (rvc_nan
, rvc_zero
):
956 case CLASS2 (rvc_nan
, rvc_normal
):
957 case CLASS2 (rvc_nan
, rvc_inf
):
960 case CLASS2 (rvc_normal
, rvc_normal
):
967 if (a
->sign
!= b
->sign
)
968 return -a
->sign
- -b
->sign
;
972 else if (a
->exp
< b
->exp
)
975 ret
= cmp_significands (a
, b
);
977 return (a
->sign
? -ret
: ret
);
980 /* Return A truncated to an integral value toward zero. */
985 const REAL_VALUE_TYPE
*a
;
998 get_zero (r
, r
->sign
);
999 else if (r
->exp
< SIGNIFICAND_BITS
)
1000 clear_significand_below (r
, SIGNIFICAND_BITS
- r
->exp
);
1008 /* Perform the binary or unary operation described by CODE.
1009 For a unary operation, leave OP1 NULL. */
1012 real_arithmetic (r
, icode
, op0
, op1
)
1015 const REAL_VALUE_TYPE
*op0
, *op1
;
1017 enum tree_code code
= icode
;
1022 do_add (r
, op0
, op1
, 0);
1026 do_add (r
, op0
, op1
, 1);
1030 do_multiply (r
, op0
, op1
);
1034 do_divide (r
, op0
, op1
);
1038 if (op1
->class == rvc_nan
)
1040 else if (do_compare (op0
, op1
, -1) < 0)
1047 if (op1
->class == rvc_nan
)
1049 else if (do_compare (op0
, op1
, 1) < 0)
1065 case FIX_TRUNC_EXPR
:
1066 do_fix_trunc (r
, op0
);
1074 /* Legacy. Similar, but return the result directly. */
1077 real_arithmetic2 (icode
, op0
, op1
)
1079 const REAL_VALUE_TYPE
*op0
, *op1
;
1082 real_arithmetic (&r
, icode
, op0
, op1
);
1087 real_compare (icode
, op0
, op1
)
1089 const REAL_VALUE_TYPE
*op0
, *op1
;
1091 enum tree_code code
= icode
;
1096 return do_compare (op0
, op1
, 1) < 0;
1098 return do_compare (op0
, op1
, 1) <= 0;
1100 return do_compare (op0
, op1
, -1) > 0;
1102 return do_compare (op0
, op1
, -1) >= 0;
1104 return do_compare (op0
, op1
, -1) == 0;
1106 return do_compare (op0
, op1
, -1) != 0;
1107 case UNORDERED_EXPR
:
1108 return op0
->class == rvc_nan
|| op1
->class == rvc_nan
;
1110 return op0
->class != rvc_nan
&& op1
->class != rvc_nan
;
1112 return do_compare (op0
, op1
, -1) < 0;
1114 return do_compare (op0
, op1
, -1) <= 0;
1116 return do_compare (op0
, op1
, 1) > 0;
1118 return do_compare (op0
, op1
, 1) >= 0;
1120 return do_compare (op0
, op1
, 0) == 0;
1127 /* Return floor log2(R). */
1131 const REAL_VALUE_TYPE
*r
;
1139 return (unsigned int)-1 >> 1;
1147 /* R = OP0 * 2**EXP. */
1150 real_ldexp (r
, op0
, exp
)
1152 const REAL_VALUE_TYPE
*op0
;
1166 get_inf (r
, r
->sign
);
1167 else if (exp
< -MAX_EXP
)
1168 get_zero (r
, r
->sign
);
1178 /* Determine whether a floating-point value X is infinite. */
1182 const REAL_VALUE_TYPE
*r
;
1184 return (r
->class == rvc_inf
);
1187 /* Determine whether a floating-point value X is a NaN. */
1191 const REAL_VALUE_TYPE
*r
;
1193 return (r
->class == rvc_nan
);
1196 /* Determine whether a floating-point value X is negative. */
1200 const REAL_VALUE_TYPE
*r
;
1205 /* Determine whether a floating-point value X is minus zero. */
1209 const REAL_VALUE_TYPE
*r
;
1211 return r
->sign
&& r
->class == rvc_zero
;
1214 /* Compare two floating-point objects for bitwise identity. */
1217 real_identical (a
, b
)
1218 const REAL_VALUE_TYPE
*a
, *b
;
1222 if (a
->class != b
->class)
1224 if (a
->sign
!= b
->sign
)
1234 if (a
->exp
!= b
->exp
)
1238 for (i
= 0; i
< SIGSZ
; ++i
)
1239 if (a
->sig
[i
] != b
->sig
[i
])
1250 /* Try to change R into its exact multiplicative inverse in machine
1251 mode MODE. Return true if successful. */
1254 exact_real_inverse (mode
, r
)
1255 enum machine_mode mode
;
1258 const REAL_VALUE_TYPE
*one
= real_digit (1);
1262 if (r
->class != rvc_normal
)
1265 /* Check for a power of two: all significand bits zero except the MSB. */
1266 for (i
= 0; i
< SIGSZ
-1; ++i
)
1269 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1272 /* Find the inverse and truncate to the required mode. */
1273 do_divide (&u
, one
, r
);
1274 real_convert (&u
, mode
, &u
);
1276 /* The rounding may have overflowed. */
1277 if (u
.class != rvc_normal
)
1279 for (i
= 0; i
< SIGSZ
-1; ++i
)
1282 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1289 /* Render R as an integer. */
1293 const REAL_VALUE_TYPE
*r
;
1295 unsigned HOST_WIDE_INT i
;
1306 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1314 if (r
->exp
> HOST_BITS_PER_WIDE_INT
)
1317 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1318 i
= r
->sig
[SIGSZ
-1];
1319 else if (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
)
1321 i
= r
->sig
[SIGSZ
-1];
1322 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1323 i
|= r
->sig
[SIGSZ
-2];
1328 i
>>= HOST_BITS_PER_WIDE_INT
- r
->exp
;
1339 /* Likewise, but to an integer pair, HI+LOW. */
1342 real_to_integer2 (plow
, phigh
, r
)
1343 HOST_WIDE_INT
*plow
, *phigh
;
1344 const REAL_VALUE_TYPE
*r
;
1347 HOST_WIDE_INT low
, high
;
1360 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1374 if (exp
>= 2*HOST_BITS_PER_WIDE_INT
)
1377 rshift_significand (&t
, r
, 2*HOST_BITS_PER_WIDE_INT
- exp
);
1378 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1380 high
= t
.sig
[SIGSZ
-1];
1381 low
= t
.sig
[SIGSZ
-2];
1383 else if (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
)
1385 high
= t
.sig
[SIGSZ
-1];
1386 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1387 high
|= t
.sig
[SIGSZ
-2];
1389 low
= t
.sig
[SIGSZ
-3];
1390 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1391 low
|= t
.sig
[SIGSZ
-4];
1401 low
= -low
, high
= ~high
;
1413 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1414 of NUM / DEN. Return the quotient and place the remainder in NUM.
1415 It is expected that NUM / DEN are close enough that the quotient is
1418 static unsigned long
1419 rtd_divmod (num
, den
)
1420 REAL_VALUE_TYPE
*num
, *den
;
1422 unsigned long q
, msb
;
1423 int expn
= num
->exp
, expd
= den
->exp
;
1432 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1434 lshift_significand_1 (num
, num
);
1436 if (msb
|| cmp_significands (num
, den
) >= 0)
1438 sub_significands (num
, num
, den
, 0);
1442 while (--expn
>= expd
);
1450 /* Render R as a decimal floating point constant. Emit DIGITS significant
1451 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1452 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1455 #define M_LOG10_2 0.30102999566398119521
1458 real_to_decimal (str
, r_orig
, buf_size
, digits
, crop_trailing_zeros
)
1460 const REAL_VALUE_TYPE
*r_orig
;
1461 size_t buf_size
, digits
;
1462 int crop_trailing_zeros
;
1464 const REAL_VALUE_TYPE
*one
, *ten
;
1465 REAL_VALUE_TYPE r
, pten
, u
, v
;
1466 int dec_exp
, cmp_one
, digit
;
1468 char *p
, *first
, *last
;
1475 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1480 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1483 /* ??? Print the significand as well, if not canonical? */
1484 strcpy (str
, (r
.sign
? "-NaN" : "+NaN"));
1490 /* Bound the number of digits printed by the size of the representation. */
1491 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1492 if (digits
== 0 || digits
> max_digits
)
1493 digits
= max_digits
;
1495 /* Estimate the decimal exponent, and compute the length of the string it
1496 will print as. Be conservative and add one to account for possible
1497 overflow or rounding error. */
1498 dec_exp
= r
.exp
* M_LOG10_2
;
1499 for (max_digits
= 1; dec_exp
; max_digits
++)
1502 /* Bound the number of digits printed by the size of the output buffer. */
1503 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1504 if (max_digits
> buf_size
)
1506 if (digits
> max_digits
)
1507 digits
= max_digits
;
1509 one
= real_digit (1);
1510 ten
= ten_to_ptwo (0);
1518 cmp_one
= do_compare (&r
, one
, 0);
1523 /* Number is greater than one. Convert significand to an integer
1524 and strip trailing decimal zeros. */
1527 u
.exp
= SIGNIFICAND_BITS
- 1;
1529 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1530 m
= floor_log2 (max_digits
);
1532 /* Iterate over the bits of the possible powers of 10 that might
1533 be present in U and eliminate them. That is, if we find that
1534 10**2**M divides U evenly, keep the division and increase
1540 do_divide (&t
, &u
, ten_to_ptwo (m
));
1541 do_fix_trunc (&v
, &t
);
1542 if (cmp_significands (&v
, &t
) == 0)
1550 /* Revert the scaling to integer that we performed earlier. */
1551 u
.exp
+= r
.exp
- (SIGNIFICAND_BITS
- 1);
1554 /* Find power of 10. Do this by dividing out 10**2**M when
1555 this is larger than the current remainder. Fill PTEN with
1556 the power of 10 that we compute. */
1559 m
= floor_log2 ((int)(r
.exp
* M_LOG10_2
)) + 1;
1562 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1563 if (do_compare (&u
, ptentwo
, 0) >= 0)
1565 do_divide (&u
, &u
, ptentwo
);
1566 do_multiply (&pten
, &pten
, ptentwo
);
1573 /* We managed to divide off enough tens in the above reduction
1574 loop that we've now got a negative exponent. Fall into the
1575 less-than-one code to compute the proper value for PTEN. */
1582 /* Number is less than one. Pad significand with leading
1588 /* Stop if we'd shift bits off the bottom. */
1592 do_multiply (&u
, &v
, ten
);
1594 /* Stop if we're now >= 1. */
1603 /* Find power of 10. Do this by multiplying in P=10**2**M when
1604 the current remainder is smaller than 1/P. Fill PTEN with the
1605 power of 10 that we compute. */
1606 m
= floor_log2 ((int)(-r
.exp
* M_LOG10_2
)) + 1;
1609 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1610 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1612 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1614 do_multiply (&v
, &v
, ptentwo
);
1615 do_multiply (&pten
, &pten
, ptentwo
);
1621 /* Invert the positive power of 10 that we've collected so far. */
1622 do_divide (&pten
, one
, &pten
);
1630 /* At this point, PTEN should contain the nearest power of 10 smaller
1631 than R, such that this division produces the first digit.
1633 Using a divide-step primitive that returns the complete integral
1634 remainder avoids the rounding error that would be produced if
1635 we were to use do_divide here and then simply multiply by 10 for
1636 each subsequent digit. */
1638 digit
= rtd_divmod (&r
, &pten
);
1640 /* Be prepared for error in that division via underflow ... */
1641 if (digit
== 0 && cmp_significand_0 (&r
))
1643 /* Multiply by 10 and try again. */
1644 do_multiply (&r
, &r
, ten
);
1645 digit
= rtd_divmod (&r
, &pten
);
1651 /* ... or overflow. */
1659 else if (digit
> 10)
1664 /* Generate subsequent digits. */
1665 while (--digits
> 0)
1667 do_multiply (&r
, &r
, ten
);
1668 digit
= rtd_divmod (&r
, &pten
);
1673 /* Generate one more digit with which to do rounding. */
1674 do_multiply (&r
, &r
, ten
);
1675 digit
= rtd_divmod (&r
, &pten
);
1677 /* Round the result. */
1680 /* Round to nearest. If R is nonzero there are additional
1681 nonzero digits to be extracted. */
1682 if (cmp_significand_0 (&r
))
1684 /* Round to even. */
1685 else if ((p
[-1] - '0') & 1)
1702 /* Carry out of the first digit. This means we had all 9's and
1703 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1711 /* Insert the decimal point. */
1712 first
[0] = first
[1];
1715 /* If requested, drop trailing zeros. Never crop past "1.0". */
1716 if (crop_trailing_zeros
)
1717 while (last
> first
+ 3 && last
[-1] == '0')
1720 /* Append the exponent. */
1721 sprintf (last
, "e%+d", dec_exp
);
1724 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1725 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1726 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1727 strip trailing zeros. */
1730 real_to_hexadecimal (str
, r
, buf_size
, digits
, crop_trailing_zeros
)
1732 const REAL_VALUE_TYPE
*r
;
1733 size_t buf_size
, digits
;
1734 int crop_trailing_zeros
;
1736 int i
, j
, exp
= r
->exp
;
1749 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1752 /* ??? Print the significand as well, if not canonical? */
1753 strcpy (str
, (r
->sign
? "-NaN" : "+NaN"));
1760 digits
= SIGNIFICAND_BITS
/ 4;
1762 /* Bound the number of digits printed by the size of the output buffer. */
1764 sprintf (exp_buf
, "p%+d", exp
);
1765 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1766 if (max_digits
> buf_size
)
1768 if (digits
> max_digits
)
1769 digits
= max_digits
;
1780 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1781 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1783 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1789 if (crop_trailing_zeros
)
1790 while (p
> first
+ 1 && p
[-1] == '0')
1793 sprintf (p
, "p%+d", exp
);
1796 /* Initialize R from a decimal or hexadecimal string. The string is
1797 assumed to have been syntax checked already. */
1800 real_from_string (r
, str
)
1814 else if (*str
== '+')
1817 if (str
[0] == '0' && str
[1] == 'x')
1819 /* Hexadecimal floating point. */
1820 int pos
= SIGNIFICAND_BITS
- 4, d
;
1828 d
= hex_value (*str
);
1833 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1834 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1843 if (pos
== SIGNIFICAND_BITS
- 4)
1850 d
= hex_value (*str
);
1855 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1856 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1862 if (*str
== 'p' || *str
== 'P')
1864 bool exp_neg
= false;
1872 else if (*str
== '+')
1876 while (ISDIGIT (*str
))
1882 /* Overflowed the exponent. */
1896 r
->class = rvc_normal
;
1903 /* Decimal floating point. */
1904 const REAL_VALUE_TYPE
*ten
= ten_to_ptwo (0);
1909 while (ISDIGIT (*str
))
1912 do_multiply (r
, r
, ten
);
1914 do_add (r
, r
, real_digit (d
), 0);
1919 if (r
->class == rvc_zero
)
1924 while (ISDIGIT (*str
))
1927 do_multiply (r
, r
, ten
);
1929 do_add (r
, r
, real_digit (d
), 0);
1934 if (*str
== 'e' || *str
== 'E')
1936 bool exp_neg
= false;
1944 else if (*str
== '+')
1948 while (ISDIGIT (*str
))
1954 /* Overflowed the exponent. */
1968 times_pten (r
, exp
);
1983 /* Legacy. Similar, but return the result directly. */
1986 real_from_string2 (s
, mode
)
1988 enum machine_mode mode
;
1992 real_from_string (&r
, s
);
1993 if (mode
!= VOIDmode
)
1994 real_convert (&r
, mode
, &r
);
1999 /* Initialize R from the integer pair HIGH+LOW. */
2002 real_from_integer (r
, mode
, low
, high
, unsigned_p
)
2004 enum machine_mode mode
;
2005 unsigned HOST_WIDE_INT low
;
2009 if (low
== 0 && high
== 0)
2013 r
->class = rvc_normal
;
2014 r
->sign
= high
< 0 && !unsigned_p
;
2015 r
->exp
= 2 * HOST_BITS_PER_WIDE_INT
;
2026 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2028 r
->sig
[SIGSZ
-1] = high
;
2029 r
->sig
[SIGSZ
-2] = low
;
2030 memset (r
->sig
, 0, sizeof(long)*(SIGSZ
-2));
2032 else if (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
)
2034 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2035 r
->sig
[SIGSZ
-2] = high
;
2036 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2037 r
->sig
[SIGSZ
-4] = low
;
2039 memset (r
->sig
, 0, sizeof(long)*(SIGSZ
-4));
2047 if (mode
!= VOIDmode
)
2048 real_convert (r
, mode
, r
);
2051 /* Returns 10**2**N. */
2053 static const REAL_VALUE_TYPE
*
2057 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2059 if (n
< 0 || n
>= EXP_BITS
)
2062 if (tens
[n
].class == rvc_zero
)
2064 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2066 HOST_WIDE_INT t
= 10;
2069 for (i
= 0; i
< n
; ++i
)
2072 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2076 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2077 do_multiply (&tens
[n
], t
, t
);
2084 /* Returns 10**(-2**N). */
2086 static const REAL_VALUE_TYPE
*
2090 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2092 if (n
< 0 || n
>= EXP_BITS
)
2095 if (tens
[n
].class == rvc_zero
)
2096 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2103 static const REAL_VALUE_TYPE
*
2107 static REAL_VALUE_TYPE num
[10];
2112 if (n
> 0 && num
[n
].class == rvc_zero
)
2113 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2118 /* Multiply R by 10**EXP. */
2125 REAL_VALUE_TYPE pten
, *rr
;
2126 bool negative
= (exp
< 0);
2132 pten
= *real_digit (1);
2138 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2140 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2143 do_divide (r
, r
, &pten
);
2146 /* Fills R with +Inf. */
2155 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2156 we force a QNaN, else we force an SNaN. The string, if not empty,
2157 is parsed as a number and placed in the significand. Return true
2158 if the string was successfully parsed. */
2161 real_nan (r
, str
, quiet
, mode
)
2165 enum machine_mode mode
;
2167 const struct real_format
*fmt
;
2169 fmt
= real_format_for_mode
[mode
- QFmode
];
2176 get_canonical_qnan (r
, 0);
2178 get_canonical_snan (r
, 0);
2185 memset (r
, 0, sizeof (*r
));
2188 /* Parse akin to strtol into the significand of R. */
2190 while (ISSPACE (*str
))
2194 else if (*str
== '+')
2204 while ((d
= hex_value (*str
)) < base
)
2211 lshift_significand (r
, r
, 3);
2214 lshift_significand (r
, r
, 4);
2217 lshift_significand_1 (&u
, r
);
2218 lshift_significand (r
, r
, 3);
2219 add_significands (r
, r
, &u
);
2227 add_significands (r
, r
, &u
);
2232 /* Must have consumed the entire string for success. */
2236 /* Shift the significand into place such that the bits
2237 are in the most significant bits for the format. */
2238 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->p
);
2240 /* Our MSB is always unset for NaNs. */
2241 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2243 /* Force quiet or signalling NaN. */
2245 r
->sig
[SIGSZ
-1] |= SIG_MSB
>> 1;
2247 r
->sig
[SIGSZ
-1] &= ~(SIG_MSB
>> 1);
2249 /* Force at least one bit of the significand set. */
2250 for (d
= 0; d
< SIGSZ
; ++d
)
2254 r
->sig
[SIGSZ
-1] |= SIG_MSB
>> 2;
2256 /* Our intermediate format forces QNaNs to have MSB-1 set.
2257 If the target format has QNaNs with the top bit unset,
2258 mirror the output routines and invert the top two bits. */
2259 if (!fmt
->qnan_msb_set
)
2260 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1) | (SIG_MSB
>> 2);
2266 /* Fills R with 2**N. */
2273 memset (r
, 0, sizeof (*r
));
2278 else if (n
< -MAX_EXP
)
2282 r
->class = rvc_normal
;
2284 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2290 round_for_format (fmt
, r
)
2291 const struct real_format
*fmt
;
2295 unsigned long sticky
;
2299 p2
= fmt
->p
* fmt
->log2_b
;
2300 emin2m1
= (fmt
->emin
- 1) * fmt
->log2_b
;
2301 emax2
= fmt
->emax
* fmt
->log2_b
;
2303 np2
= SIGNIFICAND_BITS
- p2
;
2307 get_zero (r
, r
->sign
);
2309 if (!fmt
->has_signed_zero
)
2314 get_inf (r
, r
->sign
);
2319 clear_significand_below (r
, np2
);
2321 /* If we've cleared the entire significand, we need one bit
2322 set for this to continue to be a NaN. */
2323 for (i
= 0; i
< SIGSZ
; ++i
)
2327 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 2;
2337 /* If we're not base2, normalize the exponent to a multiple of
2339 if (fmt
->log2_b
!= 1)
2341 int shift
= r
->exp
& (fmt
->log2_b
- 1);
2344 shift
= fmt
->log2_b
- shift
;
2345 r
->sig
[0] |= sticky_rshift_significand (r
, r
, shift
);
2350 /* Check the range of the exponent. If we're out of range,
2351 either underflow or overflow. */
2354 else if (r
->exp
<= emin2m1
)
2358 if (!fmt
->has_denorm
)
2360 /* Don't underflow completely until we've had a chance to round. */
2361 if (r
->exp
< emin2m1
)
2366 diff
= emin2m1
- r
->exp
+ 1;
2370 /* De-normalize the significand. */
2371 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2376 /* There are P2 true significand bits, followed by one guard bit,
2377 followed by one sticky bit, followed by stuff. Fold nonzero
2378 stuff into the sticky bit. */
2381 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2382 sticky
|= r
->sig
[i
];
2384 r
->sig
[w
] & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2386 guard
= test_significand_bit (r
, np2
- 1);
2387 lsb
= test_significand_bit (r
, np2
);
2389 /* Round to even. */
2390 if (guard
&& (sticky
|| lsb
))
2394 set_significand_bit (&u
, np2
);
2396 if (add_significands (r
, r
, &u
))
2398 /* Overflow. Means the significand had been all ones, and
2399 is now all zeros. Need to increase the exponent, and
2400 possibly re-normalize it. */
2401 if (++r
->exp
> emax2
)
2403 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2405 if (fmt
->log2_b
!= 1)
2407 int shift
= r
->exp
& (fmt
->log2_b
- 1);
2410 shift
= fmt
->log2_b
- shift
;
2411 rshift_significand (r
, r
, shift
);
2420 /* Catch underflow that we deferred until after rounding. */
2421 if (r
->exp
<= emin2m1
)
2424 /* Clear out trailing garbage. */
2425 clear_significand_below (r
, np2
);
2428 /* Extend or truncate to a new mode. */
2431 real_convert (r
, mode
, a
)
2433 enum machine_mode mode
;
2434 const REAL_VALUE_TYPE
*a
;
2436 const struct real_format
*fmt
;
2438 fmt
= real_format_for_mode
[mode
- QFmode
];
2443 round_for_format (fmt
, r
);
2445 /* round_for_format de-normalizes denormals. Undo just that part. */
2446 if (r
->class == rvc_normal
)
2450 /* Legacy. Likewise, except return the struct directly. */
2453 real_value_truncate (mode
, a
)
2454 enum machine_mode mode
;
2458 real_convert (&r
, mode
, &a
);
2462 /* Return true if truncating to MODE is exact. */
2465 exact_real_truncate (mode
, a
)
2466 enum machine_mode mode
;
2467 const REAL_VALUE_TYPE
*a
;
2470 real_convert (&t
, mode
, a
);
2471 return real_identical (&t
, a
);
2474 /* Write R to the given target format. Place the words of the result
2475 in target word order in BUF. There are always 32 bits in each
2476 long, no matter the size of the host long.
2478 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2481 real_to_target_fmt (buf
, r_orig
, fmt
)
2483 const REAL_VALUE_TYPE
*r_orig
;
2484 const struct real_format
*fmt
;
2490 round_for_format (fmt
, &r
);
2494 (*fmt
->encode
) (fmt
, buf
, &r
);
2499 /* Similar, but look up the format from MODE. */
2502 real_to_target (buf
, r
, mode
)
2504 const REAL_VALUE_TYPE
*r
;
2505 enum machine_mode mode
;
2507 const struct real_format
*fmt
;
2509 fmt
= real_format_for_mode
[mode
- QFmode
];
2513 return real_to_target_fmt (buf
, r
, fmt
);
2516 /* Read R from the given target format. Read the words of the result
2517 in target word order in BUF. There are always 32 bits in each
2518 long, no matter the size of the host long. */
2521 real_from_target_fmt (r
, buf
, fmt
)
2524 const struct real_format
*fmt
;
2526 (*fmt
->decode
) (fmt
, r
, buf
);
2529 /* Similar, but look up the format from MODE. */
2532 real_from_target (r
, buf
, mode
)
2535 enum machine_mode mode
;
2537 const struct real_format
*fmt
;
2539 fmt
= real_format_for_mode
[mode
- QFmode
];
2543 (*fmt
->decode
) (fmt
, r
, buf
);
2546 /* Return the number of bits in the significand for MODE. */
2547 /* ??? Legacy. Should get access to real_format directly. */
2550 significand_size (mode
)
2551 enum machine_mode mode
;
2553 const struct real_format
*fmt
;
2555 fmt
= real_format_for_mode
[mode
- QFmode
];
2559 return fmt
->p
* fmt
->log2_b
;
2562 /* Return a hash value for the given real value. */
2563 /* ??? The "unsigned int" return value is intended to be hashval_t,
2564 but I didn't want to pull hashtab.h into real.h. */
2568 const REAL_VALUE_TYPE
*r
;
2573 h
= r
->class | (r
->sign
<< 2);
2585 if (sizeof(unsigned long) > sizeof(unsigned int))
2586 for (i
= 0; i
< SIGSZ
; ++i
)
2588 unsigned long s
= r
->sig
[i
];
2589 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2592 for (i
= 0; i
< SIGSZ
; ++i
)
2603 /* IEEE single-precision format. */
2605 static void encode_ieee_single
PARAMS ((const struct real_format
*fmt
,
2606 long *, const REAL_VALUE_TYPE
*));
2607 static void decode_ieee_single
PARAMS ((const struct real_format
*,
2608 REAL_VALUE_TYPE
*, const long *));
2611 encode_ieee_single (fmt
, buf
, r
)
2612 const struct real_format
*fmt
;
2614 const REAL_VALUE_TYPE
*r
;
2616 unsigned long image
, sig
, exp
;
2617 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2619 image
= r
->sign
<< 31;
2620 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2631 image
|= 0x7fffffff;
2639 if (!fmt
->qnan_msb_set
)
2640 image
^= 1 << 23 | 1 << 22;
2643 image
|= 0x7fffffff;
2647 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2648 whereas the intermediate representation is 0.F x 2**exp.
2649 Which means we're off by one. */
2653 exp
= r
->exp
+ 127 - 1;
2666 decode_ieee_single (fmt
, r
, buf
)
2667 const struct real_format
*fmt
;
2671 unsigned long image
= buf
[0] & 0xffffffff;
2672 bool sign
= (image
>> 31) & 1;
2673 int exp
= (image
>> 23) & 0xff;
2675 memset (r
, 0, sizeof (*r
));
2676 image
<<= HOST_BITS_PER_LONG
- 24;
2681 if (image
&& fmt
->has_denorm
)
2683 r
->class = rvc_normal
;
2686 r
->sig
[SIGSZ
-1] = image
<< 1;
2689 else if (fmt
->has_signed_zero
)
2692 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2698 if (!fmt
->qnan_msb_set
)
2699 image
^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
2700 r
->sig
[SIGSZ
-1] = image
;
2710 r
->class = rvc_normal
;
2712 r
->exp
= exp
- 127 + 1;
2713 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2717 const struct real_format ieee_single_format
=
2735 /* IEEE double-precision format. */
2737 static void encode_ieee_double
PARAMS ((const struct real_format
*fmt
,
2738 long *, const REAL_VALUE_TYPE
*));
2739 static void decode_ieee_double
PARAMS ((const struct real_format
*,
2740 REAL_VALUE_TYPE
*, const long *));
2743 encode_ieee_double (fmt
, buf
, r
)
2744 const struct real_format
*fmt
;
2746 const REAL_VALUE_TYPE
*r
;
2748 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
2749 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2751 image_hi
= r
->sign
<< 31;
2754 if (HOST_BITS_PER_LONG
== 64)
2756 sig_hi
= r
->sig
[SIGSZ
-1];
2757 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
2758 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
2762 sig_hi
= r
->sig
[SIGSZ
-1];
2763 sig_lo
= r
->sig
[SIGSZ
-2];
2764 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
2765 sig_hi
= (sig_hi
>> 11) & 0xfffff;
2775 image_hi
|= 2047 << 20;
2778 image_hi
|= 0x7fffffff;
2779 image_lo
= 0xffffffff;
2786 image_hi
|= 2047 << 20;
2788 if (!fmt
->qnan_msb_set
)
2789 image_hi
^= 1 << 19 | 1 << 18;
2794 image_hi
|= 0x7fffffff;
2795 image_lo
= 0xffffffff;
2800 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2801 whereas the intermediate representation is 0.F x 2**exp.
2802 Which means we're off by one. */
2806 exp
= r
->exp
+ 1023 - 1;
2807 image_hi
|= exp
<< 20;
2816 if (FLOAT_WORDS_BIG_ENDIAN
)
2817 buf
[0] = image_hi
, buf
[1] = image_lo
;
2819 buf
[0] = image_lo
, buf
[1] = image_hi
;
2823 decode_ieee_double (fmt
, r
, buf
)
2824 const struct real_format
*fmt
;
2828 unsigned long image_hi
, image_lo
;
2832 if (FLOAT_WORDS_BIG_ENDIAN
)
2833 image_hi
= buf
[0], image_lo
= buf
[1];
2835 image_lo
= buf
[0], image_hi
= buf
[1];
2836 image_lo
&= 0xffffffff;
2837 image_hi
&= 0xffffffff;
2839 sign
= (image_hi
>> 31) & 1;
2840 exp
= (image_hi
>> 20) & 0x7ff;
2842 memset (r
, 0, sizeof (*r
));
2844 image_hi
<<= 32 - 21;
2845 image_hi
|= image_lo
>> 21;
2846 image_hi
&= 0x7fffffff;
2847 image_lo
<<= 32 - 21;
2851 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
2853 r
->class = rvc_normal
;
2856 if (HOST_BITS_PER_LONG
== 32)
2858 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
2860 r
->sig
[SIGSZ
-1] = image_hi
;
2861 r
->sig
[SIGSZ
-2] = image_lo
;
2865 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
2866 r
->sig
[SIGSZ
-1] = image_hi
;
2870 else if (fmt
->has_signed_zero
)
2873 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
2875 if (image_hi
|| image_lo
)
2879 if (HOST_BITS_PER_LONG
== 32)
2881 r
->sig
[SIGSZ
-1] = image_hi
;
2882 r
->sig
[SIGSZ
-2] = image_lo
;
2885 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
2887 if (!fmt
->qnan_msb_set
)
2888 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
2898 r
->class = rvc_normal
;
2900 r
->exp
= exp
- 1023 + 1;
2901 if (HOST_BITS_PER_LONG
== 32)
2903 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
2904 r
->sig
[SIGSZ
-2] = image_lo
;
2907 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
2911 const struct real_format ieee_double_format
=
2929 /* IEEE extended double precision format. This comes in three
2930 flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2933 static void encode_ieee_extended
PARAMS ((const struct real_format
*fmt
,
2934 long *, const REAL_VALUE_TYPE
*));
2935 static void decode_ieee_extended
PARAMS ((const struct real_format
*,
2936 REAL_VALUE_TYPE
*, const long *));
2938 static void encode_ieee_extended_128
PARAMS ((const struct real_format
*fmt
,
2940 const REAL_VALUE_TYPE
*));
2941 static void decode_ieee_extended_128
PARAMS ((const struct real_format
*,
2946 encode_ieee_extended (fmt
, buf
, r
)
2947 const struct real_format
*fmt
;
2949 const REAL_VALUE_TYPE
*r
;
2951 unsigned long image_hi
, sig_hi
, sig_lo
;
2952 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2954 image_hi
= r
->sign
<< 15;
2955 sig_hi
= sig_lo
= 0;
2967 /* Intel requires the explicit integer bit to be set, otherwise
2968 it considers the value a "pseudo-infinity". Motorola docs
2969 say it doesn't care. */
2970 sig_hi
= 0x80000000;
2975 sig_lo
= sig_hi
= 0xffffffff;
2983 if (HOST_BITS_PER_LONG
== 32)
2985 sig_hi
= r
->sig
[SIGSZ
-1];
2986 sig_lo
= r
->sig
[SIGSZ
-2];
2990 sig_lo
= r
->sig
[SIGSZ
-1];
2991 sig_hi
= sig_lo
>> 31 >> 1;
2992 sig_lo
&= 0xffffffff;
2994 if (!fmt
->qnan_msb_set
)
2995 sig_hi
^= 1 << 30 | 1 << 29;
2997 /* Intel requires the explicit integer bit to be set, otherwise
2998 it considers the value a "pseudo-nan". Motorola docs say it
3000 sig_hi
|= 0x80000000;
3005 sig_lo
= sig_hi
= 0xffffffff;
3013 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3014 whereas the intermediate representation is 0.F x 2**exp.
3015 Which means we're off by one.
3017 Except for Motorola, which consider exp=0 and explicit
3018 integer bit set to continue to be normalized. In theory
3019 this discrepancy has been taken care of by the difference
3020 in fmt->emin in round_for_format. */
3032 if (HOST_BITS_PER_LONG
== 32)
3034 sig_hi
= r
->sig
[SIGSZ
-1];
3035 sig_lo
= r
->sig
[SIGSZ
-2];
3039 sig_lo
= r
->sig
[SIGSZ
-1];
3040 sig_hi
= sig_lo
>> 31 >> 1;
3041 sig_lo
&= 0xffffffff;
3050 if (FLOAT_WORDS_BIG_ENDIAN
)
3051 buf
[0] = image_hi
<< 16, buf
[1] = sig_hi
, buf
[2] = sig_lo
;
3053 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3057 encode_ieee_extended_128 (fmt
, buf
, r
)
3058 const struct real_format
*fmt
;
3060 const REAL_VALUE_TYPE
*r
;
3062 buf
[3 * !FLOAT_WORDS_BIG_ENDIAN
] = 0;
3063 encode_ieee_extended (fmt
, buf
+!!FLOAT_WORDS_BIG_ENDIAN
, r
);
3067 decode_ieee_extended (fmt
, r
, buf
)
3068 const struct real_format
*fmt
;
3072 unsigned long image_hi
, sig_hi
, sig_lo
;
3076 if (FLOAT_WORDS_BIG_ENDIAN
)
3077 image_hi
= buf
[0] >> 16, sig_hi
= buf
[1], sig_lo
= buf
[2];
3079 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3080 sig_lo
&= 0xffffffff;
3081 sig_hi
&= 0xffffffff;
3082 image_hi
&= 0xffffffff;
3084 sign
= (image_hi
>> 15) & 1;
3085 exp
= image_hi
& 0x7fff;
3087 memset (r
, 0, sizeof (*r
));
3091 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3093 r
->class = rvc_normal
;
3096 /* When the IEEE format contains a hidden bit, we know that
3097 it's zero at this point, and so shift up the significand
3098 and decrease the exponent to match. In this case, Motorola
3099 defines the explicit integer bit to be valid, so we don't
3100 know whether the msb is set or not. */
3102 if (HOST_BITS_PER_LONG
== 32)
3104 r
->sig
[SIGSZ
-1] = sig_hi
;
3105 r
->sig
[SIGSZ
-2] = sig_lo
;
3108 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3112 else if (fmt
->has_signed_zero
)
3115 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3117 /* See above re "pseudo-infinities" and "pseudo-nans".
3118 Short summary is that the MSB will likely always be
3119 set, and that we don't care about it. */
3120 sig_hi
&= 0x7fffffff;
3122 if (sig_hi
|| sig_lo
)
3126 if (HOST_BITS_PER_LONG
== 32)
3128 r
->sig
[SIGSZ
-1] = sig_hi
;
3129 r
->sig
[SIGSZ
-2] = sig_lo
;
3132 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3134 if (!fmt
->qnan_msb_set
)
3135 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
3145 r
->class = rvc_normal
;
3147 r
->exp
= exp
- 16383 + 1;
3148 if (HOST_BITS_PER_LONG
== 32)
3150 r
->sig
[SIGSZ
-1] = sig_hi
;
3151 r
->sig
[SIGSZ
-2] = sig_lo
;
3154 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3159 decode_ieee_extended_128 (fmt
, r
, buf
)
3160 const struct real_format
*fmt
;
3164 decode_ieee_extended (fmt
, r
, buf
+!!FLOAT_WORDS_BIG_ENDIAN
);
3167 const struct real_format ieee_extended_motorola_format
=
3169 encode_ieee_extended
,
3170 decode_ieee_extended
,
3184 const struct real_format ieee_extended_intel_96_format
=
3186 encode_ieee_extended
,
3187 decode_ieee_extended
,
3201 const struct real_format ieee_extended_intel_128_format
=
3203 encode_ieee_extended_128
,
3204 decode_ieee_extended_128
,
3219 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3220 numbers whose sum is equal to the extended precision value. The number
3221 with greater magnitude is first. This format has the same magnitude
3222 range as an IEEE double precision value, but effectively 106 bits of
3223 significand precision. Infinity and NaN are represented by their IEEE
3224 double precision value stored in the first number, the second number is
3225 ignored. Zeroes, Infinities, and NaNs are set in both doubles
3226 due to precedent. */
3228 static void encode_ibm_extended
PARAMS ((const struct real_format
*fmt
,
3229 long *, const REAL_VALUE_TYPE
*));
3230 static void decode_ibm_extended
PARAMS ((const struct real_format
*,
3231 REAL_VALUE_TYPE
*, const long *));
3234 encode_ibm_extended (fmt
, buf
, r
)
3235 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3237 const REAL_VALUE_TYPE
*r
;
3239 REAL_VALUE_TYPE u
, v
;
3244 /* Both doubles have sign bit set. */
3245 buf
[0] = FLOAT_WORDS_BIG_ENDIAN
? r
->sign
<< 31 : 0;
3246 buf
[1] = FLOAT_WORDS_BIG_ENDIAN
? 0 : r
->sign
<< 31;
3253 /* Both doubles set to Inf / NaN. */
3254 encode_ieee_double (&ieee_double_format
, &buf
[0], r
);
3260 /* u = IEEE double precision portion of significand. */
3262 clear_significand_below (&u
, SIGNIFICAND_BITS
- 53);
3265 /* If the upper double is zero, we have a denormal double, so
3266 move it to the first double and leave the second as zero. */
3267 if (u
.class == rvc_zero
)
3275 /* v = remainder containing additional 53 bits of significand. */
3276 do_add (&v
, r
, &u
, 1);
3277 round_for_format (&ieee_double_format
, &v
);
3280 round_for_format (&ieee_double_format
, &u
);
3282 encode_ieee_double (&ieee_double_format
, &buf
[0], &u
);
3283 encode_ieee_double (&ieee_double_format
, &buf
[2], &v
);
3292 decode_ibm_extended (fmt
, r
, buf
)
3293 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3297 REAL_VALUE_TYPE u
, v
;
3299 decode_ieee_double (&ieee_double_format
, &u
, &buf
[0]);
3301 if (u
.class != rvc_zero
&& u
.class != rvc_inf
&& u
.class != rvc_nan
)
3303 decode_ieee_double (&ieee_double_format
, &v
, &buf
[2]);
3304 do_add (r
, &u
, &v
, 0);
3310 const struct real_format ibm_extended_format
=
3312 encode_ibm_extended
,
3313 decode_ibm_extended
,
3328 /* IEEE quad precision format. */
3330 static void encode_ieee_quad
PARAMS ((const struct real_format
*fmt
,
3331 long *, const REAL_VALUE_TYPE
*));
3332 static void decode_ieee_quad
PARAMS ((const struct real_format
*,
3333 REAL_VALUE_TYPE
*, const long *));
3336 encode_ieee_quad (fmt
, buf
, r
)
3337 const struct real_format
*fmt
;
3339 const REAL_VALUE_TYPE
*r
;
3341 unsigned long image3
, image2
, image1
, image0
, exp
;
3342 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3345 image3
= r
->sign
<< 31;
3350 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3359 image3
|= 32767 << 16;
3362 image3
|= 0x7fffffff;
3363 image2
= 0xffffffff;
3364 image1
= 0xffffffff;
3365 image0
= 0xffffffff;
3372 image3
|= 32767 << 16;
3374 if (HOST_BITS_PER_LONG
== 32)
3379 image3
|= u
.sig
[3] & 0xffff;
3384 image1
= image0
>> 31 >> 1;
3386 image3
|= (image2
>> 31 >> 1) & 0xffff;
3387 image0
&= 0xffffffff;
3388 image2
&= 0xffffffff;
3391 if (!fmt
->qnan_msb_set
)
3392 image3
^= 1 << 15 | 1 << 14;
3396 image3
|= 0x7fffffff;
3397 image2
= 0xffffffff;
3398 image1
= 0xffffffff;
3399 image0
= 0xffffffff;
3404 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3405 whereas the intermediate representation is 0.F x 2**exp.
3406 Which means we're off by one. */
3410 exp
= r
->exp
+ 16383 - 1;
3411 image3
|= exp
<< 16;
3413 if (HOST_BITS_PER_LONG
== 32)
3418 image3
|= u
.sig
[3] & 0xffff;
3423 image1
= image0
>> 31 >> 1;
3425 image3
|= (image2
>> 31 >> 1) & 0xffff;
3426 image0
&= 0xffffffff;
3427 image2
&= 0xffffffff;
3435 if (FLOAT_WORDS_BIG_ENDIAN
)
3452 decode_ieee_quad (fmt
, r
, buf
)
3453 const struct real_format
*fmt
;
3457 unsigned long image3
, image2
, image1
, image0
;
3461 if (FLOAT_WORDS_BIG_ENDIAN
)
3475 image0
&= 0xffffffff;
3476 image1
&= 0xffffffff;
3477 image2
&= 0xffffffff;
3479 sign
= (image3
>> 31) & 1;
3480 exp
= (image3
>> 16) & 0x7fff;
3483 memset (r
, 0, sizeof (*r
));
3487 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
3489 r
->class = rvc_normal
;
3492 r
->exp
= -16382 + (SIGNIFICAND_BITS
- 112);
3493 if (HOST_BITS_PER_LONG
== 32)
3502 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3503 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3508 else if (fmt
->has_signed_zero
)
3511 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3513 if (image3
| image2
| image1
| image0
)
3518 if (HOST_BITS_PER_LONG
== 32)
3527 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3528 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3530 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3532 if (!fmt
->qnan_msb_set
)
3533 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
3543 r
->class = rvc_normal
;
3545 r
->exp
= exp
- 16383 + 1;
3547 if (HOST_BITS_PER_LONG
== 32)
3556 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3557 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3559 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3560 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3564 const struct real_format ieee_quad_format
=
3581 /* Descriptions of VAX floating point formats can be found beginning at
3583 http://www.openvms.compaq.com:8000/73final/4515/4515pro_013.html#f_floating_point_format
3585 The thing to remember is that they're almost IEEE, except for word
3586 order, exponent bias, and the lack of infinities, nans, and denormals.
3588 We don't implement the H_floating format here, simply because neither
3589 the VAX or Alpha ports use it. */
3591 static void encode_vax_f
PARAMS ((const struct real_format
*fmt
,
3592 long *, const REAL_VALUE_TYPE
*));
3593 static void decode_vax_f
PARAMS ((const struct real_format
*,
3594 REAL_VALUE_TYPE
*, const long *));
3595 static void encode_vax_d
PARAMS ((const struct real_format
*fmt
,
3596 long *, const REAL_VALUE_TYPE
*));
3597 static void decode_vax_d
PARAMS ((const struct real_format
*,
3598 REAL_VALUE_TYPE
*, const long *));
3599 static void encode_vax_g
PARAMS ((const struct real_format
*fmt
,
3600 long *, const REAL_VALUE_TYPE
*));
3601 static void decode_vax_g
PARAMS ((const struct real_format
*,
3602 REAL_VALUE_TYPE
*, const long *));
3605 encode_vax_f (fmt
, buf
, r
)
3606 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3608 const REAL_VALUE_TYPE
*r
;
3610 unsigned long sign
, exp
, sig
, image
;
3612 sign
= r
->sign
<< 15;
3622 image
= 0xffff7fff | sign
;
3626 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
3629 image
= (sig
<< 16) & 0xffff0000;
3643 decode_vax_f (fmt
, r
, buf
)
3644 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3648 unsigned long image
= buf
[0] & 0xffffffff;
3649 int exp
= (image
>> 7) & 0xff;
3651 memset (r
, 0, sizeof (*r
));
3655 r
->class = rvc_normal
;
3656 r
->sign
= (image
>> 15) & 1;
3659 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
3660 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
3665 encode_vax_d (fmt
, buf
, r
)
3666 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3668 const REAL_VALUE_TYPE
*r
;
3670 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3675 image0
= image1
= 0;
3680 image0
= 0xffff7fff | sign
;
3681 image1
= 0xffffffff;
3685 /* Extract the significand into straight hi:lo. */
3686 if (HOST_BITS_PER_LONG
== 64)
3688 image0
= r
->sig
[SIGSZ
-1];
3689 image1
= (image0
>> (64 - 56)) & 0xffffffff;
3690 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
3694 image0
= r
->sig
[SIGSZ
-1];
3695 image1
= r
->sig
[SIGSZ
-2];
3696 image1
= (image0
<< 24) | (image1
>> 8);
3697 image0
= (image0
>> 8) & 0xffffff;
3700 /* Rearrange the half-words of the significand to match the
3702 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
3703 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
3705 /* Add the sign and exponent. */
3707 image0
|= (r
->exp
+ 128) << 7;
3714 if (FLOAT_WORDS_BIG_ENDIAN
)
3715 buf
[0] = image1
, buf
[1] = image0
;
3717 buf
[0] = image0
, buf
[1] = image1
;
3721 decode_vax_d (fmt
, r
, buf
)
3722 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3726 unsigned long image0
, image1
;
3729 if (FLOAT_WORDS_BIG_ENDIAN
)
3730 image1
= buf
[0], image0
= buf
[1];
3732 image0
= buf
[0], image1
= buf
[1];
3733 image0
&= 0xffffffff;
3734 image1
&= 0xffffffff;
3736 exp
= (image0
>> 7) & 0x7f;
3738 memset (r
, 0, sizeof (*r
));
3742 r
->class = rvc_normal
;
3743 r
->sign
= (image0
>> 15) & 1;
3746 /* Rearrange the half-words of the external format into
3747 proper ascending order. */
3748 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
3749 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
3751 if (HOST_BITS_PER_LONG
== 64)
3753 image0
= (image0
<< 31 << 1) | image1
;
3756 r
->sig
[SIGSZ
-1] = image0
;
3760 r
->sig
[SIGSZ
-1] = image0
;
3761 r
->sig
[SIGSZ
-2] = image1
;
3762 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
3763 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3769 encode_vax_g (fmt
, buf
, r
)
3770 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3772 const REAL_VALUE_TYPE
*r
;
3774 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3779 image0
= image1
= 0;
3784 image0
= 0xffff7fff | sign
;
3785 image1
= 0xffffffff;
3789 /* Extract the significand into straight hi:lo. */
3790 if (HOST_BITS_PER_LONG
== 64)
3792 image0
= r
->sig
[SIGSZ
-1];
3793 image1
= (image0
>> (64 - 53)) & 0xffffffff;
3794 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
3798 image0
= r
->sig
[SIGSZ
-1];
3799 image1
= r
->sig
[SIGSZ
-2];
3800 image1
= (image0
<< 21) | (image1
>> 11);
3801 image0
= (image0
>> 11) & 0xfffff;
3804 /* Rearrange the half-words of the significand to match the
3806 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
3807 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
3809 /* Add the sign and exponent. */
3811 image0
|= (r
->exp
+ 1024) << 4;
3818 if (FLOAT_WORDS_BIG_ENDIAN
)
3819 buf
[0] = image1
, buf
[1] = image0
;
3821 buf
[0] = image0
, buf
[1] = image1
;
3825 decode_vax_g (fmt
, r
, buf
)
3826 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3830 unsigned long image0
, image1
;
3833 if (FLOAT_WORDS_BIG_ENDIAN
)
3834 image1
= buf
[0], image0
= buf
[1];
3836 image0
= buf
[0], image1
= buf
[1];
3837 image0
&= 0xffffffff;
3838 image1
&= 0xffffffff;
3840 exp
= (image0
>> 4) & 0x7ff;
3842 memset (r
, 0, sizeof (*r
));
3846 r
->class = rvc_normal
;
3847 r
->sign
= (image0
>> 15) & 1;
3848 r
->exp
= exp
- 1024;
3850 /* Rearrange the half-words of the external format into
3851 proper ascending order. */
3852 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
3853 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
3855 if (HOST_BITS_PER_LONG
== 64)
3857 image0
= (image0
<< 31 << 1) | image1
;
3860 r
->sig
[SIGSZ
-1] = image0
;
3864 r
->sig
[SIGSZ
-1] = image0
;
3865 r
->sig
[SIGSZ
-2] = image1
;
3866 lshift_significand (r
, r
, 64 - 53);
3867 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3872 const struct real_format vax_f_format
=
3889 const struct real_format vax_d_format
=
3906 const struct real_format vax_g_format
=
3923 /* A good reference for these can be found in chapter 9 of
3924 "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
3925 An on-line version can be found here:
3927 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3930 static void encode_i370_single
PARAMS ((const struct real_format
*fmt
,
3931 long *, const REAL_VALUE_TYPE
*));
3932 static void decode_i370_single
PARAMS ((const struct real_format
*,
3933 REAL_VALUE_TYPE
*, const long *));
3934 static void encode_i370_double
PARAMS ((const struct real_format
*fmt
,
3935 long *, const REAL_VALUE_TYPE
*));
3936 static void decode_i370_double
PARAMS ((const struct real_format
*,
3937 REAL_VALUE_TYPE
*, const long *));
3940 encode_i370_single (fmt
, buf
, r
)
3941 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3943 const REAL_VALUE_TYPE
*r
;
3945 unsigned long sign
, exp
, sig
, image
;
3947 sign
= r
->sign
<< 31;
3957 image
= 0x7fffffff | sign
;
3961 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0xffffff;
3962 exp
= ((r
->exp
/ 4) + 64) << 24;
3963 image
= sign
| exp
| sig
;
3974 decode_i370_single (fmt
, r
, buf
)
3975 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3979 unsigned long sign
, sig
, image
= buf
[0];
3982 sign
= (image
>> 31) & 1;
3983 exp
= (image
>> 24) & 0x7f;
3984 sig
= image
& 0xffffff;
3986 memset (r
, 0, sizeof (*r
));
3990 r
->class = rvc_normal
;
3992 r
->exp
= (exp
- 64) * 4;
3993 r
->sig
[SIGSZ
-1] = sig
<< (HOST_BITS_PER_LONG
- 24);
3999 encode_i370_double (fmt
, buf
, r
)
4000 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4002 const REAL_VALUE_TYPE
*r
;
4004 unsigned long sign
, exp
, image_hi
, image_lo
;
4006 sign
= r
->sign
<< 31;
4011 image_hi
= image_lo
= 0;
4016 image_hi
= 0x7fffffff | sign
;
4017 image_lo
= 0xffffffff;
4021 if (HOST_BITS_PER_LONG
== 64)
4023 image_hi
= r
->sig
[SIGSZ
-1];
4024 image_lo
= (image_hi
>> (64 - 56)) & 0xffffffff;
4025 image_hi
= (image_hi
>> (64 - 56 + 1) >> 31) & 0xffffff;
4029 image_hi
= r
->sig
[SIGSZ
-1];
4030 image_lo
= r
->sig
[SIGSZ
-2];
4031 image_lo
= (image_lo
>> 8) | (image_hi
<< 24);
4035 exp
= ((r
->exp
/ 4) + 64) << 24;
4036 image_hi
|= sign
| exp
;
4043 if (FLOAT_WORDS_BIG_ENDIAN
)
4044 buf
[0] = image_hi
, buf
[1] = image_lo
;
4046 buf
[0] = image_lo
, buf
[1] = image_hi
;
4050 decode_i370_double (fmt
, r
, buf
)
4051 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4055 unsigned long sign
, image_hi
, image_lo
;
4058 if (FLOAT_WORDS_BIG_ENDIAN
)
4059 image_hi
= buf
[0], image_lo
= buf
[1];
4061 image_lo
= buf
[0], image_hi
= buf
[1];
4063 sign
= (image_hi
>> 31) & 1;
4064 exp
= (image_hi
>> 24) & 0x7f;
4065 image_hi
&= 0xffffff;
4066 image_lo
&= 0xffffffff;
4068 memset (r
, 0, sizeof (*r
));
4070 if (exp
|| image_hi
|| image_lo
)
4072 r
->class = rvc_normal
;
4074 r
->exp
= (exp
- 64) * 4 + (SIGNIFICAND_BITS
- 56);
4076 if (HOST_BITS_PER_LONG
== 32)
4078 r
->sig
[0] = image_lo
;
4079 r
->sig
[1] = image_hi
;
4082 r
->sig
[0] = image_lo
| (image_hi
<< 31 << 1);
4088 const struct real_format i370_single_format
=
4100 false, /* ??? The encoding does allow for "unnormals". */
4101 false, /* ??? The encoding does allow for "unnormals". */
4105 const struct real_format i370_double_format
=
4117 false, /* ??? The encoding does allow for "unnormals". */
4118 false, /* ??? The encoding does allow for "unnormals". */
4122 /* The "twos-complement" c4x format is officially defined as
4126 This is rather misleading. One must remember that F is signed.
4127 A better description would be
4129 x = -1**s * ((s + 1 + .f) * 2**e
4131 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4132 that's -1 * (1+1+(-.5)) == -1.5. I think.
4134 The constructions here are taken from Tables 5-1 and 5-2 of the
4135 TMS320C4x User's Guide wherein step-by-step instructions for
4136 conversion from IEEE are presented. That's close enough to our
4137 internal representation so as to make things easy.
4139 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
4141 static void encode_c4x_single
PARAMS ((const struct real_format
*fmt
,
4142 long *, const REAL_VALUE_TYPE
*));
4143 static void decode_c4x_single
PARAMS ((const struct real_format
*,
4144 REAL_VALUE_TYPE
*, const long *));
4145 static void encode_c4x_extended
PARAMS ((const struct real_format
*fmt
,
4146 long *, const REAL_VALUE_TYPE
*));
4147 static void decode_c4x_extended
PARAMS ((const struct real_format
*,
4148 REAL_VALUE_TYPE
*, const long *));
4151 encode_c4x_single (fmt
, buf
, r
)
4152 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4154 const REAL_VALUE_TYPE
*r
;
4156 unsigned long image
, exp
, sig
;
4168 sig
= 0x800000 - r
->sign
;
4173 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4188 image
= ((exp
& 0xff) << 24) | (sig
& 0xffffff);
4193 decode_c4x_single (fmt
, r
, buf
)
4194 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4198 unsigned long image
= buf
[0];
4202 exp
= (((image
>> 24) & 0xff) ^ 0x80) - 0x80;
4203 sf
= ((image
& 0xffffff) ^ 0x800000) - 0x800000;
4205 memset (r
, 0, sizeof (*r
));
4209 r
->class = rvc_normal
;
4211 sig
= sf
& 0x7fffff;
4220 sig
= (sig
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4223 r
->sig
[SIGSZ
-1] = sig
;
4228 encode_c4x_extended (fmt
, buf
, r
)
4229 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4231 const REAL_VALUE_TYPE
*r
;
4233 unsigned long exp
, sig
;
4245 sig
= 0x80000000 - r
->sign
;
4251 sig
= r
->sig
[SIGSZ
-1];
4252 if (HOST_BITS_PER_LONG
== 64)
4253 sig
= sig
>> 1 >> 31;
4270 exp
= (exp
& 0xff) << 24;
4273 if (FLOAT_WORDS_BIG_ENDIAN
)
4274 buf
[0] = exp
, buf
[1] = sig
;
4276 buf
[0] = sig
, buf
[0] = exp
;
4280 decode_c4x_extended (fmt
, r
, buf
)
4281 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4288 if (FLOAT_WORDS_BIG_ENDIAN
)
4289 exp
= buf
[0], sf
= buf
[1];
4291 sf
= buf
[0], exp
= buf
[1];
4293 exp
= (((exp
>> 24) & 0xff) & 0x80) - 0x80;
4294 sf
= ((sf
& 0xffffffff) ^ 0x80000000) - 0x80000000;
4296 memset (r
, 0, sizeof (*r
));
4300 r
->class = rvc_normal
;
4302 sig
= sf
& 0x7fffffff;
4311 if (HOST_BITS_PER_LONG
== 64)
4312 sig
= sig
<< 1 << 31;
4316 r
->sig
[SIGSZ
-1] = sig
;
4320 const struct real_format c4x_single_format
=
4337 const struct real_format c4x_extended_format
=
4339 encode_c4x_extended
,
4340 decode_c4x_extended
,
4355 /* A synthetic "format" for internal arithmetic. It's the size of the
4356 internal significand minus the two bits needed for proper rounding.
4357 The encode and decode routines exist only to satisfy our paranoia
4360 static void encode_internal
PARAMS ((const struct real_format
*fmt
,
4361 long *, const REAL_VALUE_TYPE
*));
4362 static void decode_internal
PARAMS ((const struct real_format
*,
4363 REAL_VALUE_TYPE
*, const long *));
4366 encode_internal (fmt
, buf
, r
)
4367 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4369 const REAL_VALUE_TYPE
*r
;
4371 memcpy (buf
, r
, sizeof (*r
));
4375 decode_internal (fmt
, r
, buf
)
4376 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4380 memcpy (r
, buf
, sizeof (*r
));
4383 const struct real_format real_internal_format
=
4389 SIGNIFICAND_BITS
- 2,
4400 /* Set up default mode to format mapping for IEEE. Everyone else has
4401 to set these values in OVERRIDE_OPTIONS. */
4403 const struct real_format
*real_format_for_mode
[TFmode
- QFmode
+ 1] =
4408 &ieee_single_format
, /* SFmode */
4409 &ieee_double_format
, /* DFmode */
4411 /* We explicitly don't handle XFmode. There are two formats,
4412 pretty much equally common. Choose one in OVERRIDE_OPTIONS. */
4414 &ieee_quad_format
/* TFmode */
4418 /* Calculate the square root of X in mode MODE, and store the result
4419 in R. Return TRUE if the operation does not raise an exception.
4420 For details see "High Precision Division and Square Root",
4421 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4422 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4425 real_sqrt (r
, mode
, x
)
4427 enum machine_mode mode
;
4428 const REAL_VALUE_TYPE
*x
;
4430 static REAL_VALUE_TYPE halfthree
;
4431 static REAL_VALUE_TYPE half
;
4432 static bool init
= false;
4433 REAL_VALUE_TYPE h
, t
, i
;
4436 /* sqrt(-0.0) is -0.0. */
4437 if (real_isnegzero (x
))
4443 /* Negative arguments return NaN. */
4446 /* Mode is ignored for canonical NaN. */
4447 real_nan (r
, "", 1, SFmode
);
4451 /* Infinity and NaN return themselves. */
4452 if (real_isinf (x
) || real_isnan (x
))
4460 real_arithmetic (&half
, RDIV_EXPR
, &dconst1
, &dconst2
);
4461 real_arithmetic (&halfthree
, PLUS_EXPR
, &dconst1
, &half
);
4465 /* Initial guess for reciprocal sqrt, i. */
4466 exp
= real_exponent (x
);
4467 real_ldexp (&i
, &dconst1
, -exp
/2);
4469 /* Newton's iteration for reciprocal sqrt, i. */
4470 for (iter
= 0; iter
< 16; iter
++)
4472 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4473 real_arithmetic (&t
, MULT_EXPR
, x
, &i
);
4474 real_arithmetic (&h
, MULT_EXPR
, &t
, &i
);
4475 real_arithmetic (&t
, MULT_EXPR
, &h
, &half
);
4476 real_arithmetic (&h
, MINUS_EXPR
, &halfthree
, &t
);
4477 real_arithmetic (&t
, MULT_EXPR
, &i
, &h
);
4479 /* Check for early convergence. */
4480 if (iter
>= 6 && real_identical (&i
, &t
))
4483 /* ??? Unroll loop to avoid copying. */
4487 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4488 real_arithmetic (&t
, MULT_EXPR
, x
, &i
);
4489 real_arithmetic (&h
, MULT_EXPR
, &t
, &i
);
4490 real_arithmetic (&i
, MINUS_EXPR
, &dconst1
, &h
);
4491 real_arithmetic (&h
, MULT_EXPR
, &t
, &i
);
4492 real_arithmetic (&i
, MULT_EXPR
, &half
, &h
);
4493 real_arithmetic (&h
, PLUS_EXPR
, &t
, &i
);
4495 /* ??? We need a Tuckerman test to get the last bit. */
4497 real_convert (r
, mode
, &h
);