* gcc.dg/compat/struct-layout-1_generate.c (dg_options): New. Moved
[official-gcc.git] / gcc / real.c
blobdc6d7483e998824e509e72953becec5c610e3de1
1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2002, 2003, 2004, 2005, 2007, 2008 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 "tm_p.h"
31 #include "dfp.h"
33 /* The floating point model used internally is not exactly IEEE 754
34 compliant, and close to the description in the ISO C99 standard,
35 section 5.2.4.2.2 Characteristics of floating types.
37 Specifically
39 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
41 where
42 s = sign (+- 1)
43 b = base or radix, here always 2
44 e = exponent
45 p = precision (the number of base-b digits in the significand)
46 f_k = the digits of the significand.
48 We differ from typical IEEE 754 encodings in that the entire
49 significand is fractional. Normalized significands are in the
50 range [0.5, 1.0).
52 A requirement of the model is that P be larger than the largest
53 supported target floating-point type by at least 2 bits. This gives
54 us proper rounding when we truncate to the target type. In addition,
55 E must be large enough to hold the smallest supported denormal number
56 in a normalized form.
58 Both of these requirements are easily satisfied. The largest target
59 significand is 113 bits; we store at least 160. The smallest
60 denormal number fits in 17 exponent bits; we store 27.
62 Note that the decimal string conversion routines are sensitive to
63 rounding errors. Since the raw arithmetic routines do not themselves
64 have guard digits or rounding, the computation of 10**exp can
65 accumulate more than a few digits of error. The previous incarnation
66 of real.c successfully used a 144-bit fraction; given the current
67 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits. */
70 /* Used to classify two numbers simultaneously. */
71 #define CLASS2(A, B) ((A) << 2 | (B))
73 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
74 #error "Some constant folding done by hand to avoid shift count warnings"
75 #endif
77 static void get_zero (REAL_VALUE_TYPE *, int);
78 static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
79 static void get_canonical_snan (REAL_VALUE_TYPE *, int);
80 static void get_inf (REAL_VALUE_TYPE *, int);
81 static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
82 const REAL_VALUE_TYPE *, unsigned int);
83 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
84 unsigned int);
85 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
86 unsigned int);
87 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
88 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
89 const REAL_VALUE_TYPE *);
90 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
91 const REAL_VALUE_TYPE *, int);
92 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
93 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
94 static int cmp_significand_0 (const REAL_VALUE_TYPE *);
95 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
96 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
97 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
98 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
99 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
100 const REAL_VALUE_TYPE *);
101 static void normalize (REAL_VALUE_TYPE *);
103 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
104 const REAL_VALUE_TYPE *, int);
105 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
106 const REAL_VALUE_TYPE *);
107 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
108 const REAL_VALUE_TYPE *);
109 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
110 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
112 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
114 static const REAL_VALUE_TYPE * ten_to_ptwo (int);
115 static const REAL_VALUE_TYPE * ten_to_mptwo (int);
116 static const REAL_VALUE_TYPE * real_digit (int);
117 static void times_pten (REAL_VALUE_TYPE *, int);
119 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
121 /* Initialize R with a positive zero. */
123 static inline void
124 get_zero (REAL_VALUE_TYPE *r, int sign)
126 memset (r, 0, sizeof (*r));
127 r->sign = sign;
130 /* Initialize R with the canonical quiet NaN. */
132 static inline void
133 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
135 memset (r, 0, sizeof (*r));
136 r->cl = rvc_nan;
137 r->sign = sign;
138 r->canonical = 1;
141 static inline void
142 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
144 memset (r, 0, sizeof (*r));
145 r->cl = rvc_nan;
146 r->sign = sign;
147 r->signalling = 1;
148 r->canonical = 1;
151 static inline void
152 get_inf (REAL_VALUE_TYPE *r, int sign)
154 memset (r, 0, sizeof (*r));
155 r->cl = rvc_inf;
156 r->sign = sign;
160 /* Right-shift the significand of A by N bits; put the result in the
161 significand of R. If any one bits are shifted out, return true. */
163 static bool
164 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
165 unsigned int n)
167 unsigned long sticky = 0;
168 unsigned int i, ofs = 0;
170 if (n >= HOST_BITS_PER_LONG)
172 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
173 sticky |= a->sig[i];
174 n &= HOST_BITS_PER_LONG - 1;
177 if (n != 0)
179 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
180 for (i = 0; i < SIGSZ; ++i)
182 r->sig[i]
183 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
184 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
185 << (HOST_BITS_PER_LONG - n)));
188 else
190 for (i = 0; ofs + i < SIGSZ; ++i)
191 r->sig[i] = a->sig[ofs + i];
192 for (; i < SIGSZ; ++i)
193 r->sig[i] = 0;
196 return sticky != 0;
199 /* Right-shift the significand of A by N bits; put the result in the
200 significand of R. */
202 static void
203 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
204 unsigned int n)
206 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
208 n &= HOST_BITS_PER_LONG - 1;
209 if (n != 0)
211 for (i = 0; i < SIGSZ; ++i)
213 r->sig[i]
214 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
215 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
216 << (HOST_BITS_PER_LONG - n)));
219 else
221 for (i = 0; ofs + i < SIGSZ; ++i)
222 r->sig[i] = a->sig[ofs + i];
223 for (; i < SIGSZ; ++i)
224 r->sig[i] = 0;
228 /* Left-shift the significand of A by N bits; put the result in the
229 significand of R. */
231 static void
232 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
233 unsigned int n)
235 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
237 n &= HOST_BITS_PER_LONG - 1;
238 if (n == 0)
240 for (i = 0; ofs + i < SIGSZ; ++i)
241 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
242 for (; i < SIGSZ; ++i)
243 r->sig[SIGSZ-1-i] = 0;
245 else
246 for (i = 0; i < SIGSZ; ++i)
248 r->sig[SIGSZ-1-i]
249 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
250 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
251 >> (HOST_BITS_PER_LONG - n)));
255 /* Likewise, but N is specialized to 1. */
257 static inline void
258 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
260 unsigned int i;
262 for (i = SIGSZ - 1; i > 0; --i)
263 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
264 r->sig[0] = a->sig[0] << 1;
267 /* Add the significands of A and B, placing the result in R. Return
268 true if there was carry out of the most significant word. */
270 static inline bool
271 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
272 const REAL_VALUE_TYPE *b)
274 bool carry = false;
275 int i;
277 for (i = 0; i < SIGSZ; ++i)
279 unsigned long ai = a->sig[i];
280 unsigned long ri = ai + b->sig[i];
282 if (carry)
284 carry = ri < ai;
285 carry |= ++ri == 0;
287 else
288 carry = ri < ai;
290 r->sig[i] = ri;
293 return carry;
296 /* Subtract the significands of A and B, placing the result in R. CARRY is
297 true if there's a borrow incoming to the least significant word.
298 Return true if there was borrow out of the most significant word. */
300 static inline bool
301 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
302 const REAL_VALUE_TYPE *b, int carry)
304 int i;
306 for (i = 0; i < SIGSZ; ++i)
308 unsigned long ai = a->sig[i];
309 unsigned long ri = ai - b->sig[i];
311 if (carry)
313 carry = ri > ai;
314 carry |= ~--ri == 0;
316 else
317 carry = ri > ai;
319 r->sig[i] = ri;
322 return carry;
325 /* Negate the significand A, placing the result in R. */
327 static inline void
328 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
330 bool carry = true;
331 int i;
333 for (i = 0; i < SIGSZ; ++i)
335 unsigned long ri, ai = a->sig[i];
337 if (carry)
339 if (ai)
341 ri = -ai;
342 carry = false;
344 else
345 ri = ai;
347 else
348 ri = ~ai;
350 r->sig[i] = ri;
354 /* Compare significands. Return tri-state vs zero. */
356 static inline int
357 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
359 int i;
361 for (i = SIGSZ - 1; i >= 0; --i)
363 unsigned long ai = a->sig[i];
364 unsigned long bi = b->sig[i];
366 if (ai > bi)
367 return 1;
368 if (ai < bi)
369 return -1;
372 return 0;
375 /* Return true if A is nonzero. */
377 static inline int
378 cmp_significand_0 (const REAL_VALUE_TYPE *a)
380 int i;
382 for (i = SIGSZ - 1; i >= 0; --i)
383 if (a->sig[i])
384 return 1;
386 return 0;
389 /* Set bit N of the significand of R. */
391 static inline void
392 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
394 r->sig[n / HOST_BITS_PER_LONG]
395 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
398 /* Clear bit N of the significand of R. */
400 static inline void
401 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
403 r->sig[n / HOST_BITS_PER_LONG]
404 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
407 /* Test bit N of the significand of R. */
409 static inline bool
410 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
412 /* ??? Compiler bug here if we return this expression directly.
413 The conversion to bool strips the "&1" and we wind up testing
414 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
415 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
416 return t;
419 /* Clear bits 0..N-1 of the significand of R. */
421 static void
422 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
424 int i, w = n / HOST_BITS_PER_LONG;
426 for (i = 0; i < w; ++i)
427 r->sig[i] = 0;
429 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
432 /* Divide the significands of A and B, placing the result in R. Return
433 true if the division was inexact. */
435 static inline bool
436 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
437 const REAL_VALUE_TYPE *b)
439 REAL_VALUE_TYPE u;
440 int i, bit = SIGNIFICAND_BITS - 1;
441 unsigned long msb, inexact;
443 u = *a;
444 memset (r->sig, 0, sizeof (r->sig));
446 msb = 0;
447 goto start;
450 msb = u.sig[SIGSZ-1] & SIG_MSB;
451 lshift_significand_1 (&u, &u);
452 start:
453 if (msb || cmp_significands (&u, b) >= 0)
455 sub_significands (&u, &u, b, 0);
456 set_significand_bit (r, bit);
459 while (--bit >= 0);
461 for (i = 0, inexact = 0; i < SIGSZ; i++)
462 inexact |= u.sig[i];
464 return inexact != 0;
467 /* Adjust the exponent and significand of R such that the most
468 significant bit is set. We underflow to zero and overflow to
469 infinity here, without denormals. (The intermediate representation
470 exponent is large enough to handle target denormals normalized.) */
472 static void
473 normalize (REAL_VALUE_TYPE *r)
475 int shift = 0, exp;
476 int i, j;
478 if (r->decimal)
479 return;
481 /* Find the first word that is nonzero. */
482 for (i = SIGSZ - 1; i >= 0; i--)
483 if (r->sig[i] == 0)
484 shift += HOST_BITS_PER_LONG;
485 else
486 break;
488 /* Zero significand flushes to zero. */
489 if (i < 0)
491 r->cl = rvc_zero;
492 SET_REAL_EXP (r, 0);
493 return;
496 /* Find the first bit that is nonzero. */
497 for (j = 0; ; j++)
498 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
499 break;
500 shift += j;
502 if (shift > 0)
504 exp = REAL_EXP (r) - shift;
505 if (exp > MAX_EXP)
506 get_inf (r, r->sign);
507 else if (exp < -MAX_EXP)
508 get_zero (r, r->sign);
509 else
511 SET_REAL_EXP (r, exp);
512 lshift_significand (r, r, shift);
517 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
518 result may be inexact due to a loss of precision. */
520 static bool
521 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
522 const REAL_VALUE_TYPE *b, int subtract_p)
524 int dexp, sign, exp;
525 REAL_VALUE_TYPE t;
526 bool inexact = false;
528 /* Determine if we need to add or subtract. */
529 sign = a->sign;
530 subtract_p = (sign ^ b->sign) ^ subtract_p;
532 switch (CLASS2 (a->cl, b->cl))
534 case CLASS2 (rvc_zero, rvc_zero):
535 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
536 get_zero (r, sign & !subtract_p);
537 return false;
539 case CLASS2 (rvc_zero, rvc_normal):
540 case CLASS2 (rvc_zero, rvc_inf):
541 case CLASS2 (rvc_zero, rvc_nan):
542 /* 0 + ANY = ANY. */
543 case CLASS2 (rvc_normal, rvc_nan):
544 case CLASS2 (rvc_inf, rvc_nan):
545 case CLASS2 (rvc_nan, rvc_nan):
546 /* ANY + NaN = NaN. */
547 case CLASS2 (rvc_normal, rvc_inf):
548 /* R + Inf = Inf. */
549 *r = *b;
550 r->sign = sign ^ subtract_p;
551 return false;
553 case CLASS2 (rvc_normal, rvc_zero):
554 case CLASS2 (rvc_inf, rvc_zero):
555 case CLASS2 (rvc_nan, rvc_zero):
556 /* ANY + 0 = ANY. */
557 case CLASS2 (rvc_nan, rvc_normal):
558 case CLASS2 (rvc_nan, rvc_inf):
559 /* NaN + ANY = NaN. */
560 case CLASS2 (rvc_inf, rvc_normal):
561 /* Inf + R = Inf. */
562 *r = *a;
563 return false;
565 case CLASS2 (rvc_inf, rvc_inf):
566 if (subtract_p)
567 /* Inf - Inf = NaN. */
568 get_canonical_qnan (r, 0);
569 else
570 /* Inf + Inf = Inf. */
571 *r = *a;
572 return false;
574 case CLASS2 (rvc_normal, rvc_normal):
575 break;
577 default:
578 gcc_unreachable ();
581 /* Swap the arguments such that A has the larger exponent. */
582 dexp = REAL_EXP (a) - REAL_EXP (b);
583 if (dexp < 0)
585 const REAL_VALUE_TYPE *t;
586 t = a, a = b, b = t;
587 dexp = -dexp;
588 sign ^= subtract_p;
590 exp = REAL_EXP (a);
592 /* If the exponents are not identical, we need to shift the
593 significand of B down. */
594 if (dexp > 0)
596 /* If the exponents are too far apart, the significands
597 do not overlap, which makes the subtraction a noop. */
598 if (dexp >= SIGNIFICAND_BITS)
600 *r = *a;
601 r->sign = sign;
602 return true;
605 inexact |= sticky_rshift_significand (&t, b, dexp);
606 b = &t;
609 if (subtract_p)
611 if (sub_significands (r, a, b, inexact))
613 /* We got a borrow out of the subtraction. That means that
614 A and B had the same exponent, and B had the larger
615 significand. We need to swap the sign and negate the
616 significand. */
617 sign ^= 1;
618 neg_significand (r, r);
621 else
623 if (add_significands (r, a, b))
625 /* We got carry out of the addition. This means we need to
626 shift the significand back down one bit and increase the
627 exponent. */
628 inexact |= sticky_rshift_significand (r, r, 1);
629 r->sig[SIGSZ-1] |= SIG_MSB;
630 if (++exp > MAX_EXP)
632 get_inf (r, sign);
633 return true;
638 r->cl = rvc_normal;
639 r->sign = sign;
640 SET_REAL_EXP (r, exp);
641 /* Zero out the remaining fields. */
642 r->signalling = 0;
643 r->canonical = 0;
644 r->decimal = 0;
646 /* Re-normalize the result. */
647 normalize (r);
649 /* Special case: if the subtraction results in zero, the result
650 is positive. */
651 if (r->cl == rvc_zero)
652 r->sign = 0;
653 else
654 r->sig[0] |= inexact;
656 return inexact;
659 /* Calculate R = A * B. Return true if the result may be inexact. */
661 static bool
662 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
663 const REAL_VALUE_TYPE *b)
665 REAL_VALUE_TYPE u, t, *rr;
666 unsigned int i, j, k;
667 int sign = a->sign ^ b->sign;
668 bool inexact = false;
670 switch (CLASS2 (a->cl, b->cl))
672 case CLASS2 (rvc_zero, rvc_zero):
673 case CLASS2 (rvc_zero, rvc_normal):
674 case CLASS2 (rvc_normal, rvc_zero):
675 /* +-0 * ANY = 0 with appropriate sign. */
676 get_zero (r, sign);
677 return false;
679 case CLASS2 (rvc_zero, rvc_nan):
680 case CLASS2 (rvc_normal, rvc_nan):
681 case CLASS2 (rvc_inf, rvc_nan):
682 case CLASS2 (rvc_nan, rvc_nan):
683 /* ANY * NaN = NaN. */
684 *r = *b;
685 r->sign = sign;
686 return false;
688 case CLASS2 (rvc_nan, rvc_zero):
689 case CLASS2 (rvc_nan, rvc_normal):
690 case CLASS2 (rvc_nan, rvc_inf):
691 /* NaN * ANY = NaN. */
692 *r = *a;
693 r->sign = sign;
694 return false;
696 case CLASS2 (rvc_zero, rvc_inf):
697 case CLASS2 (rvc_inf, rvc_zero):
698 /* 0 * Inf = NaN */
699 get_canonical_qnan (r, sign);
700 return false;
702 case CLASS2 (rvc_inf, rvc_inf):
703 case CLASS2 (rvc_normal, rvc_inf):
704 case CLASS2 (rvc_inf, rvc_normal):
705 /* Inf * Inf = Inf, R * Inf = Inf */
706 get_inf (r, sign);
707 return false;
709 case CLASS2 (rvc_normal, rvc_normal):
710 break;
712 default:
713 gcc_unreachable ();
716 if (r == a || r == b)
717 rr = &t;
718 else
719 rr = r;
720 get_zero (rr, 0);
722 /* Collect all the partial products. Since we don't have sure access
723 to a widening multiply, we split each long into two half-words.
725 Consider the long-hand form of a four half-word multiplication:
727 A B C D
728 * E F G H
729 --------------
730 DE DF DG DH
731 CE CF CG CH
732 BE BF BG BH
733 AE AF AG AH
735 We construct partial products of the widened half-word products
736 that are known to not overlap, e.g. DF+DH. Each such partial
737 product is given its proper exponent, which allows us to sum them
738 and obtain the finished product. */
740 for (i = 0; i < SIGSZ * 2; ++i)
742 unsigned long ai = a->sig[i / 2];
743 if (i & 1)
744 ai >>= HOST_BITS_PER_LONG / 2;
745 else
746 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
748 if (ai == 0)
749 continue;
751 for (j = 0; j < 2; ++j)
753 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
754 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
756 if (exp > MAX_EXP)
758 get_inf (r, sign);
759 return true;
761 if (exp < -MAX_EXP)
763 /* Would underflow to zero, which we shouldn't bother adding. */
764 inexact = true;
765 continue;
768 memset (&u, 0, sizeof (u));
769 u.cl = rvc_normal;
770 SET_REAL_EXP (&u, exp);
772 for (k = j; k < SIGSZ * 2; k += 2)
774 unsigned long bi = b->sig[k / 2];
775 if (k & 1)
776 bi >>= HOST_BITS_PER_LONG / 2;
777 else
778 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
780 u.sig[k / 2] = ai * bi;
783 normalize (&u);
784 inexact |= do_add (rr, rr, &u, 0);
788 rr->sign = sign;
789 if (rr != r)
790 *r = t;
792 return inexact;
795 /* Calculate R = A / B. Return true if the result may be inexact. */
797 static bool
798 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
799 const REAL_VALUE_TYPE *b)
801 int exp, sign = a->sign ^ b->sign;
802 REAL_VALUE_TYPE t, *rr;
803 bool inexact;
805 switch (CLASS2 (a->cl, b->cl))
807 case CLASS2 (rvc_zero, rvc_zero):
808 /* 0 / 0 = NaN. */
809 case CLASS2 (rvc_inf, rvc_inf):
810 /* Inf / Inf = NaN. */
811 get_canonical_qnan (r, sign);
812 return false;
814 case CLASS2 (rvc_zero, rvc_normal):
815 case CLASS2 (rvc_zero, rvc_inf):
816 /* 0 / ANY = 0. */
817 case CLASS2 (rvc_normal, rvc_inf):
818 /* R / Inf = 0. */
819 get_zero (r, sign);
820 return false;
822 case CLASS2 (rvc_normal, rvc_zero):
823 /* R / 0 = Inf. */
824 case CLASS2 (rvc_inf, rvc_zero):
825 /* Inf / 0 = Inf. */
826 get_inf (r, sign);
827 return false;
829 case CLASS2 (rvc_zero, rvc_nan):
830 case CLASS2 (rvc_normal, rvc_nan):
831 case CLASS2 (rvc_inf, rvc_nan):
832 case CLASS2 (rvc_nan, rvc_nan):
833 /* ANY / NaN = NaN. */
834 *r = *b;
835 r->sign = sign;
836 return false;
838 case CLASS2 (rvc_nan, rvc_zero):
839 case CLASS2 (rvc_nan, rvc_normal):
840 case CLASS2 (rvc_nan, rvc_inf):
841 /* NaN / ANY = NaN. */
842 *r = *a;
843 r->sign = sign;
844 return false;
846 case CLASS2 (rvc_inf, rvc_normal):
847 /* Inf / R = Inf. */
848 get_inf (r, sign);
849 return false;
851 case CLASS2 (rvc_normal, rvc_normal):
852 break;
854 default:
855 gcc_unreachable ();
858 if (r == a || r == b)
859 rr = &t;
860 else
861 rr = r;
863 /* Make sure all fields in the result are initialized. */
864 get_zero (rr, 0);
865 rr->cl = rvc_normal;
866 rr->sign = sign;
868 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
869 if (exp > MAX_EXP)
871 get_inf (r, sign);
872 return true;
874 if (exp < -MAX_EXP)
876 get_zero (r, sign);
877 return true;
879 SET_REAL_EXP (rr, exp);
881 inexact = div_significands (rr, a, b);
883 /* Re-normalize the result. */
884 normalize (rr);
885 rr->sig[0] |= inexact;
887 if (rr != r)
888 *r = t;
890 return inexact;
893 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
894 one of the two operands is a NaN. */
896 static int
897 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
898 int nan_result)
900 int ret;
902 switch (CLASS2 (a->cl, b->cl))
904 case CLASS2 (rvc_zero, rvc_zero):
905 /* Sign of zero doesn't matter for compares. */
906 return 0;
908 case CLASS2 (rvc_inf, rvc_zero):
909 case CLASS2 (rvc_inf, rvc_normal):
910 case CLASS2 (rvc_normal, rvc_zero):
911 return (a->sign ? -1 : 1);
913 case CLASS2 (rvc_inf, rvc_inf):
914 return -a->sign - -b->sign;
916 case CLASS2 (rvc_zero, rvc_normal):
917 case CLASS2 (rvc_zero, rvc_inf):
918 case CLASS2 (rvc_normal, rvc_inf):
919 return (b->sign ? 1 : -1);
921 case CLASS2 (rvc_zero, rvc_nan):
922 case CLASS2 (rvc_normal, rvc_nan):
923 case CLASS2 (rvc_inf, rvc_nan):
924 case CLASS2 (rvc_nan, rvc_nan):
925 case CLASS2 (rvc_nan, rvc_zero):
926 case CLASS2 (rvc_nan, rvc_normal):
927 case CLASS2 (rvc_nan, rvc_inf):
928 return nan_result;
930 case CLASS2 (rvc_normal, rvc_normal):
931 break;
933 default:
934 gcc_unreachable ();
937 if (a->sign != b->sign)
938 return -a->sign - -b->sign;
940 if (a->decimal || b->decimal)
941 return decimal_do_compare (a, b, nan_result);
943 if (REAL_EXP (a) > REAL_EXP (b))
944 ret = 1;
945 else if (REAL_EXP (a) < REAL_EXP (b))
946 ret = -1;
947 else
948 ret = cmp_significands (a, b);
950 return (a->sign ? -ret : ret);
953 /* Return A truncated to an integral value toward zero. */
955 static void
956 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
958 *r = *a;
960 switch (r->cl)
962 case rvc_zero:
963 case rvc_inf:
964 case rvc_nan:
965 break;
967 case rvc_normal:
968 if (r->decimal)
970 decimal_do_fix_trunc (r, a);
971 return;
973 if (REAL_EXP (r) <= 0)
974 get_zero (r, r->sign);
975 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
976 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
977 break;
979 default:
980 gcc_unreachable ();
984 /* Perform the binary or unary operation described by CODE.
985 For a unary operation, leave OP1 NULL. This function returns
986 true if the result may be inexact due to loss of precision. */
988 bool
989 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
990 const REAL_VALUE_TYPE *op1)
992 enum tree_code code = icode;
994 if (op0->decimal || (op1 && op1->decimal))
995 return decimal_real_arithmetic (r, icode, op0, op1);
997 switch (code)
999 case PLUS_EXPR:
1000 return do_add (r, op0, op1, 0);
1002 case MINUS_EXPR:
1003 return do_add (r, op0, op1, 1);
1005 case MULT_EXPR:
1006 return do_multiply (r, op0, op1);
1008 case RDIV_EXPR:
1009 return do_divide (r, op0, op1);
1011 case MIN_EXPR:
1012 if (op1->cl == rvc_nan)
1013 *r = *op1;
1014 else if (do_compare (op0, op1, -1) < 0)
1015 *r = *op0;
1016 else
1017 *r = *op1;
1018 break;
1020 case MAX_EXPR:
1021 if (op1->cl == rvc_nan)
1022 *r = *op1;
1023 else if (do_compare (op0, op1, 1) < 0)
1024 *r = *op1;
1025 else
1026 *r = *op0;
1027 break;
1029 case NEGATE_EXPR:
1030 *r = *op0;
1031 r->sign ^= 1;
1032 break;
1034 case ABS_EXPR:
1035 *r = *op0;
1036 r->sign = 0;
1037 break;
1039 case FIX_TRUNC_EXPR:
1040 do_fix_trunc (r, op0);
1041 break;
1043 default:
1044 gcc_unreachable ();
1046 return false;
1049 /* Legacy. Similar, but return the result directly. */
1051 REAL_VALUE_TYPE
1052 real_arithmetic2 (int icode, const REAL_VALUE_TYPE *op0,
1053 const REAL_VALUE_TYPE *op1)
1055 REAL_VALUE_TYPE r;
1056 real_arithmetic (&r, icode, op0, op1);
1057 return r;
1060 bool
1061 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1062 const REAL_VALUE_TYPE *op1)
1064 enum tree_code code = icode;
1066 switch (code)
1068 case LT_EXPR:
1069 return do_compare (op0, op1, 1) < 0;
1070 case LE_EXPR:
1071 return do_compare (op0, op1, 1) <= 0;
1072 case GT_EXPR:
1073 return do_compare (op0, op1, -1) > 0;
1074 case GE_EXPR:
1075 return do_compare (op0, op1, -1) >= 0;
1076 case EQ_EXPR:
1077 return do_compare (op0, op1, -1) == 0;
1078 case NE_EXPR:
1079 return do_compare (op0, op1, -1) != 0;
1080 case UNORDERED_EXPR:
1081 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1082 case ORDERED_EXPR:
1083 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1084 case UNLT_EXPR:
1085 return do_compare (op0, op1, -1) < 0;
1086 case UNLE_EXPR:
1087 return do_compare (op0, op1, -1) <= 0;
1088 case UNGT_EXPR:
1089 return do_compare (op0, op1, 1) > 0;
1090 case UNGE_EXPR:
1091 return do_compare (op0, op1, 1) >= 0;
1092 case UNEQ_EXPR:
1093 return do_compare (op0, op1, 0) == 0;
1094 case LTGT_EXPR:
1095 return do_compare (op0, op1, 0) != 0;
1097 default:
1098 gcc_unreachable ();
1102 /* Return floor log2(R). */
1105 real_exponent (const REAL_VALUE_TYPE *r)
1107 switch (r->cl)
1109 case rvc_zero:
1110 return 0;
1111 case rvc_inf:
1112 case rvc_nan:
1113 return (unsigned int)-1 >> 1;
1114 case rvc_normal:
1115 return REAL_EXP (r);
1116 default:
1117 gcc_unreachable ();
1121 /* R = OP0 * 2**EXP. */
1123 void
1124 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1126 *r = *op0;
1127 switch (r->cl)
1129 case rvc_zero:
1130 case rvc_inf:
1131 case rvc_nan:
1132 break;
1134 case rvc_normal:
1135 exp += REAL_EXP (op0);
1136 if (exp > MAX_EXP)
1137 get_inf (r, r->sign);
1138 else if (exp < -MAX_EXP)
1139 get_zero (r, r->sign);
1140 else
1141 SET_REAL_EXP (r, exp);
1142 break;
1144 default:
1145 gcc_unreachable ();
1149 /* Determine whether a floating-point value X is infinite. */
1151 bool
1152 real_isinf (const REAL_VALUE_TYPE *r)
1154 return (r->cl == rvc_inf);
1157 /* Determine whether a floating-point value X is a NaN. */
1159 bool
1160 real_isnan (const REAL_VALUE_TYPE *r)
1162 return (r->cl == rvc_nan);
1165 /* Determine whether a floating-point value X is finite. */
1167 bool
1168 real_isfinite (const REAL_VALUE_TYPE *r)
1170 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1173 /* Determine whether a floating-point value X is negative. */
1175 bool
1176 real_isneg (const REAL_VALUE_TYPE *r)
1178 return r->sign;
1181 /* Determine whether a floating-point value X is minus zero. */
1183 bool
1184 real_isnegzero (const REAL_VALUE_TYPE *r)
1186 return r->sign && r->cl == rvc_zero;
1189 /* Compare two floating-point objects for bitwise identity. */
1191 bool
1192 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1194 int i;
1196 if (a->cl != b->cl)
1197 return false;
1198 if (a->sign != b->sign)
1199 return false;
1201 switch (a->cl)
1203 case rvc_zero:
1204 case rvc_inf:
1205 return true;
1207 case rvc_normal:
1208 if (a->decimal != b->decimal)
1209 return false;
1210 if (REAL_EXP (a) != REAL_EXP (b))
1211 return false;
1212 break;
1214 case rvc_nan:
1215 if (a->signalling != b->signalling)
1216 return false;
1217 /* The significand is ignored for canonical NaNs. */
1218 if (a->canonical || b->canonical)
1219 return a->canonical == b->canonical;
1220 break;
1222 default:
1223 gcc_unreachable ();
1226 for (i = 0; i < SIGSZ; ++i)
1227 if (a->sig[i] != b->sig[i])
1228 return false;
1230 return true;
1233 /* Try to change R into its exact multiplicative inverse in machine
1234 mode MODE. Return true if successful. */
1236 bool
1237 exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r)
1239 const REAL_VALUE_TYPE *one = real_digit (1);
1240 REAL_VALUE_TYPE u;
1241 int i;
1243 if (r->cl != rvc_normal)
1244 return false;
1246 /* Check for a power of two: all significand bits zero except the MSB. */
1247 for (i = 0; i < SIGSZ-1; ++i)
1248 if (r->sig[i] != 0)
1249 return false;
1250 if (r->sig[SIGSZ-1] != SIG_MSB)
1251 return false;
1253 /* Find the inverse and truncate to the required mode. */
1254 do_divide (&u, one, r);
1255 real_convert (&u, mode, &u);
1257 /* The rounding may have overflowed. */
1258 if (u.cl != rvc_normal)
1259 return false;
1260 for (i = 0; i < SIGSZ-1; ++i)
1261 if (u.sig[i] != 0)
1262 return false;
1263 if (u.sig[SIGSZ-1] != SIG_MSB)
1264 return false;
1266 *r = u;
1267 return true;
1270 /* Render R as an integer. */
1272 HOST_WIDE_INT
1273 real_to_integer (const REAL_VALUE_TYPE *r)
1275 unsigned HOST_WIDE_INT i;
1277 switch (r->cl)
1279 case rvc_zero:
1280 underflow:
1281 return 0;
1283 case rvc_inf:
1284 case rvc_nan:
1285 overflow:
1286 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1287 if (!r->sign)
1288 i--;
1289 return i;
1291 case rvc_normal:
1292 if (r->decimal)
1293 return decimal_real_to_integer (r);
1295 if (REAL_EXP (r) <= 0)
1296 goto underflow;
1297 /* Only force overflow for unsigned overflow. Signed overflow is
1298 undefined, so it doesn't matter what we return, and some callers
1299 expect to be able to use this routine for both signed and
1300 unsigned conversions. */
1301 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1302 goto overflow;
1304 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1305 i = r->sig[SIGSZ-1];
1306 else
1308 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1309 i = r->sig[SIGSZ-1];
1310 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1311 i |= r->sig[SIGSZ-2];
1314 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1316 if (r->sign)
1317 i = -i;
1318 return i;
1320 default:
1321 gcc_unreachable ();
1325 /* Likewise, but to an integer pair, HI+LOW. */
1327 void
1328 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
1329 const REAL_VALUE_TYPE *r)
1331 REAL_VALUE_TYPE t;
1332 HOST_WIDE_INT low, high;
1333 int exp;
1335 switch (r->cl)
1337 case rvc_zero:
1338 underflow:
1339 low = high = 0;
1340 break;
1342 case rvc_inf:
1343 case rvc_nan:
1344 overflow:
1345 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1346 if (r->sign)
1347 low = 0;
1348 else
1350 high--;
1351 low = -1;
1353 break;
1355 case rvc_normal:
1356 if (r->decimal)
1358 decimal_real_to_integer2 (plow, phigh, r);
1359 return;
1362 exp = REAL_EXP (r);
1363 if (exp <= 0)
1364 goto underflow;
1365 /* Only force overflow for unsigned overflow. Signed overflow is
1366 undefined, so it doesn't matter what we return, and some callers
1367 expect to be able to use this routine for both signed and
1368 unsigned conversions. */
1369 if (exp > 2*HOST_BITS_PER_WIDE_INT)
1370 goto overflow;
1372 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1373 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1375 high = t.sig[SIGSZ-1];
1376 low = t.sig[SIGSZ-2];
1378 else
1380 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1381 high = t.sig[SIGSZ-1];
1382 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1383 high |= t.sig[SIGSZ-2];
1385 low = t.sig[SIGSZ-3];
1386 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1387 low |= t.sig[SIGSZ-4];
1390 if (r->sign)
1392 if (low == 0)
1393 high = -high;
1394 else
1395 low = -low, high = ~high;
1397 break;
1399 default:
1400 gcc_unreachable ();
1403 *plow = low;
1404 *phigh = high;
1407 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1408 of NUM / DEN. Return the quotient and place the remainder in NUM.
1409 It is expected that NUM / DEN are close enough that the quotient is
1410 small. */
1412 static unsigned long
1413 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1415 unsigned long q, msb;
1416 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1418 if (expn < expd)
1419 return 0;
1421 q = msb = 0;
1422 goto start;
1425 msb = num->sig[SIGSZ-1] & SIG_MSB;
1426 q <<= 1;
1427 lshift_significand_1 (num, num);
1428 start:
1429 if (msb || cmp_significands (num, den) >= 0)
1431 sub_significands (num, num, den, 0);
1432 q |= 1;
1435 while (--expn >= expd);
1437 SET_REAL_EXP (num, expd);
1438 normalize (num);
1440 return q;
1443 /* Render R as a decimal floating point constant. Emit DIGITS significant
1444 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1445 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1446 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1447 to a string that, when parsed back in mode MODE, yields the same value. */
1449 #define M_LOG10_2 0.30102999566398119521
1451 void
1452 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1453 size_t buf_size, size_t digits,
1454 int crop_trailing_zeros, enum machine_mode mode)
1456 const struct real_format *fmt = NULL;
1457 const REAL_VALUE_TYPE *one, *ten;
1458 REAL_VALUE_TYPE r, pten, u, v;
1459 int dec_exp, cmp_one, digit;
1460 size_t max_digits;
1461 char *p, *first, *last;
1462 bool sign;
1463 bool round_up;
1465 if (mode != VOIDmode)
1467 fmt = REAL_MODE_FORMAT (mode);
1468 gcc_assert (fmt);
1471 r = *r_orig;
1472 switch (r.cl)
1474 case rvc_zero:
1475 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1476 return;
1477 case rvc_normal:
1478 break;
1479 case rvc_inf:
1480 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1481 return;
1482 case rvc_nan:
1483 /* ??? Print the significand as well, if not canonical? */
1484 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1485 (r_orig->signalling ? 'S' : 'Q'));
1486 return;
1487 default:
1488 gcc_unreachable ();
1491 if (r.decimal)
1493 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1494 return;
1497 /* Bound the number of digits printed by the size of the representation. */
1498 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1499 if (digits == 0 || digits > max_digits)
1500 digits = max_digits;
1502 /* Estimate the decimal exponent, and compute the length of the string it
1503 will print as. Be conservative and add one to account for possible
1504 overflow or rounding error. */
1505 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1506 for (max_digits = 1; dec_exp ; max_digits++)
1507 dec_exp /= 10;
1509 /* Bound the number of digits printed by the size of the output buffer. */
1510 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1511 gcc_assert (max_digits <= buf_size);
1512 if (digits > max_digits)
1513 digits = max_digits;
1515 one = real_digit (1);
1516 ten = ten_to_ptwo (0);
1518 sign = r.sign;
1519 r.sign = 0;
1521 dec_exp = 0;
1522 pten = *one;
1524 cmp_one = do_compare (&r, one, 0);
1525 if (cmp_one > 0)
1527 int m;
1529 /* Number is greater than one. Convert significand to an integer
1530 and strip trailing decimal zeros. */
1532 u = r;
1533 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1535 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1536 m = floor_log2 (max_digits);
1538 /* Iterate over the bits of the possible powers of 10 that might
1539 be present in U and eliminate them. That is, if we find that
1540 10**2**M divides U evenly, keep the division and increase
1541 DEC_EXP by 2**M. */
1544 REAL_VALUE_TYPE t;
1546 do_divide (&t, &u, ten_to_ptwo (m));
1547 do_fix_trunc (&v, &t);
1548 if (cmp_significands (&v, &t) == 0)
1550 u = t;
1551 dec_exp += 1 << m;
1554 while (--m >= 0);
1556 /* Revert the scaling to integer that we performed earlier. */
1557 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1558 - (SIGNIFICAND_BITS - 1));
1559 r = u;
1561 /* Find power of 10. Do this by dividing out 10**2**M when
1562 this is larger than the current remainder. Fill PTEN with
1563 the power of 10 that we compute. */
1564 if (REAL_EXP (&r) > 0)
1566 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1569 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1570 if (do_compare (&u, ptentwo, 0) >= 0)
1572 do_divide (&u, &u, ptentwo);
1573 do_multiply (&pten, &pten, ptentwo);
1574 dec_exp += 1 << m;
1577 while (--m >= 0);
1579 else
1580 /* We managed to divide off enough tens in the above reduction
1581 loop that we've now got a negative exponent. Fall into the
1582 less-than-one code to compute the proper value for PTEN. */
1583 cmp_one = -1;
1585 if (cmp_one < 0)
1587 int m;
1589 /* Number is less than one. Pad significand with leading
1590 decimal zeros. */
1592 v = r;
1593 while (1)
1595 /* Stop if we'd shift bits off the bottom. */
1596 if (v.sig[0] & 7)
1597 break;
1599 do_multiply (&u, &v, ten);
1601 /* Stop if we're now >= 1. */
1602 if (REAL_EXP (&u) > 0)
1603 break;
1605 v = u;
1606 dec_exp -= 1;
1608 r = v;
1610 /* Find power of 10. Do this by multiplying in P=10**2**M when
1611 the current remainder is smaller than 1/P. Fill PTEN with the
1612 power of 10 that we compute. */
1613 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1616 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1617 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1619 if (do_compare (&v, ptenmtwo, 0) <= 0)
1621 do_multiply (&v, &v, ptentwo);
1622 do_multiply (&pten, &pten, ptentwo);
1623 dec_exp -= 1 << m;
1626 while (--m >= 0);
1628 /* Invert the positive power of 10 that we've collected so far. */
1629 do_divide (&pten, one, &pten);
1632 p = str;
1633 if (sign)
1634 *p++ = '-';
1635 first = p++;
1637 /* At this point, PTEN should contain the nearest power of 10 smaller
1638 than R, such that this division produces the first digit.
1640 Using a divide-step primitive that returns the complete integral
1641 remainder avoids the rounding error that would be produced if
1642 we were to use do_divide here and then simply multiply by 10 for
1643 each subsequent digit. */
1645 digit = rtd_divmod (&r, &pten);
1647 /* Be prepared for error in that division via underflow ... */
1648 if (digit == 0 && cmp_significand_0 (&r))
1650 /* Multiply by 10 and try again. */
1651 do_multiply (&r, &r, ten);
1652 digit = rtd_divmod (&r, &pten);
1653 dec_exp -= 1;
1654 gcc_assert (digit != 0);
1657 /* ... or overflow. */
1658 if (digit == 10)
1660 *p++ = '1';
1661 if (--digits > 0)
1662 *p++ = '0';
1663 dec_exp += 1;
1665 else
1667 gcc_assert (digit <= 10);
1668 *p++ = digit + '0';
1671 /* Generate subsequent digits. */
1672 while (--digits > 0)
1674 do_multiply (&r, &r, ten);
1675 digit = rtd_divmod (&r, &pten);
1676 *p++ = digit + '0';
1678 last = p;
1680 /* Generate one more digit with which to do rounding. */
1681 do_multiply (&r, &r, ten);
1682 digit = rtd_divmod (&r, &pten);
1684 /* Round the result. */
1685 if (fmt && fmt->round_towards_zero)
1687 /* If the format uses round towards zero when parsing the string
1688 back in, we need to always round away from zero here. */
1689 if (cmp_significand_0 (&r))
1690 digit++;
1691 round_up = digit > 0;
1693 else
1695 if (digit == 5)
1697 /* Round to nearest. If R is nonzero there are additional
1698 nonzero digits to be extracted. */
1699 if (cmp_significand_0 (&r))
1700 digit++;
1701 /* Round to even. */
1702 else if ((p[-1] - '0') & 1)
1703 digit++;
1706 round_up = digit > 5;
1709 if (round_up)
1711 while (p > first)
1713 digit = *--p;
1714 if (digit == '9')
1715 *p = '0';
1716 else
1718 *p = digit + 1;
1719 break;
1723 /* Carry out of the first digit. This means we had all 9's and
1724 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1725 if (p == first)
1727 first[1] = '1';
1728 dec_exp++;
1732 /* Insert the decimal point. */
1733 first[0] = first[1];
1734 first[1] = '.';
1736 /* If requested, drop trailing zeros. Never crop past "1.0". */
1737 if (crop_trailing_zeros)
1738 while (last > first + 3 && last[-1] == '0')
1739 last--;
1741 /* Append the exponent. */
1742 sprintf (last, "e%+d", dec_exp);
1744 #ifdef ENABLE_CHECKING
1745 /* Verify that we can read the original value back in. */
1746 if (mode != VOIDmode)
1748 real_from_string (&r, str);
1749 real_convert (&r, mode, &r);
1750 gcc_assert (real_identical (&r, r_orig));
1752 #endif
1755 /* Likewise, except always uses round-to-nearest. */
1757 void
1758 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1759 size_t digits, int crop_trailing_zeros)
1761 real_to_decimal_for_mode (str, r_orig, buf_size,
1762 digits, crop_trailing_zeros, VOIDmode);
1765 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1766 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1767 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1768 strip trailing zeros. */
1770 void
1771 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1772 size_t digits, int crop_trailing_zeros)
1774 int i, j, exp = REAL_EXP (r);
1775 char *p, *first;
1776 char exp_buf[16];
1777 size_t max_digits;
1779 switch (r->cl)
1781 case rvc_zero:
1782 exp = 0;
1783 break;
1784 case rvc_normal:
1785 break;
1786 case rvc_inf:
1787 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1788 return;
1789 case rvc_nan:
1790 /* ??? Print the significand as well, if not canonical? */
1791 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1792 (r->signalling ? 'S' : 'Q'));
1793 return;
1794 default:
1795 gcc_unreachable ();
1798 if (r->decimal)
1800 /* Hexadecimal format for decimal floats is not interesting. */
1801 strcpy (str, "N/A");
1802 return;
1805 if (digits == 0)
1806 digits = SIGNIFICAND_BITS / 4;
1808 /* Bound the number of digits printed by the size of the output buffer. */
1810 sprintf (exp_buf, "p%+d", exp);
1811 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1812 gcc_assert (max_digits <= buf_size);
1813 if (digits > max_digits)
1814 digits = max_digits;
1816 p = str;
1817 if (r->sign)
1818 *p++ = '-';
1819 *p++ = '0';
1820 *p++ = 'x';
1821 *p++ = '0';
1822 *p++ = '.';
1823 first = p;
1825 for (i = SIGSZ - 1; i >= 0; --i)
1826 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1828 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1829 if (--digits == 0)
1830 goto out;
1833 out:
1834 if (crop_trailing_zeros)
1835 while (p > first + 1 && p[-1] == '0')
1836 p--;
1838 sprintf (p, "p%+d", exp);
1841 /* Initialize R from a decimal or hexadecimal string. The string is
1842 assumed to have been syntax checked already. Return -1 if the
1843 value underflows, +1 if overflows, and 0 otherwise. */
1846 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1848 int exp = 0;
1849 bool sign = false;
1851 get_zero (r, 0);
1853 if (*str == '-')
1855 sign = true;
1856 str++;
1858 else if (*str == '+')
1859 str++;
1861 if (!strncmp (str, "QNaN", 4))
1863 get_canonical_qnan (r, sign);
1864 return 0;
1866 else if (!strncmp (str, "SNaN", 4))
1868 get_canonical_snan (r, sign);
1869 return 0;
1871 else if (!strncmp (str, "Inf", 3))
1873 get_inf (r, sign);
1874 return 0;
1877 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1879 /* Hexadecimal floating point. */
1880 int pos = SIGNIFICAND_BITS - 4, d;
1882 str += 2;
1884 while (*str == '0')
1885 str++;
1886 while (1)
1888 d = hex_value (*str);
1889 if (d == _hex_bad)
1890 break;
1891 if (pos >= 0)
1893 r->sig[pos / HOST_BITS_PER_LONG]
1894 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1895 pos -= 4;
1897 else if (d)
1898 /* Ensure correct rounding by setting last bit if there is
1899 a subsequent nonzero digit. */
1900 r->sig[0] |= 1;
1901 exp += 4;
1902 str++;
1904 if (*str == '.')
1906 str++;
1907 if (pos == SIGNIFICAND_BITS - 4)
1909 while (*str == '0')
1910 str++, exp -= 4;
1912 while (1)
1914 d = hex_value (*str);
1915 if (d == _hex_bad)
1916 break;
1917 if (pos >= 0)
1919 r->sig[pos / HOST_BITS_PER_LONG]
1920 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1921 pos -= 4;
1923 else if (d)
1924 /* Ensure correct rounding by setting last bit if there is
1925 a subsequent nonzero digit. */
1926 r->sig[0] |= 1;
1927 str++;
1931 /* If the mantissa is zero, ignore the exponent. */
1932 if (!cmp_significand_0 (r))
1933 goto is_a_zero;
1935 if (*str == 'p' || *str == 'P')
1937 bool exp_neg = false;
1939 str++;
1940 if (*str == '-')
1942 exp_neg = true;
1943 str++;
1945 else if (*str == '+')
1946 str++;
1948 d = 0;
1949 while (ISDIGIT (*str))
1951 d *= 10;
1952 d += *str - '0';
1953 if (d > MAX_EXP)
1955 /* Overflowed the exponent. */
1956 if (exp_neg)
1957 goto underflow;
1958 else
1959 goto overflow;
1961 str++;
1963 if (exp_neg)
1964 d = -d;
1966 exp += d;
1969 r->cl = rvc_normal;
1970 SET_REAL_EXP (r, exp);
1972 normalize (r);
1974 else
1976 /* Decimal floating point. */
1977 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1978 int d;
1980 while (*str == '0')
1981 str++;
1982 while (ISDIGIT (*str))
1984 d = *str++ - '0';
1985 do_multiply (r, r, ten);
1986 if (d)
1987 do_add (r, r, real_digit (d), 0);
1989 if (*str == '.')
1991 str++;
1992 if (r->cl == rvc_zero)
1994 while (*str == '0')
1995 str++, exp--;
1997 while (ISDIGIT (*str))
1999 d = *str++ - '0';
2000 do_multiply (r, r, ten);
2001 if (d)
2002 do_add (r, r, real_digit (d), 0);
2003 exp--;
2007 /* If the mantissa is zero, ignore the exponent. */
2008 if (r->cl == rvc_zero)
2009 goto is_a_zero;
2011 if (*str == 'e' || *str == 'E')
2013 bool exp_neg = false;
2015 str++;
2016 if (*str == '-')
2018 exp_neg = true;
2019 str++;
2021 else if (*str == '+')
2022 str++;
2024 d = 0;
2025 while (ISDIGIT (*str))
2027 d *= 10;
2028 d += *str - '0';
2029 if (d > MAX_EXP)
2031 /* Overflowed the exponent. */
2032 if (exp_neg)
2033 goto underflow;
2034 else
2035 goto overflow;
2037 str++;
2039 if (exp_neg)
2040 d = -d;
2041 exp += d;
2044 if (exp)
2045 times_pten (r, exp);
2048 r->sign = sign;
2049 return 0;
2051 is_a_zero:
2052 get_zero (r, sign);
2053 return 0;
2055 underflow:
2056 get_zero (r, sign);
2057 return -1;
2059 overflow:
2060 get_inf (r, sign);
2061 return 1;
2064 /* Legacy. Similar, but return the result directly. */
2066 REAL_VALUE_TYPE
2067 real_from_string2 (const char *s, enum machine_mode mode)
2069 REAL_VALUE_TYPE r;
2071 real_from_string (&r, s);
2072 if (mode != VOIDmode)
2073 real_convert (&r, mode, &r);
2075 return r;
2078 /* Initialize R from string S and desired MODE. */
2080 void
2081 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2083 if (DECIMAL_FLOAT_MODE_P (mode))
2084 decimal_real_from_string (r, s);
2085 else
2086 real_from_string (r, s);
2088 if (mode != VOIDmode)
2089 real_convert (r, mode, r);
2092 /* Initialize R from the integer pair HIGH+LOW. */
2094 void
2095 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2096 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2097 int unsigned_p)
2099 if (low == 0 && high == 0)
2100 get_zero (r, 0);
2101 else
2103 memset (r, 0, sizeof (*r));
2104 r->cl = rvc_normal;
2105 r->sign = high < 0 && !unsigned_p;
2106 SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT);
2108 if (r->sign)
2110 high = ~high;
2111 if (low == 0)
2112 high += 1;
2113 else
2114 low = -low;
2117 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2119 r->sig[SIGSZ-1] = high;
2120 r->sig[SIGSZ-2] = low;
2122 else
2124 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2125 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2126 r->sig[SIGSZ-2] = high;
2127 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2128 r->sig[SIGSZ-4] = low;
2131 normalize (r);
2134 if (mode != VOIDmode)
2135 real_convert (r, mode, r);
2138 /* Returns 10**2**N. */
2140 static const REAL_VALUE_TYPE *
2141 ten_to_ptwo (int n)
2143 static REAL_VALUE_TYPE tens[EXP_BITS];
2145 gcc_assert (n >= 0);
2146 gcc_assert (n < EXP_BITS);
2148 if (tens[n].cl == rvc_zero)
2150 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2152 HOST_WIDE_INT t = 10;
2153 int i;
2155 for (i = 0; i < n; ++i)
2156 t *= t;
2158 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2160 else
2162 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2163 do_multiply (&tens[n], t, t);
2167 return &tens[n];
2170 /* Returns 10**(-2**N). */
2172 static const REAL_VALUE_TYPE *
2173 ten_to_mptwo (int n)
2175 static REAL_VALUE_TYPE tens[EXP_BITS];
2177 gcc_assert (n >= 0);
2178 gcc_assert (n < EXP_BITS);
2180 if (tens[n].cl == rvc_zero)
2181 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2183 return &tens[n];
2186 /* Returns N. */
2188 static const REAL_VALUE_TYPE *
2189 real_digit (int n)
2191 static REAL_VALUE_TYPE num[10];
2193 gcc_assert (n >= 0);
2194 gcc_assert (n <= 9);
2196 if (n > 0 && num[n].cl == rvc_zero)
2197 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2199 return &num[n];
2202 /* Multiply R by 10**EXP. */
2204 static void
2205 times_pten (REAL_VALUE_TYPE *r, int exp)
2207 REAL_VALUE_TYPE pten, *rr;
2208 bool negative = (exp < 0);
2209 int i;
2211 if (negative)
2213 exp = -exp;
2214 pten = *real_digit (1);
2215 rr = &pten;
2217 else
2218 rr = r;
2220 for (i = 0; exp > 0; ++i, exp >>= 1)
2221 if (exp & 1)
2222 do_multiply (rr, rr, ten_to_ptwo (i));
2224 if (negative)
2225 do_divide (r, r, &pten);
2228 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2230 const REAL_VALUE_TYPE *
2231 dconst_e_ptr (void)
2233 static REAL_VALUE_TYPE value;
2235 /* Initialize mathematical constants for constant folding builtins.
2236 These constants need to be given to at least 160 bits precision. */
2237 if (value.cl == rvc_zero)
2239 mpfr_t m;
2240 mpfr_init2 (m, SIGNIFICAND_BITS);
2241 mpfr_set_ui (m, 1, GMP_RNDN);
2242 mpfr_exp (m, m, GMP_RNDN);
2243 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2244 mpfr_clear (m);
2247 return &value;
2250 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2252 const REAL_VALUE_TYPE *
2253 dconst_third_ptr (void)
2255 static REAL_VALUE_TYPE value;
2257 /* Initialize mathematical constants for constant folding builtins.
2258 These constants need to be given to at least 160 bits precision. */
2259 if (value.cl == rvc_zero)
2261 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3));
2263 return &value;
2266 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2268 const REAL_VALUE_TYPE *
2269 dconst_sqrt2_ptr (void)
2271 static REAL_VALUE_TYPE value;
2273 /* Initialize mathematical constants for constant folding builtins.
2274 These constants need to be given to at least 160 bits precision. */
2275 if (value.cl == rvc_zero)
2277 mpfr_t m;
2278 mpfr_init2 (m, SIGNIFICAND_BITS);
2279 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2280 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2281 mpfr_clear (m);
2283 return &value;
2286 /* Fills R with +Inf. */
2288 void
2289 real_inf (REAL_VALUE_TYPE *r)
2291 get_inf (r, 0);
2294 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2295 we force a QNaN, else we force an SNaN. The string, if not empty,
2296 is parsed as a number and placed in the significand. Return true
2297 if the string was successfully parsed. */
2299 bool
2300 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2301 enum machine_mode mode)
2303 const struct real_format *fmt;
2305 fmt = REAL_MODE_FORMAT (mode);
2306 gcc_assert (fmt);
2308 if (*str == 0)
2310 if (quiet)
2311 get_canonical_qnan (r, 0);
2312 else
2313 get_canonical_snan (r, 0);
2315 else
2317 int base = 10, d;
2319 memset (r, 0, sizeof (*r));
2320 r->cl = rvc_nan;
2322 /* Parse akin to strtol into the significand of R. */
2324 while (ISSPACE (*str))
2325 str++;
2326 if (*str == '-')
2327 str++;
2328 else if (*str == '+')
2329 str++;
2330 if (*str == '0')
2332 str++;
2333 if (*str == 'x' || *str == 'X')
2335 base = 16;
2336 str++;
2338 else
2339 base = 8;
2342 while ((d = hex_value (*str)) < base)
2344 REAL_VALUE_TYPE u;
2346 switch (base)
2348 case 8:
2349 lshift_significand (r, r, 3);
2350 break;
2351 case 16:
2352 lshift_significand (r, r, 4);
2353 break;
2354 case 10:
2355 lshift_significand_1 (&u, r);
2356 lshift_significand (r, r, 3);
2357 add_significands (r, r, &u);
2358 break;
2359 default:
2360 gcc_unreachable ();
2363 get_zero (&u, 0);
2364 u.sig[0] = d;
2365 add_significands (r, r, &u);
2367 str++;
2370 /* Must have consumed the entire string for success. */
2371 if (*str != 0)
2372 return false;
2374 /* Shift the significand into place such that the bits
2375 are in the most significant bits for the format. */
2376 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2378 /* Our MSB is always unset for NaNs. */
2379 r->sig[SIGSZ-1] &= ~SIG_MSB;
2381 /* Force quiet or signalling NaN. */
2382 r->signalling = !quiet;
2385 return true;
2388 /* Fills R with the largest finite value representable in mode MODE.
2389 If SIGN is nonzero, R is set to the most negative finite value. */
2391 void
2392 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2394 const struct real_format *fmt;
2395 int np2;
2397 fmt = REAL_MODE_FORMAT (mode);
2398 gcc_assert (fmt);
2399 memset (r, 0, sizeof (*r));
2401 if (fmt->b == 10)
2402 decimal_real_maxval (r, sign, mode);
2403 else
2405 r->cl = rvc_normal;
2406 r->sign = sign;
2407 SET_REAL_EXP (r, fmt->emax);
2409 np2 = SIGNIFICAND_BITS - fmt->p;
2410 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2411 clear_significand_below (r, np2);
2413 if (fmt->pnan < fmt->p)
2414 /* This is an IBM extended double format made up of two IEEE
2415 doubles. The value of the long double is the sum of the
2416 values of the two parts. The most significant part is
2417 required to be the value of the long double rounded to the
2418 nearest double. Rounding means we need a slightly smaller
2419 value for LDBL_MAX. */
2420 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2424 /* Fills R with 2**N. */
2426 void
2427 real_2expN (REAL_VALUE_TYPE *r, int n, enum machine_mode fmode)
2429 memset (r, 0, sizeof (*r));
2431 n++;
2432 if (n > MAX_EXP)
2433 r->cl = rvc_inf;
2434 else if (n < -MAX_EXP)
2436 else
2438 r->cl = rvc_normal;
2439 SET_REAL_EXP (r, n);
2440 r->sig[SIGSZ-1] = SIG_MSB;
2442 if (DECIMAL_FLOAT_MODE_P (fmode))
2443 decimal_real_convert (r, fmode, r);
2447 static void
2448 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2450 int p2, np2, i, w;
2451 int emin2m1, emax2;
2452 bool round_up = false;
2454 if (r->decimal)
2456 if (fmt->b == 10)
2458 decimal_round_for_format (fmt, r);
2459 return;
2461 /* FIXME. We can come here via fp_easy_constant
2462 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2463 investigated whether this convert needs to be here, or
2464 something else is missing. */
2465 decimal_real_convert (r, DFmode, r);
2468 p2 = fmt->p;
2469 emin2m1 = fmt->emin - 1;
2470 emax2 = fmt->emax;
2472 np2 = SIGNIFICAND_BITS - p2;
2473 switch (r->cl)
2475 underflow:
2476 get_zero (r, r->sign);
2477 case rvc_zero:
2478 if (!fmt->has_signed_zero)
2479 r->sign = 0;
2480 return;
2482 overflow:
2483 get_inf (r, r->sign);
2484 case rvc_inf:
2485 return;
2487 case rvc_nan:
2488 clear_significand_below (r, np2);
2489 return;
2491 case rvc_normal:
2492 break;
2494 default:
2495 gcc_unreachable ();
2498 /* Check the range of the exponent. If we're out of range,
2499 either underflow or overflow. */
2500 if (REAL_EXP (r) > emax2)
2501 goto overflow;
2502 else if (REAL_EXP (r) <= emin2m1)
2504 int diff;
2506 if (!fmt->has_denorm)
2508 /* Don't underflow completely until we've had a chance to round. */
2509 if (REAL_EXP (r) < emin2m1)
2510 goto underflow;
2512 else
2514 diff = emin2m1 - REAL_EXP (r) + 1;
2515 if (diff > p2)
2516 goto underflow;
2518 /* De-normalize the significand. */
2519 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2520 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2524 if (!fmt->round_towards_zero)
2526 /* There are P2 true significand bits, followed by one guard bit,
2527 followed by one sticky bit, followed by stuff. Fold nonzero
2528 stuff into the sticky bit. */
2529 unsigned long sticky;
2530 bool guard, lsb;
2532 sticky = 0;
2533 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2534 sticky |= r->sig[i];
2535 sticky |= r->sig[w]
2536 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2538 guard = test_significand_bit (r, np2 - 1);
2539 lsb = test_significand_bit (r, np2);
2541 /* Round to even. */
2542 round_up = guard && (sticky || lsb);
2545 if (round_up)
2547 REAL_VALUE_TYPE u;
2548 get_zero (&u, 0);
2549 set_significand_bit (&u, np2);
2551 if (add_significands (r, r, &u))
2553 /* Overflow. Means the significand had been all ones, and
2554 is now all zeros. Need to increase the exponent, and
2555 possibly re-normalize it. */
2556 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2557 if (REAL_EXP (r) > emax2)
2558 goto overflow;
2559 r->sig[SIGSZ-1] = SIG_MSB;
2563 /* Catch underflow that we deferred until after rounding. */
2564 if (REAL_EXP (r) <= emin2m1)
2565 goto underflow;
2567 /* Clear out trailing garbage. */
2568 clear_significand_below (r, np2);
2571 /* Extend or truncate to a new mode. */
2573 void
2574 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2575 const REAL_VALUE_TYPE *a)
2577 const struct real_format *fmt;
2579 fmt = REAL_MODE_FORMAT (mode);
2580 gcc_assert (fmt);
2582 *r = *a;
2584 if (a->decimal || fmt->b == 10)
2585 decimal_real_convert (r, mode, a);
2587 round_for_format (fmt, r);
2589 /* round_for_format de-normalizes denormals. Undo just that part. */
2590 if (r->cl == rvc_normal)
2591 normalize (r);
2594 /* Legacy. Likewise, except return the struct directly. */
2596 REAL_VALUE_TYPE
2597 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2599 REAL_VALUE_TYPE r;
2600 real_convert (&r, mode, &a);
2601 return r;
2604 /* Return true if truncating to MODE is exact. */
2606 bool
2607 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2609 const struct real_format *fmt;
2610 REAL_VALUE_TYPE t;
2611 int emin2m1;
2613 fmt = REAL_MODE_FORMAT (mode);
2614 gcc_assert (fmt);
2616 /* Don't allow conversion to denormals. */
2617 emin2m1 = fmt->emin - 1;
2618 if (REAL_EXP (a) <= emin2m1)
2619 return false;
2621 /* After conversion to the new mode, the value must be identical. */
2622 real_convert (&t, mode, a);
2623 return real_identical (&t, a);
2626 /* Write R to the given target format. Place the words of the result
2627 in target word order in BUF. There are always 32 bits in each
2628 long, no matter the size of the host long.
2630 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2632 long
2633 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2634 const struct real_format *fmt)
2636 REAL_VALUE_TYPE r;
2637 long buf1;
2639 r = *r_orig;
2640 round_for_format (fmt, &r);
2642 if (!buf)
2643 buf = &buf1;
2644 (*fmt->encode) (fmt, buf, &r);
2646 return *buf;
2649 /* Similar, but look up the format from MODE. */
2651 long
2652 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2654 const struct real_format *fmt;
2656 fmt = REAL_MODE_FORMAT (mode);
2657 gcc_assert (fmt);
2659 return real_to_target_fmt (buf, r, fmt);
2662 /* Read R from the given target format. Read the words of the result
2663 in target word order in BUF. There are always 32 bits in each
2664 long, no matter the size of the host long. */
2666 void
2667 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2668 const struct real_format *fmt)
2670 (*fmt->decode) (fmt, r, buf);
2673 /* Similar, but look up the format from MODE. */
2675 void
2676 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2678 const struct real_format *fmt;
2680 fmt = REAL_MODE_FORMAT (mode);
2681 gcc_assert (fmt);
2683 (*fmt->decode) (fmt, r, buf);
2686 /* Return the number of bits of the largest binary value that the
2687 significand of MODE will hold. */
2688 /* ??? Legacy. Should get access to real_format directly. */
2691 significand_size (enum machine_mode mode)
2693 const struct real_format *fmt;
2695 fmt = REAL_MODE_FORMAT (mode);
2696 if (fmt == NULL)
2697 return 0;
2699 if (fmt->b == 10)
2701 /* Return the size in bits of the largest binary value that can be
2702 held by the decimal coefficient for this mode. This is one more
2703 than the number of bits required to hold the largest coefficient
2704 of this mode. */
2705 double log2_10 = 3.3219281;
2706 return fmt->p * log2_10;
2708 return fmt->p;
2711 /* Return a hash value for the given real value. */
2712 /* ??? The "unsigned int" return value is intended to be hashval_t,
2713 but I didn't want to pull hashtab.h into real.h. */
2715 unsigned int
2716 real_hash (const REAL_VALUE_TYPE *r)
2718 unsigned int h;
2719 size_t i;
2721 h = r->cl | (r->sign << 2);
2722 switch (r->cl)
2724 case rvc_zero:
2725 case rvc_inf:
2726 return h;
2728 case rvc_normal:
2729 h |= REAL_EXP (r) << 3;
2730 break;
2732 case rvc_nan:
2733 if (r->signalling)
2734 h ^= (unsigned int)-1;
2735 if (r->canonical)
2736 return h;
2737 break;
2739 default:
2740 gcc_unreachable ();
2743 if (sizeof(unsigned long) > sizeof(unsigned int))
2744 for (i = 0; i < SIGSZ; ++i)
2746 unsigned long s = r->sig[i];
2747 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2749 else
2750 for (i = 0; i < SIGSZ; ++i)
2751 h ^= r->sig[i];
2753 return h;
2756 /* IEEE single-precision format. */
2758 static void encode_ieee_single (const struct real_format *fmt,
2759 long *, const REAL_VALUE_TYPE *);
2760 static void decode_ieee_single (const struct real_format *,
2761 REAL_VALUE_TYPE *, const long *);
2763 static void
2764 encode_ieee_single (const struct real_format *fmt, long *buf,
2765 const REAL_VALUE_TYPE *r)
2767 unsigned long image, sig, exp;
2768 unsigned long sign = r->sign;
2769 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2771 image = sign << 31;
2772 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2774 switch (r->cl)
2776 case rvc_zero:
2777 break;
2779 case rvc_inf:
2780 if (fmt->has_inf)
2781 image |= 255 << 23;
2782 else
2783 image |= 0x7fffffff;
2784 break;
2786 case rvc_nan:
2787 if (fmt->has_nans)
2789 if (r->canonical)
2790 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2791 if (r->signalling == fmt->qnan_msb_set)
2792 sig &= ~(1 << 22);
2793 else
2794 sig |= 1 << 22;
2795 if (sig == 0)
2796 sig = 1 << 21;
2798 image |= 255 << 23;
2799 image |= sig;
2801 else
2802 image |= 0x7fffffff;
2803 break;
2805 case rvc_normal:
2806 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2807 whereas the intermediate representation is 0.F x 2**exp.
2808 Which means we're off by one. */
2809 if (denormal)
2810 exp = 0;
2811 else
2812 exp = REAL_EXP (r) + 127 - 1;
2813 image |= exp << 23;
2814 image |= sig;
2815 break;
2817 default:
2818 gcc_unreachable ();
2821 buf[0] = image;
2824 static void
2825 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2826 const long *buf)
2828 unsigned long image = buf[0] & 0xffffffff;
2829 bool sign = (image >> 31) & 1;
2830 int exp = (image >> 23) & 0xff;
2832 memset (r, 0, sizeof (*r));
2833 image <<= HOST_BITS_PER_LONG - 24;
2834 image &= ~SIG_MSB;
2836 if (exp == 0)
2838 if (image && fmt->has_denorm)
2840 r->cl = rvc_normal;
2841 r->sign = sign;
2842 SET_REAL_EXP (r, -126);
2843 r->sig[SIGSZ-1] = image << 1;
2844 normalize (r);
2846 else if (fmt->has_signed_zero)
2847 r->sign = sign;
2849 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2851 if (image)
2853 r->cl = rvc_nan;
2854 r->sign = sign;
2855 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2856 ^ fmt->qnan_msb_set);
2857 r->sig[SIGSZ-1] = image;
2859 else
2861 r->cl = rvc_inf;
2862 r->sign = sign;
2865 else
2867 r->cl = rvc_normal;
2868 r->sign = sign;
2869 SET_REAL_EXP (r, exp - 127 + 1);
2870 r->sig[SIGSZ-1] = image | SIG_MSB;
2874 const struct real_format ieee_single_format =
2876 encode_ieee_single,
2877 decode_ieee_single,
2881 -125,
2882 128,
2885 false,
2886 true,
2887 true,
2888 true,
2889 true,
2890 true,
2891 true,
2892 false
2895 const struct real_format mips_single_format =
2897 encode_ieee_single,
2898 decode_ieee_single,
2902 -125,
2903 128,
2906 false,
2907 true,
2908 true,
2909 true,
2910 true,
2911 true,
2912 false,
2913 true
2916 const struct real_format motorola_single_format =
2918 encode_ieee_single,
2919 decode_ieee_single,
2923 -125,
2924 128,
2927 false,
2928 true,
2929 true,
2930 true,
2931 true,
2932 true,
2933 true,
2934 true
2937 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
2938 single precision with the following differences:
2939 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
2940 are generated.
2941 - NaNs are not supported.
2942 - The range of non-zero numbers in binary is
2943 (001)[1.]000...000 to (255)[1.]111...111.
2944 - Denormals can be represented, but are treated as +0.0 when
2945 used as an operand and are never generated as a result.
2946 - -0.0 can be represented, but a zero result is always +0.0.
2947 - the only supported rounding mode is trunction (towards zero). */
2948 const struct real_format spu_single_format =
2950 encode_ieee_single,
2951 decode_ieee_single,
2955 -125,
2956 129,
2959 true,
2960 false,
2961 false,
2962 false,
2963 true,
2964 true,
2965 false,
2966 false
2969 /* IEEE double-precision format. */
2971 static void encode_ieee_double (const struct real_format *fmt,
2972 long *, const REAL_VALUE_TYPE *);
2973 static void decode_ieee_double (const struct real_format *,
2974 REAL_VALUE_TYPE *, const long *);
2976 static void
2977 encode_ieee_double (const struct real_format *fmt, long *buf,
2978 const REAL_VALUE_TYPE *r)
2980 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2981 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2983 image_hi = r->sign << 31;
2984 image_lo = 0;
2986 if (HOST_BITS_PER_LONG == 64)
2988 sig_hi = r->sig[SIGSZ-1];
2989 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2990 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2992 else
2994 sig_hi = r->sig[SIGSZ-1];
2995 sig_lo = r->sig[SIGSZ-2];
2996 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2997 sig_hi = (sig_hi >> 11) & 0xfffff;
3000 switch (r->cl)
3002 case rvc_zero:
3003 break;
3005 case rvc_inf:
3006 if (fmt->has_inf)
3007 image_hi |= 2047 << 20;
3008 else
3010 image_hi |= 0x7fffffff;
3011 image_lo = 0xffffffff;
3013 break;
3015 case rvc_nan:
3016 if (fmt->has_nans)
3018 if (r->canonical)
3020 if (fmt->canonical_nan_lsbs_set)
3022 sig_hi = (1 << 19) - 1;
3023 sig_lo = 0xffffffff;
3025 else
3027 sig_hi = 0;
3028 sig_lo = 0;
3031 if (r->signalling == fmt->qnan_msb_set)
3032 sig_hi &= ~(1 << 19);
3033 else
3034 sig_hi |= 1 << 19;
3035 if (sig_hi == 0 && sig_lo == 0)
3036 sig_hi = 1 << 18;
3038 image_hi |= 2047 << 20;
3039 image_hi |= sig_hi;
3040 image_lo = sig_lo;
3042 else
3044 image_hi |= 0x7fffffff;
3045 image_lo = 0xffffffff;
3047 break;
3049 case rvc_normal:
3050 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3051 whereas the intermediate representation is 0.F x 2**exp.
3052 Which means we're off by one. */
3053 if (denormal)
3054 exp = 0;
3055 else
3056 exp = REAL_EXP (r) + 1023 - 1;
3057 image_hi |= exp << 20;
3058 image_hi |= sig_hi;
3059 image_lo = sig_lo;
3060 break;
3062 default:
3063 gcc_unreachable ();
3066 if (FLOAT_WORDS_BIG_ENDIAN)
3067 buf[0] = image_hi, buf[1] = image_lo;
3068 else
3069 buf[0] = image_lo, buf[1] = image_hi;
3072 static void
3073 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3074 const long *buf)
3076 unsigned long image_hi, image_lo;
3077 bool sign;
3078 int exp;
3080 if (FLOAT_WORDS_BIG_ENDIAN)
3081 image_hi = buf[0], image_lo = buf[1];
3082 else
3083 image_lo = buf[0], image_hi = buf[1];
3084 image_lo &= 0xffffffff;
3085 image_hi &= 0xffffffff;
3087 sign = (image_hi >> 31) & 1;
3088 exp = (image_hi >> 20) & 0x7ff;
3090 memset (r, 0, sizeof (*r));
3092 image_hi <<= 32 - 21;
3093 image_hi |= image_lo >> 21;
3094 image_hi &= 0x7fffffff;
3095 image_lo <<= 32 - 21;
3097 if (exp == 0)
3099 if ((image_hi || image_lo) && fmt->has_denorm)
3101 r->cl = rvc_normal;
3102 r->sign = sign;
3103 SET_REAL_EXP (r, -1022);
3104 if (HOST_BITS_PER_LONG == 32)
3106 image_hi = (image_hi << 1) | (image_lo >> 31);
3107 image_lo <<= 1;
3108 r->sig[SIGSZ-1] = image_hi;
3109 r->sig[SIGSZ-2] = image_lo;
3111 else
3113 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3114 r->sig[SIGSZ-1] = image_hi;
3116 normalize (r);
3118 else if (fmt->has_signed_zero)
3119 r->sign = sign;
3121 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3123 if (image_hi || image_lo)
3125 r->cl = rvc_nan;
3126 r->sign = sign;
3127 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3128 if (HOST_BITS_PER_LONG == 32)
3130 r->sig[SIGSZ-1] = image_hi;
3131 r->sig[SIGSZ-2] = image_lo;
3133 else
3134 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3136 else
3138 r->cl = rvc_inf;
3139 r->sign = sign;
3142 else
3144 r->cl = rvc_normal;
3145 r->sign = sign;
3146 SET_REAL_EXP (r, exp - 1023 + 1);
3147 if (HOST_BITS_PER_LONG == 32)
3149 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3150 r->sig[SIGSZ-2] = image_lo;
3152 else
3153 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3157 const struct real_format ieee_double_format =
3159 encode_ieee_double,
3160 decode_ieee_double,
3164 -1021,
3165 1024,
3168 false,
3169 true,
3170 true,
3171 true,
3172 true,
3173 true,
3174 true,
3175 false
3178 const struct real_format mips_double_format =
3180 encode_ieee_double,
3181 decode_ieee_double,
3185 -1021,
3186 1024,
3189 false,
3190 true,
3191 true,
3192 true,
3193 true,
3194 true,
3195 false,
3196 true
3199 const struct real_format motorola_double_format =
3201 encode_ieee_double,
3202 decode_ieee_double,
3206 -1021,
3207 1024,
3210 false,
3211 true,
3212 true,
3213 true,
3214 true,
3215 true,
3216 true,
3217 true
3220 /* IEEE extended real format. This comes in three flavors: Intel's as
3221 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3222 12- and 16-byte images may be big- or little endian; Motorola's is
3223 always big endian. */
3225 /* Helper subroutine which converts from the internal format to the
3226 12-byte little-endian Intel format. Functions below adjust this
3227 for the other possible formats. */
3228 static void
3229 encode_ieee_extended (const struct real_format *fmt, long *buf,
3230 const REAL_VALUE_TYPE *r)
3232 unsigned long image_hi, sig_hi, sig_lo;
3233 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3235 image_hi = r->sign << 15;
3236 sig_hi = sig_lo = 0;
3238 switch (r->cl)
3240 case rvc_zero:
3241 break;
3243 case rvc_inf:
3244 if (fmt->has_inf)
3246 image_hi |= 32767;
3248 /* Intel requires the explicit integer bit to be set, otherwise
3249 it considers the value a "pseudo-infinity". Motorola docs
3250 say it doesn't care. */
3251 sig_hi = 0x80000000;
3253 else
3255 image_hi |= 32767;
3256 sig_lo = sig_hi = 0xffffffff;
3258 break;
3260 case rvc_nan:
3261 if (fmt->has_nans)
3263 image_hi |= 32767;
3264 if (r->canonical)
3266 if (fmt->canonical_nan_lsbs_set)
3268 sig_hi = (1 << 30) - 1;
3269 sig_lo = 0xffffffff;
3272 else if (HOST_BITS_PER_LONG == 32)
3274 sig_hi = r->sig[SIGSZ-1];
3275 sig_lo = r->sig[SIGSZ-2];
3277 else
3279 sig_lo = r->sig[SIGSZ-1];
3280 sig_hi = sig_lo >> 31 >> 1;
3281 sig_lo &= 0xffffffff;
3283 if (r->signalling == fmt->qnan_msb_set)
3284 sig_hi &= ~(1 << 30);
3285 else
3286 sig_hi |= 1 << 30;
3287 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3288 sig_hi = 1 << 29;
3290 /* Intel requires the explicit integer bit to be set, otherwise
3291 it considers the value a "pseudo-nan". Motorola docs say it
3292 doesn't care. */
3293 sig_hi |= 0x80000000;
3295 else
3297 image_hi |= 32767;
3298 sig_lo = sig_hi = 0xffffffff;
3300 break;
3302 case rvc_normal:
3304 int exp = REAL_EXP (r);
3306 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3307 whereas the intermediate representation is 0.F x 2**exp.
3308 Which means we're off by one.
3310 Except for Motorola, which consider exp=0 and explicit
3311 integer bit set to continue to be normalized. In theory
3312 this discrepancy has been taken care of by the difference
3313 in fmt->emin in round_for_format. */
3315 if (denormal)
3316 exp = 0;
3317 else
3319 exp += 16383 - 1;
3320 gcc_assert (exp >= 0);
3322 image_hi |= exp;
3324 if (HOST_BITS_PER_LONG == 32)
3326 sig_hi = r->sig[SIGSZ-1];
3327 sig_lo = r->sig[SIGSZ-2];
3329 else
3331 sig_lo = r->sig[SIGSZ-1];
3332 sig_hi = sig_lo >> 31 >> 1;
3333 sig_lo &= 0xffffffff;
3336 break;
3338 default:
3339 gcc_unreachable ();
3342 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3345 /* Convert from the internal format to the 12-byte Motorola format
3346 for an IEEE extended real. */
3347 static void
3348 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3349 const REAL_VALUE_TYPE *r)
3351 long intermed[3];
3352 encode_ieee_extended (fmt, intermed, r);
3354 /* Motorola chips are assumed always to be big-endian. Also, the
3355 padding in a Motorola extended real goes between the exponent and
3356 the mantissa. At this point the mantissa is entirely within
3357 elements 0 and 1 of intermed, and the exponent entirely within
3358 element 2, so all we have to do is swap the order around, and
3359 shift element 2 left 16 bits. */
3360 buf[0] = intermed[2] << 16;
3361 buf[1] = intermed[1];
3362 buf[2] = intermed[0];
3365 /* Convert from the internal format to the 12-byte Intel format for
3366 an IEEE extended real. */
3367 static void
3368 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3369 const REAL_VALUE_TYPE *r)
3371 if (FLOAT_WORDS_BIG_ENDIAN)
3373 /* All the padding in an Intel-format extended real goes at the high
3374 end, which in this case is after the mantissa, not the exponent.
3375 Therefore we must shift everything down 16 bits. */
3376 long intermed[3];
3377 encode_ieee_extended (fmt, intermed, r);
3378 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3379 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3380 buf[2] = (intermed[0] << 16);
3382 else
3383 /* encode_ieee_extended produces what we want directly. */
3384 encode_ieee_extended (fmt, buf, r);
3387 /* Convert from the internal format to the 16-byte Intel format for
3388 an IEEE extended real. */
3389 static void
3390 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3391 const REAL_VALUE_TYPE *r)
3393 /* All the padding in an Intel-format extended real goes at the high end. */
3394 encode_ieee_extended_intel_96 (fmt, buf, r);
3395 buf[3] = 0;
3398 /* As above, we have a helper function which converts from 12-byte
3399 little-endian Intel format to internal format. Functions below
3400 adjust for the other possible formats. */
3401 static void
3402 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3403 const long *buf)
3405 unsigned long image_hi, sig_hi, sig_lo;
3406 bool sign;
3407 int exp;
3409 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3410 sig_lo &= 0xffffffff;
3411 sig_hi &= 0xffffffff;
3412 image_hi &= 0xffffffff;
3414 sign = (image_hi >> 15) & 1;
3415 exp = image_hi & 0x7fff;
3417 memset (r, 0, sizeof (*r));
3419 if (exp == 0)
3421 if ((sig_hi || sig_lo) && fmt->has_denorm)
3423 r->cl = rvc_normal;
3424 r->sign = sign;
3426 /* When the IEEE format contains a hidden bit, we know that
3427 it's zero at this point, and so shift up the significand
3428 and decrease the exponent to match. In this case, Motorola
3429 defines the explicit integer bit to be valid, so we don't
3430 know whether the msb is set or not. */
3431 SET_REAL_EXP (r, fmt->emin);
3432 if (HOST_BITS_PER_LONG == 32)
3434 r->sig[SIGSZ-1] = sig_hi;
3435 r->sig[SIGSZ-2] = sig_lo;
3437 else
3438 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3440 normalize (r);
3442 else if (fmt->has_signed_zero)
3443 r->sign = sign;
3445 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3447 /* See above re "pseudo-infinities" and "pseudo-nans".
3448 Short summary is that the MSB will likely always be
3449 set, and that we don't care about it. */
3450 sig_hi &= 0x7fffffff;
3452 if (sig_hi || sig_lo)
3454 r->cl = rvc_nan;
3455 r->sign = sign;
3456 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3457 if (HOST_BITS_PER_LONG == 32)
3459 r->sig[SIGSZ-1] = sig_hi;
3460 r->sig[SIGSZ-2] = sig_lo;
3462 else
3463 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3465 else
3467 r->cl = rvc_inf;
3468 r->sign = sign;
3471 else
3473 r->cl = rvc_normal;
3474 r->sign = sign;
3475 SET_REAL_EXP (r, exp - 16383 + 1);
3476 if (HOST_BITS_PER_LONG == 32)
3478 r->sig[SIGSZ-1] = sig_hi;
3479 r->sig[SIGSZ-2] = sig_lo;
3481 else
3482 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3486 /* Convert from the internal format to the 12-byte Motorola format
3487 for an IEEE extended real. */
3488 static void
3489 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3490 const long *buf)
3492 long intermed[3];
3494 /* Motorola chips are assumed always to be big-endian. Also, the
3495 padding in a Motorola extended real goes between the exponent and
3496 the mantissa; remove it. */
3497 intermed[0] = buf[2];
3498 intermed[1] = buf[1];
3499 intermed[2] = (unsigned long)buf[0] >> 16;
3501 decode_ieee_extended (fmt, r, intermed);
3504 /* Convert from the internal format to the 12-byte Intel format for
3505 an IEEE extended real. */
3506 static void
3507 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3508 const long *buf)
3510 if (FLOAT_WORDS_BIG_ENDIAN)
3512 /* All the padding in an Intel-format extended real goes at the high
3513 end, which in this case is after the mantissa, not the exponent.
3514 Therefore we must shift everything up 16 bits. */
3515 long intermed[3];
3517 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3518 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3519 intermed[2] = ((unsigned long)buf[0] >> 16);
3521 decode_ieee_extended (fmt, r, intermed);
3523 else
3524 /* decode_ieee_extended produces what we want directly. */
3525 decode_ieee_extended (fmt, r, buf);
3528 /* Convert from the internal format to the 16-byte Intel format for
3529 an IEEE extended real. */
3530 static void
3531 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3532 const long *buf)
3534 /* All the padding in an Intel-format extended real goes at the high end. */
3535 decode_ieee_extended_intel_96 (fmt, r, buf);
3538 const struct real_format ieee_extended_motorola_format =
3540 encode_ieee_extended_motorola,
3541 decode_ieee_extended_motorola,
3545 -16382,
3546 16384,
3549 false,
3550 true,
3551 true,
3552 true,
3553 true,
3554 true,
3555 true,
3556 true
3559 const struct real_format ieee_extended_intel_96_format =
3561 encode_ieee_extended_intel_96,
3562 decode_ieee_extended_intel_96,
3566 -16381,
3567 16384,
3570 false,
3571 true,
3572 true,
3573 true,
3574 true,
3575 true,
3576 true,
3577 false
3580 const struct real_format ieee_extended_intel_128_format =
3582 encode_ieee_extended_intel_128,
3583 decode_ieee_extended_intel_128,
3587 -16381,
3588 16384,
3591 false,
3592 true,
3593 true,
3594 true,
3595 true,
3596 true,
3597 true,
3598 false
3601 /* The following caters to i386 systems that set the rounding precision
3602 to 53 bits instead of 64, e.g. FreeBSD. */
3603 const struct real_format ieee_extended_intel_96_round_53_format =
3605 encode_ieee_extended_intel_96,
3606 decode_ieee_extended_intel_96,
3610 -16381,
3611 16384,
3614 false,
3615 true,
3616 true,
3617 true,
3618 true,
3619 true,
3620 true,
3621 false
3624 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3625 numbers whose sum is equal to the extended precision value. The number
3626 with greater magnitude is first. This format has the same magnitude
3627 range as an IEEE double precision value, but effectively 106 bits of
3628 significand precision. Infinity and NaN are represented by their IEEE
3629 double precision value stored in the first number, the second number is
3630 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3632 static void encode_ibm_extended (const struct real_format *fmt,
3633 long *, const REAL_VALUE_TYPE *);
3634 static void decode_ibm_extended (const struct real_format *,
3635 REAL_VALUE_TYPE *, const long *);
3637 static void
3638 encode_ibm_extended (const struct real_format *fmt, long *buf,
3639 const REAL_VALUE_TYPE *r)
3641 REAL_VALUE_TYPE u, normr, v;
3642 const struct real_format *base_fmt;
3644 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3646 /* Renormalize R before doing any arithmetic on it. */
3647 normr = *r;
3648 if (normr.cl == rvc_normal)
3649 normalize (&normr);
3651 /* u = IEEE double precision portion of significand. */
3652 u = normr;
3653 round_for_format (base_fmt, &u);
3654 encode_ieee_double (base_fmt, &buf[0], &u);
3656 if (u.cl == rvc_normal)
3658 do_add (&v, &normr, &u, 1);
3659 /* Call round_for_format since we might need to denormalize. */
3660 round_for_format (base_fmt, &v);
3661 encode_ieee_double (base_fmt, &buf[2], &v);
3663 else
3665 /* Inf, NaN, 0 are all representable as doubles, so the
3666 least-significant part can be 0.0. */
3667 buf[2] = 0;
3668 buf[3] = 0;
3672 static void
3673 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3674 const long *buf)
3676 REAL_VALUE_TYPE u, v;
3677 const struct real_format *base_fmt;
3679 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3680 decode_ieee_double (base_fmt, &u, &buf[0]);
3682 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3684 decode_ieee_double (base_fmt, &v, &buf[2]);
3685 do_add (r, &u, &v, 0);
3687 else
3688 *r = u;
3691 const struct real_format ibm_extended_format =
3693 encode_ibm_extended,
3694 decode_ibm_extended,
3696 53 + 53,
3698 -1021 + 53,
3699 1024,
3700 127,
3702 false,
3703 true,
3704 true,
3705 true,
3706 true,
3707 true,
3708 true,
3709 false
3712 const struct real_format mips_extended_format =
3714 encode_ibm_extended,
3715 decode_ibm_extended,
3717 53 + 53,
3719 -1021 + 53,
3720 1024,
3721 127,
3723 false,
3724 true,
3725 true,
3726 true,
3727 true,
3728 true,
3729 false,
3730 true
3734 /* IEEE quad precision format. */
3736 static void encode_ieee_quad (const struct real_format *fmt,
3737 long *, const REAL_VALUE_TYPE *);
3738 static void decode_ieee_quad (const struct real_format *,
3739 REAL_VALUE_TYPE *, const long *);
3741 static void
3742 encode_ieee_quad (const struct real_format *fmt, long *buf,
3743 const REAL_VALUE_TYPE *r)
3745 unsigned long image3, image2, image1, image0, exp;
3746 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3747 REAL_VALUE_TYPE u;
3749 image3 = r->sign << 31;
3750 image2 = 0;
3751 image1 = 0;
3752 image0 = 0;
3754 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3756 switch (r->cl)
3758 case rvc_zero:
3759 break;
3761 case rvc_inf:
3762 if (fmt->has_inf)
3763 image3 |= 32767 << 16;
3764 else
3766 image3 |= 0x7fffffff;
3767 image2 = 0xffffffff;
3768 image1 = 0xffffffff;
3769 image0 = 0xffffffff;
3771 break;
3773 case rvc_nan:
3774 if (fmt->has_nans)
3776 image3 |= 32767 << 16;
3778 if (r->canonical)
3780 if (fmt->canonical_nan_lsbs_set)
3782 image3 |= 0x7fff;
3783 image2 = image1 = image0 = 0xffffffff;
3786 else if (HOST_BITS_PER_LONG == 32)
3788 image0 = u.sig[0];
3789 image1 = u.sig[1];
3790 image2 = u.sig[2];
3791 image3 |= u.sig[3] & 0xffff;
3793 else
3795 image0 = u.sig[0];
3796 image1 = image0 >> 31 >> 1;
3797 image2 = u.sig[1];
3798 image3 |= (image2 >> 31 >> 1) & 0xffff;
3799 image0 &= 0xffffffff;
3800 image2 &= 0xffffffff;
3802 if (r->signalling == fmt->qnan_msb_set)
3803 image3 &= ~0x8000;
3804 else
3805 image3 |= 0x8000;
3806 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3807 image3 |= 0x4000;
3809 else
3811 image3 |= 0x7fffffff;
3812 image2 = 0xffffffff;
3813 image1 = 0xffffffff;
3814 image0 = 0xffffffff;
3816 break;
3818 case rvc_normal:
3819 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3820 whereas the intermediate representation is 0.F x 2**exp.
3821 Which means we're off by one. */
3822 if (denormal)
3823 exp = 0;
3824 else
3825 exp = REAL_EXP (r) + 16383 - 1;
3826 image3 |= exp << 16;
3828 if (HOST_BITS_PER_LONG == 32)
3830 image0 = u.sig[0];
3831 image1 = u.sig[1];
3832 image2 = u.sig[2];
3833 image3 |= u.sig[3] & 0xffff;
3835 else
3837 image0 = u.sig[0];
3838 image1 = image0 >> 31 >> 1;
3839 image2 = u.sig[1];
3840 image3 |= (image2 >> 31 >> 1) & 0xffff;
3841 image0 &= 0xffffffff;
3842 image2 &= 0xffffffff;
3844 break;
3846 default:
3847 gcc_unreachable ();
3850 if (FLOAT_WORDS_BIG_ENDIAN)
3852 buf[0] = image3;
3853 buf[1] = image2;
3854 buf[2] = image1;
3855 buf[3] = image0;
3857 else
3859 buf[0] = image0;
3860 buf[1] = image1;
3861 buf[2] = image2;
3862 buf[3] = image3;
3866 static void
3867 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3868 const long *buf)
3870 unsigned long image3, image2, image1, image0;
3871 bool sign;
3872 int exp;
3874 if (FLOAT_WORDS_BIG_ENDIAN)
3876 image3 = buf[0];
3877 image2 = buf[1];
3878 image1 = buf[2];
3879 image0 = buf[3];
3881 else
3883 image0 = buf[0];
3884 image1 = buf[1];
3885 image2 = buf[2];
3886 image3 = buf[3];
3888 image0 &= 0xffffffff;
3889 image1 &= 0xffffffff;
3890 image2 &= 0xffffffff;
3892 sign = (image3 >> 31) & 1;
3893 exp = (image3 >> 16) & 0x7fff;
3894 image3 &= 0xffff;
3896 memset (r, 0, sizeof (*r));
3898 if (exp == 0)
3900 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3902 r->cl = rvc_normal;
3903 r->sign = sign;
3905 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
3906 if (HOST_BITS_PER_LONG == 32)
3908 r->sig[0] = image0;
3909 r->sig[1] = image1;
3910 r->sig[2] = image2;
3911 r->sig[3] = image3;
3913 else
3915 r->sig[0] = (image1 << 31 << 1) | image0;
3916 r->sig[1] = (image3 << 31 << 1) | image2;
3919 normalize (r);
3921 else if (fmt->has_signed_zero)
3922 r->sign = sign;
3924 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3926 if (image3 | image2 | image1 | image0)
3928 r->cl = rvc_nan;
3929 r->sign = sign;
3930 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
3932 if (HOST_BITS_PER_LONG == 32)
3934 r->sig[0] = image0;
3935 r->sig[1] = image1;
3936 r->sig[2] = image2;
3937 r->sig[3] = image3;
3939 else
3941 r->sig[0] = (image1 << 31 << 1) | image0;
3942 r->sig[1] = (image3 << 31 << 1) | image2;
3944 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3946 else
3948 r->cl = rvc_inf;
3949 r->sign = sign;
3952 else
3954 r->cl = rvc_normal;
3955 r->sign = sign;
3956 SET_REAL_EXP (r, exp - 16383 + 1);
3958 if (HOST_BITS_PER_LONG == 32)
3960 r->sig[0] = image0;
3961 r->sig[1] = image1;
3962 r->sig[2] = image2;
3963 r->sig[3] = image3;
3965 else
3967 r->sig[0] = (image1 << 31 << 1) | image0;
3968 r->sig[1] = (image3 << 31 << 1) | image2;
3970 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3971 r->sig[SIGSZ-1] |= SIG_MSB;
3975 const struct real_format ieee_quad_format =
3977 encode_ieee_quad,
3978 decode_ieee_quad,
3980 113,
3981 113,
3982 -16381,
3983 16384,
3984 127,
3985 127,
3986 false,
3987 true,
3988 true,
3989 true,
3990 true,
3991 true,
3992 true,
3993 false
3996 const struct real_format mips_quad_format =
3998 encode_ieee_quad,
3999 decode_ieee_quad,
4001 113,
4002 113,
4003 -16381,
4004 16384,
4005 127,
4006 127,
4007 false,
4008 true,
4009 true,
4010 true,
4011 true,
4012 true,
4013 false,
4014 true
4017 /* Descriptions of VAX floating point formats can be found beginning at
4019 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4021 The thing to remember is that they're almost IEEE, except for word
4022 order, exponent bias, and the lack of infinities, nans, and denormals.
4024 We don't implement the H_floating format here, simply because neither
4025 the VAX or Alpha ports use it. */
4027 static void encode_vax_f (const struct real_format *fmt,
4028 long *, const REAL_VALUE_TYPE *);
4029 static void decode_vax_f (const struct real_format *,
4030 REAL_VALUE_TYPE *, const long *);
4031 static void encode_vax_d (const struct real_format *fmt,
4032 long *, const REAL_VALUE_TYPE *);
4033 static void decode_vax_d (const struct real_format *,
4034 REAL_VALUE_TYPE *, const long *);
4035 static void encode_vax_g (const struct real_format *fmt,
4036 long *, const REAL_VALUE_TYPE *);
4037 static void decode_vax_g (const struct real_format *,
4038 REAL_VALUE_TYPE *, const long *);
4040 static void
4041 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4042 const REAL_VALUE_TYPE *r)
4044 unsigned long sign, exp, sig, image;
4046 sign = r->sign << 15;
4048 switch (r->cl)
4050 case rvc_zero:
4051 image = 0;
4052 break;
4054 case rvc_inf:
4055 case rvc_nan:
4056 image = 0xffff7fff | sign;
4057 break;
4059 case rvc_normal:
4060 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4061 exp = REAL_EXP (r) + 128;
4063 image = (sig << 16) & 0xffff0000;
4064 image |= sign;
4065 image |= exp << 7;
4066 image |= sig >> 16;
4067 break;
4069 default:
4070 gcc_unreachable ();
4073 buf[0] = image;
4076 static void
4077 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4078 REAL_VALUE_TYPE *r, const long *buf)
4080 unsigned long image = buf[0] & 0xffffffff;
4081 int exp = (image >> 7) & 0xff;
4083 memset (r, 0, sizeof (*r));
4085 if (exp != 0)
4087 r->cl = rvc_normal;
4088 r->sign = (image >> 15) & 1;
4089 SET_REAL_EXP (r, exp - 128);
4091 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4092 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4096 static void
4097 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4098 const REAL_VALUE_TYPE *r)
4100 unsigned long image0, image1, sign = r->sign << 15;
4102 switch (r->cl)
4104 case rvc_zero:
4105 image0 = image1 = 0;
4106 break;
4108 case rvc_inf:
4109 case rvc_nan:
4110 image0 = 0xffff7fff | sign;
4111 image1 = 0xffffffff;
4112 break;
4114 case rvc_normal:
4115 /* Extract the significand into straight hi:lo. */
4116 if (HOST_BITS_PER_LONG == 64)
4118 image0 = r->sig[SIGSZ-1];
4119 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4120 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4122 else
4124 image0 = r->sig[SIGSZ-1];
4125 image1 = r->sig[SIGSZ-2];
4126 image1 = (image0 << 24) | (image1 >> 8);
4127 image0 = (image0 >> 8) & 0xffffff;
4130 /* Rearrange the half-words of the significand to match the
4131 external format. */
4132 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4133 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4135 /* Add the sign and exponent. */
4136 image0 |= sign;
4137 image0 |= (REAL_EXP (r) + 128) << 7;
4138 break;
4140 default:
4141 gcc_unreachable ();
4144 if (FLOAT_WORDS_BIG_ENDIAN)
4145 buf[0] = image1, buf[1] = image0;
4146 else
4147 buf[0] = image0, buf[1] = image1;
4150 static void
4151 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4152 REAL_VALUE_TYPE *r, const long *buf)
4154 unsigned long image0, image1;
4155 int exp;
4157 if (FLOAT_WORDS_BIG_ENDIAN)
4158 image1 = buf[0], image0 = buf[1];
4159 else
4160 image0 = buf[0], image1 = buf[1];
4161 image0 &= 0xffffffff;
4162 image1 &= 0xffffffff;
4164 exp = (image0 >> 7) & 0xff;
4166 memset (r, 0, sizeof (*r));
4168 if (exp != 0)
4170 r->cl = rvc_normal;
4171 r->sign = (image0 >> 15) & 1;
4172 SET_REAL_EXP (r, exp - 128);
4174 /* Rearrange the half-words of the external format into
4175 proper ascending order. */
4176 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4177 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4179 if (HOST_BITS_PER_LONG == 64)
4181 image0 = (image0 << 31 << 1) | image1;
4182 image0 <<= 64 - 56;
4183 image0 |= SIG_MSB;
4184 r->sig[SIGSZ-1] = image0;
4186 else
4188 r->sig[SIGSZ-1] = image0;
4189 r->sig[SIGSZ-2] = image1;
4190 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4191 r->sig[SIGSZ-1] |= SIG_MSB;
4196 static void
4197 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4198 const REAL_VALUE_TYPE *r)
4200 unsigned long image0, image1, sign = r->sign << 15;
4202 switch (r->cl)
4204 case rvc_zero:
4205 image0 = image1 = 0;
4206 break;
4208 case rvc_inf:
4209 case rvc_nan:
4210 image0 = 0xffff7fff | sign;
4211 image1 = 0xffffffff;
4212 break;
4214 case rvc_normal:
4215 /* Extract the significand into straight hi:lo. */
4216 if (HOST_BITS_PER_LONG == 64)
4218 image0 = r->sig[SIGSZ-1];
4219 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4220 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4222 else
4224 image0 = r->sig[SIGSZ-1];
4225 image1 = r->sig[SIGSZ-2];
4226 image1 = (image0 << 21) | (image1 >> 11);
4227 image0 = (image0 >> 11) & 0xfffff;
4230 /* Rearrange the half-words of the significand to match the
4231 external format. */
4232 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4233 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4235 /* Add the sign and exponent. */
4236 image0 |= sign;
4237 image0 |= (REAL_EXP (r) + 1024) << 4;
4238 break;
4240 default:
4241 gcc_unreachable ();
4244 if (FLOAT_WORDS_BIG_ENDIAN)
4245 buf[0] = image1, buf[1] = image0;
4246 else
4247 buf[0] = image0, buf[1] = image1;
4250 static void
4251 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4252 REAL_VALUE_TYPE *r, const long *buf)
4254 unsigned long image0, image1;
4255 int exp;
4257 if (FLOAT_WORDS_BIG_ENDIAN)
4258 image1 = buf[0], image0 = buf[1];
4259 else
4260 image0 = buf[0], image1 = buf[1];
4261 image0 &= 0xffffffff;
4262 image1 &= 0xffffffff;
4264 exp = (image0 >> 4) & 0x7ff;
4266 memset (r, 0, sizeof (*r));
4268 if (exp != 0)
4270 r->cl = rvc_normal;
4271 r->sign = (image0 >> 15) & 1;
4272 SET_REAL_EXP (r, exp - 1024);
4274 /* Rearrange the half-words of the external format into
4275 proper ascending order. */
4276 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4277 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4279 if (HOST_BITS_PER_LONG == 64)
4281 image0 = (image0 << 31 << 1) | image1;
4282 image0 <<= 64 - 53;
4283 image0 |= SIG_MSB;
4284 r->sig[SIGSZ-1] = image0;
4286 else
4288 r->sig[SIGSZ-1] = image0;
4289 r->sig[SIGSZ-2] = image1;
4290 lshift_significand (r, r, 64 - 53);
4291 r->sig[SIGSZ-1] |= SIG_MSB;
4296 const struct real_format vax_f_format =
4298 encode_vax_f,
4299 decode_vax_f,
4303 -127,
4304 127,
4307 false,
4308 false,
4309 false,
4310 false,
4311 false,
4312 false,
4313 false,
4314 false
4317 const struct real_format vax_d_format =
4319 encode_vax_d,
4320 decode_vax_d,
4324 -127,
4325 127,
4328 false,
4329 false,
4330 false,
4331 false,
4332 false,
4333 false,
4334 false,
4335 false
4338 const struct real_format vax_g_format =
4340 encode_vax_g,
4341 decode_vax_g,
4345 -1023,
4346 1023,
4349 false,
4350 false,
4351 false,
4352 false,
4353 false,
4354 false,
4355 false,
4356 false
4359 /* Encode real R into a single precision DFP value in BUF. */
4360 static void
4361 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4362 long *buf ATTRIBUTE_UNUSED,
4363 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4365 encode_decimal32 (fmt, buf, r);
4368 /* Decode a single precision DFP value in BUF into a real R. */
4369 static void
4370 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4371 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4372 const long *buf ATTRIBUTE_UNUSED)
4374 decode_decimal32 (fmt, r, buf);
4377 /* Encode real R into a double precision DFP value in BUF. */
4378 static void
4379 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4380 long *buf ATTRIBUTE_UNUSED,
4381 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4383 encode_decimal64 (fmt, buf, r);
4386 /* Decode a double precision DFP value in BUF into a real R. */
4387 static void
4388 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4389 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4390 const long *buf ATTRIBUTE_UNUSED)
4392 decode_decimal64 (fmt, r, buf);
4395 /* Encode real R into a quad precision DFP value in BUF. */
4396 static void
4397 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4398 long *buf ATTRIBUTE_UNUSED,
4399 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4401 encode_decimal128 (fmt, buf, r);
4404 /* Decode a quad precision DFP value in BUF into a real R. */
4405 static void
4406 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4407 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4408 const long *buf ATTRIBUTE_UNUSED)
4410 decode_decimal128 (fmt, r, buf);
4413 /* Single precision decimal floating point (IEEE 754). */
4414 const struct real_format decimal_single_format =
4416 encode_decimal_single,
4417 decode_decimal_single,
4418 10,
4421 -95,
4425 false,
4426 true,
4427 true,
4428 true,
4429 true,
4430 true,
4431 true,
4432 false
4435 /* Double precision decimal floating point (IEEE 754). */
4436 const struct real_format decimal_double_format =
4438 encode_decimal_double,
4439 decode_decimal_double,
4443 -383,
4444 384,
4447 false,
4448 true,
4449 true,
4450 true,
4451 true,
4452 true,
4453 true,
4454 false
4457 /* Quad precision decimal floating point (IEEE 754). */
4458 const struct real_format decimal_quad_format =
4460 encode_decimal_quad,
4461 decode_decimal_quad,
4465 -6143,
4466 6144,
4467 127,
4468 127,
4469 false,
4470 true,
4471 true,
4472 true,
4473 true,
4474 true,
4475 true,
4476 false
4479 /* A synthetic "format" for internal arithmetic. It's the size of the
4480 internal significand minus the two bits needed for proper rounding.
4481 The encode and decode routines exist only to satisfy our paranoia
4482 harness. */
4484 static void encode_internal (const struct real_format *fmt,
4485 long *, const REAL_VALUE_TYPE *);
4486 static void decode_internal (const struct real_format *,
4487 REAL_VALUE_TYPE *, const long *);
4489 static void
4490 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4491 const REAL_VALUE_TYPE *r)
4493 memcpy (buf, r, sizeof (*r));
4496 static void
4497 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4498 REAL_VALUE_TYPE *r, const long *buf)
4500 memcpy (r, buf, sizeof (*r));
4503 const struct real_format real_internal_format =
4505 encode_internal,
4506 decode_internal,
4508 SIGNIFICAND_BITS - 2,
4509 SIGNIFICAND_BITS - 2,
4510 -MAX_EXP,
4511 MAX_EXP,
4514 false,
4515 false,
4516 true,
4517 true,
4518 false,
4519 true,
4520 true,
4521 false
4524 /* Calculate the square root of X in mode MODE, and store the result
4525 in R. Return TRUE if the operation does not raise an exception.
4526 For details see "High Precision Division and Square Root",
4527 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4528 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4530 bool
4531 real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode,
4532 const REAL_VALUE_TYPE *x)
4534 static REAL_VALUE_TYPE halfthree;
4535 static bool init = false;
4536 REAL_VALUE_TYPE h, t, i;
4537 int iter, exp;
4539 /* sqrt(-0.0) is -0.0. */
4540 if (real_isnegzero (x))
4542 *r = *x;
4543 return false;
4546 /* Negative arguments return NaN. */
4547 if (real_isneg (x))
4549 get_canonical_qnan (r, 0);
4550 return false;
4553 /* Infinity and NaN return themselves. */
4554 if (!real_isfinite (x))
4556 *r = *x;
4557 return false;
4560 if (!init)
4562 do_add (&halfthree, &dconst1, &dconsthalf, 0);
4563 init = true;
4566 /* Initial guess for reciprocal sqrt, i. */
4567 exp = real_exponent (x);
4568 real_ldexp (&i, &dconst1, -exp/2);
4570 /* Newton's iteration for reciprocal sqrt, i. */
4571 for (iter = 0; iter < 16; iter++)
4573 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4574 do_multiply (&t, x, &i);
4575 do_multiply (&h, &t, &i);
4576 do_multiply (&t, &h, &dconsthalf);
4577 do_add (&h, &halfthree, &t, 1);
4578 do_multiply (&t, &i, &h);
4580 /* Check for early convergence. */
4581 if (iter >= 6 && real_identical (&i, &t))
4582 break;
4584 /* ??? Unroll loop to avoid copying. */
4585 i = t;
4588 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4589 do_multiply (&t, x, &i);
4590 do_multiply (&h, &t, &i);
4591 do_add (&i, &dconst1, &h, 1);
4592 do_multiply (&h, &t, &i);
4593 do_multiply (&i, &dconsthalf, &h);
4594 do_add (&h, &t, &i, 0);
4596 /* ??? We need a Tuckerman test to get the last bit. */
4598 real_convert (r, mode, &h);
4599 return true;
4602 /* Calculate X raised to the integer exponent N in mode MODE and store
4603 the result in R. Return true if the result may be inexact due to
4604 loss of precision. The algorithm is the classic "left-to-right binary
4605 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4606 Algorithms", "The Art of Computer Programming", Volume 2. */
4608 bool
4609 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4610 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4612 unsigned HOST_WIDE_INT bit;
4613 REAL_VALUE_TYPE t;
4614 bool inexact = false;
4615 bool init = false;
4616 bool neg;
4617 int i;
4619 if (n == 0)
4621 *r = dconst1;
4622 return false;
4624 else if (n < 0)
4626 /* Don't worry about overflow, from now on n is unsigned. */
4627 neg = true;
4628 n = -n;
4630 else
4631 neg = false;
4633 t = *x;
4634 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4635 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4637 if (init)
4639 inexact |= do_multiply (&t, &t, &t);
4640 if (n & bit)
4641 inexact |= do_multiply (&t, &t, x);
4643 else if (n & bit)
4644 init = true;
4645 bit >>= 1;
4648 if (neg)
4649 inexact |= do_divide (&t, &dconst1, &t);
4651 real_convert (r, mode, &t);
4652 return inexact;
4655 /* Round X to the nearest integer not larger in absolute value, i.e.
4656 towards zero, placing the result in R in mode MODE. */
4658 void
4659 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4660 const REAL_VALUE_TYPE *x)
4662 do_fix_trunc (r, x);
4663 if (mode != VOIDmode)
4664 real_convert (r, mode, r);
4667 /* Round X to the largest integer not greater in value, i.e. round
4668 down, placing the result in R in mode MODE. */
4670 void
4671 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4672 const REAL_VALUE_TYPE *x)
4674 REAL_VALUE_TYPE t;
4676 do_fix_trunc (&t, x);
4677 if (! real_identical (&t, x) && x->sign)
4678 do_add (&t, &t, &dconstm1, 0);
4679 if (mode != VOIDmode)
4680 real_convert (r, mode, &t);
4681 else
4682 *r = t;
4685 /* Round X to the smallest integer not less then argument, i.e. round
4686 up, placing the result in R in mode MODE. */
4688 void
4689 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4690 const REAL_VALUE_TYPE *x)
4692 REAL_VALUE_TYPE t;
4694 do_fix_trunc (&t, x);
4695 if (! real_identical (&t, x) && ! x->sign)
4696 do_add (&t, &t, &dconst1, 0);
4697 if (mode != VOIDmode)
4698 real_convert (r, mode, &t);
4699 else
4700 *r = t;
4703 /* Round X to the nearest integer, but round halfway cases away from
4704 zero. */
4706 void
4707 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4708 const REAL_VALUE_TYPE *x)
4710 do_add (r, x, &dconsthalf, x->sign);
4711 do_fix_trunc (r, r);
4712 if (mode != VOIDmode)
4713 real_convert (r, mode, r);
4716 /* Set the sign of R to the sign of X. */
4718 void
4719 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4721 r->sign = x->sign;
4724 /* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible
4725 for initializing and clearing the MPFR parameter. */
4727 void
4728 mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode)
4730 /* We use a string as an intermediate type. */
4731 char buf[128];
4732 int ret;
4734 /* Take care of Infinity and NaN. */
4735 if (r->cl == rvc_inf)
4737 mpfr_set_inf (m, r->sign == 1 ? -1 : 1);
4738 return;
4741 if (r->cl == rvc_nan)
4743 mpfr_set_nan (m);
4744 return;
4747 real_to_hexadecimal (buf, r, sizeof (buf), 0, 1);
4748 /* mpfr_set_str() parses hexadecimal floats from strings in the same
4749 format that GCC will output them. Nothing extra is needed. */
4750 ret = mpfr_set_str (m, buf, 16, rndmode);
4751 gcc_assert (ret == 0);
4754 /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
4755 mode RNDMODE. TYPE is only relevant if M is a NaN. */
4757 void
4758 real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode)
4760 /* We use a string as an intermediate type. */
4761 char buf[128], *rstr;
4762 mp_exp_t exp;
4764 /* Take care of Infinity and NaN. */
4765 if (mpfr_inf_p (m))
4767 real_inf (r);
4768 if (mpfr_sgn (m) < 0)
4769 *r = REAL_VALUE_NEGATE (*r);
4770 return;
4773 if (mpfr_nan_p (m))
4775 real_nan (r, "", 1, TYPE_MODE (type));
4776 return;
4779 rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode);
4781 /* The additional 12 chars add space for the sprintf below. This
4782 leaves 6 digits for the exponent which is supposedly enough. */
4783 gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12);
4785 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
4786 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
4787 for that. */
4788 exp *= 4;
4790 if (rstr[0] == '-')
4791 sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp);
4792 else
4793 sprintf (buf, "0x.%sp%d", rstr, (int) exp);
4795 mpfr_free_str (rstr);
4797 real_from_string (r, buf);
4800 /* Check whether the real constant value given is an integer. */
4802 bool
4803 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode)
4805 REAL_VALUE_TYPE cint;
4807 real_trunc (&cint, mode, c);
4808 return real_identical (c, &cint);
4811 /* Write into BUF the maximum representable finite floating-point
4812 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4813 float string. LEN is the size of BUF, and the buffer must be large
4814 enough to contain the resulting string. */
4816 void
4817 get_max_float (const struct real_format *fmt, char *buf, size_t len)
4819 int i, n;
4820 char *p;
4822 strcpy (buf, "0x0.");
4823 n = fmt->p;
4824 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
4825 *p++ = 'f';
4826 if (i < n)
4827 *p++ = "08ce"[n - i];
4828 sprintf (p, "p%d", fmt->emax);
4829 if (fmt->pnan < fmt->p)
4831 /* This is an IBM extended double format made up of two IEEE
4832 doubles. The value of the long double is the sum of the
4833 values of the two parts. The most significant part is
4834 required to be the value of the long double rounded to the
4835 nearest double. Rounding means we need a slightly smaller
4836 value for LDBL_MAX. */
4837 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
4840 gcc_assert (strlen (buf) < len);