2007-05-01 H.J. Lu <hongjiu.lu@intel.com>
[official-gcc.git] / gcc / real.c
blob48d9e9e23fa3bd4728db466bd5a32c22199ffaf0
1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "toplev.h"
30 #include "real.h"
31 #include "tm_p.h"
32 #include "dfp.h"
34 /* The floating point model used internally is not exactly IEEE 754
35 compliant, and close to the description in the ISO C99 standard,
36 section 5.2.4.2.2 Characteristics of floating types.
38 Specifically
40 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
42 where
43 s = sign (+- 1)
44 b = base or radix, here always 2
45 e = exponent
46 p = precision (the number of base-b digits in the significand)
47 f_k = the digits of the significand.
49 We differ from typical IEEE 754 encodings in that the entire
50 significand is fractional. Normalized significands are in the
51 range [0.5, 1.0).
53 A requirement of the model is that P be larger than the largest
54 supported target floating-point type by at least 2 bits. This gives
55 us proper rounding when we truncate to the target type. In addition,
56 E must be large enough to hold the smallest supported denormal number
57 in a normalized form.
59 Both of these requirements are easily satisfied. The largest target
60 significand is 113 bits; we store at least 160. The smallest
61 denormal number fits in 17 exponent bits; we store 27.
63 Note that the decimal string conversion routines are sensitive to
64 rounding errors. Since the raw arithmetic routines do not themselves
65 have guard digits or rounding, the computation of 10**exp can
66 accumulate more than a few digits of error. The previous incarnation
67 of real.c successfully used a 144-bit fraction; given the current
68 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits.
70 Target floating point models that use base 16 instead of base 2
71 (i.e. IBM 370), are handled during round_for_format, in which we
72 canonicalize the exponent to be a multiple of 4 (log2(16)), and
73 adjust the significand to match. */
76 /* Used to classify two numbers simultaneously. */
77 #define CLASS2(A, B) ((A) << 2 | (B))
79 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
80 #error "Some constant folding done by hand to avoid shift count warnings"
81 #endif
83 static void get_zero (REAL_VALUE_TYPE *, int);
84 static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
85 static void get_canonical_snan (REAL_VALUE_TYPE *, int);
86 static void get_inf (REAL_VALUE_TYPE *, int);
87 static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
88 const REAL_VALUE_TYPE *, unsigned int);
89 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
90 unsigned int);
91 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
92 unsigned int);
93 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
94 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
95 const REAL_VALUE_TYPE *);
96 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
97 const REAL_VALUE_TYPE *, int);
98 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
99 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
100 static int cmp_significand_0 (const REAL_VALUE_TYPE *);
101 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
102 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
103 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
104 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
105 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
106 const REAL_VALUE_TYPE *);
107 static void normalize (REAL_VALUE_TYPE *);
109 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
110 const REAL_VALUE_TYPE *, int);
111 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
112 const REAL_VALUE_TYPE *);
113 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
114 const REAL_VALUE_TYPE *);
115 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
116 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
118 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
120 static const REAL_VALUE_TYPE * ten_to_ptwo (int);
121 static const REAL_VALUE_TYPE * ten_to_mptwo (int);
122 static const REAL_VALUE_TYPE * real_digit (int);
123 static void times_pten (REAL_VALUE_TYPE *, int);
125 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
127 /* Initialize R with a positive zero. */
129 static inline void
130 get_zero (REAL_VALUE_TYPE *r, int sign)
132 memset (r, 0, sizeof (*r));
133 r->sign = sign;
136 /* Initialize R with the canonical quiet NaN. */
138 static inline void
139 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
141 memset (r, 0, sizeof (*r));
142 r->cl = rvc_nan;
143 r->sign = sign;
144 r->canonical = 1;
147 static inline void
148 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
150 memset (r, 0, sizeof (*r));
151 r->cl = rvc_nan;
152 r->sign = sign;
153 r->signalling = 1;
154 r->canonical = 1;
157 static inline void
158 get_inf (REAL_VALUE_TYPE *r, int sign)
160 memset (r, 0, sizeof (*r));
161 r->cl = rvc_inf;
162 r->sign = sign;
166 /* Right-shift the significand of A by N bits; put the result in the
167 significand of R. If any one bits are shifted out, return true. */
169 static bool
170 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
171 unsigned int n)
173 unsigned long sticky = 0;
174 unsigned int i, ofs = 0;
176 if (n >= HOST_BITS_PER_LONG)
178 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
179 sticky |= a->sig[i];
180 n &= HOST_BITS_PER_LONG - 1;
183 if (n != 0)
185 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
186 for (i = 0; i < SIGSZ; ++i)
188 r->sig[i]
189 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
190 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
191 << (HOST_BITS_PER_LONG - n)));
194 else
196 for (i = 0; ofs + i < SIGSZ; ++i)
197 r->sig[i] = a->sig[ofs + i];
198 for (; i < SIGSZ; ++i)
199 r->sig[i] = 0;
202 return sticky != 0;
205 /* Right-shift the significand of A by N bits; put the result in the
206 significand of R. */
208 static void
209 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
210 unsigned int n)
212 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
214 n &= HOST_BITS_PER_LONG - 1;
215 if (n != 0)
217 for (i = 0; i < SIGSZ; ++i)
219 r->sig[i]
220 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
221 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
222 << (HOST_BITS_PER_LONG - n)));
225 else
227 for (i = 0; ofs + i < SIGSZ; ++i)
228 r->sig[i] = a->sig[ofs + i];
229 for (; i < SIGSZ; ++i)
230 r->sig[i] = 0;
234 /* Left-shift the significand of A by N bits; put the result in the
235 significand of R. */
237 static void
238 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
239 unsigned int n)
241 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
243 n &= HOST_BITS_PER_LONG - 1;
244 if (n == 0)
246 for (i = 0; ofs + i < SIGSZ; ++i)
247 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
248 for (; i < SIGSZ; ++i)
249 r->sig[SIGSZ-1-i] = 0;
251 else
252 for (i = 0; i < SIGSZ; ++i)
254 r->sig[SIGSZ-1-i]
255 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
256 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
257 >> (HOST_BITS_PER_LONG - n)));
261 /* Likewise, but N is specialized to 1. */
263 static inline void
264 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
266 unsigned int i;
268 for (i = SIGSZ - 1; i > 0; --i)
269 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
270 r->sig[0] = a->sig[0] << 1;
273 /* Add the significands of A and B, placing the result in R. Return
274 true if there was carry out of the most significant word. */
276 static inline bool
277 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
278 const REAL_VALUE_TYPE *b)
280 bool carry = false;
281 int i;
283 for (i = 0; i < SIGSZ; ++i)
285 unsigned long ai = a->sig[i];
286 unsigned long ri = ai + b->sig[i];
288 if (carry)
290 carry = ri < ai;
291 carry |= ++ri == 0;
293 else
294 carry = ri < ai;
296 r->sig[i] = ri;
299 return carry;
302 /* Subtract the significands of A and B, placing the result in R. CARRY is
303 true if there's a borrow incoming to the least significant word.
304 Return true if there was borrow out of the most significant word. */
306 static inline bool
307 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
308 const REAL_VALUE_TYPE *b, int carry)
310 int i;
312 for (i = 0; i < SIGSZ; ++i)
314 unsigned long ai = a->sig[i];
315 unsigned long ri = ai - b->sig[i];
317 if (carry)
319 carry = ri > ai;
320 carry |= ~--ri == 0;
322 else
323 carry = ri > ai;
325 r->sig[i] = ri;
328 return carry;
331 /* Negate the significand A, placing the result in R. */
333 static inline void
334 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
336 bool carry = true;
337 int i;
339 for (i = 0; i < SIGSZ; ++i)
341 unsigned long ri, ai = a->sig[i];
343 if (carry)
345 if (ai)
347 ri = -ai;
348 carry = false;
350 else
351 ri = ai;
353 else
354 ri = ~ai;
356 r->sig[i] = ri;
360 /* Compare significands. Return tri-state vs zero. */
362 static inline int
363 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
365 int i;
367 for (i = SIGSZ - 1; i >= 0; --i)
369 unsigned long ai = a->sig[i];
370 unsigned long bi = b->sig[i];
372 if (ai > bi)
373 return 1;
374 if (ai < bi)
375 return -1;
378 return 0;
381 /* Return true if A is nonzero. */
383 static inline int
384 cmp_significand_0 (const REAL_VALUE_TYPE *a)
386 int i;
388 for (i = SIGSZ - 1; i >= 0; --i)
389 if (a->sig[i])
390 return 1;
392 return 0;
395 /* Set bit N of the significand of R. */
397 static inline void
398 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
400 r->sig[n / HOST_BITS_PER_LONG]
401 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
404 /* Clear bit N of the significand of R. */
406 static inline void
407 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
409 r->sig[n / HOST_BITS_PER_LONG]
410 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
413 /* Test bit N of the significand of R. */
415 static inline bool
416 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
418 /* ??? Compiler bug here if we return this expression directly.
419 The conversion to bool strips the "&1" and we wind up testing
420 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
421 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
422 return t;
425 /* Clear bits 0..N-1 of the significand of R. */
427 static void
428 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
430 int i, w = n / HOST_BITS_PER_LONG;
432 for (i = 0; i < w; ++i)
433 r->sig[i] = 0;
435 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
438 /* Divide the significands of A and B, placing the result in R. Return
439 true if the division was inexact. */
441 static inline bool
442 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
443 const REAL_VALUE_TYPE *b)
445 REAL_VALUE_TYPE u;
446 int i, bit = SIGNIFICAND_BITS - 1;
447 unsigned long msb, inexact;
449 u = *a;
450 memset (r->sig, 0, sizeof (r->sig));
452 msb = 0;
453 goto start;
456 msb = u.sig[SIGSZ-1] & SIG_MSB;
457 lshift_significand_1 (&u, &u);
458 start:
459 if (msb || cmp_significands (&u, b) >= 0)
461 sub_significands (&u, &u, b, 0);
462 set_significand_bit (r, bit);
465 while (--bit >= 0);
467 for (i = 0, inexact = 0; i < SIGSZ; i++)
468 inexact |= u.sig[i];
470 return inexact != 0;
473 /* Adjust the exponent and significand of R such that the most
474 significant bit is set. We underflow to zero and overflow to
475 infinity here, without denormals. (The intermediate representation
476 exponent is large enough to handle target denormals normalized.) */
478 static void
479 normalize (REAL_VALUE_TYPE *r)
481 int shift = 0, exp;
482 int i, j;
484 if (r->decimal)
485 return;
487 /* Find the first word that is nonzero. */
488 for (i = SIGSZ - 1; i >= 0; i--)
489 if (r->sig[i] == 0)
490 shift += HOST_BITS_PER_LONG;
491 else
492 break;
494 /* Zero significand flushes to zero. */
495 if (i < 0)
497 r->cl = rvc_zero;
498 SET_REAL_EXP (r, 0);
499 return;
502 /* Find the first bit that is nonzero. */
503 for (j = 0; ; j++)
504 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
505 break;
506 shift += j;
508 if (shift > 0)
510 exp = REAL_EXP (r) - shift;
511 if (exp > MAX_EXP)
512 get_inf (r, r->sign);
513 else if (exp < -MAX_EXP)
514 get_zero (r, r->sign);
515 else
517 SET_REAL_EXP (r, exp);
518 lshift_significand (r, r, shift);
523 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
524 result may be inexact due to a loss of precision. */
526 static bool
527 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
528 const REAL_VALUE_TYPE *b, int subtract_p)
530 int dexp, sign, exp;
531 REAL_VALUE_TYPE t;
532 bool inexact = false;
534 /* Determine if we need to add or subtract. */
535 sign = a->sign;
536 subtract_p = (sign ^ b->sign) ^ subtract_p;
538 switch (CLASS2 (a->cl, b->cl))
540 case CLASS2 (rvc_zero, rvc_zero):
541 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
542 get_zero (r, sign & !subtract_p);
543 return false;
545 case CLASS2 (rvc_zero, rvc_normal):
546 case CLASS2 (rvc_zero, rvc_inf):
547 case CLASS2 (rvc_zero, rvc_nan):
548 /* 0 + ANY = ANY. */
549 case CLASS2 (rvc_normal, rvc_nan):
550 case CLASS2 (rvc_inf, rvc_nan):
551 case CLASS2 (rvc_nan, rvc_nan):
552 /* ANY + NaN = NaN. */
553 case CLASS2 (rvc_normal, rvc_inf):
554 /* R + Inf = Inf. */
555 *r = *b;
556 r->sign = sign ^ subtract_p;
557 return false;
559 case CLASS2 (rvc_normal, rvc_zero):
560 case CLASS2 (rvc_inf, rvc_zero):
561 case CLASS2 (rvc_nan, rvc_zero):
562 /* ANY + 0 = ANY. */
563 case CLASS2 (rvc_nan, rvc_normal):
564 case CLASS2 (rvc_nan, rvc_inf):
565 /* NaN + ANY = NaN. */
566 case CLASS2 (rvc_inf, rvc_normal):
567 /* Inf + R = Inf. */
568 *r = *a;
569 return false;
571 case CLASS2 (rvc_inf, rvc_inf):
572 if (subtract_p)
573 /* Inf - Inf = NaN. */
574 get_canonical_qnan (r, 0);
575 else
576 /* Inf + Inf = Inf. */
577 *r = *a;
578 return false;
580 case CLASS2 (rvc_normal, rvc_normal):
581 break;
583 default:
584 gcc_unreachable ();
587 /* Swap the arguments such that A has the larger exponent. */
588 dexp = REAL_EXP (a) - REAL_EXP (b);
589 if (dexp < 0)
591 const REAL_VALUE_TYPE *t;
592 t = a, a = b, b = t;
593 dexp = -dexp;
594 sign ^= subtract_p;
596 exp = REAL_EXP (a);
598 /* If the exponents are not identical, we need to shift the
599 significand of B down. */
600 if (dexp > 0)
602 /* If the exponents are too far apart, the significands
603 do not overlap, which makes the subtraction a noop. */
604 if (dexp >= SIGNIFICAND_BITS)
606 *r = *a;
607 r->sign = sign;
608 return true;
611 inexact |= sticky_rshift_significand (&t, b, dexp);
612 b = &t;
615 if (subtract_p)
617 if (sub_significands (r, a, b, inexact))
619 /* We got a borrow out of the subtraction. That means that
620 A and B had the same exponent, and B had the larger
621 significand. We need to swap the sign and negate the
622 significand. */
623 sign ^= 1;
624 neg_significand (r, r);
627 else
629 if (add_significands (r, a, b))
631 /* We got carry out of the addition. This means we need to
632 shift the significand back down one bit and increase the
633 exponent. */
634 inexact |= sticky_rshift_significand (r, r, 1);
635 r->sig[SIGSZ-1] |= SIG_MSB;
636 if (++exp > MAX_EXP)
638 get_inf (r, sign);
639 return true;
644 r->cl = rvc_normal;
645 r->sign = sign;
646 SET_REAL_EXP (r, exp);
647 /* Zero out the remaining fields. */
648 r->signalling = 0;
649 r->canonical = 0;
650 r->decimal = 0;
652 /* Re-normalize the result. */
653 normalize (r);
655 /* Special case: if the subtraction results in zero, the result
656 is positive. */
657 if (r->cl == rvc_zero)
658 r->sign = 0;
659 else
660 r->sig[0] |= inexact;
662 return inexact;
665 /* Calculate R = A * B. Return true if the result may be inexact. */
667 static bool
668 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
669 const REAL_VALUE_TYPE *b)
671 REAL_VALUE_TYPE u, t, *rr;
672 unsigned int i, j, k;
673 int sign = a->sign ^ b->sign;
674 bool inexact = false;
676 switch (CLASS2 (a->cl, b->cl))
678 case CLASS2 (rvc_zero, rvc_zero):
679 case CLASS2 (rvc_zero, rvc_normal):
680 case CLASS2 (rvc_normal, rvc_zero):
681 /* +-0 * ANY = 0 with appropriate sign. */
682 get_zero (r, sign);
683 return false;
685 case CLASS2 (rvc_zero, rvc_nan):
686 case CLASS2 (rvc_normal, rvc_nan):
687 case CLASS2 (rvc_inf, rvc_nan):
688 case CLASS2 (rvc_nan, rvc_nan):
689 /* ANY * NaN = NaN. */
690 *r = *b;
691 r->sign = sign;
692 return false;
694 case CLASS2 (rvc_nan, rvc_zero):
695 case CLASS2 (rvc_nan, rvc_normal):
696 case CLASS2 (rvc_nan, rvc_inf):
697 /* NaN * ANY = NaN. */
698 *r = *a;
699 r->sign = sign;
700 return false;
702 case CLASS2 (rvc_zero, rvc_inf):
703 case CLASS2 (rvc_inf, rvc_zero):
704 /* 0 * Inf = NaN */
705 get_canonical_qnan (r, sign);
706 return false;
708 case CLASS2 (rvc_inf, rvc_inf):
709 case CLASS2 (rvc_normal, rvc_inf):
710 case CLASS2 (rvc_inf, rvc_normal):
711 /* Inf * Inf = Inf, R * Inf = Inf */
712 get_inf (r, sign);
713 return false;
715 case CLASS2 (rvc_normal, rvc_normal):
716 break;
718 default:
719 gcc_unreachable ();
722 if (r == a || r == b)
723 rr = &t;
724 else
725 rr = r;
726 get_zero (rr, 0);
728 /* Collect all the partial products. Since we don't have sure access
729 to a widening multiply, we split each long into two half-words.
731 Consider the long-hand form of a four half-word multiplication:
733 A B C D
734 * E F G H
735 --------------
736 DE DF DG DH
737 CE CF CG CH
738 BE BF BG BH
739 AE AF AG AH
741 We construct partial products of the widened half-word products
742 that are known to not overlap, e.g. DF+DH. Each such partial
743 product is given its proper exponent, which allows us to sum them
744 and obtain the finished product. */
746 for (i = 0; i < SIGSZ * 2; ++i)
748 unsigned long ai = a->sig[i / 2];
749 if (i & 1)
750 ai >>= HOST_BITS_PER_LONG / 2;
751 else
752 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
754 if (ai == 0)
755 continue;
757 for (j = 0; j < 2; ++j)
759 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
760 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
762 if (exp > MAX_EXP)
764 get_inf (r, sign);
765 return true;
767 if (exp < -MAX_EXP)
769 /* Would underflow to zero, which we shouldn't bother adding. */
770 inexact = true;
771 continue;
774 memset (&u, 0, sizeof (u));
775 u.cl = rvc_normal;
776 SET_REAL_EXP (&u, exp);
778 for (k = j; k < SIGSZ * 2; k += 2)
780 unsigned long bi = b->sig[k / 2];
781 if (k & 1)
782 bi >>= HOST_BITS_PER_LONG / 2;
783 else
784 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
786 u.sig[k / 2] = ai * bi;
789 normalize (&u);
790 inexact |= do_add (rr, rr, &u, 0);
794 rr->sign = sign;
795 if (rr != r)
796 *r = t;
798 return inexact;
801 /* Calculate R = A / B. Return true if the result may be inexact. */
803 static bool
804 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
805 const REAL_VALUE_TYPE *b)
807 int exp, sign = a->sign ^ b->sign;
808 REAL_VALUE_TYPE t, *rr;
809 bool inexact;
811 switch (CLASS2 (a->cl, b->cl))
813 case CLASS2 (rvc_zero, rvc_zero):
814 /* 0 / 0 = NaN. */
815 case CLASS2 (rvc_inf, rvc_inf):
816 /* Inf / Inf = NaN. */
817 get_canonical_qnan (r, sign);
818 return false;
820 case CLASS2 (rvc_zero, rvc_normal):
821 case CLASS2 (rvc_zero, rvc_inf):
822 /* 0 / ANY = 0. */
823 case CLASS2 (rvc_normal, rvc_inf):
824 /* R / Inf = 0. */
825 get_zero (r, sign);
826 return false;
828 case CLASS2 (rvc_normal, rvc_zero):
829 /* R / 0 = Inf. */
830 case CLASS2 (rvc_inf, rvc_zero):
831 /* Inf / 0 = Inf. */
832 get_inf (r, sign);
833 return false;
835 case CLASS2 (rvc_zero, rvc_nan):
836 case CLASS2 (rvc_normal, rvc_nan):
837 case CLASS2 (rvc_inf, rvc_nan):
838 case CLASS2 (rvc_nan, rvc_nan):
839 /* ANY / NaN = NaN. */
840 *r = *b;
841 r->sign = sign;
842 return false;
844 case CLASS2 (rvc_nan, rvc_zero):
845 case CLASS2 (rvc_nan, rvc_normal):
846 case CLASS2 (rvc_nan, rvc_inf):
847 /* NaN / ANY = NaN. */
848 *r = *a;
849 r->sign = sign;
850 return false;
852 case CLASS2 (rvc_inf, rvc_normal):
853 /* Inf / R = Inf. */
854 get_inf (r, sign);
855 return false;
857 case CLASS2 (rvc_normal, rvc_normal):
858 break;
860 default:
861 gcc_unreachable ();
864 if (r == a || r == b)
865 rr = &t;
866 else
867 rr = r;
869 /* Make sure all fields in the result are initialized. */
870 get_zero (rr, 0);
871 rr->cl = rvc_normal;
872 rr->sign = sign;
874 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
875 if (exp > MAX_EXP)
877 get_inf (r, sign);
878 return true;
880 if (exp < -MAX_EXP)
882 get_zero (r, sign);
883 return true;
885 SET_REAL_EXP (rr, exp);
887 inexact = div_significands (rr, a, b);
889 /* Re-normalize the result. */
890 normalize (rr);
891 rr->sig[0] |= inexact;
893 if (rr != r)
894 *r = t;
896 return inexact;
899 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
900 one of the two operands is a NaN. */
902 static int
903 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
904 int nan_result)
906 int ret;
908 switch (CLASS2 (a->cl, b->cl))
910 case CLASS2 (rvc_zero, rvc_zero):
911 /* Sign of zero doesn't matter for compares. */
912 return 0;
914 case CLASS2 (rvc_inf, rvc_zero):
915 case CLASS2 (rvc_inf, rvc_normal):
916 case CLASS2 (rvc_normal, rvc_zero):
917 return (a->sign ? -1 : 1);
919 case CLASS2 (rvc_inf, rvc_inf):
920 return -a->sign - -b->sign;
922 case CLASS2 (rvc_zero, rvc_normal):
923 case CLASS2 (rvc_zero, rvc_inf):
924 case CLASS2 (rvc_normal, rvc_inf):
925 return (b->sign ? 1 : -1);
927 case CLASS2 (rvc_zero, rvc_nan):
928 case CLASS2 (rvc_normal, rvc_nan):
929 case CLASS2 (rvc_inf, rvc_nan):
930 case CLASS2 (rvc_nan, rvc_nan):
931 case CLASS2 (rvc_nan, rvc_zero):
932 case CLASS2 (rvc_nan, rvc_normal):
933 case CLASS2 (rvc_nan, rvc_inf):
934 return nan_result;
936 case CLASS2 (rvc_normal, rvc_normal):
937 break;
939 default:
940 gcc_unreachable ();
943 if (a->sign != b->sign)
944 return -a->sign - -b->sign;
946 if (a->decimal || b->decimal)
947 return decimal_do_compare (a, b, nan_result);
949 if (REAL_EXP (a) > REAL_EXP (b))
950 ret = 1;
951 else if (REAL_EXP (a) < REAL_EXP (b))
952 ret = -1;
953 else
954 ret = cmp_significands (a, b);
956 return (a->sign ? -ret : ret);
959 /* Return A truncated to an integral value toward zero. */
961 static void
962 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
964 *r = *a;
966 switch (r->cl)
968 case rvc_zero:
969 case rvc_inf:
970 case rvc_nan:
971 break;
973 case rvc_normal:
974 if (r->decimal)
976 decimal_do_fix_trunc (r, a);
977 return;
979 if (REAL_EXP (r) <= 0)
980 get_zero (r, r->sign);
981 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
982 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
983 break;
985 default:
986 gcc_unreachable ();
990 /* Perform the binary or unary operation described by CODE.
991 For a unary operation, leave OP1 NULL. This function returns
992 true if the result may be inexact due to loss of precision. */
994 bool
995 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
996 const REAL_VALUE_TYPE *op1)
998 enum tree_code code = icode;
1000 if (op0->decimal || (op1 && op1->decimal))
1001 return decimal_real_arithmetic (r, icode, op0, op1);
1003 switch (code)
1005 case PLUS_EXPR:
1006 return do_add (r, op0, op1, 0);
1008 case MINUS_EXPR:
1009 return do_add (r, op0, op1, 1);
1011 case MULT_EXPR:
1012 return do_multiply (r, op0, op1);
1014 case RDIV_EXPR:
1015 return do_divide (r, op0, op1);
1017 case MIN_EXPR:
1018 if (op1->cl == rvc_nan)
1019 *r = *op1;
1020 else if (do_compare (op0, op1, -1) < 0)
1021 *r = *op0;
1022 else
1023 *r = *op1;
1024 break;
1026 case MAX_EXPR:
1027 if (op1->cl == rvc_nan)
1028 *r = *op1;
1029 else if (do_compare (op0, op1, 1) < 0)
1030 *r = *op1;
1031 else
1032 *r = *op0;
1033 break;
1035 case NEGATE_EXPR:
1036 *r = *op0;
1037 r->sign ^= 1;
1038 break;
1040 case ABS_EXPR:
1041 *r = *op0;
1042 r->sign = 0;
1043 break;
1045 case FIX_TRUNC_EXPR:
1046 do_fix_trunc (r, op0);
1047 break;
1049 default:
1050 gcc_unreachable ();
1052 return false;
1055 /* Legacy. Similar, but return the result directly. */
1057 REAL_VALUE_TYPE
1058 real_arithmetic2 (int icode, const REAL_VALUE_TYPE *op0,
1059 const REAL_VALUE_TYPE *op1)
1061 REAL_VALUE_TYPE r;
1062 real_arithmetic (&r, icode, op0, op1);
1063 return r;
1066 bool
1067 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1068 const REAL_VALUE_TYPE *op1)
1070 enum tree_code code = icode;
1072 switch (code)
1074 case LT_EXPR:
1075 return do_compare (op0, op1, 1) < 0;
1076 case LE_EXPR:
1077 return do_compare (op0, op1, 1) <= 0;
1078 case GT_EXPR:
1079 return do_compare (op0, op1, -1) > 0;
1080 case GE_EXPR:
1081 return do_compare (op0, op1, -1) >= 0;
1082 case EQ_EXPR:
1083 return do_compare (op0, op1, -1) == 0;
1084 case NE_EXPR:
1085 return do_compare (op0, op1, -1) != 0;
1086 case UNORDERED_EXPR:
1087 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1088 case ORDERED_EXPR:
1089 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1090 case UNLT_EXPR:
1091 return do_compare (op0, op1, -1) < 0;
1092 case UNLE_EXPR:
1093 return do_compare (op0, op1, -1) <= 0;
1094 case UNGT_EXPR:
1095 return do_compare (op0, op1, 1) > 0;
1096 case UNGE_EXPR:
1097 return do_compare (op0, op1, 1) >= 0;
1098 case UNEQ_EXPR:
1099 return do_compare (op0, op1, 0) == 0;
1100 case LTGT_EXPR:
1101 return do_compare (op0, op1, 0) != 0;
1103 default:
1104 gcc_unreachable ();
1108 /* Return floor log2(R). */
1111 real_exponent (const REAL_VALUE_TYPE *r)
1113 switch (r->cl)
1115 case rvc_zero:
1116 return 0;
1117 case rvc_inf:
1118 case rvc_nan:
1119 return (unsigned int)-1 >> 1;
1120 case rvc_normal:
1121 return REAL_EXP (r);
1122 default:
1123 gcc_unreachable ();
1127 /* R = OP0 * 2**EXP. */
1129 void
1130 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1132 *r = *op0;
1133 switch (r->cl)
1135 case rvc_zero:
1136 case rvc_inf:
1137 case rvc_nan:
1138 break;
1140 case rvc_normal:
1141 exp += REAL_EXP (op0);
1142 if (exp > MAX_EXP)
1143 get_inf (r, r->sign);
1144 else if (exp < -MAX_EXP)
1145 get_zero (r, r->sign);
1146 else
1147 SET_REAL_EXP (r, exp);
1148 break;
1150 default:
1151 gcc_unreachable ();
1155 /* Determine whether a floating-point value X is infinite. */
1157 bool
1158 real_isinf (const REAL_VALUE_TYPE *r)
1160 return (r->cl == rvc_inf);
1163 /* Determine whether a floating-point value X is a NaN. */
1165 bool
1166 real_isnan (const REAL_VALUE_TYPE *r)
1168 return (r->cl == rvc_nan);
1171 /* Determine whether a floating-point value X is negative. */
1173 bool
1174 real_isneg (const REAL_VALUE_TYPE *r)
1176 return r->sign;
1179 /* Determine whether a floating-point value X is minus zero. */
1181 bool
1182 real_isnegzero (const REAL_VALUE_TYPE *r)
1184 return r->sign && r->cl == rvc_zero;
1187 /* Compare two floating-point objects for bitwise identity. */
1189 bool
1190 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1192 int i;
1194 if (a->cl != b->cl)
1195 return false;
1196 if (a->sign != b->sign)
1197 return false;
1199 switch (a->cl)
1201 case rvc_zero:
1202 case rvc_inf:
1203 return true;
1205 case rvc_normal:
1206 if (a->decimal != b->decimal)
1207 return false;
1208 if (REAL_EXP (a) != REAL_EXP (b))
1209 return false;
1210 break;
1212 case rvc_nan:
1213 if (a->signalling != b->signalling)
1214 return false;
1215 /* The significand is ignored for canonical NaNs. */
1216 if (a->canonical || b->canonical)
1217 return a->canonical == b->canonical;
1218 break;
1220 default:
1221 gcc_unreachable ();
1224 for (i = 0; i < SIGSZ; ++i)
1225 if (a->sig[i] != b->sig[i])
1226 return false;
1228 return true;
1231 /* Try to change R into its exact multiplicative inverse in machine
1232 mode MODE. Return true if successful. */
1234 bool
1235 exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r)
1237 const REAL_VALUE_TYPE *one = real_digit (1);
1238 REAL_VALUE_TYPE u;
1239 int i;
1241 if (r->cl != rvc_normal)
1242 return false;
1244 /* Check for a power of two: all significand bits zero except the MSB. */
1245 for (i = 0; i < SIGSZ-1; ++i)
1246 if (r->sig[i] != 0)
1247 return false;
1248 if (r->sig[SIGSZ-1] != SIG_MSB)
1249 return false;
1251 /* Find the inverse and truncate to the required mode. */
1252 do_divide (&u, one, r);
1253 real_convert (&u, mode, &u);
1255 /* The rounding may have overflowed. */
1256 if (u.cl != rvc_normal)
1257 return false;
1258 for (i = 0; i < SIGSZ-1; ++i)
1259 if (u.sig[i] != 0)
1260 return false;
1261 if (u.sig[SIGSZ-1] != SIG_MSB)
1262 return false;
1264 *r = u;
1265 return true;
1268 /* Render R as an integer. */
1270 HOST_WIDE_INT
1271 real_to_integer (const REAL_VALUE_TYPE *r)
1273 unsigned HOST_WIDE_INT i;
1275 switch (r->cl)
1277 case rvc_zero:
1278 underflow:
1279 return 0;
1281 case rvc_inf:
1282 case rvc_nan:
1283 overflow:
1284 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1285 if (!r->sign)
1286 i--;
1287 return i;
1289 case rvc_normal:
1290 if (r->decimal)
1291 return decimal_real_to_integer (r);
1293 if (REAL_EXP (r) <= 0)
1294 goto underflow;
1295 /* Only force overflow for unsigned overflow. Signed overflow is
1296 undefined, so it doesn't matter what we return, and some callers
1297 expect to be able to use this routine for both signed and
1298 unsigned conversions. */
1299 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1300 goto overflow;
1302 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1303 i = r->sig[SIGSZ-1];
1304 else
1306 gcc_assert (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];
1312 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1314 if (r->sign)
1315 i = -i;
1316 return i;
1318 default:
1319 gcc_unreachable ();
1323 /* Likewise, but to an integer pair, HI+LOW. */
1325 void
1326 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
1327 const REAL_VALUE_TYPE *r)
1329 REAL_VALUE_TYPE t;
1330 HOST_WIDE_INT low, high;
1331 int exp;
1333 switch (r->cl)
1335 case rvc_zero:
1336 underflow:
1337 low = high = 0;
1338 break;
1340 case rvc_inf:
1341 case rvc_nan:
1342 overflow:
1343 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1344 if (r->sign)
1345 low = 0;
1346 else
1348 high--;
1349 low = -1;
1351 break;
1353 case rvc_normal:
1354 if (r->decimal)
1356 decimal_real_to_integer2 (plow, phigh, r);
1357 return;
1360 exp = REAL_EXP (r);
1361 if (exp <= 0)
1362 goto underflow;
1363 /* Only force overflow for unsigned overflow. Signed overflow is
1364 undefined, so it doesn't matter what we return, and some callers
1365 expect to be able to use this routine for both signed and
1366 unsigned conversions. */
1367 if (exp > 2*HOST_BITS_PER_WIDE_INT)
1368 goto overflow;
1370 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1371 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1373 high = t.sig[SIGSZ-1];
1374 low = t.sig[SIGSZ-2];
1376 else
1378 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1379 high = t.sig[SIGSZ-1];
1380 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1381 high |= t.sig[SIGSZ-2];
1383 low = t.sig[SIGSZ-3];
1384 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1385 low |= t.sig[SIGSZ-4];
1388 if (r->sign)
1390 if (low == 0)
1391 high = -high;
1392 else
1393 low = -low, high = ~high;
1395 break;
1397 default:
1398 gcc_unreachable ();
1401 *plow = low;
1402 *phigh = high;
1405 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1406 of NUM / DEN. Return the quotient and place the remainder in NUM.
1407 It is expected that NUM / DEN are close enough that the quotient is
1408 small. */
1410 static unsigned long
1411 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1413 unsigned long q, msb;
1414 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1416 if (expn < expd)
1417 return 0;
1419 q = msb = 0;
1420 goto start;
1423 msb = num->sig[SIGSZ-1] & SIG_MSB;
1424 q <<= 1;
1425 lshift_significand_1 (num, num);
1426 start:
1427 if (msb || cmp_significands (num, den) >= 0)
1429 sub_significands (num, num, den, 0);
1430 q |= 1;
1433 while (--expn >= expd);
1435 SET_REAL_EXP (num, expd);
1436 normalize (num);
1438 return q;
1441 /* Render R as a decimal floating point constant. Emit DIGITS significant
1442 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1443 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1444 zeros. */
1446 #define M_LOG10_2 0.30102999566398119521
1448 void
1449 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1450 size_t digits, int crop_trailing_zeros)
1452 const REAL_VALUE_TYPE *one, *ten;
1453 REAL_VALUE_TYPE r, pten, u, v;
1454 int dec_exp, cmp_one, digit;
1455 size_t max_digits;
1456 char *p, *first, *last;
1457 bool sign;
1459 r = *r_orig;
1460 switch (r.cl)
1462 case rvc_zero:
1463 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1464 return;
1465 case rvc_normal:
1466 break;
1467 case rvc_inf:
1468 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1469 return;
1470 case rvc_nan:
1471 /* ??? Print the significand as well, if not canonical? */
1472 strcpy (str, (r.sign ? "-NaN" : "+NaN"));
1473 return;
1474 default:
1475 gcc_unreachable ();
1478 if (r.decimal)
1480 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1481 return;
1484 /* Bound the number of digits printed by the size of the representation. */
1485 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1486 if (digits == 0 || digits > max_digits)
1487 digits = max_digits;
1489 /* Estimate the decimal exponent, and compute the length of the string it
1490 will print as. Be conservative and add one to account for possible
1491 overflow or rounding error. */
1492 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1493 for (max_digits = 1; dec_exp ; max_digits++)
1494 dec_exp /= 10;
1496 /* Bound the number of digits printed by the size of the output buffer. */
1497 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1498 gcc_assert (max_digits <= buf_size);
1499 if (digits > max_digits)
1500 digits = max_digits;
1502 one = real_digit (1);
1503 ten = ten_to_ptwo (0);
1505 sign = r.sign;
1506 r.sign = 0;
1508 dec_exp = 0;
1509 pten = *one;
1511 cmp_one = do_compare (&r, one, 0);
1512 if (cmp_one > 0)
1514 int m;
1516 /* Number is greater than one. Convert significand to an integer
1517 and strip trailing decimal zeros. */
1519 u = r;
1520 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1522 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1523 m = floor_log2 (max_digits);
1525 /* Iterate over the bits of the possible powers of 10 that might
1526 be present in U and eliminate them. That is, if we find that
1527 10**2**M divides U evenly, keep the division and increase
1528 DEC_EXP by 2**M. */
1531 REAL_VALUE_TYPE t;
1533 do_divide (&t, &u, ten_to_ptwo (m));
1534 do_fix_trunc (&v, &t);
1535 if (cmp_significands (&v, &t) == 0)
1537 u = t;
1538 dec_exp += 1 << m;
1541 while (--m >= 0);
1543 /* Revert the scaling to integer that we performed earlier. */
1544 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1545 - (SIGNIFICAND_BITS - 1));
1546 r = u;
1548 /* Find power of 10. Do this by dividing out 10**2**M when
1549 this is larger than the current remainder. Fill PTEN with
1550 the power of 10 that we compute. */
1551 if (REAL_EXP (&r) > 0)
1553 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1556 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1557 if (do_compare (&u, ptentwo, 0) >= 0)
1559 do_divide (&u, &u, ptentwo);
1560 do_multiply (&pten, &pten, ptentwo);
1561 dec_exp += 1 << m;
1564 while (--m >= 0);
1566 else
1567 /* We managed to divide off enough tens in the above reduction
1568 loop that we've now got a negative exponent. Fall into the
1569 less-than-one code to compute the proper value for PTEN. */
1570 cmp_one = -1;
1572 if (cmp_one < 0)
1574 int m;
1576 /* Number is less than one. Pad significand with leading
1577 decimal zeros. */
1579 v = r;
1580 while (1)
1582 /* Stop if we'd shift bits off the bottom. */
1583 if (v.sig[0] & 7)
1584 break;
1586 do_multiply (&u, &v, ten);
1588 /* Stop if we're now >= 1. */
1589 if (REAL_EXP (&u) > 0)
1590 break;
1592 v = u;
1593 dec_exp -= 1;
1595 r = v;
1597 /* Find power of 10. Do this by multiplying in P=10**2**M when
1598 the current remainder is smaller than 1/P. Fill PTEN with the
1599 power of 10 that we compute. */
1600 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1603 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1604 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1606 if (do_compare (&v, ptenmtwo, 0) <= 0)
1608 do_multiply (&v, &v, ptentwo);
1609 do_multiply (&pten, &pten, ptentwo);
1610 dec_exp -= 1 << m;
1613 while (--m >= 0);
1615 /* Invert the positive power of 10 that we've collected so far. */
1616 do_divide (&pten, one, &pten);
1619 p = str;
1620 if (sign)
1621 *p++ = '-';
1622 first = p++;
1624 /* At this point, PTEN should contain the nearest power of 10 smaller
1625 than R, such that this division produces the first digit.
1627 Using a divide-step primitive that returns the complete integral
1628 remainder avoids the rounding error that would be produced if
1629 we were to use do_divide here and then simply multiply by 10 for
1630 each subsequent digit. */
1632 digit = rtd_divmod (&r, &pten);
1634 /* Be prepared for error in that division via underflow ... */
1635 if (digit == 0 && cmp_significand_0 (&r))
1637 /* Multiply by 10 and try again. */
1638 do_multiply (&r, &r, ten);
1639 digit = rtd_divmod (&r, &pten);
1640 dec_exp -= 1;
1641 gcc_assert (digit != 0);
1644 /* ... or overflow. */
1645 if (digit == 10)
1647 *p++ = '1';
1648 if (--digits > 0)
1649 *p++ = '0';
1650 dec_exp += 1;
1652 else
1654 gcc_assert (digit <= 10);
1655 *p++ = digit + '0';
1658 /* Generate subsequent digits. */
1659 while (--digits > 0)
1661 do_multiply (&r, &r, ten);
1662 digit = rtd_divmod (&r, &pten);
1663 *p++ = digit + '0';
1665 last = p;
1667 /* Generate one more digit with which to do rounding. */
1668 do_multiply (&r, &r, ten);
1669 digit = rtd_divmod (&r, &pten);
1671 /* Round the result. */
1672 if (digit == 5)
1674 /* Round to nearest. If R is nonzero there are additional
1675 nonzero digits to be extracted. */
1676 if (cmp_significand_0 (&r))
1677 digit++;
1678 /* Round to even. */
1679 else if ((p[-1] - '0') & 1)
1680 digit++;
1682 if (digit > 5)
1684 while (p > first)
1686 digit = *--p;
1687 if (digit == '9')
1688 *p = '0';
1689 else
1691 *p = digit + 1;
1692 break;
1696 /* Carry out of the first digit. This means we had all 9's and
1697 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1698 if (p == first)
1700 first[1] = '1';
1701 dec_exp++;
1705 /* Insert the decimal point. */
1706 first[0] = first[1];
1707 first[1] = '.';
1709 /* If requested, drop trailing zeros. Never crop past "1.0". */
1710 if (crop_trailing_zeros)
1711 while (last > first + 3 && last[-1] == '0')
1712 last--;
1714 /* Append the exponent. */
1715 sprintf (last, "e%+d", dec_exp);
1718 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1719 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1720 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1721 strip trailing zeros. */
1723 void
1724 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1725 size_t digits, int crop_trailing_zeros)
1727 int i, j, exp = REAL_EXP (r);
1728 char *p, *first;
1729 char exp_buf[16];
1730 size_t max_digits;
1732 switch (r->cl)
1734 case rvc_zero:
1735 exp = 0;
1736 break;
1737 case rvc_normal:
1738 break;
1739 case rvc_inf:
1740 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1741 return;
1742 case rvc_nan:
1743 /* ??? Print the significand as well, if not canonical? */
1744 strcpy (str, (r->sign ? "-NaN" : "+NaN"));
1745 return;
1746 default:
1747 gcc_unreachable ();
1750 if (r->decimal)
1752 /* Hexadecimal format for decimal floats is not interesting. */
1753 strcpy (str, "N/A");
1754 return;
1757 if (digits == 0)
1758 digits = SIGNIFICAND_BITS / 4;
1760 /* Bound the number of digits printed by the size of the output buffer. */
1762 sprintf (exp_buf, "p%+d", exp);
1763 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1764 gcc_assert (max_digits <= buf_size);
1765 if (digits > max_digits)
1766 digits = max_digits;
1768 p = str;
1769 if (r->sign)
1770 *p++ = '-';
1771 *p++ = '0';
1772 *p++ = 'x';
1773 *p++ = '0';
1774 *p++ = '.';
1775 first = p;
1777 for (i = SIGSZ - 1; i >= 0; --i)
1778 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1780 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1781 if (--digits == 0)
1782 goto out;
1785 out:
1786 if (crop_trailing_zeros)
1787 while (p > first + 1 && p[-1] == '0')
1788 p--;
1790 sprintf (p, "p%+d", exp);
1793 /* Initialize R from a decimal or hexadecimal string. The string is
1794 assumed to have been syntax checked already. Return -1 if the
1795 value underflows, +1 if overflows, and 0 otherwise. */
1798 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1800 int exp = 0;
1801 bool sign = false;
1803 get_zero (r, 0);
1805 if (*str == '-')
1807 sign = true;
1808 str++;
1810 else if (*str == '+')
1811 str++;
1813 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1815 /* Hexadecimal floating point. */
1816 int pos = SIGNIFICAND_BITS - 4, d;
1818 str += 2;
1820 while (*str == '0')
1821 str++;
1822 while (1)
1824 d = hex_value (*str);
1825 if (d == _hex_bad)
1826 break;
1827 if (pos >= 0)
1829 r->sig[pos / HOST_BITS_PER_LONG]
1830 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1831 pos -= 4;
1833 else if (d)
1834 /* Ensure correct rounding by setting last bit if there is
1835 a subsequent nonzero digit. */
1836 r->sig[0] |= 1;
1837 exp += 4;
1838 str++;
1840 if (*str == '.')
1842 str++;
1843 if (pos == SIGNIFICAND_BITS - 4)
1845 while (*str == '0')
1846 str++, exp -= 4;
1848 while (1)
1850 d = hex_value (*str);
1851 if (d == _hex_bad)
1852 break;
1853 if (pos >= 0)
1855 r->sig[pos / HOST_BITS_PER_LONG]
1856 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1857 pos -= 4;
1859 else if (d)
1860 /* Ensure correct rounding by setting last bit if there is
1861 a subsequent nonzero digit. */
1862 r->sig[0] |= 1;
1863 str++;
1867 /* If the mantissa is zero, ignore the exponent. */
1868 if (!cmp_significand_0 (r))
1869 goto is_a_zero;
1871 if (*str == 'p' || *str == 'P')
1873 bool exp_neg = false;
1875 str++;
1876 if (*str == '-')
1878 exp_neg = true;
1879 str++;
1881 else if (*str == '+')
1882 str++;
1884 d = 0;
1885 while (ISDIGIT (*str))
1887 d *= 10;
1888 d += *str - '0';
1889 if (d > MAX_EXP)
1891 /* Overflowed the exponent. */
1892 if (exp_neg)
1893 goto underflow;
1894 else
1895 goto overflow;
1897 str++;
1899 if (exp_neg)
1900 d = -d;
1902 exp += d;
1905 r->cl = rvc_normal;
1906 SET_REAL_EXP (r, exp);
1908 normalize (r);
1910 else
1912 /* Decimal floating point. */
1913 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1914 int d;
1916 while (*str == '0')
1917 str++;
1918 while (ISDIGIT (*str))
1920 d = *str++ - '0';
1921 do_multiply (r, r, ten);
1922 if (d)
1923 do_add (r, r, real_digit (d), 0);
1925 if (*str == '.')
1927 str++;
1928 if (r->cl == rvc_zero)
1930 while (*str == '0')
1931 str++, exp--;
1933 while (ISDIGIT (*str))
1935 d = *str++ - '0';
1936 do_multiply (r, r, ten);
1937 if (d)
1938 do_add (r, r, real_digit (d), 0);
1939 exp--;
1943 /* If the mantissa is zero, ignore the exponent. */
1944 if (r->cl == rvc_zero)
1945 goto is_a_zero;
1947 if (*str == 'e' || *str == 'E')
1949 bool exp_neg = false;
1951 str++;
1952 if (*str == '-')
1954 exp_neg = true;
1955 str++;
1957 else if (*str == '+')
1958 str++;
1960 d = 0;
1961 while (ISDIGIT (*str))
1963 d *= 10;
1964 d += *str - '0';
1965 if (d > MAX_EXP)
1967 /* Overflowed the exponent. */
1968 if (exp_neg)
1969 goto underflow;
1970 else
1971 goto overflow;
1973 str++;
1975 if (exp_neg)
1976 d = -d;
1977 exp += d;
1980 if (exp)
1981 times_pten (r, exp);
1984 r->sign = sign;
1985 return 0;
1987 is_a_zero:
1988 get_zero (r, sign);
1989 return 0;
1991 underflow:
1992 get_zero (r, sign);
1993 return -1;
1995 overflow:
1996 get_inf (r, sign);
1997 return 1;
2000 /* Legacy. Similar, but return the result directly. */
2002 REAL_VALUE_TYPE
2003 real_from_string2 (const char *s, enum machine_mode mode)
2005 REAL_VALUE_TYPE r;
2007 real_from_string (&r, s);
2008 if (mode != VOIDmode)
2009 real_convert (&r, mode, &r);
2011 return r;
2014 /* Initialize R from string S and desired MODE. */
2016 void
2017 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2019 if (DECIMAL_FLOAT_MODE_P (mode))
2020 decimal_real_from_string (r, s);
2021 else
2022 real_from_string (r, s);
2024 if (mode != VOIDmode)
2025 real_convert (r, mode, r);
2028 /* Initialize R from the integer pair HIGH+LOW. */
2030 void
2031 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2032 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2033 int unsigned_p)
2035 if (low == 0 && high == 0)
2036 get_zero (r, 0);
2037 else
2039 memset (r, 0, sizeof (*r));
2040 r->cl = rvc_normal;
2041 r->sign = high < 0 && !unsigned_p;
2042 SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT);
2044 if (r->sign)
2046 high = ~high;
2047 if (low == 0)
2048 high += 1;
2049 else
2050 low = -low;
2053 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2055 r->sig[SIGSZ-1] = high;
2056 r->sig[SIGSZ-2] = low;
2058 else
2060 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2061 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2062 r->sig[SIGSZ-2] = high;
2063 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2064 r->sig[SIGSZ-4] = low;
2067 normalize (r);
2070 if (mode != VOIDmode)
2071 real_convert (r, mode, r);
2074 /* Returns 10**2**N. */
2076 static const REAL_VALUE_TYPE *
2077 ten_to_ptwo (int n)
2079 static REAL_VALUE_TYPE tens[EXP_BITS];
2081 gcc_assert (n >= 0);
2082 gcc_assert (n < EXP_BITS);
2084 if (tens[n].cl == rvc_zero)
2086 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2088 HOST_WIDE_INT t = 10;
2089 int i;
2091 for (i = 0; i < n; ++i)
2092 t *= t;
2094 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2096 else
2098 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2099 do_multiply (&tens[n], t, t);
2103 return &tens[n];
2106 /* Returns 10**(-2**N). */
2108 static const REAL_VALUE_TYPE *
2109 ten_to_mptwo (int n)
2111 static REAL_VALUE_TYPE tens[EXP_BITS];
2113 gcc_assert (n >= 0);
2114 gcc_assert (n < EXP_BITS);
2116 if (tens[n].cl == rvc_zero)
2117 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2119 return &tens[n];
2122 /* Returns N. */
2124 static const REAL_VALUE_TYPE *
2125 real_digit (int n)
2127 static REAL_VALUE_TYPE num[10];
2129 gcc_assert (n >= 0);
2130 gcc_assert (n <= 9);
2132 if (n > 0 && num[n].cl == rvc_zero)
2133 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2135 return &num[n];
2138 /* Multiply R by 10**EXP. */
2140 static void
2141 times_pten (REAL_VALUE_TYPE *r, int exp)
2143 REAL_VALUE_TYPE pten, *rr;
2144 bool negative = (exp < 0);
2145 int i;
2147 if (negative)
2149 exp = -exp;
2150 pten = *real_digit (1);
2151 rr = &pten;
2153 else
2154 rr = r;
2156 for (i = 0; exp > 0; ++i, exp >>= 1)
2157 if (exp & 1)
2158 do_multiply (rr, rr, ten_to_ptwo (i));
2160 if (negative)
2161 do_divide (r, r, &pten);
2164 /* Fills R with +Inf. */
2166 void
2167 real_inf (REAL_VALUE_TYPE *r)
2169 get_inf (r, 0);
2172 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2173 we force a QNaN, else we force an SNaN. The string, if not empty,
2174 is parsed as a number and placed in the significand. Return true
2175 if the string was successfully parsed. */
2177 bool
2178 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2179 enum machine_mode mode)
2181 const struct real_format *fmt;
2183 fmt = REAL_MODE_FORMAT (mode);
2184 gcc_assert (fmt);
2186 if (*str == 0)
2188 if (quiet)
2189 get_canonical_qnan (r, 0);
2190 else
2191 get_canonical_snan (r, 0);
2193 else
2195 int base = 10, d;
2197 memset (r, 0, sizeof (*r));
2198 r->cl = rvc_nan;
2200 /* Parse akin to strtol into the significand of R. */
2202 while (ISSPACE (*str))
2203 str++;
2204 if (*str == '-')
2205 str++;
2206 else if (*str == '+')
2207 str++;
2208 if (*str == '0')
2210 str++;
2211 if (*str == 'x' || *str == 'X')
2213 base = 16;
2214 str++;
2216 else
2217 base = 8;
2220 while ((d = hex_value (*str)) < base)
2222 REAL_VALUE_TYPE u;
2224 switch (base)
2226 case 8:
2227 lshift_significand (r, r, 3);
2228 break;
2229 case 16:
2230 lshift_significand (r, r, 4);
2231 break;
2232 case 10:
2233 lshift_significand_1 (&u, r);
2234 lshift_significand (r, r, 3);
2235 add_significands (r, r, &u);
2236 break;
2237 default:
2238 gcc_unreachable ();
2241 get_zero (&u, 0);
2242 u.sig[0] = d;
2243 add_significands (r, r, &u);
2245 str++;
2248 /* Must have consumed the entire string for success. */
2249 if (*str != 0)
2250 return false;
2252 /* Shift the significand into place such that the bits
2253 are in the most significant bits for the format. */
2254 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2256 /* Our MSB is always unset for NaNs. */
2257 r->sig[SIGSZ-1] &= ~SIG_MSB;
2259 /* Force quiet or signalling NaN. */
2260 r->signalling = !quiet;
2263 return true;
2266 /* Fills R with the largest finite value representable in mode MODE.
2267 If SIGN is nonzero, R is set to the most negative finite value. */
2269 void
2270 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2272 const struct real_format *fmt;
2273 int np2;
2275 fmt = REAL_MODE_FORMAT (mode);
2276 gcc_assert (fmt);
2277 memset (r, 0, sizeof (*r));
2279 if (fmt->b == 10)
2280 decimal_real_maxval (r, sign, mode);
2281 else
2283 r->cl = rvc_normal;
2284 r->sign = sign;
2285 SET_REAL_EXP (r, fmt->emax * fmt->log2_b);
2287 np2 = SIGNIFICAND_BITS - fmt->p * fmt->log2_b;
2288 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2289 clear_significand_below (r, np2);
2291 if (fmt->pnan < fmt->p)
2292 /* This is an IBM extended double format made up of two IEEE
2293 doubles. The value of the long double is the sum of the
2294 values of the two parts. The most significant part is
2295 required to be the value of the long double rounded to the
2296 nearest double. Rounding means we need a slightly smaller
2297 value for LDBL_MAX. */
2298 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan);
2302 /* Fills R with 2**N. */
2304 void
2305 real_2expN (REAL_VALUE_TYPE *r, int n)
2307 memset (r, 0, sizeof (*r));
2309 n++;
2310 if (n > MAX_EXP)
2311 r->cl = rvc_inf;
2312 else if (n < -MAX_EXP)
2314 else
2316 r->cl = rvc_normal;
2317 SET_REAL_EXP (r, n);
2318 r->sig[SIGSZ-1] = SIG_MSB;
2323 static void
2324 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2326 int p2, np2, i, w;
2327 unsigned long sticky;
2328 bool guard, lsb;
2329 int emin2m1, emax2;
2331 if (r->decimal)
2333 if (fmt->b == 10)
2335 decimal_round_for_format (fmt, r);
2336 return;
2338 /* FIXME. We can come here via fp_easy_constant
2339 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2340 investigated whether this convert needs to be here, or
2341 something else is missing. */
2342 decimal_real_convert (r, DFmode, r);
2345 p2 = fmt->p * fmt->log2_b;
2346 emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2347 emax2 = fmt->emax * fmt->log2_b;
2349 np2 = SIGNIFICAND_BITS - p2;
2350 switch (r->cl)
2352 underflow:
2353 get_zero (r, r->sign);
2354 case rvc_zero:
2355 if (!fmt->has_signed_zero)
2356 r->sign = 0;
2357 return;
2359 overflow:
2360 get_inf (r, r->sign);
2361 case rvc_inf:
2362 return;
2364 case rvc_nan:
2365 clear_significand_below (r, np2);
2366 return;
2368 case rvc_normal:
2369 break;
2371 default:
2372 gcc_unreachable ();
2375 /* If we're not base2, normalize the exponent to a multiple of
2376 the true base. */
2377 if (fmt->log2_b != 1)
2379 int shift;
2381 gcc_assert (fmt->b != 10);
2382 shift = REAL_EXP (r) & (fmt->log2_b - 1);
2383 if (shift)
2385 shift = fmt->log2_b - shift;
2386 r->sig[0] |= sticky_rshift_significand (r, r, shift);
2387 SET_REAL_EXP (r, REAL_EXP (r) + shift);
2391 /* Check the range of the exponent. If we're out of range,
2392 either underflow or overflow. */
2393 if (REAL_EXP (r) > emax2)
2394 goto overflow;
2395 else if (REAL_EXP (r) <= emin2m1)
2397 int diff;
2399 if (!fmt->has_denorm)
2401 /* Don't underflow completely until we've had a chance to round. */
2402 if (REAL_EXP (r) < emin2m1)
2403 goto underflow;
2405 else
2407 diff = emin2m1 - REAL_EXP (r) + 1;
2408 if (diff > p2)
2409 goto underflow;
2411 /* De-normalize the significand. */
2412 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2413 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2417 /* There are P2 true significand bits, followed by one guard bit,
2418 followed by one sticky bit, followed by stuff. Fold nonzero
2419 stuff into the sticky bit. */
2421 sticky = 0;
2422 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2423 sticky |= r->sig[i];
2424 sticky |=
2425 r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2427 guard = test_significand_bit (r, np2 - 1);
2428 lsb = test_significand_bit (r, np2);
2430 /* Round to even. */
2431 if (guard && (sticky || lsb))
2433 REAL_VALUE_TYPE u;
2434 get_zero (&u, 0);
2435 set_significand_bit (&u, np2);
2437 if (add_significands (r, r, &u))
2439 /* Overflow. Means the significand had been all ones, and
2440 is now all zeros. Need to increase the exponent, and
2441 possibly re-normalize it. */
2442 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2443 if (REAL_EXP (r) > emax2)
2444 goto overflow;
2445 r->sig[SIGSZ-1] = SIG_MSB;
2447 if (fmt->log2_b != 1)
2449 int shift = REAL_EXP (r) & (fmt->log2_b - 1);
2450 if (shift)
2452 shift = fmt->log2_b - shift;
2453 rshift_significand (r, r, shift);
2454 SET_REAL_EXP (r, REAL_EXP (r) + shift);
2455 if (REAL_EXP (r) > emax2)
2456 goto overflow;
2462 /* Catch underflow that we deferred until after rounding. */
2463 if (REAL_EXP (r) <= emin2m1)
2464 goto underflow;
2466 /* Clear out trailing garbage. */
2467 clear_significand_below (r, np2);
2470 /* Extend or truncate to a new mode. */
2472 void
2473 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2474 const REAL_VALUE_TYPE *a)
2476 const struct real_format *fmt;
2478 fmt = REAL_MODE_FORMAT (mode);
2479 gcc_assert (fmt);
2481 *r = *a;
2483 if (a->decimal || fmt->b == 10)
2484 decimal_real_convert (r, mode, a);
2486 round_for_format (fmt, r);
2488 /* round_for_format de-normalizes denormals. Undo just that part. */
2489 if (r->cl == rvc_normal)
2490 normalize (r);
2493 /* Legacy. Likewise, except return the struct directly. */
2495 REAL_VALUE_TYPE
2496 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2498 REAL_VALUE_TYPE r;
2499 real_convert (&r, mode, &a);
2500 return r;
2503 /* Return true if truncating to MODE is exact. */
2505 bool
2506 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2508 const struct real_format *fmt;
2509 REAL_VALUE_TYPE t;
2510 int emin2m1;
2512 fmt = REAL_MODE_FORMAT (mode);
2513 gcc_assert (fmt);
2515 /* Don't allow conversion to denormals. */
2516 emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2517 if (REAL_EXP (a) <= emin2m1)
2518 return false;
2520 /* After conversion to the new mode, the value must be identical. */
2521 real_convert (&t, mode, a);
2522 return real_identical (&t, a);
2525 /* Write R to the given target format. Place the words of the result
2526 in target word order in BUF. There are always 32 bits in each
2527 long, no matter the size of the host long.
2529 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2531 long
2532 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2533 const struct real_format *fmt)
2535 REAL_VALUE_TYPE r;
2536 long buf1;
2538 r = *r_orig;
2539 round_for_format (fmt, &r);
2541 if (!buf)
2542 buf = &buf1;
2543 (*fmt->encode) (fmt, buf, &r);
2545 return *buf;
2548 /* Similar, but look up the format from MODE. */
2550 long
2551 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2553 const struct real_format *fmt;
2555 fmt = REAL_MODE_FORMAT (mode);
2556 gcc_assert (fmt);
2558 return real_to_target_fmt (buf, r, fmt);
2561 /* Read R from the given target format. Read the words of the result
2562 in target word order in BUF. There are always 32 bits in each
2563 long, no matter the size of the host long. */
2565 void
2566 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2567 const struct real_format *fmt)
2569 (*fmt->decode) (fmt, r, buf);
2572 /* Similar, but look up the format from MODE. */
2574 void
2575 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2577 const struct real_format *fmt;
2579 fmt = REAL_MODE_FORMAT (mode);
2580 gcc_assert (fmt);
2582 (*fmt->decode) (fmt, r, buf);
2585 /* Return the number of bits of the largest binary value that the
2586 significand of MODE will hold. */
2587 /* ??? Legacy. Should get access to real_format directly. */
2590 significand_size (enum machine_mode mode)
2592 const struct real_format *fmt;
2594 fmt = REAL_MODE_FORMAT (mode);
2595 if (fmt == NULL)
2596 return 0;
2598 if (fmt->b == 10)
2600 /* Return the size in bits of the largest binary value that can be
2601 held by the decimal coefficient for this mode. This is one more
2602 than the number of bits required to hold the largest coefficient
2603 of this mode. */
2604 double log2_10 = 3.3219281;
2605 return fmt->p * log2_10;
2607 return fmt->p * fmt->log2_b;
2610 /* Return a hash value for the given real value. */
2611 /* ??? The "unsigned int" return value is intended to be hashval_t,
2612 but I didn't want to pull hashtab.h into real.h. */
2614 unsigned int
2615 real_hash (const REAL_VALUE_TYPE *r)
2617 unsigned int h;
2618 size_t i;
2620 h = r->cl | (r->sign << 2);
2621 switch (r->cl)
2623 case rvc_zero:
2624 case rvc_inf:
2625 return h;
2627 case rvc_normal:
2628 h |= REAL_EXP (r) << 3;
2629 break;
2631 case rvc_nan:
2632 if (r->signalling)
2633 h ^= (unsigned int)-1;
2634 if (r->canonical)
2635 return h;
2636 break;
2638 default:
2639 gcc_unreachable ();
2642 if (sizeof(unsigned long) > sizeof(unsigned int))
2643 for (i = 0; i < SIGSZ; ++i)
2645 unsigned long s = r->sig[i];
2646 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2648 else
2649 for (i = 0; i < SIGSZ; ++i)
2650 h ^= r->sig[i];
2652 return h;
2655 /* IEEE single-precision format. */
2657 static void encode_ieee_single (const struct real_format *fmt,
2658 long *, const REAL_VALUE_TYPE *);
2659 static void decode_ieee_single (const struct real_format *,
2660 REAL_VALUE_TYPE *, const long *);
2662 static void
2663 encode_ieee_single (const struct real_format *fmt, long *buf,
2664 const REAL_VALUE_TYPE *r)
2666 unsigned long image, sig, exp;
2667 unsigned long sign = r->sign;
2668 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2670 image = sign << 31;
2671 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2673 switch (r->cl)
2675 case rvc_zero:
2676 break;
2678 case rvc_inf:
2679 if (fmt->has_inf)
2680 image |= 255 << 23;
2681 else
2682 image |= 0x7fffffff;
2683 break;
2685 case rvc_nan:
2686 if (fmt->has_nans)
2688 if (r->canonical)
2689 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2690 if (r->signalling == fmt->qnan_msb_set)
2691 sig &= ~(1 << 22);
2692 else
2693 sig |= 1 << 22;
2694 if (sig == 0)
2695 sig = 1 << 21;
2697 image |= 255 << 23;
2698 image |= sig;
2700 else
2701 image |= 0x7fffffff;
2702 break;
2704 case rvc_normal:
2705 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2706 whereas the intermediate representation is 0.F x 2**exp.
2707 Which means we're off by one. */
2708 if (denormal)
2709 exp = 0;
2710 else
2711 exp = REAL_EXP (r) + 127 - 1;
2712 image |= exp << 23;
2713 image |= sig;
2714 break;
2716 default:
2717 gcc_unreachable ();
2720 buf[0] = image;
2723 static void
2724 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2725 const long *buf)
2727 unsigned long image = buf[0] & 0xffffffff;
2728 bool sign = (image >> 31) & 1;
2729 int exp = (image >> 23) & 0xff;
2731 memset (r, 0, sizeof (*r));
2732 image <<= HOST_BITS_PER_LONG - 24;
2733 image &= ~SIG_MSB;
2735 if (exp == 0)
2737 if (image && fmt->has_denorm)
2739 r->cl = rvc_normal;
2740 r->sign = sign;
2741 SET_REAL_EXP (r, -126);
2742 r->sig[SIGSZ-1] = image << 1;
2743 normalize (r);
2745 else if (fmt->has_signed_zero)
2746 r->sign = sign;
2748 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2750 if (image)
2752 r->cl = rvc_nan;
2753 r->sign = sign;
2754 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2755 ^ fmt->qnan_msb_set);
2756 r->sig[SIGSZ-1] = image;
2758 else
2760 r->cl = rvc_inf;
2761 r->sign = sign;
2764 else
2766 r->cl = rvc_normal;
2767 r->sign = sign;
2768 SET_REAL_EXP (r, exp - 127 + 1);
2769 r->sig[SIGSZ-1] = image | SIG_MSB;
2773 const struct real_format ieee_single_format =
2775 encode_ieee_single,
2776 decode_ieee_single,
2781 -125,
2782 128,
2785 true,
2786 true,
2787 true,
2788 true,
2789 true,
2790 false
2793 const struct real_format mips_single_format =
2795 encode_ieee_single,
2796 decode_ieee_single,
2801 -125,
2802 128,
2805 true,
2806 true,
2807 true,
2808 true,
2809 false,
2810 true
2813 const struct real_format coldfire_single_format =
2815 encode_ieee_single,
2816 decode_ieee_single,
2821 -125,
2822 128,
2825 true,
2826 true,
2827 true,
2828 true,
2829 true,
2830 true
2833 /* IEEE double-precision format. */
2835 static void encode_ieee_double (const struct real_format *fmt,
2836 long *, const REAL_VALUE_TYPE *);
2837 static void decode_ieee_double (const struct real_format *,
2838 REAL_VALUE_TYPE *, const long *);
2840 static void
2841 encode_ieee_double (const struct real_format *fmt, long *buf,
2842 const REAL_VALUE_TYPE *r)
2844 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2845 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2847 image_hi = r->sign << 31;
2848 image_lo = 0;
2850 if (HOST_BITS_PER_LONG == 64)
2852 sig_hi = r->sig[SIGSZ-1];
2853 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2854 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2856 else
2858 sig_hi = r->sig[SIGSZ-1];
2859 sig_lo = r->sig[SIGSZ-2];
2860 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2861 sig_hi = (sig_hi >> 11) & 0xfffff;
2864 switch (r->cl)
2866 case rvc_zero:
2867 break;
2869 case rvc_inf:
2870 if (fmt->has_inf)
2871 image_hi |= 2047 << 20;
2872 else
2874 image_hi |= 0x7fffffff;
2875 image_lo = 0xffffffff;
2877 break;
2879 case rvc_nan:
2880 if (fmt->has_nans)
2882 if (r->canonical)
2884 if (fmt->canonical_nan_lsbs_set)
2886 sig_hi = (1 << 19) - 1;
2887 sig_lo = 0xffffffff;
2889 else
2891 sig_hi = 0;
2892 sig_lo = 0;
2895 if (r->signalling == fmt->qnan_msb_set)
2896 sig_hi &= ~(1 << 19);
2897 else
2898 sig_hi |= 1 << 19;
2899 if (sig_hi == 0 && sig_lo == 0)
2900 sig_hi = 1 << 18;
2902 image_hi |= 2047 << 20;
2903 image_hi |= sig_hi;
2904 image_lo = sig_lo;
2906 else
2908 image_hi |= 0x7fffffff;
2909 image_lo = 0xffffffff;
2911 break;
2913 case rvc_normal:
2914 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2915 whereas the intermediate representation is 0.F x 2**exp.
2916 Which means we're off by one. */
2917 if (denormal)
2918 exp = 0;
2919 else
2920 exp = REAL_EXP (r) + 1023 - 1;
2921 image_hi |= exp << 20;
2922 image_hi |= sig_hi;
2923 image_lo = sig_lo;
2924 break;
2926 default:
2927 gcc_unreachable ();
2930 if (FLOAT_WORDS_BIG_ENDIAN)
2931 buf[0] = image_hi, buf[1] = image_lo;
2932 else
2933 buf[0] = image_lo, buf[1] = image_hi;
2936 static void
2937 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2938 const long *buf)
2940 unsigned long image_hi, image_lo;
2941 bool sign;
2942 int exp;
2944 if (FLOAT_WORDS_BIG_ENDIAN)
2945 image_hi = buf[0], image_lo = buf[1];
2946 else
2947 image_lo = buf[0], image_hi = buf[1];
2948 image_lo &= 0xffffffff;
2949 image_hi &= 0xffffffff;
2951 sign = (image_hi >> 31) & 1;
2952 exp = (image_hi >> 20) & 0x7ff;
2954 memset (r, 0, sizeof (*r));
2956 image_hi <<= 32 - 21;
2957 image_hi |= image_lo >> 21;
2958 image_hi &= 0x7fffffff;
2959 image_lo <<= 32 - 21;
2961 if (exp == 0)
2963 if ((image_hi || image_lo) && fmt->has_denorm)
2965 r->cl = rvc_normal;
2966 r->sign = sign;
2967 SET_REAL_EXP (r, -1022);
2968 if (HOST_BITS_PER_LONG == 32)
2970 image_hi = (image_hi << 1) | (image_lo >> 31);
2971 image_lo <<= 1;
2972 r->sig[SIGSZ-1] = image_hi;
2973 r->sig[SIGSZ-2] = image_lo;
2975 else
2977 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2978 r->sig[SIGSZ-1] = image_hi;
2980 normalize (r);
2982 else if (fmt->has_signed_zero)
2983 r->sign = sign;
2985 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2987 if (image_hi || image_lo)
2989 r->cl = rvc_nan;
2990 r->sign = sign;
2991 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
2992 if (HOST_BITS_PER_LONG == 32)
2994 r->sig[SIGSZ-1] = image_hi;
2995 r->sig[SIGSZ-2] = image_lo;
2997 else
2998 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3000 else
3002 r->cl = rvc_inf;
3003 r->sign = sign;
3006 else
3008 r->cl = rvc_normal;
3009 r->sign = sign;
3010 SET_REAL_EXP (r, exp - 1023 + 1);
3011 if (HOST_BITS_PER_LONG == 32)
3013 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3014 r->sig[SIGSZ-2] = image_lo;
3016 else
3017 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3021 const struct real_format ieee_double_format =
3023 encode_ieee_double,
3024 decode_ieee_double,
3029 -1021,
3030 1024,
3033 true,
3034 true,
3035 true,
3036 true,
3037 true,
3038 false
3041 const struct real_format mips_double_format =
3043 encode_ieee_double,
3044 decode_ieee_double,
3049 -1021,
3050 1024,
3053 true,
3054 true,
3055 true,
3056 true,
3057 false,
3058 true
3061 const struct real_format coldfire_double_format =
3063 encode_ieee_double,
3064 decode_ieee_double,
3069 -1021,
3070 1024,
3073 true,
3074 true,
3075 true,
3076 true,
3077 true,
3078 true
3081 /* IEEE extended real format. This comes in three flavors: Intel's as
3082 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3083 12- and 16-byte images may be big- or little endian; Motorola's is
3084 always big endian. */
3086 /* Helper subroutine which converts from the internal format to the
3087 12-byte little-endian Intel format. Functions below adjust this
3088 for the other possible formats. */
3089 static void
3090 encode_ieee_extended (const struct real_format *fmt, long *buf,
3091 const REAL_VALUE_TYPE *r)
3093 unsigned long image_hi, sig_hi, sig_lo;
3094 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3096 image_hi = r->sign << 15;
3097 sig_hi = sig_lo = 0;
3099 switch (r->cl)
3101 case rvc_zero:
3102 break;
3104 case rvc_inf:
3105 if (fmt->has_inf)
3107 image_hi |= 32767;
3109 /* Intel requires the explicit integer bit to be set, otherwise
3110 it considers the value a "pseudo-infinity". Motorola docs
3111 say it doesn't care. */
3112 sig_hi = 0x80000000;
3114 else
3116 image_hi |= 32767;
3117 sig_lo = sig_hi = 0xffffffff;
3119 break;
3121 case rvc_nan:
3122 if (fmt->has_nans)
3124 image_hi |= 32767;
3125 if (HOST_BITS_PER_LONG == 32)
3127 sig_hi = r->sig[SIGSZ-1];
3128 sig_lo = r->sig[SIGSZ-2];
3130 else
3132 sig_lo = r->sig[SIGSZ-1];
3133 sig_hi = sig_lo >> 31 >> 1;
3134 sig_lo &= 0xffffffff;
3136 if (r->signalling == fmt->qnan_msb_set)
3137 sig_hi &= ~(1 << 30);
3138 else
3139 sig_hi |= 1 << 30;
3140 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3141 sig_hi = 1 << 29;
3143 /* Intel requires the explicit integer bit to be set, otherwise
3144 it considers the value a "pseudo-nan". Motorola docs say it
3145 doesn't care. */
3146 sig_hi |= 0x80000000;
3148 else
3150 image_hi |= 32767;
3151 sig_lo = sig_hi = 0xffffffff;
3153 break;
3155 case rvc_normal:
3157 int exp = REAL_EXP (r);
3159 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3160 whereas the intermediate representation is 0.F x 2**exp.
3161 Which means we're off by one.
3163 Except for Motorola, which consider exp=0 and explicit
3164 integer bit set to continue to be normalized. In theory
3165 this discrepancy has been taken care of by the difference
3166 in fmt->emin in round_for_format. */
3168 if (denormal)
3169 exp = 0;
3170 else
3172 exp += 16383 - 1;
3173 gcc_assert (exp >= 0);
3175 image_hi |= exp;
3177 if (HOST_BITS_PER_LONG == 32)
3179 sig_hi = r->sig[SIGSZ-1];
3180 sig_lo = r->sig[SIGSZ-2];
3182 else
3184 sig_lo = r->sig[SIGSZ-1];
3185 sig_hi = sig_lo >> 31 >> 1;
3186 sig_lo &= 0xffffffff;
3189 break;
3191 default:
3192 gcc_unreachable ();
3195 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3198 /* Convert from the internal format to the 12-byte Motorola format
3199 for an IEEE extended real. */
3200 static void
3201 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3202 const REAL_VALUE_TYPE *r)
3204 long intermed[3];
3205 encode_ieee_extended (fmt, intermed, r);
3207 /* Motorola chips are assumed always to be big-endian. Also, the
3208 padding in a Motorola extended real goes between the exponent and
3209 the mantissa. At this point the mantissa is entirely within
3210 elements 0 and 1 of intermed, and the exponent entirely within
3211 element 2, so all we have to do is swap the order around, and
3212 shift element 2 left 16 bits. */
3213 buf[0] = intermed[2] << 16;
3214 buf[1] = intermed[1];
3215 buf[2] = intermed[0];
3218 /* Convert from the internal format to the 12-byte Intel format for
3219 an IEEE extended real. */
3220 static void
3221 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3222 const REAL_VALUE_TYPE *r)
3224 if (FLOAT_WORDS_BIG_ENDIAN)
3226 /* All the padding in an Intel-format extended real goes at the high
3227 end, which in this case is after the mantissa, not the exponent.
3228 Therefore we must shift everything down 16 bits. */
3229 long intermed[3];
3230 encode_ieee_extended (fmt, intermed, r);
3231 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3232 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3233 buf[2] = (intermed[0] << 16);
3235 else
3236 /* encode_ieee_extended produces what we want directly. */
3237 encode_ieee_extended (fmt, buf, r);
3240 /* Convert from the internal format to the 16-byte Intel format for
3241 an IEEE extended real. */
3242 static void
3243 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3244 const REAL_VALUE_TYPE *r)
3246 /* All the padding in an Intel-format extended real goes at the high end. */
3247 encode_ieee_extended_intel_96 (fmt, buf, r);
3248 buf[3] = 0;
3251 /* As above, we have a helper function which converts from 12-byte
3252 little-endian Intel format to internal format. Functions below
3253 adjust for the other possible formats. */
3254 static void
3255 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3256 const long *buf)
3258 unsigned long image_hi, sig_hi, sig_lo;
3259 bool sign;
3260 int exp;
3262 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3263 sig_lo &= 0xffffffff;
3264 sig_hi &= 0xffffffff;
3265 image_hi &= 0xffffffff;
3267 sign = (image_hi >> 15) & 1;
3268 exp = image_hi & 0x7fff;
3270 memset (r, 0, sizeof (*r));
3272 if (exp == 0)
3274 if ((sig_hi || sig_lo) && fmt->has_denorm)
3276 r->cl = rvc_normal;
3277 r->sign = sign;
3279 /* When the IEEE format contains a hidden bit, we know that
3280 it's zero at this point, and so shift up the significand
3281 and decrease the exponent to match. In this case, Motorola
3282 defines the explicit integer bit to be valid, so we don't
3283 know whether the msb is set or not. */
3284 SET_REAL_EXP (r, fmt->emin);
3285 if (HOST_BITS_PER_LONG == 32)
3287 r->sig[SIGSZ-1] = sig_hi;
3288 r->sig[SIGSZ-2] = sig_lo;
3290 else
3291 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3293 normalize (r);
3295 else if (fmt->has_signed_zero)
3296 r->sign = sign;
3298 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3300 /* See above re "pseudo-infinities" and "pseudo-nans".
3301 Short summary is that the MSB will likely always be
3302 set, and that we don't care about it. */
3303 sig_hi &= 0x7fffffff;
3305 if (sig_hi || sig_lo)
3307 r->cl = rvc_nan;
3308 r->sign = sign;
3309 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3310 if (HOST_BITS_PER_LONG == 32)
3312 r->sig[SIGSZ-1] = sig_hi;
3313 r->sig[SIGSZ-2] = sig_lo;
3315 else
3316 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3318 else
3320 r->cl = rvc_inf;
3321 r->sign = sign;
3324 else
3326 r->cl = rvc_normal;
3327 r->sign = sign;
3328 SET_REAL_EXP (r, exp - 16383 + 1);
3329 if (HOST_BITS_PER_LONG == 32)
3331 r->sig[SIGSZ-1] = sig_hi;
3332 r->sig[SIGSZ-2] = sig_lo;
3334 else
3335 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3339 /* Convert from the internal format to the 12-byte Motorola format
3340 for an IEEE extended real. */
3341 static void
3342 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3343 const long *buf)
3345 long intermed[3];
3347 /* Motorola chips are assumed always to be big-endian. Also, the
3348 padding in a Motorola extended real goes between the exponent and
3349 the mantissa; remove it. */
3350 intermed[0] = buf[2];
3351 intermed[1] = buf[1];
3352 intermed[2] = (unsigned long)buf[0] >> 16;
3354 decode_ieee_extended (fmt, r, intermed);
3357 /* Convert from the internal format to the 12-byte Intel format for
3358 an IEEE extended real. */
3359 static void
3360 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3361 const long *buf)
3363 if (FLOAT_WORDS_BIG_ENDIAN)
3365 /* All the padding in an Intel-format extended real goes at the high
3366 end, which in this case is after the mantissa, not the exponent.
3367 Therefore we must shift everything up 16 bits. */
3368 long intermed[3];
3370 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3371 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3372 intermed[2] = ((unsigned long)buf[0] >> 16);
3374 decode_ieee_extended (fmt, r, intermed);
3376 else
3377 /* decode_ieee_extended produces what we want directly. */
3378 decode_ieee_extended (fmt, r, buf);
3381 /* Convert from the internal format to the 16-byte Intel format for
3382 an IEEE extended real. */
3383 static void
3384 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3385 const long *buf)
3387 /* All the padding in an Intel-format extended real goes at the high end. */
3388 decode_ieee_extended_intel_96 (fmt, r, buf);
3391 const struct real_format ieee_extended_motorola_format =
3393 encode_ieee_extended_motorola,
3394 decode_ieee_extended_motorola,
3399 -16382,
3400 16384,
3403 true,
3404 true,
3405 true,
3406 true,
3407 true,
3408 false
3411 const struct real_format ieee_extended_intel_96_format =
3413 encode_ieee_extended_intel_96,
3414 decode_ieee_extended_intel_96,
3419 -16381,
3420 16384,
3423 true,
3424 true,
3425 true,
3426 true,
3427 true,
3428 false
3431 const struct real_format ieee_extended_intel_128_format =
3433 encode_ieee_extended_intel_128,
3434 decode_ieee_extended_intel_128,
3439 -16381,
3440 16384,
3443 true,
3444 true,
3445 true,
3446 true,
3447 true,
3448 false
3451 /* The following caters to i386 systems that set the rounding precision
3452 to 53 bits instead of 64, e.g. FreeBSD. */
3453 const struct real_format ieee_extended_intel_96_round_53_format =
3455 encode_ieee_extended_intel_96,
3456 decode_ieee_extended_intel_96,
3461 -16381,
3462 16384,
3465 true,
3466 true,
3467 true,
3468 true,
3469 true,
3470 false
3473 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3474 numbers whose sum is equal to the extended precision value. The number
3475 with greater magnitude is first. This format has the same magnitude
3476 range as an IEEE double precision value, but effectively 106 bits of
3477 significand precision. Infinity and NaN are represented by their IEEE
3478 double precision value stored in the first number, the second number is
3479 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3481 static void encode_ibm_extended (const struct real_format *fmt,
3482 long *, const REAL_VALUE_TYPE *);
3483 static void decode_ibm_extended (const struct real_format *,
3484 REAL_VALUE_TYPE *, const long *);
3486 static void
3487 encode_ibm_extended (const struct real_format *fmt, long *buf,
3488 const REAL_VALUE_TYPE *r)
3490 REAL_VALUE_TYPE u, normr, v;
3491 const struct real_format *base_fmt;
3493 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3495 /* Renormlize R before doing any arithmetic on it. */
3496 normr = *r;
3497 if (normr.cl == rvc_normal)
3498 normalize (&normr);
3500 /* u = IEEE double precision portion of significand. */
3501 u = normr;
3502 round_for_format (base_fmt, &u);
3503 encode_ieee_double (base_fmt, &buf[0], &u);
3505 if (u.cl == rvc_normal)
3507 do_add (&v, &normr, &u, 1);
3508 /* Call round_for_format since we might need to denormalize. */
3509 round_for_format (base_fmt, &v);
3510 encode_ieee_double (base_fmt, &buf[2], &v);
3512 else
3514 /* Inf, NaN, 0 are all representable as doubles, so the
3515 least-significant part can be 0.0. */
3516 buf[2] = 0;
3517 buf[3] = 0;
3521 static void
3522 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3523 const long *buf)
3525 REAL_VALUE_TYPE u, v;
3526 const struct real_format *base_fmt;
3528 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3529 decode_ieee_double (base_fmt, &u, &buf[0]);
3531 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3533 decode_ieee_double (base_fmt, &v, &buf[2]);
3534 do_add (r, &u, &v, 0);
3536 else
3537 *r = u;
3540 const struct real_format ibm_extended_format =
3542 encode_ibm_extended,
3543 decode_ibm_extended,
3546 53 + 53,
3548 -1021 + 53,
3549 1024,
3550 127,
3552 true,
3553 true,
3554 true,
3555 true,
3556 true,
3557 false
3560 const struct real_format mips_extended_format =
3562 encode_ibm_extended,
3563 decode_ibm_extended,
3566 53 + 53,
3568 -1021 + 53,
3569 1024,
3570 127,
3572 true,
3573 true,
3574 true,
3575 true,
3576 false,
3577 true
3581 /* IEEE quad precision format. */
3583 static void encode_ieee_quad (const struct real_format *fmt,
3584 long *, const REAL_VALUE_TYPE *);
3585 static void decode_ieee_quad (const struct real_format *,
3586 REAL_VALUE_TYPE *, const long *);
3588 static void
3589 encode_ieee_quad (const struct real_format *fmt, long *buf,
3590 const REAL_VALUE_TYPE *r)
3592 unsigned long image3, image2, image1, image0, exp;
3593 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3594 REAL_VALUE_TYPE u;
3596 image3 = r->sign << 31;
3597 image2 = 0;
3598 image1 = 0;
3599 image0 = 0;
3601 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3603 switch (r->cl)
3605 case rvc_zero:
3606 break;
3608 case rvc_inf:
3609 if (fmt->has_inf)
3610 image3 |= 32767 << 16;
3611 else
3613 image3 |= 0x7fffffff;
3614 image2 = 0xffffffff;
3615 image1 = 0xffffffff;
3616 image0 = 0xffffffff;
3618 break;
3620 case rvc_nan:
3621 if (fmt->has_nans)
3623 image3 |= 32767 << 16;
3625 if (r->canonical)
3627 if (fmt->canonical_nan_lsbs_set)
3629 image3 |= 0x7fff;
3630 image2 = image1 = image0 = 0xffffffff;
3633 else if (HOST_BITS_PER_LONG == 32)
3635 image0 = u.sig[0];
3636 image1 = u.sig[1];
3637 image2 = u.sig[2];
3638 image3 |= u.sig[3] & 0xffff;
3640 else
3642 image0 = u.sig[0];
3643 image1 = image0 >> 31 >> 1;
3644 image2 = u.sig[1];
3645 image3 |= (image2 >> 31 >> 1) & 0xffff;
3646 image0 &= 0xffffffff;
3647 image2 &= 0xffffffff;
3649 if (r->signalling == fmt->qnan_msb_set)
3650 image3 &= ~0x8000;
3651 else
3652 image3 |= 0x8000;
3653 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3654 image3 |= 0x4000;
3656 else
3658 image3 |= 0x7fffffff;
3659 image2 = 0xffffffff;
3660 image1 = 0xffffffff;
3661 image0 = 0xffffffff;
3663 break;
3665 case rvc_normal:
3666 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3667 whereas the intermediate representation is 0.F x 2**exp.
3668 Which means we're off by one. */
3669 if (denormal)
3670 exp = 0;
3671 else
3672 exp = REAL_EXP (r) + 16383 - 1;
3673 image3 |= exp << 16;
3675 if (HOST_BITS_PER_LONG == 32)
3677 image0 = u.sig[0];
3678 image1 = u.sig[1];
3679 image2 = u.sig[2];
3680 image3 |= u.sig[3] & 0xffff;
3682 else
3684 image0 = u.sig[0];
3685 image1 = image0 >> 31 >> 1;
3686 image2 = u.sig[1];
3687 image3 |= (image2 >> 31 >> 1) & 0xffff;
3688 image0 &= 0xffffffff;
3689 image2 &= 0xffffffff;
3691 break;
3693 default:
3694 gcc_unreachable ();
3697 if (FLOAT_WORDS_BIG_ENDIAN)
3699 buf[0] = image3;
3700 buf[1] = image2;
3701 buf[2] = image1;
3702 buf[3] = image0;
3704 else
3706 buf[0] = image0;
3707 buf[1] = image1;
3708 buf[2] = image2;
3709 buf[3] = image3;
3713 static void
3714 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3715 const long *buf)
3717 unsigned long image3, image2, image1, image0;
3718 bool sign;
3719 int exp;
3721 if (FLOAT_WORDS_BIG_ENDIAN)
3723 image3 = buf[0];
3724 image2 = buf[1];
3725 image1 = buf[2];
3726 image0 = buf[3];
3728 else
3730 image0 = buf[0];
3731 image1 = buf[1];
3732 image2 = buf[2];
3733 image3 = buf[3];
3735 image0 &= 0xffffffff;
3736 image1 &= 0xffffffff;
3737 image2 &= 0xffffffff;
3739 sign = (image3 >> 31) & 1;
3740 exp = (image3 >> 16) & 0x7fff;
3741 image3 &= 0xffff;
3743 memset (r, 0, sizeof (*r));
3745 if (exp == 0)
3747 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3749 r->cl = rvc_normal;
3750 r->sign = sign;
3752 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
3753 if (HOST_BITS_PER_LONG == 32)
3755 r->sig[0] = image0;
3756 r->sig[1] = image1;
3757 r->sig[2] = image2;
3758 r->sig[3] = image3;
3760 else
3762 r->sig[0] = (image1 << 31 << 1) | image0;
3763 r->sig[1] = (image3 << 31 << 1) | image2;
3766 normalize (r);
3768 else if (fmt->has_signed_zero)
3769 r->sign = sign;
3771 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3773 if (image3 | image2 | image1 | image0)
3775 r->cl = rvc_nan;
3776 r->sign = sign;
3777 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
3779 if (HOST_BITS_PER_LONG == 32)
3781 r->sig[0] = image0;
3782 r->sig[1] = image1;
3783 r->sig[2] = image2;
3784 r->sig[3] = image3;
3786 else
3788 r->sig[0] = (image1 << 31 << 1) | image0;
3789 r->sig[1] = (image3 << 31 << 1) | image2;
3791 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3793 else
3795 r->cl = rvc_inf;
3796 r->sign = sign;
3799 else
3801 r->cl = rvc_normal;
3802 r->sign = sign;
3803 SET_REAL_EXP (r, exp - 16383 + 1);
3805 if (HOST_BITS_PER_LONG == 32)
3807 r->sig[0] = image0;
3808 r->sig[1] = image1;
3809 r->sig[2] = image2;
3810 r->sig[3] = image3;
3812 else
3814 r->sig[0] = (image1 << 31 << 1) | image0;
3815 r->sig[1] = (image3 << 31 << 1) | image2;
3817 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3818 r->sig[SIGSZ-1] |= SIG_MSB;
3822 const struct real_format ieee_quad_format =
3824 encode_ieee_quad,
3825 decode_ieee_quad,
3828 113,
3829 113,
3830 -16381,
3831 16384,
3832 127,
3833 127,
3834 true,
3835 true,
3836 true,
3837 true,
3838 true,
3839 false
3842 const struct real_format mips_quad_format =
3844 encode_ieee_quad,
3845 decode_ieee_quad,
3848 113,
3849 113,
3850 -16381,
3851 16384,
3852 127,
3853 127,
3854 true,
3855 true,
3856 true,
3857 true,
3858 false,
3859 true
3862 /* Descriptions of VAX floating point formats can be found beginning at
3864 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
3866 The thing to remember is that they're almost IEEE, except for word
3867 order, exponent bias, and the lack of infinities, nans, and denormals.
3869 We don't implement the H_floating format here, simply because neither
3870 the VAX or Alpha ports use it. */
3872 static void encode_vax_f (const struct real_format *fmt,
3873 long *, const REAL_VALUE_TYPE *);
3874 static void decode_vax_f (const struct real_format *,
3875 REAL_VALUE_TYPE *, const long *);
3876 static void encode_vax_d (const struct real_format *fmt,
3877 long *, const REAL_VALUE_TYPE *);
3878 static void decode_vax_d (const struct real_format *,
3879 REAL_VALUE_TYPE *, const long *);
3880 static void encode_vax_g (const struct real_format *fmt,
3881 long *, const REAL_VALUE_TYPE *);
3882 static void decode_vax_g (const struct real_format *,
3883 REAL_VALUE_TYPE *, const long *);
3885 static void
3886 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
3887 const REAL_VALUE_TYPE *r)
3889 unsigned long sign, exp, sig, image;
3891 sign = r->sign << 15;
3893 switch (r->cl)
3895 case rvc_zero:
3896 image = 0;
3897 break;
3899 case rvc_inf:
3900 case rvc_nan:
3901 image = 0xffff7fff | sign;
3902 break;
3904 case rvc_normal:
3905 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3906 exp = REAL_EXP (r) + 128;
3908 image = (sig << 16) & 0xffff0000;
3909 image |= sign;
3910 image |= exp << 7;
3911 image |= sig >> 16;
3912 break;
3914 default:
3915 gcc_unreachable ();
3918 buf[0] = image;
3921 static void
3922 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
3923 REAL_VALUE_TYPE *r, const long *buf)
3925 unsigned long image = buf[0] & 0xffffffff;
3926 int exp = (image >> 7) & 0xff;
3928 memset (r, 0, sizeof (*r));
3930 if (exp != 0)
3932 r->cl = rvc_normal;
3933 r->sign = (image >> 15) & 1;
3934 SET_REAL_EXP (r, exp - 128);
3936 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3937 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3941 static void
3942 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
3943 const REAL_VALUE_TYPE *r)
3945 unsigned long image0, image1, sign = r->sign << 15;
3947 switch (r->cl)
3949 case rvc_zero:
3950 image0 = image1 = 0;
3951 break;
3953 case rvc_inf:
3954 case rvc_nan:
3955 image0 = 0xffff7fff | sign;
3956 image1 = 0xffffffff;
3957 break;
3959 case rvc_normal:
3960 /* Extract the significand into straight hi:lo. */
3961 if (HOST_BITS_PER_LONG == 64)
3963 image0 = r->sig[SIGSZ-1];
3964 image1 = (image0 >> (64 - 56)) & 0xffffffff;
3965 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3967 else
3969 image0 = r->sig[SIGSZ-1];
3970 image1 = r->sig[SIGSZ-2];
3971 image1 = (image0 << 24) | (image1 >> 8);
3972 image0 = (image0 >> 8) & 0xffffff;
3975 /* Rearrange the half-words of the significand to match the
3976 external format. */
3977 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3978 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3980 /* Add the sign and exponent. */
3981 image0 |= sign;
3982 image0 |= (REAL_EXP (r) + 128) << 7;
3983 break;
3985 default:
3986 gcc_unreachable ();
3989 if (FLOAT_WORDS_BIG_ENDIAN)
3990 buf[0] = image1, buf[1] = image0;
3991 else
3992 buf[0] = image0, buf[1] = image1;
3995 static void
3996 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
3997 REAL_VALUE_TYPE *r, const long *buf)
3999 unsigned long image0, image1;
4000 int exp;
4002 if (FLOAT_WORDS_BIG_ENDIAN)
4003 image1 = buf[0], image0 = buf[1];
4004 else
4005 image0 = buf[0], image1 = buf[1];
4006 image0 &= 0xffffffff;
4007 image1 &= 0xffffffff;
4009 exp = (image0 >> 7) & 0xff;
4011 memset (r, 0, sizeof (*r));
4013 if (exp != 0)
4015 r->cl = rvc_normal;
4016 r->sign = (image0 >> 15) & 1;
4017 SET_REAL_EXP (r, exp - 128);
4019 /* Rearrange the half-words of the external format into
4020 proper ascending order. */
4021 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4022 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4024 if (HOST_BITS_PER_LONG == 64)
4026 image0 = (image0 << 31 << 1) | image1;
4027 image0 <<= 64 - 56;
4028 image0 |= SIG_MSB;
4029 r->sig[SIGSZ-1] = image0;
4031 else
4033 r->sig[SIGSZ-1] = image0;
4034 r->sig[SIGSZ-2] = image1;
4035 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4036 r->sig[SIGSZ-1] |= SIG_MSB;
4041 static void
4042 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4043 const REAL_VALUE_TYPE *r)
4045 unsigned long image0, image1, sign = r->sign << 15;
4047 switch (r->cl)
4049 case rvc_zero:
4050 image0 = image1 = 0;
4051 break;
4053 case rvc_inf:
4054 case rvc_nan:
4055 image0 = 0xffff7fff | sign;
4056 image1 = 0xffffffff;
4057 break;
4059 case rvc_normal:
4060 /* Extract the significand into straight hi:lo. */
4061 if (HOST_BITS_PER_LONG == 64)
4063 image0 = r->sig[SIGSZ-1];
4064 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4065 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4067 else
4069 image0 = r->sig[SIGSZ-1];
4070 image1 = r->sig[SIGSZ-2];
4071 image1 = (image0 << 21) | (image1 >> 11);
4072 image0 = (image0 >> 11) & 0xfffff;
4075 /* Rearrange the half-words of the significand to match the
4076 external format. */
4077 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4078 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4080 /* Add the sign and exponent. */
4081 image0 |= sign;
4082 image0 |= (REAL_EXP (r) + 1024) << 4;
4083 break;
4085 default:
4086 gcc_unreachable ();
4089 if (FLOAT_WORDS_BIG_ENDIAN)
4090 buf[0] = image1, buf[1] = image0;
4091 else
4092 buf[0] = image0, buf[1] = image1;
4095 static void
4096 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4097 REAL_VALUE_TYPE *r, const long *buf)
4099 unsigned long image0, image1;
4100 int exp;
4102 if (FLOAT_WORDS_BIG_ENDIAN)
4103 image1 = buf[0], image0 = buf[1];
4104 else
4105 image0 = buf[0], image1 = buf[1];
4106 image0 &= 0xffffffff;
4107 image1 &= 0xffffffff;
4109 exp = (image0 >> 4) & 0x7ff;
4111 memset (r, 0, sizeof (*r));
4113 if (exp != 0)
4115 r->cl = rvc_normal;
4116 r->sign = (image0 >> 15) & 1;
4117 SET_REAL_EXP (r, exp - 1024);
4119 /* Rearrange the half-words of the external format into
4120 proper ascending order. */
4121 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4122 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4124 if (HOST_BITS_PER_LONG == 64)
4126 image0 = (image0 << 31 << 1) | image1;
4127 image0 <<= 64 - 53;
4128 image0 |= SIG_MSB;
4129 r->sig[SIGSZ-1] = image0;
4131 else
4133 r->sig[SIGSZ-1] = image0;
4134 r->sig[SIGSZ-2] = image1;
4135 lshift_significand (r, r, 64 - 53);
4136 r->sig[SIGSZ-1] |= SIG_MSB;
4141 const struct real_format vax_f_format =
4143 encode_vax_f,
4144 decode_vax_f,
4149 -127,
4150 127,
4153 false,
4154 false,
4155 false,
4156 false,
4157 false,
4158 false
4161 const struct real_format vax_d_format =
4163 encode_vax_d,
4164 decode_vax_d,
4169 -127,
4170 127,
4173 false,
4174 false,
4175 false,
4176 false,
4177 false,
4178 false
4181 const struct real_format vax_g_format =
4183 encode_vax_g,
4184 decode_vax_g,
4189 -1023,
4190 1023,
4193 false,
4194 false,
4195 false,
4196 false,
4197 false,
4198 false
4201 /* A good reference for these can be found in chapter 9 of
4202 "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
4203 An on-line version can be found here:
4205 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
4208 static void encode_i370_single (const struct real_format *fmt,
4209 long *, const REAL_VALUE_TYPE *);
4210 static void decode_i370_single (const struct real_format *,
4211 REAL_VALUE_TYPE *, const long *);
4212 static void encode_i370_double (const struct real_format *fmt,
4213 long *, const REAL_VALUE_TYPE *);
4214 static void decode_i370_double (const struct real_format *,
4215 REAL_VALUE_TYPE *, const long *);
4217 static void
4218 encode_i370_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4219 long *buf, const REAL_VALUE_TYPE *r)
4221 unsigned long sign, exp, sig, image;
4223 sign = r->sign << 31;
4225 switch (r->cl)
4227 case rvc_zero:
4228 image = 0;
4229 break;
4231 case rvc_inf:
4232 case rvc_nan:
4233 image = 0x7fffffff | sign;
4234 break;
4236 case rvc_normal:
4237 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
4238 exp = ((REAL_EXP (r) / 4) + 64) << 24;
4239 image = sign | exp | sig;
4240 break;
4242 default:
4243 gcc_unreachable ();
4246 buf[0] = image;
4249 static void
4250 decode_i370_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4251 REAL_VALUE_TYPE *r, const long *buf)
4253 unsigned long sign, sig, image = buf[0];
4254 int exp;
4256 sign = (image >> 31) & 1;
4257 exp = (image >> 24) & 0x7f;
4258 sig = image & 0xffffff;
4260 memset (r, 0, sizeof (*r));
4262 if (exp || sig)
4264 r->cl = rvc_normal;
4265 r->sign = sign;
4266 SET_REAL_EXP (r, (exp - 64) * 4);
4267 r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
4268 normalize (r);
4272 static void
4273 encode_i370_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4274 long *buf, const REAL_VALUE_TYPE *r)
4276 unsigned long sign, exp, image_hi, image_lo;
4278 sign = r->sign << 31;
4280 switch (r->cl)
4282 case rvc_zero:
4283 image_hi = image_lo = 0;
4284 break;
4286 case rvc_inf:
4287 case rvc_nan:
4288 image_hi = 0x7fffffff | sign;
4289 image_lo = 0xffffffff;
4290 break;
4292 case rvc_normal:
4293 if (HOST_BITS_PER_LONG == 64)
4295 image_hi = r->sig[SIGSZ-1];
4296 image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
4297 image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
4299 else
4301 image_hi = r->sig[SIGSZ-1];
4302 image_lo = r->sig[SIGSZ-2];
4303 image_lo = (image_lo >> 8) | (image_hi << 24);
4304 image_hi >>= 8;
4307 exp = ((REAL_EXP (r) / 4) + 64) << 24;
4308 image_hi |= sign | exp;
4309 break;
4311 default:
4312 gcc_unreachable ();
4315 if (FLOAT_WORDS_BIG_ENDIAN)
4316 buf[0] = image_hi, buf[1] = image_lo;
4317 else
4318 buf[0] = image_lo, buf[1] = image_hi;
4321 static void
4322 decode_i370_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4323 REAL_VALUE_TYPE *r, const long *buf)
4325 unsigned long sign, image_hi, image_lo;
4326 int exp;
4328 if (FLOAT_WORDS_BIG_ENDIAN)
4329 image_hi = buf[0], image_lo = buf[1];
4330 else
4331 image_lo = buf[0], image_hi = buf[1];
4333 sign = (image_hi >> 31) & 1;
4334 exp = (image_hi >> 24) & 0x7f;
4335 image_hi &= 0xffffff;
4336 image_lo &= 0xffffffff;
4338 memset (r, 0, sizeof (*r));
4340 if (exp || image_hi || image_lo)
4342 r->cl = rvc_normal;
4343 r->sign = sign;
4344 SET_REAL_EXP (r, (exp - 64) * 4 + (SIGNIFICAND_BITS - 56));
4346 if (HOST_BITS_PER_LONG == 32)
4348 r->sig[0] = image_lo;
4349 r->sig[1] = image_hi;
4351 else
4352 r->sig[0] = image_lo | (image_hi << 31 << 1);
4354 normalize (r);
4358 const struct real_format i370_single_format =
4360 encode_i370_single,
4361 decode_i370_single,
4366 -64,
4370 false,
4371 false,
4372 false, /* ??? The encoding does allow for "unnormals". */
4373 false, /* ??? The encoding does allow for "unnormals". */
4374 false,
4375 false
4378 const struct real_format i370_double_format =
4380 encode_i370_double,
4381 decode_i370_double,
4386 -64,
4390 false,
4391 false,
4392 false, /* ??? The encoding does allow for "unnormals". */
4393 false, /* ??? The encoding does allow for "unnormals". */
4394 false,
4395 false
4398 /* Encode real R into a single precision DFP value in BUF. */
4399 static void
4400 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4401 long *buf ATTRIBUTE_UNUSED,
4402 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4404 encode_decimal32 (fmt, buf, r);
4407 /* Decode a single precision DFP value in BUF into a real R. */
4408 static void
4409 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4410 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4411 const long *buf ATTRIBUTE_UNUSED)
4413 decode_decimal32 (fmt, r, buf);
4416 /* Encode real R into a double precision DFP value in BUF. */
4417 static void
4418 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4419 long *buf ATTRIBUTE_UNUSED,
4420 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4422 encode_decimal64 (fmt, buf, r);
4425 /* Decode a double precision DFP value in BUF into a real R. */
4426 static void
4427 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4428 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4429 const long *buf ATTRIBUTE_UNUSED)
4431 decode_decimal64 (fmt, r, buf);
4434 /* Encode real R into a quad precision DFP value in BUF. */
4435 static void
4436 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4437 long *buf ATTRIBUTE_UNUSED,
4438 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4440 encode_decimal128 (fmt, buf, r);
4443 /* Decode a quad precision DFP value in BUF into a real R. */
4444 static void
4445 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4446 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4447 const long *buf ATTRIBUTE_UNUSED)
4449 decode_decimal128 (fmt, r, buf);
4452 /* Single precision decimal floating point (IEEE 754R). */
4453 const struct real_format decimal_single_format =
4455 encode_decimal_single,
4456 decode_decimal_single,
4457 10,
4458 1, /* log10 */
4461 -95,
4465 true,
4466 true,
4467 true,
4468 true,
4469 true,
4470 false
4473 /* Double precision decimal floating point (IEEE 754R). */
4474 const struct real_format decimal_double_format =
4476 encode_decimal_double,
4477 decode_decimal_double,
4479 1, /* log10 */
4482 -383,
4483 384,
4486 true,
4487 true,
4488 true,
4489 true,
4490 true,
4491 false
4494 /* Quad precision decimal floating point (IEEE 754R). */
4495 const struct real_format decimal_quad_format =
4497 encode_decimal_quad,
4498 decode_decimal_quad,
4500 1, /* log10 */
4503 -6143,
4504 6144,
4505 127,
4506 127,
4507 true,
4508 true,
4509 true,
4510 true,
4511 true,
4512 false
4515 /* The "twos-complement" c4x format is officially defined as
4517 x = s(~s).f * 2**e
4519 This is rather misleading. One must remember that F is signed.
4520 A better description would be
4522 x = -1**s * ((s + 1 + .f) * 2**e
4524 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4525 that's -1 * (1+1+(-.5)) == -1.5. I think.
4527 The constructions here are taken from Tables 5-1 and 5-2 of the
4528 TMS320C4x User's Guide wherein step-by-step instructions for
4529 conversion from IEEE are presented. That's close enough to our
4530 internal representation so as to make things easy.
4532 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
4534 static void encode_c4x_single (const struct real_format *fmt,
4535 long *, const REAL_VALUE_TYPE *);
4536 static void decode_c4x_single (const struct real_format *,
4537 REAL_VALUE_TYPE *, const long *);
4538 static void encode_c4x_extended (const struct real_format *fmt,
4539 long *, const REAL_VALUE_TYPE *);
4540 static void decode_c4x_extended (const struct real_format *,
4541 REAL_VALUE_TYPE *, const long *);
4543 static void
4544 encode_c4x_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4545 long *buf, const REAL_VALUE_TYPE *r)
4547 unsigned long image, exp, sig;
4549 switch (r->cl)
4551 case rvc_zero:
4552 exp = -128;
4553 sig = 0;
4554 break;
4556 case rvc_inf:
4557 case rvc_nan:
4558 exp = 127;
4559 sig = 0x800000 - r->sign;
4560 break;
4562 case rvc_normal:
4563 exp = REAL_EXP (r) - 1;
4564 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4565 if (r->sign)
4567 if (sig)
4568 sig = -sig;
4569 else
4570 exp--;
4571 sig |= 0x800000;
4573 break;
4575 default:
4576 gcc_unreachable ();
4579 image = ((exp & 0xff) << 24) | (sig & 0xffffff);
4580 buf[0] = image;
4583 static void
4584 decode_c4x_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4585 REAL_VALUE_TYPE *r, const long *buf)
4587 unsigned long image = buf[0];
4588 unsigned long sig;
4589 int exp, sf;
4591 exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
4592 sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
4594 memset (r, 0, sizeof (*r));
4596 if (exp != -128)
4598 r->cl = rvc_normal;
4600 sig = sf & 0x7fffff;
4601 if (sf < 0)
4603 r->sign = 1;
4604 if (sig)
4605 sig = -sig;
4606 else
4607 exp++;
4609 sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4611 SET_REAL_EXP (r, exp + 1);
4612 r->sig[SIGSZ-1] = sig;
4616 static void
4617 encode_c4x_extended (const struct real_format *fmt ATTRIBUTE_UNUSED,
4618 long *buf, const REAL_VALUE_TYPE *r)
4620 unsigned long exp, sig;
4622 switch (r->cl)
4624 case rvc_zero:
4625 exp = -128;
4626 sig = 0;
4627 break;
4629 case rvc_inf:
4630 case rvc_nan:
4631 exp = 127;
4632 sig = 0x80000000 - r->sign;
4633 break;
4635 case rvc_normal:
4636 exp = REAL_EXP (r) - 1;
4638 sig = r->sig[SIGSZ-1];
4639 if (HOST_BITS_PER_LONG == 64)
4640 sig = sig >> 1 >> 31;
4641 sig &= 0x7fffffff;
4643 if (r->sign)
4645 if (sig)
4646 sig = -sig;
4647 else
4648 exp--;
4649 sig |= 0x80000000;
4651 break;
4653 default:
4654 gcc_unreachable ();
4657 exp = (exp & 0xff) << 24;
4658 sig &= 0xffffffff;
4660 if (FLOAT_WORDS_BIG_ENDIAN)
4661 buf[0] = exp, buf[1] = sig;
4662 else
4663 buf[0] = sig, buf[0] = exp;
4666 static void
4667 decode_c4x_extended (const struct real_format *fmt ATTRIBUTE_UNUSED,
4668 REAL_VALUE_TYPE *r, const long *buf)
4670 unsigned long sig;
4671 int exp, sf;
4673 if (FLOAT_WORDS_BIG_ENDIAN)
4674 exp = buf[0], sf = buf[1];
4675 else
4676 sf = buf[0], exp = buf[1];
4678 exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
4679 sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
4681 memset (r, 0, sizeof (*r));
4683 if (exp != -128)
4685 r->cl = rvc_normal;
4687 sig = sf & 0x7fffffff;
4688 if (sf < 0)
4690 r->sign = 1;
4691 if (sig)
4692 sig = -sig;
4693 else
4694 exp++;
4696 if (HOST_BITS_PER_LONG == 64)
4697 sig = sig << 1 << 31;
4698 sig |= SIG_MSB;
4700 SET_REAL_EXP (r, exp + 1);
4701 r->sig[SIGSZ-1] = sig;
4705 const struct real_format c4x_single_format =
4707 encode_c4x_single,
4708 decode_c4x_single,
4713 -126,
4714 128,
4717 false,
4718 false,
4719 false,
4720 false,
4721 false,
4722 false
4725 const struct real_format c4x_extended_format =
4727 encode_c4x_extended,
4728 decode_c4x_extended,
4733 -126,
4734 128,
4737 false,
4738 false,
4739 false,
4740 false,
4741 false,
4742 false
4746 /* A synthetic "format" for internal arithmetic. It's the size of the
4747 internal significand minus the two bits needed for proper rounding.
4748 The encode and decode routines exist only to satisfy our paranoia
4749 harness. */
4751 static void encode_internal (const struct real_format *fmt,
4752 long *, const REAL_VALUE_TYPE *);
4753 static void decode_internal (const struct real_format *,
4754 REAL_VALUE_TYPE *, const long *);
4756 static void
4757 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4758 const REAL_VALUE_TYPE *r)
4760 memcpy (buf, r, sizeof (*r));
4763 static void
4764 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4765 REAL_VALUE_TYPE *r, const long *buf)
4767 memcpy (r, buf, sizeof (*r));
4770 const struct real_format real_internal_format =
4772 encode_internal,
4773 decode_internal,
4776 SIGNIFICAND_BITS - 2,
4777 SIGNIFICAND_BITS - 2,
4778 -MAX_EXP,
4779 MAX_EXP,
4782 true,
4783 true,
4784 false,
4785 true,
4786 true,
4787 false
4790 /* Calculate the square root of X in mode MODE, and store the result
4791 in R. Return TRUE if the operation does not raise an exception.
4792 For details see "High Precision Division and Square Root",
4793 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4794 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4796 bool
4797 real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode,
4798 const REAL_VALUE_TYPE *x)
4800 static REAL_VALUE_TYPE halfthree;
4801 static bool init = false;
4802 REAL_VALUE_TYPE h, t, i;
4803 int iter, exp;
4805 /* sqrt(-0.0) is -0.0. */
4806 if (real_isnegzero (x))
4808 *r = *x;
4809 return false;
4812 /* Negative arguments return NaN. */
4813 if (real_isneg (x))
4815 get_canonical_qnan (r, 0);
4816 return false;
4819 /* Infinity and NaN return themselves. */
4820 if (real_isinf (x) || real_isnan (x))
4822 *r = *x;
4823 return false;
4826 if (!init)
4828 do_add (&halfthree, &dconst1, &dconsthalf, 0);
4829 init = true;
4832 /* Initial guess for reciprocal sqrt, i. */
4833 exp = real_exponent (x);
4834 real_ldexp (&i, &dconst1, -exp/2);
4836 /* Newton's iteration for reciprocal sqrt, i. */
4837 for (iter = 0; iter < 16; iter++)
4839 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4840 do_multiply (&t, x, &i);
4841 do_multiply (&h, &t, &i);
4842 do_multiply (&t, &h, &dconsthalf);
4843 do_add (&h, &halfthree, &t, 1);
4844 do_multiply (&t, &i, &h);
4846 /* Check for early convergence. */
4847 if (iter >= 6 && real_identical (&i, &t))
4848 break;
4850 /* ??? Unroll loop to avoid copying. */
4851 i = t;
4854 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4855 do_multiply (&t, x, &i);
4856 do_multiply (&h, &t, &i);
4857 do_add (&i, &dconst1, &h, 1);
4858 do_multiply (&h, &t, &i);
4859 do_multiply (&i, &dconsthalf, &h);
4860 do_add (&h, &t, &i, 0);
4862 /* ??? We need a Tuckerman test to get the last bit. */
4864 real_convert (r, mode, &h);
4865 return true;
4868 /* Calculate X raised to the integer exponent N in mode MODE and store
4869 the result in R. Return true if the result may be inexact due to
4870 loss of precision. The algorithm is the classic "left-to-right binary
4871 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4872 Algorithms", "The Art of Computer Programming", Volume 2. */
4874 bool
4875 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4876 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4878 unsigned HOST_WIDE_INT bit;
4879 REAL_VALUE_TYPE t;
4880 bool inexact = false;
4881 bool init = false;
4882 bool neg;
4883 int i;
4885 if (n == 0)
4887 *r = dconst1;
4888 return false;
4890 else if (n < 0)
4892 /* Don't worry about overflow, from now on n is unsigned. */
4893 neg = true;
4894 n = -n;
4896 else
4897 neg = false;
4899 t = *x;
4900 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4901 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4903 if (init)
4905 inexact |= do_multiply (&t, &t, &t);
4906 if (n & bit)
4907 inexact |= do_multiply (&t, &t, x);
4909 else if (n & bit)
4910 init = true;
4911 bit >>= 1;
4914 if (neg)
4915 inexact |= do_divide (&t, &dconst1, &t);
4917 real_convert (r, mode, &t);
4918 return inexact;
4921 /* Round X to the nearest integer not larger in absolute value, i.e.
4922 towards zero, placing the result in R in mode MODE. */
4924 void
4925 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4926 const REAL_VALUE_TYPE *x)
4928 do_fix_trunc (r, x);
4929 if (mode != VOIDmode)
4930 real_convert (r, mode, r);
4933 /* Round X to the largest integer not greater in value, i.e. round
4934 down, placing the result in R in mode MODE. */
4936 void
4937 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4938 const REAL_VALUE_TYPE *x)
4940 REAL_VALUE_TYPE t;
4942 do_fix_trunc (&t, x);
4943 if (! real_identical (&t, x) && x->sign)
4944 do_add (&t, &t, &dconstm1, 0);
4945 if (mode != VOIDmode)
4946 real_convert (r, mode, &t);
4947 else
4948 *r = t;
4951 /* Round X to the smallest integer not less then argument, i.e. round
4952 up, placing the result in R in mode MODE. */
4954 void
4955 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4956 const REAL_VALUE_TYPE *x)
4958 REAL_VALUE_TYPE t;
4960 do_fix_trunc (&t, x);
4961 if (! real_identical (&t, x) && ! x->sign)
4962 do_add (&t, &t, &dconst1, 0);
4963 if (mode != VOIDmode)
4964 real_convert (r, mode, &t);
4965 else
4966 *r = t;
4969 /* Round X to the nearest integer, but round halfway cases away from
4970 zero. */
4972 void
4973 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4974 const REAL_VALUE_TYPE *x)
4976 do_add (r, x, &dconsthalf, x->sign);
4977 do_fix_trunc (r, r);
4978 if (mode != VOIDmode)
4979 real_convert (r, mode, r);
4982 /* Set the sign of R to the sign of X. */
4984 void
4985 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4987 r->sign = x->sign;
4990 /* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible
4991 for initializing and clearing the MPFR parameter. */
4993 void
4994 mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode)
4996 /* We use a string as an intermediate type. */
4997 char buf[128];
4998 int ret;
5000 /* Take care of Infinity and NaN. */
5001 if (r->cl == rvc_inf)
5003 mpfr_set_inf (m, r->sign);
5004 return;
5007 if (r->cl == rvc_nan)
5009 mpfr_set_nan (m);
5010 return;
5013 real_to_hexadecimal (buf, r, sizeof (buf), 0, 1);
5014 /* mpfr_set_str() parses hexadecimal floats from strings in the same
5015 format that GCC will output them. Nothing extra is needed. */
5016 ret = mpfr_set_str (m, buf, 16, rndmode);
5017 gcc_assert (ret == 0);
5020 /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
5021 mode RNDMODE. TYPE is only relevant if M is a NaN. */
5023 void
5024 real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode)
5026 /* We use a string as an intermediate type. */
5027 char buf[128], *rstr;
5028 mp_exp_t exp;
5030 /* Take care of Infinity and NaN. */
5031 if (mpfr_inf_p (m))
5033 real_inf (r);
5034 if (mpfr_sgn (m) < 0)
5035 *r = REAL_VALUE_NEGATE (*r);
5036 return;
5039 if (mpfr_nan_p (m))
5041 real_nan (r, "", 1, TYPE_MODE (type));
5042 return;
5045 rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode);
5047 /* The additional 12 chars add space for the sprintf below. This
5048 leaves 6 digits for the exponent which is supposedly enough. */
5049 gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12);
5051 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
5052 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
5053 for that. */
5054 exp *= 4;
5056 if (rstr[0] == '-')
5057 sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp);
5058 else
5059 sprintf (buf, "0x.%sp%d", rstr, (int) exp);
5061 mpfr_free_str (rstr);
5063 real_from_string (r, buf);
5066 /* Check whether the real constant value given is an integer. */
5068 bool
5069 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode)
5071 REAL_VALUE_TYPE cint;
5073 real_trunc (&cint, mode, c);
5074 return real_identical (c, &cint);