hpux.h (CTORS_SECTION_ASM_OP): New.
[official-gcc.git] / gcc / real.c
blob57127b643f37a8a5f67953ca24a54e0ac6d5ebff
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
12 version.
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
17 for more details.
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
22 02111-1307, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "tree.h"
27 #include "toplev.h"
28 #include "real.h"
29 #include "tm_p.h"
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.
35 Specifically
37 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
39 where
40 s = sign (+- 1)
41 b = base or radix, here always 2
42 e = exponent
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
48 range [0.5, 1.0).
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 {
68 rvc_zero,
69 rvc_normal,
70 rvc_inf,
71 rvc_nan
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"
87 #endif
89 struct real_value
91 enum real_value_class class : 2;
92 unsigned int sign : 1;
93 int exp : EXP_BITS;
94 unsigned long sig[SIGSZ];
97 /* Describes the properties of the specific target format in use. */
98 struct real_format
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 *,
104 const long *);
106 /* The radix of the exponent and digits of the significand. */
107 int b;
109 /* log2(b). */
110 int log2_b;
112 /* Size of the significand in digits of radix B. */
113 int p;
115 /* The minimum negative integer, x, such that b**(x-1) is normalized. */
116 int emin;
118 /* The maximum integer, x, such that b**(x-1) is representable. */
119 int emax;
121 /* Properties of the format. */
122 bool has_nans;
123 bool has_inf;
124 bool has_denorm;
125 bool has_signed_zero;
126 bool qnan_msb_set;
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 *,
139 unsigned int));
140 static void rshift_significand PARAMS ((struct real_value *,
141 const struct real_value *,
142 unsigned int));
143 static void lshift_significand PARAMS ((struct real_value *,
144 const struct real_value *,
145 unsigned int));
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 *,
162 unsigned int));
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. */
188 static inline void
189 get_zero (r, sign)
190 struct real_value *r;
191 int sign;
193 memset (r, 0, sizeof (*r));
194 r->sign = sign;
197 /* Initialize R with the canonical quiet NaN. */
199 static inline void
200 get_canonical_qnan (r, sign)
201 struct real_value *r;
202 int sign;
204 memset (r, 0, sizeof (*r));
205 r->class = rvc_nan;
206 r->sign = sign;
207 r->sig[SIGSZ-1] = SIG_MSB >> 1;
210 static inline void
211 get_canonical_snan (r, sign)
212 struct real_value *r;
213 int sign;
215 memset (r, 0, sizeof (*r));
216 r->class = rvc_nan;
217 r->sign = sign;
218 r->sig[SIGSZ-1] = SIG_MSB >> 2;
221 static inline void
222 get_inf (r, sign)
223 struct real_value *r;
224 int sign;
226 memset (r, 0, sizeof (*r));
227 r->class = rvc_inf;
228 r->sign = sign;
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. */
236 static void
237 sticky_rshift_significand (r, a, n)
238 struct real_value *r;
239 const struct real_value *a;
240 unsigned int n;
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)
248 sticky |= a->sig[i];
249 n -= ofs * HOST_BITS_PER_LONG;
252 if (n != 0)
254 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
255 for (i = 0; i < SIGSZ; ++i)
257 r->sig[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)));
263 else
265 for (i = 0; ofs + i < SIGSZ; ++i)
266 r->sig[i] = a->sig[ofs + i];
267 for (; i < SIGSZ; ++i)
268 r->sig[i] = 0;
271 r->sig[0] |= (sticky != 0);
274 /* Right-shift the significand of A by N bits; put the result in the
275 significand of R. */
277 static void
278 rshift_significand (r, a, n)
279 struct real_value *r;
280 const struct real_value *a;
281 unsigned int n;
283 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
285 n -= ofs * HOST_BITS_PER_LONG;
286 if (n != 0)
288 for (i = 0; i < SIGSZ; ++i)
290 r->sig[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)));
296 else
298 for (i = 0; ofs + i < SIGSZ; ++i)
299 r->sig[i] = a->sig[ofs + i];
300 for (; i < SIGSZ; ++i)
301 r->sig[i] = 0;
305 /* Left-shift the significand of A by N bits; put the result in the
306 significand of R. */
308 static void
309 lshift_significand (r, a, n)
310 struct real_value *r;
311 const struct real_value *a;
312 unsigned int n;
314 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
316 n -= ofs * HOST_BITS_PER_LONG;
317 if (n == 0)
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;
324 else
325 for (i = 0; i < SIGSZ; ++i)
327 r->sig[SIGSZ-1-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. */
336 static inline void
337 lshift_significand_1 (r, a)
338 struct real_value *r;
339 const struct real_value *a;
341 unsigned int i;
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. */
351 static inline bool
352 add_significands (r, a, b)
353 struct real_value *r;
354 const struct real_value *a, *b;
356 bool carry = false;
357 int i;
359 for (i = 0; i < SIGSZ; ++i)
361 unsigned long ai = a->sig[i];
362 unsigned long ri = ai + b->sig[i];
364 if (carry)
366 carry = ri < ai;
367 carry |= ++ri == 0;
369 else
370 carry = ri < ai;
372 r->sig[i] = ri;
375 return carry;
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. */
381 static inline bool
382 sub_significands (r, a, b)
383 struct real_value *r;
384 const struct real_value *a, *b;
386 bool carry = false;
387 int i;
389 for (i = 0; i < SIGSZ; ++i)
391 unsigned long ai = a->sig[i];
392 unsigned long ri = ai - b->sig[i];
394 if (carry)
396 carry = ri > ai;
397 carry |= ~--ri == 0;
399 else
400 carry = ri > ai;
402 r->sig[i] = ri;
405 return carry;
408 /* Negate the significand A, placing the result in R. */
410 static inline void
411 neg_significand (r, a)
412 struct real_value *r;
413 const struct real_value *a;
415 bool carry = true;
416 int i;
418 for (i = 0; i < SIGSZ; ++i)
420 unsigned long ri, ai = a->sig[i];
422 if (carry)
424 if (ai)
426 ri = -ai;
427 carry = false;
429 else
430 ri = ai;
432 else
433 ri = ~ai;
435 r->sig[i] = ri;
439 /* Compare significands. Return tri-state vs zero. */
441 static inline int
442 cmp_significands (a, b)
443 const struct real_value *a, *b;
445 int i;
447 for (i = SIGSZ - 1; i >= 0; --i)
449 unsigned long ai = a->sig[i];
450 unsigned long bi = b->sig[i];
452 if (ai > bi)
453 return 1;
454 if (ai < bi)
455 return -1;
458 return 0;
461 /* Set bit N of the significand of R. */
463 static inline void
464 set_significand_bit (r, n)
465 struct real_value *r;
466 unsigned int n;
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. */
474 static inline void
475 clear_significand_bit (r, n)
476 struct real_value *r;
477 unsigned int n;
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. */
485 static inline bool
486 test_significand_bit (r, n)
487 struct real_value *r;
488 unsigned int n;
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;
494 return t;
497 /* Clear bits 0..N-1 of the significand of R. */
499 static void
500 clear_significand_below (r, n)
501 struct real_value *r;
502 unsigned int n;
504 int i, w = n / HOST_BITS_PER_LONG;
506 for (i = 0; i < w; ++i)
507 r->sig[i] = 0;
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. */
515 static inline bool
516 div_significands (r, a, b)
517 struct real_value *r;
518 const struct real_value *a, *b;
520 struct real_value u;
521 int bit = SIGNIFICAND_BITS - 1;
522 int i;
523 long inexact;
525 u = *a;
526 memset (r->sig, 0, sizeof (r->sig));
528 goto start;
531 if ((u.sig[SIGSZ-1] & SIG_MSB) == 0)
533 lshift_significand_1 (&u, &u);
534 start:
535 if (cmp_significands (&u, b) >= 0)
537 sub_significands (&u, &u, b);
538 set_significand_bit (r, bit);
541 else
543 /* We lose a bit here, and thus know the next quotient bit
544 will be one. */
545 lshift_significand_1 (&u, &u);
546 sub_significands (&u, &u, b);
547 set_significand_bit (r, bit);
550 while (--bit >= 0);
552 for (i = 0, inexact = 0; i < SIGSZ; i++)
553 inexact |= u.sig[i];
555 return inexact != 0;
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.) */
563 static void
564 normalize (r)
565 struct real_value *r;
567 int shift = 0, exp;
568 int i, j;
570 /* Find the first word that is non-zero. */
571 for (i = SIGSZ - 1; i >= 0; i--)
572 if (r->sig[i] == 0)
573 shift += HOST_BITS_PER_LONG;
574 else
575 break;
577 /* Zero significand flushes to zero. */
578 if (i < 0)
580 r->class = rvc_zero;
581 r->exp = 0;
582 return;
585 /* Find the first bit that is non-zero. */
586 for (j = 0; ; j++)
587 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
588 break;
589 shift += j;
591 if (shift > 0)
593 exp = r->exp - shift;
594 if (exp > MAX_EXP)
595 get_inf (r, r->sign);
596 else if (exp < -MAX_EXP)
597 get_zero (r, r->sign);
598 else
600 r->exp = exp;
601 lshift_significand (r, r, shift);
606 /* Return R = A + (SUBTRACT_P ? -B : B). */
608 static void
609 do_add (r, a, b, subtract_p)
610 struct real_value *r;
611 const struct real_value *a, *b;
612 int subtract_p;
614 int dexp, sign, exp;
615 struct real_value t;
617 /* Determine if we need to add or subtract. */
618 sign = a->sign;
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. */
625 get_zero (r, 0);
626 return;
628 case CLASS2 (rvc_zero, rvc_normal):
629 case CLASS2 (rvc_zero, rvc_inf):
630 case CLASS2 (rvc_zero, rvc_nan):
631 /* 0 + ANY = ANY. */
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):
637 /* R + Inf = Inf. */
638 *r = *b;
639 r->sign = sign ^ subtract_p;
640 return;
642 case CLASS2 (rvc_normal, rvc_zero):
643 case CLASS2 (rvc_inf, rvc_zero):
644 case CLASS2 (rvc_nan, rvc_zero):
645 /* ANY + 0 = ANY. */
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):
650 /* Inf + R = Inf. */
651 *r = *a;
652 return;
654 case CLASS2 (rvc_inf, rvc_inf):
655 if (subtract_p)
656 /* Inf - Inf = NaN. */
657 get_canonical_qnan (r, 0);
658 else
659 /* Inf + Inf = Inf. */
660 *r = *a;
661 return;
663 case CLASS2 (rvc_normal, rvc_normal):
664 break;
666 default:
667 abort ();
670 /* Swap the arguments such that A has the larger exponent. */
671 dexp = a->exp - b->exp;
672 if (dexp < 0)
674 const struct real_value *t;
675 t = a, a = b, b = t;
676 dexp = -dexp;
677 sign ^= subtract_p;
679 exp = a->exp;
681 /* If the exponents are not identical, we need to shift the
682 significand of B down. */
683 if (dexp > 0)
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)
689 *r = *a;
690 r->sign = sign;
691 return;
694 sticky_rshift_significand (&t, b, dexp);
695 b = &t;
698 if (subtract_p)
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
705 significand. */
706 sign ^= 1;
707 neg_significand (r, r);
710 else
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
716 exponent. */
717 sticky_rshift_significand (r, r, 1);
718 r->sig[SIGSZ-1] |= SIG_MSB;
719 if (++exp > MAX_EXP)
721 get_inf (r, sign);
722 return;
727 r->class = rvc_normal;
728 r->sign = sign;
729 r->exp = exp;
731 /* Re-normalize the result. */
732 normalize (r);
734 /* Special case: if the subtraction results in zero, the result
735 is positive. */
736 if (r->class == rvc_zero)
737 r->sign = 0;
740 /* Return R = A * B. */
742 static void
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. */
757 get_zero (r, sign);
758 return;
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. */
765 *r = *b;
766 r->sign = sign;
767 return;
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. */
773 *r = *a;
774 r->sign = sign;
775 return;
777 case CLASS2 (rvc_zero, rvc_inf):
778 case CLASS2 (rvc_inf, rvc_zero):
779 /* 0 * Inf = NaN */
780 get_canonical_qnan (r, sign);
781 return;
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 */
787 overflow:
788 get_inf (r, sign);
789 return;
791 case CLASS2 (rvc_normal, rvc_normal):
792 break;
794 default:
795 abort ();
798 if (r == a || r == b)
799 rr = &t;
800 else
801 rr = r;
802 get_zero (rr, 0);
804 u.class = rvc_normal;
805 u.sign = 0;
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:
812 A B C D
813 * E F G H
814 --------------
815 DE DF DG DH
816 CE CF CG CH
817 BE BF BG BH
818 AE AF AG AH
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];
828 if (i & 1)
829 ai >>= HOST_BITS_PER_LONG / 2;
830 else
831 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
833 if (ai == 0)
834 continue;
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)));
841 if (exp > MAX_EXP)
842 goto overflow;
843 if (exp < -MAX_EXP)
844 /* Would underflow to zero, which we shouldn't bother adding. */
845 continue;
847 u.exp = exp;
849 for (k = j; k < SIGSZ * 2; k += 2)
851 unsigned long bi = b->sig[k / 2];
852 if (k & 1)
853 bi >>= HOST_BITS_PER_LONG / 2;
854 else
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);
864 rr->sign = sign;
865 if (rr != r)
866 *r = t;
869 /* Return R = A / B. */
871 static void
872 do_divide (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;
878 bool inexact;
880 switch (CLASS2 (a->class, b->class))
882 case CLASS2 (rvc_zero, rvc_zero):
883 /* 0 / 0 = NaN. */
884 case CLASS2 (rvc_inf, rvc_zero):
885 /* Inf / 0 = NaN. */
886 case CLASS2 (rvc_inf, rvc_inf):
887 /* Inf / Inf = NaN. */
888 get_canonical_qnan (r, sign);
889 return;
891 case CLASS2 (rvc_zero, rvc_normal):
892 case CLASS2 (rvc_zero, rvc_inf):
893 /* 0 / ANY = 0. */
894 case CLASS2 (rvc_normal, rvc_inf):
895 /* R / Inf = 0. */
896 underflow:
897 get_zero (r, sign);
898 return;
900 case CLASS2 (rvc_normal, rvc_zero):
901 /* R / 0 = Inf. */
902 get_inf (r, sign);
903 return;
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. */
910 *r = *b;
911 r->sign = sign;
912 return;
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. */
918 *r = *a;
919 r->sign = sign;
920 return;
922 case CLASS2 (rvc_inf, rvc_normal):
923 /* Inf / R = Inf. */
924 overflow:
925 get_inf (r, sign);
926 return;
928 case CLASS2 (rvc_normal, rvc_normal):
929 break;
931 default:
932 abort ();
935 if (r == a || r == b)
936 rr = &t;
937 else
938 rr = r;
940 rr->class = rvc_normal;
941 rr->sign = sign;
943 exp = a->exp - b->exp + 1;
944 if (exp > MAX_EXP)
945 goto overflow;
946 if (exp < -MAX_EXP)
947 goto underflow;
948 rr->exp = exp;
950 inexact = div_significands (rr, a, b);
951 rr->sig[0] |= inexact;
953 /* Re-normalize the result. */
954 normalize (rr);
956 if (rr != r)
957 *r = t;
960 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
961 one of the two operands is a NaN. */
963 static int
964 do_compare (a, b, nan_result)
965 const struct real_value *a, *b;
966 int nan_result;
968 int ret;
970 switch (CLASS2 (a->class, b->class))
972 case CLASS2 (rvc_zero, rvc_zero):
973 /* Sign of zero doesn't matter for compares. */
974 return 0;
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):
996 return nan_result;
998 case CLASS2 (rvc_normal, rvc_normal):
999 break;
1001 default:
1002 abort ();
1005 if (a->sign != b->sign)
1006 return -a->sign - -b->sign;
1008 if (a->exp > b->exp)
1009 ret = 1;
1010 else if (a->exp < b->exp)
1011 ret = -1;
1012 else
1013 ret = cmp_significands (a, b);
1015 return (a->sign ? -ret : ret);
1018 /* Return A truncated to an integral value toward zero. */
1020 void
1021 do_fix_trunc (r, a)
1022 struct real_value *r;
1023 const struct real_value *a;
1025 *r = *a;
1027 switch (a->class)
1029 case rvc_zero:
1030 case rvc_inf:
1031 case rvc_nan:
1032 break;
1034 case rvc_normal:
1035 if (r->exp <= 0)
1036 get_zero (r, r->sign);
1037 else if (r->exp < SIGNIFICAND_BITS)
1038 clear_significand_below (r, SIGNIFICAND_BITS - r->exp);
1039 break;
1041 default:
1042 abort ();
1046 /* Perform the binary or unary operation described by CODE.
1047 For a unary operation, leave OP1 NULL. */
1049 void
1050 real_arithmetic (tr, icode, top0, top1)
1051 REAL_VALUE_TYPE *tr;
1052 int icode;
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;
1060 switch (code)
1062 case PLUS_EXPR:
1063 do_add (r, op0, op1, 0);
1064 break;
1066 case MINUS_EXPR:
1067 do_add (r, op0, op1, 1);
1068 break;
1070 case MULT_EXPR:
1071 do_multiply (r, op0, op1);
1072 break;
1074 case RDIV_EXPR:
1075 do_divide (r, op0, op1);
1076 break;
1078 case MIN_EXPR:
1079 if (op1->class == rvc_nan)
1080 *r = *op1;
1081 else if (do_compare (op0, op1, -1) < 0)
1082 *r = *op0;
1083 else
1084 *r = *op1;
1085 break;
1087 case MAX_EXPR:
1088 if (op1->class == rvc_nan)
1089 *r = *op1;
1090 else if (do_compare (op0, op1, 1) < 0)
1091 *r = *op1;
1092 else
1093 *r = *op0;
1094 break;
1096 case NEGATE_EXPR:
1097 *r = *op0;
1098 r->sign ^= 1;
1099 break;
1101 case ABS_EXPR:
1102 *r = *op0;
1103 r->sign = 0;
1104 break;
1106 case FIX_TRUNC_EXPR:
1107 do_fix_trunc (r, op0);
1108 break;
1110 default:
1111 abort ();
1115 /* Legacy. Similar, but return the result directly. */
1117 REAL_VALUE_TYPE
1118 real_arithmetic2 (icode, top0, top1)
1119 int icode;
1120 const REAL_VALUE_TYPE *top0, *top1;
1122 REAL_VALUE_TYPE r;
1123 real_arithmetic (&r, icode, top0, top1);
1124 return r;
1127 bool
1128 real_compare (icode, top0, top1)
1129 int icode;
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;
1136 switch (code)
1138 case LT_EXPR:
1139 return do_compare (op0, op1, 1) < 0;
1140 case LE_EXPR:
1141 return do_compare (op0, op1, 1) <= 0;
1142 case GT_EXPR:
1143 return do_compare (op0, op1, -1) > 0;
1144 case GE_EXPR:
1145 return do_compare (op0, op1, -1) >= 0;
1146 case EQ_EXPR:
1147 return do_compare (op0, op1, -1) == 0;
1148 case NE_EXPR:
1149 return do_compare (op0, op1, -1) != 0;
1150 case UNORDERED_EXPR:
1151 return op0->class == rvc_nan || op1->class == rvc_nan;
1152 case ORDERED_EXPR:
1153 return op0->class != rvc_nan && op1->class != rvc_nan;
1154 case UNLT_EXPR:
1155 return do_compare (op0, op1, -1) < 0;
1156 case UNLE_EXPR:
1157 return do_compare (op0, op1, -1) <= 0;
1158 case UNGT_EXPR:
1159 return do_compare (op0, op1, 1) > 0;
1160 case UNGE_EXPR:
1161 return do_compare (op0, op1, 1) >= 0;
1162 case UNEQ_EXPR:
1163 return do_compare (op0, op1, 0) == 0;
1165 default:
1166 abort ();
1170 /* Return floor log2(R). */
1173 real_exponent (tr)
1174 const REAL_VALUE_TYPE *tr;
1176 const struct real_value *r = (const struct real_value *) tr;
1178 switch (r->class)
1180 case rvc_zero:
1181 return 0;
1182 case rvc_inf:
1183 case rvc_nan:
1184 return (unsigned int)-1 >> 1;
1185 case rvc_normal:
1186 return r->exp;
1187 default:
1188 abort ();
1193 /* R = OP0 * 2**EXP. */
1195 void
1196 real_ldexp (tr, top0, exp)
1197 REAL_VALUE_TYPE *tr;
1198 const REAL_VALUE_TYPE *top0;
1199 int exp;
1201 struct real_value *r = (struct real_value *) tr;
1202 const struct real_value *op0 = (const struct real_value *) top0;
1204 *r = *op0;
1205 switch (r->class)
1207 case rvc_zero:
1208 case rvc_inf:
1209 case rvc_nan:
1210 break;
1212 case rvc_normal:
1213 exp += op0->exp;
1214 if (exp > MAX_EXP)
1215 get_inf (r, r->sign);
1216 else if (exp < -MAX_EXP)
1217 get_zero (r, r->sign);
1218 else
1219 r->exp = exp;
1220 break;
1222 default:
1223 abort ();
1227 /* Determine whether a floating-point value X is infinite. */
1229 bool
1230 real_isinf (tr)
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. */
1239 bool
1240 real_isnan (tr)
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. */
1249 bool
1250 real_isneg (tr)
1251 const REAL_VALUE_TYPE *tr;
1253 const struct real_value *r = (const struct real_value *) tr;
1254 return r->sign;
1257 /* Determine whether a floating-point value X is minus zero. */
1259 bool
1260 real_isnegzero (tr)
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. */
1269 extern bool
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;
1275 int i;
1277 if (a->class != b->class)
1278 return false;
1279 if (a->sign != b->sign)
1280 return false;
1282 switch (a->class)
1284 case rvc_zero:
1285 case rvc_inf:
1286 break;
1288 case rvc_normal:
1289 if (a->exp != b->exp)
1290 return false;
1291 /* FALLTHRU */
1292 case rvc_nan:
1293 for (i = 0; i < SIGSZ; ++i)
1294 if (a->sig[i] != b->sig[i])
1295 return false;
1296 break;
1298 default:
1299 abort ();
1302 return true;
1305 /* Try to change R into its exact multiplicative inverse in machine
1306 mode MODE. Return true if successful. */
1308 bool
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;
1316 int i;
1318 if (r->class != rvc_normal)
1319 return false;
1321 /* Check for a power of two: all significand bits zero except the MSB. */
1322 for (i = 0; i < SIGSZ-1; ++i)
1323 if (r->sig[i] != 0)
1324 return false;
1325 if (r->sig[SIGSZ-1] != SIG_MSB)
1326 return false;
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)
1334 return false;
1335 for (i = 0; i < SIGSZ-1; ++i)
1336 if (u.sig[i] != 0)
1337 return false;
1338 if (u.sig[SIGSZ-1] != SIG_MSB)
1339 return false;
1341 *r = u;
1342 return true;
1345 /* Render R as an integer. */
1347 HOST_WIDE_INT
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;
1354 switch (r->class)
1356 case rvc_zero:
1357 underflow:
1358 return 0;
1360 case rvc_inf:
1361 case rvc_nan:
1362 overflow:
1363 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1364 if (!r->sign)
1365 i--;
1366 return i;
1368 case rvc_normal:
1369 if (r->exp <= 0)
1370 goto underflow;
1371 if (r->exp > HOST_BITS_PER_WIDE_INT)
1372 goto overflow;
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];
1382 else
1383 abort ();
1385 i >>= HOST_BITS_PER_WIDE_INT - r->exp;
1387 if (r->sign)
1388 i = -i;
1389 return i;
1391 default:
1392 abort ();
1396 /* Likewise, but to an integer pair, HI+LOW. */
1398 void
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;
1405 int exp;
1407 r = *(const struct real_value *) tr;
1408 switch (r.class)
1410 case rvc_zero:
1411 underflow:
1412 low = high = 0;
1413 break;
1415 case rvc_inf:
1416 case rvc_nan:
1417 overflow:
1418 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1419 if (r.sign)
1420 low = 0;
1421 else
1423 high--;
1424 low = -1;
1426 break;
1428 case rvc_normal:
1429 exp = r.exp;
1430 if (exp <= 0)
1431 goto underflow;
1432 if (exp >= 2*HOST_BITS_PER_WIDE_INT)
1433 goto overflow;
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];
1451 else
1452 abort ();
1454 if (r.sign)
1456 if (low == 0)
1457 high = -high;
1458 else
1459 low = -low, high = ~high;
1461 break;
1463 default:
1464 abort ();
1467 *plow = low;
1468 *phigh = 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
1477 void
1478 real_to_decimal (str, r_orig, digits)
1479 char *str;
1480 const REAL_VALUE_TYPE *r_orig;
1481 int digits;
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;
1487 bool sign;
1489 r = *(const struct real_value *)r_orig;
1490 switch (r.class)
1492 case rvc_zero:
1493 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1494 return;
1495 case rvc_normal:
1496 break;
1497 case rvc_inf:
1498 strcpy (str, (r.sign ? "+Inf" : "-Inf"));
1499 return;
1500 case rvc_nan:
1501 /* ??? Print the significand as well, if not canonical? */
1502 strcpy (str, (r.sign ? "+NaN" : "-NaN"));
1503 return;
1504 default:
1505 abort ();
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);
1515 sign = r.sign;
1516 r.sign = 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). */
1522 if (dec_exp > 0)
1524 int i;
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);
1543 dec_exp--;
1545 else if (do_compare (&r, ten, 1) >= 0)
1547 do_divide (&r, &r, ten);
1548 dec_exp++;
1551 p = str;
1552 if (sign)
1553 *p++ = '-';
1554 first = p++;
1555 while (1)
1557 d = real_to_integer ((const REAL_VALUE_TYPE *) &r);
1558 do_add (&r, &r, real_digit (d), 1);
1560 *p++ = d + '0';
1561 if (--digits == 0)
1562 break;
1563 do_multiply (&r, &r, ten);
1565 last = p;
1567 /* Round the result. Compare R vs 0.5 by doing R*2 vs 1.0. */
1568 r.exp += 1;
1569 cmp_half = do_compare (&r, one, -1);
1570 if (cmp_half == 0)
1571 /* Round to even. */
1572 cmp_half += d & 1;
1573 if (cmp_half > 0)
1575 while (p > first)
1577 d = *--p;
1578 if (d == '9')
1579 *p = '0';
1580 else
1582 *p = d + 1;
1583 break;
1587 if (p == first)
1589 first[1] = '1';
1590 dec_exp++;
1594 first[0] = first[1];
1595 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. */
1604 void
1605 real_to_hexadecimal (str, tr, digits)
1606 char *str;
1607 const REAL_VALUE_TYPE *tr;
1608 int digits;
1610 struct real_value r;
1611 int i, j;
1612 char *p;
1614 r = *(const struct real_value *) tr;
1616 switch (r.class)
1618 case rvc_zero:
1619 r.exp = 0;
1620 break;
1621 case rvc_normal:
1622 break;
1623 case rvc_inf:
1624 strcpy (str, (r.sign ? "+Inf" : "-Inf"));
1625 return;
1626 case rvc_nan:
1627 /* ??? Print the significand as well, if not canonical? */
1628 strcpy (str, (r.sign ? "+NaN" : "-NaN"));
1629 return;
1630 default:
1631 abort ();
1634 if (digits <= 0)
1635 digits = SIGNIFICAND_BITS / 4;
1637 p = str;
1638 if (r.sign)
1639 *p++ = '-';
1640 *p++ = '0';
1641 *p++ = 'x';
1642 *p++ = '0';
1643 *p++ = '.';
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];
1649 if (--digits == 0)
1650 goto out;
1652 out:
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. */
1659 void
1660 real_from_string (tr, str)
1661 REAL_VALUE_TYPE *tr;
1662 const char *str;
1664 struct real_value *r = (struct real_value *) tr;
1665 int exp = 0;
1667 get_zero (r, 0);
1669 if (*str == '-')
1671 r->sign = 1;
1672 str++;
1674 else if (*str == '+')
1675 str++;
1677 if (str[0] == '0' && str[1] == 'x')
1679 /* Hexadecimal floating point. */
1680 int pos = SIGNIFICAND_BITS - 4, d;
1682 str += 2;
1684 while (*str == '0')
1685 str++;
1686 while (1)
1688 d = hex_value (*str);
1689 if (d == _hex_bad)
1690 break;
1691 if (pos >= 0)
1693 r->sig[pos / HOST_BITS_PER_LONG]
1694 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1695 pos -= 4;
1697 exp += 4;
1698 str++;
1700 if (*str == '.')
1702 str++;
1703 while (1)
1705 d = hex_value (*str);
1706 if (d == _hex_bad)
1707 break;
1708 if (pos >= 0)
1710 r->sig[pos / HOST_BITS_PER_LONG]
1711 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1712 pos -= 4;
1714 str++;
1717 if (*str == 'p' || *str == 'P')
1719 int exp_neg = 0;
1721 str++;
1722 if (*str == '-')
1724 exp_neg = 1;
1725 str++;
1727 else if (*str == '+')
1728 str++;
1730 d = 0;
1731 while (ISDIGIT (*str))
1733 int t = d;
1734 d *= 10;
1735 d += *str - '0';
1736 if (d < t)
1738 /* Overflowed the exponent. */
1739 if (exp_neg)
1740 goto underflow;
1741 else
1742 goto overflow;
1744 str++;
1746 if (exp_neg)
1747 d = -d;
1749 exp += d;
1752 r->class = rvc_normal;
1753 r->exp = exp;
1754 if (r->exp != exp)
1756 if (exp < 0)
1757 goto underflow;
1758 else
1759 goto overflow;
1762 normalize (r);
1764 else
1766 /* Decimal floating point. */
1767 const struct real_value *ten = ten_to_ptwo (0);
1768 int d;
1770 while (*str == '0')
1771 str++;
1772 while (ISDIGIT (*str))
1774 d = *str++ - '0';
1775 do_multiply (r, r, ten);
1776 if (d)
1777 do_add (r, r, real_digit (d), 0);
1779 if (*str == '.')
1781 str++;
1782 while (ISDIGIT (*str))
1784 d = *str++ - '0';
1785 do_multiply (r, r, ten);
1786 if (d)
1787 do_add (r, r, real_digit (d), 0);
1788 exp--;
1792 if (*str == 'e' || *str == 'E')
1794 int exp_neg = 0;
1796 str++;
1797 if (*str == '-')
1799 exp_neg = 1;
1800 str++;
1802 else if (*str == '+')
1803 str++;
1805 d = 0;
1806 while (ISDIGIT (*str))
1808 int t = d;
1809 d *= 10;
1810 d += *str - '0';
1811 if (d < t)
1813 /* Overflowed the exponent. */
1814 if (exp_neg)
1815 goto underflow;
1816 else
1817 goto overflow;
1819 str++;
1821 if (exp_neg)
1822 d = -d;
1823 exp += d;
1826 if (exp < 0)
1828 exp = -exp;
1829 for (d = 0; d < EXP_BITS; ++d)
1830 if (exp & (1 << d))
1831 do_divide (r, r, ten_to_ptwo (d));
1833 else if (exp > 0)
1835 for (d = 0; d < EXP_BITS; ++d)
1836 if (exp & (1 << d))
1837 do_multiply (r, r, ten_to_ptwo (d));
1841 return;
1843 underflow:
1844 get_zero (r, r->sign);
1845 return;
1847 overflow:
1848 get_inf (r, r->sign);
1849 return;
1852 /* Legacy. Similar, but return the result directly. */
1854 REAL_VALUE_TYPE
1855 real_from_string2 (s, mode)
1856 const char *s;
1857 enum machine_mode mode;
1859 REAL_VALUE_TYPE r;
1861 real_from_string (&r, s);
1862 if (mode != VOIDmode)
1863 real_convert (&r, mode, &r);
1865 return r;
1868 /* Initialize R from the integer pair HIGH+LOW. */
1870 void
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;
1875 HOST_WIDE_INT high;
1876 int unsigned_p;
1878 struct real_value *r = (struct real_value *) tr;
1880 if (low == 0 && high == 0)
1881 get_zero (r, 0);
1882 else
1884 r->class = rvc_normal;
1885 r->sign = high < 0 && !unsigned_p;
1886 r->exp = 2 * HOST_BITS_PER_WIDE_INT;
1888 if (r->sign)
1890 high = ~high;
1891 if (low == 0)
1892 high += 1;
1893 else
1894 low = -low;
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;
1909 if (SIGSZ > 4)
1910 memset (r->sig, 0, sizeof(long)*(SIGSZ-4));
1912 else
1913 abort ();
1915 normalize (r);
1918 if (mode != VOIDmode)
1919 real_convert (tr, mode, tr);
1922 /* Returns 10**2**n. */
1924 static const struct real_value *
1925 ten_to_ptwo (n)
1926 int n;
1928 static struct real_value tens[EXP_BITS];
1930 if (n < 0 || n >= EXP_BITS)
1931 abort ();
1933 if (tens[n].class == rvc_zero)
1935 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
1937 HOST_WIDE_INT t = 10;
1938 int i;
1940 for (i = 0; i < n; ++i)
1941 t *= t;
1943 real_from_integer ((REAL_VALUE_TYPE *) &tens[n], VOIDmode, t, 0, 1);
1945 else
1947 const struct real_value *t = ten_to_ptwo (n - 1);
1948 do_multiply (&tens[n], t, t);
1952 return &tens[n];
1955 /* Returns N. */
1957 static const struct real_value *
1958 real_digit (n)
1959 int n;
1961 static struct real_value num[10];
1963 if (n < 0 || n > 9)
1964 abort ();
1966 if (n > 0 && num[n].class == rvc_zero)
1967 real_from_integer ((REAL_VALUE_TYPE *) &num[n], VOIDmode, n, 0, 1);
1969 return &num[n];
1972 /* Fills R with +Inf. */
1974 void
1975 real_inf (tr)
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. */
1986 bool
1987 real_nan (tr, str, quiet, mode)
1988 REAL_VALUE_TYPE *tr;
1989 const char *str;
1990 int quiet;
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];
1997 if (fmt == NULL)
1998 abort ();
2000 if (*str == 0)
2002 if (quiet)
2003 get_canonical_qnan (r, 0);
2004 else
2005 get_canonical_snan (r, 0);
2007 else
2009 int base = 10, d;
2010 bool neg = false;
2012 memset (r, 0, sizeof (*r));
2013 r->class = rvc_nan;
2015 /* Parse akin to strtol into the significand of R. */
2017 while (ISSPACE (*str))
2018 str++;
2019 if (*str == '-')
2020 str++, neg = true;
2021 else if (*str == '+')
2022 str++;
2023 if (*str == '0')
2025 if (*++str == 'x')
2026 str++, base = 16;
2027 else
2028 base = 8;
2031 while ((d = hex_value (*str)) < base)
2033 struct real_value u;
2035 switch (base)
2037 case 8:
2038 lshift_significand (r, r, 3);
2039 break;
2040 case 16:
2041 lshift_significand (r, r, 4);
2042 break;
2043 case 10:
2044 lshift_significand_1 (&u, r);
2045 lshift_significand (r, r, 3);
2046 add_significands (r, r, &u);
2047 break;
2048 default:
2049 abort ();
2052 get_zero (&u, 0);
2053 u.sig[0] = d;
2054 add_significands (r, r, &u);
2056 str++;
2059 /* Must have consumed the entire string for success. */
2060 if (*str != 0)
2061 return false;
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. */
2071 if (quiet)
2072 r->sig[SIGSZ-1] |= SIG_MSB >> 1;
2073 else
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)
2078 if (r->sig[d])
2079 break;
2080 if (d == SIGSZ)
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);
2090 return true;
2093 /* Fills R with 2**N. */
2095 void
2096 real_2expN (tr, n)
2097 REAL_VALUE_TYPE *tr;
2098 int n;
2100 struct real_value *r = (struct real_value *) tr;
2102 memset (r, 0, sizeof (*r));
2104 n++;
2105 if (n > MAX_EXP)
2106 r->class = rvc_inf;
2107 else if (n < -MAX_EXP)
2109 else
2111 r->class = rvc_normal;
2112 r->exp = n;
2113 r->sig[SIGSZ-1] = SIG_MSB;
2118 static void
2119 round_for_format (fmt, r)
2120 const struct real_format *fmt;
2121 struct real_value *r;
2123 int p2, np2, i, w;
2124 unsigned long sticky;
2125 bool guard, lsb;
2126 int emin2m1, emax2;
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;
2133 switch (r->class)
2135 underflow:
2136 get_zero (r, r->sign);
2137 case rvc_zero:
2138 if (!fmt->has_signed_zero)
2139 r->sign = 0;
2140 return;
2142 overflow:
2143 get_inf (r, r->sign);
2144 case rvc_inf:
2145 return;
2147 case rvc_nan:
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)
2153 if (r->sig[i])
2154 break;
2155 if (i == SIGSZ)
2156 r->sig[SIGSZ-1] = SIG_MSB >> 2;
2157 return;
2159 case rvc_normal:
2160 break;
2162 default:
2163 abort ();
2166 /* If we're not base2, normalize the exponent to a multiple of
2167 the true base. */
2168 if (fmt->log2_b != 1)
2170 int shift = r->exp & (fmt->log2_b - 1);
2171 if (shift)
2173 shift = fmt->log2_b - shift;
2174 sticky_rshift_significand (r, r, shift);
2175 r->exp += shift;
2179 /* Check the range of the exponent. If we're out of range,
2180 either underflow or overflow. */
2181 if (r->exp > emax2)
2182 goto overflow;
2183 else if (r->exp <= emin2m1)
2185 int diff;
2187 if (!fmt->has_denorm)
2189 /* Don't underflow completely until we've had a chance to round. */
2190 if (r->exp < emin2m1)
2191 goto underflow;
2193 else
2195 diff = emin2m1 - r->exp + 1;
2196 if (diff > p2)
2197 goto underflow;
2199 /* De-normalize the significand. */
2200 sticky_rshift_significand (r, r, diff);
2201 r->exp += 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. */
2209 sticky = 0;
2210 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2211 sticky |= r->sig[i];
2212 sticky |=
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;
2222 get_zero (&u, 0);
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)
2231 goto overflow;
2232 r->sig[SIGSZ-1] = SIG_MSB;
2234 if (fmt->log2_b != 1)
2236 int shift = r->exp & (fmt->log2_b - 1);
2237 if (shift)
2239 shift = fmt->log2_b - shift;
2240 sticky_rshift_significand (r, r, shift);
2241 r->exp += shift;
2242 if (r->exp > emax2)
2243 goto overflow;
2249 /* Catch underflow that we deferred until after rounding. */
2250 if (r->exp <= emin2m1)
2251 goto underflow;
2253 /* Clear out trailing garbage. */
2254 clear_significand_below (r, np2);
2257 /* Extend or truncate to a new mode. */
2259 void
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];
2270 if (fmt == NULL)
2271 abort ();
2273 *r = *a;
2274 round_for_format (fmt, r);
2276 /* round_for_format de-normalizes denormals. Undo just that part. */
2277 if (r->class == rvc_normal)
2278 normalize (r);
2281 /* Legacy. Likewise, except return the struct directly. */
2283 REAL_VALUE_TYPE
2284 real_value_truncate (mode, a)
2285 enum machine_mode mode;
2286 REAL_VALUE_TYPE a;
2288 REAL_VALUE_TYPE r;
2289 real_convert (&r, mode, &a);
2290 return r;
2293 /* Return true if truncating to MODE is exact. */
2295 bool
2296 exact_real_truncate (mode, ta)
2297 enum machine_mode mode;
2298 const REAL_VALUE_TYPE *ta;
2300 REAL_VALUE_TYPE t;
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. */
2311 long
2312 real_to_target (buf, tr, mode)
2313 long *buf;
2314 const REAL_VALUE_TYPE *tr;
2315 enum machine_mode mode;
2317 struct real_value r;
2318 const struct real_format *fmt;
2319 long buf1;
2321 r = *(const struct real_value *) tr;
2323 fmt = fmt_for_mode[mode - QFmode];
2324 if (fmt == NULL)
2325 abort ();
2327 round_for_format (fmt, &r);
2328 if (!buf)
2329 buf = &buf1;
2330 (*fmt->encode) (fmt, buf, &r);
2332 return *buf;
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. */
2339 void
2340 real_from_target (tr, buf, mode)
2341 REAL_VALUE_TYPE *tr;
2342 const long *buf;
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];
2349 if (fmt == NULL)
2350 abort ();
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];
2365 if (fmt == NULL)
2366 return 0;
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 *));
2378 static void
2379 encode_ieee_single (fmt, buf, r)
2380 const struct real_format *fmt;
2381 long *buf;
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;
2390 switch (r->class)
2392 case rvc_zero:
2393 break;
2395 case rvc_inf:
2396 if (fmt->has_inf)
2397 image |= 255 << 23;
2398 else
2399 image |= 0x7fffffff;
2400 break;
2402 case rvc_nan:
2403 if (fmt->has_nans)
2405 image |= 255 << 23;
2406 image |= sig;
2407 if (!fmt->qnan_msb_set)
2408 image ^= 1 << 23 | 1 << 22;
2410 else
2411 image |= 0x7fffffff;
2412 break;
2414 case rvc_normal:
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. */
2418 if (denormal)
2419 exp = 0;
2420 else
2421 exp = r->exp + 127 - 1;
2422 image |= exp << 23;
2423 image |= sig;
2424 break;
2427 buf[0] = image;
2430 static void
2431 decode_ieee_single (fmt, r, buf)
2432 const struct real_format *fmt;
2433 struct real_value *r;
2434 const long *buf;
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;
2442 image &= ~SIG_MSB;
2444 if (exp == 0)
2446 if (image && fmt->has_denorm)
2448 r->class = rvc_normal;
2449 r->sign = sign;
2450 r->exp = -126;
2451 r->sig[SIGSZ-1] = image << 1;
2452 normalize (r);
2454 else if (fmt->has_signed_zero)
2455 r->sign = sign;
2457 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2459 if (image)
2461 r->class = rvc_nan;
2462 r->sign = sign;
2463 if (!fmt->qnan_msb_set)
2464 image ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2465 r->sig[SIGSZ-1] = image;
2467 else
2469 r->class = rvc_inf;
2470 r->sign = sign;
2473 else
2475 r->class = rvc_normal;
2476 r->sign = sign;
2477 r->exp = exp - 127 + 1;
2478 r->sig[SIGSZ-1] = image | SIG_MSB;
2482 const struct real_format ieee_single =
2484 encode_ieee_single,
2485 decode_ieee_single,
2489 -125,
2490 128,
2491 true,
2492 true,
2493 true,
2494 true,
2495 true
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 *));
2506 static void
2507 encode_ieee_double (fmt, buf, r)
2508 const struct real_format *fmt;
2509 long *buf;
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;
2516 image_lo = 0;
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;
2524 else
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;
2532 switch (r->class)
2534 case rvc_zero:
2535 break;
2537 case rvc_inf:
2538 if (fmt->has_inf)
2539 image_hi |= 2047 << 20;
2540 else
2542 image_hi |= 0x7fffffff;
2543 image_lo = 0xffffffff;
2545 break;
2547 case rvc_nan:
2548 if (fmt->has_nans)
2550 image_hi |= 2047 << 20;
2551 image_hi |= sig_hi;
2552 if (!fmt->qnan_msb_set)
2553 image_hi ^= 1 << 19 | 1 << 18;
2554 image_lo = sig_lo;
2556 else
2558 image_hi |= 0x7fffffff;
2559 image_lo = 0xffffffff;
2561 break;
2563 case rvc_normal:
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. */
2567 if (denormal)
2568 exp = 0;
2569 else
2570 exp = r->exp + 1023 - 1;
2571 image_hi |= exp << 20;
2572 image_hi |= sig_hi;
2573 image_lo = sig_lo;
2574 break;
2577 if (FLOAT_WORDS_BIG_ENDIAN)
2578 buf[0] = image_hi, buf[1] = image_lo;
2579 else
2580 buf[0] = image_lo, buf[1] = image_hi;
2583 static void
2584 decode_ieee_double (fmt, r, buf)
2585 const struct real_format *fmt;
2586 struct real_value *r;
2587 const long *buf;
2589 unsigned long image_hi, image_lo;
2590 bool sign;
2591 int exp;
2593 if (FLOAT_WORDS_BIG_ENDIAN)
2594 image_hi = buf[0], image_lo = buf[1];
2595 else
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;
2610 if (exp == 0)
2612 if ((image_hi || image_lo) && fmt->has_denorm)
2614 r->class = rvc_normal;
2615 r->sign = sign;
2616 r->exp = -1022;
2617 if (HOST_BITS_PER_LONG == 32)
2619 image_hi = (image_hi << 1) | (image_lo >> 31);
2620 image_lo <<= 1;
2621 r->sig[SIGSZ-1] = image_hi;
2622 r->sig[SIGSZ-2] = image_lo;
2624 else
2626 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2627 r->sig[SIGSZ-1] = image_hi;
2629 normalize (r);
2631 else if (fmt->has_signed_zero)
2632 r->sign = sign;
2634 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2636 if (image_hi || image_lo)
2638 r->class = rvc_nan;
2639 r->sign = sign;
2640 if (HOST_BITS_PER_LONG == 32)
2642 r->sig[SIGSZ-1] = image_hi;
2643 r->sig[SIGSZ-2] = image_lo;
2645 else
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);
2651 else
2653 r->class = rvc_inf;
2654 r->sign = sign;
2657 else
2659 r->class = rvc_normal;
2660 r->sign = sign;
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;
2667 else
2668 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2672 const struct real_format ieee_double =
2674 encode_ieee_double,
2675 decode_ieee_double,
2679 -1021,
2680 1024,
2681 true,
2682 true,
2683 true,
2684 true,
2685 true
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,
2691 and Motorola's. */
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,
2699 long *,
2700 const struct real_value *));
2701 static void decode_ieee_extended_128 PARAMS ((const struct real_format *,
2702 struct real_value *,
2703 const long *));
2705 static void
2706 encode_ieee_extended (fmt, buf, r)
2707 const struct real_format *fmt;
2708 long *buf;
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;
2717 switch (r->class)
2719 case rvc_zero:
2720 break;
2722 case rvc_inf:
2723 if (fmt->has_inf)
2725 image_hi |= 32767;
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;
2732 else
2734 image_hi |= 32767;
2735 sig_lo = sig_hi = 0xffffffff;
2737 break;
2739 case rvc_nan:
2740 if (fmt->has_nans)
2742 image_hi |= 32767;
2743 if (HOST_BITS_PER_LONG == 32)
2745 sig_hi = r->sig[SIGSZ-1];
2746 sig_lo = r->sig[SIGSZ-2];
2748 else
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
2759 doesn't care. */
2760 sig_hi |= 0x80000000;
2762 else
2764 image_hi |= 32767;
2765 sig_lo = sig_hi = 0xffffffff;
2767 break;
2769 case rvc_normal:
2771 int exp = r->exp;
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. */
2782 if (denormal)
2783 exp = 0;
2784 else
2786 exp += 16383 - 1;
2787 if (exp < 0)
2788 abort ();
2790 image_hi |= exp;
2792 if (HOST_BITS_PER_LONG == 32)
2794 sig_hi = r->sig[SIGSZ-1];
2795 sig_lo = r->sig[SIGSZ-2];
2797 else
2799 sig_lo = r->sig[SIGSZ-1];
2800 sig_hi = sig_lo >> 31 >> 1;
2801 sig_lo &= 0xffffffff;
2804 break;
2807 if (FLOAT_WORDS_BIG_ENDIAN)
2808 buf[0] = image_hi << 16, buf[1] = sig_hi, buf[2] = sig_lo;
2809 else
2810 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
2813 static void
2814 encode_ieee_extended_128 (fmt, buf, r)
2815 const struct real_format *fmt;
2816 long *buf;
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);
2823 static void
2824 decode_ieee_extended (fmt, r, buf)
2825 const struct real_format *fmt;
2826 struct real_value *r;
2827 const long *buf;
2829 unsigned long image_hi, sig_hi, sig_lo;
2830 bool sign;
2831 int exp;
2833 if (FLOAT_WORDS_BIG_ENDIAN)
2834 image_hi = buf[0] >> 16, sig_hi = buf[1], sig_lo = buf[2];
2835 else
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));
2846 if (exp == 0)
2848 if ((sig_hi || sig_lo) && fmt->has_denorm)
2850 r->class = rvc_normal;
2851 r->sign = sign;
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. */
2858 r->exp = fmt->emin;
2859 if (HOST_BITS_PER_LONG == 32)
2861 r->sig[SIGSZ-1] = sig_hi;
2862 r->sig[SIGSZ-2] = sig_lo;
2864 else
2865 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2867 normalize (r);
2869 else if (fmt->has_signed_zero)
2870 r->sign = sign;
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)
2881 r->class = rvc_nan;
2882 r->sign = sign;
2883 if (HOST_BITS_PER_LONG == 32)
2885 r->sig[SIGSZ-1] = sig_hi;
2886 r->sig[SIGSZ-2] = sig_lo;
2888 else
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);
2894 else
2896 r->class = rvc_inf;
2897 r->sign = sign;
2900 else
2902 r->class = rvc_normal;
2903 r->sign = sign;
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;
2910 else
2911 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2915 static void
2916 decode_ieee_extended_128 (fmt, r, buf)
2917 const struct real_format *fmt;
2918 struct real_value *r;
2919 const long *buf;
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,
2931 -16382,
2932 16384,
2933 true,
2934 true,
2935 true,
2936 true,
2937 true
2940 const struct real_format ieee_extended_intel_96 =
2942 encode_ieee_extended,
2943 decode_ieee_extended,
2947 -16381,
2948 16384,
2949 true,
2950 true,
2951 true,
2952 true,
2953 true
2956 const struct real_format ieee_extended_intel_128 =
2958 encode_ieee_extended_128,
2959 decode_ieee_extended_128,
2963 -16381,
2964 16384,
2965 true,
2966 true,
2967 true,
2968 true,
2969 true
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 *));
2980 static void
2981 encode_ieee_quad (fmt, buf, r)
2982 const struct real_format *fmt;
2983 long *buf;
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;
2991 image2 = 0;
2992 image1 = 0;
2993 image0 = 0;
2995 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
2997 switch (r->class)
2999 case rvc_zero:
3000 break;
3002 case rvc_inf:
3003 if (fmt->has_inf)
3004 image3 |= 32767 << 16;
3005 else
3007 image3 |= 0x7fffffff;
3008 image2 = 0xffffffff;
3009 image1 = 0xffffffff;
3010 image0 = 0xffffffff;
3012 break;
3014 case rvc_nan:
3015 if (fmt->has_nans)
3017 image3 |= 32767 << 16;
3019 if (HOST_BITS_PER_LONG == 32)
3021 image0 = u.sig[0];
3022 image1 = u.sig[1];
3023 image2 = u.sig[2];
3024 image3 |= u.sig[3] & 0xffff;
3026 else
3028 image0 = u.sig[0];
3029 image1 = image0 >> 31 >> 1;
3030 image2 = u.sig[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;
3039 else
3041 image3 |= 0x7fffffff;
3042 image2 = 0xffffffff;
3043 image1 = 0xffffffff;
3044 image0 = 0xffffffff;
3046 break;
3048 case rvc_normal:
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. */
3052 if (denormal)
3053 exp = 0;
3054 else
3055 exp = r->exp + 16383 - 1;
3056 image3 |= exp << 16;
3058 if (HOST_BITS_PER_LONG == 32)
3060 image0 = u.sig[0];
3061 image1 = u.sig[1];
3062 image2 = u.sig[2];
3063 image3 |= u.sig[3] & 0xffff;
3065 else
3067 image0 = u.sig[0];
3068 image1 = image0 >> 31 >> 1;
3069 image2 = u.sig[1];
3070 image3 |= (image2 >> 31 >> 1) & 0xffff;
3071 image0 &= 0xffffffff;
3072 image2 &= 0xffffffff;
3074 break;
3077 if (FLOAT_WORDS_BIG_ENDIAN)
3079 buf[0] = image3;
3080 buf[1] = image2;
3081 buf[2] = image1;
3082 buf[3] = image0;
3084 else
3086 buf[0] = image0;
3087 buf[1] = image1;
3088 buf[2] = image2;
3089 buf[3] = image3;
3093 static void
3094 decode_ieee_quad (fmt, r, buf)
3095 const struct real_format *fmt;
3096 struct real_value *r;
3097 const long *buf;
3099 unsigned long image3, image2, image1, image0;
3100 bool sign;
3101 int exp;
3103 if (FLOAT_WORDS_BIG_ENDIAN)
3105 image3 = buf[0];
3106 image2 = buf[1];
3107 image1 = buf[2];
3108 image0 = buf[3];
3110 else
3112 image0 = buf[0];
3113 image1 = buf[1];
3114 image2 = buf[2];
3115 image3 = buf[3];
3117 image0 &= 0xffffffff;
3118 image1 &= 0xffffffff;
3119 image2 &= 0xffffffff;
3121 sign = (image3 >> 31) & 1;
3122 exp = (image3 >> 16) & 0x7fff;
3123 image3 &= 0xffff;
3125 memset (r, 0, sizeof (*r));
3127 if (exp == 0)
3129 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3131 r->class = rvc_normal;
3132 r->sign = sign;
3134 r->exp = -16382 + (SIGNIFICAND_BITS - 112);
3135 if (HOST_BITS_PER_LONG == 32)
3137 r->sig[0] = image0;
3138 r->sig[1] = image1;
3139 r->sig[2] = image2;
3140 r->sig[3] = image3;
3142 else
3144 r->sig[0] = (image1 << 31 << 1) | image0;
3145 r->sig[1] = (image3 << 31 << 1) | image2;
3148 normalize (r);
3150 else if (fmt->has_signed_zero)
3151 r->sign = sign;
3153 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3155 if (image3 | image2 | image1 | image0)
3157 r->class = rvc_nan;
3158 r->sign = sign;
3160 if (HOST_BITS_PER_LONG == 32)
3162 r->sig[0] = image0;
3163 r->sig[1] = image1;
3164 r->sig[2] = image2;
3165 r->sig[3] = image3;
3167 else
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);
3177 else
3179 r->class = rvc_inf;
3180 r->sign = sign;
3183 else
3185 r->class = rvc_normal;
3186 r->sign = sign;
3187 r->exp = exp - 16383 + 1;
3189 if (HOST_BITS_PER_LONG == 32)
3191 r->sig[0] = image0;
3192 r->sig[1] = image1;
3193 r->sig[2] = image2;
3194 r->sig[3] = image3;
3196 else
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 =
3208 encode_ieee_quad,
3209 decode_ieee_quad,
3212 113,
3213 -16382,
3214 16384,
3215 true,
3216 true,
3217 true,
3218 true,
3219 true
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 *));
3238 static void
3239 encode_vax_f (fmt, buf, r)
3240 const struct real_format *fmt ATTRIBUTE_UNUSED;
3241 long *buf;
3242 const struct real_value *r;
3244 unsigned long sign, exp, sig, image;
3246 sign = r->sign << 15;
3248 switch (r->class)
3250 case rvc_zero:
3251 image = 0;
3252 break;
3254 case rvc_inf:
3255 case rvc_nan:
3256 image = 0xffff7fff | sign;
3257 break;
3259 case rvc_normal:
3260 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3261 exp = r->exp + 128;
3263 image = (sig << 16) & 0xffff0000;
3264 image |= sign;
3265 image |= exp << 7;
3266 image |= sig >> 16;
3267 break;
3270 buf[0] = image;
3273 static void
3274 decode_vax_f (fmt, r, buf)
3275 const struct real_format *fmt ATTRIBUTE_UNUSED;
3276 struct real_value *r;
3277 const long *buf;
3279 unsigned long image = buf[0] & 0xffffffff;
3280 int exp = (image >> 7) & 0xff;
3282 memset (r, 0, sizeof (*r));
3284 if (exp != 0)
3286 r->class = rvc_normal;
3287 r->sign = (image >> 15) & 1;
3288 r->exp = exp - 128;
3290 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3291 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3295 static void
3296 encode_vax_d (fmt, buf, r)
3297 const struct real_format *fmt ATTRIBUTE_UNUSED;
3298 long *buf;
3299 const struct real_value *r;
3301 unsigned long image0, image1, sign = r->sign << 15;
3303 switch (r->class)
3305 case rvc_zero:
3306 image0 = image1 = 0;
3307 break;
3309 case rvc_inf:
3310 case rvc_nan:
3311 image0 = 0xffff7fff | sign;
3312 image1 = 0xffffffff;
3313 break;
3315 case rvc_normal:
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;
3323 else
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
3332 external format. */
3333 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3334 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3336 /* Add the sign and exponent. */
3337 image0 |= sign;
3338 image0 |= (r->exp + 128) << 7;
3339 break;
3342 if (FLOAT_WORDS_BIG_ENDIAN)
3343 buf[0] = image1, buf[1] = image0;
3344 else
3345 buf[0] = image0, buf[1] = image1;
3348 static void
3349 decode_vax_d (fmt, r, buf)
3350 const struct real_format *fmt ATTRIBUTE_UNUSED;
3351 struct real_value *r;
3352 const long *buf;
3354 unsigned long image0, image1;
3355 int exp;
3357 if (FLOAT_WORDS_BIG_ENDIAN)
3358 image1 = buf[0], image0 = buf[1];
3359 else
3360 image0 = buf[0], image1 = buf[1];
3361 image0 &= 0xffffffff;
3362 image1 &= 0xffffffff;
3364 exp = (image0 >> 7) & 0x7f;
3366 memset (r, 0, sizeof (*r));
3368 if (exp != 0)
3370 r->class = rvc_normal;
3371 r->sign = (image0 >> 15) & 1;
3372 r->exp = exp - 128;
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;
3382 image0 <<= 64 - 56;
3383 image0 |= SIG_MSB;
3384 r->sig[SIGSZ-1] = image0;
3386 else
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;
3396 static void
3397 encode_vax_g (fmt, buf, r)
3398 const struct real_format *fmt ATTRIBUTE_UNUSED;
3399 long *buf;
3400 const struct real_value *r;
3402 unsigned long image0, image1, sign = r->sign << 15;
3404 switch (r->class)
3406 case rvc_zero:
3407 image0 = image1 = 0;
3408 break;
3410 case rvc_inf:
3411 case rvc_nan:
3412 image0 = 0xffff7fff | sign;
3413 image1 = 0xffffffff;
3414 break;
3416 case rvc_normal:
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;
3424 else
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
3433 external format. */
3434 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
3435 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3437 /* Add the sign and exponent. */
3438 image0 |= sign;
3439 image0 |= (r->exp + 1024) << 4;
3440 break;
3443 if (FLOAT_WORDS_BIG_ENDIAN)
3444 buf[0] = image1, buf[1] = image0;
3445 else
3446 buf[0] = image0, buf[1] = image1;
3449 static void
3450 decode_vax_g (fmt, r, buf)
3451 const struct real_format *fmt ATTRIBUTE_UNUSED;
3452 struct real_value *r;
3453 const long *buf;
3455 unsigned long image0, image1;
3456 int exp;
3458 if (FLOAT_WORDS_BIG_ENDIAN)
3459 image1 = buf[0], image0 = buf[1];
3460 else
3461 image0 = buf[0], image1 = buf[1];
3462 image0 &= 0xffffffff;
3463 image1 &= 0xffffffff;
3465 exp = (image0 >> 4) & 0x7ff;
3467 memset (r, 0, sizeof (*r));
3469 if (exp != 0)
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;
3483 image0 <<= 64 - 53;
3484 image0 |= SIG_MSB;
3485 r->sig[SIGSZ-1] = image0;
3487 else
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 =
3499 encode_vax_f,
3500 decode_vax_f,
3504 -127,
3505 127,
3506 false,
3507 false,
3508 false,
3509 false,
3510 false
3513 const struct real_format vax_d_format =
3515 encode_vax_d,
3516 decode_vax_d,
3520 -127,
3521 127,
3522 false,
3523 false,
3524 false,
3525 false,
3526 false
3529 const struct real_format vax_g_format =
3531 encode_vax_g,
3532 decode_vax_g,
3536 -1023,
3537 1023,
3538 false,
3539 false,
3540 false,
3541 false,
3542 false
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 *));
3562 static void
3563 encode_i370_single (fmt, buf, r)
3564 const struct real_format *fmt ATTRIBUTE_UNUSED;
3565 long *buf;
3566 const struct real_value *r;
3568 unsigned long sign, exp, sig, image;
3570 sign = r->sign << 31;
3572 switch (r->class)
3574 case rvc_zero:
3575 image = 0;
3576 break;
3578 case rvc_inf:
3579 case rvc_nan:
3580 image = 0x7fffffff | sign;
3581 break;
3583 case rvc_normal:
3584 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
3585 exp = ((r->exp / 4) + 64) << 24;
3586 image = sign | exp | sig;
3587 break;
3590 buf[0] = image;
3593 static void
3594 decode_i370_single (fmt, r, buf)
3595 const struct real_format *fmt ATTRIBUTE_UNUSED;
3596 struct real_value *r;
3597 const long *buf;
3599 unsigned long sign, sig, image = buf[0];
3600 int exp;
3602 sign = (image >> 31) & 1;
3603 exp = (image >> 24) & 0x7f;
3604 sig = image & 0xffffff;
3606 memset (r, 0, sizeof (*r));
3608 if (exp || sig)
3610 r->class = rvc_normal;
3611 r->sign = sign;
3612 r->exp = (exp - 64) * 4;
3613 r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
3614 normalize (r);
3618 static void
3619 encode_i370_double (fmt, buf, r)
3620 const struct real_format *fmt ATTRIBUTE_UNUSED;
3621 long *buf;
3622 const struct real_value *r;
3624 unsigned long sign, exp, image_hi, image_lo;
3626 sign = r->sign << 31;
3628 switch (r->class)
3630 case rvc_zero:
3631 image_hi = image_lo = 0;
3632 break;
3634 case rvc_inf:
3635 case rvc_nan:
3636 image_hi = 0x7fffffff | sign;
3637 image_lo = 0xffffffff;
3638 break;
3640 case rvc_normal:
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;
3647 else
3649 image_hi = r->sig[SIGSZ-1];
3650 image_lo = r->sig[SIGSZ-2];
3651 image_lo = (image_lo >> 8) | (image_hi << 24);
3652 image_hi >>= 8;
3655 exp = ((r->exp / 4) + 64) << 24;
3656 image_hi |= sign | exp;
3657 break;
3660 if (FLOAT_WORDS_BIG_ENDIAN)
3661 buf[0] = image_hi, buf[1] = image_lo;
3662 else
3663 buf[0] = image_lo, buf[1] = image_hi;
3666 static void
3667 decode_i370_double (fmt, r, buf)
3668 const struct real_format *fmt ATTRIBUTE_UNUSED;
3669 struct real_value *r;
3670 const long *buf;
3672 unsigned long sign, image_hi, image_lo;
3673 int exp;
3675 if (FLOAT_WORDS_BIG_ENDIAN)
3676 image_hi = buf[0], image_lo = buf[1];
3677 else
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;
3690 r->sign = sign;
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;
3698 else
3699 r->sig[0] = image_lo | (image_hi << 31 << 1);
3701 normalize (r);
3705 const struct real_format i370_single =
3707 encode_i370_single,
3708 decode_i370_single,
3712 -64,
3714 false,
3715 false,
3716 false, /* ??? The encoding does allow for "unnormals". */
3717 false, /* ??? The encoding does allow for "unnormals". */
3718 false
3721 const struct real_format i370_double =
3723 encode_i370_double,
3724 decode_i370_double,
3728 -64,
3730 false,
3731 false,
3732 false, /* ??? The encoding does allow for "unnormals". */
3733 false, /* ??? The encoding does allow for "unnormals". */
3734 false
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 *));
3749 static void
3750 encode_c4x_single (fmt, buf, r)
3751 const struct real_format *fmt ATTRIBUTE_UNUSED;
3752 long *buf;
3753 const struct real_value *r;
3755 unsigned long image, exp, sig;
3757 switch (r->class)
3759 case rvc_zero:
3760 exp = -128;
3761 sig = 0;
3762 break;
3764 case rvc_inf:
3765 case rvc_nan:
3766 exp = 127;
3767 sig = 0x800000 - r->sign;
3768 break;
3770 case rvc_normal:
3771 exp = r->exp - 1;
3772 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3773 if (r->sign)
3775 if (sig)
3776 sig = -sig;
3777 else
3778 exp--;
3779 sig |= 0x800000;
3781 break;
3784 image = ((exp & 0xff) << 24) | (sig & 0xffffff);
3785 buf[0] = image;
3788 static void
3789 decode_c4x_single (fmt, r, buf)
3790 const struct real_format *fmt ATTRIBUTE_UNUSED;
3791 struct real_value *r;
3792 const long *buf;
3794 unsigned long image = buf[0];
3795 unsigned long sig;
3796 int exp, sf;
3798 exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
3799 sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
3801 memset (r, 0, sizeof (*r));
3803 if (exp != -128)
3805 r->class = rvc_normal;
3807 sig = sf & 0x7fffff;
3808 if (sf < 0)
3810 r->sign = 1;
3811 if (sig)
3812 sig = -sig;
3813 else
3814 exp++;
3816 sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3818 r->exp = exp + 1;
3819 r->sig[SIGSZ-1] = sig;
3823 static void
3824 encode_c4x_extended (fmt, buf, r)
3825 const struct real_format *fmt ATTRIBUTE_UNUSED;
3826 long *buf;
3827 const struct real_value *r;
3829 unsigned long exp, sig;
3831 switch (r->class)
3833 case rvc_zero:
3834 exp = -128;
3835 sig = 0;
3836 break;
3838 case rvc_inf:
3839 case rvc_nan:
3840 exp = 127;
3841 sig = 0x80000000 - r->sign;
3842 break;
3844 case rvc_normal:
3845 exp = r->exp - 1;
3847 sig = r->sig[SIGSZ-1];
3848 if (HOST_BITS_PER_LONG == 64)
3849 sig = sig >> 1 >> 31;
3850 sig &= 0x7fffffff;
3852 if (r->sign)
3854 if (sig)
3855 sig = -sig;
3856 else
3857 exp--;
3858 sig |= 0x80000000;
3860 break;
3863 exp = (exp & 0xff) << 24;
3864 sig &= 0xffffffff;
3866 if (FLOAT_WORDS_BIG_ENDIAN)
3867 buf[0] = exp, buf[1] = sig;
3868 else
3869 buf[0] = sig, buf[0] = exp;
3872 static void
3873 decode_c4x_extended (fmt, r, buf)
3874 const struct real_format *fmt ATTRIBUTE_UNUSED;
3875 struct real_value *r;
3876 const long *buf;
3878 unsigned long sig;
3879 int exp, sf;
3881 if (FLOAT_WORDS_BIG_ENDIAN)
3882 exp = buf[0], sf = buf[1];
3883 else
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));
3891 if (exp != -128)
3893 r->class = rvc_normal;
3895 sig = sf & 0x7fffffff;
3896 if (sf < 0)
3898 r->sign = 1;
3899 if (sig)
3900 sig = -sig;
3901 else
3902 exp++;
3904 if (HOST_BITS_PER_LONG == 64)
3905 sig = sig << 1 << 31;
3906 sig |= SIG_MSB;
3908 r->exp = exp + 1;
3909 r->sig[SIGSZ-1] = sig;
3913 const struct real_format c4x_single =
3915 encode_c4x_single,
3916 decode_c4x_single,
3920 -126,
3921 128,
3922 false,
3923 false,
3924 false,
3925 false,
3926 false
3929 const struct real_format c4x_extended =
3931 encode_c4x_extended,
3932 decode_c4x_extended,
3936 -126,
3937 128,
3938 false,
3939 false,
3940 false,
3941 false,
3942 false
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)
3952 int size;
3954 #ifndef TARGET_G_FORMAT
3955 #define TARGET_G_FORMAT 0
3956 #endif
3958 switch (TARGET_FLOAT_FORMAT)
3960 case IEEE_FLOAT_FORMAT:
3961 switch (size)
3963 case 32:
3964 return &ieee_single;
3966 case 64:
3967 return &ieee_double;
3969 case 96:
3970 if (!INTEL_EXTENDED_IEEE_FORMAT)
3971 return &ieee_extended_motorola;
3972 else
3973 return &ieee_extended_intel_96;
3975 case 128:
3976 if (!INTEL_EXTENDED_IEEE_FORMAT)
3977 return &ieee_quad;
3978 else
3979 return &ieee_extended_intel_128;
3981 break;
3983 case VAX_FLOAT_FORMAT:
3984 switch (size)
3986 case 32:
3987 return &vax_f_format;
3989 case 64:
3990 if (TARGET_G_FORMAT)
3991 return &vax_g_format;
3992 else
3993 return &vax_d_format;
3995 break;
3997 case IBM_FLOAT_FORMAT:
3998 switch (size)
4000 case 32:
4001 return &i370_single;
4002 case 64:
4003 return &i370_double;
4005 break;
4007 case C4X_FLOAT_FORMAT:
4008 switch (size)
4010 case 32:
4011 return &c4x_single;
4012 case 64:
4013 return &c4x_extended;
4015 break;
4018 abort ();
4021 void
4022 init_real_once ()
4024 int i;
4026 /* Set up the mode->format table. */
4027 for (i = 0; i < 3; ++i)
4029 enum machine_mode mode;
4030 int size;
4032 if (i == 0)
4033 size = FLOAT_TYPE_SIZE;
4034 else if (i == 1)
4035 size = DOUBLE_TYPE_SIZE;
4036 else
4037 size = LONG_DOUBLE_TYPE_SIZE;
4039 mode = mode_for_size (size, MODE_FLOAT, 0);
4040 if (mode == BLKmode)
4041 abort ();
4043 fmt_for_mode[mode - QFmode] = format_for_size (size);