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 void 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
*));
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, set the least
186 significant bit of R. */
189 sticky_rshift_significand (r
, a
, n
)
191 const REAL_VALUE_TYPE
*a
;
194 unsigned long sticky
= 0;
195 unsigned int i
, ofs
= 0;
197 if (n
>= HOST_BITS_PER_LONG
)
199 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
201 n
&= HOST_BITS_PER_LONG
- 1;
206 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
207 for (i
= 0; i
< SIGSZ
; ++i
)
210 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
211 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
212 << (HOST_BITS_PER_LONG
- n
)));
217 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
218 r
->sig
[i
] = a
->sig
[ofs
+ i
];
219 for (; i
< SIGSZ
; ++i
)
223 r
->sig
[0] |= (sticky
!= 0);
226 /* Right-shift the significand of A by N bits; put the result in the
230 rshift_significand (r
, a
, n
)
232 const REAL_VALUE_TYPE
*a
;
235 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
237 n
&= HOST_BITS_PER_LONG
- 1;
240 for (i
= 0; i
< SIGSZ
; ++i
)
243 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
244 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
245 << (HOST_BITS_PER_LONG
- n
)));
250 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
251 r
->sig
[i
] = a
->sig
[ofs
+ i
];
252 for (; i
< SIGSZ
; ++i
)
257 /* Left-shift the significand of A by N bits; put the result in the
261 lshift_significand (r
, a
, n
)
263 const REAL_VALUE_TYPE
*a
;
266 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
268 n
&= HOST_BITS_PER_LONG
- 1;
271 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
272 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
273 for (; i
< SIGSZ
; ++i
)
274 r
->sig
[SIGSZ
-1-i
] = 0;
277 for (i
= 0; i
< SIGSZ
; ++i
)
280 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
281 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
282 >> (HOST_BITS_PER_LONG
- n
)));
286 /* Likewise, but N is specialized to 1. */
289 lshift_significand_1 (r
, a
)
291 const REAL_VALUE_TYPE
*a
;
295 for (i
= SIGSZ
- 1; i
> 0; --i
)
296 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
297 r
->sig
[0] = a
->sig
[0] << 1;
300 /* Add the significands of A and B, placing the result in R. Return
301 true if there was carry out of the most significant word. */
304 add_significands (r
, a
, b
)
306 const REAL_VALUE_TYPE
*a
, *b
;
311 for (i
= 0; i
< SIGSZ
; ++i
)
313 unsigned long ai
= a
->sig
[i
];
314 unsigned long ri
= ai
+ b
->sig
[i
];
330 /* Subtract the significands of A and B, placing the result in R.
331 Return true if there was carry out of the most significant word. */
334 sub_significands (r
, a
, b
)
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
);
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
;
574 /* Determine if we need to add or subtract. */
576 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
578 switch (CLASS2 (a
->class, b
->class))
580 case CLASS2 (rvc_zero
, rvc_zero
):
581 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
582 get_zero (r
, sign
& !subtract_p
);
585 case CLASS2 (rvc_zero
, rvc_normal
):
586 case CLASS2 (rvc_zero
, rvc_inf
):
587 case CLASS2 (rvc_zero
, rvc_nan
):
589 case CLASS2 (rvc_normal
, rvc_nan
):
590 case CLASS2 (rvc_inf
, rvc_nan
):
591 case CLASS2 (rvc_nan
, rvc_nan
):
592 /* ANY + NaN = NaN. */
593 case CLASS2 (rvc_normal
, rvc_inf
):
596 r
->sign
= sign
^ subtract_p
;
599 case CLASS2 (rvc_normal
, rvc_zero
):
600 case CLASS2 (rvc_inf
, rvc_zero
):
601 case CLASS2 (rvc_nan
, rvc_zero
):
603 case CLASS2 (rvc_nan
, rvc_normal
):
604 case CLASS2 (rvc_nan
, rvc_inf
):
605 /* NaN + ANY = NaN. */
606 case CLASS2 (rvc_inf
, rvc_normal
):
611 case CLASS2 (rvc_inf
, rvc_inf
):
613 /* Inf - Inf = NaN. */
614 get_canonical_qnan (r
, 0);
616 /* Inf + Inf = Inf. */
620 case CLASS2 (rvc_normal
, rvc_normal
):
627 /* Swap the arguments such that A has the larger exponent. */
628 dexp
= a
->exp
- b
->exp
;
631 const REAL_VALUE_TYPE
*t
;
638 /* If the exponents are not identical, we need to shift the
639 significand of B down. */
642 /* If the exponents are too far apart, the significands
643 do not overlap, which makes the subtraction a noop. */
644 if (dexp
>= SIGNIFICAND_BITS
)
651 sticky_rshift_significand (&t
, b
, dexp
);
657 if (sub_significands (r
, a
, b
))
659 /* We got a borrow out of the subtraction. That means that
660 A and B had the same exponent, and B had the larger
661 significand. We need to swap the sign and negate the
664 neg_significand (r
, r
);
669 if (add_significands (r
, a
, b
))
671 /* We got carry out of the addition. This means we need to
672 shift the significand back down one bit and increase the
674 sticky_rshift_significand (r
, r
, 1);
675 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
684 r
->class = rvc_normal
;
688 /* Re-normalize the result. */
691 /* Special case: if the subtraction results in zero, the result
693 if (r
->class == rvc_zero
)
697 /* Return R = A * B. */
700 do_multiply (r
, a
, b
)
702 const REAL_VALUE_TYPE
*a
, *b
;
704 REAL_VALUE_TYPE u
, t
, *rr
;
705 unsigned int i
, j
, k
;
706 int sign
= a
->sign
^ b
->sign
;
708 switch (CLASS2 (a
->class, b
->class))
710 case CLASS2 (rvc_zero
, rvc_zero
):
711 case CLASS2 (rvc_zero
, rvc_normal
):
712 case CLASS2 (rvc_normal
, rvc_zero
):
713 /* +-0 * ANY = 0 with appropriate sign. */
717 case CLASS2 (rvc_zero
, rvc_nan
):
718 case CLASS2 (rvc_normal
, rvc_nan
):
719 case CLASS2 (rvc_inf
, rvc_nan
):
720 case CLASS2 (rvc_nan
, rvc_nan
):
721 /* ANY * NaN = NaN. */
726 case CLASS2 (rvc_nan
, rvc_zero
):
727 case CLASS2 (rvc_nan
, rvc_normal
):
728 case CLASS2 (rvc_nan
, rvc_inf
):
729 /* NaN * ANY = NaN. */
734 case CLASS2 (rvc_zero
, rvc_inf
):
735 case CLASS2 (rvc_inf
, rvc_zero
):
737 get_canonical_qnan (r
, sign
);
740 case CLASS2 (rvc_inf
, rvc_inf
):
741 case CLASS2 (rvc_normal
, rvc_inf
):
742 case CLASS2 (rvc_inf
, rvc_normal
):
743 /* Inf * Inf = Inf, R * Inf = Inf */
748 case CLASS2 (rvc_normal
, rvc_normal
):
755 if (r
== a
|| r
== b
)
761 /* Collect all the partial products. Since we don't have sure access
762 to a widening multiply, we split each long into two half-words.
764 Consider the long-hand form of a four half-word multiplication:
774 We construct partial products of the widened half-word products
775 that are known to not overlap, e.g. DF+DH. Each such partial
776 product is given its proper exponent, which allows us to sum them
777 and obtain the finished product. */
779 for (i
= 0; i
< SIGSZ
* 2; ++i
)
781 unsigned long ai
= a
->sig
[i
/ 2];
783 ai
>>= HOST_BITS_PER_LONG
/ 2;
785 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
790 for (j
= 0; j
< 2; ++j
)
792 int exp
= (a
->exp
- (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
793 + (b
->exp
- (1-j
)*(HOST_BITS_PER_LONG
/2)));
798 /* Would underflow to zero, which we shouldn't bother adding. */
801 u
.class = rvc_normal
;
805 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
807 unsigned long bi
= b
->sig
[k
/ 2];
809 bi
>>= HOST_BITS_PER_LONG
/ 2;
811 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
813 u
.sig
[k
/ 2] = ai
* bi
;
817 do_add (rr
, rr
, &u
, 0);
826 /* Return R = A / B. */
831 const REAL_VALUE_TYPE
*a
, *b
;
833 int exp
, sign
= a
->sign
^ b
->sign
;
834 REAL_VALUE_TYPE t
, *rr
;
837 switch (CLASS2 (a
->class, b
->class))
839 case CLASS2 (rvc_zero
, rvc_zero
):
841 case CLASS2 (rvc_inf
, rvc_inf
):
842 /* Inf / Inf = NaN. */
843 get_canonical_qnan (r
, sign
);
846 case CLASS2 (rvc_zero
, rvc_normal
):
847 case CLASS2 (rvc_zero
, rvc_inf
):
849 case CLASS2 (rvc_normal
, rvc_inf
):
855 case CLASS2 (rvc_normal
, rvc_zero
):
857 case CLASS2 (rvc_inf
, rvc_zero
):
862 case CLASS2 (rvc_zero
, rvc_nan
):
863 case CLASS2 (rvc_normal
, rvc_nan
):
864 case CLASS2 (rvc_inf
, rvc_nan
):
865 case CLASS2 (rvc_nan
, rvc_nan
):
866 /* ANY / NaN = NaN. */
871 case CLASS2 (rvc_nan
, rvc_zero
):
872 case CLASS2 (rvc_nan
, rvc_normal
):
873 case CLASS2 (rvc_nan
, rvc_inf
):
874 /* NaN / ANY = NaN. */
879 case CLASS2 (rvc_inf
, rvc_normal
):
885 case CLASS2 (rvc_normal
, rvc_normal
):
892 if (r
== a
|| r
== b
)
897 rr
->class = rvc_normal
;
900 exp
= a
->exp
- b
->exp
+ 1;
907 inexact
= div_significands (rr
, a
, b
);
909 /* Re-normalize the result. */
911 rr
->sig
[0] |= inexact
;
917 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
918 one of the two operands is a NaN. */
921 do_compare (a
, b
, nan_result
)
922 const REAL_VALUE_TYPE
*a
, *b
;
927 switch (CLASS2 (a
->class, b
->class))
929 case CLASS2 (rvc_zero
, rvc_zero
):
930 /* Sign of zero doesn't matter for compares. */
933 case CLASS2 (rvc_inf
, rvc_zero
):
934 case CLASS2 (rvc_inf
, rvc_normal
):
935 case CLASS2 (rvc_normal
, rvc_zero
):
936 return (a
->sign
? -1 : 1);
938 case CLASS2 (rvc_inf
, rvc_inf
):
939 return -a
->sign
- -b
->sign
;
941 case CLASS2 (rvc_zero
, rvc_normal
):
942 case CLASS2 (rvc_zero
, rvc_inf
):
943 case CLASS2 (rvc_normal
, rvc_inf
):
944 return (b
->sign
? 1 : -1);
946 case CLASS2 (rvc_zero
, rvc_nan
):
947 case CLASS2 (rvc_normal
, rvc_nan
):
948 case CLASS2 (rvc_inf
, rvc_nan
):
949 case CLASS2 (rvc_nan
, rvc_nan
):
950 case CLASS2 (rvc_nan
, rvc_zero
):
951 case CLASS2 (rvc_nan
, rvc_normal
):
952 case CLASS2 (rvc_nan
, rvc_inf
):
955 case CLASS2 (rvc_normal
, rvc_normal
):
962 if (a
->sign
!= b
->sign
)
963 return -a
->sign
- -b
->sign
;
967 else if (a
->exp
< b
->exp
)
970 ret
= cmp_significands (a
, b
);
972 return (a
->sign
? -ret
: ret
);
975 /* Return A truncated to an integral value toward zero. */
980 const REAL_VALUE_TYPE
*a
;
993 get_zero (r
, r
->sign
);
994 else if (r
->exp
< SIGNIFICAND_BITS
)
995 clear_significand_below (r
, SIGNIFICAND_BITS
- r
->exp
);
1003 /* Perform the binary or unary operation described by CODE.
1004 For a unary operation, leave OP1 NULL. */
1007 real_arithmetic (r
, icode
, op0
, op1
)
1010 const REAL_VALUE_TYPE
*op0
, *op1
;
1012 enum tree_code code
= icode
;
1017 do_add (r
, op0
, op1
, 0);
1021 do_add (r
, op0
, op1
, 1);
1025 do_multiply (r
, op0
, op1
);
1029 do_divide (r
, op0
, op1
);
1033 if (op1
->class == rvc_nan
)
1035 else if (do_compare (op0
, op1
, -1) < 0)
1042 if (op1
->class == rvc_nan
)
1044 else if (do_compare (op0
, op1
, 1) < 0)
1060 case FIX_TRUNC_EXPR
:
1061 do_fix_trunc (r
, op0
);
1069 /* Legacy. Similar, but return the result directly. */
1072 real_arithmetic2 (icode
, op0
, op1
)
1074 const REAL_VALUE_TYPE
*op0
, *op1
;
1077 real_arithmetic (&r
, icode
, op0
, op1
);
1082 real_compare (icode
, op0
, op1
)
1084 const REAL_VALUE_TYPE
*op0
, *op1
;
1086 enum tree_code code
= icode
;
1091 return do_compare (op0
, op1
, 1) < 0;
1093 return do_compare (op0
, op1
, 1) <= 0;
1095 return do_compare (op0
, op1
, -1) > 0;
1097 return do_compare (op0
, op1
, -1) >= 0;
1099 return do_compare (op0
, op1
, -1) == 0;
1101 return do_compare (op0
, op1
, -1) != 0;
1102 case UNORDERED_EXPR
:
1103 return op0
->class == rvc_nan
|| op1
->class == rvc_nan
;
1105 return op0
->class != rvc_nan
&& op1
->class != rvc_nan
;
1107 return do_compare (op0
, op1
, -1) < 0;
1109 return do_compare (op0
, op1
, -1) <= 0;
1111 return do_compare (op0
, op1
, 1) > 0;
1113 return do_compare (op0
, op1
, 1) >= 0;
1115 return do_compare (op0
, op1
, 0) == 0;
1122 /* Return floor log2(R). */
1126 const REAL_VALUE_TYPE
*r
;
1134 return (unsigned int)-1 >> 1;
1142 /* R = OP0 * 2**EXP. */
1145 real_ldexp (r
, op0
, exp
)
1147 const REAL_VALUE_TYPE
*op0
;
1161 get_inf (r
, r
->sign
);
1162 else if (exp
< -MAX_EXP
)
1163 get_zero (r
, r
->sign
);
1173 /* Determine whether a floating-point value X is infinite. */
1177 const REAL_VALUE_TYPE
*r
;
1179 return (r
->class == rvc_inf
);
1182 /* Determine whether a floating-point value X is a NaN. */
1186 const REAL_VALUE_TYPE
*r
;
1188 return (r
->class == rvc_nan
);
1191 /* Determine whether a floating-point value X is negative. */
1195 const REAL_VALUE_TYPE
*r
;
1200 /* Determine whether a floating-point value X is minus zero. */
1204 const REAL_VALUE_TYPE
*r
;
1206 return r
->sign
&& r
->class == rvc_zero
;
1209 /* Compare two floating-point objects for bitwise identity. */
1212 real_identical (a
, b
)
1213 const REAL_VALUE_TYPE
*a
, *b
;
1217 if (a
->class != b
->class)
1219 if (a
->sign
!= b
->sign
)
1229 if (a
->exp
!= b
->exp
)
1233 for (i
= 0; i
< SIGSZ
; ++i
)
1234 if (a
->sig
[i
] != b
->sig
[i
])
1245 /* Try to change R into its exact multiplicative inverse in machine
1246 mode MODE. Return true if successful. */
1249 exact_real_inverse (mode
, r
)
1250 enum machine_mode mode
;
1253 const REAL_VALUE_TYPE
*one
= real_digit (1);
1257 if (r
->class != rvc_normal
)
1260 /* Check for a power of two: all significand bits zero except the MSB. */
1261 for (i
= 0; i
< SIGSZ
-1; ++i
)
1264 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1267 /* Find the inverse and truncate to the required mode. */
1268 do_divide (&u
, one
, r
);
1269 real_convert (&u
, mode
, &u
);
1271 /* The rounding may have overflowed. */
1272 if (u
.class != rvc_normal
)
1274 for (i
= 0; i
< SIGSZ
-1; ++i
)
1277 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1284 /* Render R as an integer. */
1288 const REAL_VALUE_TYPE
*r
;
1290 unsigned HOST_WIDE_INT i
;
1301 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1309 if (r
->exp
> HOST_BITS_PER_WIDE_INT
)
1312 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1313 i
= r
->sig
[SIGSZ
-1];
1314 else if (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
)
1316 i
= r
->sig
[SIGSZ
-1];
1317 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1318 i
|= r
->sig
[SIGSZ
-2];
1323 i
>>= HOST_BITS_PER_WIDE_INT
- r
->exp
;
1334 /* Likewise, but to an integer pair, HI+LOW. */
1337 real_to_integer2 (plow
, phigh
, r
)
1338 HOST_WIDE_INT
*plow
, *phigh
;
1339 const REAL_VALUE_TYPE
*r
;
1342 HOST_WIDE_INT low
, high
;
1355 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1369 if (exp
>= 2*HOST_BITS_PER_WIDE_INT
)
1372 rshift_significand (&t
, r
, 2*HOST_BITS_PER_WIDE_INT
- exp
);
1373 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1375 high
= t
.sig
[SIGSZ
-1];
1376 low
= t
.sig
[SIGSZ
-2];
1378 else if (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
)
1380 high
= t
.sig
[SIGSZ
-1];
1381 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1382 high
|= t
.sig
[SIGSZ
-2];
1384 low
= t
.sig
[SIGSZ
-3];
1385 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1386 low
|= t
.sig
[SIGSZ
-4];
1396 low
= -low
, high
= ~high
;
1408 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1409 of NUM / DEN. Return the quotient and place the remainder in NUM.
1410 It is expected that NUM / DEN are close enough that the quotient is
1413 static unsigned long
1414 rtd_divmod (num
, den
)
1415 REAL_VALUE_TYPE
*num
, *den
;
1417 unsigned long q
, msb
;
1418 int expn
= num
->exp
, expd
= den
->exp
;
1427 msb
= num
->sig
[SIGSZ
-1] & SIG_MSB
;
1429 lshift_significand_1 (num
, num
);
1431 if (msb
|| cmp_significands (num
, den
) >= 0)
1433 sub_significands (num
, num
, den
);
1437 while (--expn
>= expd
);
1445 /* Render R as a decimal floating point constant. Emit DIGITS significant
1446 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1447 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1450 #define M_LOG10_2 0.30102999566398119521
1453 real_to_decimal (str
, r_orig
, buf_size
, digits
, crop_trailing_zeros
)
1455 const REAL_VALUE_TYPE
*r_orig
;
1456 size_t buf_size
, digits
;
1457 int crop_trailing_zeros
;
1459 const REAL_VALUE_TYPE
*one
, *ten
;
1460 REAL_VALUE_TYPE r
, pten
, u
, v
;
1461 int dec_exp
, cmp_one
, digit
;
1463 char *p
, *first
, *last
;
1470 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1475 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1478 /* ??? Print the significand as well, if not canonical? */
1479 strcpy (str
, (r
.sign
? "-NaN" : "+NaN"));
1485 /* Estimate the decimal exponent, and compute the length of the string it
1486 will print as. Be conservative and add one to account for possible
1487 overflow or rounding error. */
1488 dec_exp
= r
.exp
* M_LOG10_2
;
1489 for (max_digits
= 1; dec_exp
; max_digits
++)
1492 /* Bound the number of digits printed by the size of the output buffer. */
1493 max_digits
= buf_size
- 1 - 1 - 2 - max_digits
- 1;
1494 if (max_digits
> buf_size
)
1496 if (digits
> max_digits
)
1497 digits
= max_digits
;
1499 /* Bound the number of digits printed by the size of the representation. */
1500 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1501 if (digits
== 0 || digits
> max_digits
)
1502 digits
= max_digits
;
1504 one
= real_digit (1);
1505 ten
= ten_to_ptwo (0);
1513 cmp_one
= do_compare (&r
, one
, 0);
1518 /* Number is greater than one. Convert significand to an integer
1519 and strip trailing decimal zeros. */
1522 u
.exp
= SIGNIFICAND_BITS
- 1;
1524 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1525 m
= floor_log2 (max_digits
);
1527 /* Iterate over the bits of the possible powers of 10 that might
1528 be present in U and eliminate them. That is, if we find that
1529 10**2**M divides U evenly, keep the division and increase
1535 do_divide (&t
, &u
, ten_to_ptwo (m
));
1536 do_fix_trunc (&v
, &t
);
1537 if (cmp_significands (&v
, &t
) == 0)
1545 /* Revert the scaling to integer that we performed earlier. */
1546 u
.exp
+= r
.exp
- (SIGNIFICAND_BITS
- 1);
1549 /* Find power of 10. Do this by dividing out 10**2**M when
1550 this is larger than the current remainder. Fill PTEN with
1551 the power of 10 that we compute. */
1552 m
= floor_log2 ((int)(r
.exp
* M_LOG10_2
)) + 1;
1555 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1556 if (do_compare (&u
, ptentwo
, 0) >= 0)
1558 do_divide (&u
, &u
, ptentwo
);
1559 do_multiply (&pten
, &pten
, ptentwo
);
1565 else if (cmp_one
< 0)
1569 /* Number is less than one. Pad significand with leading
1575 /* Stop if we'd shift bits off the bottom. */
1579 do_multiply (&u
, &v
, ten
);
1581 /* Stop if we're now >= 1. */
1590 /* Find power of 10. Do this by multiplying in P=10**2**M when
1591 the current remainder is smaller than 1/P. Fill PTEN with the
1592 power of 10 that we compute. */
1593 m
= floor_log2 ((int)(-r
.exp
* M_LOG10_2
)) + 1;
1596 const REAL_VALUE_TYPE
*ptentwo
= ten_to_ptwo (m
);
1597 const REAL_VALUE_TYPE
*ptenmtwo
= ten_to_mptwo (m
);
1599 if (do_compare (&v
, ptenmtwo
, 0) <= 0)
1601 do_multiply (&v
, &v
, ptentwo
);
1602 do_multiply (&pten
, &pten
, ptentwo
);
1608 /* Invert the positive power of 10 that we've collected so far. */
1609 do_divide (&pten
, one
, &pten
);
1617 /* At this point, PTEN should contain the nearest power of 10 smaller
1618 than R, such that this division produces the first digit.
1620 Using a divide-step primitive that returns the complete integral
1621 remainder avoids the rounding error that would be produced if
1622 we were to use do_divide here and then simply multiply by 10 for
1623 each subsequent digit. */
1625 digit
= rtd_divmod (&r
, &pten
);
1627 /* Be prepared for error in that division via underflow ... */
1628 if (digit
== 0 && cmp_significand_0 (&r
))
1630 /* Multiply by 10 and try again. */
1631 do_multiply (&r
, &r
, ten
);
1632 digit
= rtd_divmod (&r
, &pten
);
1638 /* ... or overflow. */
1646 else if (digit
> 10)
1651 /* Generate subsequent digits. */
1652 while (--digits
> 0)
1654 do_multiply (&r
, &r
, ten
);
1655 digit
= rtd_divmod (&r
, &pten
);
1660 /* Generate one more digit with which to do rounding. */
1661 do_multiply (&r
, &r
, ten
);
1662 digit
= rtd_divmod (&r
, &pten
);
1664 /* Round the result. */
1667 /* Round to nearest. If R is non-zero there are additional
1668 non-zero digits to be extracted. */
1669 if (cmp_significand_0 (&r
))
1671 /* Round to even. */
1672 else if ((p
[-1] - '0') & 1)
1689 /* Carry out of the first digit. This means we had all 9's and
1690 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1698 /* Insert the decimal point. */
1699 first
[0] = first
[1];
1702 /* If requested, drop trailing zeros. Never crop past "1.0". */
1703 if (crop_trailing_zeros
)
1704 while (last
> first
+ 3 && last
[-1] == '0')
1707 /* Append the exponent. */
1708 sprintf (last
, "e%+d", dec_exp
);
1711 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1712 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1713 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1714 strip trailing zeros. */
1717 real_to_hexadecimal (str
, r
, buf_size
, digits
, crop_trailing_zeros
)
1719 const REAL_VALUE_TYPE
*r
;
1720 size_t buf_size
, digits
;
1721 int crop_trailing_zeros
;
1723 int i
, j
, exp
= r
->exp
;
1736 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1739 /* ??? Print the significand as well, if not canonical? */
1740 strcpy (str
, (r
->sign
? "-NaN" : "+NaN"));
1747 digits
= SIGNIFICAND_BITS
/ 4;
1749 /* Bound the number of digits printed by the size of the output buffer. */
1751 sprintf (exp_buf
, "p%+d", exp
);
1752 max_digits
= buf_size
- strlen (exp_buf
) - r
->sign
- 4 - 1;
1753 if (max_digits
> buf_size
)
1755 if (digits
> max_digits
)
1756 digits
= max_digits
;
1767 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1768 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1770 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1776 if (crop_trailing_zeros
)
1777 while (p
> first
+ 1 && p
[-1] == '0')
1780 sprintf (p
, "p%+d", exp
);
1783 /* Initialize R from a decimal or hexadecimal string. The string is
1784 assumed to have been syntax checked already. */
1787 real_from_string (r
, str
)
1801 else if (*str
== '+')
1804 if (str
[0] == '0' && str
[1] == 'x')
1806 /* Hexadecimal floating point. */
1807 int pos
= SIGNIFICAND_BITS
- 4, d
;
1815 d
= hex_value (*str
);
1820 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1821 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1830 if (pos
== SIGNIFICAND_BITS
- 4)
1837 d
= hex_value (*str
);
1842 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1843 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1849 if (*str
== 'p' || *str
== 'P')
1851 bool exp_neg
= false;
1859 else if (*str
== '+')
1863 while (ISDIGIT (*str
))
1869 /* Overflowed the exponent. */
1883 r
->class = rvc_normal
;
1890 /* Decimal floating point. */
1891 const REAL_VALUE_TYPE
*ten
= ten_to_ptwo (0);
1896 while (ISDIGIT (*str
))
1899 do_multiply (r
, r
, ten
);
1901 do_add (r
, r
, real_digit (d
), 0);
1906 if (r
->class == rvc_zero
)
1911 while (ISDIGIT (*str
))
1914 do_multiply (r
, r
, ten
);
1916 do_add (r
, r
, real_digit (d
), 0);
1921 if (*str
== 'e' || *str
== 'E')
1923 bool exp_neg
= false;
1931 else if (*str
== '+')
1935 while (ISDIGIT (*str
))
1941 /* Overflowed the exponent. */
1955 times_pten (r
, exp
);
1970 /* Legacy. Similar, but return the result directly. */
1973 real_from_string2 (s
, mode
)
1975 enum machine_mode mode
;
1979 real_from_string (&r
, s
);
1980 if (mode
!= VOIDmode
)
1981 real_convert (&r
, mode
, &r
);
1986 /* Initialize R from the integer pair HIGH+LOW. */
1989 real_from_integer (r
, mode
, low
, high
, unsigned_p
)
1991 enum machine_mode mode
;
1992 unsigned HOST_WIDE_INT low
;
1996 if (low
== 0 && high
== 0)
2000 r
->class = rvc_normal
;
2001 r
->sign
= high
< 0 && !unsigned_p
;
2002 r
->exp
= 2 * HOST_BITS_PER_WIDE_INT
;
2013 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
2015 r
->sig
[SIGSZ
-1] = high
;
2016 r
->sig
[SIGSZ
-2] = low
;
2017 memset (r
->sig
, 0, sizeof(long)*(SIGSZ
-2));
2019 else if (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
)
2021 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2022 r
->sig
[SIGSZ
-2] = high
;
2023 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
2024 r
->sig
[SIGSZ
-4] = low
;
2026 memset (r
->sig
, 0, sizeof(long)*(SIGSZ
-4));
2034 if (mode
!= VOIDmode
)
2035 real_convert (r
, mode
, r
);
2038 /* Returns 10**2**N. */
2040 static const REAL_VALUE_TYPE
*
2044 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2046 if (n
< 0 || n
>= EXP_BITS
)
2049 if (tens
[n
].class == rvc_zero
)
2051 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
2053 HOST_WIDE_INT t
= 10;
2056 for (i
= 0; i
< n
; ++i
)
2059 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
2063 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
2064 do_multiply (&tens
[n
], t
, t
);
2071 /* Returns 10**(-2**N). */
2073 static const REAL_VALUE_TYPE
*
2077 static REAL_VALUE_TYPE tens
[EXP_BITS
];
2079 if (n
< 0 || n
>= EXP_BITS
)
2082 if (tens
[n
].class == rvc_zero
)
2083 do_divide (&tens
[n
], real_digit (1), ten_to_ptwo (n
));
2090 static const REAL_VALUE_TYPE
*
2094 static REAL_VALUE_TYPE num
[10];
2099 if (n
> 0 && num
[n
].class == rvc_zero
)
2100 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
2105 /* Multiply R by 10**EXP. */
2112 REAL_VALUE_TYPE pten
, *rr
;
2113 bool negative
= (exp
< 0);
2119 pten
= *real_digit (1);
2125 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
2127 do_multiply (rr
, rr
, ten_to_ptwo (i
));
2130 do_divide (r
, r
, &pten
);
2133 /* Fills R with +Inf. */
2142 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2143 we force a QNaN, else we force an SNaN. The string, if not empty,
2144 is parsed as a number and placed in the significand. Return true
2145 if the string was successfully parsed. */
2148 real_nan (r
, str
, quiet
, mode
)
2152 enum machine_mode mode
;
2154 const struct real_format
*fmt
;
2156 fmt
= real_format_for_mode
[mode
- QFmode
];
2163 get_canonical_qnan (r
, 0);
2165 get_canonical_snan (r
, 0);
2172 memset (r
, 0, sizeof (*r
));
2175 /* Parse akin to strtol into the significand of R. */
2177 while (ISSPACE (*str
))
2181 else if (*str
== '+')
2191 while ((d
= hex_value (*str
)) < base
)
2198 lshift_significand (r
, r
, 3);
2201 lshift_significand (r
, r
, 4);
2204 lshift_significand_1 (&u
, r
);
2205 lshift_significand (r
, r
, 3);
2206 add_significands (r
, r
, &u
);
2214 add_significands (r
, r
, &u
);
2219 /* Must have consumed the entire string for success. */
2223 /* Shift the significand into place such that the bits
2224 are in the most significant bits for the format. */
2225 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->p
);
2227 /* Our MSB is always unset for NaNs. */
2228 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2230 /* Force quiet or signalling NaN. */
2232 r
->sig
[SIGSZ
-1] |= SIG_MSB
>> 1;
2234 r
->sig
[SIGSZ
-1] &= ~(SIG_MSB
>> 1);
2236 /* Force at least one bit of the significand set. */
2237 for (d
= 0; d
< SIGSZ
; ++d
)
2241 r
->sig
[SIGSZ
-1] |= SIG_MSB
>> 2;
2243 /* Our intermediate format forces QNaNs to have MSB-1 set.
2244 If the target format has QNaNs with the top bit unset,
2245 mirror the output routines and invert the top two bits. */
2246 if (!fmt
->qnan_msb_set
)
2247 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1) | (SIG_MSB
>> 2);
2253 /* Fills R with 2**N. */
2260 memset (r
, 0, sizeof (*r
));
2265 else if (n
< -MAX_EXP
)
2269 r
->class = rvc_normal
;
2271 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2277 round_for_format (fmt
, r
)
2278 const struct real_format
*fmt
;
2282 unsigned long sticky
;
2286 p2
= fmt
->p
* fmt
->log2_b
;
2287 emin2m1
= (fmt
->emin
- 1) * fmt
->log2_b
;
2288 emax2
= fmt
->emax
* fmt
->log2_b
;
2290 np2
= SIGNIFICAND_BITS
- p2
;
2294 get_zero (r
, r
->sign
);
2296 if (!fmt
->has_signed_zero
)
2301 get_inf (r
, r
->sign
);
2306 clear_significand_below (r
, np2
);
2308 /* If we've cleared the entire significand, we need one bit
2309 set for this to continue to be a NaN. */
2310 for (i
= 0; i
< SIGSZ
; ++i
)
2314 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 2;
2324 /* If we're not base2, normalize the exponent to a multiple of
2326 if (fmt
->log2_b
!= 1)
2328 int shift
= r
->exp
& (fmt
->log2_b
- 1);
2331 shift
= fmt
->log2_b
- shift
;
2332 sticky_rshift_significand (r
, r
, shift
);
2337 /* Check the range of the exponent. If we're out of range,
2338 either underflow or overflow. */
2341 else if (r
->exp
<= emin2m1
)
2345 if (!fmt
->has_denorm
)
2347 /* Don't underflow completely until we've had a chance to round. */
2348 if (r
->exp
< emin2m1
)
2353 diff
= emin2m1
- r
->exp
+ 1;
2357 /* De-normalize the significand. */
2358 sticky_rshift_significand (r
, r
, diff
);
2363 /* There are P2 true significand bits, followed by one guard bit,
2364 followed by one sticky bit, followed by stuff. Fold nonzero
2365 stuff into the sticky bit. */
2368 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2369 sticky
|= r
->sig
[i
];
2371 r
->sig
[w
] & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2373 guard
= test_significand_bit (r
, np2
- 1);
2374 lsb
= test_significand_bit (r
, np2
);
2376 /* Round to even. */
2377 if (guard
&& (sticky
|| lsb
))
2381 set_significand_bit (&u
, np2
);
2383 if (add_significands (r
, r
, &u
))
2385 /* Overflow. Means the significand had been all ones, and
2386 is now all zeros. Need to increase the exponent, and
2387 possibly re-normalize it. */
2388 if (++r
->exp
> emax2
)
2390 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2392 if (fmt
->log2_b
!= 1)
2394 int shift
= r
->exp
& (fmt
->log2_b
- 1);
2397 shift
= fmt
->log2_b
- shift
;
2398 sticky_rshift_significand (r
, r
, shift
);
2407 /* Catch underflow that we deferred until after rounding. */
2408 if (r
->exp
<= emin2m1
)
2411 /* Clear out trailing garbage. */
2412 clear_significand_below (r
, np2
);
2415 /* Extend or truncate to a new mode. */
2418 real_convert (r
, mode
, a
)
2420 enum machine_mode mode
;
2421 const REAL_VALUE_TYPE
*a
;
2423 const struct real_format
*fmt
;
2425 fmt
= real_format_for_mode
[mode
- QFmode
];
2430 round_for_format (fmt
, r
);
2432 /* round_for_format de-normalizes denormals. Undo just that part. */
2433 if (r
->class == rvc_normal
)
2437 /* Legacy. Likewise, except return the struct directly. */
2440 real_value_truncate (mode
, a
)
2441 enum machine_mode mode
;
2445 real_convert (&r
, mode
, &a
);
2449 /* Return true if truncating to MODE is exact. */
2452 exact_real_truncate (mode
, a
)
2453 enum machine_mode mode
;
2454 const REAL_VALUE_TYPE
*a
;
2457 real_convert (&t
, mode
, a
);
2458 return real_identical (&t
, a
);
2461 /* Write R to the given target format. Place the words of the result
2462 in target word order in BUF. There are always 32 bits in each
2463 long, no matter the size of the host long.
2465 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2468 real_to_target_fmt (buf
, r_orig
, fmt
)
2470 const REAL_VALUE_TYPE
*r_orig
;
2471 const struct real_format
*fmt
;
2477 round_for_format (fmt
, &r
);
2481 (*fmt
->encode
) (fmt
, buf
, &r
);
2486 /* Similar, but look up the format from MODE. */
2489 real_to_target (buf
, r
, mode
)
2491 const REAL_VALUE_TYPE
*r
;
2492 enum machine_mode mode
;
2494 const struct real_format
*fmt
;
2496 fmt
= real_format_for_mode
[mode
- QFmode
];
2500 return real_to_target_fmt (buf
, r
, fmt
);
2503 /* Read R from the given target format. Read the words of the result
2504 in target word order in BUF. There are always 32 bits in each
2505 long, no matter the size of the host long. */
2508 real_from_target_fmt (r
, buf
, fmt
)
2511 const struct real_format
*fmt
;
2513 (*fmt
->decode
) (fmt
, r
, buf
);
2516 /* Similar, but look up the format from MODE. */
2519 real_from_target (r
, buf
, mode
)
2522 enum machine_mode mode
;
2524 const struct real_format
*fmt
;
2526 fmt
= real_format_for_mode
[mode
- QFmode
];
2530 (*fmt
->decode
) (fmt
, r
, buf
);
2533 /* Return the number of bits in the significand for MODE. */
2534 /* ??? Legacy. Should get access to real_format directly. */
2537 significand_size (mode
)
2538 enum machine_mode mode
;
2540 const struct real_format
*fmt
;
2542 fmt
= real_format_for_mode
[mode
- QFmode
];
2546 return fmt
->p
* fmt
->log2_b
;
2549 /* Return a hash value for the given real value. */
2550 /* ??? The "unsigned int" return value is intended to be hashval_t,
2551 but I didn't want to pull hashtab.h into real.h. */
2555 const REAL_VALUE_TYPE
*r
;
2560 h
= r
->class | (r
->sign
<< 2);
2572 if (sizeof(unsigned long) > sizeof(unsigned int))
2573 for (i
= 0; i
< SIGSZ
; ++i
)
2575 unsigned long s
= r
->sig
[i
];
2576 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2579 for (i
= 0; i
< SIGSZ
; ++i
)
2590 /* IEEE single-precision format. */
2592 static void encode_ieee_single
PARAMS ((const struct real_format
*fmt
,
2593 long *, const REAL_VALUE_TYPE
*));
2594 static void decode_ieee_single
PARAMS ((const struct real_format
*,
2595 REAL_VALUE_TYPE
*, const long *));
2598 encode_ieee_single (fmt
, buf
, r
)
2599 const struct real_format
*fmt
;
2601 const REAL_VALUE_TYPE
*r
;
2603 unsigned long image
, sig
, exp
;
2604 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2606 image
= r
->sign
<< 31;
2607 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2618 image
|= 0x7fffffff;
2626 if (!fmt
->qnan_msb_set
)
2627 image
^= 1 << 23 | 1 << 22;
2630 image
|= 0x7fffffff;
2634 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2635 whereas the intermediate representation is 0.F x 2**exp.
2636 Which means we're off by one. */
2640 exp
= r
->exp
+ 127 - 1;
2653 decode_ieee_single (fmt
, r
, buf
)
2654 const struct real_format
*fmt
;
2658 unsigned long image
= buf
[0] & 0xffffffff;
2659 bool sign
= (image
>> 31) & 1;
2660 int exp
= (image
>> 23) & 0xff;
2662 memset (r
, 0, sizeof (*r
));
2663 image
<<= HOST_BITS_PER_LONG
- 24;
2668 if (image
&& fmt
->has_denorm
)
2670 r
->class = rvc_normal
;
2673 r
->sig
[SIGSZ
-1] = image
<< 1;
2676 else if (fmt
->has_signed_zero
)
2679 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2685 if (!fmt
->qnan_msb_set
)
2686 image
^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
2687 r
->sig
[SIGSZ
-1] = image
;
2697 r
->class = rvc_normal
;
2699 r
->exp
= exp
- 127 + 1;
2700 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2704 const struct real_format ieee_single_format
=
2721 /* IEEE double-precision format. */
2723 static void encode_ieee_double
PARAMS ((const struct real_format
*fmt
,
2724 long *, const REAL_VALUE_TYPE
*));
2725 static void decode_ieee_double
PARAMS ((const struct real_format
*,
2726 REAL_VALUE_TYPE
*, const long *));
2729 encode_ieee_double (fmt
, buf
, r
)
2730 const struct real_format
*fmt
;
2732 const REAL_VALUE_TYPE
*r
;
2734 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
2735 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2737 image_hi
= r
->sign
<< 31;
2740 if (HOST_BITS_PER_LONG
== 64)
2742 sig_hi
= r
->sig
[SIGSZ
-1];
2743 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
2744 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
2748 sig_hi
= r
->sig
[SIGSZ
-1];
2749 sig_lo
= r
->sig
[SIGSZ
-2];
2750 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
2751 sig_hi
= (sig_hi
>> 11) & 0xfffff;
2761 image_hi
|= 2047 << 20;
2764 image_hi
|= 0x7fffffff;
2765 image_lo
= 0xffffffff;
2772 image_hi
|= 2047 << 20;
2774 if (!fmt
->qnan_msb_set
)
2775 image_hi
^= 1 << 19 | 1 << 18;
2780 image_hi
|= 0x7fffffff;
2781 image_lo
= 0xffffffff;
2786 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2787 whereas the intermediate representation is 0.F x 2**exp.
2788 Which means we're off by one. */
2792 exp
= r
->exp
+ 1023 - 1;
2793 image_hi
|= exp
<< 20;
2802 if (FLOAT_WORDS_BIG_ENDIAN
)
2803 buf
[0] = image_hi
, buf
[1] = image_lo
;
2805 buf
[0] = image_lo
, buf
[1] = image_hi
;
2809 decode_ieee_double (fmt
, r
, buf
)
2810 const struct real_format
*fmt
;
2814 unsigned long image_hi
, image_lo
;
2818 if (FLOAT_WORDS_BIG_ENDIAN
)
2819 image_hi
= buf
[0], image_lo
= buf
[1];
2821 image_lo
= buf
[0], image_hi
= buf
[1];
2822 image_lo
&= 0xffffffff;
2823 image_hi
&= 0xffffffff;
2825 sign
= (image_hi
>> 31) & 1;
2826 exp
= (image_hi
>> 20) & 0x7ff;
2828 memset (r
, 0, sizeof (*r
));
2830 image_hi
<<= 32 - 21;
2831 image_hi
|= image_lo
>> 21;
2832 image_hi
&= 0x7fffffff;
2833 image_lo
<<= 32 - 21;
2837 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
2839 r
->class = rvc_normal
;
2842 if (HOST_BITS_PER_LONG
== 32)
2844 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
2846 r
->sig
[SIGSZ
-1] = image_hi
;
2847 r
->sig
[SIGSZ
-2] = image_lo
;
2851 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
2852 r
->sig
[SIGSZ
-1] = image_hi
;
2856 else if (fmt
->has_signed_zero
)
2859 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
2861 if (image_hi
|| image_lo
)
2865 if (HOST_BITS_PER_LONG
== 32)
2867 r
->sig
[SIGSZ
-1] = image_hi
;
2868 r
->sig
[SIGSZ
-2] = image_lo
;
2871 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
2873 if (!fmt
->qnan_msb_set
)
2874 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
2884 r
->class = rvc_normal
;
2886 r
->exp
= exp
- 1023 + 1;
2887 if (HOST_BITS_PER_LONG
== 32)
2889 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
2890 r
->sig
[SIGSZ
-2] = image_lo
;
2893 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
2897 const struct real_format ieee_double_format
=
2914 /* IEEE extended double precision format. This comes in three
2915 flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2918 static void encode_ieee_extended
PARAMS ((const struct real_format
*fmt
,
2919 long *, const REAL_VALUE_TYPE
*));
2920 static void decode_ieee_extended
PARAMS ((const struct real_format
*,
2921 REAL_VALUE_TYPE
*, const long *));
2923 static void encode_ieee_extended_128
PARAMS ((const struct real_format
*fmt
,
2925 const REAL_VALUE_TYPE
*));
2926 static void decode_ieee_extended_128
PARAMS ((const struct real_format
*,
2931 encode_ieee_extended (fmt
, buf
, r
)
2932 const struct real_format
*fmt
;
2934 const REAL_VALUE_TYPE
*r
;
2936 unsigned long image_hi
, sig_hi
, sig_lo
;
2937 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2939 image_hi
= r
->sign
<< 15;
2940 sig_hi
= sig_lo
= 0;
2952 /* Intel requires the explicit integer bit to be set, otherwise
2953 it considers the value a "pseudo-infinity". Motorola docs
2954 say it doesn't care. */
2955 sig_hi
= 0x80000000;
2960 sig_lo
= sig_hi
= 0xffffffff;
2968 if (HOST_BITS_PER_LONG
== 32)
2970 sig_hi
= r
->sig
[SIGSZ
-1];
2971 sig_lo
= r
->sig
[SIGSZ
-2];
2975 sig_lo
= r
->sig
[SIGSZ
-1];
2976 sig_hi
= sig_lo
>> 31 >> 1;
2977 sig_lo
&= 0xffffffff;
2979 if (!fmt
->qnan_msb_set
)
2980 sig_hi
^= 1 << 30 | 1 << 29;
2982 /* Intel requires the explicit integer bit to be set, otherwise
2983 it considers the value a "pseudo-nan". Motorola docs say it
2985 sig_hi
|= 0x80000000;
2990 sig_lo
= sig_hi
= 0xffffffff;
2998 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2999 whereas the intermediate representation is 0.F x 2**exp.
3000 Which means we're off by one.
3002 Except for Motorola, which consider exp=0 and explicit
3003 integer bit set to continue to be normalized. In theory
3004 this descrepency has been taken care of by the difference
3005 in fmt->emin in round_for_format. */
3017 if (HOST_BITS_PER_LONG
== 32)
3019 sig_hi
= r
->sig
[SIGSZ
-1];
3020 sig_lo
= r
->sig
[SIGSZ
-2];
3024 sig_lo
= r
->sig
[SIGSZ
-1];
3025 sig_hi
= sig_lo
>> 31 >> 1;
3026 sig_lo
&= 0xffffffff;
3035 if (FLOAT_WORDS_BIG_ENDIAN
)
3036 buf
[0] = image_hi
<< 16, buf
[1] = sig_hi
, buf
[2] = sig_lo
;
3038 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
3042 encode_ieee_extended_128 (fmt
, buf
, r
)
3043 const struct real_format
*fmt
;
3045 const REAL_VALUE_TYPE
*r
;
3047 buf
[3 * !FLOAT_WORDS_BIG_ENDIAN
] = 0;
3048 encode_ieee_extended (fmt
, buf
+!!FLOAT_WORDS_BIG_ENDIAN
, r
);
3052 decode_ieee_extended (fmt
, r
, buf
)
3053 const struct real_format
*fmt
;
3057 unsigned long image_hi
, sig_hi
, sig_lo
;
3061 if (FLOAT_WORDS_BIG_ENDIAN
)
3062 image_hi
= buf
[0] >> 16, sig_hi
= buf
[1], sig_lo
= buf
[2];
3064 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
3065 sig_lo
&= 0xffffffff;
3066 sig_hi
&= 0xffffffff;
3067 image_hi
&= 0xffffffff;
3069 sign
= (image_hi
>> 15) & 1;
3070 exp
= image_hi
& 0x7fff;
3072 memset (r
, 0, sizeof (*r
));
3076 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
3078 r
->class = rvc_normal
;
3081 /* When the IEEE format contains a hidden bit, we know that
3082 it's zero at this point, and so shift up the significand
3083 and decrease the exponent to match. In this case, Motorola
3084 defines the explicit integer bit to be valid, so we don't
3085 know whether the msb is set or not. */
3087 if (HOST_BITS_PER_LONG
== 32)
3089 r
->sig
[SIGSZ
-1] = sig_hi
;
3090 r
->sig
[SIGSZ
-2] = sig_lo
;
3093 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3097 else if (fmt
->has_signed_zero
)
3100 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3102 /* See above re "pseudo-infinities" and "pseudo-nans".
3103 Short summary is that the MSB will likely always be
3104 set, and that we don't care about it. */
3105 sig_hi
&= 0x7fffffff;
3107 if (sig_hi
|| sig_lo
)
3111 if (HOST_BITS_PER_LONG
== 32)
3113 r
->sig
[SIGSZ
-1] = sig_hi
;
3114 r
->sig
[SIGSZ
-2] = sig_lo
;
3117 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3119 if (!fmt
->qnan_msb_set
)
3120 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
3130 r
->class = rvc_normal
;
3132 r
->exp
= exp
- 16383 + 1;
3133 if (HOST_BITS_PER_LONG
== 32)
3135 r
->sig
[SIGSZ
-1] = sig_hi
;
3136 r
->sig
[SIGSZ
-2] = sig_lo
;
3139 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
3144 decode_ieee_extended_128 (fmt
, r
, buf
)
3145 const struct real_format
*fmt
;
3149 decode_ieee_extended (fmt
, r
, buf
+!!FLOAT_WORDS_BIG_ENDIAN
);
3152 const struct real_format ieee_extended_motorola_format
=
3154 encode_ieee_extended
,
3155 decode_ieee_extended
,
3168 const struct real_format ieee_extended_intel_96_format
=
3170 encode_ieee_extended
,
3171 decode_ieee_extended
,
3184 const struct real_format ieee_extended_intel_128_format
=
3186 encode_ieee_extended_128
,
3187 decode_ieee_extended_128
,
3201 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3202 numbers whose sum is equal to the extended precision value. The number
3203 with greater magnitude is first. This format has the same magnitude
3204 range as an IEEE double precision value, but effectively 106 bits of
3205 significand precision. Infinity and NaN are represented by their IEEE
3206 double precision value stored in the first number, the second number is
3207 ignored. Zeroes, Infinities, and NaNs are set in both doubles
3208 due to precedent. */
3210 static void encode_ibm_extended
PARAMS ((const struct real_format
*fmt
,
3211 long *, const REAL_VALUE_TYPE
*));
3212 static void decode_ibm_extended
PARAMS ((const struct real_format
*,
3213 REAL_VALUE_TYPE
*, const long *));
3216 encode_ibm_extended (fmt
, buf
, r
)
3217 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3219 const REAL_VALUE_TYPE
*r
;
3221 REAL_VALUE_TYPE u
, v
;
3226 /* Both doubles have sign bit set. */
3227 buf
[0] = FLOAT_WORDS_BIG_ENDIAN
? r
->sign
<< 31 : 0;
3228 buf
[1] = FLOAT_WORDS_BIG_ENDIAN
? 0 : r
->sign
<< 31;
3235 /* Both doubles set to Inf / NaN. */
3236 encode_ieee_double (&ieee_double_format
, &buf
[0], r
);
3242 /* u = IEEE double precision portion of significand. */
3244 clear_significand_below (&u
, SIGNIFICAND_BITS
- 53);
3246 /* v = remainder containing additional 53 bits of significand. */
3247 do_add (&v
, r
, &u
, 1);
3249 encode_ieee_double (&ieee_double_format
, &buf
[0], &u
);
3250 encode_ieee_double (&ieee_double_format
, &buf
[2], &v
);
3259 decode_ibm_extended (fmt
, r
, buf
)
3260 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3264 REAL_VALUE_TYPE u
, v
;
3266 decode_ieee_double (&ieee_double_format
, &u
, &buf
[0]);
3268 if (u
.class != rvc_zero
&& u
.class != rvc_inf
&& u
.class != rvc_nan
)
3270 decode_ieee_double (&ieee_double_format
, &v
, &buf
[2]);
3271 do_add (r
, &u
, &v
, 0);
3277 const struct real_format ibm_extended_format
=
3279 encode_ibm_extended
,
3280 decode_ibm_extended
,
3294 /* IEEE quad precision format. */
3296 static void encode_ieee_quad
PARAMS ((const struct real_format
*fmt
,
3297 long *, const REAL_VALUE_TYPE
*));
3298 static void decode_ieee_quad
PARAMS ((const struct real_format
*,
3299 REAL_VALUE_TYPE
*, const long *));
3302 encode_ieee_quad (fmt
, buf
, r
)
3303 const struct real_format
*fmt
;
3305 const REAL_VALUE_TYPE
*r
;
3307 unsigned long image3
, image2
, image1
, image0
, exp
;
3308 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3311 image3
= r
->sign
<< 31;
3316 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3325 image3
|= 32767 << 16;
3328 image3
|= 0x7fffffff;
3329 image2
= 0xffffffff;
3330 image1
= 0xffffffff;
3331 image0
= 0xffffffff;
3338 image3
|= 32767 << 16;
3340 if (HOST_BITS_PER_LONG
== 32)
3345 image3
|= u
.sig
[3] & 0xffff;
3350 image1
= image0
>> 31 >> 1;
3352 image3
|= (image2
>> 31 >> 1) & 0xffff;
3353 image0
&= 0xffffffff;
3354 image2
&= 0xffffffff;
3357 if (!fmt
->qnan_msb_set
)
3358 image3
^= 1 << 15 | 1 << 14;
3362 image3
|= 0x7fffffff;
3363 image2
= 0xffffffff;
3364 image1
= 0xffffffff;
3365 image0
= 0xffffffff;
3370 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3371 whereas the intermediate representation is 0.F x 2**exp.
3372 Which means we're off by one. */
3376 exp
= r
->exp
+ 16383 - 1;
3377 image3
|= exp
<< 16;
3379 if (HOST_BITS_PER_LONG
== 32)
3384 image3
|= u
.sig
[3] & 0xffff;
3389 image1
= image0
>> 31 >> 1;
3391 image3
|= (image2
>> 31 >> 1) & 0xffff;
3392 image0
&= 0xffffffff;
3393 image2
&= 0xffffffff;
3401 if (FLOAT_WORDS_BIG_ENDIAN
)
3418 decode_ieee_quad (fmt
, r
, buf
)
3419 const struct real_format
*fmt
;
3423 unsigned long image3
, image2
, image1
, image0
;
3427 if (FLOAT_WORDS_BIG_ENDIAN
)
3441 image0
&= 0xffffffff;
3442 image1
&= 0xffffffff;
3443 image2
&= 0xffffffff;
3445 sign
= (image3
>> 31) & 1;
3446 exp
= (image3
>> 16) & 0x7fff;
3449 memset (r
, 0, sizeof (*r
));
3453 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
3455 r
->class = rvc_normal
;
3458 r
->exp
= -16382 + (SIGNIFICAND_BITS
- 112);
3459 if (HOST_BITS_PER_LONG
== 32)
3468 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3469 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3474 else if (fmt
->has_signed_zero
)
3477 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3479 if (image3
| image2
| image1
| image0
)
3484 if (HOST_BITS_PER_LONG
== 32)
3493 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3494 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3496 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3498 if (!fmt
->qnan_msb_set
)
3499 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
3509 r
->class = rvc_normal
;
3511 r
->exp
= exp
- 16383 + 1;
3513 if (HOST_BITS_PER_LONG
== 32)
3522 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3523 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3525 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3526 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3530 const struct real_format ieee_quad_format
=
3546 /* Descriptions of VAX floating point formats can be found beginning at
3548 http://www.openvms.compaq.com:8000/73final/4515/4515pro_013.html#f_floating_point_format
3550 The thing to remember is that they're almost IEEE, except for word
3551 order, exponent bias, and the lack of infinities, nans, and denormals.
3553 We don't implement the H_floating format here, simply because neither
3554 the VAX or Alpha ports use it. */
3556 static void encode_vax_f
PARAMS ((const struct real_format
*fmt
,
3557 long *, const REAL_VALUE_TYPE
*));
3558 static void decode_vax_f
PARAMS ((const struct real_format
*,
3559 REAL_VALUE_TYPE
*, const long *));
3560 static void encode_vax_d
PARAMS ((const struct real_format
*fmt
,
3561 long *, const REAL_VALUE_TYPE
*));
3562 static void decode_vax_d
PARAMS ((const struct real_format
*,
3563 REAL_VALUE_TYPE
*, const long *));
3564 static void encode_vax_g
PARAMS ((const struct real_format
*fmt
,
3565 long *, const REAL_VALUE_TYPE
*));
3566 static void decode_vax_g
PARAMS ((const struct real_format
*,
3567 REAL_VALUE_TYPE
*, const long *));
3570 encode_vax_f (fmt
, buf
, r
)
3571 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3573 const REAL_VALUE_TYPE
*r
;
3575 unsigned long sign
, exp
, sig
, image
;
3577 sign
= r
->sign
<< 15;
3587 image
= 0xffff7fff | sign
;
3591 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
3594 image
= (sig
<< 16) & 0xffff0000;
3608 decode_vax_f (fmt
, r
, buf
)
3609 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3613 unsigned long image
= buf
[0] & 0xffffffff;
3614 int exp
= (image
>> 7) & 0xff;
3616 memset (r
, 0, sizeof (*r
));
3620 r
->class = rvc_normal
;
3621 r
->sign
= (image
>> 15) & 1;
3624 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
3625 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
3630 encode_vax_d (fmt
, buf
, r
)
3631 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3633 const REAL_VALUE_TYPE
*r
;
3635 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3640 image0
= image1
= 0;
3645 image0
= 0xffff7fff | sign
;
3646 image1
= 0xffffffff;
3650 /* Extract the significand into straight hi:lo. */
3651 if (HOST_BITS_PER_LONG
== 64)
3653 image0
= r
->sig
[SIGSZ
-1];
3654 image1
= (image0
>> (64 - 56)) & 0xffffffff;
3655 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
3659 image0
= r
->sig
[SIGSZ
-1];
3660 image1
= r
->sig
[SIGSZ
-2];
3661 image1
= (image0
<< 24) | (image1
>> 8);
3662 image0
= (image0
>> 8) & 0xffffff;
3665 /* Rearrange the half-words of the significand to match the
3667 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
3668 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
3670 /* Add the sign and exponent. */
3672 image0
|= (r
->exp
+ 128) << 7;
3679 if (FLOAT_WORDS_BIG_ENDIAN
)
3680 buf
[0] = image1
, buf
[1] = image0
;
3682 buf
[0] = image0
, buf
[1] = image1
;
3686 decode_vax_d (fmt
, r
, buf
)
3687 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3691 unsigned long image0
, image1
;
3694 if (FLOAT_WORDS_BIG_ENDIAN
)
3695 image1
= buf
[0], image0
= buf
[1];
3697 image0
= buf
[0], image1
= buf
[1];
3698 image0
&= 0xffffffff;
3699 image1
&= 0xffffffff;
3701 exp
= (image0
>> 7) & 0x7f;
3703 memset (r
, 0, sizeof (*r
));
3707 r
->class = rvc_normal
;
3708 r
->sign
= (image0
>> 15) & 1;
3711 /* Rearrange the half-words of the external format into
3712 proper ascending order. */
3713 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
3714 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
3716 if (HOST_BITS_PER_LONG
== 64)
3718 image0
= (image0
<< 31 << 1) | image1
;
3721 r
->sig
[SIGSZ
-1] = image0
;
3725 r
->sig
[SIGSZ
-1] = image0
;
3726 r
->sig
[SIGSZ
-2] = image1
;
3727 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
3728 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3734 encode_vax_g (fmt
, buf
, r
)
3735 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3737 const REAL_VALUE_TYPE
*r
;
3739 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3744 image0
= image1
= 0;
3749 image0
= 0xffff7fff | sign
;
3750 image1
= 0xffffffff;
3754 /* Extract the significand into straight hi:lo. */
3755 if (HOST_BITS_PER_LONG
== 64)
3757 image0
= r
->sig
[SIGSZ
-1];
3758 image1
= (image0
>> (64 - 53)) & 0xffffffff;
3759 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
3763 image0
= r
->sig
[SIGSZ
-1];
3764 image1
= r
->sig
[SIGSZ
-2];
3765 image1
= (image0
<< 21) | (image1
>> 11);
3766 image0
= (image0
>> 11) & 0xfffff;
3769 /* Rearrange the half-words of the significand to match the
3771 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
3772 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
3774 /* Add the sign and exponent. */
3776 image0
|= (r
->exp
+ 1024) << 4;
3783 if (FLOAT_WORDS_BIG_ENDIAN
)
3784 buf
[0] = image1
, buf
[1] = image0
;
3786 buf
[0] = image0
, buf
[1] = image1
;
3790 decode_vax_g (fmt
, r
, buf
)
3791 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3795 unsigned long image0
, image1
;
3798 if (FLOAT_WORDS_BIG_ENDIAN
)
3799 image1
= buf
[0], image0
= buf
[1];
3801 image0
= buf
[0], image1
= buf
[1];
3802 image0
&= 0xffffffff;
3803 image1
&= 0xffffffff;
3805 exp
= (image0
>> 4) & 0x7ff;
3807 memset (r
, 0, sizeof (*r
));
3811 r
->class = rvc_normal
;
3812 r
->sign
= (image0
>> 15) & 1;
3813 r
->exp
= exp
- 1024;
3815 /* Rearrange the half-words of the external format into
3816 proper ascending order. */
3817 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
3818 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
3820 if (HOST_BITS_PER_LONG
== 64)
3822 image0
= (image0
<< 31 << 1) | image1
;
3825 r
->sig
[SIGSZ
-1] = image0
;
3829 r
->sig
[SIGSZ
-1] = image0
;
3830 r
->sig
[SIGSZ
-2] = image1
;
3831 lshift_significand (r
, r
, 64 - 53);
3832 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3837 const struct real_format vax_f_format
=
3853 const struct real_format vax_d_format
=
3869 const struct real_format vax_g_format
=
3885 /* A good reference for these can be found in chapter 9 of
3886 "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
3887 An on-line version can be found here:
3889 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3892 static void encode_i370_single
PARAMS ((const struct real_format
*fmt
,
3893 long *, const REAL_VALUE_TYPE
*));
3894 static void decode_i370_single
PARAMS ((const struct real_format
*,
3895 REAL_VALUE_TYPE
*, const long *));
3896 static void encode_i370_double
PARAMS ((const struct real_format
*fmt
,
3897 long *, const REAL_VALUE_TYPE
*));
3898 static void decode_i370_double
PARAMS ((const struct real_format
*,
3899 REAL_VALUE_TYPE
*, const long *));
3902 encode_i370_single (fmt
, buf
, r
)
3903 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3905 const REAL_VALUE_TYPE
*r
;
3907 unsigned long sign
, exp
, sig
, image
;
3909 sign
= r
->sign
<< 31;
3919 image
= 0x7fffffff | sign
;
3923 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0xffffff;
3924 exp
= ((r
->exp
/ 4) + 64) << 24;
3925 image
= sign
| exp
| sig
;
3936 decode_i370_single (fmt
, r
, buf
)
3937 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3941 unsigned long sign
, sig
, image
= buf
[0];
3944 sign
= (image
>> 31) & 1;
3945 exp
= (image
>> 24) & 0x7f;
3946 sig
= image
& 0xffffff;
3948 memset (r
, 0, sizeof (*r
));
3952 r
->class = rvc_normal
;
3954 r
->exp
= (exp
- 64) * 4;
3955 r
->sig
[SIGSZ
-1] = sig
<< (HOST_BITS_PER_LONG
- 24);
3961 encode_i370_double (fmt
, buf
, r
)
3962 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3964 const REAL_VALUE_TYPE
*r
;
3966 unsigned long sign
, exp
, image_hi
, image_lo
;
3968 sign
= r
->sign
<< 31;
3973 image_hi
= image_lo
= 0;
3978 image_hi
= 0x7fffffff | sign
;
3979 image_lo
= 0xffffffff;
3983 if (HOST_BITS_PER_LONG
== 64)
3985 image_hi
= r
->sig
[SIGSZ
-1];
3986 image_lo
= (image_hi
>> (64 - 56)) & 0xffffffff;
3987 image_hi
= (image_hi
>> (64 - 56 + 1) >> 31) & 0xffffff;
3991 image_hi
= r
->sig
[SIGSZ
-1];
3992 image_lo
= r
->sig
[SIGSZ
-2];
3993 image_lo
= (image_lo
>> 8) | (image_hi
<< 24);
3997 exp
= ((r
->exp
/ 4) + 64) << 24;
3998 image_hi
|= sign
| exp
;
4005 if (FLOAT_WORDS_BIG_ENDIAN
)
4006 buf
[0] = image_hi
, buf
[1] = image_lo
;
4008 buf
[0] = image_lo
, buf
[1] = image_hi
;
4012 decode_i370_double (fmt
, r
, buf
)
4013 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4017 unsigned long sign
, image_hi
, image_lo
;
4020 if (FLOAT_WORDS_BIG_ENDIAN
)
4021 image_hi
= buf
[0], image_lo
= buf
[1];
4023 image_lo
= buf
[0], image_hi
= buf
[1];
4025 sign
= (image_hi
>> 31) & 1;
4026 exp
= (image_hi
>> 24) & 0x7f;
4027 image_hi
&= 0xffffff;
4028 image_lo
&= 0xffffffff;
4030 memset (r
, 0, sizeof (*r
));
4032 if (exp
|| image_hi
|| image_lo
)
4034 r
->class = rvc_normal
;
4036 r
->exp
= (exp
- 64) * 4 + (SIGNIFICAND_BITS
- 56);
4038 if (HOST_BITS_PER_LONG
== 32)
4040 r
->sig
[0] = image_lo
;
4041 r
->sig
[1] = image_hi
;
4044 r
->sig
[0] = image_lo
| (image_hi
<< 31 << 1);
4050 const struct real_format i370_single_format
=
4061 false, /* ??? The encoding does allow for "unnormals". */
4062 false, /* ??? The encoding does allow for "unnormals". */
4066 const struct real_format i370_double_format
=
4077 false, /* ??? The encoding does allow for "unnormals". */
4078 false, /* ??? The encoding does allow for "unnormals". */
4082 /* The "twos-complement" c4x format is officially defined as
4086 This is rather misleading. One must remember that F is signed.
4087 A better description would be
4089 x = -1**s * ((s + 1 + .f) * 2**e
4091 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4092 that's -1 * (1+1+(-.5)) == -1.5. I think.
4094 The constructions here are taken from Tables 5-1 and 5-2 of the
4095 TMS320C4x User's Guide wherein step-by-step instructions for
4096 conversion from IEEE are presented. That's close enough to our
4097 internal representation so as to make things easy.
4099 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
4101 static void encode_c4x_single
PARAMS ((const struct real_format
*fmt
,
4102 long *, const REAL_VALUE_TYPE
*));
4103 static void decode_c4x_single
PARAMS ((const struct real_format
*,
4104 REAL_VALUE_TYPE
*, const long *));
4105 static void encode_c4x_extended
PARAMS ((const struct real_format
*fmt
,
4106 long *, const REAL_VALUE_TYPE
*));
4107 static void decode_c4x_extended
PARAMS ((const struct real_format
*,
4108 REAL_VALUE_TYPE
*, const long *));
4111 encode_c4x_single (fmt
, buf
, r
)
4112 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4114 const REAL_VALUE_TYPE
*r
;
4116 unsigned long image
, exp
, sig
;
4128 sig
= 0x800000 - r
->sign
;
4133 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
4148 image
= ((exp
& 0xff) << 24) | (sig
& 0xffffff);
4153 decode_c4x_single (fmt
, r
, buf
)
4154 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4158 unsigned long image
= buf
[0];
4162 exp
= (((image
>> 24) & 0xff) ^ 0x80) - 0x80;
4163 sf
= ((image
& 0xffffff) ^ 0x800000) - 0x800000;
4165 memset (r
, 0, sizeof (*r
));
4169 r
->class = rvc_normal
;
4171 sig
= sf
& 0x7fffff;
4180 sig
= (sig
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
4183 r
->sig
[SIGSZ
-1] = sig
;
4188 encode_c4x_extended (fmt
, buf
, r
)
4189 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4191 const REAL_VALUE_TYPE
*r
;
4193 unsigned long exp
, sig
;
4205 sig
= 0x80000000 - r
->sign
;
4211 sig
= r
->sig
[SIGSZ
-1];
4212 if (HOST_BITS_PER_LONG
== 64)
4213 sig
= sig
>> 1 >> 31;
4230 exp
= (exp
& 0xff) << 24;
4233 if (FLOAT_WORDS_BIG_ENDIAN
)
4234 buf
[0] = exp
, buf
[1] = sig
;
4236 buf
[0] = sig
, buf
[0] = exp
;
4240 decode_c4x_extended (fmt
, r
, buf
)
4241 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4248 if (FLOAT_WORDS_BIG_ENDIAN
)
4249 exp
= buf
[0], sf
= buf
[1];
4251 sf
= buf
[0], exp
= buf
[1];
4253 exp
= (((exp
>> 24) & 0xff) & 0x80) - 0x80;
4254 sf
= ((sf
& 0xffffffff) ^ 0x80000000) - 0x80000000;
4256 memset (r
, 0, sizeof (*r
));
4260 r
->class = rvc_normal
;
4262 sig
= sf
& 0x7fffffff;
4271 if (HOST_BITS_PER_LONG
== 64)
4272 sig
= sig
<< 1 << 31;
4276 r
->sig
[SIGSZ
-1] = sig
;
4280 const struct real_format c4x_single_format
=
4296 const struct real_format c4x_extended_format
=
4298 encode_c4x_extended
,
4299 decode_c4x_extended
,
4312 /* Set up default mode to format mapping for IEEE. Everyone else has
4313 to set these values in OVERRIDE_OPTIONS. */
4315 const struct real_format
*real_format_for_mode
[TFmode
- QFmode
+ 1] =
4320 &ieee_single_format
, /* SFmode */
4321 &ieee_double_format
, /* DFmode */
4323 /* We explicitly don't handle XFmode. There are two formats,
4324 pretty much equally common. Choose one in OVERRIDE_OPTIONS. */
4326 &ieee_quad_format
/* TFmode */