1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2002 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
31 /* The floating point model used internally is not exactly IEEE 754
32 compliant, and close to the description in the ISO C standard,
33 section 5.2.4.2.2 Characteristics of floating types.
37 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
41 b = base or radix, here always 2
43 p = precision (the number of base-b digits in the significand)
44 f_k = the digits of the significand.
46 We differ from typical IEEE 754 encodings in that the entire
47 significand is fractional. Normalized significands are in the
50 A requirement of the model is that P be larger than than the
51 largest supported target floating-point type by at least 2 bits.
52 This gives us proper rounding when we truncate to the target type.
53 In addition, E must be large enough to hold the smallest supported
54 denormal number in a normalized form.
56 Both of these requirements are easily satisfied. The largest target
57 significand is 113 bits; we store at least 160. The smallest
58 denormal number fits in 17 exponent bits; we store 29.
60 Note that the decimal string conversion routines are sensitive to
61 rounding error. Since the raw arithmetic routines do not themselves
62 have guard digits or rounding, the computation of 10**exp can
63 accumulate more than a few digits of error. The previous incarnation
64 of real.c successfully used a 144 bit fraction; given the current
65 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits.
67 Target floating point models that use base 16 instead of base 2
68 (i.e. IBM 370), are handled during round_for_format, in which we
69 canonicalize the exponent to be a multiple of 4 (log2(16)), and
70 adjust the significand to match. */
73 /* Used to classify two numbers simultaneously. */
74 #define CLASS2(A, B) ((A) << 2 | (B))
76 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
77 #error "Some constant folding done by hand to avoid shift count warnings"
80 static void get_zero
PARAMS ((REAL_VALUE_TYPE
*, int));
81 static void get_canonical_qnan
PARAMS ((REAL_VALUE_TYPE
*, int));
82 static void get_canonical_snan
PARAMS ((REAL_VALUE_TYPE
*, int));
83 static void get_inf
PARAMS ((REAL_VALUE_TYPE
*, int));
84 static bool sticky_rshift_significand
PARAMS ((REAL_VALUE_TYPE
*,
85 const REAL_VALUE_TYPE
*,
87 static void rshift_significand
PARAMS ((REAL_VALUE_TYPE
*,
88 const REAL_VALUE_TYPE
*,
90 static void lshift_significand
PARAMS ((REAL_VALUE_TYPE
*,
91 const REAL_VALUE_TYPE
*,
93 static void lshift_significand_1
PARAMS ((REAL_VALUE_TYPE
*,
94 const REAL_VALUE_TYPE
*));
95 static bool add_significands
PARAMS ((REAL_VALUE_TYPE
*r
,
96 const REAL_VALUE_TYPE
*,
97 const REAL_VALUE_TYPE
*));
98 static bool sub_significands
PARAMS ((REAL_VALUE_TYPE
*,
99 const REAL_VALUE_TYPE
*,
100 const REAL_VALUE_TYPE
*, int));
101 static void neg_significand
PARAMS ((REAL_VALUE_TYPE
*,
102 const REAL_VALUE_TYPE
*));
103 static int cmp_significands
PARAMS ((const REAL_VALUE_TYPE
*,
104 const REAL_VALUE_TYPE
*));
105 static int cmp_significand_0
PARAMS ((const REAL_VALUE_TYPE
*));
106 static void set_significand_bit
PARAMS ((REAL_VALUE_TYPE
*, unsigned int));
107 static void clear_significand_bit
PARAMS ((REAL_VALUE_TYPE
*, unsigned int));
108 static bool test_significand_bit
PARAMS ((REAL_VALUE_TYPE
*, unsigned int));
109 static void clear_significand_below
PARAMS ((REAL_VALUE_TYPE
*,
111 static bool div_significands
PARAMS ((REAL_VALUE_TYPE
*,
112 const REAL_VALUE_TYPE
*,
113 const REAL_VALUE_TYPE
*));
114 static void normalize
PARAMS ((REAL_VALUE_TYPE
*));
116 static void do_add
PARAMS ((REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
117 const REAL_VALUE_TYPE
*, int));
118 static void do_multiply
PARAMS ((REAL_VALUE_TYPE
*,
119 const REAL_VALUE_TYPE
*,
120 const REAL_VALUE_TYPE
*));
121 static void do_divide
PARAMS ((REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
122 const REAL_VALUE_TYPE
*));
123 static int do_compare
PARAMS ((const REAL_VALUE_TYPE
*,
124 const REAL_VALUE_TYPE
*, int));
125 static void do_fix_trunc
PARAMS ((REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*));
127 static unsigned long rtd_divmod
PARAMS ((REAL_VALUE_TYPE
*,
130 static const REAL_VALUE_TYPE
* ten_to_ptwo
PARAMS ((int));
131 static const REAL_VALUE_TYPE
* ten_to_mptwo
PARAMS ((int));
132 static const REAL_VALUE_TYPE
* real_digit
PARAMS ((int));
133 static void times_pten
PARAMS ((REAL_VALUE_TYPE
*, int));
135 static void round_for_format
PARAMS ((const struct real_format
*,
138 /* Initialize R with a positive zero. */
145 memset (r
, 0, sizeof (*r
));
149 /* Initialize R with the canonical quiet NaN. */
152 get_canonical_qnan (r
, sign
)
156 memset (r
, 0, sizeof (*r
));
159 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 1;
163 get_canonical_snan (r
, sign
)
167 memset (r
, 0, sizeof (*r
));
170 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 2;
178 memset (r
, 0, sizeof (*r
));
184 /* Right-shift the significand of A by N bits; put the result in the
185 significand of R. If any one bits are shifted out, return true. */
188 sticky_rshift_significand (r
, a
, n
)
190 const REAL_VALUE_TYPE
*a
;
193 unsigned long sticky
= 0;
194 unsigned int i
, ofs
= 0;
196 if (n
>= HOST_BITS_PER_LONG
)
198 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
200 n
&= HOST_BITS_PER_LONG
- 1;
205 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
206 for (i
= 0; i
< SIGSZ
; ++i
)
209 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
210 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
211 << (HOST_BITS_PER_LONG
- n
)));
216 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
217 r
->sig
[i
] = a
->sig
[ofs
+ i
];
218 for (; i
< SIGSZ
; ++i
)
225 /* Right-shift the significand of A by N bits; put the result in the
229 rshift_significand (r
, a
, n
)
231 const REAL_VALUE_TYPE
*a
;
234 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
236 n
&= HOST_BITS_PER_LONG
- 1;
239 for (i
= 0; i
< SIGSZ
; ++i
)
242 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
243 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
244 << (HOST_BITS_PER_LONG
- n
)));
249 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
250 r
->sig
[i
] = a
->sig
[ofs
+ i
];
251 for (; i
< SIGSZ
; ++i
)
256 /* Left-shift the significand of A by N bits; put the result in the
260 lshift_significand (r
, a
, n
)
262 const REAL_VALUE_TYPE
*a
;
265 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
267 n
&= HOST_BITS_PER_LONG
- 1;
270 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
271 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
272 for (; i
< SIGSZ
; ++i
)
273 r
->sig
[SIGSZ
-1-i
] = 0;
276 for (i
= 0; i
< SIGSZ
; ++i
)
279 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
280 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
281 >> (HOST_BITS_PER_LONG
- n
)));
285 /* Likewise, but N is specialized to 1. */
288 lshift_significand_1 (r
, a
)
290 const REAL_VALUE_TYPE
*a
;
294 for (i
= SIGSZ
- 1; i
> 0; --i
)
295 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
296 r
->sig
[0] = a
->sig
[0] << 1;
299 /* Add the significands of A and B, placing the result in R. Return
300 true if there was carry out of the most significant word. */
303 add_significands (r
, a
, b
)
305 const REAL_VALUE_TYPE
*a
, *b
;
310 for (i
= 0; i
< SIGSZ
; ++i
)
312 unsigned long ai
= a
->sig
[i
];
313 unsigned long ri
= ai
+ b
->sig
[i
];
329 /* Subtract the significands of A and B, placing the result in R. CARRY is
330 true if there's a borrow incoming to the least significant word.
331 Return true if there was borrow out of the most significant word. */
334 sub_significands (r
, a
, b
, carry
)
336 const REAL_VALUE_TYPE
*a
, *b
;
341 for (i
= 0; i
< SIGSZ
; ++i
)
343 unsigned long ai
= a
->sig
[i
];
344 unsigned long ri
= ai
- b
->sig
[i
];
360 /* Negate the significand A, placing the result in R. */
363 neg_significand (r
, a
)
365 const REAL_VALUE_TYPE
*a
;
370 for (i
= 0; i
< SIGSZ
; ++i
)
372 unsigned long ri
, ai
= a
->sig
[i
];
391 /* Compare significands. Return tri-state vs zero. */
394 cmp_significands (a
, b
)
395 const REAL_VALUE_TYPE
*a
, *b
;
399 for (i
= SIGSZ
- 1; i
>= 0; --i
)
401 unsigned long ai
= a
->sig
[i
];
402 unsigned long bi
= b
->sig
[i
];
413 /* Return true if A is non-zero. */
416 cmp_significand_0 (a
)
417 const REAL_VALUE_TYPE
*a
;
421 for (i
= SIGSZ
- 1; i
>= 0; --i
)
428 /* Set bit N of the significand of R. */
431 set_significand_bit (r
, n
)
435 r
->sig
[n
/ HOST_BITS_PER_LONG
]
436 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
439 /* Clear bit N of the significand of R. */
442 clear_significand_bit (r
, n
)
446 r
->sig
[n
/ HOST_BITS_PER_LONG
]
447 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
450 /* Test bit N of the significand of R. */
453 test_significand_bit (r
, n
)
457 /* ??? Compiler bug here if we return this expression directly.
458 The conversion to bool strips the "&1" and we wind up testing
459 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
460 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
464 /* Clear bits 0..N-1 of the significand of R. */
467 clear_significand_below (r
, n
)
471 int i
, w
= n
/ HOST_BITS_PER_LONG
;
473 for (i
= 0; i
< w
; ++i
)
476 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
479 /* Divide the significands of A and B, placing the result in R. Return
480 true if the division was inexact. */
483 div_significands (r
, a
, b
)
485 const REAL_VALUE_TYPE
*a
, *b
;
488 int i
, bit
= SIGNIFICAND_BITS
- 1;
489 unsigned long msb
, inexact
;
492 memset (r
->sig
, 0, sizeof (r
->sig
));
498 msb
= u
.sig
[SIGSZ
-1] & SIG_MSB
;
499 lshift_significand_1 (&u
, &u
);
501 if (msb
|| cmp_significands (&u
, b
) >= 0)
503 sub_significands (&u
, &u
, b
, 0);
504 set_significand_bit (r
, bit
);
509 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
515 /* Adjust the exponent and significand of R such that the most
516 significant bit is set. We underflow to zero and overflow to
517 infinity here, without denormals. (The intermediate representation
518 exponent is large enough to handle target denormals normalized.) */
527 /* Find the first word that is nonzero. */
528 for (i
= SIGSZ
- 1; i
>= 0; i
--)
530 shift
+= HOST_BITS_PER_LONG
;
534 /* Zero significand flushes to zero. */
542 /* Find the first bit that is nonzero. */
544 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
550 exp
= r
->exp
- shift
;
552 get_inf (r
, r
->sign
);
553 else if (exp
< -MAX_EXP
)
554 get_zero (r
, r
->sign
);
558 lshift_significand (r
, r
, shift
);
563 /* Return R = A + (SUBTRACT_P ? -B : B). */
566 do_add (r
, a
, b
, subtract_p
)
568 const REAL_VALUE_TYPE
*a
, *b
;
573 bool inexact
= false;
575 /* Determine if we need to add or subtract. */
577 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
579 switch (CLASS2 (a
->class, b
->class))
581 case CLASS2 (rvc_zero
, rvc_zero
):
582 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
583 get_zero (r
, sign
& !subtract_p
);
586 case CLASS2 (rvc_zero
, rvc_normal
):
587 case CLASS2 (rvc_zero
, rvc_inf
):
588 case CLASS2 (rvc_zero
, rvc_nan
):
590 case CLASS2 (rvc_normal
, rvc_nan
):
591 case CLASS2 (rvc_inf
, rvc_nan
):
592 case CLASS2 (rvc_nan
, rvc_nan
):
593 /* ANY + NaN = NaN. */
594 case CLASS2 (rvc_normal
, rvc_inf
):
597 r
->sign
= sign
^ subtract_p
;
600 case CLASS2 (rvc_normal
, rvc_zero
):
601 case CLASS2 (rvc_inf
, rvc_zero
):
602 case CLASS2 (rvc_nan
, rvc_zero
):
604 case CLASS2 (rvc_nan
, rvc_normal
):
605 case CLASS2 (rvc_nan
, rvc_inf
):
606 /* NaN + ANY = NaN. */
607 case CLASS2 (rvc_inf
, rvc_normal
):
612 case CLASS2 (rvc_inf
, rvc_inf
):
614 /* Inf - Inf = NaN. */
615 get_canonical_qnan (r
, 0);
617 /* Inf + Inf = Inf. */
621 case CLASS2 (rvc_normal
, rvc_normal
):
628 /* Swap the arguments such that A has the larger exponent. */
629 dexp
= a
->exp
- b
->exp
;
632 const REAL_VALUE_TYPE
*t
;
639 /* If the exponents are not identical, we need to shift the
640 significand of B down. */
643 /* If the exponents are too far apart, the significands
644 do not overlap, which makes the subtraction a noop. */
645 if (dexp
>= SIGNIFICAND_BITS
)
652 inexact
|= sticky_rshift_significand (&t
, b
, dexp
);
658 if (sub_significands (r
, a
, b
, inexact
))
660 /* We got a borrow out of the subtraction. That means that
661 A and B had the same exponent, and B had the larger
662 significand. We need to swap the sign and negate the
665 neg_significand (r
, r
);
670 if (add_significands (r
, a
, b
))
672 /* We got carry out of the addition. This means we need to
673 shift the significand back down one bit and increase the
675 inexact
|= sticky_rshift_significand (r
, r
, 1);
676 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
685 r
->class = rvc_normal
;
689 /* Re-normalize the result. */
692 /* Special case: if the subtraction results in zero, the result
694 if (r
->class == rvc_zero
)
697 r
->sig
[0] |= inexact
;
700 /* Return R = A * B. */
703 do_multiply (r
, a
, b
)
705 const REAL_VALUE_TYPE
*a
, *b
;
707 REAL_VALUE_TYPE u
, t
, *rr
;
708 unsigned int i
, j
, k
;
709 int sign
= a
->sign
^ b
->sign
;
711 switch (CLASS2 (a
->class, b
->class))
713 case CLASS2 (rvc_zero
, rvc_zero
):
714 case CLASS2 (rvc_zero
, rvc_normal
):
715 case CLASS2 (rvc_normal
, rvc_zero
):
716 /* +-0 * ANY = 0 with appropriate sign. */
720 case CLASS2 (rvc_zero
, rvc_nan
):
721 case CLASS2 (rvc_normal
, rvc_nan
):
722 case CLASS2 (rvc_inf
, rvc_nan
):
723 case CLASS2 (rvc_nan
, rvc_nan
):
724 /* ANY * NaN = NaN. */
729 case CLASS2 (rvc_nan
, rvc_zero
):
730 case CLASS2 (rvc_nan
, rvc_normal
):
731 case CLASS2 (rvc_nan
, rvc_inf
):
732 /* NaN * ANY = NaN. */
737 case CLASS2 (rvc_zero
, rvc_inf
):
738 case CLASS2 (rvc_inf
, rvc_zero
):
740 get_canonical_qnan (r
, sign
);
743 case CLASS2 (rvc_inf
, rvc_inf
):
744 case CLASS2 (rvc_normal
, rvc_inf
):
745 case CLASS2 (rvc_inf
, rvc_normal
):
746 /* Inf * Inf = Inf, R * Inf = Inf */
751 case CLASS2 (rvc_normal
, rvc_normal
):
758 if (r
== a
|| r
== b
)
764 /* Collect all the partial products. Since we don't have sure access
765 to a widening multiply, we split each long into two half-words.
767 Consider the long-hand form of a four half-word multiplication:
777 We construct partial products of the widened half-word products
778 that are known to not overlap, e.g. DF+DH. Each such partial
779 product is given its proper exponent, which allows us to sum them
780 and obtain the finished product. */
782 for (i
= 0; i
< SIGSZ
* 2; ++i
)
784 unsigned long ai
= a
->sig
[i
/ 2];
786 ai
>>= HOST_BITS_PER_LONG
/ 2;
788 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
793 for (j
= 0; j
< 2; ++j
)
795 int exp
= (a
->exp
- (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
796 + (b
->exp
- (1-j
)*(HOST_BITS_PER_LONG
/2)));
801 /* Would underflow to zero, which we shouldn't bother adding. */
804 u
.class = rvc_normal
;
808 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
810 unsigned long bi
= b
->sig
[k
/ 2];
812 bi
>>= HOST_BITS_PER_LONG
/ 2;
814 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
816 u
.sig
[k
/ 2] = ai
* bi
;
820 do_add (rr
, rr
, &u
, 0);
829 /* Return R = A / B. */
834 const REAL_VALUE_TYPE
*a
, *b
;
836 int exp
, sign
= a
->sign
^ b
->sign
;
837 REAL_VALUE_TYPE t
, *rr
;
840 switch (CLASS2 (a
->class, b
->class))
842 case CLASS2 (rvc_zero
, rvc_zero
):
844 case CLASS2 (rvc_inf
, rvc_inf
):
845 /* Inf / Inf = NaN. */
846 get_canonical_qnan (r
, sign
);
849 case CLASS2 (rvc_zero
, rvc_normal
):
850 case CLASS2 (rvc_zero
, rvc_inf
):
852 case CLASS2 (rvc_normal
, rvc_inf
):
858 case CLASS2 (rvc_normal
, rvc_zero
):
860 case CLASS2 (rvc_inf
, rvc_zero
):
865 case CLASS2 (rvc_zero
, rvc_nan
):
866 case CLASS2 (rvc_normal
, rvc_nan
):
867 case CLASS2 (rvc_inf
, rvc_nan
):
868 case CLASS2 (rvc_nan
, rvc_nan
):
869 /* ANY / NaN = NaN. */
874 case CLASS2 (rvc_nan
, rvc_zero
):
875 case CLASS2 (rvc_nan
, rvc_normal
):
876 case CLASS2 (rvc_nan
, rvc_inf
):
877 /* NaN / ANY = NaN. */
882 case CLASS2 (rvc_inf
, rvc_normal
):
888 case CLASS2 (rvc_normal
, rvc_normal
):
895 if (r
== a
|| r
== b
)
900 rr
->class = rvc_normal
;
903 exp
= a
->exp
- b
->exp
+ 1;
910 inexact
= div_significands (rr
, a
, b
);
912 /* Re-normalize the result. */
914 rr
->sig
[0] |= inexact
;
920 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
921 one of the two operands is a NaN. */
924 do_compare (a
, b
, nan_result
)
925 const REAL_VALUE_TYPE
*a
, *b
;
930 switch (CLASS2 (a
->class, b
->class))
932 case CLASS2 (rvc_zero
, rvc_zero
):
933 /* Sign of zero doesn't matter for compares. */
936 case CLASS2 (rvc_inf
, rvc_zero
):
937 case CLASS2 (rvc_inf
, rvc_normal
):
938 case CLASS2 (rvc_normal
, rvc_zero
):
939 return (a
->sign
? -1 : 1);
941 case CLASS2 (rvc_inf
, rvc_inf
):
942 return -a
->sign
- -b
->sign
;
944 case CLASS2 (rvc_zero
, rvc_normal
):
945 case CLASS2 (rvc_zero
, rvc_inf
):
946 case CLASS2 (rvc_normal
, rvc_inf
):
947 return (b
->sign
? 1 : -1);
949 case CLASS2 (rvc_zero
, rvc_nan
):
950 case CLASS2 (rvc_normal
, rvc_nan
):
951 case CLASS2 (rvc_inf
, rvc_nan
):
952 case CLASS2 (rvc_nan
, rvc_nan
):
953 case CLASS2 (rvc_nan
, rvc_zero
):
954 case CLASS2 (rvc_nan
, rvc_normal
):
955 case CLASS2 (rvc_nan
, rvc_inf
):
958 case CLASS2 (rvc_normal
, rvc_normal
):
965 if (a
->sign
!= b
->sign
)
966 return -a
->sign
- -b
->sign
;
970 else if (a
->exp
< b
->exp
)
973 ret
= cmp_significands (a
, b
);
975 return (a
->sign
? -ret
: ret
);
978 /* Return A truncated to an integral value toward zero. */
983 const REAL_VALUE_TYPE
*a
;
996 get_zero (r
, r
->sign
);
997 else if (r
->exp
< SIGNIFICAND_BITS
)
998 clear_significand_below (r
, SIGNIFICAND_BITS
- r
->exp
);
1006 /* Perform the binary or unary operation described by CODE.
1007 For a unary operation, leave OP1 NULL. */
1010 real_arithmetic (r
, icode
, op0
, op1
)
1013 const REAL_VALUE_TYPE
*op0
, *op1
;
1015 enum tree_code code
= icode
;
1020 do_add (r
, op0
, op1
, 0);
1024 do_add (r
, op0
, op1
, 1);
1028 do_multiply (r
, op0
, op1
);
1032 do_divide (r
, op0
, op1
);
1036 if (op1
->class == rvc_nan
)
1038 else if (do_compare (op0
, op1
, -1) < 0)
1045 if (op1
->class == rvc_nan
)
1047 else if (do_compare (op0
, op1
, 1) < 0)
1063 case FIX_TRUNC_EXPR
:
1064 do_fix_trunc (r
, op0
);
1072 /* Legacy. Similar, but return the result directly. */
1075 real_arithmetic2 (icode
, op0
, op1
)
1077 const REAL_VALUE_TYPE
*op0
, *op1
;
1080 real_arithmetic (&r
, icode
, op0
, op1
);
1085 real_compare (icode
, op0
, op1
)
1087 const REAL_VALUE_TYPE
*op0
, *op1
;
1089 enum tree_code code
= icode
;
1094 return do_compare (op0
, op1
, 1) < 0;
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;
1105 case UNORDERED_EXPR
:
1106 return op0
->class == rvc_nan
|| op1
->class == rvc_nan
;
1108 return op0
->class != rvc_nan
&& op1
->class != rvc_nan
;
1110 return do_compare (op0
, op1
, -1) < 0;
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
, 0) == 0;
1125 /* Return floor log2(R). */
1129 const REAL_VALUE_TYPE
*r
;
1137 return (unsigned int)-1 >> 1;
1145 /* R = OP0 * 2**EXP. */
1148 real_ldexp (r
, op0
, exp
)
1150 const REAL_VALUE_TYPE
*op0
;
1164 get_inf (r
, r
->sign
);
1165 else if (exp
< -MAX_EXP
)
1166 get_zero (r
, r
->sign
);
1176 /* Determine whether a floating-point value X is infinite. */
1180 const REAL_VALUE_TYPE
*r
;
1182 return (r
->class == rvc_inf
);
1185 /* Determine whether a floating-point value X is a NaN. */
1189 const REAL_VALUE_TYPE
*r
;
1191 return (r
->class == rvc_nan
);
1194 /* Determine whether a floating-point value X is negative. */
1198 const REAL_VALUE_TYPE
*r
;
1203 /* Determine whether a floating-point value X is minus zero. */
1207 const REAL_VALUE_TYPE
*r
;
1209 return r
->sign
&& r
->class == rvc_zero
;
1212 /* Compare two floating-point objects for bitwise identity. */
1215 real_identical (a
, b
)
1216 const REAL_VALUE_TYPE
*a
, *b
;
1220 if (a
->class != b
->class)
1222 if (a
->sign
!= b
->sign
)
1232 if (a
->exp
!= b
->exp
)
1236 for (i
= 0; i
< SIGSZ
; ++i
)
1237 if (a
->sig
[i
] != b
->sig
[i
])
1248 /* Try to change R into its exact multiplicative inverse in machine
1249 mode MODE. Return true if successful. */
1252 exact_real_inverse (mode
, r
)
1253 enum machine_mode mode
;
1256 const REAL_VALUE_TYPE
*one
= real_digit (1);
1260 if (r
->class != rvc_normal
)
1263 /* Check for a power of two: all significand bits zero except the MSB. */
1264 for (i
= 0; i
< SIGSZ
-1; ++i
)
1267 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1270 /* Find the inverse and truncate to the required mode. */
1271 do_divide (&u
, one
, r
);
1272 real_convert (&u
, mode
, &u
);
1274 /* The rounding may have overflowed. */
1275 if (u
.class != rvc_normal
)
1277 for (i
= 0; i
< SIGSZ
-1; ++i
)
1280 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1287 /* Render R as an integer. */
1291 const REAL_VALUE_TYPE
*r
;
1293 unsigned HOST_WIDE_INT i
;
1304 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1312 if (r
->exp
> HOST_BITS_PER_WIDE_INT
)
1315 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1316 i
= r
->sig
[SIGSZ
-1];
1317 else if (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
)
1319 i
= r
->sig
[SIGSZ
-1];
1320 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1321 i
|= r
->sig
[SIGSZ
-2];
1326 i
>>= HOST_BITS_PER_WIDE_INT
- r
->exp
;
1337 /* Likewise, but to an integer pair, HI+LOW. */
1340 real_to_integer2 (plow
, phigh
, r
)
1341 HOST_WIDE_INT
*plow
, *phigh
;
1342 const REAL_VALUE_TYPE
*r
;
1345 HOST_WIDE_INT low
, high
;
1358 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1372 if (exp
>= 2*HOST_BITS_PER_WIDE_INT
)
1375 rshift_significand (&t
, r
, 2*HOST_BITS_PER_WIDE_INT
- exp
);
1376 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1378 high
= t
.sig
[SIGSZ
-1];
1379 low
= t
.sig
[SIGSZ
-2];
1381 else if (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
)
1383 high
= t
.sig
[SIGSZ
-1];
1384 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1385 high
|= t
.sig
[SIGSZ
-2];
1387 low
= t
.sig
[SIGSZ
-3];
1388 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1389 low
|= t
.sig
[SIGSZ
-4];
1399 low
= -low
, high
= ~high
;
1411 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1412 of NUM / DEN. Return the quotient and place the remainder in NUM.
1413 It is expected that NUM / DEN are close enough that the quotient is
1416 static unsigned long
1417 rtd_divmod (num
, den
)
1418 REAL_VALUE_TYPE
*num
, *den
;
1420 unsigned long q
, msb
;
1421 int expn
= num
->exp
, expd
= den
->exp
;
1430 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1432 lshift_significand_1 (num
, num
);
1434 if (msb
|| cmp_significands (num
, den
) >= 0)
1436 sub_significands (num
, num
, den
, 0);
1440 while (--expn
>= expd
);
1448 /* Render R as a decimal floating point constant. Emit DIGITS significant
1449 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1450 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1453 #define M_LOG10_2 0.30102999566398119521
1456 real_to_decimal (str
, r_orig
, buf_size
, digits
, crop_trailing_zeros
)
1458 const REAL_VALUE_TYPE
*r_orig
;
1459 size_t buf_size
, digits
;
1460 int crop_trailing_zeros
;
1462 const REAL_VALUE_TYPE
*one
, *ten
;
1463 REAL_VALUE_TYPE r
, pten
, u
, v
;
1464 int dec_exp
, cmp_one
, digit
;
1466 char *p
, *first
, *last
;
1473 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1478 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1481 /* ??? Print the significand as well, if not canonical? */
1482 strcpy (str
, (r
.sign
? "-NaN" : "+NaN"));
1488 /* Estimate the decimal exponent, and compute the length of the string it
1489 will print as. Be conservative and add one to account for possible
1490 overflow or rounding error. */
1491 dec_exp
= r
.exp
* M_LOG10_2
;
1492 for (max_digits
= 1; dec_exp
; max_digits
++)
1495 /* Bound the number of digits printed by the size of the output buffer. */
1496 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1497 if (max_digits
> buf_size
)
1499 if (digits
> max_digits
)
1500 digits
= max_digits
;
1502 /* Bound the number of digits printed by the size of the representation. */
1503 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1504 if (digits
== 0 || digits
> max_digits
)
1505 digits
= max_digits
;
1507 one
= real_digit (1);
1508 ten
= ten_to_ptwo (0);
1516 cmp_one
= do_compare (&r
, one
, 0);
1521 /* Number is greater than one. Convert significand to an integer
1522 and strip trailing decimal zeros. */
1525 u
.exp
= SIGNIFICAND_BITS
- 1;
1527 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1528 m
= floor_log2 (max_digits
);
1530 /* Iterate over the bits of the possible powers of 10 that might
1531 be present in U and eliminate them. That is, if we find that
1532 10**2**M divides U evenly, keep the division and increase
1538 do_divide (&t
, &u
, ten_to_ptwo (m
));
1539 do_fix_trunc (&v
, &t
);
1540 if (cmp_significands (&v
, &t
) == 0)
1548 /* Revert the scaling to integer that we performed earlier. */
1549 u
.exp
+= r
.exp
- (SIGNIFICAND_BITS
- 1);
1552 /* Find power of 10. Do this by dividing out 10**2**M when
1553 this is larger than the current remainder. Fill PTEN with
1554 the power of 10 that we compute. */
1557 m
= floor_log2 ((int)(r
.exp
* M_LOG10_2
)) + 1;
1560 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1561 if (do_compare (&u
, ptentwo
, 0) >= 0)
1563 do_divide (&u
, &u
, ptentwo
);
1564 do_multiply (&pten
, &pten
, ptentwo
);
1571 /* We managed to divide off enough tens in the above reduction
1572 loop that we've now got a negative exponent. Fall into the
1573 less-than-one code to compute the proper value for PTEN. */
1580 /* Number is less than one. Pad significand with leading
1586 /* Stop if we'd shift bits off the bottom. */
1590 do_multiply (&u
, &v
, ten
);
1592 /* Stop if we're now >= 1. */
1601 /* Find power of 10. Do this by multiplying in P=10**2**M when
1602 the current remainder is smaller than 1/P. Fill PTEN with the
1603 power of 10 that we compute. */
1604 m
= floor_log2 ((int)(-r
.exp
* M_LOG10_2
)) + 1;
1607 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1608 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1610 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1612 do_multiply (&v
, &v
, ptentwo
);
1613 do_multiply (&pten
, &pten
, ptentwo
);
1619 /* Invert the positive power of 10 that we've collected so far. */
1620 do_divide (&pten
, one
, &pten
);
1628 /* At this point, PTEN should contain the nearest power of 10 smaller
1629 than R, such that this division produces the first digit.
1631 Using a divide-step primitive that returns the complete integral
1632 remainder avoids the rounding error that would be produced if
1633 we were to use do_divide here and then simply multiply by 10 for
1634 each subsequent digit. */
1636 digit
= rtd_divmod (&r
, &pten
);
1638 /* Be prepared for error in that division via underflow ... */
1639 if (digit
== 0 && cmp_significand_0 (&r
))
1641 /* Multiply by 10 and try again. */
1642 do_multiply (&r
, &r
, ten
);
1643 digit
= rtd_divmod (&r
, &pten
);
1649 /* ... or overflow. */
1657 else if (digit
> 10)
1662 /* Generate subsequent digits. */
1663 while (--digits
> 0)
1665 do_multiply (&r
, &r
, ten
);
1666 digit
= rtd_divmod (&r
, &pten
);
1671 /* Generate one more digit with which to do rounding. */
1672 do_multiply (&r
, &r
, ten
);
1673 digit
= rtd_divmod (&r
, &pten
);
1675 /* Round the result. */
1678 /* Round to nearest. If R is non-zero there are additional
1679 non-zero digits to be extracted. */
1680 if (cmp_significand_0 (&r
))
1682 /* Round to even. */
1683 else if ((p
[-1] - '0') & 1)
1700 /* Carry out of the first digit. This means we had all 9's and
1701 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1709 /* Insert the decimal point. */
1710 first
[0] = first
[1];
1713 /* If requested, drop trailing zeros. Never crop past "1.0". */
1714 if (crop_trailing_zeros
)
1715 while (last
> first
+ 3 && last
[-1] == '0')
1718 /* Append the exponent. */
1719 sprintf (last
, "e%+d", dec_exp
);
1722 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1723 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1724 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1725 strip trailing zeros. */
1728 real_to_hexadecimal (str
, r
, buf_size
, digits
, crop_trailing_zeros
)
1730 const REAL_VALUE_TYPE
*r
;
1731 size_t buf_size
, digits
;
1732 int crop_trailing_zeros
;
1734 int i
, j
, exp
= r
->exp
;
1747 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1750 /* ??? Print the significand as well, if not canonical? */
1751 strcpy (str
, (r
->sign
? "-NaN" : "+NaN"));
1758 digits
= SIGNIFICAND_BITS
/ 4;
1760 /* Bound the number of digits printed by the size of the output buffer. */
1762 sprintf (exp_buf
, "p%+d", exp
);
1763 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1764 if (max_digits
> buf_size
)
1766 if (digits
> max_digits
)
1767 digits
= max_digits
;
1778 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1779 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1781 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1787 if (crop_trailing_zeros
)
1788 while (p
> first
+ 1 && p
[-1] == '0')
1791 sprintf (p
, "p%+d", exp
);
1794 /* Initialize R from a decimal or hexadecimal string. The string is
1795 assumed to have been syntax checked already. */
1798 real_from_string (r
, str
)
1812 else if (*str
== '+')
1815 if (str
[0] == '0' && str
[1] == 'x')
1817 /* Hexadecimal floating point. */
1818 int pos
= SIGNIFICAND_BITS
- 4, d
;
1826 d
= hex_value (*str
);
1831 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1832 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1841 if (pos
== SIGNIFICAND_BITS
- 4)
1848 d
= hex_value (*str
);
1853 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1854 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1860 if (*str
== 'p' || *str
== 'P')
1862 bool exp_neg
= false;
1870 else if (*str
== '+')
1874 while (ISDIGIT (*str
))
1880 /* Overflowed the exponent. */
1894 r
->class = rvc_normal
;
1901 /* Decimal floating point. */
1902 const REAL_VALUE_TYPE
*ten
= ten_to_ptwo (0);
1907 while (ISDIGIT (*str
))
1910 do_multiply (r
, r
, ten
);
1912 do_add (r
, r
, real_digit (d
), 0);
1917 if (r
->class == rvc_zero
)
1922 while (ISDIGIT (*str
))
1925 do_multiply (r
, r
, ten
);
1927 do_add (r
, r
, real_digit (d
), 0);
1932 if (*str
== 'e' || *str
== 'E')
1934 bool exp_neg
= false;
1942 else if (*str
== '+')
1946 while (ISDIGIT (*str
))
1952 /* Overflowed the exponent. */
1966 times_pten (r
, exp
);
1981 /* Legacy. Similar, but return the result directly. */
1984 real_from_string2 (s
, mode
)
1986 enum machine_mode mode
;
1990 real_from_string (&r
, s
);
1991 if (mode
!= VOIDmode
)
1992 real_convert (&r
, mode
, &r
);
1997 /* Initialize R from the integer pair HIGH+LOW. */
2000 real_from_integer (r
, mode
, low
, high
, unsigned_p
)
2002 enum machine_mode mode
;
2003 unsigned HOST_WIDE_INT low
;
2007 if (low
== 0 && high
== 0)
2011 r
->class = rvc_normal
;
2012 r
->sign
= high
< 0 && !unsigned_p
;
2013 r
->exp
= 2 * HOST_BITS_PER_WIDE_INT
;
2024 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2026 r
->sig
[SIGSZ
-1] = high
;
2027 r
->sig
[SIGSZ
-2] = low
;
2028 memset (r
->sig
, 0, sizeof(long)*(SIGSZ
-2));
2030 else if (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
)
2032 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2033 r
->sig
[SIGSZ
-2] = high
;
2034 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2035 r
->sig
[SIGSZ
-4] = low
;
2037 memset (r
->sig
, 0, sizeof(long)*(SIGSZ
-4));
2045 if (mode
!= VOIDmode
)
2046 real_convert (r
, mode
, r
);
2049 /* Returns 10**2**N. */
2051 static const REAL_VALUE_TYPE
*
2055 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2057 if (n
< 0 || n
>= EXP_BITS
)
2060 if (tens
[n
].class == rvc_zero
)
2062 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2064 HOST_WIDE_INT t
= 10;
2067 for (i
= 0; i
< n
; ++i
)
2070 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2074 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2075 do_multiply (&tens
[n
], t
, t
);
2082 /* Returns 10**(-2**N). */
2084 static const REAL_VALUE_TYPE
*
2088 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2090 if (n
< 0 || n
>= EXP_BITS
)
2093 if (tens
[n
].class == rvc_zero
)
2094 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2101 static const REAL_VALUE_TYPE
*
2105 static REAL_VALUE_TYPE num
[10];
2110 if (n
> 0 && num
[n
].class == rvc_zero
)
2111 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2116 /* Multiply R by 10**EXP. */
2123 REAL_VALUE_TYPE pten
, *rr
;
2124 bool negative
= (exp
< 0);
2130 pten
= *real_digit (1);
2136 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2138 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2141 do_divide (r
, r
, &pten
);
2144 /* Fills R with +Inf. */
2153 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2154 we force a QNaN, else we force an SNaN. The string, if not empty,
2155 is parsed as a number and placed in the significand. Return true
2156 if the string was successfully parsed. */
2159 real_nan (r
, str
, quiet
, mode
)
2163 enum machine_mode mode
;
2165 const struct real_format
*fmt
;
2167 fmt
= real_format_for_mode
[mode
- QFmode
];
2174 get_canonical_qnan (r
, 0);
2176 get_canonical_snan (r
, 0);
2183 memset (r
, 0, sizeof (*r
));
2186 /* Parse akin to strtol into the significand of R. */
2188 while (ISSPACE (*str
))
2192 else if (*str
== '+')
2202 while ((d
= hex_value (*str
)) < base
)
2209 lshift_significand (r
, r
, 3);
2212 lshift_significand (r
, r
, 4);
2215 lshift_significand_1 (&u
, r
);
2216 lshift_significand (r
, r
, 3);
2217 add_significands (r
, r
, &u
);
2225 add_significands (r
, r
, &u
);
2230 /* Must have consumed the entire string for success. */
2234 /* Shift the significand into place such that the bits
2235 are in the most significant bits for the format. */
2236 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->p
);
2238 /* Our MSB is always unset for NaNs. */
2239 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2241 /* Force quiet or signalling NaN. */
2243 r
->sig
[SIGSZ
-1] |= SIG_MSB
>> 1;
2245 r
->sig
[SIGSZ
-1] &= ~(SIG_MSB
>> 1);
2247 /* Force at least one bit of the significand set. */
2248 for (d
= 0; d
< SIGSZ
; ++d
)
2252 r
->sig
[SIGSZ
-1] |= SIG_MSB
>> 2;
2254 /* Our intermediate format forces QNaNs to have MSB-1 set.
2255 If the target format has QNaNs with the top bit unset,
2256 mirror the output routines and invert the top two bits. */
2257 if (!fmt
->qnan_msb_set
)
2258 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1) | (SIG_MSB
>> 2);
2264 /* Fills R with 2**N. */
2271 memset (r
, 0, sizeof (*r
));
2276 else if (n
< -MAX_EXP
)
2280 r
->class = rvc_normal
;
2282 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2288 round_for_format (fmt
, r
)
2289 const struct real_format
*fmt
;
2293 unsigned long sticky
;
2297 p2
= fmt
->p
* fmt
->log2_b
;
2298 emin2m1
= (fmt
->emin
- 1) * fmt
->log2_b
;
2299 emax2
= fmt
->emax
* fmt
->log2_b
;
2301 np2
= SIGNIFICAND_BITS
- p2
;
2305 get_zero (r
, r
->sign
);
2307 if (!fmt
->has_signed_zero
)
2312 get_inf (r
, r
->sign
);
2317 clear_significand_below (r
, np2
);
2319 /* If we've cleared the entire significand, we need one bit
2320 set for this to continue to be a NaN. */
2321 for (i
= 0; i
< SIGSZ
; ++i
)
2325 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 2;
2335 /* If we're not base2, normalize the exponent to a multiple of
2337 if (fmt
->log2_b
!= 1)
2339 int shift
= r
->exp
& (fmt
->log2_b
- 1);
2342 shift
= fmt
->log2_b
- shift
;
2343 r
->sig
[0] |= sticky_rshift_significand (r
, r
, shift
);
2348 /* Check the range of the exponent. If we're out of range,
2349 either underflow or overflow. */
2352 else if (r
->exp
<= emin2m1
)
2356 if (!fmt
->has_denorm
)
2358 /* Don't underflow completely until we've had a chance to round. */
2359 if (r
->exp
< emin2m1
)
2364 diff
= emin2m1
- r
->exp
+ 1;
2368 /* De-normalize the significand. */
2369 r
->sig
[0] |= sticky_rshift_significand (r
, r
, diff
);
2374 /* There are P2 true significand bits, followed by one guard bit,
2375 followed by one sticky bit, followed by stuff. Fold nonzero
2376 stuff into the sticky bit. */
2379 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2380 sticky
|= r
->sig
[i
];
2382 r
->sig
[w
] & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2384 guard
= test_significand_bit (r
, np2
- 1);
2385 lsb
= test_significand_bit (r
, np2
);
2387 /* Round to even. */
2388 if (guard
&& (sticky
|| lsb
))
2392 set_significand_bit (&u
, np2
);
2394 if (add_significands (r
, r
, &u
))
2396 /* Overflow. Means the significand had been all ones, and
2397 is now all zeros. Need to increase the exponent, and
2398 possibly re-normalize it. */
2399 if (++r
->exp
> emax2
)
2401 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2403 if (fmt
->log2_b
!= 1)
2405 int shift
= r
->exp
& (fmt
->log2_b
- 1);
2408 shift
= fmt
->log2_b
- shift
;
2409 rshift_significand (r
, r
, shift
);
2418 /* Catch underflow that we deferred until after rounding. */
2419 if (r
->exp
<= emin2m1
)
2422 /* Clear out trailing garbage. */
2423 clear_significand_below (r
, np2
);
2426 /* Extend or truncate to a new mode. */
2429 real_convert (r
, mode
, a
)
2431 enum machine_mode mode
;
2432 const REAL_VALUE_TYPE
*a
;
2434 const struct real_format
*fmt
;
2436 fmt
= real_format_for_mode
[mode
- QFmode
];
2441 round_for_format (fmt
, r
);
2443 /* round_for_format de-normalizes denormals. Undo just that part. */
2444 if (r
->class == rvc_normal
)
2448 /* Legacy. Likewise, except return the struct directly. */
2451 real_value_truncate (mode
, a
)
2452 enum machine_mode mode
;
2456 real_convert (&r
, mode
, &a
);
2460 /* Return true if truncating to MODE is exact. */
2463 exact_real_truncate (mode
, a
)
2464 enum machine_mode mode
;
2465 const REAL_VALUE_TYPE
*a
;
2468 real_convert (&t
, mode
, a
);
2469 return real_identical (&t
, a
);
2472 /* Write R to the given target format. Place the words of the result
2473 in target word order in BUF. There are always 32 bits in each
2474 long, no matter the size of the host long.
2476 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2479 real_to_target_fmt (buf
, r_orig
, fmt
)
2481 const REAL_VALUE_TYPE
*r_orig
;
2482 const struct real_format
*fmt
;
2488 round_for_format (fmt
, &r
);
2492 (*fmt
->encode
) (fmt
, buf
, &r
);
2497 /* Similar, but look up the format from MODE. */
2500 real_to_target (buf
, r
, mode
)
2502 const REAL_VALUE_TYPE
*r
;
2503 enum machine_mode mode
;
2505 const struct real_format
*fmt
;
2507 fmt
= real_format_for_mode
[mode
- QFmode
];
2511 return real_to_target_fmt (buf
, r
, fmt
);
2514 /* Read R from the given target format. Read the words of the result
2515 in target word order in BUF. There are always 32 bits in each
2516 long, no matter the size of the host long. */
2519 real_from_target_fmt (r
, buf
, fmt
)
2522 const struct real_format
*fmt
;
2524 (*fmt
->decode
) (fmt
, r
, buf
);
2527 /* Similar, but look up the format from MODE. */
2530 real_from_target (r
, buf
, mode
)
2533 enum machine_mode mode
;
2535 const struct real_format
*fmt
;
2537 fmt
= real_format_for_mode
[mode
- QFmode
];
2541 (*fmt
->decode
) (fmt
, r
, buf
);
2544 /* Return the number of bits in the significand for MODE. */
2545 /* ??? Legacy. Should get access to real_format directly. */
2548 significand_size (mode
)
2549 enum machine_mode mode
;
2551 const struct real_format
*fmt
;
2553 fmt
= real_format_for_mode
[mode
- QFmode
];
2557 return fmt
->p
* fmt
->log2_b
;
2560 /* Return a hash value for the given real value. */
2561 /* ??? The "unsigned int" return value is intended to be hashval_t,
2562 but I didn't want to pull hashtab.h into real.h. */
2566 const REAL_VALUE_TYPE
*r
;
2571 h
= r
->class | (r
->sign
<< 2);
2583 if (sizeof(unsigned long) > sizeof(unsigned int))
2584 for (i
= 0; i
< SIGSZ
; ++i
)
2586 unsigned long s
= r
->sig
[i
];
2587 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2590 for (i
= 0; i
< SIGSZ
; ++i
)
2601 /* IEEE single-precision format. */
2603 static void encode_ieee_single
PARAMS ((const struct real_format
*fmt
,
2604 long *, const REAL_VALUE_TYPE
*));
2605 static void decode_ieee_single
PARAMS ((const struct real_format
*,
2606 REAL_VALUE_TYPE
*, const long *));
2609 encode_ieee_single (fmt
, buf
, r
)
2610 const struct real_format
*fmt
;
2612 const REAL_VALUE_TYPE
*r
;
2614 unsigned long image
, sig
, exp
;
2615 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2617 image
= r
->sign
<< 31;
2618 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2629 image
|= 0x7fffffff;
2637 if (!fmt
->qnan_msb_set
)
2638 image
^= 1 << 23 | 1 << 22;
2641 image
|= 0x7fffffff;
2645 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2646 whereas the intermediate representation is 0.F x 2**exp.
2647 Which means we're off by one. */
2651 exp
= r
->exp
+ 127 - 1;
2664 decode_ieee_single (fmt
, r
, buf
)
2665 const struct real_format
*fmt
;
2669 unsigned long image
= buf
[0] & 0xffffffff;
2670 bool sign
= (image
>> 31) & 1;
2671 int exp
= (image
>> 23) & 0xff;
2673 memset (r
, 0, sizeof (*r
));
2674 image
<<= HOST_BITS_PER_LONG
- 24;
2679 if (image
&& fmt
->has_denorm
)
2681 r
->class = rvc_normal
;
2684 r
->sig
[SIGSZ
-1] = image
<< 1;
2687 else if (fmt
->has_signed_zero
)
2690 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2696 if (!fmt
->qnan_msb_set
)
2697 image
^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
2698 r
->sig
[SIGSZ
-1] = image
;
2708 r
->class = rvc_normal
;
2710 r
->exp
= exp
- 127 + 1;
2711 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2715 const struct real_format ieee_single_format
=
2732 /* IEEE double-precision format. */
2734 static void encode_ieee_double
PARAMS ((const struct real_format
*fmt
,
2735 long *, const REAL_VALUE_TYPE
*));
2736 static void decode_ieee_double
PARAMS ((const struct real_format
*,
2737 REAL_VALUE_TYPE
*, const long *));
2740 encode_ieee_double (fmt
, buf
, r
)
2741 const struct real_format
*fmt
;
2743 const REAL_VALUE_TYPE
*r
;
2745 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
2746 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2748 image_hi
= r
->sign
<< 31;
2751 if (HOST_BITS_PER_LONG
== 64)
2753 sig_hi
= r
->sig
[SIGSZ
-1];
2754 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
2755 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
2759 sig_hi
= r
->sig
[SIGSZ
-1];
2760 sig_lo
= r
->sig
[SIGSZ
-2];
2761 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
2762 sig_hi
= (sig_hi
>> 11) & 0xfffff;
2772 image_hi
|= 2047 << 20;
2775 image_hi
|= 0x7fffffff;
2776 image_lo
= 0xffffffff;
2783 image_hi
|= 2047 << 20;
2785 if (!fmt
->qnan_msb_set
)
2786 image_hi
^= 1 << 19 | 1 << 18;
2791 image_hi
|= 0x7fffffff;
2792 image_lo
= 0xffffffff;
2797 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2798 whereas the intermediate representation is 0.F x 2**exp.
2799 Which means we're off by one. */
2803 exp
= r
->exp
+ 1023 - 1;
2804 image_hi
|= exp
<< 20;
2813 if (FLOAT_WORDS_BIG_ENDIAN
)
2814 buf
[0] = image_hi
, buf
[1] = image_lo
;
2816 buf
[0] = image_lo
, buf
[1] = image_hi
;
2820 decode_ieee_double (fmt
, r
, buf
)
2821 const struct real_format
*fmt
;
2825 unsigned long image_hi
, image_lo
;
2829 if (FLOAT_WORDS_BIG_ENDIAN
)
2830 image_hi
= buf
[0], image_lo
= buf
[1];
2832 image_lo
= buf
[0], image_hi
= buf
[1];
2833 image_lo
&= 0xffffffff;
2834 image_hi
&= 0xffffffff;
2836 sign
= (image_hi
>> 31) & 1;
2837 exp
= (image_hi
>> 20) & 0x7ff;
2839 memset (r
, 0, sizeof (*r
));
2841 image_hi
<<= 32 - 21;
2842 image_hi
|= image_lo
>> 21;
2843 image_hi
&= 0x7fffffff;
2844 image_lo
<<= 32 - 21;
2848 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
2850 r
->class = rvc_normal
;
2853 if (HOST_BITS_PER_LONG
== 32)
2855 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
2857 r
->sig
[SIGSZ
-1] = image_hi
;
2858 r
->sig
[SIGSZ
-2] = image_lo
;
2862 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
2863 r
->sig
[SIGSZ
-1] = image_hi
;
2867 else if (fmt
->has_signed_zero
)
2870 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
2872 if (image_hi
|| image_lo
)
2876 if (HOST_BITS_PER_LONG
== 32)
2878 r
->sig
[SIGSZ
-1] = image_hi
;
2879 r
->sig
[SIGSZ
-2] = image_lo
;
2882 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
2884 if (!fmt
->qnan_msb_set
)
2885 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
2895 r
->class = rvc_normal
;
2897 r
->exp
= exp
- 1023 + 1;
2898 if (HOST_BITS_PER_LONG
== 32)
2900 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
2901 r
->sig
[SIGSZ
-2] = image_lo
;
2904 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
2908 const struct real_format ieee_double_format
=
2925 /* IEEE extended double precision format. This comes in three
2926 flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2929 static void encode_ieee_extended
PARAMS ((const struct real_format
*fmt
,
2930 long *, const REAL_VALUE_TYPE
*));
2931 static void decode_ieee_extended
PARAMS ((const struct real_format
*,
2932 REAL_VALUE_TYPE
*, const long *));
2934 static void encode_ieee_extended_128
PARAMS ((const struct real_format
*fmt
,
2936 const REAL_VALUE_TYPE
*));
2937 static void decode_ieee_extended_128
PARAMS ((const struct real_format
*,
2942 encode_ieee_extended (fmt
, buf
, r
)
2943 const struct real_format
*fmt
;
2945 const REAL_VALUE_TYPE
*r
;
2947 unsigned long image_hi
, sig_hi
, sig_lo
;
2948 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2950 image_hi
= r
->sign
<< 15;
2951 sig_hi
= sig_lo
= 0;
2963 /* Intel requires the explicit integer bit to be set, otherwise
2964 it considers the value a "pseudo-infinity". Motorola docs
2965 say it doesn't care. */
2966 sig_hi
= 0x80000000;
2971 sig_lo
= sig_hi
= 0xffffffff;
2979 if (HOST_BITS_PER_LONG
== 32)
2981 sig_hi
= r
->sig
[SIGSZ
-1];
2982 sig_lo
= r
->sig
[SIGSZ
-2];
2986 sig_lo
= r
->sig
[SIGSZ
-1];
2987 sig_hi
= sig_lo
>> 31 >> 1;
2988 sig_lo
&= 0xffffffff;
2990 if (!fmt
->qnan_msb_set
)
2991 sig_hi
^= 1 << 30 | 1 << 29;
2993 /* Intel requires the explicit integer bit to be set, otherwise
2994 it considers the value a "pseudo-nan". Motorola docs say it
2996 sig_hi
|= 0x80000000;
3001 sig_lo
= sig_hi
= 0xffffffff;
3009 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3010 whereas the intermediate representation is 0.F x 2**exp.
3011 Which means we're off by one.
3013 Except for Motorola, which consider exp=0 and explicit
3014 integer bit set to continue to be normalized. In theory
3015 this descrepency has been taken care of by the difference
3016 in fmt->emin in round_for_format. */
3028 if (HOST_BITS_PER_LONG
== 32)
3030 sig_hi
= r
->sig
[SIGSZ
-1];
3031 sig_lo
= r
->sig
[SIGSZ
-2];
3035 sig_lo
= r
->sig
[SIGSZ
-1];
3036 sig_hi
= sig_lo
>> 31 >> 1;
3037 sig_lo
&= 0xffffffff;
3046 if (FLOAT_WORDS_BIG_ENDIAN
)
3047 buf
[0] = image_hi
<< 16, buf
[1] = sig_hi
, buf
[2] = sig_lo
;
3049 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3053 encode_ieee_extended_128 (fmt
, buf
, r
)
3054 const struct real_format
*fmt
;
3056 const REAL_VALUE_TYPE
*r
;
3058 buf
[3 * !FLOAT_WORDS_BIG_ENDIAN
] = 0;
3059 encode_ieee_extended (fmt
, buf
+!!FLOAT_WORDS_BIG_ENDIAN
, r
);
3063 decode_ieee_extended (fmt
, r
, buf
)
3064 const struct real_format
*fmt
;
3068 unsigned long image_hi
, sig_hi
, sig_lo
;
3072 if (FLOAT_WORDS_BIG_ENDIAN
)
3073 image_hi
= buf
[0] >> 16, sig_hi
= buf
[1], sig_lo
= buf
[2];
3075 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3076 sig_lo
&= 0xffffffff;
3077 sig_hi
&= 0xffffffff;
3078 image_hi
&= 0xffffffff;
3080 sign
= (image_hi
>> 15) & 1;
3081 exp
= image_hi
& 0x7fff;
3083 memset (r
, 0, sizeof (*r
));
3087 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3089 r
->class = rvc_normal
;
3092 /* When the IEEE format contains a hidden bit, we know that
3093 it's zero at this point, and so shift up the significand
3094 and decrease the exponent to match. In this case, Motorola
3095 defines the explicit integer bit to be valid, so we don't
3096 know whether the msb is set or not. */
3098 if (HOST_BITS_PER_LONG
== 32)
3100 r
->sig
[SIGSZ
-1] = sig_hi
;
3101 r
->sig
[SIGSZ
-2] = sig_lo
;
3104 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3108 else if (fmt
->has_signed_zero
)
3111 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3113 /* See above re "pseudo-infinities" and "pseudo-nans".
3114 Short summary is that the MSB will likely always be
3115 set, and that we don't care about it. */
3116 sig_hi
&= 0x7fffffff;
3118 if (sig_hi
|| sig_lo
)
3122 if (HOST_BITS_PER_LONG
== 32)
3124 r
->sig
[SIGSZ
-1] = sig_hi
;
3125 r
->sig
[SIGSZ
-2] = sig_lo
;
3128 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3130 if (!fmt
->qnan_msb_set
)
3131 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
3141 r
->class = rvc_normal
;
3143 r
->exp
= exp
- 16383 + 1;
3144 if (HOST_BITS_PER_LONG
== 32)
3146 r
->sig
[SIGSZ
-1] = sig_hi
;
3147 r
->sig
[SIGSZ
-2] = sig_lo
;
3150 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3155 decode_ieee_extended_128 (fmt
, r
, buf
)
3156 const struct real_format
*fmt
;
3160 decode_ieee_extended (fmt
, r
, buf
+!!FLOAT_WORDS_BIG_ENDIAN
);
3163 const struct real_format ieee_extended_motorola_format
=
3165 encode_ieee_extended
,
3166 decode_ieee_extended
,
3179 const struct real_format ieee_extended_intel_96_format
=
3181 encode_ieee_extended
,
3182 decode_ieee_extended
,
3195 const struct real_format ieee_extended_intel_128_format
=
3197 encode_ieee_extended_128
,
3198 decode_ieee_extended_128
,
3212 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3213 numbers whose sum is equal to the extended precision value. The number
3214 with greater magnitude is first. This format has the same magnitude
3215 range as an IEEE double precision value, but effectively 106 bits of
3216 significand precision. Infinity and NaN are represented by their IEEE
3217 double precision value stored in the first number, the second number is
3218 ignored. Zeroes, Infinities, and NaNs are set in both doubles
3219 due to precedent. */
3221 static void encode_ibm_extended
PARAMS ((const struct real_format
*fmt
,
3222 long *, const REAL_VALUE_TYPE
*));
3223 static void decode_ibm_extended
PARAMS ((const struct real_format
*,
3224 REAL_VALUE_TYPE
*, const long *));
3227 encode_ibm_extended (fmt
, buf
, r
)
3228 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3230 const REAL_VALUE_TYPE
*r
;
3232 REAL_VALUE_TYPE u
, v
;
3237 /* Both doubles have sign bit set. */
3238 buf
[0] = FLOAT_WORDS_BIG_ENDIAN
? r
->sign
<< 31 : 0;
3239 buf
[1] = FLOAT_WORDS_BIG_ENDIAN
? 0 : r
->sign
<< 31;
3246 /* Both doubles set to Inf / NaN. */
3247 encode_ieee_double (&ieee_double_format
, &buf
[0], r
);
3253 /* u = IEEE double precision portion of significand. */
3255 clear_significand_below (&u
, SIGNIFICAND_BITS
- 53);
3257 /* v = remainder containing additional 53 bits of significand. */
3258 do_add (&v
, r
, &u
, 1);
3260 encode_ieee_double (&ieee_double_format
, &buf
[0], &u
);
3261 encode_ieee_double (&ieee_double_format
, &buf
[2], &v
);
3270 decode_ibm_extended (fmt
, r
, buf
)
3271 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3275 REAL_VALUE_TYPE u
, v
;
3277 decode_ieee_double (&ieee_double_format
, &u
, &buf
[0]);
3279 if (u
.class != rvc_zero
&& u
.class != rvc_inf
&& u
.class != rvc_nan
)
3281 decode_ieee_double (&ieee_double_format
, &v
, &buf
[2]);
3282 do_add (r
, &u
, &v
, 0);
3288 const struct real_format ibm_extended_format
=
3290 encode_ibm_extended
,
3291 decode_ibm_extended
,
3305 /* IEEE quad precision format. */
3307 static void encode_ieee_quad
PARAMS ((const struct real_format
*fmt
,
3308 long *, const REAL_VALUE_TYPE
*));
3309 static void decode_ieee_quad
PARAMS ((const struct real_format
*,
3310 REAL_VALUE_TYPE
*, const long *));
3313 encode_ieee_quad (fmt
, buf
, r
)
3314 const struct real_format
*fmt
;
3316 const REAL_VALUE_TYPE
*r
;
3318 unsigned long image3
, image2
, image1
, image0
, exp
;
3319 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3322 image3
= r
->sign
<< 31;
3327 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3336 image3
|= 32767 << 16;
3339 image3
|= 0x7fffffff;
3340 image2
= 0xffffffff;
3341 image1
= 0xffffffff;
3342 image0
= 0xffffffff;
3349 image3
|= 32767 << 16;
3351 if (HOST_BITS_PER_LONG
== 32)
3356 image3
|= u
.sig
[3] & 0xffff;
3361 image1
= image0
>> 31 >> 1;
3363 image3
|= (image2
>> 31 >> 1) & 0xffff;
3364 image0
&= 0xffffffff;
3365 image2
&= 0xffffffff;
3368 if (!fmt
->qnan_msb_set
)
3369 image3
^= 1 << 15 | 1 << 14;
3373 image3
|= 0x7fffffff;
3374 image2
= 0xffffffff;
3375 image1
= 0xffffffff;
3376 image0
= 0xffffffff;
3381 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3382 whereas the intermediate representation is 0.F x 2**exp.
3383 Which means we're off by one. */
3387 exp
= r
->exp
+ 16383 - 1;
3388 image3
|= exp
<< 16;
3390 if (HOST_BITS_PER_LONG
== 32)
3395 image3
|= u
.sig
[3] & 0xffff;
3400 image1
= image0
>> 31 >> 1;
3402 image3
|= (image2
>> 31 >> 1) & 0xffff;
3403 image0
&= 0xffffffff;
3404 image2
&= 0xffffffff;
3412 if (FLOAT_WORDS_BIG_ENDIAN
)
3429 decode_ieee_quad (fmt
, r
, buf
)
3430 const struct real_format
*fmt
;
3434 unsigned long image3
, image2
, image1
, image0
;
3438 if (FLOAT_WORDS_BIG_ENDIAN
)
3452 image0
&= 0xffffffff;
3453 image1
&= 0xffffffff;
3454 image2
&= 0xffffffff;
3456 sign
= (image3
>> 31) & 1;
3457 exp
= (image3
>> 16) & 0x7fff;
3460 memset (r
, 0, sizeof (*r
));
3464 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
3466 r
->class = rvc_normal
;
3469 r
->exp
= -16382 + (SIGNIFICAND_BITS
- 112);
3470 if (HOST_BITS_PER_LONG
== 32)
3479 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3480 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3485 else if (fmt
->has_signed_zero
)
3488 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3490 if (image3
| image2
| image1
| image0
)
3495 if (HOST_BITS_PER_LONG
== 32)
3504 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3505 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3507 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3509 if (!fmt
->qnan_msb_set
)
3510 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
3520 r
->class = rvc_normal
;
3522 r
->exp
= exp
- 16383 + 1;
3524 if (HOST_BITS_PER_LONG
== 32)
3533 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3534 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3536 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3537 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3541 const struct real_format ieee_quad_format
=
3557 /* Descriptions of VAX floating point formats can be found beginning at
3559 http://www.openvms.compaq.com:8000/73final/4515/4515pro_013.html#f_floating_point_format
3561 The thing to remember is that they're almost IEEE, except for word
3562 order, exponent bias, and the lack of infinities, nans, and denormals.
3564 We don't implement the H_floating format here, simply because neither
3565 the VAX or Alpha ports use it. */
3567 static void encode_vax_f
PARAMS ((const struct real_format
*fmt
,
3568 long *, const REAL_VALUE_TYPE
*));
3569 static void decode_vax_f
PARAMS ((const struct real_format
*,
3570 REAL_VALUE_TYPE
*, const long *));
3571 static void encode_vax_d
PARAMS ((const struct real_format
*fmt
,
3572 long *, const REAL_VALUE_TYPE
*));
3573 static void decode_vax_d
PARAMS ((const struct real_format
*,
3574 REAL_VALUE_TYPE
*, const long *));
3575 static void encode_vax_g
PARAMS ((const struct real_format
*fmt
,
3576 long *, const REAL_VALUE_TYPE
*));
3577 static void decode_vax_g
PARAMS ((const struct real_format
*,
3578 REAL_VALUE_TYPE
*, const long *));
3581 encode_vax_f (fmt
, buf
, r
)
3582 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3584 const REAL_VALUE_TYPE
*r
;
3586 unsigned long sign
, exp
, sig
, image
;
3588 sign
= r
->sign
<< 15;
3598 image
= 0xffff7fff | sign
;
3602 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
3605 image
= (sig
<< 16) & 0xffff0000;
3619 decode_vax_f (fmt
, r
, buf
)
3620 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3624 unsigned long image
= buf
[0] & 0xffffffff;
3625 int exp
= (image
>> 7) & 0xff;
3627 memset (r
, 0, sizeof (*r
));
3631 r
->class = rvc_normal
;
3632 r
->sign
= (image
>> 15) & 1;
3635 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
3636 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
3641 encode_vax_d (fmt
, buf
, r
)
3642 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3644 const REAL_VALUE_TYPE
*r
;
3646 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3651 image0
= image1
= 0;
3656 image0
= 0xffff7fff | sign
;
3657 image1
= 0xffffffff;
3661 /* Extract the significand into straight hi:lo. */
3662 if (HOST_BITS_PER_LONG
== 64)
3664 image0
= r
->sig
[SIGSZ
-1];
3665 image1
= (image0
>> (64 - 56)) & 0xffffffff;
3666 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
3670 image0
= r
->sig
[SIGSZ
-1];
3671 image1
= r
->sig
[SIGSZ
-2];
3672 image1
= (image0
<< 24) | (image1
>> 8);
3673 image0
= (image0
>> 8) & 0xffffff;
3676 /* Rearrange the half-words of the significand to match the
3678 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
3679 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
3681 /* Add the sign and exponent. */
3683 image0
|= (r
->exp
+ 128) << 7;
3690 if (FLOAT_WORDS_BIG_ENDIAN
)
3691 buf
[0] = image1
, buf
[1] = image0
;
3693 buf
[0] = image0
, buf
[1] = image1
;
3697 decode_vax_d (fmt
, r
, buf
)
3698 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3702 unsigned long image0
, image1
;
3705 if (FLOAT_WORDS_BIG_ENDIAN
)
3706 image1
= buf
[0], image0
= buf
[1];
3708 image0
= buf
[0], image1
= buf
[1];
3709 image0
&= 0xffffffff;
3710 image1
&= 0xffffffff;
3712 exp
= (image0
>> 7) & 0x7f;
3714 memset (r
, 0, sizeof (*r
));
3718 r
->class = rvc_normal
;
3719 r
->sign
= (image0
>> 15) & 1;
3722 /* Rearrange the half-words of the external format into
3723 proper ascending order. */
3724 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
3725 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
3727 if (HOST_BITS_PER_LONG
== 64)
3729 image0
= (image0
<< 31 << 1) | image1
;
3732 r
->sig
[SIGSZ
-1] = image0
;
3736 r
->sig
[SIGSZ
-1] = image0
;
3737 r
->sig
[SIGSZ
-2] = image1
;
3738 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
3739 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3745 encode_vax_g (fmt
, buf
, r
)
3746 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3748 const REAL_VALUE_TYPE
*r
;
3750 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3755 image0
= image1
= 0;
3760 image0
= 0xffff7fff | sign
;
3761 image1
= 0xffffffff;
3765 /* Extract the significand into straight hi:lo. */
3766 if (HOST_BITS_PER_LONG
== 64)
3768 image0
= r
->sig
[SIGSZ
-1];
3769 image1
= (image0
>> (64 - 53)) & 0xffffffff;
3770 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
3774 image0
= r
->sig
[SIGSZ
-1];
3775 image1
= r
->sig
[SIGSZ
-2];
3776 image1
= (image0
<< 21) | (image1
>> 11);
3777 image0
= (image0
>> 11) & 0xfffff;
3780 /* Rearrange the half-words of the significand to match the
3782 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
3783 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
3785 /* Add the sign and exponent. */
3787 image0
|= (r
->exp
+ 1024) << 4;
3794 if (FLOAT_WORDS_BIG_ENDIAN
)
3795 buf
[0] = image1
, buf
[1] = image0
;
3797 buf
[0] = image0
, buf
[1] = image1
;
3801 decode_vax_g (fmt
, r
, buf
)
3802 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3806 unsigned long image0
, image1
;
3809 if (FLOAT_WORDS_BIG_ENDIAN
)
3810 image1
= buf
[0], image0
= buf
[1];
3812 image0
= buf
[0], image1
= buf
[1];
3813 image0
&= 0xffffffff;
3814 image1
&= 0xffffffff;
3816 exp
= (image0
>> 4) & 0x7ff;
3818 memset (r
, 0, sizeof (*r
));
3822 r
->class = rvc_normal
;
3823 r
->sign
= (image0
>> 15) & 1;
3824 r
->exp
= exp
- 1024;
3826 /* Rearrange the half-words of the external format into
3827 proper ascending order. */
3828 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
3829 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
3831 if (HOST_BITS_PER_LONG
== 64)
3833 image0
= (image0
<< 31 << 1) | image1
;
3836 r
->sig
[SIGSZ
-1] = image0
;
3840 r
->sig
[SIGSZ
-1] = image0
;
3841 r
->sig
[SIGSZ
-2] = image1
;
3842 lshift_significand (r
, r
, 64 - 53);
3843 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3848 const struct real_format vax_f_format
=
3864 const struct real_format vax_d_format
=
3880 const struct real_format vax_g_format
=
3896 /* A good reference for these can be found in chapter 9 of
3897 "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
3898 An on-line version can be found here:
3900 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3903 static void encode_i370_single
PARAMS ((const struct real_format
*fmt
,
3904 long *, const REAL_VALUE_TYPE
*));
3905 static void decode_i370_single
PARAMS ((const struct real_format
*,
3906 REAL_VALUE_TYPE
*, const long *));
3907 static void encode_i370_double
PARAMS ((const struct real_format
*fmt
,
3908 long *, const REAL_VALUE_TYPE
*));
3909 static void decode_i370_double
PARAMS ((const struct real_format
*,
3910 REAL_VALUE_TYPE
*, const long *));
3913 encode_i370_single (fmt
, buf
, r
)
3914 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3916 const REAL_VALUE_TYPE
*r
;
3918 unsigned long sign
, exp
, sig
, image
;
3920 sign
= r
->sign
<< 31;
3930 image
= 0x7fffffff | sign
;
3934 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0xffffff;
3935 exp
= ((r
->exp
/ 4) + 64) << 24;
3936 image
= sign
| exp
| sig
;
3947 decode_i370_single (fmt
, r
, buf
)
3948 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3952 unsigned long sign
, sig
, image
= buf
[0];
3955 sign
= (image
>> 31) & 1;
3956 exp
= (image
>> 24) & 0x7f;
3957 sig
= image
& 0xffffff;
3959 memset (r
, 0, sizeof (*r
));
3963 r
->class = rvc_normal
;
3965 r
->exp
= (exp
- 64) * 4;
3966 r
->sig
[SIGSZ
-1] = sig
<< (HOST_BITS_PER_LONG
- 24);
3972 encode_i370_double (fmt
, buf
, r
)
3973 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3975 const REAL_VALUE_TYPE
*r
;
3977 unsigned long sign
, exp
, image_hi
, image_lo
;
3979 sign
= r
->sign
<< 31;
3984 image_hi
= image_lo
= 0;
3989 image_hi
= 0x7fffffff | sign
;
3990 image_lo
= 0xffffffff;
3994 if (HOST_BITS_PER_LONG
== 64)
3996 image_hi
= r
->sig
[SIGSZ
-1];
3997 image_lo
= (image_hi
>> (64 - 56)) & 0xffffffff;
3998 image_hi
= (image_hi
>> (64 - 56 + 1) >> 31) & 0xffffff;
4002 image_hi
= r
->sig
[SIGSZ
-1];
4003 image_lo
= r
->sig
[SIGSZ
-2];
4004 image_lo
= (image_lo
>> 8) | (image_hi
<< 24);
4008 exp
= ((r
->exp
/ 4) + 64) << 24;
4009 image_hi
|= sign
| exp
;
4016 if (FLOAT_WORDS_BIG_ENDIAN
)
4017 buf
[0] = image_hi
, buf
[1] = image_lo
;
4019 buf
[0] = image_lo
, buf
[1] = image_hi
;
4023 decode_i370_double (fmt
, r
, buf
)
4024 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4028 unsigned long sign
, image_hi
, image_lo
;
4031 if (FLOAT_WORDS_BIG_ENDIAN
)
4032 image_hi
= buf
[0], image_lo
= buf
[1];
4034 image_lo
= buf
[0], image_hi
= buf
[1];
4036 sign
= (image_hi
>> 31) & 1;
4037 exp
= (image_hi
>> 24) & 0x7f;
4038 image_hi
&= 0xffffff;
4039 image_lo
&= 0xffffffff;
4041 memset (r
, 0, sizeof (*r
));
4043 if (exp
|| image_hi
|| image_lo
)
4045 r
->class = rvc_normal
;
4047 r
->exp
= (exp
- 64) * 4 + (SIGNIFICAND_BITS
- 56);
4049 if (HOST_BITS_PER_LONG
== 32)
4051 r
->sig
[0] = image_lo
;
4052 r
->sig
[1] = image_hi
;
4055 r
->sig
[0] = image_lo
| (image_hi
<< 31 << 1);
4061 const struct real_format i370_single_format
=
4072 false, /* ??? The encoding does allow for "unnormals". */
4073 false, /* ??? The encoding does allow for "unnormals". */
4077 const struct real_format i370_double_format
=
4088 false, /* ??? The encoding does allow for "unnormals". */
4089 false, /* ??? The encoding does allow for "unnormals". */
4093 /* The "twos-complement" c4x format is officially defined as
4097 This is rather misleading. One must remember that F is signed.
4098 A better description would be
4100 x = -1**s * ((s + 1 + .f) * 2**e
4102 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4103 that's -1 * (1+1+(-.5)) == -1.5. I think.
4105 The constructions here are taken from Tables 5-1 and 5-2 of the
4106 TMS320C4x User's Guide wherein step-by-step instructions for
4107 conversion from IEEE are presented. That's close enough to our
4108 internal representation so as to make things easy.
4110 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
4112 static void encode_c4x_single
PARAMS ((const struct real_format
*fmt
,
4113 long *, const REAL_VALUE_TYPE
*));
4114 static void decode_c4x_single
PARAMS ((const struct real_format
*,
4115 REAL_VALUE_TYPE
*, const long *));
4116 static void encode_c4x_extended
PARAMS ((const struct real_format
*fmt
,
4117 long *, const REAL_VALUE_TYPE
*));
4118 static void decode_c4x_extended
PARAMS ((const struct real_format
*,
4119 REAL_VALUE_TYPE
*, const long *));
4122 encode_c4x_single (fmt
, buf
, r
)
4123 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4125 const REAL_VALUE_TYPE
*r
;
4127 unsigned long image
, exp
, sig
;
4139 sig
= 0x800000 - r
->sign
;
4144 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4159 image
= ((exp
& 0xff) << 24) | (sig
& 0xffffff);
4164 decode_c4x_single (fmt
, r
, buf
)
4165 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4169 unsigned long image
= buf
[0];
4173 exp
= (((image
>> 24) & 0xff) ^ 0x80) - 0x80;
4174 sf
= ((image
& 0xffffff) ^ 0x800000) - 0x800000;
4176 memset (r
, 0, sizeof (*r
));
4180 r
->class = rvc_normal
;
4182 sig
= sf
& 0x7fffff;
4191 sig
= (sig
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4194 r
->sig
[SIGSZ
-1] = sig
;
4199 encode_c4x_extended (fmt
, buf
, r
)
4200 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4202 const REAL_VALUE_TYPE
*r
;
4204 unsigned long exp
, sig
;
4216 sig
= 0x80000000 - r
->sign
;
4222 sig
= r
->sig
[SIGSZ
-1];
4223 if (HOST_BITS_PER_LONG
== 64)
4224 sig
= sig
>> 1 >> 31;
4241 exp
= (exp
& 0xff) << 24;
4244 if (FLOAT_WORDS_BIG_ENDIAN
)
4245 buf
[0] = exp
, buf
[1] = sig
;
4247 buf
[0] = sig
, buf
[0] = exp
;
4251 decode_c4x_extended (fmt
, r
, buf
)
4252 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4259 if (FLOAT_WORDS_BIG_ENDIAN
)
4260 exp
= buf
[0], sf
= buf
[1];
4262 sf
= buf
[0], exp
= buf
[1];
4264 exp
= (((exp
>> 24) & 0xff) & 0x80) - 0x80;
4265 sf
= ((sf
& 0xffffffff) ^ 0x80000000) - 0x80000000;
4267 memset (r
, 0, sizeof (*r
));
4271 r
->class = rvc_normal
;
4273 sig
= sf
& 0x7fffffff;
4282 if (HOST_BITS_PER_LONG
== 64)
4283 sig
= sig
<< 1 << 31;
4287 r
->sig
[SIGSZ
-1] = sig
;
4291 const struct real_format c4x_single_format
=
4307 const struct real_format c4x_extended_format
=
4309 encode_c4x_extended
,
4310 decode_c4x_extended
,
4324 /* A synthetic "format" for internal arithmetic. It's the size of the
4325 internal significand minus the two bits needed for proper rounding.
4326 The encode and decode routines exist only to satisfy our paranoia
4329 static void encode_internal
PARAMS ((const struct real_format
*fmt
,
4330 long *, const REAL_VALUE_TYPE
*));
4331 static void decode_internal
PARAMS ((const struct real_format
*,
4332 REAL_VALUE_TYPE
*, const long *));
4335 encode_internal (fmt
, buf
, r
)
4336 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4338 const REAL_VALUE_TYPE
*r
;
4340 memcpy (buf
, r
, sizeof (*r
));
4344 decode_internal (fmt
, r
, buf
)
4345 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4349 memcpy (r
, buf
, sizeof (*r
));
4352 const struct real_format real_internal_format
=
4358 SIGNIFICAND_BITS
- 2,
4368 /* Set up default mode to format mapping for IEEE. Everyone else has
4369 to set these values in OVERRIDE_OPTIONS. */
4371 const struct real_format
*real_format_for_mode
[TFmode
- QFmode
+ 1] =
4376 &ieee_single_format
, /* SFmode */
4377 &ieee_double_format
, /* DFmode */
4379 /* We explicitly don't handle XFmode. There are two formats,
4380 pretty much equally common. Choose one in OVERRIDE_OPTIONS. */
4382 &ieee_quad_format
/* TFmode */