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 void set_significand_bit
PARAMS ((REAL_VALUE_TYPE
*, unsigned int));
106 static void clear_significand_bit
PARAMS ((REAL_VALUE_TYPE
*, unsigned int));
107 static bool test_significand_bit
PARAMS ((REAL_VALUE_TYPE
*, unsigned int));
108 static void clear_significand_below
PARAMS ((REAL_VALUE_TYPE
*,
110 static bool div_significands
PARAMS ((REAL_VALUE_TYPE
*,
111 const REAL_VALUE_TYPE
*,
112 const REAL_VALUE_TYPE
*));
113 static void normalize
PARAMS ((REAL_VALUE_TYPE
*));
115 static void do_add
PARAMS ((REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
116 const REAL_VALUE_TYPE
*, int));
117 static void do_multiply
PARAMS ((REAL_VALUE_TYPE
*,
118 const REAL_VALUE_TYPE
*,
119 const REAL_VALUE_TYPE
*));
120 static void do_divide
PARAMS ((REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*,
121 const REAL_VALUE_TYPE
*));
122 static int do_compare
PARAMS ((const REAL_VALUE_TYPE
*,
123 const REAL_VALUE_TYPE
*, int));
124 static void do_fix_trunc
PARAMS ((REAL_VALUE_TYPE
*,
125 const REAL_VALUE_TYPE
*));
127 static const REAL_VALUE_TYPE
* ten_to_ptwo
PARAMS ((int));
128 static const REAL_VALUE_TYPE
* real_digit
PARAMS ((int));
129 static void times_pten
PARAMS ((REAL_VALUE_TYPE
*, int));
131 static void round_for_format
PARAMS ((const struct real_format
*,
134 /* Initialize R with a positive zero. */
141 memset (r
, 0, sizeof (*r
));
145 /* Initialize R with the canonical quiet NaN. */
148 get_canonical_qnan (r
, sign
)
152 memset (r
, 0, sizeof (*r
));
155 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 1;
159 get_canonical_snan (r
, sign
)
163 memset (r
, 0, sizeof (*r
));
166 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 2;
174 memset (r
, 0, sizeof (*r
));
180 /* Right-shift the significand of A by N bits; put the result in the
181 significand of R. If any one bits are shifted out, set the least
182 significant bit of R. */
185 sticky_rshift_significand (r
, a
, n
)
187 const REAL_VALUE_TYPE
*a
;
190 unsigned long sticky
= 0;
191 unsigned int i
, ofs
= 0;
193 if (n
>= HOST_BITS_PER_LONG
)
195 for (i
= 0, ofs
= n
/ HOST_BITS_PER_LONG
; i
< ofs
; ++i
)
197 n
&= HOST_BITS_PER_LONG
- 1;
202 sticky
|= a
->sig
[ofs
] & (((unsigned long)1 << n
) - 1);
203 for (i
= 0; i
< SIGSZ
; ++i
)
206 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
207 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
208 << (HOST_BITS_PER_LONG
- n
)));
213 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
214 r
->sig
[i
] = a
->sig
[ofs
+ i
];
215 for (; i
< SIGSZ
; ++i
)
219 r
->sig
[0] |= (sticky
!= 0);
222 /* Right-shift the significand of A by N bits; put the result in the
226 rshift_significand (r
, a
, n
)
228 const REAL_VALUE_TYPE
*a
;
231 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
233 n
&= HOST_BITS_PER_LONG
- 1;
236 for (i
= 0; i
< SIGSZ
; ++i
)
239 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[ofs
+ i
]) >> n
)
240 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[ofs
+ i
+ 1])
241 << (HOST_BITS_PER_LONG
- n
)));
246 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
247 r
->sig
[i
] = a
->sig
[ofs
+ i
];
248 for (; i
< SIGSZ
; ++i
)
253 /* Left-shift the significand of A by N bits; put the result in the
257 lshift_significand (r
, a
, n
)
259 const REAL_VALUE_TYPE
*a
;
262 unsigned int i
, ofs
= n
/ HOST_BITS_PER_LONG
;
264 n
&= HOST_BITS_PER_LONG
- 1;
267 for (i
= 0; ofs
+ i
< SIGSZ
; ++i
)
268 r
->sig
[SIGSZ
-1-i
] = a
->sig
[SIGSZ
-1-i
-ofs
];
269 for (; i
< SIGSZ
; ++i
)
270 r
->sig
[SIGSZ
-1-i
] = 0;
273 for (i
= 0; i
< SIGSZ
; ++i
)
276 = (((ofs
+ i
>= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
]) << n
)
277 | ((ofs
+ i
+ 1 >= SIGSZ
? 0 : a
->sig
[SIGSZ
-1-i
-ofs
-1])
278 >> (HOST_BITS_PER_LONG
- n
)));
282 /* Likewise, but N is specialized to 1. */
285 lshift_significand_1 (r
, a
)
287 const REAL_VALUE_TYPE
*a
;
291 for (i
= SIGSZ
- 1; i
> 0; --i
)
292 r
->sig
[i
] = (a
->sig
[i
] << 1) | (a
->sig
[i
-1] >> (HOST_BITS_PER_LONG
- 1));
293 r
->sig
[0] = a
->sig
[0] << 1;
296 /* Add the significands of A and B, placing the result in R. Return
297 true if there was carry out of the most significant word. */
300 add_significands (r
, a
, b
)
302 const REAL_VALUE_TYPE
*a
, *b
;
307 for (i
= 0; i
< SIGSZ
; ++i
)
309 unsigned long ai
= a
->sig
[i
];
310 unsigned long ri
= ai
+ b
->sig
[i
];
326 /* Subtract the significands of A and B, placing the result in R.
327 Return true if there was carry out of the most significant word. */
330 sub_significands (r
, a
, b
)
332 const REAL_VALUE_TYPE
*a
, *b
;
337 for (i
= 0; i
< SIGSZ
; ++i
)
339 unsigned long ai
= a
->sig
[i
];
340 unsigned long ri
= ai
- b
->sig
[i
];
356 /* Negate the significand A, placing the result in R. */
359 neg_significand (r
, a
)
361 const REAL_VALUE_TYPE
*a
;
366 for (i
= 0; i
< SIGSZ
; ++i
)
368 unsigned long ri
, ai
= a
->sig
[i
];
387 /* Compare significands. Return tri-state vs zero. */
390 cmp_significands (a
, b
)
391 const REAL_VALUE_TYPE
*a
, *b
;
395 for (i
= SIGSZ
- 1; i
>= 0; --i
)
397 unsigned long ai
= a
->sig
[i
];
398 unsigned long bi
= b
->sig
[i
];
409 /* Set bit N of the significand of R. */
412 set_significand_bit (r
, n
)
416 r
->sig
[n
/ HOST_BITS_PER_LONG
]
417 |= (unsigned long)1 << (n
% HOST_BITS_PER_LONG
);
420 /* Clear bit N of the significand of R. */
423 clear_significand_bit (r
, n
)
427 r
->sig
[n
/ HOST_BITS_PER_LONG
]
428 &= ~((unsigned long)1 << (n
% HOST_BITS_PER_LONG
));
431 /* Test bit N of the significand of R. */
434 test_significand_bit (r
, n
)
438 /* ??? Compiler bug here if we return this expression directly.
439 The conversion to bool strips the "&1" and we wind up testing
440 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
441 int t
= (r
->sig
[n
/ HOST_BITS_PER_LONG
] >> (n
% HOST_BITS_PER_LONG
)) & 1;
445 /* Clear bits 0..N-1 of the significand of R. */
448 clear_significand_below (r
, n
)
452 int i
, w
= n
/ HOST_BITS_PER_LONG
;
454 for (i
= 0; i
< w
; ++i
)
457 r
->sig
[w
] &= ~(((unsigned long)1 << (n
% HOST_BITS_PER_LONG
)) - 1);
460 /* Divide the significands of A and B, placing the result in R. Return
461 true if the division was inexact. */
464 div_significands (r
, a
, b
)
466 const REAL_VALUE_TYPE
*a
, *b
;
469 int bit
= SIGNIFICAND_BITS
- 1;
474 memset (r
->sig
, 0, sizeof (r
->sig
));
479 if ((u
.sig
[SIGSZ
-1] & SIG_MSB
) == 0)
481 lshift_significand_1 (&u
, &u
);
483 if (cmp_significands (&u
, b
) >= 0)
485 sub_significands (&u
, &u
, b
);
486 set_significand_bit (r
, bit
);
491 /* We lose a bit here, and thus know the next quotient bit
493 lshift_significand_1 (&u
, &u
);
494 sub_significands (&u
, &u
, b
);
495 set_significand_bit (r
, bit
);
500 for (i
= 0, inexact
= 0; i
< SIGSZ
; i
++)
506 /* Adjust the exponent and significand of R such that the most
507 significant bit is set. We underflow to zero and overflow to
508 infinity here, without denormals. (The intermediate representation
509 exponent is large enough to handle target denormals normalized.) */
518 /* Find the first word that is nonzero. */
519 for (i
= SIGSZ
- 1; i
>= 0; i
--)
521 shift
+= HOST_BITS_PER_LONG
;
525 /* Zero significand flushes to zero. */
533 /* Find the first bit that is nonzero. */
535 if (r
->sig
[i
] & ((unsigned long)1 << (HOST_BITS_PER_LONG
- 1 - j
)))
541 exp
= r
->exp
- shift
;
543 get_inf (r
, r
->sign
);
544 else if (exp
< -MAX_EXP
)
545 get_zero (r
, r
->sign
);
549 lshift_significand (r
, r
, shift
);
554 /* Return R = A + (SUBTRACT_P ? -B : B). */
557 do_add (r
, a
, b
, subtract_p
)
559 const REAL_VALUE_TYPE
*a
, *b
;
565 /* Determine if we need to add or subtract. */
567 subtract_p
= (sign
^ b
->sign
) ^ subtract_p
;
569 switch (CLASS2 (a
->class, b
->class))
571 case CLASS2 (rvc_zero
, rvc_zero
):
572 /* +-0 +/- +-0 = +0. */
576 case CLASS2 (rvc_zero
, rvc_normal
):
577 case CLASS2 (rvc_zero
, rvc_inf
):
578 case CLASS2 (rvc_zero
, rvc_nan
):
580 case CLASS2 (rvc_normal
, rvc_nan
):
581 case CLASS2 (rvc_inf
, rvc_nan
):
582 case CLASS2 (rvc_nan
, rvc_nan
):
583 /* ANY + NaN = NaN. */
584 case CLASS2 (rvc_normal
, rvc_inf
):
587 r
->sign
= sign
^ subtract_p
;
590 case CLASS2 (rvc_normal
, rvc_zero
):
591 case CLASS2 (rvc_inf
, rvc_zero
):
592 case CLASS2 (rvc_nan
, rvc_zero
):
594 case CLASS2 (rvc_nan
, rvc_normal
):
595 case CLASS2 (rvc_nan
, rvc_inf
):
596 /* NaN + ANY = NaN. */
597 case CLASS2 (rvc_inf
, rvc_normal
):
602 case CLASS2 (rvc_inf
, rvc_inf
):
604 /* Inf - Inf = NaN. */
605 get_canonical_qnan (r
, 0);
607 /* Inf + Inf = Inf. */
611 case CLASS2 (rvc_normal
, rvc_normal
):
618 /* Swap the arguments such that A has the larger exponent. */
619 dexp
= a
->exp
- b
->exp
;
622 const REAL_VALUE_TYPE
*t
;
629 /* If the exponents are not identical, we need to shift the
630 significand of B down. */
633 /* If the exponents are too far apart, the significands
634 do not overlap, which makes the subtraction a noop. */
635 if (dexp
>= SIGNIFICAND_BITS
)
642 sticky_rshift_significand (&t
, b
, dexp
);
648 if (sub_significands (r
, a
, b
))
650 /* We got a borrow out of the subtraction. That means that
651 A and B had the same exponent, and B had the larger
652 significand. We need to swap the sign and negate the
655 neg_significand (r
, r
);
660 if (add_significands (r
, a
, b
))
662 /* We got carry out of the addition. This means we need to
663 shift the significand back down one bit and increase the
665 sticky_rshift_significand (r
, r
, 1);
666 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
675 r
->class = rvc_normal
;
679 /* Re-normalize the result. */
682 /* Special case: if the subtraction results in zero, the result
684 if (r
->class == rvc_zero
)
688 /* Return R = A * B. */
691 do_multiply (r
, a
, b
)
693 const REAL_VALUE_TYPE
*a
, *b
;
695 REAL_VALUE_TYPE u
, t
, *rr
;
696 unsigned int i
, j
, k
;
697 int sign
= a
->sign
^ b
->sign
;
699 switch (CLASS2 (a
->class, b
->class))
701 case CLASS2 (rvc_zero
, rvc_zero
):
702 case CLASS2 (rvc_zero
, rvc_normal
):
703 case CLASS2 (rvc_normal
, rvc_zero
):
704 /* +-0 * ANY = 0 with appropriate sign. */
708 case CLASS2 (rvc_zero
, rvc_nan
):
709 case CLASS2 (rvc_normal
, rvc_nan
):
710 case CLASS2 (rvc_inf
, rvc_nan
):
711 case CLASS2 (rvc_nan
, rvc_nan
):
712 /* ANY * NaN = NaN. */
717 case CLASS2 (rvc_nan
, rvc_zero
):
718 case CLASS2 (rvc_nan
, rvc_normal
):
719 case CLASS2 (rvc_nan
, rvc_inf
):
720 /* NaN * ANY = NaN. */
725 case CLASS2 (rvc_zero
, rvc_inf
):
726 case CLASS2 (rvc_inf
, rvc_zero
):
728 get_canonical_qnan (r
, sign
);
731 case CLASS2 (rvc_inf
, rvc_inf
):
732 case CLASS2 (rvc_normal
, rvc_inf
):
733 case CLASS2 (rvc_inf
, rvc_normal
):
734 /* Inf * Inf = Inf, R * Inf = Inf */
739 case CLASS2 (rvc_normal
, rvc_normal
):
746 if (r
== a
|| r
== b
)
752 /* Collect all the partial products. Since we don't have sure access
753 to a widening multiply, we split each long into two half-words.
755 Consider the long-hand form of a four half-word multiplication:
765 We construct partial products of the widened half-word products
766 that are known to not overlap, e.g. DF+DH. Each such partial
767 product is given its proper exponent, which allows us to sum them
768 and obtain the finished product. */
770 for (i
= 0; i
< SIGSZ
* 2; ++i
)
772 unsigned long ai
= a
->sig
[i
/ 2];
774 ai
>>= HOST_BITS_PER_LONG
/ 2;
776 ai
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
781 for (j
= 0; j
< 2; ++j
)
783 int exp
= (a
->exp
- (2*SIGSZ
-1-i
)*(HOST_BITS_PER_LONG
/2)
784 + (b
->exp
- (1-j
)*(HOST_BITS_PER_LONG
/2)));
789 /* Would underflow to zero, which we shouldn't bother adding. */
792 u
.class = rvc_normal
;
796 for (k
= j
; k
< SIGSZ
* 2; k
+= 2)
798 unsigned long bi
= b
->sig
[k
/ 2];
800 bi
>>= HOST_BITS_PER_LONG
/ 2;
802 bi
&= ((unsigned long)1 << (HOST_BITS_PER_LONG
/ 2)) - 1;
804 u
.sig
[k
/ 2] = ai
* bi
;
808 do_add (rr
, rr
, &u
, 0);
817 /* Return R = A / B. */
822 const REAL_VALUE_TYPE
*a
, *b
;
824 int exp
, sign
= a
->sign
^ b
->sign
;
825 REAL_VALUE_TYPE t
, *rr
;
828 switch (CLASS2 (a
->class, b
->class))
830 case CLASS2 (rvc_zero
, rvc_zero
):
832 case CLASS2 (rvc_inf
, rvc_zero
):
834 case CLASS2 (rvc_inf
, rvc_inf
):
835 /* Inf / Inf = NaN. */
836 get_canonical_qnan (r
, sign
);
839 case CLASS2 (rvc_zero
, rvc_normal
):
840 case CLASS2 (rvc_zero
, rvc_inf
):
842 case CLASS2 (rvc_normal
, rvc_inf
):
848 case CLASS2 (rvc_normal
, rvc_zero
):
853 case CLASS2 (rvc_zero
, rvc_nan
):
854 case CLASS2 (rvc_normal
, rvc_nan
):
855 case CLASS2 (rvc_inf
, rvc_nan
):
856 case CLASS2 (rvc_nan
, rvc_nan
):
857 /* ANY / NaN = NaN. */
862 case CLASS2 (rvc_nan
, rvc_zero
):
863 case CLASS2 (rvc_nan
, rvc_normal
):
864 case CLASS2 (rvc_nan
, rvc_inf
):
865 /* NaN / ANY = NaN. */
870 case CLASS2 (rvc_inf
, rvc_normal
):
876 case CLASS2 (rvc_normal
, rvc_normal
):
883 if (r
== a
|| r
== b
)
888 rr
->class = rvc_normal
;
891 exp
= a
->exp
- b
->exp
+ 1;
898 inexact
= div_significands (rr
, a
, b
);
900 /* Re-normalize the result. */
902 rr
->sig
[0] |= inexact
;
908 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
909 one of the two operands is a NaN. */
912 do_compare (a
, b
, nan_result
)
913 const REAL_VALUE_TYPE
*a
, *b
;
918 switch (CLASS2 (a
->class, b
->class))
920 case CLASS2 (rvc_zero
, rvc_zero
):
921 /* Sign of zero doesn't matter for compares. */
924 case CLASS2 (rvc_inf
, rvc_zero
):
925 case CLASS2 (rvc_inf
, rvc_normal
):
926 case CLASS2 (rvc_normal
, rvc_zero
):
927 return (a
->sign
? -1 : 1);
929 case CLASS2 (rvc_inf
, rvc_inf
):
930 return -a
->sign
- -b
->sign
;
932 case CLASS2 (rvc_zero
, rvc_normal
):
933 case CLASS2 (rvc_zero
, rvc_inf
):
934 case CLASS2 (rvc_normal
, rvc_inf
):
935 return (b
->sign
? 1 : -1);
937 case CLASS2 (rvc_zero
, rvc_nan
):
938 case CLASS2 (rvc_normal
, rvc_nan
):
939 case CLASS2 (rvc_inf
, rvc_nan
):
940 case CLASS2 (rvc_nan
, rvc_nan
):
941 case CLASS2 (rvc_nan
, rvc_zero
):
942 case CLASS2 (rvc_nan
, rvc_normal
):
943 case CLASS2 (rvc_nan
, rvc_inf
):
946 case CLASS2 (rvc_normal
, rvc_normal
):
953 if (a
->sign
!= b
->sign
)
954 return -a
->sign
- -b
->sign
;
958 else if (a
->exp
< b
->exp
)
961 ret
= cmp_significands (a
, b
);
963 return (a
->sign
? -ret
: ret
);
966 /* Return A truncated to an integral value toward zero. */
971 const REAL_VALUE_TYPE
*a
;
984 get_zero (r
, r
->sign
);
985 else if (r
->exp
< SIGNIFICAND_BITS
)
986 clear_significand_below (r
, SIGNIFICAND_BITS
- r
->exp
);
994 /* Perform the binary or unary operation described by CODE.
995 For a unary operation, leave OP1 NULL. */
998 real_arithmetic (r
, icode
, op0
, op1
)
1001 const REAL_VALUE_TYPE
*op0
, *op1
;
1003 enum tree_code code
= icode
;
1008 do_add (r
, op0
, op1
, 0);
1012 do_add (r
, op0
, op1
, 1);
1016 do_multiply (r
, op0
, op1
);
1020 do_divide (r
, op0
, op1
);
1024 if (op1
->class == rvc_nan
)
1026 else if (do_compare (op0
, op1
, -1) < 0)
1033 if (op1
->class == rvc_nan
)
1035 else if (do_compare (op0
, op1
, 1) < 0)
1051 case FIX_TRUNC_EXPR
:
1052 do_fix_trunc (r
, op0
);
1060 /* Legacy. Similar, but return the result directly. */
1063 real_arithmetic2 (icode
, op0
, op1
)
1065 const REAL_VALUE_TYPE
*op0
, *op1
;
1068 real_arithmetic (&r
, icode
, op0
, op1
);
1073 real_compare (icode
, op0
, op1
)
1075 const REAL_VALUE_TYPE
*op0
, *op1
;
1077 enum tree_code code
= icode
;
1082 return do_compare (op0
, op1
, 1) < 0;
1084 return do_compare (op0
, op1
, 1) <= 0;
1086 return do_compare (op0
, op1
, -1) > 0;
1088 return do_compare (op0
, op1
, -1) >= 0;
1090 return do_compare (op0
, op1
, -1) == 0;
1092 return do_compare (op0
, op1
, -1) != 0;
1093 case UNORDERED_EXPR
:
1094 return op0
->class == rvc_nan
|| op1
->class == rvc_nan
;
1096 return op0
->class != rvc_nan
&& op1
->class != rvc_nan
;
1098 return do_compare (op0
, op1
, -1) < 0;
1100 return do_compare (op0
, op1
, -1) <= 0;
1102 return do_compare (op0
, op1
, 1) > 0;
1104 return do_compare (op0
, op1
, 1) >= 0;
1106 return do_compare (op0
, op1
, 0) == 0;
1113 /* Return floor log2(R). */
1117 const REAL_VALUE_TYPE
*r
;
1125 return (unsigned int)-1 >> 1;
1133 /* R = OP0 * 2**EXP. */
1136 real_ldexp (r
, op0
, exp
)
1138 const REAL_VALUE_TYPE
*op0
;
1152 get_inf (r
, r
->sign
);
1153 else if (exp
< -MAX_EXP
)
1154 get_zero (r
, r
->sign
);
1164 /* Determine whether a floating-point value X is infinite. */
1168 const REAL_VALUE_TYPE
*r
;
1170 return (r
->class == rvc_inf
);
1173 /* Determine whether a floating-point value X is a NaN. */
1177 const REAL_VALUE_TYPE
*r
;
1179 return (r
->class == rvc_nan
);
1182 /* Determine whether a floating-point value X is negative. */
1186 const REAL_VALUE_TYPE
*r
;
1191 /* Determine whether a floating-point value X is minus zero. */
1195 const REAL_VALUE_TYPE
*r
;
1197 return r
->sign
&& r
->class == rvc_zero
;
1200 /* Compare two floating-point objects for bitwise identity. */
1203 real_identical (a
, b
)
1204 const REAL_VALUE_TYPE
*a
, *b
;
1208 if (a
->class != b
->class)
1210 if (a
->sign
!= b
->sign
)
1220 if (a
->exp
!= b
->exp
)
1224 for (i
= 0; i
< SIGSZ
; ++i
)
1225 if (a
->sig
[i
] != b
->sig
[i
])
1236 /* Try to change R into its exact multiplicative inverse in machine
1237 mode MODE. Return true if successful. */
1240 exact_real_inverse (mode
, r
)
1241 enum machine_mode mode
;
1244 const REAL_VALUE_TYPE
*one
= real_digit (1);
1248 if (r
->class != rvc_normal
)
1251 /* Check for a power of two: all significand bits zero except the MSB. */
1252 for (i
= 0; i
< SIGSZ
-1; ++i
)
1255 if (r
->sig
[SIGSZ
-1] != SIG_MSB
)
1258 /* Find the inverse and truncate to the required mode. */
1259 do_divide (&u
, one
, r
);
1260 real_convert (&u
, mode
, &u
);
1262 /* The rounding may have overflowed. */
1263 if (u
.class != rvc_normal
)
1265 for (i
= 0; i
< SIGSZ
-1; ++i
)
1268 if (u
.sig
[SIGSZ
-1] != SIG_MSB
)
1275 /* Render R as an integer. */
1279 const REAL_VALUE_TYPE
*r
;
1281 unsigned HOST_WIDE_INT i
;
1292 i
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1300 if (r
->exp
> HOST_BITS_PER_WIDE_INT
)
1303 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1304 i
= r
->sig
[SIGSZ
-1];
1305 else if (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
)
1307 i
= r
->sig
[SIGSZ
-1];
1308 i
= i
<< (HOST_BITS_PER_LONG
- 1) << 1;
1309 i
|= r
->sig
[SIGSZ
-2];
1314 i
>>= HOST_BITS_PER_WIDE_INT
- r
->exp
;
1325 /* Likewise, but to an integer pair, HI+LOW. */
1328 real_to_integer2 (plow
, phigh
, r
)
1329 HOST_WIDE_INT
*plow
, *phigh
;
1330 const REAL_VALUE_TYPE
*r
;
1333 HOST_WIDE_INT low
, high
;
1346 high
= (unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1);
1360 if (exp
>= 2*HOST_BITS_PER_WIDE_INT
)
1363 rshift_significand (&t
, r
, 2*HOST_BITS_PER_WIDE_INT
- exp
);
1364 if (HOST_BITS_PER_WIDE_INT
== HOST_BITS_PER_LONG
)
1366 high
= t
.sig
[SIGSZ
-1];
1367 low
= t
.sig
[SIGSZ
-2];
1369 else if (HOST_BITS_PER_WIDE_INT
== 2*HOST_BITS_PER_LONG
)
1371 high
= t
.sig
[SIGSZ
-1];
1372 high
= high
<< (HOST_BITS_PER_LONG
- 1) << 1;
1373 high
|= t
.sig
[SIGSZ
-2];
1375 low
= t
.sig
[SIGSZ
-3];
1376 low
= low
<< (HOST_BITS_PER_LONG
- 1) << 1;
1377 low
|= t
.sig
[SIGSZ
-4];
1387 low
= -low
, high
= ~high
;
1399 /* Render R as a decimal floating point constant. Emit DIGITS significant
1400 digits in the result. If DIGITS <= 0, choose the maximum for the
1401 representation. If DIGITS < 0, strip trailing zeros. */
1403 #define M_LOG10_2 0.30102999566398119521
1406 real_to_decimal (str
, r_orig
, digits
)
1408 const REAL_VALUE_TYPE
*r_orig
;
1412 const REAL_VALUE_TYPE
*one
, *ten
;
1413 int dec_exp
, max_digits
, d
, cmp_half
;
1414 char *p
, *first
, *last
;
1416 bool crop_trailing_zeros
;
1422 strcpy (str
, (r
.sign
? "-0.0" : "0.0"));
1427 strcpy (str
, (r
.sign
? "-Inf" : "+Inf"));
1430 /* ??? Print the significand as well, if not canonical? */
1431 strcpy (str
, (r
.sign
? "-NaN" : "+NaN"));
1437 max_digits
= SIGNIFICAND_BITS
* M_LOG10_2
;
1438 crop_trailing_zeros
= digits
< 0;
1439 if (digits
<= 0 || digits
> max_digits
)
1440 digits
= max_digits
;
1442 one
= real_digit (1);
1443 ten
= ten_to_ptwo (0);
1448 /* Estimate the decimal exponent. */
1449 dec_exp
= r
.exp
* M_LOG10_2
;
1451 /* Scale the number such that it is in [1, 10). */
1452 times_pten (&r
, (dec_exp
> 0 ? -dec_exp
: -(--dec_exp
)));
1454 /* Assert that the number is in the proper range. Round-off can
1455 prevent the above from working exactly. */
1456 if (do_compare (&r
, one
, -1) < 0)
1458 do_multiply (&r
, &r
, ten
);
1461 else if (do_compare (&r
, ten
, 1) >= 0)
1463 do_divide (&r
, &r
, ten
);
1473 d
= real_to_integer ((const REAL_VALUE_TYPE
*) &r
);
1474 do_add (&r
, &r
, real_digit (d
), 1);
1479 do_multiply (&r
, &r
, ten
);
1483 /* Round the result. Compare R vs 0.5 by doing R*2 vs 1.0. */
1485 cmp_half
= do_compare (&r
, one
, -1);
1487 /* Round to even. */
1510 first
[0] = first
[1];
1513 if (crop_trailing_zeros
)
1514 while (last
> first
+ 3 && last
[-1] == '0')
1517 sprintf (last
, "e%+d", dec_exp
);
1520 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1521 significant digits in the result. If DIGITS <= 0, choose the maximum
1522 for the representation. If DIGITS < 0, strip trailing zeros. */
1525 real_to_hexadecimal (str
, r
, digits
)
1527 const REAL_VALUE_TYPE
*r
;
1530 int i
, j
, exp
= r
->exp
;
1532 bool crop_trailing_zeros
;
1542 strcpy (str
, (r
->sign
? "-Inf" : "+Inf"));
1545 /* ??? Print the significand as well, if not canonical? */
1546 strcpy (str
, (r
->sign
? "-NaN" : "+NaN"));
1552 crop_trailing_zeros
= digits
< 0;
1554 digits
= SIGNIFICAND_BITS
/ 4;
1565 for (i
= SIGSZ
- 1; i
>= 0; --i
)
1566 for (j
= HOST_BITS_PER_LONG
- 4; j
>= 0; j
-= 4)
1568 *p
++ = "0123456789abcdef"[(r
->sig
[i
] >> j
) & 15];
1574 if (crop_trailing_zeros
)
1575 while (p
> first
+ 2 && p
[-1] == '0')
1578 sprintf (p
, "p%+d", exp
);
1581 /* Initialize R from a decimal or hexadecimal string. The string is
1582 assumed to have been syntax checked already. */
1585 real_from_string (r
, str
)
1599 else if (*str
== '+')
1602 if (str
[0] == '0' && str
[1] == 'x')
1604 /* Hexadecimal floating point. */
1605 int pos
= SIGNIFICAND_BITS
- 4, d
;
1613 d
= hex_value (*str
);
1618 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1619 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1628 if (pos
== SIGNIFICAND_BITS
- 4)
1635 d
= hex_value (*str
);
1640 r
->sig
[pos
/ HOST_BITS_PER_LONG
]
1641 |= (unsigned long) d
<< (pos
% HOST_BITS_PER_LONG
);
1647 if (*str
== 'p' || *str
== 'P')
1649 bool exp_neg
= false;
1657 else if (*str
== '+')
1661 while (ISDIGIT (*str
))
1667 /* Overflowed the exponent. */
1681 r
->class = rvc_normal
;
1688 /* Decimal floating point. */
1689 const REAL_VALUE_TYPE
*ten
= ten_to_ptwo (0);
1694 while (ISDIGIT (*str
))
1697 do_multiply (r
, r
, ten
);
1699 do_add (r
, r
, real_digit (d
), 0);
1704 if (r
->class == rvc_zero
)
1709 while (ISDIGIT (*str
))
1712 do_multiply (r
, r
, ten
);
1714 do_add (r
, r
, real_digit (d
), 0);
1719 if (*str
== 'e' || *str
== 'E')
1721 bool exp_neg
= false;
1729 else if (*str
== '+')
1733 while (ISDIGIT (*str
))
1739 /* Overflowed the exponent. */
1753 times_pten (r
, exp
);
1768 /* Legacy. Similar, but return the result directly. */
1771 real_from_string2 (s
, mode
)
1773 enum machine_mode mode
;
1777 real_from_string (&r
, s
);
1778 if (mode
!= VOIDmode
)
1779 real_convert (&r
, mode
, &r
);
1784 /* Initialize R from the integer pair HIGH+LOW. */
1787 real_from_integer (r
, mode
, low
, high
, unsigned_p
)
1789 enum machine_mode mode
;
1790 unsigned HOST_WIDE_INT low
;
1794 if (low
== 0 && high
== 0)
1798 r
->class = rvc_normal
;
1799 r
->sign
= high
< 0 && !unsigned_p
;
1800 r
->exp
= 2 * HOST_BITS_PER_WIDE_INT
;
1811 if (HOST_BITS_PER_LONG
== HOST_BITS_PER_WIDE_INT
)
1813 r
->sig
[SIGSZ
-1] = high
;
1814 r
->sig
[SIGSZ
-2] = low
;
1815 memset (r
->sig
, 0, sizeof(long)*(SIGSZ
-2));
1817 else if (HOST_BITS_PER_LONG
*2 == HOST_BITS_PER_WIDE_INT
)
1819 r
->sig
[SIGSZ
-1] = high
>> (HOST_BITS_PER_LONG
- 1) >> 1;
1820 r
->sig
[SIGSZ
-2] = high
;
1821 r
->sig
[SIGSZ
-3] = low
>> (HOST_BITS_PER_LONG
- 1) >> 1;
1822 r
->sig
[SIGSZ
-4] = low
;
1824 memset (r
->sig
, 0, sizeof(long)*(SIGSZ
-4));
1832 if (mode
!= VOIDmode
)
1833 real_convert (r
, mode
, r
);
1836 /* Returns 10**2**n. */
1838 static const REAL_VALUE_TYPE
*
1842 static REAL_VALUE_TYPE tens
[EXP_BITS
];
1844 if (n
< 0 || n
>= EXP_BITS
)
1847 if (tens
[n
].class == rvc_zero
)
1849 if (n
< (HOST_BITS_PER_WIDE_INT
== 64 ? 5 : 4))
1851 HOST_WIDE_INT t
= 10;
1854 for (i
= 0; i
< n
; ++i
)
1857 real_from_integer (&tens
[n
], VOIDmode
, t
, 0, 1);
1861 const REAL_VALUE_TYPE
*t
= ten_to_ptwo (n
- 1);
1862 do_multiply (&tens
[n
], t
, t
);
1871 static const REAL_VALUE_TYPE
*
1875 static REAL_VALUE_TYPE num
[10];
1880 if (n
> 0 && num
[n
].class == rvc_zero
)
1881 real_from_integer (&num
[n
], VOIDmode
, n
, 0, 1);
1886 /* Multiply R by 10**EXP. */
1893 REAL_VALUE_TYPE pten
, *rr
;
1894 bool negative
= (exp
< 0);
1900 pten
= *real_digit (1);
1906 for (i
= 0; exp
> 0; ++i
, exp
>>= 1)
1908 do_multiply (rr
, rr
, ten_to_ptwo (i
));
1911 do_divide (r
, r
, &pten
);
1914 /* Fills R with +Inf. */
1923 /* Fills R with a NaN whose significand is described by STR. If QUIET,
1924 we force a QNaN, else we force an SNaN. The string, if not empty,
1925 is parsed as a number and placed in the significand. Return true
1926 if the string was successfully parsed. */
1929 real_nan (r
, str
, quiet
, mode
)
1933 enum machine_mode mode
;
1935 const struct real_format
*fmt
;
1937 fmt
= real_format_for_mode
[mode
- QFmode
];
1944 get_canonical_qnan (r
, 0);
1946 get_canonical_snan (r
, 0);
1953 memset (r
, 0, sizeof (*r
));
1956 /* Parse akin to strtol into the significand of R. */
1958 while (ISSPACE (*str
))
1962 else if (*str
== '+')
1972 while ((d
= hex_value (*str
)) < base
)
1979 lshift_significand (r
, r
, 3);
1982 lshift_significand (r
, r
, 4);
1985 lshift_significand_1 (&u
, r
);
1986 lshift_significand (r
, r
, 3);
1987 add_significands (r
, r
, &u
);
1995 add_significands (r
, r
, &u
);
2000 /* Must have consumed the entire string for success. */
2004 /* Shift the significand into place such that the bits
2005 are in the most significant bits for the format. */
2006 lshift_significand (r
, r
, SIGNIFICAND_BITS
- fmt
->p
);
2008 /* Our MSB is always unset for NaNs. */
2009 r
->sig
[SIGSZ
-1] &= ~SIG_MSB
;
2011 /* Force quiet or signalling NaN. */
2013 r
->sig
[SIGSZ
-1] |= SIG_MSB
>> 1;
2015 r
->sig
[SIGSZ
-1] &= ~(SIG_MSB
>> 1);
2017 /* Force at least one bit of the significand set. */
2018 for (d
= 0; d
< SIGSZ
; ++d
)
2022 r
->sig
[SIGSZ
-1] |= SIG_MSB
>> 2;
2024 /* Our intermediate format forces QNaNs to have MSB-1 set.
2025 If the target format has QNaNs with the top bit unset,
2026 mirror the output routines and invert the top two bits. */
2027 if (!fmt
->qnan_msb_set
)
2028 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1) | (SIG_MSB
>> 2);
2034 /* Fills R with 2**N. */
2041 memset (r
, 0, sizeof (*r
));
2046 else if (n
< -MAX_EXP
)
2050 r
->class = rvc_normal
;
2052 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2058 round_for_format (fmt
, r
)
2059 const struct real_format
*fmt
;
2063 unsigned long sticky
;
2067 p2
= fmt
->p
* fmt
->log2_b
;
2068 emin2m1
= (fmt
->emin
- 1) * fmt
->log2_b
;
2069 emax2
= fmt
->emax
* fmt
->log2_b
;
2071 np2
= SIGNIFICAND_BITS
- p2
;
2075 get_zero (r
, r
->sign
);
2077 if (!fmt
->has_signed_zero
)
2082 get_inf (r
, r
->sign
);
2087 clear_significand_below (r
, np2
);
2089 /* If we've cleared the entire significand, we need one bit
2090 set for this to continue to be a NaN. */
2091 for (i
= 0; i
< SIGSZ
; ++i
)
2095 r
->sig
[SIGSZ
-1] = SIG_MSB
>> 2;
2105 /* If we're not base2, normalize the exponent to a multiple of
2107 if (fmt
->log2_b
!= 1)
2109 int shift
= r
->exp
& (fmt
->log2_b
- 1);
2112 shift
= fmt
->log2_b
- shift
;
2113 sticky_rshift_significand (r
, r
, shift
);
2118 /* Check the range of the exponent. If we're out of range,
2119 either underflow or overflow. */
2122 else if (r
->exp
<= emin2m1
)
2126 if (!fmt
->has_denorm
)
2128 /* Don't underflow completely until we've had a chance to round. */
2129 if (r
->exp
< emin2m1
)
2134 diff
= emin2m1
- r
->exp
+ 1;
2138 /* De-normalize the significand. */
2139 sticky_rshift_significand (r
, r
, diff
);
2144 /* There are P2 true significand bits, followed by one guard bit,
2145 followed by one sticky bit, followed by stuff. Fold nonzero
2146 stuff into the sticky bit. */
2149 for (i
= 0, w
= (np2
- 1) / HOST_BITS_PER_LONG
; i
< w
; ++i
)
2150 sticky
|= r
->sig
[i
];
2152 r
->sig
[w
] & (((unsigned long)1 << ((np2
- 1) % HOST_BITS_PER_LONG
)) - 1);
2154 guard
= test_significand_bit (r
, np2
- 1);
2155 lsb
= test_significand_bit (r
, np2
);
2157 /* Round to even. */
2158 if (guard
&& (sticky
|| lsb
))
2162 set_significand_bit (&u
, np2
);
2164 if (add_significands (r
, r
, &u
))
2166 /* Overflow. Means the significand had been all ones, and
2167 is now all zeros. Need to increase the exponent, and
2168 possibly re-normalize it. */
2169 if (++r
->exp
> emax2
)
2171 r
->sig
[SIGSZ
-1] = SIG_MSB
;
2173 if (fmt
->log2_b
!= 1)
2175 int shift
= r
->exp
& (fmt
->log2_b
- 1);
2178 shift
= fmt
->log2_b
- shift
;
2179 sticky_rshift_significand (r
, r
, shift
);
2188 /* Catch underflow that we deferred until after rounding. */
2189 if (r
->exp
<= emin2m1
)
2192 /* Clear out trailing garbage. */
2193 clear_significand_below (r
, np2
);
2196 /* Extend or truncate to a new mode. */
2199 real_convert (r
, mode
, a
)
2201 enum machine_mode mode
;
2202 const REAL_VALUE_TYPE
*a
;
2204 const struct real_format
*fmt
;
2206 fmt
= real_format_for_mode
[mode
- QFmode
];
2211 round_for_format (fmt
, r
);
2213 /* round_for_format de-normalizes denormals. Undo just that part. */
2214 if (r
->class == rvc_normal
)
2218 /* Legacy. Likewise, except return the struct directly. */
2221 real_value_truncate (mode
, a
)
2222 enum machine_mode mode
;
2226 real_convert (&r
, mode
, &a
);
2230 /* Return true if truncating to MODE is exact. */
2233 exact_real_truncate (mode
, a
)
2234 enum machine_mode mode
;
2235 const REAL_VALUE_TYPE
*a
;
2238 real_convert (&t
, mode
, a
);
2239 return real_identical (&t
, a
);
2242 /* Write R to the given target format. Place the words of the result
2243 in target word order in BUF. There are always 32 bits in each
2244 long, no matter the size of the host long.
2246 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2249 real_to_target_fmt (buf
, r_orig
, fmt
)
2251 const REAL_VALUE_TYPE
*r_orig
;
2252 const struct real_format
*fmt
;
2258 round_for_format (fmt
, &r
);
2262 (*fmt
->encode
) (fmt
, buf
, &r
);
2267 /* Similar, but look up the format from MODE. */
2270 real_to_target (buf
, r
, mode
)
2272 const REAL_VALUE_TYPE
*r
;
2273 enum machine_mode mode
;
2275 const struct real_format
*fmt
;
2277 fmt
= real_format_for_mode
[mode
- QFmode
];
2281 return real_to_target_fmt (buf
, r
, fmt
);
2284 /* Read R from the given target format. Read the words of the result
2285 in target word order in BUF. There are always 32 bits in each
2286 long, no matter the size of the host long. */
2289 real_from_target_fmt (r
, buf
, fmt
)
2292 const struct real_format
*fmt
;
2294 (*fmt
->decode
) (fmt
, r
, buf
);
2297 /* Similar, but look up the format from MODE. */
2300 real_from_target (r
, buf
, mode
)
2303 enum machine_mode mode
;
2305 const struct real_format
*fmt
;
2307 fmt
= real_format_for_mode
[mode
- QFmode
];
2311 (*fmt
->decode
) (fmt
, r
, buf
);
2314 /* Return the number of bits in the significand for MODE. */
2315 /* ??? Legacy. Should get access to real_format directly. */
2318 significand_size (mode
)
2319 enum machine_mode mode
;
2321 const struct real_format
*fmt
;
2323 fmt
= real_format_for_mode
[mode
- QFmode
];
2327 return fmt
->p
* fmt
->log2_b
;
2330 /* Return a hash value for the given real value. */
2331 /* ??? The "unsigned int" return value is intended to be hashval_t,
2332 but I didn't want to pull hashtab.h into real.h. */
2336 const REAL_VALUE_TYPE
*r
;
2341 h
= r
->class | (r
->sign
<< 2);
2353 if (sizeof(unsigned long) > sizeof(unsigned int))
2354 for (i
= 0; i
< SIGSZ
; ++i
)
2356 unsigned long s
= r
->sig
[i
];
2357 h
^= s
^ (s
>> (HOST_BITS_PER_LONG
/ 2));
2360 for (i
= 0; i
< SIGSZ
; ++i
)
2371 /* IEEE single-precision format. */
2373 static void encode_ieee_single
PARAMS ((const struct real_format
*fmt
,
2374 long *, const REAL_VALUE_TYPE
*));
2375 static void decode_ieee_single
PARAMS ((const struct real_format
*,
2376 REAL_VALUE_TYPE
*, const long *));
2379 encode_ieee_single (fmt
, buf
, r
)
2380 const struct real_format
*fmt
;
2382 const REAL_VALUE_TYPE
*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;
2434 decode_ieee_single (fmt
, r
, buf
)
2435 const struct real_format
*fmt
;
2439 unsigned long image
= buf
[0] & 0xffffffff;
2440 bool sign
= (image
>> 31) & 1;
2441 int exp
= (image
>> 23) & 0xff;
2443 memset (r
, 0, sizeof (*r
));
2444 image
<<= HOST_BITS_PER_LONG
- 24;
2449 if (image
&& fmt
->has_denorm
)
2451 r
->class = rvc_normal
;
2454 r
->sig
[SIGSZ
-1] = image
<< 1;
2457 else if (fmt
->has_signed_zero
)
2460 else if (exp
== 255 && (fmt
->has_nans
|| fmt
->has_inf
))
2466 if (!fmt
->qnan_msb_set
)
2467 image
^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
2468 r
->sig
[SIGSZ
-1] = image
;
2478 r
->class = rvc_normal
;
2480 r
->exp
= exp
- 127 + 1;
2481 r
->sig
[SIGSZ
-1] = image
| SIG_MSB
;
2485 const struct real_format ieee_single_format
=
2502 /* IEEE double-precision format. */
2504 static void encode_ieee_double
PARAMS ((const struct real_format
*fmt
,
2505 long *, const REAL_VALUE_TYPE
*));
2506 static void decode_ieee_double
PARAMS ((const struct real_format
*,
2507 REAL_VALUE_TYPE
*, const long *));
2510 encode_ieee_double (fmt
, buf
, r
)
2511 const struct real_format
*fmt
;
2513 const REAL_VALUE_TYPE
*r
;
2515 unsigned long image_lo
, image_hi
, sig_lo
, sig_hi
, exp
;
2516 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2518 image_hi
= r
->sign
<< 31;
2521 if (HOST_BITS_PER_LONG
== 64)
2523 sig_hi
= r
->sig
[SIGSZ
-1];
2524 sig_lo
= (sig_hi
>> (64 - 53)) & 0xffffffff;
2525 sig_hi
= (sig_hi
>> (64 - 53 + 1) >> 31) & 0xfffff;
2529 sig_hi
= r
->sig
[SIGSZ
-1];
2530 sig_lo
= r
->sig
[SIGSZ
-2];
2531 sig_lo
= (sig_hi
<< 21) | (sig_lo
>> 11);
2532 sig_hi
= (sig_hi
>> 11) & 0xfffff;
2542 image_hi
|= 2047 << 20;
2545 image_hi
|= 0x7fffffff;
2546 image_lo
= 0xffffffff;
2553 image_hi
|= 2047 << 20;
2555 if (!fmt
->qnan_msb_set
)
2556 image_hi
^= 1 << 19 | 1 << 18;
2561 image_hi
|= 0x7fffffff;
2562 image_lo
= 0xffffffff;
2567 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2568 whereas the intermediate representation is 0.F x 2**exp.
2569 Which means we're off by one. */
2573 exp
= r
->exp
+ 1023 - 1;
2574 image_hi
|= exp
<< 20;
2583 if (FLOAT_WORDS_BIG_ENDIAN
)
2584 buf
[0] = image_hi
, buf
[1] = image_lo
;
2586 buf
[0] = image_lo
, buf
[1] = image_hi
;
2590 decode_ieee_double (fmt
, r
, buf
)
2591 const struct real_format
*fmt
;
2595 unsigned long image_hi
, image_lo
;
2599 if (FLOAT_WORDS_BIG_ENDIAN
)
2600 image_hi
= buf
[0], image_lo
= buf
[1];
2602 image_lo
= buf
[0], image_hi
= buf
[1];
2603 image_lo
&= 0xffffffff;
2604 image_hi
&= 0xffffffff;
2606 sign
= (image_hi
>> 31) & 1;
2607 exp
= (image_hi
>> 20) & 0x7ff;
2609 memset (r
, 0, sizeof (*r
));
2611 image_hi
<<= 32 - 21;
2612 image_hi
|= image_lo
>> 21;
2613 image_hi
&= 0x7fffffff;
2614 image_lo
<<= 32 - 21;
2618 if ((image_hi
|| image_lo
) && fmt
->has_denorm
)
2620 r
->class = rvc_normal
;
2623 if (HOST_BITS_PER_LONG
== 32)
2625 image_hi
= (image_hi
<< 1) | (image_lo
>> 31);
2627 r
->sig
[SIGSZ
-1] = image_hi
;
2628 r
->sig
[SIGSZ
-2] = image_lo
;
2632 image_hi
= (image_hi
<< 31 << 2) | (image_lo
<< 1);
2633 r
->sig
[SIGSZ
-1] = image_hi
;
2637 else if (fmt
->has_signed_zero
)
2640 else if (exp
== 2047 && (fmt
->has_nans
|| fmt
->has_inf
))
2642 if (image_hi
|| image_lo
)
2646 if (HOST_BITS_PER_LONG
== 32)
2648 r
->sig
[SIGSZ
-1] = image_hi
;
2649 r
->sig
[SIGSZ
-2] = image_lo
;
2652 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
;
2654 if (!fmt
->qnan_msb_set
)
2655 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
2665 r
->class = rvc_normal
;
2667 r
->exp
= exp
- 1023 + 1;
2668 if (HOST_BITS_PER_LONG
== 32)
2670 r
->sig
[SIGSZ
-1] = image_hi
| SIG_MSB
;
2671 r
->sig
[SIGSZ
-2] = image_lo
;
2674 r
->sig
[SIGSZ
-1] = (image_hi
<< 31 << 1) | image_lo
| SIG_MSB
;
2678 const struct real_format ieee_double_format
=
2695 /* IEEE extended double precision format. This comes in three
2696 flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2699 static void encode_ieee_extended
PARAMS ((const struct real_format
*fmt
,
2700 long *, const REAL_VALUE_TYPE
*));
2701 static void decode_ieee_extended
PARAMS ((const struct real_format
*,
2702 REAL_VALUE_TYPE
*, const long *));
2704 static void encode_ieee_extended_128
PARAMS ((const struct real_format
*fmt
,
2706 const REAL_VALUE_TYPE
*));
2707 static void decode_ieee_extended_128
PARAMS ((const struct real_format
*,
2712 encode_ieee_extended (fmt
, buf
, r
)
2713 const struct real_format
*fmt
;
2715 const REAL_VALUE_TYPE
*r
;
2717 unsigned long image_hi
, sig_hi
, sig_lo
;
2718 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
2720 image_hi
= r
->sign
<< 15;
2721 sig_hi
= sig_lo
= 0;
2733 /* Intel requires the explicit integer bit to be set, otherwise
2734 it considers the value a "pseudo-infinity". Motorola docs
2735 say it doesn't care. */
2736 sig_hi
= 0x80000000;
2741 sig_lo
= sig_hi
= 0xffffffff;
2749 if (HOST_BITS_PER_LONG
== 32)
2751 sig_hi
= r
->sig
[SIGSZ
-1];
2752 sig_lo
= r
->sig
[SIGSZ
-2];
2756 sig_lo
= r
->sig
[SIGSZ
-1];
2757 sig_hi
= sig_lo
>> 31 >> 1;
2758 sig_lo
&= 0xffffffff;
2760 if (!fmt
->qnan_msb_set
)
2761 sig_hi
^= 1 << 30 | 1 << 29;
2763 /* Intel requires the explicit integer bit to be set, otherwise
2764 it considers the value a "pseudo-nan". Motorola docs say it
2766 sig_hi
|= 0x80000000;
2771 sig_lo
= sig_hi
= 0xffffffff;
2779 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2780 whereas the intermediate representation is 0.F x 2**exp.
2781 Which means we're off by one.
2783 Except for Motorola, which consider exp=0 and explicit
2784 integer bit set to continue to be normalized. In theory
2785 this descrepency has been taken care of by the difference
2786 in fmt->emin in round_for_format. */
2798 if (HOST_BITS_PER_LONG
== 32)
2800 sig_hi
= r
->sig
[SIGSZ
-1];
2801 sig_lo
= r
->sig
[SIGSZ
-2];
2805 sig_lo
= r
->sig
[SIGSZ
-1];
2806 sig_hi
= sig_lo
>> 31 >> 1;
2807 sig_lo
&= 0xffffffff;
2816 if (FLOAT_WORDS_BIG_ENDIAN
)
2817 buf
[0] = image_hi
<< 16, buf
[1] = sig_hi
, buf
[2] = sig_lo
;
2819 buf
[0] = sig_lo
, buf
[1] = sig_hi
, buf
[2] = image_hi
;
2823 encode_ieee_extended_128 (fmt
, buf
, r
)
2824 const struct real_format
*fmt
;
2826 const REAL_VALUE_TYPE
*r
;
2828 buf
[3 * !FLOAT_WORDS_BIG_ENDIAN
] = 0;
2829 encode_ieee_extended (fmt
, buf
+!!FLOAT_WORDS_BIG_ENDIAN
, r
);
2833 decode_ieee_extended (fmt
, r
, buf
)
2834 const struct real_format
*fmt
;
2838 unsigned long image_hi
, sig_hi
, sig_lo
;
2842 if (FLOAT_WORDS_BIG_ENDIAN
)
2843 image_hi
= buf
[0] >> 16, sig_hi
= buf
[1], sig_lo
= buf
[2];
2845 sig_lo
= buf
[0], sig_hi
= buf
[1], image_hi
= buf
[2];
2846 sig_lo
&= 0xffffffff;
2847 sig_hi
&= 0xffffffff;
2848 image_hi
&= 0xffffffff;
2850 sign
= (image_hi
>> 15) & 1;
2851 exp
= image_hi
& 0x7fff;
2853 memset (r
, 0, sizeof (*r
));
2857 if ((sig_hi
|| sig_lo
) && fmt
->has_denorm
)
2859 r
->class = rvc_normal
;
2862 /* When the IEEE format contains a hidden bit, we know that
2863 it's zero at this point, and so shift up the significand
2864 and decrease the exponent to match. In this case, Motorola
2865 defines the explicit integer bit to be valid, so we don't
2866 know whether the msb is set or not. */
2868 if (HOST_BITS_PER_LONG
== 32)
2870 r
->sig
[SIGSZ
-1] = sig_hi
;
2871 r
->sig
[SIGSZ
-2] = sig_lo
;
2874 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
2878 else if (fmt
->has_signed_zero
)
2881 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
2883 /* See above re "pseudo-infinities" and "pseudo-nans".
2884 Short summary is that the MSB will likely always be
2885 set, and that we don't care about it. */
2886 sig_hi
&= 0x7fffffff;
2888 if (sig_hi
|| sig_lo
)
2892 if (HOST_BITS_PER_LONG
== 32)
2894 r
->sig
[SIGSZ
-1] = sig_hi
;
2895 r
->sig
[SIGSZ
-2] = sig_lo
;
2898 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
2900 if (!fmt
->qnan_msb_set
)
2901 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
2911 r
->class = rvc_normal
;
2913 r
->exp
= exp
- 16383 + 1;
2914 if (HOST_BITS_PER_LONG
== 32)
2916 r
->sig
[SIGSZ
-1] = sig_hi
;
2917 r
->sig
[SIGSZ
-2] = sig_lo
;
2920 r
->sig
[SIGSZ
-1] = (sig_hi
<< 31 << 1) | sig_lo
;
2925 decode_ieee_extended_128 (fmt
, r
, buf
)
2926 const struct real_format
*fmt
;
2930 decode_ieee_extended (fmt
, r
, buf
+!!FLOAT_WORDS_BIG_ENDIAN
);
2933 const struct real_format ieee_extended_motorola_format
=
2935 encode_ieee_extended
,
2936 decode_ieee_extended
,
2949 const struct real_format ieee_extended_intel_96_format
=
2951 encode_ieee_extended
,
2952 decode_ieee_extended
,
2965 const struct real_format ieee_extended_intel_128_format
=
2967 encode_ieee_extended_128
,
2968 decode_ieee_extended_128
,
2982 /* IBM 128-bit extended precision format: a pair of IEEE double precision
2983 numbers whose sum is equal to the extended precision value. The number
2984 with greater magnitude is first. This format has the same magnitude
2985 range as an IEEE double precision value, but effectively 106 bits of
2986 significand precision. Infinity and NaN are represented by their IEEE
2987 double precision value stored in the first number, the second number is
2988 ignored. Zeroes, Infinities, and NaNs are set in both doubles
2989 due to precedent. */
2991 static void encode_ibm_extended
PARAMS ((const struct real_format
*fmt
,
2992 long *, const REAL_VALUE_TYPE
*));
2993 static void decode_ibm_extended
PARAMS ((const struct real_format
*,
2994 REAL_VALUE_TYPE
*, const long *));
2997 encode_ibm_extended (fmt
, buf
, r
)
2998 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3000 const REAL_VALUE_TYPE
*r
;
3002 REAL_VALUE_TYPE u
, v
;
3007 /* Both doubles have sign bit set. */
3008 buf
[0] = FLOAT_WORDS_BIG_ENDIAN
? r
->sign
<< 31 : 0;
3009 buf
[1] = FLOAT_WORDS_BIG_ENDIAN
? 0 : r
->sign
<< 31;
3016 /* Both doubles set to Inf / NaN. */
3017 encode_ieee_double (&ieee_double_format
, &buf
[0], r
);
3023 /* u = IEEE double precision portion of significand. */
3025 clear_significand_below (&u
, SIGNIFICAND_BITS
- 53);
3027 /* v = remainder containing additional 53 bits of significand. */
3028 do_add (&v
, r
, &u
, 1);
3030 encode_ieee_double (&ieee_double_format
, &buf
[0], &u
);
3031 encode_ieee_double (&ieee_double_format
, &buf
[2], &v
);
3040 decode_ibm_extended (fmt
, r
, buf
)
3041 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3045 REAL_VALUE_TYPE u
, v
;
3047 decode_ieee_double (&ieee_double_format
, &u
, &buf
[0]);
3049 if (u
.class != rvc_zero
&& u
.class != rvc_inf
&& u
.class != rvc_nan
)
3051 decode_ieee_double (&ieee_double_format
, &v
, &buf
[2]);
3052 do_add (r
, &u
, &v
, 0);
3058 const struct real_format ibm_extended_format
=
3060 encode_ibm_extended
,
3061 decode_ibm_extended
,
3075 /* IEEE quad precision format. */
3077 static void encode_ieee_quad
PARAMS ((const struct real_format
*fmt
,
3078 long *, const REAL_VALUE_TYPE
*));
3079 static void decode_ieee_quad
PARAMS ((const struct real_format
*,
3080 REAL_VALUE_TYPE
*, const long *));
3083 encode_ieee_quad (fmt
, buf
, r
)
3084 const struct real_format
*fmt
;
3086 const REAL_VALUE_TYPE
*r
;
3088 unsigned long image3
, image2
, image1
, image0
, exp
;
3089 bool denormal
= (r
->sig
[SIGSZ
-1] & SIG_MSB
) == 0;
3092 image3
= r
->sign
<< 31;
3097 rshift_significand (&u
, r
, SIGNIFICAND_BITS
- 113);
3106 image3
|= 32767 << 16;
3109 image3
|= 0x7fffffff;
3110 image2
= 0xffffffff;
3111 image1
= 0xffffffff;
3112 image0
= 0xffffffff;
3119 image3
|= 32767 << 16;
3121 if (HOST_BITS_PER_LONG
== 32)
3126 image3
|= u
.sig
[3] & 0xffff;
3131 image1
= image0
>> 31 >> 1;
3133 image3
|= (image2
>> 31 >> 1) & 0xffff;
3134 image0
&= 0xffffffff;
3135 image2
&= 0xffffffff;
3138 if (!fmt
->qnan_msb_set
)
3139 image3
^= 1 << 15 | 1 << 14;
3143 image3
|= 0x7fffffff;
3144 image2
= 0xffffffff;
3145 image1
= 0xffffffff;
3146 image0
= 0xffffffff;
3151 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3152 whereas the intermediate representation is 0.F x 2**exp.
3153 Which means we're off by one. */
3157 exp
= r
->exp
+ 16383 - 1;
3158 image3
|= exp
<< 16;
3160 if (HOST_BITS_PER_LONG
== 32)
3165 image3
|= u
.sig
[3] & 0xffff;
3170 image1
= image0
>> 31 >> 1;
3172 image3
|= (image2
>> 31 >> 1) & 0xffff;
3173 image0
&= 0xffffffff;
3174 image2
&= 0xffffffff;
3182 if (FLOAT_WORDS_BIG_ENDIAN
)
3199 decode_ieee_quad (fmt
, r
, buf
)
3200 const struct real_format
*fmt
;
3204 unsigned long image3
, image2
, image1
, image0
;
3208 if (FLOAT_WORDS_BIG_ENDIAN
)
3222 image0
&= 0xffffffff;
3223 image1
&= 0xffffffff;
3224 image2
&= 0xffffffff;
3226 sign
= (image3
>> 31) & 1;
3227 exp
= (image3
>> 16) & 0x7fff;
3230 memset (r
, 0, sizeof (*r
));
3234 if ((image3
| image2
| image1
| image0
) && fmt
->has_denorm
)
3236 r
->class = rvc_normal
;
3239 r
->exp
= -16382 + (SIGNIFICAND_BITS
- 112);
3240 if (HOST_BITS_PER_LONG
== 32)
3249 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3250 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3255 else if (fmt
->has_signed_zero
)
3258 else if (exp
== 32767 && (fmt
->has_nans
|| fmt
->has_inf
))
3260 if (image3
| image2
| image1
| image0
)
3265 if (HOST_BITS_PER_LONG
== 32)
3274 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3275 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3277 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3279 if (!fmt
->qnan_msb_set
)
3280 r
->sig
[SIGSZ
-1] ^= (SIG_MSB
>> 1 | SIG_MSB
>> 2);
3290 r
->class = rvc_normal
;
3292 r
->exp
= exp
- 16383 + 1;
3294 if (HOST_BITS_PER_LONG
== 32)
3303 r
->sig
[0] = (image1
<< 31 << 1) | image0
;
3304 r
->sig
[1] = (image3
<< 31 << 1) | image2
;
3306 lshift_significand (r
, r
, SIGNIFICAND_BITS
- 113);
3307 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3311 const struct real_format ieee_quad_format
=
3327 /* Descriptions of VAX floating point formats can be found beginning at
3329 http://www.openvms.compaq.com:8000/73final/4515/4515pro_013.html#f_floating_point_format
3331 The thing to remember is that they're almost IEEE, except for word
3332 order, exponent bias, and the lack of infinities, nans, and denormals.
3334 We don't implement the H_floating format here, simply because neither
3335 the VAX or Alpha ports use it. */
3337 static void encode_vax_f
PARAMS ((const struct real_format
*fmt
,
3338 long *, const REAL_VALUE_TYPE
*));
3339 static void decode_vax_f
PARAMS ((const struct real_format
*,
3340 REAL_VALUE_TYPE
*, const long *));
3341 static void encode_vax_d
PARAMS ((const struct real_format
*fmt
,
3342 long *, const REAL_VALUE_TYPE
*));
3343 static void decode_vax_d
PARAMS ((const struct real_format
*,
3344 REAL_VALUE_TYPE
*, const long *));
3345 static void encode_vax_g
PARAMS ((const struct real_format
*fmt
,
3346 long *, const REAL_VALUE_TYPE
*));
3347 static void decode_vax_g
PARAMS ((const struct real_format
*,
3348 REAL_VALUE_TYPE
*, const long *));
3351 encode_vax_f (fmt
, buf
, r
)
3352 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3354 const REAL_VALUE_TYPE
*r
;
3356 unsigned long sign
, exp
, sig
, image
;
3358 sign
= r
->sign
<< 15;
3368 image
= 0xffff7fff | sign
;
3372 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
3375 image
= (sig
<< 16) & 0xffff0000;
3389 decode_vax_f (fmt
, r
, buf
)
3390 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3394 unsigned long image
= buf
[0] & 0xffffffff;
3395 int exp
= (image
>> 7) & 0xff;
3397 memset (r
, 0, sizeof (*r
));
3401 r
->class = rvc_normal
;
3402 r
->sign
= (image
>> 15) & 1;
3405 image
= ((image
& 0x7f) << 16) | ((image
>> 16) & 0xffff);
3406 r
->sig
[SIGSZ
-1] = (image
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
3411 encode_vax_d (fmt
, buf
, r
)
3412 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3414 const REAL_VALUE_TYPE
*r
;
3416 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3421 image0
= image1
= 0;
3426 image0
= 0xffff7fff | sign
;
3427 image1
= 0xffffffff;
3431 /* Extract the significand into straight hi:lo. */
3432 if (HOST_BITS_PER_LONG
== 64)
3434 image0
= r
->sig
[SIGSZ
-1];
3435 image1
= (image0
>> (64 - 56)) & 0xffffffff;
3436 image0
= (image0
>> (64 - 56 + 1) >> 31) & 0x7fffff;
3440 image0
= r
->sig
[SIGSZ
-1];
3441 image1
= r
->sig
[SIGSZ
-2];
3442 image1
= (image0
<< 24) | (image1
>> 8);
3443 image0
= (image0
>> 8) & 0xffffff;
3446 /* Rearrange the half-words of the significand to match the
3448 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff007f;
3449 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
3451 /* Add the sign and exponent. */
3453 image0
|= (r
->exp
+ 128) << 7;
3460 if (FLOAT_WORDS_BIG_ENDIAN
)
3461 buf
[0] = image1
, buf
[1] = image0
;
3463 buf
[0] = image0
, buf
[1] = image1
;
3467 decode_vax_d (fmt
, r
, buf
)
3468 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3472 unsigned long image0
, image1
;
3475 if (FLOAT_WORDS_BIG_ENDIAN
)
3476 image1
= buf
[0], image0
= buf
[1];
3478 image0
= buf
[0], image1
= buf
[1];
3479 image0
&= 0xffffffff;
3480 image1
&= 0xffffffff;
3482 exp
= (image0
>> 7) & 0x7f;
3484 memset (r
, 0, sizeof (*r
));
3488 r
->class = rvc_normal
;
3489 r
->sign
= (image0
>> 15) & 1;
3492 /* Rearrange the half-words of the external format into
3493 proper ascending order. */
3494 image0
= ((image0
& 0x7f) << 16) | ((image0
>> 16) & 0xffff);
3495 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
3497 if (HOST_BITS_PER_LONG
== 64)
3499 image0
= (image0
<< 31 << 1) | image1
;
3502 r
->sig
[SIGSZ
-1] = image0
;
3506 r
->sig
[SIGSZ
-1] = image0
;
3507 r
->sig
[SIGSZ
-2] = image1
;
3508 lshift_significand (r
, r
, 2*HOST_BITS_PER_LONG
- 56);
3509 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3515 encode_vax_g (fmt
, buf
, r
)
3516 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3518 const REAL_VALUE_TYPE
*r
;
3520 unsigned long image0
, image1
, sign
= r
->sign
<< 15;
3525 image0
= image1
= 0;
3530 image0
= 0xffff7fff | sign
;
3531 image1
= 0xffffffff;
3535 /* Extract the significand into straight hi:lo. */
3536 if (HOST_BITS_PER_LONG
== 64)
3538 image0
= r
->sig
[SIGSZ
-1];
3539 image1
= (image0
>> (64 - 53)) & 0xffffffff;
3540 image0
= (image0
>> (64 - 53 + 1) >> 31) & 0xfffff;
3544 image0
= r
->sig
[SIGSZ
-1];
3545 image1
= r
->sig
[SIGSZ
-2];
3546 image1
= (image0
<< 21) | (image1
>> 11);
3547 image0
= (image0
>> 11) & 0xfffff;
3550 /* Rearrange the half-words of the significand to match the
3552 image0
= ((image0
<< 16) | (image0
>> 16)) & 0xffff000f;
3553 image1
= ((image1
<< 16) | (image1
>> 16)) & 0xffffffff;
3555 /* Add the sign and exponent. */
3557 image0
|= (r
->exp
+ 1024) << 4;
3564 if (FLOAT_WORDS_BIG_ENDIAN
)
3565 buf
[0] = image1
, buf
[1] = image0
;
3567 buf
[0] = image0
, buf
[1] = image1
;
3571 decode_vax_g (fmt
, r
, buf
)
3572 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3576 unsigned long image0
, image1
;
3579 if (FLOAT_WORDS_BIG_ENDIAN
)
3580 image1
= buf
[0], image0
= buf
[1];
3582 image0
= buf
[0], image1
= buf
[1];
3583 image0
&= 0xffffffff;
3584 image1
&= 0xffffffff;
3586 exp
= (image0
>> 4) & 0x7ff;
3588 memset (r
, 0, sizeof (*r
));
3592 r
->class = rvc_normal
;
3593 r
->sign
= (image0
>> 15) & 1;
3594 r
->exp
= exp
- 1024;
3596 /* Rearrange the half-words of the external format into
3597 proper ascending order. */
3598 image0
= ((image0
& 0xf) << 16) | ((image0
>> 16) & 0xffff);
3599 image1
= ((image1
& 0xffff) << 16) | ((image1
>> 16) & 0xffff);
3601 if (HOST_BITS_PER_LONG
== 64)
3603 image0
= (image0
<< 31 << 1) | image1
;
3606 r
->sig
[SIGSZ
-1] = image0
;
3610 r
->sig
[SIGSZ
-1] = image0
;
3611 r
->sig
[SIGSZ
-2] = image1
;
3612 lshift_significand (r
, r
, 64 - 53);
3613 r
->sig
[SIGSZ
-1] |= SIG_MSB
;
3618 const struct real_format vax_f_format
=
3634 const struct real_format vax_d_format
=
3650 const struct real_format vax_g_format
=
3666 /* A good reference for these can be found in chapter 9 of
3667 "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
3668 An on-line version can be found here:
3670 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3673 static void encode_i370_single
PARAMS ((const struct real_format
*fmt
,
3674 long *, const REAL_VALUE_TYPE
*));
3675 static void decode_i370_single
PARAMS ((const struct real_format
*,
3676 REAL_VALUE_TYPE
*, const long *));
3677 static void encode_i370_double
PARAMS ((const struct real_format
*fmt
,
3678 long *, const REAL_VALUE_TYPE
*));
3679 static void decode_i370_double
PARAMS ((const struct real_format
*,
3680 REAL_VALUE_TYPE
*, const long *));
3683 encode_i370_single (fmt
, buf
, r
)
3684 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3686 const REAL_VALUE_TYPE
*r
;
3688 unsigned long sign
, exp
, sig
, image
;
3690 sign
= r
->sign
<< 31;
3700 image
= 0x7fffffff | sign
;
3704 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0xffffff;
3705 exp
= ((r
->exp
/ 4) + 64) << 24;
3706 image
= sign
| exp
| sig
;
3717 decode_i370_single (fmt
, r
, buf
)
3718 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3722 unsigned long sign
, sig
, image
= buf
[0];
3725 sign
= (image
>> 31) & 1;
3726 exp
= (image
>> 24) & 0x7f;
3727 sig
= image
& 0xffffff;
3729 memset (r
, 0, sizeof (*r
));
3733 r
->class = rvc_normal
;
3735 r
->exp
= (exp
- 64) * 4;
3736 r
->sig
[SIGSZ
-1] = sig
<< (HOST_BITS_PER_LONG
- 24);
3742 encode_i370_double (fmt
, buf
, r
)
3743 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3745 const REAL_VALUE_TYPE
*r
;
3747 unsigned long sign
, exp
, image_hi
, image_lo
;
3749 sign
= r
->sign
<< 31;
3754 image_hi
= image_lo
= 0;
3759 image_hi
= 0x7fffffff | sign
;
3760 image_lo
= 0xffffffff;
3764 if (HOST_BITS_PER_LONG
== 64)
3766 image_hi
= r
->sig
[SIGSZ
-1];
3767 image_lo
= (image_hi
>> (64 - 56)) & 0xffffffff;
3768 image_hi
= (image_hi
>> (64 - 56 + 1) >> 31) & 0xffffff;
3772 image_hi
= r
->sig
[SIGSZ
-1];
3773 image_lo
= r
->sig
[SIGSZ
-2];
3774 image_lo
= (image_lo
>> 8) | (image_hi
<< 24);
3778 exp
= ((r
->exp
/ 4) + 64) << 24;
3779 image_hi
|= sign
| exp
;
3786 if (FLOAT_WORDS_BIG_ENDIAN
)
3787 buf
[0] = image_hi
, buf
[1] = image_lo
;
3789 buf
[0] = image_lo
, buf
[1] = image_hi
;
3793 decode_i370_double (fmt
, r
, buf
)
3794 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3798 unsigned long sign
, image_hi
, image_lo
;
3801 if (FLOAT_WORDS_BIG_ENDIAN
)
3802 image_hi
= buf
[0], image_lo
= buf
[1];
3804 image_lo
= buf
[0], image_hi
= buf
[1];
3806 sign
= (image_hi
>> 31) & 1;
3807 exp
= (image_hi
>> 24) & 0x7f;
3808 image_hi
&= 0xffffff;
3809 image_lo
&= 0xffffffff;
3811 memset (r
, 0, sizeof (*r
));
3813 if (exp
|| image_hi
|| image_lo
)
3815 r
->class = rvc_normal
;
3817 r
->exp
= (exp
- 64) * 4 + (SIGNIFICAND_BITS
- 56);
3819 if (HOST_BITS_PER_LONG
== 32)
3821 r
->sig
[0] = image_lo
;
3822 r
->sig
[1] = image_hi
;
3825 r
->sig
[0] = image_lo
| (image_hi
<< 31 << 1);
3831 const struct real_format i370_single_format
=
3842 false, /* ??? The encoding does allow for "unnormals". */
3843 false, /* ??? The encoding does allow for "unnormals". */
3847 const struct real_format i370_double_format
=
3858 false, /* ??? The encoding does allow for "unnormals". */
3859 false, /* ??? The encoding does allow for "unnormals". */
3863 /* The "twos-complement" c4x format is officially defined as
3867 This is rather misleading. One must remember that F is signed.
3868 A better description would be
3870 x = -1**s * ((s + 1 + .f) * 2**e
3872 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
3873 that's -1 * (1+1+(-.5)) == -1.5. I think.
3875 The constructions here are taken from Tables 5-1 and 5-2 of the
3876 TMS320C4x User's Guide wherein step-by-step instructions for
3877 conversion from IEEE are presented. That's close enough to our
3878 internal representation so as to make things easy.
3880 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
3882 static void encode_c4x_single
PARAMS ((const struct real_format
*fmt
,
3883 long *, const REAL_VALUE_TYPE
*));
3884 static void decode_c4x_single
PARAMS ((const struct real_format
*,
3885 REAL_VALUE_TYPE
*, const long *));
3886 static void encode_c4x_extended
PARAMS ((const struct real_format
*fmt
,
3887 long *, const REAL_VALUE_TYPE
*));
3888 static void decode_c4x_extended
PARAMS ((const struct real_format
*,
3889 REAL_VALUE_TYPE
*, const long *));
3892 encode_c4x_single (fmt
, buf
, r
)
3893 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3895 const REAL_VALUE_TYPE
*r
;
3897 unsigned long image
, exp
, sig
;
3909 sig
= 0x800000 - r
->sign
;
3914 sig
= (r
->sig
[SIGSZ
-1] >> (HOST_BITS_PER_LONG
- 24)) & 0x7fffff;
3929 image
= ((exp
& 0xff) << 24) | (sig
& 0xffffff);
3934 decode_c4x_single (fmt
, r
, buf
)
3935 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3939 unsigned long image
= buf
[0];
3943 exp
= (((image
>> 24) & 0xff) ^ 0x80) - 0x80;
3944 sf
= ((image
& 0xffffff) ^ 0x800000) - 0x800000;
3946 memset (r
, 0, sizeof (*r
));
3950 r
->class = rvc_normal
;
3952 sig
= sf
& 0x7fffff;
3961 sig
= (sig
<< (HOST_BITS_PER_LONG
- 24)) | SIG_MSB
;
3964 r
->sig
[SIGSZ
-1] = sig
;
3969 encode_c4x_extended (fmt
, buf
, r
)
3970 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
3972 const REAL_VALUE_TYPE
*r
;
3974 unsigned long exp
, sig
;
3986 sig
= 0x80000000 - r
->sign
;
3992 sig
= r
->sig
[SIGSZ
-1];
3993 if (HOST_BITS_PER_LONG
== 64)
3994 sig
= sig
>> 1 >> 31;
4011 exp
= (exp
& 0xff) << 24;
4014 if (FLOAT_WORDS_BIG_ENDIAN
)
4015 buf
[0] = exp
, buf
[1] = sig
;
4017 buf
[0] = sig
, buf
[0] = exp
;
4021 decode_c4x_extended (fmt
, r
, buf
)
4022 const struct real_format
*fmt ATTRIBUTE_UNUSED
;
4029 if (FLOAT_WORDS_BIG_ENDIAN
)
4030 exp
= buf
[0], sf
= buf
[1];
4032 sf
= buf
[0], exp
= buf
[1];
4034 exp
= (((exp
>> 24) & 0xff) & 0x80) - 0x80;
4035 sf
= ((sf
& 0xffffffff) ^ 0x80000000) - 0x80000000;
4037 memset (r
, 0, sizeof (*r
));
4041 r
->class = rvc_normal
;
4043 sig
= sf
& 0x7fffffff;
4052 if (HOST_BITS_PER_LONG
== 64)
4053 sig
= sig
<< 1 << 31;
4057 r
->sig
[SIGSZ
-1] = sig
;
4061 const struct real_format c4x_single_format
=
4077 const struct real_format c4x_extended_format
=
4079 encode_c4x_extended
,
4080 decode_c4x_extended
,
4093 /* Set up default mode to format mapping for IEEE. Everyone else has
4094 to set these values in OVERRIDE_OPTIONS. */
4096 const struct real_format
*real_format_for_mode
[TFmode
- QFmode
+ 1] =
4101 &ieee_single_format
, /* SFmode */
4102 &ieee_double_format
, /* DFmode */
4104 /* We explicitly don't handle XFmode. There are two formats,
4105 pretty much equally common. Choose one in OVERRIDE_OPTIONS. */
4107 &ieee_quad_format
/* TFmode */