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
57 target significand is 113 bits; we store 128. The smallest
58 denormal number fits in 17 exponent bits; we store 29.
60 Target floating point models that use base 16 instead of base 2
61 (i.e. IBM 370), are handled during round_for_format, in which we
62 canonicalize the exponent to be a multiple of 4 (log2(16)), and
63 adjust the significand to match. */
66 /* Enumerate the special cases of numbers that we encounter. */
67 enum real_value_class
{
74 /* Used to classify two numbers simultaneously. */
75 #define CLASS2(A, B) ((A) << 2 | (B))
77 /* An expanded form of the represented number. */
79 #define SIGNIFICAND_BITS 128
80 #define EXP_BITS (32 - 3)
81 #define MAX_EXP ((1 << (EXP_BITS - 1)) - 1)
82 #define SIGSZ (SIGNIFICAND_BITS / HOST_BITS_PER_LONG)
83 #define SIG_MSB ((unsigned long)1 << (HOST_BITS_PER_LONG - 1))
85 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
86 #error "Some constant folding done by hand to avoid shift count warnings"
91 enum real_value_class
class : 2;
92 unsigned int sign
: 1;
94 unsigned long sig
[SIGSZ
];
97 /* Describes the properties of the specific target format in use. */
100 /* Move to and from the target bytes. */
101 void (*encode
) (const struct real_format
*, long *,
102 const struct real_value
*);
103 void (*decode
) (const struct real_format
*, struct real_value
*,
106 /* The radix of the exponent and digits of the significand. */
112 /* Size of the significand in digits of radix B. */
115 /* The minimum negative integer, x, such that b**(x-1) is normalized. */
118 /* The maximum integer, x, such that b**(x-1) is representable. */
121 /* Properties of the format. */
125 bool has_signed_zero
;
130 static const struct real_format
*fmt_for_mode
[TFmode
- QFmode
+ 1];
133 static void get_zero
PARAMS ((struct real_value
*, int));
134 static void get_canonical_qnan
PARAMS ((struct real_value
*, int));
135 static void get_canonical_snan
PARAMS ((struct real_value
*, int));
136 static void get_inf
PARAMS ((struct real_value
*, int));
137 static void sticky_rshift_significand
PARAMS ((struct real_value
*,
138 const struct real_value
*,
140 static void rshift_significand
PARAMS ((struct real_value
*,
141 const struct real_value
*,
143 static void lshift_significand
PARAMS ((struct real_value
*,
144 const struct real_value
*,
146 static void lshift_significand_1
PARAMS ((struct real_value
*,
147 const struct real_value
*));
148 static bool add_significands
PARAMS ((struct real_value
*r
,
149 const struct real_value
*,
150 const struct real_value
*));
151 static bool sub_significands
PARAMS ((struct real_value
*,
152 const struct real_value
*,
153 const struct real_value
*));
154 static void neg_significand
PARAMS ((struct real_value
*,
155 const struct real_value
*));
156 static int cmp_significands
PARAMS ((const struct real_value
*,
157 const struct real_value
*));
158 static void set_significand_bit
PARAMS ((struct real_value
*, unsigned int));
159 static void clear_significand_bit
PARAMS ((struct real_value
*, unsigned int));
160 static bool test_significand_bit
PARAMS ((struct real_value
*, unsigned int));
161 static void clear_significand_below
PARAMS ((struct real_value
*,
163 static bool div_significands
PARAMS ((struct real_value
*,
164 const struct real_value
*,
165 const struct real_value
*));
166 static void normalize
PARAMS ((struct real_value
*));
168 static void do_add
PARAMS ((struct real_value
*, const struct real_value
*,
169 const struct real_value
*, int));
170 static void do_multiply
PARAMS ((struct real_value
*,
171 const struct real_value
*,
172 const struct real_value
*));
173 static void do_divide
PARAMS ((struct real_value
*, const struct real_value
*,
174 const struct real_value
*));
175 static int do_compare
PARAMS ((const struct real_value
*,
176 const struct real_value
*, int));
177 static void do_fix_trunc
PARAMS ((struct real_value
*,
178 const struct real_value
*));
180 static const struct real_value
* ten_to_ptwo
PARAMS ((int));
181 static const struct real_value
* real_digit
PARAMS ((int));
183 static void round_for_format
PARAMS ((const struct real_format
*,
184 struct real_value
*));
186 /* Initialize R with a positive zero. */
190 struct real_value
*r
;
193 memset (r
, 0, sizeof (*r
));
197 /* Initialize R with the canonical quiet NaN. */
200 get_canonical_qnan (r
, sign
)
201 struct real_value
*r
;
204 memset (r
, 0, sizeof (*r
));
207 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 1;
211 get_canonical_snan (r
, sign
)
212 struct real_value
*r
;
215 memset (r
, 0, sizeof (*r
));
218 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 2;
223 struct real_value
*r
;
226 memset (r
, 0, sizeof (*r
));
232 /* Right-shift the significand of A by N bits; put the result in the
233 significand of R. If any one bits are shifted out, set the least
234 significant bit of R. */
237 sticky_rshift_significand (r
, a
, n
)
238 struct real_value
*r
;
239 const struct real_value
*a
;
242 unsigned long sticky
= 0;
243 unsigned int i
, ofs
= 0;
245 if (n
>= HOST_BITS_PER_LONG
)
247 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
249 n
-= ofs
* HOST_BITS_PER_LONG
;
254 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
255 for (i
= 0; i
< SIGSZ
; ++i
)
258 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
259 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
260 << (HOST_BITS_PER_LONG
- n
)));
265 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
266 r
->sig
[i
] = a
->sig
[ofs
+ i
];
267 for (; i
< SIGSZ
; ++i
)
271 r
->sig
[0] |= (sticky
!= 0);
274 /* Right-shift the significand of A by N bits; put the result in the
278 rshift_significand (r
, a
, n
)
279 struct real_value
*r
;
280 const struct real_value
*a
;
283 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
285 n
-= ofs
* HOST_BITS_PER_LONG
;
288 for (i
= 0; i
< SIGSZ
; ++i
)
291 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
292 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
293 << (HOST_BITS_PER_LONG
- n
)));
298 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
299 r
->sig
[i
] = a
->sig
[ofs
+ i
];
300 for (; i
< SIGSZ
; ++i
)
305 /* Left-shift the significand of A by N bits; put the result in the
309 lshift_significand (r
, a
, n
)
310 struct real_value
*r
;
311 const struct real_value
*a
;
314 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
316 n
-= ofs
* HOST_BITS_PER_LONG
;
319 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
320 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
321 for (; i
< SIGSZ
; ++i
)
322 r
->sig
[SIGSZ
-1-i
] = 0;
325 for (i
= 0; i
< SIGSZ
; ++i
)
328 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
329 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
330 >> (HOST_BITS_PER_LONG
- n
)));
334 /* Likewise, but N is specialized to 1. */
337 lshift_significand_1 (r
, a
)
338 struct real_value
*r
;
339 const struct real_value
*a
;
343 for (i
= SIGSZ
- 1; i
> 0; --i
)
344 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
345 r
->sig
[0] = a
->sig
[0] << 1;
348 /* Add the significands of A and B, placing the result in R. Return
349 true if there was carry out of the most significant word. */
352 add_significands (r
, a
, b
)
353 struct real_value
*r
;
354 const struct real_value
*a
, *b
;
359 for (i
= 0; i
< SIGSZ
; ++i
)
361 unsigned long ai
= a
->sig
[i
];
362 unsigned long ri
= ai
+ b
->sig
[i
];
378 /* Subtract the significands of A and B, placing the result in R.
379 Return true if there was carry out of the most significant word. */
382 sub_significands (r
, a
, b
)
383 struct real_value
*r
;
384 const struct real_value
*a
, *b
;
389 for (i
= 0; i
< SIGSZ
; ++i
)
391 unsigned long ai
= a
->sig
[i
];
392 unsigned long ri
= ai
- b
->sig
[i
];
408 /* Negate the significand A, placing the result in R. */
411 neg_significand (r
, a
)
412 struct real_value
*r
;
413 const struct real_value
*a
;
418 for (i
= 0; i
< SIGSZ
; ++i
)
420 unsigned long ri
, ai
= a
->sig
[i
];
439 /* Compare significands. Return tri-state vs zero. */
442 cmp_significands (a
, b
)
443 const struct real_value
*a
, *b
;
447 for (i
= SIGSZ
- 1; i
>= 0; --i
)
449 unsigned long ai
= a
->sig
[i
];
450 unsigned long bi
= b
->sig
[i
];
461 /* Set bit N of the significand of R. */
464 set_significand_bit (r
, n
)
465 struct real_value
*r
;
468 r
->sig
[n
/ HOST_BITS_PER_LONG
]
469 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
472 /* Clear bit N of the significand of R. */
475 clear_significand_bit (r
, n
)
476 struct real_value
*r
;
479 r
->sig
[n
/ HOST_BITS_PER_LONG
]
480 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
483 /* Test bit N of the significand of R. */
486 test_significand_bit (r
, n
)
487 struct real_value
*r
;
490 /* ??? Compiler bug here if we return this expression directly.
491 The conversion to bool strips the "&1" and we wind up testing
492 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
493 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
497 /* Clear bits 0..N-1 of the significand of R. */
500 clear_significand_below (r
, n
)
501 struct real_value
*r
;
504 int i
, w
= n
/ HOST_BITS_PER_LONG
;
506 for (i
= 0; i
< w
; ++i
)
509 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
512 /* Divide the significands of A and B, placing the result in R. Return
513 true if the division was inexact. */
516 div_significands (r
, a
, b
)
517 struct real_value
*r
;
518 const struct real_value
*a
, *b
;
521 int bit
= SIGNIFICAND_BITS
- 1;
526 memset (r
->sig
, 0, sizeof (r
->sig
));
531 if ((u
.sig
[SIGSZ
-1] & SIG_MSB
) == 0)
533 lshift_significand_1 (&u
, &u
);
535 if (cmp_significands (&u
, b
) >= 0)
537 sub_significands (&u
, &u
, b
);
538 set_significand_bit (r
, bit
);
543 /* We lose a bit here, and thus know the next quotient bit
545 lshift_significand_1 (&u
, &u
);
546 sub_significands (&u
, &u
, b
);
547 set_significand_bit (r
, bit
);
552 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
558 /* Adjust the exponent and significand of R such that the most
559 significant bit is set. We underflow to zero and overflow to
560 infinity here, without denormals. (The intermediate representation
561 exponent is large enough to handle target denormals normalized.) */
565 struct real_value
*r
;
570 /* Find the first word that is non-zero. */
571 for (i
= SIGSZ
- 1; i
>= 0; i
--)
573 shift
+= HOST_BITS_PER_LONG
;
577 /* Zero significand flushes to zero. */
585 /* Find the first bit that is non-zero. */
587 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
593 exp
= r
->exp
- shift
;
595 get_inf (r
, r
->sign
);
596 else if (exp
< -MAX_EXP
)
597 get_zero (r
, r
->sign
);
601 lshift_significand (r
, r
, shift
);
606 /* Return R = A + (SUBTRACT_P ? -B : B). */
609 do_add (r
, a
, b
, subtract_p
)
610 struct real_value
*r
;
611 const struct real_value
*a
, *b
;
617 /* Determine if we need to add or subtract. */
619 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
621 switch (CLASS2 (a
->class, b
->class))
623 case CLASS2 (rvc_zero
, rvc_zero
):
624 /* +-0 +/- +-0 = +0. */
628 case CLASS2 (rvc_zero
, rvc_normal
):
629 case CLASS2 (rvc_zero
, rvc_inf
):
630 case CLASS2 (rvc_zero
, rvc_nan
):
632 case CLASS2 (rvc_normal
, rvc_nan
):
633 case CLASS2 (rvc_inf
, rvc_nan
):
634 case CLASS2 (rvc_nan
, rvc_nan
):
635 /* ANY + NaN = NaN. */
636 case CLASS2 (rvc_normal
, rvc_inf
):
639 r
->sign
= sign
^ subtract_p
;
642 case CLASS2 (rvc_normal
, rvc_zero
):
643 case CLASS2 (rvc_inf
, rvc_zero
):
644 case CLASS2 (rvc_nan
, rvc_zero
):
646 case CLASS2 (rvc_nan
, rvc_normal
):
647 case CLASS2 (rvc_nan
, rvc_inf
):
648 /* NaN + ANY = NaN. */
649 case CLASS2 (rvc_inf
, rvc_normal
):
654 case CLASS2 (rvc_inf
, rvc_inf
):
656 /* Inf - Inf = NaN. */
657 get_canonical_qnan (r
, 0);
659 /* Inf + Inf = Inf. */
663 case CLASS2 (rvc_normal
, rvc_normal
):
670 /* Swap the arguments such that A has the larger exponent. */
671 dexp
= a
->exp
- b
->exp
;
674 const struct real_value
*t
;
681 /* If the exponents are not identical, we need to shift the
682 significand of B down. */
685 /* If the exponents are too far apart, the significands
686 do not overlap, which makes the subtraction a noop. */
687 if (dexp
>= SIGNIFICAND_BITS
)
694 sticky_rshift_significand (&t
, b
, dexp
);
700 if (sub_significands (r
, a
, b
))
702 /* We got a borrow out of the subtraction. That means that
703 A and B had the same exponent, and B had the larger
704 significand. We need to swap the sign and negate the
707 neg_significand (r
, r
);
712 if (add_significands (r
, a
, b
))
714 /* We got carry out of the addition. This means we need to
715 shift the significand back down one bit and increase the
717 sticky_rshift_significand (r
, r
, 1);
718 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
727 r
->class = rvc_normal
;
731 /* Re-normalize the result. */
734 /* Special case: if the subtraction results in zero, the result
736 if (r
->class == rvc_zero
)
740 /* Return R = A * B. */
743 do_multiply (r
, a
, b
)
744 struct real_value
*r
;
745 const struct real_value
*a
, *b
;
747 struct real_value u
, t
, *rr
;
748 unsigned int i
, j
, k
;
749 int sign
= a
->sign
^ b
->sign
;
751 switch (CLASS2 (a
->class, b
->class))
753 case CLASS2 (rvc_zero
, rvc_zero
):
754 case CLASS2 (rvc_zero
, rvc_normal
):
755 case CLASS2 (rvc_normal
, rvc_zero
):
756 /* +-0 * ANY = 0 with appropriate sign. */
760 case CLASS2 (rvc_zero
, rvc_nan
):
761 case CLASS2 (rvc_normal
, rvc_nan
):
762 case CLASS2 (rvc_inf
, rvc_nan
):
763 case CLASS2 (rvc_nan
, rvc_nan
):
764 /* ANY * NaN = NaN. */
769 case CLASS2 (rvc_nan
, rvc_zero
):
770 case CLASS2 (rvc_nan
, rvc_normal
):
771 case CLASS2 (rvc_nan
, rvc_inf
):
772 /* NaN * ANY = NaN. */
777 case CLASS2 (rvc_zero
, rvc_inf
):
778 case CLASS2 (rvc_inf
, rvc_zero
):
780 get_canonical_qnan (r
, sign
);
783 case CLASS2 (rvc_inf
, rvc_inf
):
784 case CLASS2 (rvc_normal
, rvc_inf
):
785 case CLASS2 (rvc_inf
, rvc_normal
):
786 /* Inf * Inf = Inf, R * Inf = Inf */
791 case CLASS2 (rvc_normal
, rvc_normal
):
798 if (r
== a
|| r
== b
)
804 u
.class = rvc_normal
;
807 /* Collect all the partial products. Since we don't have sure access
808 to a widening multiply, we split each long into two half-words.
810 Consider the long-hand form of a four half-word multiplication:
820 We construct partial products of the widened half-word products
821 that are known to not overlap, e.g. DF+DH. Each such partial
822 product is given its proper exponent, which allows us to sum them
823 and obtain the finished product. */
825 for (i
= 0; i
< SIGSZ
* 2; ++i
)
827 unsigned long ai
= a
->sig
[i
/ 2];
829 ai
>>= HOST_BITS_PER_LONG
/ 2;
831 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
836 for (j
= 0; j
< 2; ++j
)
838 int exp
= (a
->exp
- (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
839 + (b
->exp
- (1-j
)*(HOST_BITS_PER_LONG
/2)));
844 /* Would underflow to zero, which we shouldn't bother adding. */
849 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
851 unsigned long bi
= b
->sig
[k
/ 2];
853 bi
>>= HOST_BITS_PER_LONG
/ 2;
855 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
857 u
.sig
[k
/ 2] = ai
* bi
;
860 do_add (rr
, rr
, &u
, 0);
869 /* Return R = A / B. */
873 struct real_value
*r
;
874 const struct real_value
*a
, *b
;
876 int exp
, sign
= a
->sign
^ b
->sign
;
877 struct real_value t
, *rr
;
880 switch (CLASS2 (a
->class, b
->class))
882 case CLASS2 (rvc_zero
, rvc_zero
):
884 case CLASS2 (rvc_inf
, rvc_zero
):
886 case CLASS2 (rvc_inf
, rvc_inf
):
887 /* Inf / Inf = NaN. */
888 get_canonical_qnan (r
, sign
);
891 case CLASS2 (rvc_zero
, rvc_normal
):
892 case CLASS2 (rvc_zero
, rvc_inf
):
894 case CLASS2 (rvc_normal
, rvc_inf
):
900 case CLASS2 (rvc_normal
, rvc_zero
):
905 case CLASS2 (rvc_zero
, rvc_nan
):
906 case CLASS2 (rvc_normal
, rvc_nan
):
907 case CLASS2 (rvc_inf
, rvc_nan
):
908 case CLASS2 (rvc_nan
, rvc_nan
):
909 /* ANY / NaN = NaN. */
914 case CLASS2 (rvc_nan
, rvc_zero
):
915 case CLASS2 (rvc_nan
, rvc_normal
):
916 case CLASS2 (rvc_nan
, rvc_inf
):
917 /* NaN / ANY = NaN. */
922 case CLASS2 (rvc_inf
, rvc_normal
):
928 case CLASS2 (rvc_normal
, rvc_normal
):
935 if (r
== a
|| r
== b
)
940 rr
->class = rvc_normal
;
943 exp
= a
->exp
- b
->exp
+ 1;
950 inexact
= div_significands (rr
, a
, b
);
951 rr
->sig
[0] |= inexact
;
953 /* Re-normalize the result. */
960 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
961 one of the two operands is a NaN. */
964 do_compare (a
, b
, nan_result
)
965 const struct real_value
*a
, *b
;
970 switch (CLASS2 (a
->class, b
->class))
972 case CLASS2 (rvc_zero
, rvc_zero
):
973 /* Sign of zero doesn't matter for compares. */
976 case CLASS2 (rvc_inf
, rvc_zero
):
977 case CLASS2 (rvc_inf
, rvc_normal
):
978 case CLASS2 (rvc_normal
, rvc_zero
):
979 return (a
->sign
? -1 : 1);
981 case CLASS2 (rvc_inf
, rvc_inf
):
982 return -a
->sign
- -b
->sign
;
984 case CLASS2 (rvc_zero
, rvc_normal
):
985 case CLASS2 (rvc_zero
, rvc_inf
):
986 case CLASS2 (rvc_normal
, rvc_inf
):
987 return (b
->sign
? 1 : -1);
989 case CLASS2 (rvc_zero
, rvc_nan
):
990 case CLASS2 (rvc_normal
, rvc_nan
):
991 case CLASS2 (rvc_inf
, rvc_nan
):
992 case CLASS2 (rvc_nan
, rvc_nan
):
993 case CLASS2 (rvc_nan
, rvc_zero
):
994 case CLASS2 (rvc_nan
, rvc_normal
):
995 case CLASS2 (rvc_nan
, rvc_inf
):
998 case CLASS2 (rvc_normal
, rvc_normal
):
1005 if (a
->sign
!= b
->sign
)
1006 return -a
->sign
- -b
->sign
;
1008 if (a
->exp
> b
->exp
)
1010 else if (a
->exp
< b
->exp
)
1013 ret
= cmp_significands (a
, b
);
1015 return (a
->sign
? -ret
: ret
);
1018 /* Return A truncated to an integral value toward zero. */
1022 struct real_value
*r
;
1023 const struct real_value
*a
;
1036 get_zero (r
, r
->sign
);
1037 else if (r
->exp
< SIGNIFICAND_BITS
)
1038 clear_significand_below (r
, SIGNIFICAND_BITS
- r
->exp
);
1046 /* Perform the binary or unary operation described by CODE.
1047 For a unary operation, leave OP1 NULL. */
1050 real_arithmetic (tr
, icode
, top0
, top1
)
1051 REAL_VALUE_TYPE
*tr
;
1053 const REAL_VALUE_TYPE
*top0
, *top1
;
1055 struct real_value
*r
= (struct real_value
*) tr
;
1056 const struct real_value
*op0
= (const struct real_value
*) top0
;
1057 const struct real_value
*op1
= (const struct real_value
*) top1
;
1058 enum tree_code code
= icode
;
1063 do_add (r
, op0
, op1
, 0);
1067 do_add (r
, op0
, op1
, 1);
1071 do_multiply (r
, op0
, op1
);
1075 do_divide (r
, op0
, op1
);
1079 if (op1
->class == rvc_nan
)
1081 else if (do_compare (op0
, op1
, -1) < 0)
1088 if (op1
->class == rvc_nan
)
1090 else if (do_compare (op0
, op1
, 1) < 0)
1106 case FIX_TRUNC_EXPR
:
1107 do_fix_trunc (r
, op0
);
1115 /* Legacy. Similar, but return the result directly. */
1118 real_arithmetic2 (icode
, top0
, top1
)
1120 const REAL_VALUE_TYPE
*top0
, *top1
;
1123 real_arithmetic (&r
, icode
, top0
, top1
);
1128 real_compare (icode
, top0
, top1
)
1130 const REAL_VALUE_TYPE
*top0
, *top1
;
1132 enum tree_code code
= icode
;
1133 const struct real_value
*op0
= (const struct real_value
*) top0
;
1134 const struct real_value
*op1
= (const struct real_value
*) top1
;
1139 return do_compare (op0
, op1
, 1) < 0;
1141 return do_compare (op0
, op1
, 1) <= 0;
1143 return do_compare (op0
, op1
, -1) > 0;
1145 return do_compare (op0
, op1
, -1) >= 0;
1147 return do_compare (op0
, op1
, -1) == 0;
1149 return do_compare (op0
, op1
, -1) != 0;
1150 case UNORDERED_EXPR
:
1151 return op0
->class == rvc_nan
|| op1
->class == rvc_nan
;
1153 return op0
->class != rvc_nan
&& op1
->class != rvc_nan
;
1155 return do_compare (op0
, op1
, -1) < 0;
1157 return do_compare (op0
, op1
, -1) <= 0;
1159 return do_compare (op0
, op1
, 1) > 0;
1161 return do_compare (op0
, op1
, 1) >= 0;
1163 return do_compare (op0
, op1
, 0) == 0;
1170 /* Return floor log2(R). */
1174 const REAL_VALUE_TYPE
*tr
;
1176 const struct real_value
*r
= (const struct real_value
*) tr
;
1184 return (unsigned int)-1 >> 1;
1193 /* R = OP0 * 2**EXP. */
1196 real_ldexp (tr
, top0
, exp
)
1197 REAL_VALUE_TYPE
*tr
;
1198 const REAL_VALUE_TYPE
*top0
;
1201 struct real_value
*r
= (struct real_value
*) tr
;
1202 const struct real_value
*op0
= (const struct real_value
*) top0
;
1215 get_inf (r
, r
->sign
);
1216 else if (exp
< -MAX_EXP
)
1217 get_zero (r
, r
->sign
);
1227 /* Determine whether a floating-point value X is infinite. */
1231 const REAL_VALUE_TYPE
*tr
;
1233 const struct real_value
*r
= (const struct real_value
*) tr
;
1234 return (r
->class == rvc_inf
);
1237 /* Determine whether a floating-point value X is a NaN. */
1241 const REAL_VALUE_TYPE
*tr
;
1243 const struct real_value
*r
= (const struct real_value
*) tr
;
1244 return (r
->class == rvc_nan
);
1247 /* Determine whether a floating-point value X is negative. */
1251 const REAL_VALUE_TYPE
*tr
;
1253 const struct real_value
*r
= (const struct real_value
*) tr
;
1257 /* Determine whether a floating-point value X is minus zero. */
1261 const REAL_VALUE_TYPE
*tr
;
1263 const struct real_value
*r
= (const struct real_value
*) tr
;
1264 return r
->sign
&& r
->class == rvc_zero
;
1267 /* Compare two floating-point objects for bitwise identity. */
1270 real_identical (ta
, tb
)
1271 const REAL_VALUE_TYPE
*ta
, *tb
;
1273 const struct real_value
*a
= (const struct real_value
*) ta
;
1274 const struct real_value
*b
= (const struct real_value
*) tb
;
1277 if (a
->class != b
->class)
1279 if (a
->sign
!= b
->sign
)
1289 if (a
->exp
!= b
->exp
)
1293 for (i
= 0; i
< SIGSZ
; ++i
)
1294 if (a
->sig
[i
] != b
->sig
[i
])
1305 /* Try to change R into its exact multiplicative inverse in machine
1306 mode MODE. Return true if successful. */
1309 exact_real_inverse (mode
, tr
)
1310 enum machine_mode mode
;
1311 REAL_VALUE_TYPE
*tr
;
1313 const struct real_value
*one
= real_digit (1);
1314 struct real_value
*r
= (struct real_value
*) tr
;
1315 struct real_value u
;
1318 if (r
->class != rvc_normal
)
1321 /* Check for a power of two: all significand bits zero except the MSB. */
1322 for (i
= 0; i
< SIGSZ
-1; ++i
)
1325 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1328 /* Find the inverse and truncate to the required mode. */
1329 do_divide (&u
, one
, r
);
1330 real_convert ((REAL_VALUE_TYPE
*)&u
, mode
, (REAL_VALUE_TYPE
*)&u
);
1332 /* The rounding may have overflowed. */
1333 if (u
.class != rvc_normal
)
1335 for (i
= 0; i
< SIGSZ
-1; ++i
)
1338 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1345 /* Render R as an integer. */
1348 real_to_integer (tr
)
1349 const REAL_VALUE_TYPE
*tr
;
1351 const struct real_value
*r
= (const struct real_value
*) tr
;
1352 unsigned HOST_WIDE_INT i
;
1363 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1371 if (r
->exp
> HOST_BITS_PER_WIDE_INT
)
1374 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1375 i
= r
->sig
[SIGSZ
-1];
1376 else if (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
)
1378 i
= r
->sig
[SIGSZ
-1];
1379 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1380 i
|= r
->sig
[SIGSZ
-2];
1385 i
>>= HOST_BITS_PER_WIDE_INT
- r
->exp
;
1396 /* Likewise, but to an integer pair, HI+LOW. */
1399 real_to_integer2 (plow
, phigh
, tr
)
1400 HOST_WIDE_INT
*plow
, *phigh
;
1401 const REAL_VALUE_TYPE
*tr
;
1403 struct real_value r
;
1404 HOST_WIDE_INT low
, high
;
1407 r
= *(const struct real_value
*) tr
;
1418 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1432 if (exp
>= 2*HOST_BITS_PER_WIDE_INT
)
1435 rshift_significand (&r
, &r
, 2*HOST_BITS_PER_WIDE_INT
- exp
);
1436 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1438 high
= r
.sig
[SIGSZ
-1];
1439 low
= r
.sig
[SIGSZ
-2];
1441 else if (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
)
1443 high
= r
.sig
[SIGSZ
-1];
1444 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1445 high
|= r
.sig
[SIGSZ
-2];
1447 low
= r
.sig
[SIGSZ
-3];
1448 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1449 low
|= r
.sig
[SIGSZ
-4];
1459 low
= -low
, high
= ~high
;
1471 /* Render R as a decimal floating point constant. Emit DIGITS
1472 significant digits in the result. If DIGITS <= 0, choose the
1473 maximum for the representation. */
1475 #define M_LOG10_2 0.30102999566398119521
1478 real_to_decimal (str
, r_orig
, digits
)
1480 const REAL_VALUE_TYPE
*r_orig
;
1483 struct real_value r
;
1484 const struct real_value
*one
, *ten
;
1485 int dec_exp
, max_digits
, d
, cmp_half
;
1486 char *p
, *first
, *last
;
1489 r
= *(const struct real_value
*)r_orig
;
1493 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1498 strcpy (str
, (r
.sign
? "+Inf" : "-Inf"));
1501 /* ??? Print the significand as well, if not canonical? */
1502 strcpy (str
, (r
.sign
? "+NaN" : "-NaN"));
1508 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1509 if (digits
<= 0 || digits
> max_digits
)
1510 digits
= max_digits
;
1512 one
= real_digit (1);
1513 ten
= ten_to_ptwo (0);
1518 /* Estimate the decimal exponent. */
1519 dec_exp
= r
.exp
* M_LOG10_2
;
1521 /* Scale the number such that it is in [1, 10). */
1525 for (i
= EXP_BITS
- 1; i
>= 0; --i
)
1526 if (dec_exp
& (1 << i
))
1527 do_divide (&r
, &r
, ten_to_ptwo (i
));
1529 else if (dec_exp
< 0)
1531 int i
, pos_exp
= -(--dec_exp
);
1533 for (i
= EXP_BITS
- 1; i
>= 0; --i
)
1534 if (pos_exp
& (1 << i
))
1535 do_multiply (&r
, &r
, ten_to_ptwo (i
));
1538 /* Assert that the number is in the proper range. Round-off can
1539 prevent the above from working exactly. */
1540 if (do_compare (&r
, one
, -1) < 0)
1542 do_multiply (&r
, &r
, ten
);
1545 else if (do_compare (&r
, ten
, 1) >= 0)
1547 do_divide (&r
, &r
, ten
);
1557 d
= real_to_integer ((const REAL_VALUE_TYPE
*) &r
);
1558 do_add (&r
, &r
, real_digit (d
), 1);
1563 do_multiply (&r
, &r
, ten
);
1567 /* Round the result. Compare R vs 0.5 by doing R*2 vs 1.0. */
1569 cmp_half
= do_compare (&r
, one
, -1);
1571 /* Round to even. */
1594 first
[0] = first
[1];
1597 sprintf (last
, "e%+d", dec_exp
);
1600 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1601 significant digits in the result. If DIGITS <= 0, choose the maximum
1602 for the representation. */
1605 real_to_hexadecimal (str
, tr
, digits
)
1607 const REAL_VALUE_TYPE
*tr
;
1610 struct real_value r
;
1614 r
= *(const struct real_value
*) tr
;
1624 strcpy (str
, (r
.sign
? "+Inf" : "-Inf"));
1627 /* ??? Print the significand as well, if not canonical? */
1628 strcpy (str
, (r
.sign
? "+NaN" : "-NaN"));
1635 digits
= SIGNIFICAND_BITS
/ 4;
1645 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1646 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1648 *p
++ = "0123456789abcdef"[(r
.sig
[i
] >> j
) & 15];
1653 sprintf (p
, "p%+d", r
.exp
);
1656 /* Initialize R from a decimal or hexadecimal string. The string is
1657 assumed to have been syntax checked already. */
1660 real_from_string (tr
, str
)
1661 REAL_VALUE_TYPE
*tr
;
1664 struct real_value
*r
= (struct real_value
*) tr
;
1674 else if (*str
== '+')
1677 if (str
[0] == '0' && str
[1] == 'x')
1679 /* Hexadecimal floating point. */
1680 int pos
= SIGNIFICAND_BITS
- 4, d
;
1688 d
= hex_value (*str
);
1693 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1694 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1705 d
= hex_value (*str
);
1710 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1711 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1717 if (*str
== 'p' || *str
== 'P')
1727 else if (*str
== '+')
1731 while (ISDIGIT (*str
))
1738 /* Overflowed the exponent. */
1752 r
->class = rvc_normal
;
1766 /* Decimal floating point. */
1767 const struct real_value
*ten
= ten_to_ptwo (0);
1772 while (ISDIGIT (*str
))
1775 do_multiply (r
, r
, ten
);
1777 do_add (r
, r
, real_digit (d
), 0);
1782 while (ISDIGIT (*str
))
1785 do_multiply (r
, r
, ten
);
1787 do_add (r
, r
, real_digit (d
), 0);
1792 if (*str
== 'e' || *str
== 'E')
1802 else if (*str
== '+')
1806 while (ISDIGIT (*str
))
1813 /* Overflowed the exponent. */
1829 for (d
= 0; d
< EXP_BITS
; ++d
)
1831 do_divide (r
, r
, ten_to_ptwo (d
));
1835 for (d
= 0; d
< EXP_BITS
; ++d
)
1837 do_multiply (r
, r
, ten_to_ptwo (d
));
1844 get_zero (r
, r
->sign
);
1848 get_inf (r
, r
->sign
);
1852 /* Legacy. Similar, but return the result directly. */
1855 real_from_string2 (s
, mode
)
1857 enum machine_mode mode
;
1861 real_from_string (&r
, s
);
1862 if (mode
!= VOIDmode
)
1863 real_convert (&r
, mode
, &r
);
1868 /* Initialize R from the integer pair HIGH+LOW. */
1871 real_from_integer (tr
, mode
, low
, high
, unsigned_p
)
1872 REAL_VALUE_TYPE
*tr
;
1873 enum machine_mode mode
;
1874 unsigned HOST_WIDE_INT low
;
1878 struct real_value
*r
= (struct real_value
*) tr
;
1880 if (low
== 0 && high
== 0)
1884 r
->class = rvc_normal
;
1885 r
->sign
= high
< 0 && !unsigned_p
;
1886 r
->exp
= 2 * HOST_BITS_PER_WIDE_INT
;
1897 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
1899 r
->sig
[SIGSZ
-1] = high
;
1900 r
->sig
[SIGSZ
-2] = low
;
1901 memset (r
->sig
, 0, sizeof(long)*(SIGSZ
-2));
1903 else if (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
)
1905 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
1906 r
->sig
[SIGSZ
-2] = high
;
1907 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
1908 r
->sig
[SIGSZ
-4] = low
;
1910 memset (r
->sig
, 0, sizeof(long)*(SIGSZ
-4));
1918 if (mode
!= VOIDmode
)
1919 real_convert (tr
, mode
, tr
);
1922 /* Returns 10**2**n. */
1924 static const struct real_value
*
1928 static struct real_value tens
[EXP_BITS
];
1930 if (n
< 0 || n
>= EXP_BITS
)
1933 if (tens
[n
].class == rvc_zero
)
1935 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
1937 HOST_WIDE_INT t
= 10;
1940 for (i
= 0; i
< n
; ++i
)
1943 real_from_integer ((REAL_VALUE_TYPE
*) &tens
[n
], VOIDmode
, t
, 0, 1);
1947 const struct real_value
*t
= ten_to_ptwo (n
- 1);
1948 do_multiply (&tens
[n
], t
, t
);
1957 static const struct real_value
*
1961 static struct real_value num
[10];
1966 if (n
> 0 && num
[n
].class == rvc_zero
)
1967 real_from_integer ((REAL_VALUE_TYPE
*) &num
[n
], VOIDmode
, n
, 0, 1);
1972 /* Fills R with +Inf. */
1976 REAL_VALUE_TYPE
*tr
;
1978 get_inf ((struct real_value
*)tr
, 0);
1981 /* Fills R with a NaN whose significand is described by STR. If QUIET,
1982 we force a QNaN, else we force an SNaN. The string, if not empty,
1983 is parsed as a number and placed in the significand. Return true
1984 if the string was successfully parsed. */
1987 real_nan (tr
, str
, quiet
, mode
)
1988 REAL_VALUE_TYPE
*tr
;
1991 enum machine_mode mode
;
1993 struct real_value
*r
= (struct real_value
*) tr
;
1994 const struct real_format
*fmt
;
1996 fmt
= fmt_for_mode
[mode
- QFmode
];
2003 get_canonical_qnan (r
, 0);
2005 get_canonical_snan (r
, 0);
2012 memset (r
, 0, sizeof (*r
));
2015 /* Parse akin to strtol into the significand of R. */
2017 while (ISSPACE (*str
))
2021 else if (*str
== '+')
2031 while ((d
= hex_value (*str
)) < base
)
2033 struct real_value u
;
2038 lshift_significand (r
, r
, 3);
2041 lshift_significand (r
, r
, 4);
2044 lshift_significand_1 (&u
, r
);
2045 lshift_significand (r
, r
, 3);
2046 add_significands (r
, r
, &u
);
2054 add_significands (r
, r
, &u
);
2059 /* Must have consumed the entire string for success. */
2063 /* Shift the significand into place such that the bits
2064 are in the most significant bits for the format. */
2065 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->p
);
2067 /* Our MSB is always unset for NaNs. */
2068 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2070 /* Force quiet or signalling NaN. */
2072 r
->sig
[SIGSZ
-1] |= SIG_MSB
>> 1;
2074 r
->sig
[SIGSZ
-1] &= ~(SIG_MSB
>> 1);
2076 /* Force at least one bit of the significand set. */
2077 for (d
= 0; d
< SIGSZ
; ++d
)
2081 r
->sig
[SIGSZ
-1] |= SIG_MSB
>> 2;
2083 /* Our intermediate format forces QNaNs to have MSB-1 set.
2084 If the target format has QNaNs with the top bit unset,
2085 mirror the output routines and invert the top two bits. */
2086 if (!fmt
->qnan_msb_set
)
2087 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1) | (SIG_MSB
>> 2);
2093 /* Fills R with 2**N. */
2097 REAL_VALUE_TYPE
*tr
;
2100 struct real_value
*r
= (struct real_value
*) tr
;
2102 memset (r
, 0, sizeof (*r
));
2107 else if (n
< -MAX_EXP
)
2111 r
->class = rvc_normal
;
2113 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2119 round_for_format (fmt
, r
)
2120 const struct real_format
*fmt
;
2121 struct real_value
*r
;
2124 unsigned long sticky
;
2128 p2
= fmt
->p
* fmt
->log2_b
;
2129 emin2m1
= (fmt
->emin
- 1) * fmt
->log2_b
;
2130 emax2
= fmt
->emax
* fmt
->log2_b
;
2132 np2
= SIGNIFICAND_BITS
- p2
;
2136 get_zero (r
, r
->sign
);
2138 if (!fmt
->has_signed_zero
)
2143 get_inf (r
, r
->sign
);
2148 clear_significand_below (r
, np2
);
2150 /* If we've cleared the entire significand, we need one bit
2151 set for this to continue to be a NaN. */
2152 for (i
= 0; i
< SIGSZ
; ++i
)
2156 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 2;
2166 /* If we're not base2, normalize the exponent to a multiple of
2168 if (fmt
->log2_b
!= 1)
2170 int shift
= r
->exp
& (fmt
->log2_b
- 1);
2173 shift
= fmt
->log2_b
- shift
;
2174 sticky_rshift_significand (r
, r
, shift
);
2179 /* Check the range of the exponent. If we're out of range,
2180 either underflow or overflow. */
2183 else if (r
->exp
<= emin2m1
)
2187 if (!fmt
->has_denorm
)
2189 /* Don't underflow completely until we've had a chance to round. */
2190 if (r
->exp
< emin2m1
)
2195 diff
= emin2m1
- r
->exp
+ 1;
2199 /* De-normalize the significand. */
2200 sticky_rshift_significand (r
, r
, diff
);
2205 /* There are P2 true significand bits, followed by one guard bit,
2206 followed by one sticky bit, followed by stuff. Fold non-zero
2207 stuff into the sticky bit. */
2210 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2211 sticky
|= r
->sig
[i
];
2213 r
->sig
[w
] & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2215 guard
= test_significand_bit (r
, np2
- 1);
2216 lsb
= test_significand_bit (r
, np2
);
2218 /* Round to even. */
2219 if (guard
&& (sticky
|| lsb
))
2221 struct real_value u
;
2223 set_significand_bit (&u
, np2
);
2225 if (add_significands (r
, r
, &u
))
2227 /* Overflow. Means the significand had been all ones, and
2228 is now all zeros. Need to increase the exponent, and
2229 possibly re-normalize it. */
2230 if (++r
->exp
> emax2
)
2232 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2234 if (fmt
->log2_b
!= 1)
2236 int shift
= r
->exp
& (fmt
->log2_b
- 1);
2239 shift
= fmt
->log2_b
- shift
;
2240 sticky_rshift_significand (r
, r
, shift
);
2249 /* Catch underflow that we deferred until after rounding. */
2250 if (r
->exp
<= emin2m1
)
2253 /* Clear out trailing garbage. */
2254 clear_significand_below (r
, np2
);
2257 /* Extend or truncate to a new mode. */
2260 real_convert (tr
, mode
, ta
)
2261 REAL_VALUE_TYPE
*tr
;
2262 enum machine_mode mode
;
2263 const REAL_VALUE_TYPE
*ta
;
2265 struct real_value
*r
= (struct real_value
*)tr
;
2266 const struct real_value
*a
= (const struct real_value
*)ta
;
2267 const struct real_format
*fmt
;
2269 fmt
= fmt_for_mode
[mode
- QFmode
];
2274 round_for_format (fmt
, r
);
2276 /* round_for_format de-normalizes denormals. Undo just that part. */
2277 if (r
->class == rvc_normal
)
2281 /* Legacy. Likewise, except return the struct directly. */
2284 real_value_truncate (mode
, a
)
2285 enum machine_mode mode
;
2289 real_convert (&r
, mode
, &a
);
2293 /* Return true if truncating to MODE is exact. */
2296 exact_real_truncate (mode
, ta
)
2297 enum machine_mode mode
;
2298 const REAL_VALUE_TYPE
*ta
;
2301 real_convert (&t
, mode
, ta
);
2302 return real_identical (&t
, ta
);
2305 /* Write R to the target format of MODE. Place the words of the
2306 result in target word order in BUF. There are always 32 bits
2307 in each long, no matter the size of the host long.
2309 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2312 real_to_target (buf
, tr
, mode
)
2314 const REAL_VALUE_TYPE
*tr
;
2315 enum machine_mode mode
;
2317 struct real_value r
;
2318 const struct real_format
*fmt
;
2321 r
= *(const struct real_value
*) tr
;
2323 fmt
= fmt_for_mode
[mode
- QFmode
];
2327 round_for_format (fmt
, &r
);
2330 (*fmt
->encode
) (fmt
, buf
, &r
);
2335 /* Read R from the target format of MODE. Read the words of the
2336 result in target word order in BUF. There are always 32 bits
2337 in each long, no matter the size of the host long. */
2340 real_from_target (tr
, buf
, mode
)
2341 REAL_VALUE_TYPE
*tr
;
2343 enum machine_mode mode
;
2345 struct real_value
*r
= (struct real_value
*) tr
;
2346 const struct real_format
*fmt
;
2348 fmt
= fmt_for_mode
[mode
- QFmode
];
2352 (*fmt
->decode
) (fmt
, r
, buf
);
2355 /* Return the number of bits in the significand for MODE. */
2356 /* ??? Legacy. Should get access to real_format directly. */
2359 significand_size (mode
)
2360 enum machine_mode mode
;
2362 const struct real_format
*fmt
;
2364 fmt
= fmt_for_mode
[mode
- QFmode
];
2368 return fmt
->p
* fmt
->log2_b
;
2371 /* IEEE single-precision format. */
2373 static void encode_ieee_single
PARAMS ((const struct real_format
*fmt
,
2374 long *, const struct real_value
*));
2375 static void decode_ieee_single
PARAMS ((const struct real_format
*,
2376 struct real_value
*, const long *));
2379 encode_ieee_single (fmt
, buf
, r
)
2380 const struct real_format
*fmt
;
2382 const struct real_value
*r
;
2384 unsigned long image
, sig
, exp
;
2385 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2387 image
= r
->sign
<< 31;
2388 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
2399 image
|= 0x7fffffff;
2407 if (!fmt
->qnan_msb_set
)
2408 image
^= 1 << 23 | 1 << 22;
2411 image
|= 0x7fffffff;
2415 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2416 whereas the intermediate representation is 0.F x 2**exp.
2417 Which means we're off by one. */
2421 exp
= r
->exp
+ 127 - 1;
2431 decode_ieee_single (fmt
, r
, buf
)
2432 const struct real_format
*fmt
;
2433 struct real_value
*r
;
2436 unsigned long image
= buf
[0] & 0xffffffff;
2437 bool sign
= (image
>> 31) & 1;
2438 int exp
= (image
>> 23) & 0xff;
2440 memset (r
, 0, sizeof (*r
));
2441 image
<<= HOST_BITS_PER_LONG
- 24;
2446 if (image
&& fmt
->has_denorm
)
2448 r
->class = rvc_normal
;
2451 r
->sig
[SIGSZ
-1] = image
<< 1;
2454 else if (fmt
->has_signed_zero
)
2457 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2463 if (!fmt
->qnan_msb_set
)
2464 image
^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
2465 r
->sig
[SIGSZ
-1] = image
;
2475 r
->class = rvc_normal
;
2477 r
->exp
= exp
- 127 + 1;
2478 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2482 const struct real_format ieee_single
=
2499 /* IEEE double-precision format. */
2501 static void encode_ieee_double
PARAMS ((const struct real_format
*fmt
,
2502 long *, const struct real_value
*));
2503 static void decode_ieee_double
PARAMS ((const struct real_format
*,
2504 struct real_value
*, const long *));
2507 encode_ieee_double (fmt
, buf
, r
)
2508 const struct real_format
*fmt
;
2510 const struct real_value
*r
;
2512 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
2513 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2515 image_hi
= r
->sign
<< 31;
2518 if (HOST_BITS_PER_LONG
== 64)
2520 sig_hi
= r
->sig
[SIGSZ
-1];
2521 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
2522 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
2526 sig_hi
= r
->sig
[SIGSZ
-1];
2527 sig_lo
= r
->sig
[SIGSZ
-2];
2528 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
2529 sig_hi
= (sig_hi
>> 11) & 0xfffff;
2539 image_hi
|= 2047 << 20;
2542 image_hi
|= 0x7fffffff;
2543 image_lo
= 0xffffffff;
2550 image_hi
|= 2047 << 20;
2552 if (!fmt
->qnan_msb_set
)
2553 image_hi
^= 1 << 19 | 1 << 18;
2558 image_hi
|= 0x7fffffff;
2559 image_lo
= 0xffffffff;
2564 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2565 whereas the intermediate representation is 0.F x 2**exp.
2566 Which means we're off by one. */
2570 exp
= r
->exp
+ 1023 - 1;
2571 image_hi
|= exp
<< 20;
2577 if (FLOAT_WORDS_BIG_ENDIAN
)
2578 buf
[0] = image_hi
, buf
[1] = image_lo
;
2580 buf
[0] = image_lo
, buf
[1] = image_hi
;
2584 decode_ieee_double (fmt
, r
, buf
)
2585 const struct real_format
*fmt
;
2586 struct real_value
*r
;
2589 unsigned long image_hi
, image_lo
;
2593 if (FLOAT_WORDS_BIG_ENDIAN
)
2594 image_hi
= buf
[0], image_lo
= buf
[1];
2596 image_lo
= buf
[0], image_hi
= buf
[1];
2597 image_lo
&= 0xffffffff;
2598 image_hi
&= 0xffffffff;
2600 sign
= (image_hi
>> 31) & 1;
2601 exp
= (image_hi
>> 20) & 0x7ff;
2603 memset (r
, 0, sizeof (*r
));
2605 image_hi
<<= 32 - 21;
2606 image_hi
|= image_lo
>> 21;
2607 image_hi
&= 0x7fffffff;
2608 image_lo
<<= 32 - 21;
2612 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
2614 r
->class = rvc_normal
;
2617 if (HOST_BITS_PER_LONG
== 32)
2619 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
2621 r
->sig
[SIGSZ
-1] = image_hi
;
2622 r
->sig
[SIGSZ
-2] = image_lo
;
2626 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
2627 r
->sig
[SIGSZ
-1] = image_hi
;
2631 else if (fmt
->has_signed_zero
)
2634 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
2636 if (image_hi
|| image_lo
)
2640 if (HOST_BITS_PER_LONG
== 32)
2642 r
->sig
[SIGSZ
-1] = image_hi
;
2643 r
->sig
[SIGSZ
-2] = image_lo
;
2646 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
2648 if (!fmt
->qnan_msb_set
)
2649 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
2659 r
->class = rvc_normal
;
2661 r
->exp
= exp
- 1023 + 1;
2662 if (HOST_BITS_PER_LONG
== 32)
2664 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
2665 r
->sig
[SIGSZ
-2] = image_lo
;
2668 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
2672 const struct real_format ieee_double
=
2689 /* IEEE extended double precision format. This comes in three
2690 flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2693 static void encode_ieee_extended
PARAMS ((const struct real_format
*fmt
,
2694 long *, const struct real_value
*));
2695 static void decode_ieee_extended
PARAMS ((const struct real_format
*,
2696 struct real_value
*, const long *));
2698 static void encode_ieee_extended_128
PARAMS ((const struct real_format
*fmt
,
2700 const struct real_value
*));
2701 static void decode_ieee_extended_128
PARAMS ((const struct real_format
*,
2702 struct real_value
*,
2706 encode_ieee_extended (fmt
, buf
, r
)
2707 const struct real_format
*fmt
;
2709 const struct real_value
*r
;
2711 unsigned long image_hi
, sig_hi
, sig_lo
;
2712 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2714 image_hi
= r
->sign
<< 15;
2715 sig_hi
= sig_lo
= 0;
2727 /* Intel requires the explicit integer bit to be set, otherwise
2728 it considers the value a "pseudo-infinity". Motorola docs
2729 say it doesn't care. */
2730 sig_hi
= 0x80000000;
2735 sig_lo
= sig_hi
= 0xffffffff;
2743 if (HOST_BITS_PER_LONG
== 32)
2745 sig_hi
= r
->sig
[SIGSZ
-1];
2746 sig_lo
= r
->sig
[SIGSZ
-2];
2750 sig_lo
= r
->sig
[SIGSZ
-1];
2751 sig_hi
= sig_lo
>> 31 >> 1;
2752 sig_lo
&= 0xffffffff;
2754 if (!fmt
->qnan_msb_set
)
2755 sig_hi
^= 1 << 30 | 1 << 29;
2757 /* Intel requires the explicit integer bit to be set, otherwise
2758 it considers the value a "pseudo-nan". Motorola docs say it
2760 sig_hi
|= 0x80000000;
2765 sig_lo
= sig_hi
= 0xffffffff;
2773 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2774 whereas the intermediate representation is 0.F x 2**exp.
2775 Which means we're off by one.
2777 Except for Motorola, which consider exp=0 and explicit
2778 integer bit set to continue to be normalized. In theory
2779 this descrepency has been taken care of by the difference
2780 in fmt->emin in round_for_format. */
2792 if (HOST_BITS_PER_LONG
== 32)
2794 sig_hi
= r
->sig
[SIGSZ
-1];
2795 sig_lo
= r
->sig
[SIGSZ
-2];
2799 sig_lo
= r
->sig
[SIGSZ
-1];
2800 sig_hi
= sig_lo
>> 31 >> 1;
2801 sig_lo
&= 0xffffffff;
2807 if (FLOAT_WORDS_BIG_ENDIAN
)
2808 buf
[0] = image_hi
<< 16, buf
[1] = sig_hi
, buf
[2] = sig_lo
;
2810 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
2814 encode_ieee_extended_128 (fmt
, buf
, r
)
2815 const struct real_format
*fmt
;
2817 const struct real_value
*r
;
2819 buf
[3 * !FLOAT_WORDS_BIG_ENDIAN
] = 0;
2820 encode_ieee_extended (fmt
, buf
+!!FLOAT_WORDS_BIG_ENDIAN
, r
);
2824 decode_ieee_extended (fmt
, r
, buf
)
2825 const struct real_format
*fmt
;
2826 struct real_value
*r
;
2829 unsigned long image_hi
, sig_hi
, sig_lo
;
2833 if (FLOAT_WORDS_BIG_ENDIAN
)
2834 image_hi
= buf
[0] >> 16, sig_hi
= buf
[1], sig_lo
= buf
[2];
2836 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
2837 sig_lo
&= 0xffffffff;
2838 sig_hi
&= 0xffffffff;
2839 image_hi
&= 0xffffffff;
2841 sign
= (image_hi
>> 15) & 1;
2842 exp
= image_hi
& 0x7fff;
2844 memset (r
, 0, sizeof (*r
));
2848 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
2850 r
->class = rvc_normal
;
2853 /* When the IEEE format contains a hidden bit, we know that
2854 it's zero at this point, and so shift up the significand
2855 and decrease the exponent to match. In this case, Motorola
2856 defines the explicit integer bit to be valid, so we don't
2857 know whether the msb is set or not. */
2859 if (HOST_BITS_PER_LONG
== 32)
2861 r
->sig
[SIGSZ
-1] = sig_hi
;
2862 r
->sig
[SIGSZ
-2] = sig_lo
;
2865 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
2869 else if (fmt
->has_signed_zero
)
2872 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
2874 /* See above re "pseudo-infinities" and "pseudo-nans".
2875 Short summary is that the MSB will likely always be
2876 set, and that we don't care about it. */
2877 sig_hi
&= 0x7fffffff;
2879 if (sig_hi
|| sig_lo
)
2883 if (HOST_BITS_PER_LONG
== 32)
2885 r
->sig
[SIGSZ
-1] = sig_hi
;
2886 r
->sig
[SIGSZ
-2] = sig_lo
;
2889 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
2891 if (!fmt
->qnan_msb_set
)
2892 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
2902 r
->class = rvc_normal
;
2904 r
->exp
= exp
- 16383 + 1;
2905 if (HOST_BITS_PER_LONG
== 32)
2907 r
->sig
[SIGSZ
-1] = sig_hi
;
2908 r
->sig
[SIGSZ
-2] = sig_lo
;
2911 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
2916 decode_ieee_extended_128 (fmt
, r
, buf
)
2917 const struct real_format
*fmt
;
2918 struct real_value
*r
;
2921 decode_ieee_extended (fmt
, r
, buf
+!!FLOAT_WORDS_BIG_ENDIAN
);
2924 const struct real_format ieee_extended_motorola
=
2926 encode_ieee_extended
,
2927 decode_ieee_extended
,
2940 const struct real_format ieee_extended_intel_96
=
2942 encode_ieee_extended
,
2943 decode_ieee_extended
,
2956 const struct real_format ieee_extended_intel_128
=
2958 encode_ieee_extended_128
,
2959 decode_ieee_extended_128
,
2973 /* IEEE quad precision format. */
2975 static void encode_ieee_quad
PARAMS ((const struct real_format
*fmt
,
2976 long *, const struct real_value
*));
2977 static void decode_ieee_quad
PARAMS ((const struct real_format
*,
2978 struct real_value
*, const long *));
2981 encode_ieee_quad (fmt
, buf
, r
)
2982 const struct real_format
*fmt
;
2984 const struct real_value
*r
;
2986 unsigned long image3
, image2
, image1
, image0
, exp
;
2987 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2988 struct real_value u
;
2990 image3
= r
->sign
<< 31;
2995 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3004 image3
|= 32767 << 16;
3007 image3
|= 0x7fffffff;
3008 image2
= 0xffffffff;
3009 image1
= 0xffffffff;
3010 image0
= 0xffffffff;
3017 image3
|= 32767 << 16;
3019 if (HOST_BITS_PER_LONG
== 32)
3024 image3
|= u
.sig
[3] & 0xffff;
3029 image1
= image0
>> 31 >> 1;
3031 image3
|= (image2
>> 31 >> 1) & 0xffff;
3032 image0
&= 0xffffffff;
3033 image2
&= 0xffffffff;
3036 if (!fmt
->qnan_msb_set
)
3037 image3
^= 1 << 15 | 1 << 14;
3041 image3
|= 0x7fffffff;
3042 image2
= 0xffffffff;
3043 image1
= 0xffffffff;
3044 image0
= 0xffffffff;
3049 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3050 whereas the intermediate representation is 0.F x 2**exp.
3051 Which means we're off by one. */
3055 exp
= r
->exp
+ 16383 - 1;
3056 image3
|= exp
<< 16;
3058 if (HOST_BITS_PER_LONG
== 32)
3063 image3
|= u
.sig
[3] & 0xffff;
3068 image1
= image0
>> 31 >> 1;
3070 image3
|= (image2
>> 31 >> 1) & 0xffff;
3071 image0
&= 0xffffffff;
3072 image2
&= 0xffffffff;
3077 if (FLOAT_WORDS_BIG_ENDIAN
)
3094 decode_ieee_quad (fmt
, r
, buf
)
3095 const struct real_format
*fmt
;
3096 struct real_value
*r
;
3099 unsigned long image3
, image2
, image1
, image0
;
3103 if (FLOAT_WORDS_BIG_ENDIAN
)
3117 image0
&= 0xffffffff;
3118 image1
&= 0xffffffff;
3119 image2
&= 0xffffffff;
3121 sign
= (image3
>> 31) & 1;
3122 exp
= (image3
>> 16) & 0x7fff;
3125 memset (r
, 0, sizeof (*r
));
3129 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
3131 r
->class = rvc_normal
;
3134 r
->exp
= -16382 + (SIGNIFICAND_BITS
- 112);
3135 if (HOST_BITS_PER_LONG
== 32)
3144 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3145 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3150 else if (fmt
->has_signed_zero
)
3153 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3155 if (image3
| image2
| image1
| image0
)
3160 if (HOST_BITS_PER_LONG
== 32)
3169 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3170 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3172 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3174 if (!fmt
->qnan_msb_set
)
3175 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
3185 r
->class = rvc_normal
;
3187 r
->exp
= exp
- 16383 + 1;
3189 if (HOST_BITS_PER_LONG
== 32)
3198 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3199 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3201 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3202 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3206 const struct real_format ieee_quad
=
3223 /* The VAX floating point formats. */
3225 static void encode_vax_f
PARAMS ((const struct real_format
*fmt
,
3226 long *, const struct real_value
*));
3227 static void decode_vax_f
PARAMS ((const struct real_format
*,
3228 struct real_value
*, const long *));
3229 static void encode_vax_d
PARAMS ((const struct real_format
*fmt
,
3230 long *, const struct real_value
*));
3231 static void decode_vax_d
PARAMS ((const struct real_format
*,
3232 struct real_value
*, const long *));
3233 static void encode_vax_g
PARAMS ((const struct real_format
*fmt
,
3234 long *, const struct real_value
*));
3235 static void decode_vax_g
PARAMS ((const struct real_format
*,
3236 struct real_value
*, const long *));
3239 encode_vax_f (fmt
, buf
, r
)
3240 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3242 const struct real_value
*r
;
3244 unsigned long sign
, exp
, sig
, image
;
3246 sign
= r
->sign
<< 15;
3256 image
= 0xffff7fff | sign
;
3260 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
3263 image
= (sig
<< 16) & 0xffff0000;
3274 decode_vax_f (fmt
, r
, buf
)
3275 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3276 struct real_value
*r
;
3279 unsigned long image
= buf
[0] & 0xffffffff;
3280 int exp
= (image
>> 7) & 0xff;
3282 memset (r
, 0, sizeof (*r
));
3286 r
->class = rvc_normal
;
3287 r
->sign
= (image
>> 15) & 1;
3290 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
3291 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
3296 encode_vax_d (fmt
, buf
, r
)
3297 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3299 const struct real_value
*r
;
3301 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3306 image0
= image1
= 0;
3311 image0
= 0xffff7fff | sign
;
3312 image1
= 0xffffffff;
3316 /* Extract the significand into straight hi:lo. */
3317 if (HOST_BITS_PER_LONG
== 64)
3319 image0
= r
->sig
[SIGSZ
-1];
3320 image1
= (image0
>> (64 - 56)) & 0xffffffff;
3321 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
3325 image0
= r
->sig
[SIGSZ
-1];
3326 image1
= r
->sig
[SIGSZ
-2];
3327 image1
= (image0
<< 24) | (image1
>> 8);
3328 image0
= (image0
>> 8) & 0xffffff;
3331 /* Rearrange the half-words of the significand to match the
3333 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
3334 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
3336 /* Add the sign and exponent. */
3338 image0
|= (r
->exp
+ 128) << 7;
3342 if (FLOAT_WORDS_BIG_ENDIAN
)
3343 buf
[0] = image1
, buf
[1] = image0
;
3345 buf
[0] = image0
, buf
[1] = image1
;
3349 decode_vax_d (fmt
, r
, buf
)
3350 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3351 struct real_value
*r
;
3354 unsigned long image0
, image1
;
3357 if (FLOAT_WORDS_BIG_ENDIAN
)
3358 image1
= buf
[0], image0
= buf
[1];
3360 image0
= buf
[0], image1
= buf
[1];
3361 image0
&= 0xffffffff;
3362 image1
&= 0xffffffff;
3364 exp
= (image0
>> 7) & 0x7f;
3366 memset (r
, 0, sizeof (*r
));
3370 r
->class = rvc_normal
;
3371 r
->sign
= (image0
>> 15) & 1;
3374 /* Rearrange the half-words of the external format into
3375 proper ascending order. */
3376 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
3377 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
3379 if (HOST_BITS_PER_LONG
== 64)
3381 image0
= (image0
<< 31 << 1) | image1
;
3384 r
->sig
[SIGSZ
-1] = image0
;
3388 r
->sig
[SIGSZ
-1] = image0
;
3389 r
->sig
[SIGSZ
-2] = image1
;
3390 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
3391 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3397 encode_vax_g (fmt
, buf
, r
)
3398 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3400 const struct real_value
*r
;
3402 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3407 image0
= image1
= 0;
3412 image0
= 0xffff7fff | sign
;
3413 image1
= 0xffffffff;
3417 /* Extract the significand into straight hi:lo. */
3418 if (HOST_BITS_PER_LONG
== 64)
3420 image0
= r
->sig
[SIGSZ
-1];
3421 image1
= (image0
>> (64 - 53)) & 0xffffffff;
3422 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
3426 image0
= r
->sig
[SIGSZ
-1];
3427 image1
= r
->sig
[SIGSZ
-2];
3428 image1
= (image0
<< 21) | (image1
>> 11);
3429 image0
= (image0
>> 11) & 0xfffff;
3432 /* Rearrange the half-words of the significand to match the
3434 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
3435 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
3437 /* Add the sign and exponent. */
3439 image0
|= (r
->exp
+ 1024) << 4;
3443 if (FLOAT_WORDS_BIG_ENDIAN
)
3444 buf
[0] = image1
, buf
[1] = image0
;
3446 buf
[0] = image0
, buf
[1] = image1
;
3450 decode_vax_g (fmt
, r
, buf
)
3451 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3452 struct real_value
*r
;
3455 unsigned long image0
, image1
;
3458 if (FLOAT_WORDS_BIG_ENDIAN
)
3459 image1
= buf
[0], image0
= buf
[1];
3461 image0
= buf
[0], image1
= buf
[1];
3462 image0
&= 0xffffffff;
3463 image1
&= 0xffffffff;
3465 exp
= (image0
>> 4) & 0x7ff;
3467 memset (r
, 0, sizeof (*r
));
3471 r
->class = rvc_normal
;
3472 r
->sign
= (image0
>> 15) & 1;
3473 r
->exp
= exp
- 1024;
3475 /* Rearrange the half-words of the external format into
3476 proper ascending order. */
3477 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
3478 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
3480 if (HOST_BITS_PER_LONG
== 64)
3482 image0
= (image0
<< 31 << 1) | image1
;
3485 r
->sig
[SIGSZ
-1] = image0
;
3489 r
->sig
[SIGSZ
-1] = image0
;
3490 r
->sig
[SIGSZ
-2] = image1
;
3491 lshift_significand (r
, r
, 64 - 53);
3492 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3497 const struct real_format vax_f_format
=
3513 const struct real_format vax_d_format
=
3529 const struct real_format vax_g_format
=
3546 /* The IBM S/390 floating point formats. A good reference for these can
3547 be found in chapter 9 of "ESA/390 Principles of Operation", IBM document
3548 number SA22-7201-01. An on-line version can be found here:
3550 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3553 static void encode_i370_single
PARAMS ((const struct real_format
*fmt
,
3554 long *, const struct real_value
*));
3555 static void decode_i370_single
PARAMS ((const struct real_format
*,
3556 struct real_value
*, const long *));
3557 static void encode_i370_double
PARAMS ((const struct real_format
*fmt
,
3558 long *, const struct real_value
*));
3559 static void decode_i370_double
PARAMS ((const struct real_format
*,
3560 struct real_value
*, const long *));
3563 encode_i370_single (fmt
, buf
, r
)
3564 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3566 const struct real_value
*r
;
3568 unsigned long sign
, exp
, sig
, image
;
3570 sign
= r
->sign
<< 31;
3580 image
= 0x7fffffff | sign
;
3584 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0xffffff;
3585 exp
= ((r
->exp
/ 4) + 64) << 24;
3586 image
= sign
| exp
| sig
;
3594 decode_i370_single (fmt
, r
, buf
)
3595 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3596 struct real_value
*r
;
3599 unsigned long sign
, sig
, image
= buf
[0];
3602 sign
= (image
>> 31) & 1;
3603 exp
= (image
>> 24) & 0x7f;
3604 sig
= image
& 0xffffff;
3606 memset (r
, 0, sizeof (*r
));
3610 r
->class = rvc_normal
;
3612 r
->exp
= (exp
- 64) * 4;
3613 r
->sig
[SIGSZ
-1] = sig
<< (HOST_BITS_PER_LONG
- 24);
3619 encode_i370_double (fmt
, buf
, r
)
3620 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3622 const struct real_value
*r
;
3624 unsigned long sign
, exp
, image_hi
, image_lo
;
3626 sign
= r
->sign
<< 31;
3631 image_hi
= image_lo
= 0;
3636 image_hi
= 0x7fffffff | sign
;
3637 image_lo
= 0xffffffff;
3641 if (HOST_BITS_PER_LONG
== 64)
3643 image_hi
= r
->sig
[SIGSZ
-1];
3644 image_lo
= (image_hi
>> (64 - 56)) & 0xffffffff;
3645 image_hi
= (image_hi
>> (64 - 56 + 1) >> 31) & 0xffffff;
3649 image_hi
= r
->sig
[SIGSZ
-1];
3650 image_lo
= r
->sig
[SIGSZ
-2];
3651 image_lo
= (image_lo
>> 8) | (image_hi
<< 24);
3655 exp
= ((r
->exp
/ 4) + 64) << 24;
3656 image_hi
|= sign
| exp
;
3660 if (FLOAT_WORDS_BIG_ENDIAN
)
3661 buf
[0] = image_hi
, buf
[1] = image_lo
;
3663 buf
[0] = image_lo
, buf
[1] = image_hi
;
3667 decode_i370_double (fmt
, r
, buf
)
3668 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3669 struct real_value
*r
;
3672 unsigned long sign
, image_hi
, image_lo
;
3675 if (FLOAT_WORDS_BIG_ENDIAN
)
3676 image_hi
= buf
[0], image_lo
= buf
[1];
3678 image_lo
= buf
[0], image_hi
= buf
[1];
3680 sign
= (image_hi
>> 31) & 1;
3681 exp
= (image_hi
>> 24) & 0x7f;
3682 image_hi
&= 0xffffff;
3683 image_lo
&= 0xffffffff;
3685 memset (r
, 0, sizeof (*r
));
3687 if (exp
|| image_hi
|| image_lo
)
3689 r
->class = rvc_normal
;
3691 r
->exp
= (exp
- 64) * 4 + (SIGNIFICAND_BITS
- 56);
3693 if (HOST_BITS_PER_LONG
== 32)
3695 r
->sig
[0] = image_lo
;
3696 r
->sig
[1] = image_hi
;
3699 r
->sig
[0] = image_lo
| (image_hi
<< 31 << 1);
3705 const struct real_format i370_single
=
3716 false, /* ??? The encoding does allow for "unnormals". */
3717 false, /* ??? The encoding does allow for "unnormals". */
3721 const struct real_format i370_double
=
3732 false, /* ??? The encoding does allow for "unnormals". */
3733 false, /* ??? The encoding does allow for "unnormals". */
3738 /* TMS320C[34]x twos complement floating point format. */
3740 static void encode_c4x_single
PARAMS ((const struct real_format
*fmt
,
3741 long *, const struct real_value
*));
3742 static void decode_c4x_single
PARAMS ((const struct real_format
*,
3743 struct real_value
*, const long *));
3744 static void encode_c4x_extended
PARAMS ((const struct real_format
*fmt
,
3745 long *, const struct real_value
*));
3746 static void decode_c4x_extended
PARAMS ((const struct real_format
*,
3747 struct real_value
*, const long *));
3750 encode_c4x_single (fmt
, buf
, r
)
3751 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3753 const struct real_value
*r
;
3755 unsigned long image
, exp
, sig
;
3767 sig
= 0x800000 - r
->sign
;
3772 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
3784 image
= ((exp
& 0xff) << 24) | (sig
& 0xffffff);
3789 decode_c4x_single (fmt
, r
, buf
)
3790 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3791 struct real_value
*r
;
3794 unsigned long image
= buf
[0];
3798 exp
= (((image
>> 24) & 0xff) ^ 0x80) - 0x80;
3799 sf
= ((image
& 0xffffff) ^ 0x800000) - 0x800000;
3801 memset (r
, 0, sizeof (*r
));
3805 r
->class = rvc_normal
;
3807 sig
= sf
& 0x7fffff;
3816 sig
= (sig
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
3819 r
->sig
[SIGSZ
-1] = sig
;
3824 encode_c4x_extended (fmt
, buf
, r
)
3825 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3827 const struct real_value
*r
;
3829 unsigned long exp
, sig
;
3841 sig
= 0x80000000 - r
->sign
;
3847 sig
= r
->sig
[SIGSZ
-1];
3848 if (HOST_BITS_PER_LONG
== 64)
3849 sig
= sig
>> 1 >> 31;
3863 exp
= (exp
& 0xff) << 24;
3866 if (FLOAT_WORDS_BIG_ENDIAN
)
3867 buf
[0] = exp
, buf
[1] = sig
;
3869 buf
[0] = sig
, buf
[0] = exp
;
3873 decode_c4x_extended (fmt
, r
, buf
)
3874 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3875 struct real_value
*r
;
3881 if (FLOAT_WORDS_BIG_ENDIAN
)
3882 exp
= buf
[0], sf
= buf
[1];
3884 sf
= buf
[0], exp
= buf
[1];
3886 exp
= (((exp
>> 24) & 0xff) & 0x80) - 0x80;
3887 sf
= ((sf
& 0xffffffff) ^ 0x80000000) - 0x80000000;
3889 memset (r
, 0, sizeof (*r
));
3893 r
->class = rvc_normal
;
3895 sig
= sf
& 0x7fffffff;
3904 if (HOST_BITS_PER_LONG
== 64)
3905 sig
= sig
<< 1 << 31;
3909 r
->sig
[SIGSZ
-1] = sig
;
3913 const struct real_format c4x_single
=
3929 const struct real_format c4x_extended
=
3931 encode_c4x_extended
,
3932 decode_c4x_extended
,
3946 /* Initialize things at start of compilation. */
3948 static const struct real_format
* format_for_size
PARAMS ((int));
3950 static const struct real_format
*
3951 format_for_size (size
)
3954 #ifndef TARGET_G_FORMAT
3955 #define TARGET_G_FORMAT 0
3958 switch (TARGET_FLOAT_FORMAT
)
3960 case IEEE_FLOAT_FORMAT
:
3964 return &ieee_single
;
3967 return &ieee_double
;
3970 if (!INTEL_EXTENDED_IEEE_FORMAT
)
3971 return &ieee_extended_motorola
;
3973 return &ieee_extended_intel_96
;
3976 if (!INTEL_EXTENDED_IEEE_FORMAT
)
3979 return &ieee_extended_intel_128
;
3983 case VAX_FLOAT_FORMAT
:
3987 return &vax_f_format
;
3990 if (TARGET_G_FORMAT
)
3991 return &vax_g_format
;
3993 return &vax_d_format
;
3997 case IBM_FLOAT_FORMAT
:
4001 return &i370_single
;
4003 return &i370_double
;
4007 case C4X_FLOAT_FORMAT
:
4013 return &c4x_extended
;
4026 /* Set up the mode->format table. */
4027 for (i
= 0; i
< 3; ++i
)
4029 enum machine_mode mode
;
4033 size
= FLOAT_TYPE_SIZE
;
4035 size
= DOUBLE_TYPE_SIZE
;
4037 size
= LONG_DOUBLE_TYPE_SIZE
;
4039 mode
= mode_for_size (size
, MODE_FLOAT
, 0);
4040 if (mode
== BLKmode
)
4043 fmt_for_mode
[mode
- QFmode
] = format_for_size (size
);