* config/darwin-c.c: Remove c-tree.h include.
[official-gcc.git] / gcc / real.c
blob8a5799e5d94b2b91c83bb5d0352555ad043886b6
1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002,
3 2003, 2004, 2005, 2007, 2008, 2009 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 3, 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 COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "toplev.h"
29 #include "real.h"
30 #include "realmpfr.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 26.
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. */
71 /* Used to classify two numbers simultaneously. */
72 #define CLASS2(A, B) ((A) << 2 | (B))
74 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
75 #error "Some constant folding done by hand to avoid shift count warnings"
76 #endif
78 static void get_zero (REAL_VALUE_TYPE *, int);
79 static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
80 static void get_canonical_snan (REAL_VALUE_TYPE *, int);
81 static void get_inf (REAL_VALUE_TYPE *, int);
82 static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
83 const REAL_VALUE_TYPE *, unsigned int);
84 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
85 unsigned int);
86 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
87 unsigned int);
88 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
89 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
90 const REAL_VALUE_TYPE *);
91 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
92 const REAL_VALUE_TYPE *, int);
93 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
94 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
95 static int cmp_significand_0 (const REAL_VALUE_TYPE *);
96 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
97 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
98 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
99 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
100 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
101 const REAL_VALUE_TYPE *);
102 static void normalize (REAL_VALUE_TYPE *);
104 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
105 const REAL_VALUE_TYPE *, int);
106 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
107 const REAL_VALUE_TYPE *);
108 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
109 const REAL_VALUE_TYPE *);
110 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
111 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
113 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
114 static void decimal_from_integer (REAL_VALUE_TYPE *);
115 static void decimal_integer_string (char *, const REAL_VALUE_TYPE *,
116 size_t);
118 static const REAL_VALUE_TYPE * ten_to_ptwo (int);
119 static const REAL_VALUE_TYPE * ten_to_mptwo (int);
120 static const REAL_VALUE_TYPE * real_digit (int);
121 static void times_pten (REAL_VALUE_TYPE *, int);
123 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
125 /* Initialize R with a positive zero. */
127 static inline void
128 get_zero (REAL_VALUE_TYPE *r, int sign)
130 memset (r, 0, sizeof (*r));
131 r->sign = sign;
134 /* Initialize R with the canonical quiet NaN. */
136 static inline void
137 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
139 memset (r, 0, sizeof (*r));
140 r->cl = rvc_nan;
141 r->sign = sign;
142 r->canonical = 1;
145 static inline void
146 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
148 memset (r, 0, sizeof (*r));
149 r->cl = rvc_nan;
150 r->sign = sign;
151 r->signalling = 1;
152 r->canonical = 1;
155 static inline void
156 get_inf (REAL_VALUE_TYPE *r, int sign)
158 memset (r, 0, sizeof (*r));
159 r->cl = rvc_inf;
160 r->sign = sign;
164 /* Right-shift the significand of A by N bits; put the result in the
165 significand of R. If any one bits are shifted out, return true. */
167 static bool
168 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
169 unsigned int n)
171 unsigned long sticky = 0;
172 unsigned int i, ofs = 0;
174 if (n >= HOST_BITS_PER_LONG)
176 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
177 sticky |= a->sig[i];
178 n &= HOST_BITS_PER_LONG - 1;
181 if (n != 0)
183 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
184 for (i = 0; i < SIGSZ; ++i)
186 r->sig[i]
187 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
188 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
189 << (HOST_BITS_PER_LONG - n)));
192 else
194 for (i = 0; ofs + i < SIGSZ; ++i)
195 r->sig[i] = a->sig[ofs + i];
196 for (; i < SIGSZ; ++i)
197 r->sig[i] = 0;
200 return sticky != 0;
203 /* Right-shift the significand of A by N bits; put the result in the
204 significand of R. */
206 static void
207 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
208 unsigned int n)
210 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
212 n &= HOST_BITS_PER_LONG - 1;
213 if (n != 0)
215 for (i = 0; i < SIGSZ; ++i)
217 r->sig[i]
218 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
219 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
220 << (HOST_BITS_PER_LONG - n)));
223 else
225 for (i = 0; ofs + i < SIGSZ; ++i)
226 r->sig[i] = a->sig[ofs + i];
227 for (; i < SIGSZ; ++i)
228 r->sig[i] = 0;
232 /* Left-shift the significand of A by N bits; put the result in the
233 significand of R. */
235 static void
236 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
237 unsigned int n)
239 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
241 n &= HOST_BITS_PER_LONG - 1;
242 if (n == 0)
244 for (i = 0; ofs + i < SIGSZ; ++i)
245 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
246 for (; i < SIGSZ; ++i)
247 r->sig[SIGSZ-1-i] = 0;
249 else
250 for (i = 0; i < SIGSZ; ++i)
252 r->sig[SIGSZ-1-i]
253 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
254 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
255 >> (HOST_BITS_PER_LONG - n)));
259 /* Likewise, but N is specialized to 1. */
261 static inline void
262 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
264 unsigned int i;
266 for (i = SIGSZ - 1; i > 0; --i)
267 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
268 r->sig[0] = a->sig[0] << 1;
271 /* Add the significands of A and B, placing the result in R. Return
272 true if there was carry out of the most significant word. */
274 static inline bool
275 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
276 const REAL_VALUE_TYPE *b)
278 bool carry = false;
279 int i;
281 for (i = 0; i < SIGSZ; ++i)
283 unsigned long ai = a->sig[i];
284 unsigned long ri = ai + b->sig[i];
286 if (carry)
288 carry = ri < ai;
289 carry |= ++ri == 0;
291 else
292 carry = ri < ai;
294 r->sig[i] = ri;
297 return carry;
300 /* Subtract the significands of A and B, placing the result in R. CARRY is
301 true if there's a borrow incoming to the least significant word.
302 Return true if there was borrow out of the most significant word. */
304 static inline bool
305 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
306 const REAL_VALUE_TYPE *b, int carry)
308 int i;
310 for (i = 0; i < SIGSZ; ++i)
312 unsigned long ai = a->sig[i];
313 unsigned long ri = ai - b->sig[i];
315 if (carry)
317 carry = ri > ai;
318 carry |= ~--ri == 0;
320 else
321 carry = ri > ai;
323 r->sig[i] = ri;
326 return carry;
329 /* Negate the significand A, placing the result in R. */
331 static inline void
332 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
334 bool carry = true;
335 int i;
337 for (i = 0; i < SIGSZ; ++i)
339 unsigned long ri, ai = a->sig[i];
341 if (carry)
343 if (ai)
345 ri = -ai;
346 carry = false;
348 else
349 ri = ai;
351 else
352 ri = ~ai;
354 r->sig[i] = ri;
358 /* Compare significands. Return tri-state vs zero. */
360 static inline int
361 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
363 int i;
365 for (i = SIGSZ - 1; i >= 0; --i)
367 unsigned long ai = a->sig[i];
368 unsigned long bi = b->sig[i];
370 if (ai > bi)
371 return 1;
372 if (ai < bi)
373 return -1;
376 return 0;
379 /* Return true if A is nonzero. */
381 static inline int
382 cmp_significand_0 (const REAL_VALUE_TYPE *a)
384 int i;
386 for (i = SIGSZ - 1; i >= 0; --i)
387 if (a->sig[i])
388 return 1;
390 return 0;
393 /* Set bit N of the significand of R. */
395 static inline void
396 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
398 r->sig[n / HOST_BITS_PER_LONG]
399 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
402 /* Clear bit N of the significand of R. */
404 static inline void
405 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
407 r->sig[n / HOST_BITS_PER_LONG]
408 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
411 /* Test bit N of the significand of R. */
413 static inline bool
414 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
416 /* ??? Compiler bug here if we return this expression directly.
417 The conversion to bool strips the "&1" and we wind up testing
418 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
419 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
420 return t;
423 /* Clear bits 0..N-1 of the significand of R. */
425 static void
426 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
428 int i, w = n / HOST_BITS_PER_LONG;
430 for (i = 0; i < w; ++i)
431 r->sig[i] = 0;
433 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
436 /* Divide the significands of A and B, placing the result in R. Return
437 true if the division was inexact. */
439 static inline bool
440 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
441 const REAL_VALUE_TYPE *b)
443 REAL_VALUE_TYPE u;
444 int i, bit = SIGNIFICAND_BITS - 1;
445 unsigned long msb, inexact;
447 u = *a;
448 memset (r->sig, 0, sizeof (r->sig));
450 msb = 0;
451 goto start;
454 msb = u.sig[SIGSZ-1] & SIG_MSB;
455 lshift_significand_1 (&u, &u);
456 start:
457 if (msb || cmp_significands (&u, b) >= 0)
459 sub_significands (&u, &u, b, 0);
460 set_significand_bit (r, bit);
463 while (--bit >= 0);
465 for (i = 0, inexact = 0; i < SIGSZ; i++)
466 inexact |= u.sig[i];
468 return inexact != 0;
471 /* Adjust the exponent and significand of R such that the most
472 significant bit is set. We underflow to zero and overflow to
473 infinity here, without denormals. (The intermediate representation
474 exponent is large enough to handle target denormals normalized.) */
476 static void
477 normalize (REAL_VALUE_TYPE *r)
479 int shift = 0, exp;
480 int i, j;
482 if (r->decimal)
483 return;
485 /* Find the first word that is nonzero. */
486 for (i = SIGSZ - 1; i >= 0; i--)
487 if (r->sig[i] == 0)
488 shift += HOST_BITS_PER_LONG;
489 else
490 break;
492 /* Zero significand flushes to zero. */
493 if (i < 0)
495 r->cl = rvc_zero;
496 SET_REAL_EXP (r, 0);
497 return;
500 /* Find the first bit that is nonzero. */
501 for (j = 0; ; j++)
502 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
503 break;
504 shift += j;
506 if (shift > 0)
508 exp = REAL_EXP (r) - shift;
509 if (exp > MAX_EXP)
510 get_inf (r, r->sign);
511 else if (exp < -MAX_EXP)
512 get_zero (r, r->sign);
513 else
515 SET_REAL_EXP (r, exp);
516 lshift_significand (r, r, shift);
521 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
522 result may be inexact due to a loss of precision. */
524 static bool
525 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
526 const REAL_VALUE_TYPE *b, int subtract_p)
528 int dexp, sign, exp;
529 REAL_VALUE_TYPE t;
530 bool inexact = false;
532 /* Determine if we need to add or subtract. */
533 sign = a->sign;
534 subtract_p = (sign ^ b->sign) ^ subtract_p;
536 switch (CLASS2 (a->cl, b->cl))
538 case CLASS2 (rvc_zero, rvc_zero):
539 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
540 get_zero (r, sign & !subtract_p);
541 return false;
543 case CLASS2 (rvc_zero, rvc_normal):
544 case CLASS2 (rvc_zero, rvc_inf):
545 case CLASS2 (rvc_zero, rvc_nan):
546 /* 0 + ANY = ANY. */
547 case CLASS2 (rvc_normal, rvc_nan):
548 case CLASS2 (rvc_inf, rvc_nan):
549 case CLASS2 (rvc_nan, rvc_nan):
550 /* ANY + NaN = NaN. */
551 case CLASS2 (rvc_normal, rvc_inf):
552 /* R + Inf = Inf. */
553 *r = *b;
554 r->sign = sign ^ subtract_p;
555 return false;
557 case CLASS2 (rvc_normal, rvc_zero):
558 case CLASS2 (rvc_inf, rvc_zero):
559 case CLASS2 (rvc_nan, rvc_zero):
560 /* ANY + 0 = ANY. */
561 case CLASS2 (rvc_nan, rvc_normal):
562 case CLASS2 (rvc_nan, rvc_inf):
563 /* NaN + ANY = NaN. */
564 case CLASS2 (rvc_inf, rvc_normal):
565 /* Inf + R = Inf. */
566 *r = *a;
567 return false;
569 case CLASS2 (rvc_inf, rvc_inf):
570 if (subtract_p)
571 /* Inf - Inf = NaN. */
572 get_canonical_qnan (r, 0);
573 else
574 /* Inf + Inf = Inf. */
575 *r = *a;
576 return false;
578 case CLASS2 (rvc_normal, rvc_normal):
579 break;
581 default:
582 gcc_unreachable ();
585 /* Swap the arguments such that A has the larger exponent. */
586 dexp = REAL_EXP (a) - REAL_EXP (b);
587 if (dexp < 0)
589 const REAL_VALUE_TYPE *t;
590 t = a, a = b, b = t;
591 dexp = -dexp;
592 sign ^= subtract_p;
594 exp = REAL_EXP (a);
596 /* If the exponents are not identical, we need to shift the
597 significand of B down. */
598 if (dexp > 0)
600 /* If the exponents are too far apart, the significands
601 do not overlap, which makes the subtraction a noop. */
602 if (dexp >= SIGNIFICAND_BITS)
604 *r = *a;
605 r->sign = sign;
606 return true;
609 inexact |= sticky_rshift_significand (&t, b, dexp);
610 b = &t;
613 if (subtract_p)
615 if (sub_significands (r, a, b, inexact))
617 /* We got a borrow out of the subtraction. That means that
618 A and B had the same exponent, and B had the larger
619 significand. We need to swap the sign and negate the
620 significand. */
621 sign ^= 1;
622 neg_significand (r, r);
625 else
627 if (add_significands (r, a, b))
629 /* We got carry out of the addition. This means we need to
630 shift the significand back down one bit and increase the
631 exponent. */
632 inexact |= sticky_rshift_significand (r, r, 1);
633 r->sig[SIGSZ-1] |= SIG_MSB;
634 if (++exp > MAX_EXP)
636 get_inf (r, sign);
637 return true;
642 r->cl = rvc_normal;
643 r->sign = sign;
644 SET_REAL_EXP (r, exp);
645 /* Zero out the remaining fields. */
646 r->signalling = 0;
647 r->canonical = 0;
648 r->decimal = 0;
650 /* Re-normalize the result. */
651 normalize (r);
653 /* Special case: if the subtraction results in zero, the result
654 is positive. */
655 if (r->cl == rvc_zero)
656 r->sign = 0;
657 else
658 r->sig[0] |= inexact;
660 return inexact;
663 /* Calculate R = A * B. Return true if the result may be inexact. */
665 static bool
666 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
667 const REAL_VALUE_TYPE *b)
669 REAL_VALUE_TYPE u, t, *rr;
670 unsigned int i, j, k;
671 int sign = a->sign ^ b->sign;
672 bool inexact = false;
674 switch (CLASS2 (a->cl, b->cl))
676 case CLASS2 (rvc_zero, rvc_zero):
677 case CLASS2 (rvc_zero, rvc_normal):
678 case CLASS2 (rvc_normal, rvc_zero):
679 /* +-0 * ANY = 0 with appropriate sign. */
680 get_zero (r, sign);
681 return false;
683 case CLASS2 (rvc_zero, rvc_nan):
684 case CLASS2 (rvc_normal, rvc_nan):
685 case CLASS2 (rvc_inf, rvc_nan):
686 case CLASS2 (rvc_nan, rvc_nan):
687 /* ANY * NaN = NaN. */
688 *r = *b;
689 r->sign = sign;
690 return false;
692 case CLASS2 (rvc_nan, rvc_zero):
693 case CLASS2 (rvc_nan, rvc_normal):
694 case CLASS2 (rvc_nan, rvc_inf):
695 /* NaN * ANY = NaN. */
696 *r = *a;
697 r->sign = sign;
698 return false;
700 case CLASS2 (rvc_zero, rvc_inf):
701 case CLASS2 (rvc_inf, rvc_zero):
702 /* 0 * Inf = NaN */
703 get_canonical_qnan (r, sign);
704 return false;
706 case CLASS2 (rvc_inf, rvc_inf):
707 case CLASS2 (rvc_normal, rvc_inf):
708 case CLASS2 (rvc_inf, rvc_normal):
709 /* Inf * Inf = Inf, R * Inf = Inf */
710 get_inf (r, sign);
711 return false;
713 case CLASS2 (rvc_normal, rvc_normal):
714 break;
716 default:
717 gcc_unreachable ();
720 if (r == a || r == b)
721 rr = &t;
722 else
723 rr = r;
724 get_zero (rr, 0);
726 /* Collect all the partial products. Since we don't have sure access
727 to a widening multiply, we split each long into two half-words.
729 Consider the long-hand form of a four half-word multiplication:
731 A B C D
732 * E F G H
733 --------------
734 DE DF DG DH
735 CE CF CG CH
736 BE BF BG BH
737 AE AF AG AH
739 We construct partial products of the widened half-word products
740 that are known to not overlap, e.g. DF+DH. Each such partial
741 product is given its proper exponent, which allows us to sum them
742 and obtain the finished product. */
744 for (i = 0; i < SIGSZ * 2; ++i)
746 unsigned long ai = a->sig[i / 2];
747 if (i & 1)
748 ai >>= HOST_BITS_PER_LONG / 2;
749 else
750 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
752 if (ai == 0)
753 continue;
755 for (j = 0; j < 2; ++j)
757 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
758 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
760 if (exp > MAX_EXP)
762 get_inf (r, sign);
763 return true;
765 if (exp < -MAX_EXP)
767 /* Would underflow to zero, which we shouldn't bother adding. */
768 inexact = true;
769 continue;
772 memset (&u, 0, sizeof (u));
773 u.cl = rvc_normal;
774 SET_REAL_EXP (&u, exp);
776 for (k = j; k < SIGSZ * 2; k += 2)
778 unsigned long bi = b->sig[k / 2];
779 if (k & 1)
780 bi >>= HOST_BITS_PER_LONG / 2;
781 else
782 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
784 u.sig[k / 2] = ai * bi;
787 normalize (&u);
788 inexact |= do_add (rr, rr, &u, 0);
792 rr->sign = sign;
793 if (rr != r)
794 *r = t;
796 return inexact;
799 /* Calculate R = A / B. Return true if the result may be inexact. */
801 static bool
802 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
803 const REAL_VALUE_TYPE *b)
805 int exp, sign = a->sign ^ b->sign;
806 REAL_VALUE_TYPE t, *rr;
807 bool inexact;
809 switch (CLASS2 (a->cl, b->cl))
811 case CLASS2 (rvc_zero, rvc_zero):
812 /* 0 / 0 = NaN. */
813 case CLASS2 (rvc_inf, rvc_inf):
814 /* Inf / Inf = NaN. */
815 get_canonical_qnan (r, sign);
816 return false;
818 case CLASS2 (rvc_zero, rvc_normal):
819 case CLASS2 (rvc_zero, rvc_inf):
820 /* 0 / ANY = 0. */
821 case CLASS2 (rvc_normal, rvc_inf):
822 /* R / Inf = 0. */
823 get_zero (r, sign);
824 return false;
826 case CLASS2 (rvc_normal, rvc_zero):
827 /* R / 0 = Inf. */
828 case CLASS2 (rvc_inf, rvc_zero):
829 /* Inf / 0 = Inf. */
830 get_inf (r, sign);
831 return false;
833 case CLASS2 (rvc_zero, rvc_nan):
834 case CLASS2 (rvc_normal, rvc_nan):
835 case CLASS2 (rvc_inf, rvc_nan):
836 case CLASS2 (rvc_nan, rvc_nan):
837 /* ANY / NaN = NaN. */
838 *r = *b;
839 r->sign = sign;
840 return false;
842 case CLASS2 (rvc_nan, rvc_zero):
843 case CLASS2 (rvc_nan, rvc_normal):
844 case CLASS2 (rvc_nan, rvc_inf):
845 /* NaN / ANY = NaN. */
846 *r = *a;
847 r->sign = sign;
848 return false;
850 case CLASS2 (rvc_inf, rvc_normal):
851 /* Inf / R = Inf. */
852 get_inf (r, sign);
853 return false;
855 case CLASS2 (rvc_normal, rvc_normal):
856 break;
858 default:
859 gcc_unreachable ();
862 if (r == a || r == b)
863 rr = &t;
864 else
865 rr = r;
867 /* Make sure all fields in the result are initialized. */
868 get_zero (rr, 0);
869 rr->cl = rvc_normal;
870 rr->sign = sign;
872 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
873 if (exp > MAX_EXP)
875 get_inf (r, sign);
876 return true;
878 if (exp < -MAX_EXP)
880 get_zero (r, sign);
881 return true;
883 SET_REAL_EXP (rr, exp);
885 inexact = div_significands (rr, a, b);
887 /* Re-normalize the result. */
888 normalize (rr);
889 rr->sig[0] |= inexact;
891 if (rr != r)
892 *r = t;
894 return inexact;
897 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
898 one of the two operands is a NaN. */
900 static int
901 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
902 int nan_result)
904 int ret;
906 switch (CLASS2 (a->cl, b->cl))
908 case CLASS2 (rvc_zero, rvc_zero):
909 /* Sign of zero doesn't matter for compares. */
910 return 0;
912 case CLASS2 (rvc_normal, rvc_zero):
913 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
914 if (a->decimal)
915 return decimal_do_compare (a, b, nan_result);
916 /* Fall through. */
917 case CLASS2 (rvc_inf, rvc_zero):
918 case CLASS2 (rvc_inf, rvc_normal):
919 return (a->sign ? -1 : 1);
921 case CLASS2 (rvc_inf, rvc_inf):
922 return -a->sign - -b->sign;
924 case CLASS2 (rvc_zero, rvc_normal):
925 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
926 if (b->decimal)
927 return decimal_do_compare (a, b, nan_result);
928 /* Fall through. */
929 case CLASS2 (rvc_zero, rvc_inf):
930 case CLASS2 (rvc_normal, rvc_inf):
931 return (b->sign ? 1 : -1);
933 case CLASS2 (rvc_zero, rvc_nan):
934 case CLASS2 (rvc_normal, rvc_nan):
935 case CLASS2 (rvc_inf, rvc_nan):
936 case CLASS2 (rvc_nan, rvc_nan):
937 case CLASS2 (rvc_nan, rvc_zero):
938 case CLASS2 (rvc_nan, rvc_normal):
939 case CLASS2 (rvc_nan, rvc_inf):
940 return nan_result;
942 case CLASS2 (rvc_normal, rvc_normal):
943 break;
945 default:
946 gcc_unreachable ();
949 if (a->sign != b->sign)
950 return -a->sign - -b->sign;
952 if (a->decimal || b->decimal)
953 return decimal_do_compare (a, b, nan_result);
955 if (REAL_EXP (a) > REAL_EXP (b))
956 ret = 1;
957 else if (REAL_EXP (a) < REAL_EXP (b))
958 ret = -1;
959 else
960 ret = cmp_significands (a, b);
962 return (a->sign ? -ret : ret);
965 /* Return A truncated to an integral value toward zero. */
967 static void
968 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
970 *r = *a;
972 switch (r->cl)
974 case rvc_zero:
975 case rvc_inf:
976 case rvc_nan:
977 break;
979 case rvc_normal:
980 if (r->decimal)
982 decimal_do_fix_trunc (r, a);
983 return;
985 if (REAL_EXP (r) <= 0)
986 get_zero (r, r->sign);
987 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
988 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
989 break;
991 default:
992 gcc_unreachable ();
996 /* Perform the binary or unary operation described by CODE.
997 For a unary operation, leave OP1 NULL. This function returns
998 true if the result may be inexact due to loss of precision. */
1000 bool
1001 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
1002 const REAL_VALUE_TYPE *op1)
1004 enum tree_code code = (enum tree_code) icode;
1006 if (op0->decimal || (op1 && op1->decimal))
1007 return decimal_real_arithmetic (r, code, op0, op1);
1009 switch (code)
1011 case PLUS_EXPR:
1012 return do_add (r, op0, op1, 0);
1014 case MINUS_EXPR:
1015 return do_add (r, op0, op1, 1);
1017 case MULT_EXPR:
1018 return do_multiply (r, op0, op1);
1020 case RDIV_EXPR:
1021 return do_divide (r, op0, op1);
1023 case MIN_EXPR:
1024 if (op1->cl == rvc_nan)
1025 *r = *op1;
1026 else if (do_compare (op0, op1, -1) < 0)
1027 *r = *op0;
1028 else
1029 *r = *op1;
1030 break;
1032 case MAX_EXPR:
1033 if (op1->cl == rvc_nan)
1034 *r = *op1;
1035 else if (do_compare (op0, op1, 1) < 0)
1036 *r = *op1;
1037 else
1038 *r = *op0;
1039 break;
1041 case NEGATE_EXPR:
1042 *r = *op0;
1043 r->sign ^= 1;
1044 break;
1046 case ABS_EXPR:
1047 *r = *op0;
1048 r->sign = 0;
1049 break;
1051 case FIX_TRUNC_EXPR:
1052 do_fix_trunc (r, op0);
1053 break;
1055 default:
1056 gcc_unreachable ();
1058 return false;
1061 REAL_VALUE_TYPE
1062 real_value_negate (const REAL_VALUE_TYPE *op0)
1064 REAL_VALUE_TYPE r;
1065 real_arithmetic (&r, NEGATE_EXPR, op0, NULL);
1066 return r;
1069 REAL_VALUE_TYPE
1070 real_value_abs (const REAL_VALUE_TYPE *op0)
1072 REAL_VALUE_TYPE r;
1073 real_arithmetic (&r, ABS_EXPR, op0, NULL);
1074 return r;
1077 bool
1078 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1079 const REAL_VALUE_TYPE *op1)
1081 enum tree_code code = (enum tree_code) icode;
1083 switch (code)
1085 case LT_EXPR:
1086 return do_compare (op0, op1, 1) < 0;
1087 case LE_EXPR:
1088 return do_compare (op0, op1, 1) <= 0;
1089 case GT_EXPR:
1090 return do_compare (op0, op1, -1) > 0;
1091 case GE_EXPR:
1092 return do_compare (op0, op1, -1) >= 0;
1093 case EQ_EXPR:
1094 return do_compare (op0, op1, -1) == 0;
1095 case NE_EXPR:
1096 return do_compare (op0, op1, -1) != 0;
1097 case UNORDERED_EXPR:
1098 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1099 case ORDERED_EXPR:
1100 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1101 case UNLT_EXPR:
1102 return do_compare (op0, op1, -1) < 0;
1103 case UNLE_EXPR:
1104 return do_compare (op0, op1, -1) <= 0;
1105 case UNGT_EXPR:
1106 return do_compare (op0, op1, 1) > 0;
1107 case UNGE_EXPR:
1108 return do_compare (op0, op1, 1) >= 0;
1109 case UNEQ_EXPR:
1110 return do_compare (op0, op1, 0) == 0;
1111 case LTGT_EXPR:
1112 return do_compare (op0, op1, 0) != 0;
1114 default:
1115 gcc_unreachable ();
1119 /* Return floor log2(R). */
1122 real_exponent (const REAL_VALUE_TYPE *r)
1124 switch (r->cl)
1126 case rvc_zero:
1127 return 0;
1128 case rvc_inf:
1129 case rvc_nan:
1130 return (unsigned int)-1 >> 1;
1131 case rvc_normal:
1132 return REAL_EXP (r);
1133 default:
1134 gcc_unreachable ();
1138 /* R = OP0 * 2**EXP. */
1140 void
1141 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1143 *r = *op0;
1144 switch (r->cl)
1146 case rvc_zero:
1147 case rvc_inf:
1148 case rvc_nan:
1149 break;
1151 case rvc_normal:
1152 exp += REAL_EXP (op0);
1153 if (exp > MAX_EXP)
1154 get_inf (r, r->sign);
1155 else if (exp < -MAX_EXP)
1156 get_zero (r, r->sign);
1157 else
1158 SET_REAL_EXP (r, exp);
1159 break;
1161 default:
1162 gcc_unreachable ();
1166 /* Determine whether a floating-point value X is infinite. */
1168 bool
1169 real_isinf (const REAL_VALUE_TYPE *r)
1171 return (r->cl == rvc_inf);
1174 /* Determine whether a floating-point value X is a NaN. */
1176 bool
1177 real_isnan (const REAL_VALUE_TYPE *r)
1179 return (r->cl == rvc_nan);
1182 /* Determine whether a floating-point value X is finite. */
1184 bool
1185 real_isfinite (const REAL_VALUE_TYPE *r)
1187 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1190 /* Determine whether a floating-point value X is negative. */
1192 bool
1193 real_isneg (const REAL_VALUE_TYPE *r)
1195 return r->sign;
1198 /* Determine whether a floating-point value X is minus zero. */
1200 bool
1201 real_isnegzero (const REAL_VALUE_TYPE *r)
1203 return r->sign && r->cl == rvc_zero;
1206 /* Compare two floating-point objects for bitwise identity. */
1208 bool
1209 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1211 int i;
1213 if (a->cl != b->cl)
1214 return false;
1215 if (a->sign != b->sign)
1216 return false;
1218 switch (a->cl)
1220 case rvc_zero:
1221 case rvc_inf:
1222 return true;
1224 case rvc_normal:
1225 if (a->decimal != b->decimal)
1226 return false;
1227 if (REAL_EXP (a) != REAL_EXP (b))
1228 return false;
1229 break;
1231 case rvc_nan:
1232 if (a->signalling != b->signalling)
1233 return false;
1234 /* The significand is ignored for canonical NaNs. */
1235 if (a->canonical || b->canonical)
1236 return a->canonical == b->canonical;
1237 break;
1239 default:
1240 gcc_unreachable ();
1243 for (i = 0; i < SIGSZ; ++i)
1244 if (a->sig[i] != b->sig[i])
1245 return false;
1247 return true;
1250 /* Try to change R into its exact multiplicative inverse in machine
1251 mode MODE. Return true if successful. */
1253 bool
1254 exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r)
1256 const REAL_VALUE_TYPE *one = real_digit (1);
1257 REAL_VALUE_TYPE u;
1258 int i;
1260 if (r->cl != rvc_normal)
1261 return false;
1263 /* Check for a power of two: all significand bits zero except the MSB. */
1264 for (i = 0; i < SIGSZ-1; ++i)
1265 if (r->sig[i] != 0)
1266 return false;
1267 if (r->sig[SIGSZ-1] != SIG_MSB)
1268 return false;
1270 /* Find the inverse and truncate to the required mode. */
1271 do_divide (&u, one, r);
1272 real_convert (&u, mode, &u);
1274 /* The rounding may have overflowed. */
1275 if (u.cl != rvc_normal)
1276 return false;
1277 for (i = 0; i < SIGSZ-1; ++i)
1278 if (u.sig[i] != 0)
1279 return false;
1280 if (u.sig[SIGSZ-1] != SIG_MSB)
1281 return false;
1283 *r = u;
1284 return true;
1287 /* Return true if arithmetic on values in IMODE that were promoted
1288 from values in TMODE is equivalent to direct arithmetic on values
1289 in TMODE. */
1291 bool
1292 real_can_shorten_arithmetic (enum machine_mode imode, enum machine_mode tmode)
1294 const struct real_format *tfmt, *ifmt;
1295 tfmt = REAL_MODE_FORMAT (tmode);
1296 ifmt = REAL_MODE_FORMAT (imode);
1297 /* These conditions are conservative rather than trying to catch the
1298 exact boundary conditions; the main case to allow is IEEE float
1299 and double. */
1300 return (ifmt->b == tfmt->b
1301 && ifmt->p > 2 * tfmt->p
1302 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1303 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1304 && ifmt->emax > 2 * tfmt->emax + 2
1305 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1306 && ifmt->round_towards_zero == tfmt->round_towards_zero
1307 && (ifmt->has_sign_dependent_rounding
1308 == tfmt->has_sign_dependent_rounding)
1309 && ifmt->has_nans >= tfmt->has_nans
1310 && ifmt->has_inf >= tfmt->has_inf
1311 && ifmt->has_signed_zero >= tfmt->has_signed_zero
1312 && !MODE_COMPOSITE_P (tmode)
1313 && !MODE_COMPOSITE_P (imode));
1316 /* Render R as an integer. */
1318 HOST_WIDE_INT
1319 real_to_integer (const REAL_VALUE_TYPE *r)
1321 unsigned HOST_WIDE_INT i;
1323 switch (r->cl)
1325 case rvc_zero:
1326 underflow:
1327 return 0;
1329 case rvc_inf:
1330 case rvc_nan:
1331 overflow:
1332 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1333 if (!r->sign)
1334 i--;
1335 return i;
1337 case rvc_normal:
1338 if (r->decimal)
1339 return decimal_real_to_integer (r);
1341 if (REAL_EXP (r) <= 0)
1342 goto underflow;
1343 /* Only force overflow for unsigned overflow. Signed overflow is
1344 undefined, so it doesn't matter what we return, and some callers
1345 expect to be able to use this routine for both signed and
1346 unsigned conversions. */
1347 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1348 goto overflow;
1350 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1351 i = r->sig[SIGSZ-1];
1352 else
1354 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1355 i = r->sig[SIGSZ-1];
1356 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1357 i |= r->sig[SIGSZ-2];
1360 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1362 if (r->sign)
1363 i = -i;
1364 return i;
1366 default:
1367 gcc_unreachable ();
1371 /* Likewise, but to an integer pair, HI+LOW. */
1373 void
1374 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
1375 const REAL_VALUE_TYPE *r)
1377 REAL_VALUE_TYPE t;
1378 HOST_WIDE_INT low, high;
1379 int exp;
1381 switch (r->cl)
1383 case rvc_zero:
1384 underflow:
1385 low = high = 0;
1386 break;
1388 case rvc_inf:
1389 case rvc_nan:
1390 overflow:
1391 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1392 if (r->sign)
1393 low = 0;
1394 else
1396 high--;
1397 low = -1;
1399 break;
1401 case rvc_normal:
1402 if (r->decimal)
1404 decimal_real_to_integer2 (plow, phigh, r);
1405 return;
1408 exp = REAL_EXP (r);
1409 if (exp <= 0)
1410 goto underflow;
1411 /* Only force overflow for unsigned overflow. Signed overflow is
1412 undefined, so it doesn't matter what we return, and some callers
1413 expect to be able to use this routine for both signed and
1414 unsigned conversions. */
1415 if (exp > 2*HOST_BITS_PER_WIDE_INT)
1416 goto overflow;
1418 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1419 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1421 high = t.sig[SIGSZ-1];
1422 low = t.sig[SIGSZ-2];
1424 else
1426 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1427 high = t.sig[SIGSZ-1];
1428 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1429 high |= t.sig[SIGSZ-2];
1431 low = t.sig[SIGSZ-3];
1432 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1433 low |= t.sig[SIGSZ-4];
1436 if (r->sign)
1438 if (low == 0)
1439 high = -high;
1440 else
1441 low = -low, high = ~high;
1443 break;
1445 default:
1446 gcc_unreachable ();
1449 *plow = low;
1450 *phigh = high;
1453 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1454 of NUM / DEN. Return the quotient and place the remainder in NUM.
1455 It is expected that NUM / DEN are close enough that the quotient is
1456 small. */
1458 static unsigned long
1459 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1461 unsigned long q, msb;
1462 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1464 if (expn < expd)
1465 return 0;
1467 q = msb = 0;
1468 goto start;
1471 msb = num->sig[SIGSZ-1] & SIG_MSB;
1472 q <<= 1;
1473 lshift_significand_1 (num, num);
1474 start:
1475 if (msb || cmp_significands (num, den) >= 0)
1477 sub_significands (num, num, den, 0);
1478 q |= 1;
1481 while (--expn >= expd);
1483 SET_REAL_EXP (num, expd);
1484 normalize (num);
1486 return q;
1489 /* Render R as a decimal floating point constant. Emit DIGITS significant
1490 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1491 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1492 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1493 to a string that, when parsed back in mode MODE, yields the same value. */
1495 #define M_LOG10_2 0.30102999566398119521
1497 void
1498 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1499 size_t buf_size, size_t digits,
1500 int crop_trailing_zeros, enum machine_mode mode)
1502 const struct real_format *fmt = NULL;
1503 const REAL_VALUE_TYPE *one, *ten;
1504 REAL_VALUE_TYPE r, pten, u, v;
1505 int dec_exp, cmp_one, digit;
1506 size_t max_digits;
1507 char *p, *first, *last;
1508 bool sign;
1509 bool round_up;
1511 if (mode != VOIDmode)
1513 fmt = REAL_MODE_FORMAT (mode);
1514 gcc_assert (fmt);
1517 r = *r_orig;
1518 switch (r.cl)
1520 case rvc_zero:
1521 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1522 return;
1523 case rvc_normal:
1524 break;
1525 case rvc_inf:
1526 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1527 return;
1528 case rvc_nan:
1529 /* ??? Print the significand as well, if not canonical? */
1530 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1531 (r_orig->signalling ? 'S' : 'Q'));
1532 return;
1533 default:
1534 gcc_unreachable ();
1537 if (r.decimal)
1539 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1540 return;
1543 /* Bound the number of digits printed by the size of the representation. */
1544 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1545 if (digits == 0 || digits > max_digits)
1546 digits = max_digits;
1548 /* Estimate the decimal exponent, and compute the length of the string it
1549 will print as. Be conservative and add one to account for possible
1550 overflow or rounding error. */
1551 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1552 for (max_digits = 1; dec_exp ; max_digits++)
1553 dec_exp /= 10;
1555 /* Bound the number of digits printed by the size of the output buffer. */
1556 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1557 gcc_assert (max_digits <= buf_size);
1558 if (digits > max_digits)
1559 digits = max_digits;
1561 one = real_digit (1);
1562 ten = ten_to_ptwo (0);
1564 sign = r.sign;
1565 r.sign = 0;
1567 dec_exp = 0;
1568 pten = *one;
1570 cmp_one = do_compare (&r, one, 0);
1571 if (cmp_one > 0)
1573 int m;
1575 /* Number is greater than one. Convert significand to an integer
1576 and strip trailing decimal zeros. */
1578 u = r;
1579 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1581 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1582 m = floor_log2 (max_digits);
1584 /* Iterate over the bits of the possible powers of 10 that might
1585 be present in U and eliminate them. That is, if we find that
1586 10**2**M divides U evenly, keep the division and increase
1587 DEC_EXP by 2**M. */
1590 REAL_VALUE_TYPE t;
1592 do_divide (&t, &u, ten_to_ptwo (m));
1593 do_fix_trunc (&v, &t);
1594 if (cmp_significands (&v, &t) == 0)
1596 u = t;
1597 dec_exp += 1 << m;
1600 while (--m >= 0);
1602 /* Revert the scaling to integer that we performed earlier. */
1603 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1604 - (SIGNIFICAND_BITS - 1));
1605 r = u;
1607 /* Find power of 10. Do this by dividing out 10**2**M when
1608 this is larger than the current remainder. Fill PTEN with
1609 the power of 10 that we compute. */
1610 if (REAL_EXP (&r) > 0)
1612 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1615 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1616 if (do_compare (&u, ptentwo, 0) >= 0)
1618 do_divide (&u, &u, ptentwo);
1619 do_multiply (&pten, &pten, ptentwo);
1620 dec_exp += 1 << m;
1623 while (--m >= 0);
1625 else
1626 /* We managed to divide off enough tens in the above reduction
1627 loop that we've now got a negative exponent. Fall into the
1628 less-than-one code to compute the proper value for PTEN. */
1629 cmp_one = -1;
1631 if (cmp_one < 0)
1633 int m;
1635 /* Number is less than one. Pad significand with leading
1636 decimal zeros. */
1638 v = r;
1639 while (1)
1641 /* Stop if we'd shift bits off the bottom. */
1642 if (v.sig[0] & 7)
1643 break;
1645 do_multiply (&u, &v, ten);
1647 /* Stop if we're now >= 1. */
1648 if (REAL_EXP (&u) > 0)
1649 break;
1651 v = u;
1652 dec_exp -= 1;
1654 r = v;
1656 /* Find power of 10. Do this by multiplying in P=10**2**M when
1657 the current remainder is smaller than 1/P. Fill PTEN with the
1658 power of 10 that we compute. */
1659 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1662 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1663 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1665 if (do_compare (&v, ptenmtwo, 0) <= 0)
1667 do_multiply (&v, &v, ptentwo);
1668 do_multiply (&pten, &pten, ptentwo);
1669 dec_exp -= 1 << m;
1672 while (--m >= 0);
1674 /* Invert the positive power of 10 that we've collected so far. */
1675 do_divide (&pten, one, &pten);
1678 p = str;
1679 if (sign)
1680 *p++ = '-';
1681 first = p++;
1683 /* At this point, PTEN should contain the nearest power of 10 smaller
1684 than R, such that this division produces the first digit.
1686 Using a divide-step primitive that returns the complete integral
1687 remainder avoids the rounding error that would be produced if
1688 we were to use do_divide here and then simply multiply by 10 for
1689 each subsequent digit. */
1691 digit = rtd_divmod (&r, &pten);
1693 /* Be prepared for error in that division via underflow ... */
1694 if (digit == 0 && cmp_significand_0 (&r))
1696 /* Multiply by 10 and try again. */
1697 do_multiply (&r, &r, ten);
1698 digit = rtd_divmod (&r, &pten);
1699 dec_exp -= 1;
1700 gcc_assert (digit != 0);
1703 /* ... or overflow. */
1704 if (digit == 10)
1706 *p++ = '1';
1707 if (--digits > 0)
1708 *p++ = '0';
1709 dec_exp += 1;
1711 else
1713 gcc_assert (digit <= 10);
1714 *p++ = digit + '0';
1717 /* Generate subsequent digits. */
1718 while (--digits > 0)
1720 do_multiply (&r, &r, ten);
1721 digit = rtd_divmod (&r, &pten);
1722 *p++ = digit + '0';
1724 last = p;
1726 /* Generate one more digit with which to do rounding. */
1727 do_multiply (&r, &r, ten);
1728 digit = rtd_divmod (&r, &pten);
1730 /* Round the result. */
1731 if (fmt && fmt->round_towards_zero)
1733 /* If the format uses round towards zero when parsing the string
1734 back in, we need to always round away from zero here. */
1735 if (cmp_significand_0 (&r))
1736 digit++;
1737 round_up = digit > 0;
1739 else
1741 if (digit == 5)
1743 /* Round to nearest. If R is nonzero there are additional
1744 nonzero digits to be extracted. */
1745 if (cmp_significand_0 (&r))
1746 digit++;
1747 /* Round to even. */
1748 else if ((p[-1] - '0') & 1)
1749 digit++;
1752 round_up = digit > 5;
1755 if (round_up)
1757 while (p > first)
1759 digit = *--p;
1760 if (digit == '9')
1761 *p = '0';
1762 else
1764 *p = digit + 1;
1765 break;
1769 /* Carry out of the first digit. This means we had all 9's and
1770 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1771 if (p == first)
1773 first[1] = '1';
1774 dec_exp++;
1778 /* Insert the decimal point. */
1779 first[0] = first[1];
1780 first[1] = '.';
1782 /* If requested, drop trailing zeros. Never crop past "1.0". */
1783 if (crop_trailing_zeros)
1784 while (last > first + 3 && last[-1] == '0')
1785 last--;
1787 /* Append the exponent. */
1788 sprintf (last, "e%+d", dec_exp);
1790 #ifdef ENABLE_CHECKING
1791 /* Verify that we can read the original value back in. */
1792 if (mode != VOIDmode)
1794 real_from_string (&r, str);
1795 real_convert (&r, mode, &r);
1796 gcc_assert (real_identical (&r, r_orig));
1798 #endif
1801 /* Likewise, except always uses round-to-nearest. */
1803 void
1804 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1805 size_t digits, int crop_trailing_zeros)
1807 real_to_decimal_for_mode (str, r_orig, buf_size,
1808 digits, crop_trailing_zeros, VOIDmode);
1811 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1812 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1813 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1814 strip trailing zeros. */
1816 void
1817 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1818 size_t digits, int crop_trailing_zeros)
1820 int i, j, exp = REAL_EXP (r);
1821 char *p, *first;
1822 char exp_buf[16];
1823 size_t max_digits;
1825 switch (r->cl)
1827 case rvc_zero:
1828 exp = 0;
1829 break;
1830 case rvc_normal:
1831 break;
1832 case rvc_inf:
1833 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1834 return;
1835 case rvc_nan:
1836 /* ??? Print the significand as well, if not canonical? */
1837 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1838 (r->signalling ? 'S' : 'Q'));
1839 return;
1840 default:
1841 gcc_unreachable ();
1844 if (r->decimal)
1846 /* Hexadecimal format for decimal floats is not interesting. */
1847 strcpy (str, "N/A");
1848 return;
1851 if (digits == 0)
1852 digits = SIGNIFICAND_BITS / 4;
1854 /* Bound the number of digits printed by the size of the output buffer. */
1856 sprintf (exp_buf, "p%+d", exp);
1857 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1858 gcc_assert (max_digits <= buf_size);
1859 if (digits > max_digits)
1860 digits = max_digits;
1862 p = str;
1863 if (r->sign)
1864 *p++ = '-';
1865 *p++ = '0';
1866 *p++ = 'x';
1867 *p++ = '0';
1868 *p++ = '.';
1869 first = p;
1871 for (i = SIGSZ - 1; i >= 0; --i)
1872 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1874 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1875 if (--digits == 0)
1876 goto out;
1879 out:
1880 if (crop_trailing_zeros)
1881 while (p > first + 1 && p[-1] == '0')
1882 p--;
1884 sprintf (p, "p%+d", exp);
1887 /* Initialize R from a decimal or hexadecimal string. The string is
1888 assumed to have been syntax checked already. Return -1 if the
1889 value underflows, +1 if overflows, and 0 otherwise. */
1892 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1894 int exp = 0;
1895 bool sign = false;
1897 get_zero (r, 0);
1899 if (*str == '-')
1901 sign = true;
1902 str++;
1904 else if (*str == '+')
1905 str++;
1907 if (!strncmp (str, "QNaN", 4))
1909 get_canonical_qnan (r, sign);
1910 return 0;
1912 else if (!strncmp (str, "SNaN", 4))
1914 get_canonical_snan (r, sign);
1915 return 0;
1917 else if (!strncmp (str, "Inf", 3))
1919 get_inf (r, sign);
1920 return 0;
1923 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1925 /* Hexadecimal floating point. */
1926 int pos = SIGNIFICAND_BITS - 4, d;
1928 str += 2;
1930 while (*str == '0')
1931 str++;
1932 while (1)
1934 d = hex_value (*str);
1935 if (d == _hex_bad)
1936 break;
1937 if (pos >= 0)
1939 r->sig[pos / HOST_BITS_PER_LONG]
1940 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1941 pos -= 4;
1943 else if (d)
1944 /* Ensure correct rounding by setting last bit if there is
1945 a subsequent nonzero digit. */
1946 r->sig[0] |= 1;
1947 exp += 4;
1948 str++;
1950 if (*str == '.')
1952 str++;
1953 if (pos == SIGNIFICAND_BITS - 4)
1955 while (*str == '0')
1956 str++, exp -= 4;
1958 while (1)
1960 d = hex_value (*str);
1961 if (d == _hex_bad)
1962 break;
1963 if (pos >= 0)
1965 r->sig[pos / HOST_BITS_PER_LONG]
1966 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1967 pos -= 4;
1969 else if (d)
1970 /* Ensure correct rounding by setting last bit if there is
1971 a subsequent nonzero digit. */
1972 r->sig[0] |= 1;
1973 str++;
1977 /* If the mantissa is zero, ignore the exponent. */
1978 if (!cmp_significand_0 (r))
1979 goto is_a_zero;
1981 if (*str == 'p' || *str == 'P')
1983 bool exp_neg = false;
1985 str++;
1986 if (*str == '-')
1988 exp_neg = true;
1989 str++;
1991 else if (*str == '+')
1992 str++;
1994 d = 0;
1995 while (ISDIGIT (*str))
1997 d *= 10;
1998 d += *str - '0';
1999 if (d > MAX_EXP)
2001 /* Overflowed the exponent. */
2002 if (exp_neg)
2003 goto underflow;
2004 else
2005 goto overflow;
2007 str++;
2009 if (exp_neg)
2010 d = -d;
2012 exp += d;
2015 r->cl = rvc_normal;
2016 SET_REAL_EXP (r, exp);
2018 normalize (r);
2020 else
2022 /* Decimal floating point. */
2023 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
2024 int d;
2026 while (*str == '0')
2027 str++;
2028 while (ISDIGIT (*str))
2030 d = *str++ - '0';
2031 do_multiply (r, r, ten);
2032 if (d)
2033 do_add (r, r, real_digit (d), 0);
2035 if (*str == '.')
2037 str++;
2038 if (r->cl == rvc_zero)
2040 while (*str == '0')
2041 str++, exp--;
2043 while (ISDIGIT (*str))
2045 d = *str++ - '0';
2046 do_multiply (r, r, ten);
2047 if (d)
2048 do_add (r, r, real_digit (d), 0);
2049 exp--;
2053 /* If the mantissa is zero, ignore the exponent. */
2054 if (r->cl == rvc_zero)
2055 goto is_a_zero;
2057 if (*str == 'e' || *str == 'E')
2059 bool exp_neg = false;
2061 str++;
2062 if (*str == '-')
2064 exp_neg = true;
2065 str++;
2067 else if (*str == '+')
2068 str++;
2070 d = 0;
2071 while (ISDIGIT (*str))
2073 d *= 10;
2074 d += *str - '0';
2075 if (d > MAX_EXP)
2077 /* Overflowed the exponent. */
2078 if (exp_neg)
2079 goto underflow;
2080 else
2081 goto overflow;
2083 str++;
2085 if (exp_neg)
2086 d = -d;
2087 exp += d;
2090 if (exp)
2091 times_pten (r, exp);
2094 r->sign = sign;
2095 return 0;
2097 is_a_zero:
2098 get_zero (r, sign);
2099 return 0;
2101 underflow:
2102 get_zero (r, sign);
2103 return -1;
2105 overflow:
2106 get_inf (r, sign);
2107 return 1;
2110 /* Legacy. Similar, but return the result directly. */
2112 REAL_VALUE_TYPE
2113 real_from_string2 (const char *s, enum machine_mode mode)
2115 REAL_VALUE_TYPE r;
2117 real_from_string (&r, s);
2118 if (mode != VOIDmode)
2119 real_convert (&r, mode, &r);
2121 return r;
2124 /* Initialize R from string S and desired MODE. */
2126 void
2127 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2129 if (DECIMAL_FLOAT_MODE_P (mode))
2130 decimal_real_from_string (r, s);
2131 else
2132 real_from_string (r, s);
2134 if (mode != VOIDmode)
2135 real_convert (r, mode, r);
2138 /* Initialize R from the integer pair HIGH+LOW. */
2140 void
2141 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2142 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2143 int unsigned_p)
2145 if (low == 0 && high == 0)
2146 get_zero (r, 0);
2147 else
2149 memset (r, 0, sizeof (*r));
2150 r->cl = rvc_normal;
2151 r->sign = high < 0 && !unsigned_p;
2152 SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT);
2154 if (r->sign)
2156 high = ~high;
2157 if (low == 0)
2158 high += 1;
2159 else
2160 low = -low;
2163 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2165 r->sig[SIGSZ-1] = high;
2166 r->sig[SIGSZ-2] = low;
2168 else
2170 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2171 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2172 r->sig[SIGSZ-2] = high;
2173 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2174 r->sig[SIGSZ-4] = low;
2177 normalize (r);
2180 if (DECIMAL_FLOAT_MODE_P (mode))
2181 decimal_from_integer (r);
2182 else if (mode != VOIDmode)
2183 real_convert (r, mode, r);
2186 /* Render R, an integral value, as a floating point constant with no
2187 specified exponent. */
2189 static void
2190 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2191 size_t buf_size)
2193 int dec_exp, digit, digits;
2194 REAL_VALUE_TYPE r, pten;
2195 char *p;
2196 bool sign;
2198 r = *r_orig;
2200 if (r.cl == rvc_zero)
2202 strcpy (str, "0.");
2203 return;
2206 sign = r.sign;
2207 r.sign = 0;
2209 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2210 digits = dec_exp + 1;
2211 gcc_assert ((digits + 2) < (int)buf_size);
2213 pten = *real_digit (1);
2214 times_pten (&pten, dec_exp);
2216 p = str;
2217 if (sign)
2218 *p++ = '-';
2220 digit = rtd_divmod (&r, &pten);
2221 gcc_assert (digit >= 0 && digit <= 9);
2222 *p++ = digit + '0';
2223 while (--digits > 0)
2225 times_pten (&r, 1);
2226 digit = rtd_divmod (&r, &pten);
2227 *p++ = digit + '0';
2229 *p++ = '.';
2230 *p++ = '\0';
2233 /* Convert a real with an integral value to decimal float. */
2235 static void
2236 decimal_from_integer (REAL_VALUE_TYPE *r)
2238 char str[256];
2240 decimal_integer_string (str, r, sizeof (str) - 1);
2241 decimal_real_from_string (r, str);
2244 /* Returns 10**2**N. */
2246 static const REAL_VALUE_TYPE *
2247 ten_to_ptwo (int n)
2249 static REAL_VALUE_TYPE tens[EXP_BITS];
2251 gcc_assert (n >= 0);
2252 gcc_assert (n < EXP_BITS);
2254 if (tens[n].cl == rvc_zero)
2256 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2258 HOST_WIDE_INT t = 10;
2259 int i;
2261 for (i = 0; i < n; ++i)
2262 t *= t;
2264 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2266 else
2268 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2269 do_multiply (&tens[n], t, t);
2273 return &tens[n];
2276 /* Returns 10**(-2**N). */
2278 static const REAL_VALUE_TYPE *
2279 ten_to_mptwo (int n)
2281 static REAL_VALUE_TYPE tens[EXP_BITS];
2283 gcc_assert (n >= 0);
2284 gcc_assert (n < EXP_BITS);
2286 if (tens[n].cl == rvc_zero)
2287 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2289 return &tens[n];
2292 /* Returns N. */
2294 static const REAL_VALUE_TYPE *
2295 real_digit (int n)
2297 static REAL_VALUE_TYPE num[10];
2299 gcc_assert (n >= 0);
2300 gcc_assert (n <= 9);
2302 if (n > 0 && num[n].cl == rvc_zero)
2303 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2305 return &num[n];
2308 /* Multiply R by 10**EXP. */
2310 static void
2311 times_pten (REAL_VALUE_TYPE *r, int exp)
2313 REAL_VALUE_TYPE pten, *rr;
2314 bool negative = (exp < 0);
2315 int i;
2317 if (negative)
2319 exp = -exp;
2320 pten = *real_digit (1);
2321 rr = &pten;
2323 else
2324 rr = r;
2326 for (i = 0; exp > 0; ++i, exp >>= 1)
2327 if (exp & 1)
2328 do_multiply (rr, rr, ten_to_ptwo (i));
2330 if (negative)
2331 do_divide (r, r, &pten);
2334 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2336 const REAL_VALUE_TYPE *
2337 dconst_e_ptr (void)
2339 static REAL_VALUE_TYPE value;
2341 /* Initialize mathematical constants for constant folding builtins.
2342 These constants need to be given to at least 160 bits precision. */
2343 if (value.cl == rvc_zero)
2345 mpfr_t m;
2346 mpfr_init2 (m, SIGNIFICAND_BITS);
2347 mpfr_set_ui (m, 1, GMP_RNDN);
2348 mpfr_exp (m, m, GMP_RNDN);
2349 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2350 mpfr_clear (m);
2353 return &value;
2356 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2358 const REAL_VALUE_TYPE *
2359 dconst_third_ptr (void)
2361 static REAL_VALUE_TYPE value;
2363 /* Initialize mathematical constants for constant folding builtins.
2364 These constants need to be given to at least 160 bits precision. */
2365 if (value.cl == rvc_zero)
2367 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3));
2369 return &value;
2372 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2374 const REAL_VALUE_TYPE *
2375 dconst_sqrt2_ptr (void)
2377 static REAL_VALUE_TYPE value;
2379 /* Initialize mathematical constants for constant folding builtins.
2380 These constants need to be given to at least 160 bits precision. */
2381 if (value.cl == rvc_zero)
2383 mpfr_t m;
2384 mpfr_init2 (m, SIGNIFICAND_BITS);
2385 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2386 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2387 mpfr_clear (m);
2389 return &value;
2392 /* Fills R with +Inf. */
2394 void
2395 real_inf (REAL_VALUE_TYPE *r)
2397 get_inf (r, 0);
2400 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2401 we force a QNaN, else we force an SNaN. The string, if not empty,
2402 is parsed as a number and placed in the significand. Return true
2403 if the string was successfully parsed. */
2405 bool
2406 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2407 enum machine_mode mode)
2409 const struct real_format *fmt;
2411 fmt = REAL_MODE_FORMAT (mode);
2412 gcc_assert (fmt);
2414 if (*str == 0)
2416 if (quiet)
2417 get_canonical_qnan (r, 0);
2418 else
2419 get_canonical_snan (r, 0);
2421 else
2423 int base = 10, d;
2425 memset (r, 0, sizeof (*r));
2426 r->cl = rvc_nan;
2428 /* Parse akin to strtol into the significand of R. */
2430 while (ISSPACE (*str))
2431 str++;
2432 if (*str == '-')
2433 str++;
2434 else if (*str == '+')
2435 str++;
2436 if (*str == '0')
2438 str++;
2439 if (*str == 'x' || *str == 'X')
2441 base = 16;
2442 str++;
2444 else
2445 base = 8;
2448 while ((d = hex_value (*str)) < base)
2450 REAL_VALUE_TYPE u;
2452 switch (base)
2454 case 8:
2455 lshift_significand (r, r, 3);
2456 break;
2457 case 16:
2458 lshift_significand (r, r, 4);
2459 break;
2460 case 10:
2461 lshift_significand_1 (&u, r);
2462 lshift_significand (r, r, 3);
2463 add_significands (r, r, &u);
2464 break;
2465 default:
2466 gcc_unreachable ();
2469 get_zero (&u, 0);
2470 u.sig[0] = d;
2471 add_significands (r, r, &u);
2473 str++;
2476 /* Must have consumed the entire string for success. */
2477 if (*str != 0)
2478 return false;
2480 /* Shift the significand into place such that the bits
2481 are in the most significant bits for the format. */
2482 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2484 /* Our MSB is always unset for NaNs. */
2485 r->sig[SIGSZ-1] &= ~SIG_MSB;
2487 /* Force quiet or signalling NaN. */
2488 r->signalling = !quiet;
2491 return true;
2494 /* Fills R with the largest finite value representable in mode MODE.
2495 If SIGN is nonzero, R is set to the most negative finite value. */
2497 void
2498 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2500 const struct real_format *fmt;
2501 int np2;
2503 fmt = REAL_MODE_FORMAT (mode);
2504 gcc_assert (fmt);
2505 memset (r, 0, sizeof (*r));
2507 if (fmt->b == 10)
2508 decimal_real_maxval (r, sign, mode);
2509 else
2511 r->cl = rvc_normal;
2512 r->sign = sign;
2513 SET_REAL_EXP (r, fmt->emax);
2515 np2 = SIGNIFICAND_BITS - fmt->p;
2516 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2517 clear_significand_below (r, np2);
2519 if (fmt->pnan < fmt->p)
2520 /* This is an IBM extended double format made up of two IEEE
2521 doubles. The value of the long double is the sum of the
2522 values of the two parts. The most significant part is
2523 required to be the value of the long double rounded to the
2524 nearest double. Rounding means we need a slightly smaller
2525 value for LDBL_MAX. */
2526 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2530 /* Fills R with 2**N. */
2532 void
2533 real_2expN (REAL_VALUE_TYPE *r, int n, enum machine_mode fmode)
2535 memset (r, 0, sizeof (*r));
2537 n++;
2538 if (n > MAX_EXP)
2539 r->cl = rvc_inf;
2540 else if (n < -MAX_EXP)
2542 else
2544 r->cl = rvc_normal;
2545 SET_REAL_EXP (r, n);
2546 r->sig[SIGSZ-1] = SIG_MSB;
2548 if (DECIMAL_FLOAT_MODE_P (fmode))
2549 decimal_real_convert (r, fmode, r);
2553 static void
2554 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2556 int p2, np2, i, w;
2557 int emin2m1, emax2;
2558 bool round_up = false;
2560 if (r->decimal)
2562 if (fmt->b == 10)
2564 decimal_round_for_format (fmt, r);
2565 return;
2567 /* FIXME. We can come here via fp_easy_constant
2568 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2569 investigated whether this convert needs to be here, or
2570 something else is missing. */
2571 decimal_real_convert (r, DFmode, r);
2574 p2 = fmt->p;
2575 emin2m1 = fmt->emin - 1;
2576 emax2 = fmt->emax;
2578 np2 = SIGNIFICAND_BITS - p2;
2579 switch (r->cl)
2581 underflow:
2582 get_zero (r, r->sign);
2583 case rvc_zero:
2584 if (!fmt->has_signed_zero)
2585 r->sign = 0;
2586 return;
2588 overflow:
2589 get_inf (r, r->sign);
2590 case rvc_inf:
2591 return;
2593 case rvc_nan:
2594 clear_significand_below (r, np2);
2595 return;
2597 case rvc_normal:
2598 break;
2600 default:
2601 gcc_unreachable ();
2604 /* Check the range of the exponent. If we're out of range,
2605 either underflow or overflow. */
2606 if (REAL_EXP (r) > emax2)
2607 goto overflow;
2608 else if (REAL_EXP (r) <= emin2m1)
2610 int diff;
2612 if (!fmt->has_denorm)
2614 /* Don't underflow completely until we've had a chance to round. */
2615 if (REAL_EXP (r) < emin2m1)
2616 goto underflow;
2618 else
2620 diff = emin2m1 - REAL_EXP (r) + 1;
2621 if (diff > p2)
2622 goto underflow;
2624 /* De-normalize the significand. */
2625 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2626 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2630 if (!fmt->round_towards_zero)
2632 /* There are P2 true significand bits, followed by one guard bit,
2633 followed by one sticky bit, followed by stuff. Fold nonzero
2634 stuff into the sticky bit. */
2635 unsigned long sticky;
2636 bool guard, lsb;
2638 sticky = 0;
2639 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2640 sticky |= r->sig[i];
2641 sticky |= r->sig[w]
2642 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2644 guard = test_significand_bit (r, np2 - 1);
2645 lsb = test_significand_bit (r, np2);
2647 /* Round to even. */
2648 round_up = guard && (sticky || lsb);
2651 if (round_up)
2653 REAL_VALUE_TYPE u;
2654 get_zero (&u, 0);
2655 set_significand_bit (&u, np2);
2657 if (add_significands (r, r, &u))
2659 /* Overflow. Means the significand had been all ones, and
2660 is now all zeros. Need to increase the exponent, and
2661 possibly re-normalize it. */
2662 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2663 if (REAL_EXP (r) > emax2)
2664 goto overflow;
2665 r->sig[SIGSZ-1] = SIG_MSB;
2669 /* Catch underflow that we deferred until after rounding. */
2670 if (REAL_EXP (r) <= emin2m1)
2671 goto underflow;
2673 /* Clear out trailing garbage. */
2674 clear_significand_below (r, np2);
2677 /* Extend or truncate to a new mode. */
2679 void
2680 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2681 const REAL_VALUE_TYPE *a)
2683 const struct real_format *fmt;
2685 fmt = REAL_MODE_FORMAT (mode);
2686 gcc_assert (fmt);
2688 *r = *a;
2690 if (a->decimal || fmt->b == 10)
2691 decimal_real_convert (r, mode, a);
2693 round_for_format (fmt, r);
2695 /* round_for_format de-normalizes denormals. Undo just that part. */
2696 if (r->cl == rvc_normal)
2697 normalize (r);
2700 /* Legacy. Likewise, except return the struct directly. */
2702 REAL_VALUE_TYPE
2703 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2705 REAL_VALUE_TYPE r;
2706 real_convert (&r, mode, &a);
2707 return r;
2710 /* Return true if truncating to MODE is exact. */
2712 bool
2713 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2715 const struct real_format *fmt;
2716 REAL_VALUE_TYPE t;
2717 int emin2m1;
2719 fmt = REAL_MODE_FORMAT (mode);
2720 gcc_assert (fmt);
2722 /* Don't allow conversion to denormals. */
2723 emin2m1 = fmt->emin - 1;
2724 if (REAL_EXP (a) <= emin2m1)
2725 return false;
2727 /* After conversion to the new mode, the value must be identical. */
2728 real_convert (&t, mode, a);
2729 return real_identical (&t, a);
2732 /* Write R to the given target format. Place the words of the result
2733 in target word order in BUF. There are always 32 bits in each
2734 long, no matter the size of the host long.
2736 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2738 long
2739 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2740 const struct real_format *fmt)
2742 REAL_VALUE_TYPE r;
2743 long buf1;
2745 r = *r_orig;
2746 round_for_format (fmt, &r);
2748 if (!buf)
2749 buf = &buf1;
2750 (*fmt->encode) (fmt, buf, &r);
2752 return *buf;
2755 /* Similar, but look up the format from MODE. */
2757 long
2758 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2760 const struct real_format *fmt;
2762 fmt = REAL_MODE_FORMAT (mode);
2763 gcc_assert (fmt);
2765 return real_to_target_fmt (buf, r, fmt);
2768 /* Read R from the given target format. Read the words of the result
2769 in target word order in BUF. There are always 32 bits in each
2770 long, no matter the size of the host long. */
2772 void
2773 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2774 const struct real_format *fmt)
2776 (*fmt->decode) (fmt, r, buf);
2779 /* Similar, but look up the format from MODE. */
2781 void
2782 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2784 const struct real_format *fmt;
2786 fmt = REAL_MODE_FORMAT (mode);
2787 gcc_assert (fmt);
2789 (*fmt->decode) (fmt, r, buf);
2792 /* Return the number of bits of the largest binary value that the
2793 significand of MODE will hold. */
2794 /* ??? Legacy. Should get access to real_format directly. */
2797 significand_size (enum machine_mode mode)
2799 const struct real_format *fmt;
2801 fmt = REAL_MODE_FORMAT (mode);
2802 if (fmt == NULL)
2803 return 0;
2805 if (fmt->b == 10)
2807 /* Return the size in bits of the largest binary value that can be
2808 held by the decimal coefficient for this mode. This is one more
2809 than the number of bits required to hold the largest coefficient
2810 of this mode. */
2811 double log2_10 = 3.3219281;
2812 return fmt->p * log2_10;
2814 return fmt->p;
2817 /* Return a hash value for the given real value. */
2818 /* ??? The "unsigned int" return value is intended to be hashval_t,
2819 but I didn't want to pull hashtab.h into real.h. */
2821 unsigned int
2822 real_hash (const REAL_VALUE_TYPE *r)
2824 unsigned int h;
2825 size_t i;
2827 h = r->cl | (r->sign << 2);
2828 switch (r->cl)
2830 case rvc_zero:
2831 case rvc_inf:
2832 return h;
2834 case rvc_normal:
2835 h |= REAL_EXP (r) << 3;
2836 break;
2838 case rvc_nan:
2839 if (r->signalling)
2840 h ^= (unsigned int)-1;
2841 if (r->canonical)
2842 return h;
2843 break;
2845 default:
2846 gcc_unreachable ();
2849 if (sizeof(unsigned long) > sizeof(unsigned int))
2850 for (i = 0; i < SIGSZ; ++i)
2852 unsigned long s = r->sig[i];
2853 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2855 else
2856 for (i = 0; i < SIGSZ; ++i)
2857 h ^= r->sig[i];
2859 return h;
2862 /* IEEE single-precision format. */
2864 static void encode_ieee_single (const struct real_format *fmt,
2865 long *, const REAL_VALUE_TYPE *);
2866 static void decode_ieee_single (const struct real_format *,
2867 REAL_VALUE_TYPE *, const long *);
2869 static void
2870 encode_ieee_single (const struct real_format *fmt, long *buf,
2871 const REAL_VALUE_TYPE *r)
2873 unsigned long image, sig, exp;
2874 unsigned long sign = r->sign;
2875 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2877 image = sign << 31;
2878 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2880 switch (r->cl)
2882 case rvc_zero:
2883 break;
2885 case rvc_inf:
2886 if (fmt->has_inf)
2887 image |= 255 << 23;
2888 else
2889 image |= 0x7fffffff;
2890 break;
2892 case rvc_nan:
2893 if (fmt->has_nans)
2895 if (r->canonical)
2896 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2897 if (r->signalling == fmt->qnan_msb_set)
2898 sig &= ~(1 << 22);
2899 else
2900 sig |= 1 << 22;
2901 if (sig == 0)
2902 sig = 1 << 21;
2904 image |= 255 << 23;
2905 image |= sig;
2907 else
2908 image |= 0x7fffffff;
2909 break;
2911 case rvc_normal:
2912 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2913 whereas the intermediate representation is 0.F x 2**exp.
2914 Which means we're off by one. */
2915 if (denormal)
2916 exp = 0;
2917 else
2918 exp = REAL_EXP (r) + 127 - 1;
2919 image |= exp << 23;
2920 image |= sig;
2921 break;
2923 default:
2924 gcc_unreachable ();
2927 buf[0] = image;
2930 static void
2931 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2932 const long *buf)
2934 unsigned long image = buf[0] & 0xffffffff;
2935 bool sign = (image >> 31) & 1;
2936 int exp = (image >> 23) & 0xff;
2938 memset (r, 0, sizeof (*r));
2939 image <<= HOST_BITS_PER_LONG - 24;
2940 image &= ~SIG_MSB;
2942 if (exp == 0)
2944 if (image && fmt->has_denorm)
2946 r->cl = rvc_normal;
2947 r->sign = sign;
2948 SET_REAL_EXP (r, -126);
2949 r->sig[SIGSZ-1] = image << 1;
2950 normalize (r);
2952 else if (fmt->has_signed_zero)
2953 r->sign = sign;
2955 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2957 if (image)
2959 r->cl = rvc_nan;
2960 r->sign = sign;
2961 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2962 ^ fmt->qnan_msb_set);
2963 r->sig[SIGSZ-1] = image;
2965 else
2967 r->cl = rvc_inf;
2968 r->sign = sign;
2971 else
2973 r->cl = rvc_normal;
2974 r->sign = sign;
2975 SET_REAL_EXP (r, exp - 127 + 1);
2976 r->sig[SIGSZ-1] = image | SIG_MSB;
2980 const struct real_format ieee_single_format =
2982 encode_ieee_single,
2983 decode_ieee_single,
2987 -125,
2988 128,
2991 false,
2992 true,
2993 true,
2994 true,
2995 true,
2996 true,
2997 true,
2998 false
3001 const struct real_format mips_single_format =
3003 encode_ieee_single,
3004 decode_ieee_single,
3008 -125,
3009 128,
3012 false,
3013 true,
3014 true,
3015 true,
3016 true,
3017 true,
3018 false,
3019 true
3022 const struct real_format motorola_single_format =
3024 encode_ieee_single,
3025 decode_ieee_single,
3029 -125,
3030 128,
3033 false,
3034 true,
3035 true,
3036 true,
3037 true,
3038 true,
3039 true,
3040 true
3043 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3044 single precision with the following differences:
3045 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3046 are generated.
3047 - NaNs are not supported.
3048 - The range of non-zero numbers in binary is
3049 (001)[1.]000...000 to (255)[1.]111...111.
3050 - Denormals can be represented, but are treated as +0.0 when
3051 used as an operand and are never generated as a result.
3052 - -0.0 can be represented, but a zero result is always +0.0.
3053 - the only supported rounding mode is trunction (towards zero). */
3054 const struct real_format spu_single_format =
3056 encode_ieee_single,
3057 decode_ieee_single,
3061 -125,
3062 129,
3065 true,
3066 false,
3067 false,
3068 false,
3069 true,
3070 true,
3071 false,
3072 false
3075 /* IEEE double-precision format. */
3077 static void encode_ieee_double (const struct real_format *fmt,
3078 long *, const REAL_VALUE_TYPE *);
3079 static void decode_ieee_double (const struct real_format *,
3080 REAL_VALUE_TYPE *, const long *);
3082 static void
3083 encode_ieee_double (const struct real_format *fmt, long *buf,
3084 const REAL_VALUE_TYPE *r)
3086 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3087 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3089 image_hi = r->sign << 31;
3090 image_lo = 0;
3092 if (HOST_BITS_PER_LONG == 64)
3094 sig_hi = r->sig[SIGSZ-1];
3095 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3096 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3098 else
3100 sig_hi = r->sig[SIGSZ-1];
3101 sig_lo = r->sig[SIGSZ-2];
3102 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3103 sig_hi = (sig_hi >> 11) & 0xfffff;
3106 switch (r->cl)
3108 case rvc_zero:
3109 break;
3111 case rvc_inf:
3112 if (fmt->has_inf)
3113 image_hi |= 2047 << 20;
3114 else
3116 image_hi |= 0x7fffffff;
3117 image_lo = 0xffffffff;
3119 break;
3121 case rvc_nan:
3122 if (fmt->has_nans)
3124 if (r->canonical)
3126 if (fmt->canonical_nan_lsbs_set)
3128 sig_hi = (1 << 19) - 1;
3129 sig_lo = 0xffffffff;
3131 else
3133 sig_hi = 0;
3134 sig_lo = 0;
3137 if (r->signalling == fmt->qnan_msb_set)
3138 sig_hi &= ~(1 << 19);
3139 else
3140 sig_hi |= 1 << 19;
3141 if (sig_hi == 0 && sig_lo == 0)
3142 sig_hi = 1 << 18;
3144 image_hi |= 2047 << 20;
3145 image_hi |= sig_hi;
3146 image_lo = sig_lo;
3148 else
3150 image_hi |= 0x7fffffff;
3151 image_lo = 0xffffffff;
3153 break;
3155 case rvc_normal:
3156 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3157 whereas the intermediate representation is 0.F x 2**exp.
3158 Which means we're off by one. */
3159 if (denormal)
3160 exp = 0;
3161 else
3162 exp = REAL_EXP (r) + 1023 - 1;
3163 image_hi |= exp << 20;
3164 image_hi |= sig_hi;
3165 image_lo = sig_lo;
3166 break;
3168 default:
3169 gcc_unreachable ();
3172 if (FLOAT_WORDS_BIG_ENDIAN)
3173 buf[0] = image_hi, buf[1] = image_lo;
3174 else
3175 buf[0] = image_lo, buf[1] = image_hi;
3178 static void
3179 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3180 const long *buf)
3182 unsigned long image_hi, image_lo;
3183 bool sign;
3184 int exp;
3186 if (FLOAT_WORDS_BIG_ENDIAN)
3187 image_hi = buf[0], image_lo = buf[1];
3188 else
3189 image_lo = buf[0], image_hi = buf[1];
3190 image_lo &= 0xffffffff;
3191 image_hi &= 0xffffffff;
3193 sign = (image_hi >> 31) & 1;
3194 exp = (image_hi >> 20) & 0x7ff;
3196 memset (r, 0, sizeof (*r));
3198 image_hi <<= 32 - 21;
3199 image_hi |= image_lo >> 21;
3200 image_hi &= 0x7fffffff;
3201 image_lo <<= 32 - 21;
3203 if (exp == 0)
3205 if ((image_hi || image_lo) && fmt->has_denorm)
3207 r->cl = rvc_normal;
3208 r->sign = sign;
3209 SET_REAL_EXP (r, -1022);
3210 if (HOST_BITS_PER_LONG == 32)
3212 image_hi = (image_hi << 1) | (image_lo >> 31);
3213 image_lo <<= 1;
3214 r->sig[SIGSZ-1] = image_hi;
3215 r->sig[SIGSZ-2] = image_lo;
3217 else
3219 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3220 r->sig[SIGSZ-1] = image_hi;
3222 normalize (r);
3224 else if (fmt->has_signed_zero)
3225 r->sign = sign;
3227 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3229 if (image_hi || image_lo)
3231 r->cl = rvc_nan;
3232 r->sign = sign;
3233 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3234 if (HOST_BITS_PER_LONG == 32)
3236 r->sig[SIGSZ-1] = image_hi;
3237 r->sig[SIGSZ-2] = image_lo;
3239 else
3240 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3242 else
3244 r->cl = rvc_inf;
3245 r->sign = sign;
3248 else
3250 r->cl = rvc_normal;
3251 r->sign = sign;
3252 SET_REAL_EXP (r, exp - 1023 + 1);
3253 if (HOST_BITS_PER_LONG == 32)
3255 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3256 r->sig[SIGSZ-2] = image_lo;
3258 else
3259 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3263 const struct real_format ieee_double_format =
3265 encode_ieee_double,
3266 decode_ieee_double,
3270 -1021,
3271 1024,
3274 false,
3275 true,
3276 true,
3277 true,
3278 true,
3279 true,
3280 true,
3281 false
3284 const struct real_format mips_double_format =
3286 encode_ieee_double,
3287 decode_ieee_double,
3291 -1021,
3292 1024,
3295 false,
3296 true,
3297 true,
3298 true,
3299 true,
3300 true,
3301 false,
3302 true
3305 const struct real_format motorola_double_format =
3307 encode_ieee_double,
3308 decode_ieee_double,
3312 -1021,
3313 1024,
3316 false,
3317 true,
3318 true,
3319 true,
3320 true,
3321 true,
3322 true,
3323 true
3326 /* IEEE extended real format. This comes in three flavors: Intel's as
3327 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3328 12- and 16-byte images may be big- or little endian; Motorola's is
3329 always big endian. */
3331 /* Helper subroutine which converts from the internal format to the
3332 12-byte little-endian Intel format. Functions below adjust this
3333 for the other possible formats. */
3334 static void
3335 encode_ieee_extended (const struct real_format *fmt, long *buf,
3336 const REAL_VALUE_TYPE *r)
3338 unsigned long image_hi, sig_hi, sig_lo;
3339 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3341 image_hi = r->sign << 15;
3342 sig_hi = sig_lo = 0;
3344 switch (r->cl)
3346 case rvc_zero:
3347 break;
3349 case rvc_inf:
3350 if (fmt->has_inf)
3352 image_hi |= 32767;
3354 /* Intel requires the explicit integer bit to be set, otherwise
3355 it considers the value a "pseudo-infinity". Motorola docs
3356 say it doesn't care. */
3357 sig_hi = 0x80000000;
3359 else
3361 image_hi |= 32767;
3362 sig_lo = sig_hi = 0xffffffff;
3364 break;
3366 case rvc_nan:
3367 if (fmt->has_nans)
3369 image_hi |= 32767;
3370 if (r->canonical)
3372 if (fmt->canonical_nan_lsbs_set)
3374 sig_hi = (1 << 30) - 1;
3375 sig_lo = 0xffffffff;
3378 else if (HOST_BITS_PER_LONG == 32)
3380 sig_hi = r->sig[SIGSZ-1];
3381 sig_lo = r->sig[SIGSZ-2];
3383 else
3385 sig_lo = r->sig[SIGSZ-1];
3386 sig_hi = sig_lo >> 31 >> 1;
3387 sig_lo &= 0xffffffff;
3389 if (r->signalling == fmt->qnan_msb_set)
3390 sig_hi &= ~(1 << 30);
3391 else
3392 sig_hi |= 1 << 30;
3393 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3394 sig_hi = 1 << 29;
3396 /* Intel requires the explicit integer bit to be set, otherwise
3397 it considers the value a "pseudo-nan". Motorola docs say it
3398 doesn't care. */
3399 sig_hi |= 0x80000000;
3401 else
3403 image_hi |= 32767;
3404 sig_lo = sig_hi = 0xffffffff;
3406 break;
3408 case rvc_normal:
3410 int exp = REAL_EXP (r);
3412 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3413 whereas the intermediate representation is 0.F x 2**exp.
3414 Which means we're off by one.
3416 Except for Motorola, which consider exp=0 and explicit
3417 integer bit set to continue to be normalized. In theory
3418 this discrepancy has been taken care of by the difference
3419 in fmt->emin in round_for_format. */
3421 if (denormal)
3422 exp = 0;
3423 else
3425 exp += 16383 - 1;
3426 gcc_assert (exp >= 0);
3428 image_hi |= exp;
3430 if (HOST_BITS_PER_LONG == 32)
3432 sig_hi = r->sig[SIGSZ-1];
3433 sig_lo = r->sig[SIGSZ-2];
3435 else
3437 sig_lo = r->sig[SIGSZ-1];
3438 sig_hi = sig_lo >> 31 >> 1;
3439 sig_lo &= 0xffffffff;
3442 break;
3444 default:
3445 gcc_unreachable ();
3448 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3451 /* Convert from the internal format to the 12-byte Motorola format
3452 for an IEEE extended real. */
3453 static void
3454 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3455 const REAL_VALUE_TYPE *r)
3457 long intermed[3];
3458 encode_ieee_extended (fmt, intermed, r);
3460 /* Motorola chips are assumed always to be big-endian. Also, the
3461 padding in a Motorola extended real goes between the exponent and
3462 the mantissa. At this point the mantissa is entirely within
3463 elements 0 and 1 of intermed, and the exponent entirely within
3464 element 2, so all we have to do is swap the order around, and
3465 shift element 2 left 16 bits. */
3466 buf[0] = intermed[2] << 16;
3467 buf[1] = intermed[1];
3468 buf[2] = intermed[0];
3471 /* Convert from the internal format to the 12-byte Intel format for
3472 an IEEE extended real. */
3473 static void
3474 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3475 const REAL_VALUE_TYPE *r)
3477 if (FLOAT_WORDS_BIG_ENDIAN)
3479 /* All the padding in an Intel-format extended real goes at the high
3480 end, which in this case is after the mantissa, not the exponent.
3481 Therefore we must shift everything down 16 bits. */
3482 long intermed[3];
3483 encode_ieee_extended (fmt, intermed, r);
3484 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3485 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3486 buf[2] = (intermed[0] << 16);
3488 else
3489 /* encode_ieee_extended produces what we want directly. */
3490 encode_ieee_extended (fmt, buf, r);
3493 /* Convert from the internal format to the 16-byte Intel format for
3494 an IEEE extended real. */
3495 static void
3496 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3497 const REAL_VALUE_TYPE *r)
3499 /* All the padding in an Intel-format extended real goes at the high end. */
3500 encode_ieee_extended_intel_96 (fmt, buf, r);
3501 buf[3] = 0;
3504 /* As above, we have a helper function which converts from 12-byte
3505 little-endian Intel format to internal format. Functions below
3506 adjust for the other possible formats. */
3507 static void
3508 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3509 const long *buf)
3511 unsigned long image_hi, sig_hi, sig_lo;
3512 bool sign;
3513 int exp;
3515 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3516 sig_lo &= 0xffffffff;
3517 sig_hi &= 0xffffffff;
3518 image_hi &= 0xffffffff;
3520 sign = (image_hi >> 15) & 1;
3521 exp = image_hi & 0x7fff;
3523 memset (r, 0, sizeof (*r));
3525 if (exp == 0)
3527 if ((sig_hi || sig_lo) && fmt->has_denorm)
3529 r->cl = rvc_normal;
3530 r->sign = sign;
3532 /* When the IEEE format contains a hidden bit, we know that
3533 it's zero at this point, and so shift up the significand
3534 and decrease the exponent to match. In this case, Motorola
3535 defines the explicit integer bit to be valid, so we don't
3536 know whether the msb is set or not. */
3537 SET_REAL_EXP (r, fmt->emin);
3538 if (HOST_BITS_PER_LONG == 32)
3540 r->sig[SIGSZ-1] = sig_hi;
3541 r->sig[SIGSZ-2] = sig_lo;
3543 else
3544 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3546 normalize (r);
3548 else if (fmt->has_signed_zero)
3549 r->sign = sign;
3551 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3553 /* See above re "pseudo-infinities" and "pseudo-nans".
3554 Short summary is that the MSB will likely always be
3555 set, and that we don't care about it. */
3556 sig_hi &= 0x7fffffff;
3558 if (sig_hi || sig_lo)
3560 r->cl = rvc_nan;
3561 r->sign = sign;
3562 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3563 if (HOST_BITS_PER_LONG == 32)
3565 r->sig[SIGSZ-1] = sig_hi;
3566 r->sig[SIGSZ-2] = sig_lo;
3568 else
3569 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3571 else
3573 r->cl = rvc_inf;
3574 r->sign = sign;
3577 else
3579 r->cl = rvc_normal;
3580 r->sign = sign;
3581 SET_REAL_EXP (r, exp - 16383 + 1);
3582 if (HOST_BITS_PER_LONG == 32)
3584 r->sig[SIGSZ-1] = sig_hi;
3585 r->sig[SIGSZ-2] = sig_lo;
3587 else
3588 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3592 /* Convert from the internal format to the 12-byte Motorola format
3593 for an IEEE extended real. */
3594 static void
3595 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3596 const long *buf)
3598 long intermed[3];
3600 /* Motorola chips are assumed always to be big-endian. Also, the
3601 padding in a Motorola extended real goes between the exponent and
3602 the mantissa; remove it. */
3603 intermed[0] = buf[2];
3604 intermed[1] = buf[1];
3605 intermed[2] = (unsigned long)buf[0] >> 16;
3607 decode_ieee_extended (fmt, r, intermed);
3610 /* Convert from the internal format to the 12-byte Intel format for
3611 an IEEE extended real. */
3612 static void
3613 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3614 const long *buf)
3616 if (FLOAT_WORDS_BIG_ENDIAN)
3618 /* All the padding in an Intel-format extended real goes at the high
3619 end, which in this case is after the mantissa, not the exponent.
3620 Therefore we must shift everything up 16 bits. */
3621 long intermed[3];
3623 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3624 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3625 intermed[2] = ((unsigned long)buf[0] >> 16);
3627 decode_ieee_extended (fmt, r, intermed);
3629 else
3630 /* decode_ieee_extended produces what we want directly. */
3631 decode_ieee_extended (fmt, r, buf);
3634 /* Convert from the internal format to the 16-byte Intel format for
3635 an IEEE extended real. */
3636 static void
3637 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3638 const long *buf)
3640 /* All the padding in an Intel-format extended real goes at the high end. */
3641 decode_ieee_extended_intel_96 (fmt, r, buf);
3644 const struct real_format ieee_extended_motorola_format =
3646 encode_ieee_extended_motorola,
3647 decode_ieee_extended_motorola,
3651 -16382,
3652 16384,
3655 false,
3656 true,
3657 true,
3658 true,
3659 true,
3660 true,
3661 true,
3662 true
3665 const struct real_format ieee_extended_intel_96_format =
3667 encode_ieee_extended_intel_96,
3668 decode_ieee_extended_intel_96,
3672 -16381,
3673 16384,
3676 false,
3677 true,
3678 true,
3679 true,
3680 true,
3681 true,
3682 true,
3683 false
3686 const struct real_format ieee_extended_intel_128_format =
3688 encode_ieee_extended_intel_128,
3689 decode_ieee_extended_intel_128,
3693 -16381,
3694 16384,
3697 false,
3698 true,
3699 true,
3700 true,
3701 true,
3702 true,
3703 true,
3704 false
3707 /* The following caters to i386 systems that set the rounding precision
3708 to 53 bits instead of 64, e.g. FreeBSD. */
3709 const struct real_format ieee_extended_intel_96_round_53_format =
3711 encode_ieee_extended_intel_96,
3712 decode_ieee_extended_intel_96,
3716 -16381,
3717 16384,
3720 false,
3721 true,
3722 true,
3723 true,
3724 true,
3725 true,
3726 true,
3727 false
3730 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3731 numbers whose sum is equal to the extended precision value. The number
3732 with greater magnitude is first. This format has the same magnitude
3733 range as an IEEE double precision value, but effectively 106 bits of
3734 significand precision. Infinity and NaN are represented by their IEEE
3735 double precision value stored in the first number, the second number is
3736 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3738 static void encode_ibm_extended (const struct real_format *fmt,
3739 long *, const REAL_VALUE_TYPE *);
3740 static void decode_ibm_extended (const struct real_format *,
3741 REAL_VALUE_TYPE *, const long *);
3743 static void
3744 encode_ibm_extended (const struct real_format *fmt, long *buf,
3745 const REAL_VALUE_TYPE *r)
3747 REAL_VALUE_TYPE u, normr, v;
3748 const struct real_format *base_fmt;
3750 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3752 /* Renormalize R before doing any arithmetic on it. */
3753 normr = *r;
3754 if (normr.cl == rvc_normal)
3755 normalize (&normr);
3757 /* u = IEEE double precision portion of significand. */
3758 u = normr;
3759 round_for_format (base_fmt, &u);
3760 encode_ieee_double (base_fmt, &buf[0], &u);
3762 if (u.cl == rvc_normal)
3764 do_add (&v, &normr, &u, 1);
3765 /* Call round_for_format since we might need to denormalize. */
3766 round_for_format (base_fmt, &v);
3767 encode_ieee_double (base_fmt, &buf[2], &v);
3769 else
3771 /* Inf, NaN, 0 are all representable as doubles, so the
3772 least-significant part can be 0.0. */
3773 buf[2] = 0;
3774 buf[3] = 0;
3778 static void
3779 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3780 const long *buf)
3782 REAL_VALUE_TYPE u, v;
3783 const struct real_format *base_fmt;
3785 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3786 decode_ieee_double (base_fmt, &u, &buf[0]);
3788 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3790 decode_ieee_double (base_fmt, &v, &buf[2]);
3791 do_add (r, &u, &v, 0);
3793 else
3794 *r = u;
3797 const struct real_format ibm_extended_format =
3799 encode_ibm_extended,
3800 decode_ibm_extended,
3802 53 + 53,
3804 -1021 + 53,
3805 1024,
3806 127,
3808 false,
3809 true,
3810 true,
3811 true,
3812 true,
3813 true,
3814 true,
3815 false
3818 const struct real_format mips_extended_format =
3820 encode_ibm_extended,
3821 decode_ibm_extended,
3823 53 + 53,
3825 -1021 + 53,
3826 1024,
3827 127,
3829 false,
3830 true,
3831 true,
3832 true,
3833 true,
3834 true,
3835 false,
3836 true
3840 /* IEEE quad precision format. */
3842 static void encode_ieee_quad (const struct real_format *fmt,
3843 long *, const REAL_VALUE_TYPE *);
3844 static void decode_ieee_quad (const struct real_format *,
3845 REAL_VALUE_TYPE *, const long *);
3847 static void
3848 encode_ieee_quad (const struct real_format *fmt, long *buf,
3849 const REAL_VALUE_TYPE *r)
3851 unsigned long image3, image2, image1, image0, exp;
3852 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3853 REAL_VALUE_TYPE u;
3855 image3 = r->sign << 31;
3856 image2 = 0;
3857 image1 = 0;
3858 image0 = 0;
3860 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3862 switch (r->cl)
3864 case rvc_zero:
3865 break;
3867 case rvc_inf:
3868 if (fmt->has_inf)
3869 image3 |= 32767 << 16;
3870 else
3872 image3 |= 0x7fffffff;
3873 image2 = 0xffffffff;
3874 image1 = 0xffffffff;
3875 image0 = 0xffffffff;
3877 break;
3879 case rvc_nan:
3880 if (fmt->has_nans)
3882 image3 |= 32767 << 16;
3884 if (r->canonical)
3886 if (fmt->canonical_nan_lsbs_set)
3888 image3 |= 0x7fff;
3889 image2 = image1 = image0 = 0xffffffff;
3892 else if (HOST_BITS_PER_LONG == 32)
3894 image0 = u.sig[0];
3895 image1 = u.sig[1];
3896 image2 = u.sig[2];
3897 image3 |= u.sig[3] & 0xffff;
3899 else
3901 image0 = u.sig[0];
3902 image1 = image0 >> 31 >> 1;
3903 image2 = u.sig[1];
3904 image3 |= (image2 >> 31 >> 1) & 0xffff;
3905 image0 &= 0xffffffff;
3906 image2 &= 0xffffffff;
3908 if (r->signalling == fmt->qnan_msb_set)
3909 image3 &= ~0x8000;
3910 else
3911 image3 |= 0x8000;
3912 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3913 image3 |= 0x4000;
3915 else
3917 image3 |= 0x7fffffff;
3918 image2 = 0xffffffff;
3919 image1 = 0xffffffff;
3920 image0 = 0xffffffff;
3922 break;
3924 case rvc_normal:
3925 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3926 whereas the intermediate representation is 0.F x 2**exp.
3927 Which means we're off by one. */
3928 if (denormal)
3929 exp = 0;
3930 else
3931 exp = REAL_EXP (r) + 16383 - 1;
3932 image3 |= exp << 16;
3934 if (HOST_BITS_PER_LONG == 32)
3936 image0 = u.sig[0];
3937 image1 = u.sig[1];
3938 image2 = u.sig[2];
3939 image3 |= u.sig[3] & 0xffff;
3941 else
3943 image0 = u.sig[0];
3944 image1 = image0 >> 31 >> 1;
3945 image2 = u.sig[1];
3946 image3 |= (image2 >> 31 >> 1) & 0xffff;
3947 image0 &= 0xffffffff;
3948 image2 &= 0xffffffff;
3950 break;
3952 default:
3953 gcc_unreachable ();
3956 if (FLOAT_WORDS_BIG_ENDIAN)
3958 buf[0] = image3;
3959 buf[1] = image2;
3960 buf[2] = image1;
3961 buf[3] = image0;
3963 else
3965 buf[0] = image0;
3966 buf[1] = image1;
3967 buf[2] = image2;
3968 buf[3] = image3;
3972 static void
3973 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3974 const long *buf)
3976 unsigned long image3, image2, image1, image0;
3977 bool sign;
3978 int exp;
3980 if (FLOAT_WORDS_BIG_ENDIAN)
3982 image3 = buf[0];
3983 image2 = buf[1];
3984 image1 = buf[2];
3985 image0 = buf[3];
3987 else
3989 image0 = buf[0];
3990 image1 = buf[1];
3991 image2 = buf[2];
3992 image3 = buf[3];
3994 image0 &= 0xffffffff;
3995 image1 &= 0xffffffff;
3996 image2 &= 0xffffffff;
3998 sign = (image3 >> 31) & 1;
3999 exp = (image3 >> 16) & 0x7fff;
4000 image3 &= 0xffff;
4002 memset (r, 0, sizeof (*r));
4004 if (exp == 0)
4006 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4008 r->cl = rvc_normal;
4009 r->sign = sign;
4011 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4012 if (HOST_BITS_PER_LONG == 32)
4014 r->sig[0] = image0;
4015 r->sig[1] = image1;
4016 r->sig[2] = image2;
4017 r->sig[3] = image3;
4019 else
4021 r->sig[0] = (image1 << 31 << 1) | image0;
4022 r->sig[1] = (image3 << 31 << 1) | image2;
4025 normalize (r);
4027 else if (fmt->has_signed_zero)
4028 r->sign = sign;
4030 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4032 if (image3 | image2 | image1 | image0)
4034 r->cl = rvc_nan;
4035 r->sign = sign;
4036 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4038 if (HOST_BITS_PER_LONG == 32)
4040 r->sig[0] = image0;
4041 r->sig[1] = image1;
4042 r->sig[2] = image2;
4043 r->sig[3] = image3;
4045 else
4047 r->sig[0] = (image1 << 31 << 1) | image0;
4048 r->sig[1] = (image3 << 31 << 1) | image2;
4050 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4052 else
4054 r->cl = rvc_inf;
4055 r->sign = sign;
4058 else
4060 r->cl = rvc_normal;
4061 r->sign = sign;
4062 SET_REAL_EXP (r, exp - 16383 + 1);
4064 if (HOST_BITS_PER_LONG == 32)
4066 r->sig[0] = image0;
4067 r->sig[1] = image1;
4068 r->sig[2] = image2;
4069 r->sig[3] = image3;
4071 else
4073 r->sig[0] = (image1 << 31 << 1) | image0;
4074 r->sig[1] = (image3 << 31 << 1) | image2;
4076 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4077 r->sig[SIGSZ-1] |= SIG_MSB;
4081 const struct real_format ieee_quad_format =
4083 encode_ieee_quad,
4084 decode_ieee_quad,
4086 113,
4087 113,
4088 -16381,
4089 16384,
4090 127,
4091 127,
4092 false,
4093 true,
4094 true,
4095 true,
4096 true,
4097 true,
4098 true,
4099 false
4102 const struct real_format mips_quad_format =
4104 encode_ieee_quad,
4105 decode_ieee_quad,
4107 113,
4108 113,
4109 -16381,
4110 16384,
4111 127,
4112 127,
4113 false,
4114 true,
4115 true,
4116 true,
4117 true,
4118 true,
4119 false,
4120 true
4123 /* Descriptions of VAX floating point formats can be found beginning at
4125 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4127 The thing to remember is that they're almost IEEE, except for word
4128 order, exponent bias, and the lack of infinities, nans, and denormals.
4130 We don't implement the H_floating format here, simply because neither
4131 the VAX or Alpha ports use it. */
4133 static void encode_vax_f (const struct real_format *fmt,
4134 long *, const REAL_VALUE_TYPE *);
4135 static void decode_vax_f (const struct real_format *,
4136 REAL_VALUE_TYPE *, const long *);
4137 static void encode_vax_d (const struct real_format *fmt,
4138 long *, const REAL_VALUE_TYPE *);
4139 static void decode_vax_d (const struct real_format *,
4140 REAL_VALUE_TYPE *, const long *);
4141 static void encode_vax_g (const struct real_format *fmt,
4142 long *, const REAL_VALUE_TYPE *);
4143 static void decode_vax_g (const struct real_format *,
4144 REAL_VALUE_TYPE *, const long *);
4146 static void
4147 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4148 const REAL_VALUE_TYPE *r)
4150 unsigned long sign, exp, sig, image;
4152 sign = r->sign << 15;
4154 switch (r->cl)
4156 case rvc_zero:
4157 image = 0;
4158 break;
4160 case rvc_inf:
4161 case rvc_nan:
4162 image = 0xffff7fff | sign;
4163 break;
4165 case rvc_normal:
4166 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4167 exp = REAL_EXP (r) + 128;
4169 image = (sig << 16) & 0xffff0000;
4170 image |= sign;
4171 image |= exp << 7;
4172 image |= sig >> 16;
4173 break;
4175 default:
4176 gcc_unreachable ();
4179 buf[0] = image;
4182 static void
4183 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4184 REAL_VALUE_TYPE *r, const long *buf)
4186 unsigned long image = buf[0] & 0xffffffff;
4187 int exp = (image >> 7) & 0xff;
4189 memset (r, 0, sizeof (*r));
4191 if (exp != 0)
4193 r->cl = rvc_normal;
4194 r->sign = (image >> 15) & 1;
4195 SET_REAL_EXP (r, exp - 128);
4197 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4198 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4202 static void
4203 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4204 const REAL_VALUE_TYPE *r)
4206 unsigned long image0, image1, sign = r->sign << 15;
4208 switch (r->cl)
4210 case rvc_zero:
4211 image0 = image1 = 0;
4212 break;
4214 case rvc_inf:
4215 case rvc_nan:
4216 image0 = 0xffff7fff | sign;
4217 image1 = 0xffffffff;
4218 break;
4220 case rvc_normal:
4221 /* Extract the significand into straight hi:lo. */
4222 if (HOST_BITS_PER_LONG == 64)
4224 image0 = r->sig[SIGSZ-1];
4225 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4226 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4228 else
4230 image0 = r->sig[SIGSZ-1];
4231 image1 = r->sig[SIGSZ-2];
4232 image1 = (image0 << 24) | (image1 >> 8);
4233 image0 = (image0 >> 8) & 0xffffff;
4236 /* Rearrange the half-words of the significand to match the
4237 external format. */
4238 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4239 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4241 /* Add the sign and exponent. */
4242 image0 |= sign;
4243 image0 |= (REAL_EXP (r) + 128) << 7;
4244 break;
4246 default:
4247 gcc_unreachable ();
4250 if (FLOAT_WORDS_BIG_ENDIAN)
4251 buf[0] = image1, buf[1] = image0;
4252 else
4253 buf[0] = image0, buf[1] = image1;
4256 static void
4257 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4258 REAL_VALUE_TYPE *r, const long *buf)
4260 unsigned long image0, image1;
4261 int exp;
4263 if (FLOAT_WORDS_BIG_ENDIAN)
4264 image1 = buf[0], image0 = buf[1];
4265 else
4266 image0 = buf[0], image1 = buf[1];
4267 image0 &= 0xffffffff;
4268 image1 &= 0xffffffff;
4270 exp = (image0 >> 7) & 0xff;
4272 memset (r, 0, sizeof (*r));
4274 if (exp != 0)
4276 r->cl = rvc_normal;
4277 r->sign = (image0 >> 15) & 1;
4278 SET_REAL_EXP (r, exp - 128);
4280 /* Rearrange the half-words of the external format into
4281 proper ascending order. */
4282 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4283 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4285 if (HOST_BITS_PER_LONG == 64)
4287 image0 = (image0 << 31 << 1) | image1;
4288 image0 <<= 64 - 56;
4289 image0 |= SIG_MSB;
4290 r->sig[SIGSZ-1] = image0;
4292 else
4294 r->sig[SIGSZ-1] = image0;
4295 r->sig[SIGSZ-2] = image1;
4296 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4297 r->sig[SIGSZ-1] |= SIG_MSB;
4302 static void
4303 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4304 const REAL_VALUE_TYPE *r)
4306 unsigned long image0, image1, sign = r->sign << 15;
4308 switch (r->cl)
4310 case rvc_zero:
4311 image0 = image1 = 0;
4312 break;
4314 case rvc_inf:
4315 case rvc_nan:
4316 image0 = 0xffff7fff | sign;
4317 image1 = 0xffffffff;
4318 break;
4320 case rvc_normal:
4321 /* Extract the significand into straight hi:lo. */
4322 if (HOST_BITS_PER_LONG == 64)
4324 image0 = r->sig[SIGSZ-1];
4325 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4326 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4328 else
4330 image0 = r->sig[SIGSZ-1];
4331 image1 = r->sig[SIGSZ-2];
4332 image1 = (image0 << 21) | (image1 >> 11);
4333 image0 = (image0 >> 11) & 0xfffff;
4336 /* Rearrange the half-words of the significand to match the
4337 external format. */
4338 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4339 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4341 /* Add the sign and exponent. */
4342 image0 |= sign;
4343 image0 |= (REAL_EXP (r) + 1024) << 4;
4344 break;
4346 default:
4347 gcc_unreachable ();
4350 if (FLOAT_WORDS_BIG_ENDIAN)
4351 buf[0] = image1, buf[1] = image0;
4352 else
4353 buf[0] = image0, buf[1] = image1;
4356 static void
4357 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4358 REAL_VALUE_TYPE *r, const long *buf)
4360 unsigned long image0, image1;
4361 int exp;
4363 if (FLOAT_WORDS_BIG_ENDIAN)
4364 image1 = buf[0], image0 = buf[1];
4365 else
4366 image0 = buf[0], image1 = buf[1];
4367 image0 &= 0xffffffff;
4368 image1 &= 0xffffffff;
4370 exp = (image0 >> 4) & 0x7ff;
4372 memset (r, 0, sizeof (*r));
4374 if (exp != 0)
4376 r->cl = rvc_normal;
4377 r->sign = (image0 >> 15) & 1;
4378 SET_REAL_EXP (r, exp - 1024);
4380 /* Rearrange the half-words of the external format into
4381 proper ascending order. */
4382 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4383 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4385 if (HOST_BITS_PER_LONG == 64)
4387 image0 = (image0 << 31 << 1) | image1;
4388 image0 <<= 64 - 53;
4389 image0 |= SIG_MSB;
4390 r->sig[SIGSZ-1] = image0;
4392 else
4394 r->sig[SIGSZ-1] = image0;
4395 r->sig[SIGSZ-2] = image1;
4396 lshift_significand (r, r, 64 - 53);
4397 r->sig[SIGSZ-1] |= SIG_MSB;
4402 const struct real_format vax_f_format =
4404 encode_vax_f,
4405 decode_vax_f,
4409 -127,
4410 127,
4413 false,
4414 false,
4415 false,
4416 false,
4417 false,
4418 false,
4419 false,
4420 false
4423 const struct real_format vax_d_format =
4425 encode_vax_d,
4426 decode_vax_d,
4430 -127,
4431 127,
4434 false,
4435 false,
4436 false,
4437 false,
4438 false,
4439 false,
4440 false,
4441 false
4444 const struct real_format vax_g_format =
4446 encode_vax_g,
4447 decode_vax_g,
4451 -1023,
4452 1023,
4455 false,
4456 false,
4457 false,
4458 false,
4459 false,
4460 false,
4461 false,
4462 false
4465 /* Encode real R into a single precision DFP value in BUF. */
4466 static void
4467 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4468 long *buf ATTRIBUTE_UNUSED,
4469 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4471 encode_decimal32 (fmt, buf, r);
4474 /* Decode a single precision DFP value in BUF into a real R. */
4475 static void
4476 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4477 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4478 const long *buf ATTRIBUTE_UNUSED)
4480 decode_decimal32 (fmt, r, buf);
4483 /* Encode real R into a double precision DFP value in BUF. */
4484 static void
4485 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4486 long *buf ATTRIBUTE_UNUSED,
4487 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4489 encode_decimal64 (fmt, buf, r);
4492 /* Decode a double precision DFP value in BUF into a real R. */
4493 static void
4494 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4495 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4496 const long *buf ATTRIBUTE_UNUSED)
4498 decode_decimal64 (fmt, r, buf);
4501 /* Encode real R into a quad precision DFP value in BUF. */
4502 static void
4503 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4504 long *buf ATTRIBUTE_UNUSED,
4505 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4507 encode_decimal128 (fmt, buf, r);
4510 /* Decode a quad precision DFP value in BUF into a real R. */
4511 static void
4512 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4513 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4514 const long *buf ATTRIBUTE_UNUSED)
4516 decode_decimal128 (fmt, r, buf);
4519 /* Single precision decimal floating point (IEEE 754). */
4520 const struct real_format decimal_single_format =
4522 encode_decimal_single,
4523 decode_decimal_single,
4527 -94,
4531 false,
4532 true,
4533 true,
4534 true,
4535 true,
4536 true,
4537 true,
4538 false
4541 /* Double precision decimal floating point (IEEE 754). */
4542 const struct real_format decimal_double_format =
4544 encode_decimal_double,
4545 decode_decimal_double,
4549 -382,
4550 385,
4553 false,
4554 true,
4555 true,
4556 true,
4557 true,
4558 true,
4559 true,
4560 false
4563 /* Quad precision decimal floating point (IEEE 754). */
4564 const struct real_format decimal_quad_format =
4566 encode_decimal_quad,
4567 decode_decimal_quad,
4571 -6142,
4572 6145,
4573 127,
4574 127,
4575 false,
4576 true,
4577 true,
4578 true,
4579 true,
4580 true,
4581 true,
4582 false
4585 /* Encode half-precision floats. This routine is used both for the IEEE
4586 ARM alternative encodings. */
4587 static void
4588 encode_ieee_half (const struct real_format *fmt, long *buf,
4589 const REAL_VALUE_TYPE *r)
4591 unsigned long image, sig, exp;
4592 unsigned long sign = r->sign;
4593 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
4595 image = sign << 15;
4596 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4598 switch (r->cl)
4600 case rvc_zero:
4601 break;
4603 case rvc_inf:
4604 if (fmt->has_inf)
4605 image |= 31 << 10;
4606 else
4607 image |= 0x7fff;
4608 break;
4610 case rvc_nan:
4611 if (fmt->has_nans)
4613 if (r->canonical)
4614 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4615 if (r->signalling == fmt->qnan_msb_set)
4616 sig &= ~(1 << 9);
4617 else
4618 sig |= 1 << 9;
4619 if (sig == 0)
4620 sig = 1 << 8;
4622 image |= 31 << 10;
4623 image |= sig;
4625 else
4626 image |= 0x3ff;
4627 break;
4629 case rvc_normal:
4630 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4631 whereas the intermediate representation is 0.F x 2**exp.
4632 Which means we're off by one. */
4633 if (denormal)
4634 exp = 0;
4635 else
4636 exp = REAL_EXP (r) + 15 - 1;
4637 image |= exp << 10;
4638 image |= sig;
4639 break;
4641 default:
4642 gcc_unreachable ();
4645 buf[0] = image;
4648 /* Decode half-precision floats. This routine is used both for the IEEE
4649 ARM alternative encodings. */
4650 static void
4651 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4652 const long *buf)
4654 unsigned long image = buf[0] & 0xffff;
4655 bool sign = (image >> 15) & 1;
4656 int exp = (image >> 10) & 0x1f;
4658 memset (r, 0, sizeof (*r));
4659 image <<= HOST_BITS_PER_LONG - 11;
4660 image &= ~SIG_MSB;
4662 if (exp == 0)
4664 if (image && fmt->has_denorm)
4666 r->cl = rvc_normal;
4667 r->sign = sign;
4668 SET_REAL_EXP (r, -14);
4669 r->sig[SIGSZ-1] = image << 1;
4670 normalize (r);
4672 else if (fmt->has_signed_zero)
4673 r->sign = sign;
4675 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4677 if (image)
4679 r->cl = rvc_nan;
4680 r->sign = sign;
4681 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4682 ^ fmt->qnan_msb_set);
4683 r->sig[SIGSZ-1] = image;
4685 else
4687 r->cl = rvc_inf;
4688 r->sign = sign;
4691 else
4693 r->cl = rvc_normal;
4694 r->sign = sign;
4695 SET_REAL_EXP (r, exp - 15 + 1);
4696 r->sig[SIGSZ-1] = image | SIG_MSB;
4700 /* Half-precision format, as specified in IEEE 754R. */
4701 const struct real_format ieee_half_format =
4703 encode_ieee_half,
4704 decode_ieee_half,
4708 -13,
4712 false,
4713 true,
4714 true,
4715 true,
4716 true,
4717 true,
4718 true,
4719 false
4722 /* ARM's alternative half-precision format, similar to IEEE but with
4723 no reserved exponent value for NaNs and infinities; rather, it just
4724 extends the range of exponents by one. */
4725 const struct real_format arm_half_format =
4727 encode_ieee_half,
4728 decode_ieee_half,
4732 -13,
4736 false,
4737 true,
4738 false,
4739 false,
4740 true,
4741 true,
4742 false,
4743 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,
4775 SIGNIFICAND_BITS - 2,
4776 SIGNIFICAND_BITS - 2,
4777 -MAX_EXP,
4778 MAX_EXP,
4781 false,
4782 false,
4783 true,
4784 true,
4785 false,
4786 true,
4787 true,
4788 false
4791 /* Calculate the square root of X in mode MODE, and store the result
4792 in R. Return TRUE if the operation does not raise an exception.
4793 For details see "High Precision Division and Square Root",
4794 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4795 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4797 bool
4798 real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode,
4799 const REAL_VALUE_TYPE *x)
4801 static REAL_VALUE_TYPE halfthree;
4802 static bool init = false;
4803 REAL_VALUE_TYPE h, t, i;
4804 int iter, exp;
4806 /* sqrt(-0.0) is -0.0. */
4807 if (real_isnegzero (x))
4809 *r = *x;
4810 return false;
4813 /* Negative arguments return NaN. */
4814 if (real_isneg (x))
4816 get_canonical_qnan (r, 0);
4817 return false;
4820 /* Infinity and NaN return themselves. */
4821 if (!real_isfinite (x))
4823 *r = *x;
4824 return false;
4827 if (!init)
4829 do_add (&halfthree, &dconst1, &dconsthalf, 0);
4830 init = true;
4833 /* Initial guess for reciprocal sqrt, i. */
4834 exp = real_exponent (x);
4835 real_ldexp (&i, &dconst1, -exp/2);
4837 /* Newton's iteration for reciprocal sqrt, i. */
4838 for (iter = 0; iter < 16; iter++)
4840 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4841 do_multiply (&t, x, &i);
4842 do_multiply (&h, &t, &i);
4843 do_multiply (&t, &h, &dconsthalf);
4844 do_add (&h, &halfthree, &t, 1);
4845 do_multiply (&t, &i, &h);
4847 /* Check for early convergence. */
4848 if (iter >= 6 && real_identical (&i, &t))
4849 break;
4851 /* ??? Unroll loop to avoid copying. */
4852 i = t;
4855 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4856 do_multiply (&t, x, &i);
4857 do_multiply (&h, &t, &i);
4858 do_add (&i, &dconst1, &h, 1);
4859 do_multiply (&h, &t, &i);
4860 do_multiply (&i, &dconsthalf, &h);
4861 do_add (&h, &t, &i, 0);
4863 /* ??? We need a Tuckerman test to get the last bit. */
4865 real_convert (r, mode, &h);
4866 return true;
4869 /* Calculate X raised to the integer exponent N in mode MODE and store
4870 the result in R. Return true if the result may be inexact due to
4871 loss of precision. The algorithm is the classic "left-to-right binary
4872 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4873 Algorithms", "The Art of Computer Programming", Volume 2. */
4875 bool
4876 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4877 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4879 unsigned HOST_WIDE_INT bit;
4880 REAL_VALUE_TYPE t;
4881 bool inexact = false;
4882 bool init = false;
4883 bool neg;
4884 int i;
4886 if (n == 0)
4888 *r = dconst1;
4889 return false;
4891 else if (n < 0)
4893 /* Don't worry about overflow, from now on n is unsigned. */
4894 neg = true;
4895 n = -n;
4897 else
4898 neg = false;
4900 t = *x;
4901 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4902 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4904 if (init)
4906 inexact |= do_multiply (&t, &t, &t);
4907 if (n & bit)
4908 inexact |= do_multiply (&t, &t, x);
4910 else if (n & bit)
4911 init = true;
4912 bit >>= 1;
4915 if (neg)
4916 inexact |= do_divide (&t, &dconst1, &t);
4918 real_convert (r, mode, &t);
4919 return inexact;
4922 /* Round X to the nearest integer not larger in absolute value, i.e.
4923 towards zero, placing the result in R in mode MODE. */
4925 void
4926 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4927 const REAL_VALUE_TYPE *x)
4929 do_fix_trunc (r, x);
4930 if (mode != VOIDmode)
4931 real_convert (r, mode, r);
4934 /* Round X to the largest integer not greater in value, i.e. round
4935 down, placing the result in R in mode MODE. */
4937 void
4938 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4939 const REAL_VALUE_TYPE *x)
4941 REAL_VALUE_TYPE t;
4943 do_fix_trunc (&t, x);
4944 if (! real_identical (&t, x) && x->sign)
4945 do_add (&t, &t, &dconstm1, 0);
4946 if (mode != VOIDmode)
4947 real_convert (r, mode, &t);
4948 else
4949 *r = t;
4952 /* Round X to the smallest integer not less then argument, i.e. round
4953 up, placing the result in R in mode MODE. */
4955 void
4956 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4957 const REAL_VALUE_TYPE *x)
4959 REAL_VALUE_TYPE t;
4961 do_fix_trunc (&t, x);
4962 if (! real_identical (&t, x) && ! x->sign)
4963 do_add (&t, &t, &dconst1, 0);
4964 if (mode != VOIDmode)
4965 real_convert (r, mode, &t);
4966 else
4967 *r = t;
4970 /* Round X to the nearest integer, but round halfway cases away from
4971 zero. */
4973 void
4974 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4975 const REAL_VALUE_TYPE *x)
4977 do_add (r, x, &dconsthalf, x->sign);
4978 do_fix_trunc (r, r);
4979 if (mode != VOIDmode)
4980 real_convert (r, mode, r);
4983 /* Set the sign of R to the sign of X. */
4985 void
4986 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4988 r->sign = x->sign;
4991 /* Check whether the real constant value given is an integer. */
4993 bool
4994 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode)
4996 REAL_VALUE_TYPE cint;
4998 real_trunc (&cint, mode, c);
4999 return real_identical (c, &cint);
5002 /* Write into BUF the maximum representable finite floating-point
5003 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5004 float string. LEN is the size of BUF, and the buffer must be large
5005 enough to contain the resulting string. */
5007 void
5008 get_max_float (const struct real_format *fmt, char *buf, size_t len)
5010 int i, n;
5011 char *p;
5013 strcpy (buf, "0x0.");
5014 n = fmt->p;
5015 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
5016 *p++ = 'f';
5017 if (i < n)
5018 *p++ = "08ce"[n - i];
5019 sprintf (p, "p%d", fmt->emax);
5020 if (fmt->pnan < fmt->p)
5022 /* This is an IBM extended double format made up of two IEEE
5023 doubles. The value of the long double is the sum of the
5024 values of the two parts. The most significant part is
5025 required to be the value of the long double rounded to the
5026 nearest double. Rounding means we need a slightly smaller
5027 value for LDBL_MAX. */
5028 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5031 gcc_assert (strlen (buf) < len);