2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / real.c
blob18c01aaf769603a17ea34faee95d5bf1177985a1
1 /* real.c - software floating point emulation.
2 Copyright (C) 1993-2015 Free Software Foundation, Inc.
3 Contributed by Stephen L. Moshier (moshier@world.std.com).
4 Re-written by Richard Henderson <rth@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "input.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "tree.h"
30 #include "diagnostic-core.h"
31 #include "realmpfr.h"
32 #include "tm_p.h"
33 #include "dfp.h"
34 #include "rtl.h"
35 #include "options.h"
37 /* The floating point model used internally is not exactly IEEE 754
38 compliant, and close to the description in the ISO C99 standard,
39 section 5.2.4.2.2 Characteristics of floating types.
41 Specifically
43 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
45 where
46 s = sign (+- 1)
47 b = base or radix, here always 2
48 e = exponent
49 p = precision (the number of base-b digits in the significand)
50 f_k = the digits of the significand.
52 We differ from typical IEEE 754 encodings in that the entire
53 significand is fractional. Normalized significands are in the
54 range [0.5, 1.0).
56 A requirement of the model is that P be larger than the largest
57 supported target floating-point type by at least 2 bits. This gives
58 us proper rounding when we truncate to the target type. In addition,
59 E must be large enough to hold the smallest supported denormal number
60 in a normalized form.
62 Both of these requirements are easily satisfied. The largest target
63 significand is 113 bits; we store at least 160. The smallest
64 denormal number fits in 17 exponent bits; we store 26. */
67 /* Used to classify two numbers simultaneously. */
68 #define CLASS2(A, B) ((A) << 2 | (B))
70 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
71 #error "Some constant folding done by hand to avoid shift count warnings"
72 #endif
74 static void get_zero (REAL_VALUE_TYPE *, int);
75 static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
76 static void get_canonical_snan (REAL_VALUE_TYPE *, int);
77 static void get_inf (REAL_VALUE_TYPE *, int);
78 static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
79 const REAL_VALUE_TYPE *, unsigned int);
80 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
81 unsigned int);
82 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
83 unsigned int);
84 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
85 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
86 const REAL_VALUE_TYPE *);
87 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
88 const REAL_VALUE_TYPE *, int);
89 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
90 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
91 static int cmp_significand_0 (const REAL_VALUE_TYPE *);
92 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
93 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
94 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
95 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
96 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
97 const REAL_VALUE_TYPE *);
98 static void normalize (REAL_VALUE_TYPE *);
100 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
101 const REAL_VALUE_TYPE *, int);
102 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
103 const REAL_VALUE_TYPE *);
104 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
105 const REAL_VALUE_TYPE *);
106 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
107 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
109 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
110 static void decimal_from_integer (REAL_VALUE_TYPE *);
111 static void decimal_integer_string (char *, const REAL_VALUE_TYPE *,
112 size_t);
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_normal, rvc_zero):
909 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
910 if (a->decimal)
911 return decimal_do_compare (a, b, nan_result);
912 /* Fall through. */
913 case CLASS2 (rvc_inf, rvc_zero):
914 case CLASS2 (rvc_inf, rvc_normal):
915 return (a->sign ? -1 : 1);
917 case CLASS2 (rvc_inf, rvc_inf):
918 return -a->sign - -b->sign;
920 case CLASS2 (rvc_zero, rvc_normal):
921 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
922 if (b->decimal)
923 return decimal_do_compare (a, b, nan_result);
924 /* Fall through. */
925 case CLASS2 (rvc_zero, rvc_inf):
926 case CLASS2 (rvc_normal, rvc_inf):
927 return (b->sign ? 1 : -1);
929 case CLASS2 (rvc_zero, rvc_nan):
930 case CLASS2 (rvc_normal, rvc_nan):
931 case CLASS2 (rvc_inf, rvc_nan):
932 case CLASS2 (rvc_nan, rvc_nan):
933 case CLASS2 (rvc_nan, rvc_zero):
934 case CLASS2 (rvc_nan, rvc_normal):
935 case CLASS2 (rvc_nan, rvc_inf):
936 return nan_result;
938 case CLASS2 (rvc_normal, rvc_normal):
939 break;
941 default:
942 gcc_unreachable ();
945 if (a->sign != b->sign)
946 return -a->sign - -b->sign;
948 if (a->decimal || b->decimal)
949 return decimal_do_compare (a, b, nan_result);
951 if (REAL_EXP (a) > REAL_EXP (b))
952 ret = 1;
953 else if (REAL_EXP (a) < REAL_EXP (b))
954 ret = -1;
955 else
956 ret = cmp_significands (a, b);
958 return (a->sign ? -ret : ret);
961 /* Return A truncated to an integral value toward zero. */
963 static void
964 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
966 *r = *a;
968 switch (r->cl)
970 case rvc_zero:
971 case rvc_inf:
972 case rvc_nan:
973 break;
975 case rvc_normal:
976 if (r->decimal)
978 decimal_do_fix_trunc (r, a);
979 return;
981 if (REAL_EXP (r) <= 0)
982 get_zero (r, r->sign);
983 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
984 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
985 break;
987 default:
988 gcc_unreachable ();
992 /* Perform the binary or unary operation described by CODE.
993 For a unary operation, leave OP1 NULL. This function returns
994 true if the result may be inexact due to loss of precision. */
996 bool
997 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
998 const REAL_VALUE_TYPE *op1)
1000 enum tree_code code = (enum tree_code) icode;
1002 if (op0->decimal || (op1 && op1->decimal))
1003 return decimal_real_arithmetic (r, code, op0, op1);
1005 switch (code)
1007 case PLUS_EXPR:
1008 /* Clear any padding areas in *r if it isn't equal to one of the
1009 operands so that we can later do bitwise comparisons later on. */
1010 if (r != op0 && r != op1)
1011 memset (r, '\0', sizeof (*r));
1012 return do_add (r, op0, op1, 0);
1014 case MINUS_EXPR:
1015 if (r != op0 && r != op1)
1016 memset (r, '\0', sizeof (*r));
1017 return do_add (r, op0, op1, 1);
1019 case MULT_EXPR:
1020 if (r != op0 && r != op1)
1021 memset (r, '\0', sizeof (*r));
1022 return do_multiply (r, op0, op1);
1024 case RDIV_EXPR:
1025 if (r != op0 && r != op1)
1026 memset (r, '\0', sizeof (*r));
1027 return do_divide (r, op0, op1);
1029 case MIN_EXPR:
1030 if (op1->cl == rvc_nan)
1031 *r = *op1;
1032 else if (do_compare (op0, op1, -1) < 0)
1033 *r = *op0;
1034 else
1035 *r = *op1;
1036 break;
1038 case MAX_EXPR:
1039 if (op1->cl == rvc_nan)
1040 *r = *op1;
1041 else if (do_compare (op0, op1, 1) < 0)
1042 *r = *op1;
1043 else
1044 *r = *op0;
1045 break;
1047 case NEGATE_EXPR:
1048 *r = *op0;
1049 r->sign ^= 1;
1050 break;
1052 case ABS_EXPR:
1053 *r = *op0;
1054 r->sign = 0;
1055 break;
1057 case FIX_TRUNC_EXPR:
1058 do_fix_trunc (r, op0);
1059 break;
1061 default:
1062 gcc_unreachable ();
1064 return false;
1067 REAL_VALUE_TYPE
1068 real_value_negate (const REAL_VALUE_TYPE *op0)
1070 REAL_VALUE_TYPE r;
1071 real_arithmetic (&r, NEGATE_EXPR, op0, NULL);
1072 return r;
1075 REAL_VALUE_TYPE
1076 real_value_abs (const REAL_VALUE_TYPE *op0)
1078 REAL_VALUE_TYPE r;
1079 real_arithmetic (&r, ABS_EXPR, op0, NULL);
1080 return r;
1083 bool
1084 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1085 const REAL_VALUE_TYPE *op1)
1087 enum tree_code code = (enum tree_code) icode;
1089 switch (code)
1091 case LT_EXPR:
1092 return do_compare (op0, op1, 1) < 0;
1093 case LE_EXPR:
1094 return do_compare (op0, op1, 1) <= 0;
1095 case GT_EXPR:
1096 return do_compare (op0, op1, -1) > 0;
1097 case GE_EXPR:
1098 return do_compare (op0, op1, -1) >= 0;
1099 case EQ_EXPR:
1100 return do_compare (op0, op1, -1) == 0;
1101 case NE_EXPR:
1102 return do_compare (op0, op1, -1) != 0;
1103 case UNORDERED_EXPR:
1104 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1105 case ORDERED_EXPR:
1106 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1107 case UNLT_EXPR:
1108 return do_compare (op0, op1, -1) < 0;
1109 case UNLE_EXPR:
1110 return do_compare (op0, op1, -1) <= 0;
1111 case UNGT_EXPR:
1112 return do_compare (op0, op1, 1) > 0;
1113 case UNGE_EXPR:
1114 return do_compare (op0, op1, 1) >= 0;
1115 case UNEQ_EXPR:
1116 return do_compare (op0, op1, 0) == 0;
1117 case LTGT_EXPR:
1118 return do_compare (op0, op1, 0) != 0;
1120 default:
1121 gcc_unreachable ();
1125 /* Return floor log2(R). */
1128 real_exponent (const REAL_VALUE_TYPE *r)
1130 switch (r->cl)
1132 case rvc_zero:
1133 return 0;
1134 case rvc_inf:
1135 case rvc_nan:
1136 return (unsigned int)-1 >> 1;
1137 case rvc_normal:
1138 return REAL_EXP (r);
1139 default:
1140 gcc_unreachable ();
1144 /* R = OP0 * 2**EXP. */
1146 void
1147 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1149 *r = *op0;
1150 switch (r->cl)
1152 case rvc_zero:
1153 case rvc_inf:
1154 case rvc_nan:
1155 break;
1157 case rvc_normal:
1158 exp += REAL_EXP (op0);
1159 if (exp > MAX_EXP)
1160 get_inf (r, r->sign);
1161 else if (exp < -MAX_EXP)
1162 get_zero (r, r->sign);
1163 else
1164 SET_REAL_EXP (r, exp);
1165 break;
1167 default:
1168 gcc_unreachable ();
1172 /* Determine whether a floating-point value X is infinite. */
1174 bool
1175 real_isinf (const REAL_VALUE_TYPE *r)
1177 return (r->cl == rvc_inf);
1180 /* Determine whether a floating-point value X is a NaN. */
1182 bool
1183 real_isnan (const REAL_VALUE_TYPE *r)
1185 return (r->cl == rvc_nan);
1188 /* Determine whether a floating-point value X is finite. */
1190 bool
1191 real_isfinite (const REAL_VALUE_TYPE *r)
1193 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1196 /* Determine whether a floating-point value X is negative. */
1198 bool
1199 real_isneg (const REAL_VALUE_TYPE *r)
1201 return r->sign;
1204 /* Determine whether a floating-point value X is minus zero. */
1206 bool
1207 real_isnegzero (const REAL_VALUE_TYPE *r)
1209 return r->sign && r->cl == rvc_zero;
1212 /* Compare two floating-point objects for bitwise identity. */
1214 bool
1215 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1217 int i;
1219 if (a->cl != b->cl)
1220 return false;
1221 if (a->sign != b->sign)
1222 return false;
1224 switch (a->cl)
1226 case rvc_zero:
1227 case rvc_inf:
1228 return true;
1230 case rvc_normal:
1231 if (a->decimal != b->decimal)
1232 return false;
1233 if (REAL_EXP (a) != REAL_EXP (b))
1234 return false;
1235 break;
1237 case rvc_nan:
1238 if (a->signalling != b->signalling)
1239 return false;
1240 /* The significand is ignored for canonical NaNs. */
1241 if (a->canonical || b->canonical)
1242 return a->canonical == b->canonical;
1243 break;
1245 default:
1246 gcc_unreachable ();
1249 for (i = 0; i < SIGSZ; ++i)
1250 if (a->sig[i] != b->sig[i])
1251 return false;
1253 return true;
1256 /* Try to change R into its exact multiplicative inverse in machine
1257 mode MODE. Return true if successful. */
1259 bool
1260 exact_real_inverse (machine_mode mode, REAL_VALUE_TYPE *r)
1262 const REAL_VALUE_TYPE *one = real_digit (1);
1263 REAL_VALUE_TYPE u;
1264 int i;
1266 if (r->cl != rvc_normal)
1267 return false;
1269 /* Check for a power of two: all significand bits zero except the MSB. */
1270 for (i = 0; i < SIGSZ-1; ++i)
1271 if (r->sig[i] != 0)
1272 return false;
1273 if (r->sig[SIGSZ-1] != SIG_MSB)
1274 return false;
1276 /* Find the inverse and truncate to the required mode. */
1277 do_divide (&u, one, r);
1278 real_convert (&u, mode, &u);
1280 /* The rounding may have overflowed. */
1281 if (u.cl != rvc_normal)
1282 return false;
1283 for (i = 0; i < SIGSZ-1; ++i)
1284 if (u.sig[i] != 0)
1285 return false;
1286 if (u.sig[SIGSZ-1] != SIG_MSB)
1287 return false;
1289 *r = u;
1290 return true;
1293 /* Return true if arithmetic on values in IMODE that were promoted
1294 from values in TMODE is equivalent to direct arithmetic on values
1295 in TMODE. */
1297 bool
1298 real_can_shorten_arithmetic (machine_mode imode, machine_mode tmode)
1300 const struct real_format *tfmt, *ifmt;
1301 tfmt = REAL_MODE_FORMAT (tmode);
1302 ifmt = REAL_MODE_FORMAT (imode);
1303 /* These conditions are conservative rather than trying to catch the
1304 exact boundary conditions; the main case to allow is IEEE float
1305 and double. */
1306 return (ifmt->b == tfmt->b
1307 && ifmt->p > 2 * tfmt->p
1308 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1309 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1310 && ifmt->emax > 2 * tfmt->emax + 2
1311 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1312 && ifmt->round_towards_zero == tfmt->round_towards_zero
1313 && (ifmt->has_sign_dependent_rounding
1314 == tfmt->has_sign_dependent_rounding)
1315 && ifmt->has_nans >= tfmt->has_nans
1316 && ifmt->has_inf >= tfmt->has_inf
1317 && ifmt->has_signed_zero >= tfmt->has_signed_zero
1318 && !MODE_COMPOSITE_P (tmode)
1319 && !MODE_COMPOSITE_P (imode));
1322 /* Render R as an integer. */
1324 HOST_WIDE_INT
1325 real_to_integer (const REAL_VALUE_TYPE *r)
1327 unsigned HOST_WIDE_INT i;
1329 switch (r->cl)
1331 case rvc_zero:
1332 underflow:
1333 return 0;
1335 case rvc_inf:
1336 case rvc_nan:
1337 overflow:
1338 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1339 if (!r->sign)
1340 i--;
1341 return i;
1343 case rvc_normal:
1344 if (r->decimal)
1345 return decimal_real_to_integer (r);
1347 if (REAL_EXP (r) <= 0)
1348 goto underflow;
1349 /* Only force overflow for unsigned overflow. Signed overflow is
1350 undefined, so it doesn't matter what we return, and some callers
1351 expect to be able to use this routine for both signed and
1352 unsigned conversions. */
1353 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1354 goto overflow;
1356 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1357 i = r->sig[SIGSZ-1];
1358 else
1360 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1361 i = r->sig[SIGSZ-1];
1362 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1363 i |= r->sig[SIGSZ-2];
1366 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1368 if (r->sign)
1369 i = -i;
1370 return i;
1372 default:
1373 gcc_unreachable ();
1377 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1378 be represented in precision, *FAIL is set to TRUE. */
1380 wide_int
1381 real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
1383 HOST_WIDE_INT val[2 * WIDE_INT_MAX_ELTS];
1384 int exp;
1385 int words, w;
1386 wide_int result;
1388 switch (r->cl)
1390 case rvc_zero:
1391 underflow:
1392 return wi::zero (precision);
1394 case rvc_inf:
1395 case rvc_nan:
1396 overflow:
1397 *fail = true;
1399 if (r->sign)
1400 return wi::set_bit_in_zero (precision - 1, precision);
1401 else
1402 return ~wi::set_bit_in_zero (precision - 1, precision);
1404 case rvc_normal:
1405 if (r->decimal)
1406 return decimal_real_to_integer (r, fail, precision);
1408 exp = REAL_EXP (r);
1409 if (exp <= 0)
1410 goto underflow;
1411 /* Only force overflow for unsigned overflow. Signed overflow is
1412 undefined, so it doesn't matter what we return, and some callers
1413 expect to be able to use this routine for both signed and
1414 unsigned conversions. */
1415 if (exp > precision)
1416 goto overflow;
1418 /* Put the significand into a wide_int that has precision W, which
1419 is the smallest HWI-multiple that has at least PRECISION bits.
1420 This ensures that the top bit of the significand is in the
1421 top bit of the wide_int. */
1422 words = (precision + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT;
1423 w = words * HOST_BITS_PER_WIDE_INT;
1425 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1426 for (int i = 0; i < words; i++)
1428 int j = SIGSZ - words + i;
1429 val[i] = (j < 0) ? 0 : r->sig[j];
1431 #else
1432 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1433 for (int i = 0; i < words; i++)
1435 int j = SIGSZ - (words * 2) + (i * 2);
1436 if (j < 0)
1437 val[i] = 0;
1438 else
1439 val[i] = r->sig[j];
1440 j += 1;
1441 if (j >= 0)
1442 val[i] |= (unsigned HOST_WIDE_INT) r->sig[j] << HOST_BITS_PER_LONG;
1444 #endif
1445 /* Shift the value into place and truncate to the desired precision. */
1446 result = wide_int::from_array (val, words, w);
1447 result = wi::lrshift (result, w - exp);
1448 result = wide_int::from (result, precision, UNSIGNED);
1450 if (r->sign)
1451 return -result;
1452 else
1453 return result;
1455 default:
1456 gcc_unreachable ();
1460 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1461 of NUM / DEN. Return the quotient and place the remainder in NUM.
1462 It is expected that NUM / DEN are close enough that the quotient is
1463 small. */
1465 static unsigned long
1466 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1468 unsigned long q, msb;
1469 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1471 if (expn < expd)
1472 return 0;
1474 q = msb = 0;
1475 goto start;
1478 msb = num->sig[SIGSZ-1] & SIG_MSB;
1479 q <<= 1;
1480 lshift_significand_1 (num, num);
1481 start:
1482 if (msb || cmp_significands (num, den) >= 0)
1484 sub_significands (num, num, den, 0);
1485 q |= 1;
1488 while (--expn >= expd);
1490 SET_REAL_EXP (num, expd);
1491 normalize (num);
1493 return q;
1496 /* Render R as a decimal floating point constant. Emit DIGITS significant
1497 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1498 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1499 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1500 to a string that, when parsed back in mode MODE, yields the same value. */
1502 #define M_LOG10_2 0.30102999566398119521
1504 void
1505 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1506 size_t buf_size, size_t digits,
1507 int crop_trailing_zeros, machine_mode mode)
1509 const struct real_format *fmt = NULL;
1510 const REAL_VALUE_TYPE *one, *ten;
1511 REAL_VALUE_TYPE r, pten, u, v;
1512 int dec_exp, cmp_one, digit;
1513 size_t max_digits;
1514 char *p, *first, *last;
1515 bool sign;
1516 bool round_up;
1518 if (mode != VOIDmode)
1520 fmt = REAL_MODE_FORMAT (mode);
1521 gcc_assert (fmt);
1524 r = *r_orig;
1525 switch (r.cl)
1527 case rvc_zero:
1528 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1529 return;
1530 case rvc_normal:
1531 break;
1532 case rvc_inf:
1533 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1534 return;
1535 case rvc_nan:
1536 /* ??? Print the significand as well, if not canonical? */
1537 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1538 (r_orig->signalling ? 'S' : 'Q'));
1539 return;
1540 default:
1541 gcc_unreachable ();
1544 if (r.decimal)
1546 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1547 return;
1550 /* Bound the number of digits printed by the size of the representation. */
1551 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1552 if (digits == 0 || digits > max_digits)
1553 digits = max_digits;
1555 /* Estimate the decimal exponent, and compute the length of the string it
1556 will print as. Be conservative and add one to account for possible
1557 overflow or rounding error. */
1558 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1559 for (max_digits = 1; dec_exp ; max_digits++)
1560 dec_exp /= 10;
1562 /* Bound the number of digits printed by the size of the output buffer. */
1563 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1564 gcc_assert (max_digits <= buf_size);
1565 if (digits > max_digits)
1566 digits = max_digits;
1568 one = real_digit (1);
1569 ten = ten_to_ptwo (0);
1571 sign = r.sign;
1572 r.sign = 0;
1574 dec_exp = 0;
1575 pten = *one;
1577 cmp_one = do_compare (&r, one, 0);
1578 if (cmp_one > 0)
1580 int m;
1582 /* Number is greater than one. Convert significand to an integer
1583 and strip trailing decimal zeros. */
1585 u = r;
1586 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1588 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1589 m = floor_log2 (max_digits);
1591 /* Iterate over the bits of the possible powers of 10 that might
1592 be present in U and eliminate them. That is, if we find that
1593 10**2**M divides U evenly, keep the division and increase
1594 DEC_EXP by 2**M. */
1597 REAL_VALUE_TYPE t;
1599 do_divide (&t, &u, ten_to_ptwo (m));
1600 do_fix_trunc (&v, &t);
1601 if (cmp_significands (&v, &t) == 0)
1603 u = t;
1604 dec_exp += 1 << m;
1607 while (--m >= 0);
1609 /* Revert the scaling to integer that we performed earlier. */
1610 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1611 - (SIGNIFICAND_BITS - 1));
1612 r = u;
1614 /* Find power of 10. Do this by dividing out 10**2**M when
1615 this is larger than the current remainder. Fill PTEN with
1616 the power of 10 that we compute. */
1617 if (REAL_EXP (&r) > 0)
1619 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1622 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1623 if (do_compare (&u, ptentwo, 0) >= 0)
1625 do_divide (&u, &u, ptentwo);
1626 do_multiply (&pten, &pten, ptentwo);
1627 dec_exp += 1 << m;
1630 while (--m >= 0);
1632 else
1633 /* We managed to divide off enough tens in the above reduction
1634 loop that we've now got a negative exponent. Fall into the
1635 less-than-one code to compute the proper value for PTEN. */
1636 cmp_one = -1;
1638 if (cmp_one < 0)
1640 int m;
1642 /* Number is less than one. Pad significand with leading
1643 decimal zeros. */
1645 v = r;
1646 while (1)
1648 /* Stop if we'd shift bits off the bottom. */
1649 if (v.sig[0] & 7)
1650 break;
1652 do_multiply (&u, &v, ten);
1654 /* Stop if we're now >= 1. */
1655 if (REAL_EXP (&u) > 0)
1656 break;
1658 v = u;
1659 dec_exp -= 1;
1661 r = v;
1663 /* Find power of 10. Do this by multiplying in P=10**2**M when
1664 the current remainder is smaller than 1/P. Fill PTEN with the
1665 power of 10 that we compute. */
1666 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1669 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1670 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1672 if (do_compare (&v, ptenmtwo, 0) <= 0)
1674 do_multiply (&v, &v, ptentwo);
1675 do_multiply (&pten, &pten, ptentwo);
1676 dec_exp -= 1 << m;
1679 while (--m >= 0);
1681 /* Invert the positive power of 10 that we've collected so far. */
1682 do_divide (&pten, one, &pten);
1685 p = str;
1686 if (sign)
1687 *p++ = '-';
1688 first = p++;
1690 /* At this point, PTEN should contain the nearest power of 10 smaller
1691 than R, such that this division produces the first digit.
1693 Using a divide-step primitive that returns the complete integral
1694 remainder avoids the rounding error that would be produced if
1695 we were to use do_divide here and then simply multiply by 10 for
1696 each subsequent digit. */
1698 digit = rtd_divmod (&r, &pten);
1700 /* Be prepared for error in that division via underflow ... */
1701 if (digit == 0 && cmp_significand_0 (&r))
1703 /* Multiply by 10 and try again. */
1704 do_multiply (&r, &r, ten);
1705 digit = rtd_divmod (&r, &pten);
1706 dec_exp -= 1;
1707 gcc_assert (digit != 0);
1710 /* ... or overflow. */
1711 if (digit == 10)
1713 *p++ = '1';
1714 if (--digits > 0)
1715 *p++ = '0';
1716 dec_exp += 1;
1718 else
1720 gcc_assert (digit <= 10);
1721 *p++ = digit + '0';
1724 /* Generate subsequent digits. */
1725 while (--digits > 0)
1727 do_multiply (&r, &r, ten);
1728 digit = rtd_divmod (&r, &pten);
1729 *p++ = digit + '0';
1731 last = p;
1733 /* Generate one more digit with which to do rounding. */
1734 do_multiply (&r, &r, ten);
1735 digit = rtd_divmod (&r, &pten);
1737 /* Round the result. */
1738 if (fmt && fmt->round_towards_zero)
1740 /* If the format uses round towards zero when parsing the string
1741 back in, we need to always round away from zero here. */
1742 if (cmp_significand_0 (&r))
1743 digit++;
1744 round_up = digit > 0;
1746 else
1748 if (digit == 5)
1750 /* Round to nearest. If R is nonzero there are additional
1751 nonzero digits to be extracted. */
1752 if (cmp_significand_0 (&r))
1753 digit++;
1754 /* Round to even. */
1755 else if ((p[-1] - '0') & 1)
1756 digit++;
1759 round_up = digit > 5;
1762 if (round_up)
1764 while (p > first)
1766 digit = *--p;
1767 if (digit == '9')
1768 *p = '0';
1769 else
1771 *p = digit + 1;
1772 break;
1776 /* Carry out of the first digit. This means we had all 9's and
1777 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1778 if (p == first)
1780 first[1] = '1';
1781 dec_exp++;
1785 /* Insert the decimal point. */
1786 first[0] = first[1];
1787 first[1] = '.';
1789 /* If requested, drop trailing zeros. Never crop past "1.0". */
1790 if (crop_trailing_zeros)
1791 while (last > first + 3 && last[-1] == '0')
1792 last--;
1794 /* Append the exponent. */
1795 sprintf (last, "e%+d", dec_exp);
1797 #ifdef ENABLE_CHECKING
1798 /* Verify that we can read the original value back in. */
1799 if (mode != VOIDmode)
1801 real_from_string (&r, str);
1802 real_convert (&r, mode, &r);
1803 gcc_assert (real_identical (&r, r_orig));
1805 #endif
1808 /* Likewise, except always uses round-to-nearest. */
1810 void
1811 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1812 size_t digits, int crop_trailing_zeros)
1814 real_to_decimal_for_mode (str, r_orig, buf_size,
1815 digits, crop_trailing_zeros, VOIDmode);
1818 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1819 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1820 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1821 strip trailing zeros. */
1823 void
1824 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1825 size_t digits, int crop_trailing_zeros)
1827 int i, j, exp = REAL_EXP (r);
1828 char *p, *first;
1829 char exp_buf[16];
1830 size_t max_digits;
1832 switch (r->cl)
1834 case rvc_zero:
1835 exp = 0;
1836 break;
1837 case rvc_normal:
1838 break;
1839 case rvc_inf:
1840 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1841 return;
1842 case rvc_nan:
1843 /* ??? Print the significand as well, if not canonical? */
1844 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1845 (r->signalling ? 'S' : 'Q'));
1846 return;
1847 default:
1848 gcc_unreachable ();
1851 if (r->decimal)
1853 /* Hexadecimal format for decimal floats is not interesting. */
1854 strcpy (str, "N/A");
1855 return;
1858 if (digits == 0)
1859 digits = SIGNIFICAND_BITS / 4;
1861 /* Bound the number of digits printed by the size of the output buffer. */
1863 sprintf (exp_buf, "p%+d", exp);
1864 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1865 gcc_assert (max_digits <= buf_size);
1866 if (digits > max_digits)
1867 digits = max_digits;
1869 p = str;
1870 if (r->sign)
1871 *p++ = '-';
1872 *p++ = '0';
1873 *p++ = 'x';
1874 *p++ = '0';
1875 *p++ = '.';
1876 first = p;
1878 for (i = SIGSZ - 1; i >= 0; --i)
1879 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1881 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1882 if (--digits == 0)
1883 goto out;
1886 out:
1887 if (crop_trailing_zeros)
1888 while (p > first + 1 && p[-1] == '0')
1889 p--;
1891 sprintf (p, "p%+d", exp);
1894 /* Initialize R from a decimal or hexadecimal string. The string is
1895 assumed to have been syntax checked already. Return -1 if the
1896 value underflows, +1 if overflows, and 0 otherwise. */
1899 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1901 int exp = 0;
1902 bool sign = false;
1904 get_zero (r, 0);
1906 if (*str == '-')
1908 sign = true;
1909 str++;
1911 else if (*str == '+')
1912 str++;
1914 if (!strncmp (str, "QNaN", 4))
1916 get_canonical_qnan (r, sign);
1917 return 0;
1919 else if (!strncmp (str, "SNaN", 4))
1921 get_canonical_snan (r, sign);
1922 return 0;
1924 else if (!strncmp (str, "Inf", 3))
1926 get_inf (r, sign);
1927 return 0;
1930 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1932 /* Hexadecimal floating point. */
1933 int pos = SIGNIFICAND_BITS - 4, d;
1935 str += 2;
1937 while (*str == '0')
1938 str++;
1939 while (1)
1941 d = hex_value (*str);
1942 if (d == _hex_bad)
1943 break;
1944 if (pos >= 0)
1946 r->sig[pos / HOST_BITS_PER_LONG]
1947 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1948 pos -= 4;
1950 else if (d)
1951 /* Ensure correct rounding by setting last bit if there is
1952 a subsequent nonzero digit. */
1953 r->sig[0] |= 1;
1954 exp += 4;
1955 str++;
1957 if (*str == '.')
1959 str++;
1960 if (pos == SIGNIFICAND_BITS - 4)
1962 while (*str == '0')
1963 str++, exp -= 4;
1965 while (1)
1967 d = hex_value (*str);
1968 if (d == _hex_bad)
1969 break;
1970 if (pos >= 0)
1972 r->sig[pos / HOST_BITS_PER_LONG]
1973 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1974 pos -= 4;
1976 else if (d)
1977 /* Ensure correct rounding by setting last bit if there is
1978 a subsequent nonzero digit. */
1979 r->sig[0] |= 1;
1980 str++;
1984 /* If the mantissa is zero, ignore the exponent. */
1985 if (!cmp_significand_0 (r))
1986 goto is_a_zero;
1988 if (*str == 'p' || *str == 'P')
1990 bool exp_neg = false;
1992 str++;
1993 if (*str == '-')
1995 exp_neg = true;
1996 str++;
1998 else if (*str == '+')
1999 str++;
2001 d = 0;
2002 while (ISDIGIT (*str))
2004 d *= 10;
2005 d += *str - '0';
2006 if (d > MAX_EXP)
2008 /* Overflowed the exponent. */
2009 if (exp_neg)
2010 goto underflow;
2011 else
2012 goto overflow;
2014 str++;
2016 if (exp_neg)
2017 d = -d;
2019 exp += d;
2022 r->cl = rvc_normal;
2023 SET_REAL_EXP (r, exp);
2025 normalize (r);
2027 else
2029 /* Decimal floating point. */
2030 const char *cstr = str;
2031 mpfr_t m;
2032 bool inexact;
2034 while (*cstr == '0')
2035 cstr++;
2036 if (*cstr == '.')
2038 cstr++;
2039 while (*cstr == '0')
2040 cstr++;
2043 /* If the mantissa is zero, ignore the exponent. */
2044 if (!ISDIGIT (*cstr))
2045 goto is_a_zero;
2047 /* Nonzero value, possibly overflowing or underflowing. */
2048 mpfr_init2 (m, SIGNIFICAND_BITS);
2049 inexact = mpfr_strtofr (m, str, NULL, 10, GMP_RNDZ);
2050 /* The result should never be a NaN, and because the rounding is
2051 toward zero should never be an infinity. */
2052 gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m));
2053 if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4)
2055 mpfr_clear (m);
2056 goto underflow;
2058 else if (mpfr_get_exp (m) > MAX_EXP - 4)
2060 mpfr_clear (m);
2061 goto overflow;
2063 else
2065 real_from_mpfr (r, m, NULL_TREE, GMP_RNDZ);
2066 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2067 because the hex digits used in real_from_mpfr did not
2068 start with a digit 8 to f, but the exponent bounds above
2069 should have avoided underflow or overflow. */
2070 gcc_assert (r->cl == rvc_normal);
2071 /* Set a sticky bit if mpfr_strtofr was inexact. */
2072 r->sig[0] |= inexact;
2073 mpfr_clear (m);
2077 r->sign = sign;
2078 return 0;
2080 is_a_zero:
2081 get_zero (r, sign);
2082 return 0;
2084 underflow:
2085 get_zero (r, sign);
2086 return -1;
2088 overflow:
2089 get_inf (r, sign);
2090 return 1;
2093 /* Legacy. Similar, but return the result directly. */
2095 REAL_VALUE_TYPE
2096 real_from_string2 (const char *s, machine_mode mode)
2098 REAL_VALUE_TYPE r;
2100 real_from_string (&r, s);
2101 if (mode != VOIDmode)
2102 real_convert (&r, mode, &r);
2104 return r;
2107 /* Initialize R from string S and desired MODE. */
2109 void
2110 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, machine_mode mode)
2112 if (DECIMAL_FLOAT_MODE_P (mode))
2113 decimal_real_from_string (r, s);
2114 else
2115 real_from_string (r, s);
2117 if (mode != VOIDmode)
2118 real_convert (r, mode, r);
2121 /* Initialize R from the wide_int VAL_IN. The MODE is not VOIDmode,*/
2123 void
2124 real_from_integer (REAL_VALUE_TYPE *r, machine_mode mode,
2125 const wide_int_ref &val_in, signop sgn)
2127 if (val_in == 0)
2128 get_zero (r, 0);
2129 else
2131 unsigned int len = val_in.get_precision ();
2132 int i, j, e = 0;
2133 int maxbitlen = MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT;
2134 const unsigned int realmax = (SIGNIFICAND_BITS / HOST_BITS_PER_WIDE_INT
2135 * HOST_BITS_PER_WIDE_INT);
2137 memset (r, 0, sizeof (*r));
2138 r->cl = rvc_normal;
2139 r->sign = wi::neg_p (val_in, sgn);
2141 /* We have to ensure we can negate the largest negative number. */
2142 wide_int val = wide_int::from (val_in, maxbitlen, sgn);
2144 if (r->sign)
2145 val = -val;
2147 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2148 won't work with precisions that are not a multiple of
2149 HOST_BITS_PER_WIDE_INT. */
2150 len += HOST_BITS_PER_WIDE_INT - 1;
2152 /* Ensure we can represent the largest negative number. */
2153 len += 1;
2155 len = len/HOST_BITS_PER_WIDE_INT * HOST_BITS_PER_WIDE_INT;
2157 /* Cap the size to the size allowed by real.h. */
2158 if (len > realmax)
2160 HOST_WIDE_INT cnt_l_z;
2161 cnt_l_z = wi::clz (val);
2163 if (maxbitlen - cnt_l_z > realmax)
2165 e = maxbitlen - cnt_l_z - realmax;
2167 /* This value is too large, we must shift it right to
2168 preserve all the bits we can, and then bump the
2169 exponent up by that amount. */
2170 val = wi::lrshift (val, e);
2172 len = realmax;
2175 /* Clear out top bits so elt will work with precisions that aren't
2176 a multiple of HOST_BITS_PER_WIDE_INT. */
2177 val = wide_int::from (val, len, sgn);
2178 len = len / HOST_BITS_PER_WIDE_INT;
2180 SET_REAL_EXP (r, len * HOST_BITS_PER_WIDE_INT + e);
2182 j = SIGSZ - 1;
2183 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2184 for (i = len - 1; i >= 0; i--)
2186 r->sig[j--] = val.elt (i);
2187 if (j < 0)
2188 break;
2190 else
2192 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2193 for (i = len - 1; i >= 0; i--)
2195 HOST_WIDE_INT e = val.elt (i);
2196 r->sig[j--] = e >> (HOST_BITS_PER_LONG - 1) >> 1;
2197 if (j < 0)
2198 break;
2199 r->sig[j--] = e;
2200 if (j < 0)
2201 break;
2205 normalize (r);
2208 if (DECIMAL_FLOAT_MODE_P (mode))
2209 decimal_from_integer (r);
2210 else if (mode != VOIDmode)
2211 real_convert (r, mode, r);
2214 /* Render R, an integral value, as a floating point constant with no
2215 specified exponent. */
2217 static void
2218 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2219 size_t buf_size)
2221 int dec_exp, digit, digits;
2222 REAL_VALUE_TYPE r, pten;
2223 char *p;
2224 bool sign;
2226 r = *r_orig;
2228 if (r.cl == rvc_zero)
2230 strcpy (str, "0.");
2231 return;
2234 sign = r.sign;
2235 r.sign = 0;
2237 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2238 digits = dec_exp + 1;
2239 gcc_assert ((digits + 2) < (int)buf_size);
2241 pten = *real_digit (1);
2242 times_pten (&pten, dec_exp);
2244 p = str;
2245 if (sign)
2246 *p++ = '-';
2248 digit = rtd_divmod (&r, &pten);
2249 gcc_assert (digit >= 0 && digit <= 9);
2250 *p++ = digit + '0';
2251 while (--digits > 0)
2253 times_pten (&r, 1);
2254 digit = rtd_divmod (&r, &pten);
2255 *p++ = digit + '0';
2257 *p++ = '.';
2258 *p++ = '\0';
2261 /* Convert a real with an integral value to decimal float. */
2263 static void
2264 decimal_from_integer (REAL_VALUE_TYPE *r)
2266 char str[256];
2268 decimal_integer_string (str, r, sizeof (str) - 1);
2269 decimal_real_from_string (r, str);
2272 /* Returns 10**2**N. */
2274 static const REAL_VALUE_TYPE *
2275 ten_to_ptwo (int n)
2277 static REAL_VALUE_TYPE tens[EXP_BITS];
2279 gcc_assert (n >= 0);
2280 gcc_assert (n < EXP_BITS);
2282 if (tens[n].cl == rvc_zero)
2284 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2286 HOST_WIDE_INT t = 10;
2287 int i;
2289 for (i = 0; i < n; ++i)
2290 t *= t;
2292 real_from_integer (&tens[n], VOIDmode, t, UNSIGNED);
2294 else
2296 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2297 do_multiply (&tens[n], t, t);
2301 return &tens[n];
2304 /* Returns 10**(-2**N). */
2306 static const REAL_VALUE_TYPE *
2307 ten_to_mptwo (int n)
2309 static REAL_VALUE_TYPE tens[EXP_BITS];
2311 gcc_assert (n >= 0);
2312 gcc_assert (n < EXP_BITS);
2314 if (tens[n].cl == rvc_zero)
2315 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2317 return &tens[n];
2320 /* Returns N. */
2322 static const REAL_VALUE_TYPE *
2323 real_digit (int n)
2325 static REAL_VALUE_TYPE num[10];
2327 gcc_assert (n >= 0);
2328 gcc_assert (n <= 9);
2330 if (n > 0 && num[n].cl == rvc_zero)
2331 real_from_integer (&num[n], VOIDmode, n, UNSIGNED);
2333 return &num[n];
2336 /* Multiply R by 10**EXP. */
2338 static void
2339 times_pten (REAL_VALUE_TYPE *r, int exp)
2341 REAL_VALUE_TYPE pten, *rr;
2342 bool negative = (exp < 0);
2343 int i;
2345 if (negative)
2347 exp = -exp;
2348 pten = *real_digit (1);
2349 rr = &pten;
2351 else
2352 rr = r;
2354 for (i = 0; exp > 0; ++i, exp >>= 1)
2355 if (exp & 1)
2356 do_multiply (rr, rr, ten_to_ptwo (i));
2358 if (negative)
2359 do_divide (r, r, &pten);
2362 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2364 const REAL_VALUE_TYPE *
2365 dconst_e_ptr (void)
2367 static REAL_VALUE_TYPE value;
2369 /* Initialize mathematical constants for constant folding builtins.
2370 These constants need to be given to at least 160 bits precision. */
2371 if (value.cl == rvc_zero)
2373 mpfr_t m;
2374 mpfr_init2 (m, SIGNIFICAND_BITS);
2375 mpfr_set_ui (m, 1, GMP_RNDN);
2376 mpfr_exp (m, m, GMP_RNDN);
2377 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2378 mpfr_clear (m);
2381 return &value;
2384 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2386 const REAL_VALUE_TYPE *
2387 dconst_third_ptr (void)
2389 static REAL_VALUE_TYPE value;
2391 /* Initialize mathematical constants for constant folding builtins.
2392 These constants need to be given to at least 160 bits precision. */
2393 if (value.cl == rvc_zero)
2395 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3));
2397 return &value;
2400 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2402 const REAL_VALUE_TYPE *
2403 dconst_sqrt2_ptr (void)
2405 static REAL_VALUE_TYPE value;
2407 /* Initialize mathematical constants for constant folding builtins.
2408 These constants need to be given to at least 160 bits precision. */
2409 if (value.cl == rvc_zero)
2411 mpfr_t m;
2412 mpfr_init2 (m, SIGNIFICAND_BITS);
2413 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2414 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2415 mpfr_clear (m);
2417 return &value;
2420 /* Fills R with +Inf. */
2422 void
2423 real_inf (REAL_VALUE_TYPE *r)
2425 get_inf (r, 0);
2428 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2429 we force a QNaN, else we force an SNaN. The string, if not empty,
2430 is parsed as a number and placed in the significand. Return true
2431 if the string was successfully parsed. */
2433 bool
2434 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2435 machine_mode mode)
2437 const struct real_format *fmt;
2439 fmt = REAL_MODE_FORMAT (mode);
2440 gcc_assert (fmt);
2442 if (*str == 0)
2444 if (quiet)
2445 get_canonical_qnan (r, 0);
2446 else
2447 get_canonical_snan (r, 0);
2449 else
2451 int base = 10, d;
2453 memset (r, 0, sizeof (*r));
2454 r->cl = rvc_nan;
2456 /* Parse akin to strtol into the significand of R. */
2458 while (ISSPACE (*str))
2459 str++;
2460 if (*str == '-')
2461 str++;
2462 else if (*str == '+')
2463 str++;
2464 if (*str == '0')
2466 str++;
2467 if (*str == 'x' || *str == 'X')
2469 base = 16;
2470 str++;
2472 else
2473 base = 8;
2476 while ((d = hex_value (*str)) < base)
2478 REAL_VALUE_TYPE u;
2480 switch (base)
2482 case 8:
2483 lshift_significand (r, r, 3);
2484 break;
2485 case 16:
2486 lshift_significand (r, r, 4);
2487 break;
2488 case 10:
2489 lshift_significand_1 (&u, r);
2490 lshift_significand (r, r, 3);
2491 add_significands (r, r, &u);
2492 break;
2493 default:
2494 gcc_unreachable ();
2497 get_zero (&u, 0);
2498 u.sig[0] = d;
2499 add_significands (r, r, &u);
2501 str++;
2504 /* Must have consumed the entire string for success. */
2505 if (*str != 0)
2506 return false;
2508 /* Shift the significand into place such that the bits
2509 are in the most significant bits for the format. */
2510 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2512 /* Our MSB is always unset for NaNs. */
2513 r->sig[SIGSZ-1] &= ~SIG_MSB;
2515 /* Force quiet or signalling NaN. */
2516 r->signalling = !quiet;
2519 return true;
2522 /* Fills R with the largest finite value representable in mode MODE.
2523 If SIGN is nonzero, R is set to the most negative finite value. */
2525 void
2526 real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
2528 const struct real_format *fmt;
2529 int np2;
2531 fmt = REAL_MODE_FORMAT (mode);
2532 gcc_assert (fmt);
2533 memset (r, 0, sizeof (*r));
2535 if (fmt->b == 10)
2536 decimal_real_maxval (r, sign, mode);
2537 else
2539 r->cl = rvc_normal;
2540 r->sign = sign;
2541 SET_REAL_EXP (r, fmt->emax);
2543 np2 = SIGNIFICAND_BITS - fmt->p;
2544 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2545 clear_significand_below (r, np2);
2547 if (fmt->pnan < fmt->p)
2548 /* This is an IBM extended double format made up of two IEEE
2549 doubles. The value of the long double is the sum of the
2550 values of the two parts. The most significant part is
2551 required to be the value of the long double rounded to the
2552 nearest double. Rounding means we need a slightly smaller
2553 value for LDBL_MAX. */
2554 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2558 /* Fills R with 2**N. */
2560 void
2561 real_2expN (REAL_VALUE_TYPE *r, int n, machine_mode fmode)
2563 memset (r, 0, sizeof (*r));
2565 n++;
2566 if (n > MAX_EXP)
2567 r->cl = rvc_inf;
2568 else if (n < -MAX_EXP)
2570 else
2572 r->cl = rvc_normal;
2573 SET_REAL_EXP (r, n);
2574 r->sig[SIGSZ-1] = SIG_MSB;
2576 if (DECIMAL_FLOAT_MODE_P (fmode))
2577 decimal_real_convert (r, fmode, r);
2581 static void
2582 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2584 int p2, np2, i, w;
2585 int emin2m1, emax2;
2586 bool round_up = false;
2588 if (r->decimal)
2590 if (fmt->b == 10)
2592 decimal_round_for_format (fmt, r);
2593 return;
2595 /* FIXME. We can come here via fp_easy_constant
2596 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2597 investigated whether this convert needs to be here, or
2598 something else is missing. */
2599 decimal_real_convert (r, DFmode, r);
2602 p2 = fmt->p;
2603 emin2m1 = fmt->emin - 1;
2604 emax2 = fmt->emax;
2606 np2 = SIGNIFICAND_BITS - p2;
2607 switch (r->cl)
2609 underflow:
2610 get_zero (r, r->sign);
2611 case rvc_zero:
2612 if (!fmt->has_signed_zero)
2613 r->sign = 0;
2614 return;
2616 overflow:
2617 get_inf (r, r->sign);
2618 case rvc_inf:
2619 return;
2621 case rvc_nan:
2622 clear_significand_below (r, np2);
2623 return;
2625 case rvc_normal:
2626 break;
2628 default:
2629 gcc_unreachable ();
2632 /* Check the range of the exponent. If we're out of range,
2633 either underflow or overflow. */
2634 if (REAL_EXP (r) > emax2)
2635 goto overflow;
2636 else if (REAL_EXP (r) <= emin2m1)
2638 int diff;
2640 if (!fmt->has_denorm)
2642 /* Don't underflow completely until we've had a chance to round. */
2643 if (REAL_EXP (r) < emin2m1)
2644 goto underflow;
2646 else
2648 diff = emin2m1 - REAL_EXP (r) + 1;
2649 if (diff > p2)
2650 goto underflow;
2652 /* De-normalize the significand. */
2653 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2654 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2658 if (!fmt->round_towards_zero)
2660 /* There are P2 true significand bits, followed by one guard bit,
2661 followed by one sticky bit, followed by stuff. Fold nonzero
2662 stuff into the sticky bit. */
2663 unsigned long sticky;
2664 bool guard, lsb;
2666 sticky = 0;
2667 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2668 sticky |= r->sig[i];
2669 sticky |= r->sig[w]
2670 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2672 guard = test_significand_bit (r, np2 - 1);
2673 lsb = test_significand_bit (r, np2);
2675 /* Round to even. */
2676 round_up = guard && (sticky || lsb);
2679 if (round_up)
2681 REAL_VALUE_TYPE u;
2682 get_zero (&u, 0);
2683 set_significand_bit (&u, np2);
2685 if (add_significands (r, r, &u))
2687 /* Overflow. Means the significand had been all ones, and
2688 is now all zeros. Need to increase the exponent, and
2689 possibly re-normalize it. */
2690 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2691 if (REAL_EXP (r) > emax2)
2692 goto overflow;
2693 r->sig[SIGSZ-1] = SIG_MSB;
2697 /* Catch underflow that we deferred until after rounding. */
2698 if (REAL_EXP (r) <= emin2m1)
2699 goto underflow;
2701 /* Clear out trailing garbage. */
2702 clear_significand_below (r, np2);
2705 /* Extend or truncate to a new mode. */
2707 void
2708 real_convert (REAL_VALUE_TYPE *r, machine_mode mode,
2709 const REAL_VALUE_TYPE *a)
2711 const struct real_format *fmt;
2713 fmt = REAL_MODE_FORMAT (mode);
2714 gcc_assert (fmt);
2716 *r = *a;
2718 if (a->decimal || fmt->b == 10)
2719 decimal_real_convert (r, mode, a);
2721 round_for_format (fmt, r);
2723 /* round_for_format de-normalizes denormals. Undo just that part. */
2724 if (r->cl == rvc_normal)
2725 normalize (r);
2728 /* Legacy. Likewise, except return the struct directly. */
2730 REAL_VALUE_TYPE
2731 real_value_truncate (machine_mode mode, REAL_VALUE_TYPE a)
2733 REAL_VALUE_TYPE r;
2734 real_convert (&r, mode, &a);
2735 return r;
2738 /* Return true if truncating to MODE is exact. */
2740 bool
2741 exact_real_truncate (machine_mode mode, const REAL_VALUE_TYPE *a)
2743 const struct real_format *fmt;
2744 REAL_VALUE_TYPE t;
2745 int emin2m1;
2747 fmt = REAL_MODE_FORMAT (mode);
2748 gcc_assert (fmt);
2750 /* Don't allow conversion to denormals. */
2751 emin2m1 = fmt->emin - 1;
2752 if (REAL_EXP (a) <= emin2m1)
2753 return false;
2755 /* After conversion to the new mode, the value must be identical. */
2756 real_convert (&t, mode, a);
2757 return real_identical (&t, a);
2760 /* Write R to the given target format. Place the words of the result
2761 in target word order in BUF. There are always 32 bits in each
2762 long, no matter the size of the host long.
2764 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2766 long
2767 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2768 const struct real_format *fmt)
2770 REAL_VALUE_TYPE r;
2771 long buf1;
2773 r = *r_orig;
2774 round_for_format (fmt, &r);
2776 if (!buf)
2777 buf = &buf1;
2778 (*fmt->encode) (fmt, buf, &r);
2780 return *buf;
2783 /* Similar, but look up the format from MODE. */
2785 long
2786 real_to_target (long *buf, const REAL_VALUE_TYPE *r, machine_mode mode)
2788 const struct real_format *fmt;
2790 fmt = REAL_MODE_FORMAT (mode);
2791 gcc_assert (fmt);
2793 return real_to_target_fmt (buf, r, fmt);
2796 /* Read R from the given target format. Read the words of the result
2797 in target word order in BUF. There are always 32 bits in each
2798 long, no matter the size of the host long. */
2800 void
2801 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2802 const struct real_format *fmt)
2804 (*fmt->decode) (fmt, r, buf);
2807 /* Similar, but look up the format from MODE. */
2809 void
2810 real_from_target (REAL_VALUE_TYPE *r, const long *buf, machine_mode mode)
2812 const struct real_format *fmt;
2814 fmt = REAL_MODE_FORMAT (mode);
2815 gcc_assert (fmt);
2817 (*fmt->decode) (fmt, r, buf);
2820 /* Return the number of bits of the largest binary value that the
2821 significand of MODE will hold. */
2822 /* ??? Legacy. Should get access to real_format directly. */
2825 significand_size (machine_mode mode)
2827 const struct real_format *fmt;
2829 fmt = REAL_MODE_FORMAT (mode);
2830 if (fmt == NULL)
2831 return 0;
2833 if (fmt->b == 10)
2835 /* Return the size in bits of the largest binary value that can be
2836 held by the decimal coefficient for this mode. This is one more
2837 than the number of bits required to hold the largest coefficient
2838 of this mode. */
2839 double log2_10 = 3.3219281;
2840 return fmt->p * log2_10;
2842 return fmt->p;
2845 /* Return a hash value for the given real value. */
2846 /* ??? The "unsigned int" return value is intended to be hashval_t,
2847 but I didn't want to pull hashtab.h into real.h. */
2849 unsigned int
2850 real_hash (const REAL_VALUE_TYPE *r)
2852 unsigned int h;
2853 size_t i;
2855 h = r->cl | (r->sign << 2);
2856 switch (r->cl)
2858 case rvc_zero:
2859 case rvc_inf:
2860 return h;
2862 case rvc_normal:
2863 h |= REAL_EXP (r) << 3;
2864 break;
2866 case rvc_nan:
2867 if (r->signalling)
2868 h ^= (unsigned int)-1;
2869 if (r->canonical)
2870 return h;
2871 break;
2873 default:
2874 gcc_unreachable ();
2877 if (sizeof (unsigned long) > sizeof (unsigned int))
2878 for (i = 0; i < SIGSZ; ++i)
2880 unsigned long s = r->sig[i];
2881 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2883 else
2884 for (i = 0; i < SIGSZ; ++i)
2885 h ^= r->sig[i];
2887 return h;
2890 /* IEEE single-precision format. */
2892 static void encode_ieee_single (const struct real_format *fmt,
2893 long *, const REAL_VALUE_TYPE *);
2894 static void decode_ieee_single (const struct real_format *,
2895 REAL_VALUE_TYPE *, const long *);
2897 static void
2898 encode_ieee_single (const struct real_format *fmt, long *buf,
2899 const REAL_VALUE_TYPE *r)
2901 unsigned long image, sig, exp;
2902 unsigned long sign = r->sign;
2903 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2905 image = sign << 31;
2906 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2908 switch (r->cl)
2910 case rvc_zero:
2911 break;
2913 case rvc_inf:
2914 if (fmt->has_inf)
2915 image |= 255 << 23;
2916 else
2917 image |= 0x7fffffff;
2918 break;
2920 case rvc_nan:
2921 if (fmt->has_nans)
2923 if (r->canonical)
2924 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2925 if (r->signalling == fmt->qnan_msb_set)
2926 sig &= ~(1 << 22);
2927 else
2928 sig |= 1 << 22;
2929 if (sig == 0)
2930 sig = 1 << 21;
2932 image |= 255 << 23;
2933 image |= sig;
2935 else
2936 image |= 0x7fffffff;
2937 break;
2939 case rvc_normal:
2940 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2941 whereas the intermediate representation is 0.F x 2**exp.
2942 Which means we're off by one. */
2943 if (denormal)
2944 exp = 0;
2945 else
2946 exp = REAL_EXP (r) + 127 - 1;
2947 image |= exp << 23;
2948 image |= sig;
2949 break;
2951 default:
2952 gcc_unreachable ();
2955 buf[0] = image;
2958 static void
2959 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2960 const long *buf)
2962 unsigned long image = buf[0] & 0xffffffff;
2963 bool sign = (image >> 31) & 1;
2964 int exp = (image >> 23) & 0xff;
2966 memset (r, 0, sizeof (*r));
2967 image <<= HOST_BITS_PER_LONG - 24;
2968 image &= ~SIG_MSB;
2970 if (exp == 0)
2972 if (image && fmt->has_denorm)
2974 r->cl = rvc_normal;
2975 r->sign = sign;
2976 SET_REAL_EXP (r, -126);
2977 r->sig[SIGSZ-1] = image << 1;
2978 normalize (r);
2980 else if (fmt->has_signed_zero)
2981 r->sign = sign;
2983 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2985 if (image)
2987 r->cl = rvc_nan;
2988 r->sign = sign;
2989 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2990 ^ fmt->qnan_msb_set);
2991 r->sig[SIGSZ-1] = image;
2993 else
2995 r->cl = rvc_inf;
2996 r->sign = sign;
2999 else
3001 r->cl = rvc_normal;
3002 r->sign = sign;
3003 SET_REAL_EXP (r, exp - 127 + 1);
3004 r->sig[SIGSZ-1] = image | SIG_MSB;
3008 const struct real_format ieee_single_format =
3010 encode_ieee_single,
3011 decode_ieee_single,
3015 -125,
3016 128,
3019 false,
3020 true,
3021 true,
3022 true,
3023 true,
3024 true,
3025 true,
3026 false,
3027 "ieee_single"
3030 const struct real_format mips_single_format =
3032 encode_ieee_single,
3033 decode_ieee_single,
3037 -125,
3038 128,
3041 false,
3042 true,
3043 true,
3044 true,
3045 true,
3046 true,
3047 false,
3048 true,
3049 "mips_single"
3052 const struct real_format motorola_single_format =
3054 encode_ieee_single,
3055 decode_ieee_single,
3059 -125,
3060 128,
3063 false,
3064 true,
3065 true,
3066 true,
3067 true,
3068 true,
3069 true,
3070 true,
3071 "motorola_single"
3074 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3075 single precision with the following differences:
3076 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3077 are generated.
3078 - NaNs are not supported.
3079 - The range of non-zero numbers in binary is
3080 (001)[1.]000...000 to (255)[1.]111...111.
3081 - Denormals can be represented, but are treated as +0.0 when
3082 used as an operand and are never generated as a result.
3083 - -0.0 can be represented, but a zero result is always +0.0.
3084 - the only supported rounding mode is trunction (towards zero). */
3085 const struct real_format spu_single_format =
3087 encode_ieee_single,
3088 decode_ieee_single,
3092 -125,
3093 129,
3096 true,
3097 false,
3098 false,
3099 false,
3100 true,
3101 true,
3102 false,
3103 false,
3104 "spu_single"
3107 /* IEEE double-precision format. */
3109 static void encode_ieee_double (const struct real_format *fmt,
3110 long *, const REAL_VALUE_TYPE *);
3111 static void decode_ieee_double (const struct real_format *,
3112 REAL_VALUE_TYPE *, const long *);
3114 static void
3115 encode_ieee_double (const struct real_format *fmt, long *buf,
3116 const REAL_VALUE_TYPE *r)
3118 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3119 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3121 image_hi = r->sign << 31;
3122 image_lo = 0;
3124 if (HOST_BITS_PER_LONG == 64)
3126 sig_hi = r->sig[SIGSZ-1];
3127 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3128 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3130 else
3132 sig_hi = r->sig[SIGSZ-1];
3133 sig_lo = r->sig[SIGSZ-2];
3134 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3135 sig_hi = (sig_hi >> 11) & 0xfffff;
3138 switch (r->cl)
3140 case rvc_zero:
3141 break;
3143 case rvc_inf:
3144 if (fmt->has_inf)
3145 image_hi |= 2047 << 20;
3146 else
3148 image_hi |= 0x7fffffff;
3149 image_lo = 0xffffffff;
3151 break;
3153 case rvc_nan:
3154 if (fmt->has_nans)
3156 if (r->canonical)
3158 if (fmt->canonical_nan_lsbs_set)
3160 sig_hi = (1 << 19) - 1;
3161 sig_lo = 0xffffffff;
3163 else
3165 sig_hi = 0;
3166 sig_lo = 0;
3169 if (r->signalling == fmt->qnan_msb_set)
3170 sig_hi &= ~(1 << 19);
3171 else
3172 sig_hi |= 1 << 19;
3173 if (sig_hi == 0 && sig_lo == 0)
3174 sig_hi = 1 << 18;
3176 image_hi |= 2047 << 20;
3177 image_hi |= sig_hi;
3178 image_lo = sig_lo;
3180 else
3182 image_hi |= 0x7fffffff;
3183 image_lo = 0xffffffff;
3185 break;
3187 case rvc_normal:
3188 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3189 whereas the intermediate representation is 0.F x 2**exp.
3190 Which means we're off by one. */
3191 if (denormal)
3192 exp = 0;
3193 else
3194 exp = REAL_EXP (r) + 1023 - 1;
3195 image_hi |= exp << 20;
3196 image_hi |= sig_hi;
3197 image_lo = sig_lo;
3198 break;
3200 default:
3201 gcc_unreachable ();
3204 if (FLOAT_WORDS_BIG_ENDIAN)
3205 buf[0] = image_hi, buf[1] = image_lo;
3206 else
3207 buf[0] = image_lo, buf[1] = image_hi;
3210 static void
3211 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3212 const long *buf)
3214 unsigned long image_hi, image_lo;
3215 bool sign;
3216 int exp;
3218 if (FLOAT_WORDS_BIG_ENDIAN)
3219 image_hi = buf[0], image_lo = buf[1];
3220 else
3221 image_lo = buf[0], image_hi = buf[1];
3222 image_lo &= 0xffffffff;
3223 image_hi &= 0xffffffff;
3225 sign = (image_hi >> 31) & 1;
3226 exp = (image_hi >> 20) & 0x7ff;
3228 memset (r, 0, sizeof (*r));
3230 image_hi <<= 32 - 21;
3231 image_hi |= image_lo >> 21;
3232 image_hi &= 0x7fffffff;
3233 image_lo <<= 32 - 21;
3235 if (exp == 0)
3237 if ((image_hi || image_lo) && fmt->has_denorm)
3239 r->cl = rvc_normal;
3240 r->sign = sign;
3241 SET_REAL_EXP (r, -1022);
3242 if (HOST_BITS_PER_LONG == 32)
3244 image_hi = (image_hi << 1) | (image_lo >> 31);
3245 image_lo <<= 1;
3246 r->sig[SIGSZ-1] = image_hi;
3247 r->sig[SIGSZ-2] = image_lo;
3249 else
3251 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3252 r->sig[SIGSZ-1] = image_hi;
3254 normalize (r);
3256 else if (fmt->has_signed_zero)
3257 r->sign = sign;
3259 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3261 if (image_hi || image_lo)
3263 r->cl = rvc_nan;
3264 r->sign = sign;
3265 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3266 if (HOST_BITS_PER_LONG == 32)
3268 r->sig[SIGSZ-1] = image_hi;
3269 r->sig[SIGSZ-2] = image_lo;
3271 else
3272 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3274 else
3276 r->cl = rvc_inf;
3277 r->sign = sign;
3280 else
3282 r->cl = rvc_normal;
3283 r->sign = sign;
3284 SET_REAL_EXP (r, exp - 1023 + 1);
3285 if (HOST_BITS_PER_LONG == 32)
3287 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3288 r->sig[SIGSZ-2] = image_lo;
3290 else
3291 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3295 const struct real_format ieee_double_format =
3297 encode_ieee_double,
3298 decode_ieee_double,
3302 -1021,
3303 1024,
3306 false,
3307 true,
3308 true,
3309 true,
3310 true,
3311 true,
3312 true,
3313 false,
3314 "ieee_double"
3317 const struct real_format mips_double_format =
3319 encode_ieee_double,
3320 decode_ieee_double,
3324 -1021,
3325 1024,
3328 false,
3329 true,
3330 true,
3331 true,
3332 true,
3333 true,
3334 false,
3335 true,
3336 "mips_double"
3339 const struct real_format motorola_double_format =
3341 encode_ieee_double,
3342 decode_ieee_double,
3346 -1021,
3347 1024,
3350 false,
3351 true,
3352 true,
3353 true,
3354 true,
3355 true,
3356 true,
3357 true,
3358 "motorola_double"
3361 /* IEEE extended real format. This comes in three flavors: Intel's as
3362 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3363 12- and 16-byte images may be big- or little endian; Motorola's is
3364 always big endian. */
3366 /* Helper subroutine which converts from the internal format to the
3367 12-byte little-endian Intel format. Functions below adjust this
3368 for the other possible formats. */
3369 static void
3370 encode_ieee_extended (const struct real_format *fmt, long *buf,
3371 const REAL_VALUE_TYPE *r)
3373 unsigned long image_hi, sig_hi, sig_lo;
3374 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3376 image_hi = r->sign << 15;
3377 sig_hi = sig_lo = 0;
3379 switch (r->cl)
3381 case rvc_zero:
3382 break;
3384 case rvc_inf:
3385 if (fmt->has_inf)
3387 image_hi |= 32767;
3389 /* Intel requires the explicit integer bit to be set, otherwise
3390 it considers the value a "pseudo-infinity". Motorola docs
3391 say it doesn't care. */
3392 sig_hi = 0x80000000;
3394 else
3396 image_hi |= 32767;
3397 sig_lo = sig_hi = 0xffffffff;
3399 break;
3401 case rvc_nan:
3402 if (fmt->has_nans)
3404 image_hi |= 32767;
3405 if (r->canonical)
3407 if (fmt->canonical_nan_lsbs_set)
3409 sig_hi = (1 << 30) - 1;
3410 sig_lo = 0xffffffff;
3413 else if (HOST_BITS_PER_LONG == 32)
3415 sig_hi = r->sig[SIGSZ-1];
3416 sig_lo = r->sig[SIGSZ-2];
3418 else
3420 sig_lo = r->sig[SIGSZ-1];
3421 sig_hi = sig_lo >> 31 >> 1;
3422 sig_lo &= 0xffffffff;
3424 if (r->signalling == fmt->qnan_msb_set)
3425 sig_hi &= ~(1 << 30);
3426 else
3427 sig_hi |= 1 << 30;
3428 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3429 sig_hi = 1 << 29;
3431 /* Intel requires the explicit integer bit to be set, otherwise
3432 it considers the value a "pseudo-nan". Motorola docs say it
3433 doesn't care. */
3434 sig_hi |= 0x80000000;
3436 else
3438 image_hi |= 32767;
3439 sig_lo = sig_hi = 0xffffffff;
3441 break;
3443 case rvc_normal:
3445 int exp = REAL_EXP (r);
3447 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3448 whereas the intermediate representation is 0.F x 2**exp.
3449 Which means we're off by one.
3451 Except for Motorola, which consider exp=0 and explicit
3452 integer bit set to continue to be normalized. In theory
3453 this discrepancy has been taken care of by the difference
3454 in fmt->emin in round_for_format. */
3456 if (denormal)
3457 exp = 0;
3458 else
3460 exp += 16383 - 1;
3461 gcc_assert (exp >= 0);
3463 image_hi |= exp;
3465 if (HOST_BITS_PER_LONG == 32)
3467 sig_hi = r->sig[SIGSZ-1];
3468 sig_lo = r->sig[SIGSZ-2];
3470 else
3472 sig_lo = r->sig[SIGSZ-1];
3473 sig_hi = sig_lo >> 31 >> 1;
3474 sig_lo &= 0xffffffff;
3477 break;
3479 default:
3480 gcc_unreachable ();
3483 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3486 /* Convert from the internal format to the 12-byte Motorola format
3487 for an IEEE extended real. */
3488 static void
3489 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3490 const REAL_VALUE_TYPE *r)
3492 long intermed[3];
3493 encode_ieee_extended (fmt, intermed, r);
3495 if (r->cl == rvc_inf)
3496 /* For infinity clear the explicit integer bit again, so that the
3497 format matches the canonical infinity generated by the FPU. */
3498 intermed[1] = 0;
3500 /* Motorola chips are assumed always to be big-endian. Also, the
3501 padding in a Motorola extended real goes between the exponent and
3502 the mantissa. At this point the mantissa is entirely within
3503 elements 0 and 1 of intermed, and the exponent entirely within
3504 element 2, so all we have to do is swap the order around, and
3505 shift element 2 left 16 bits. */
3506 buf[0] = intermed[2] << 16;
3507 buf[1] = intermed[1];
3508 buf[2] = intermed[0];
3511 /* Convert from the internal format to the 12-byte Intel format for
3512 an IEEE extended real. */
3513 static void
3514 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3515 const REAL_VALUE_TYPE *r)
3517 if (FLOAT_WORDS_BIG_ENDIAN)
3519 /* All the padding in an Intel-format extended real goes at the high
3520 end, which in this case is after the mantissa, not the exponent.
3521 Therefore we must shift everything down 16 bits. */
3522 long intermed[3];
3523 encode_ieee_extended (fmt, intermed, r);
3524 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3525 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3526 buf[2] = (intermed[0] << 16);
3528 else
3529 /* encode_ieee_extended produces what we want directly. */
3530 encode_ieee_extended (fmt, buf, r);
3533 /* Convert from the internal format to the 16-byte Intel format for
3534 an IEEE extended real. */
3535 static void
3536 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3537 const REAL_VALUE_TYPE *r)
3539 /* All the padding in an Intel-format extended real goes at the high end. */
3540 encode_ieee_extended_intel_96 (fmt, buf, r);
3541 buf[3] = 0;
3544 /* As above, we have a helper function which converts from 12-byte
3545 little-endian Intel format to internal format. Functions below
3546 adjust for the other possible formats. */
3547 static void
3548 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3549 const long *buf)
3551 unsigned long image_hi, sig_hi, sig_lo;
3552 bool sign;
3553 int exp;
3555 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3556 sig_lo &= 0xffffffff;
3557 sig_hi &= 0xffffffff;
3558 image_hi &= 0xffffffff;
3560 sign = (image_hi >> 15) & 1;
3561 exp = image_hi & 0x7fff;
3563 memset (r, 0, sizeof (*r));
3565 if (exp == 0)
3567 if ((sig_hi || sig_lo) && fmt->has_denorm)
3569 r->cl = rvc_normal;
3570 r->sign = sign;
3572 /* When the IEEE format contains a hidden bit, we know that
3573 it's zero at this point, and so shift up the significand
3574 and decrease the exponent to match. In this case, Motorola
3575 defines the explicit integer bit to be valid, so we don't
3576 know whether the msb is set or not. */
3577 SET_REAL_EXP (r, fmt->emin);
3578 if (HOST_BITS_PER_LONG == 32)
3580 r->sig[SIGSZ-1] = sig_hi;
3581 r->sig[SIGSZ-2] = sig_lo;
3583 else
3584 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3586 normalize (r);
3588 else if (fmt->has_signed_zero)
3589 r->sign = sign;
3591 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3593 /* See above re "pseudo-infinities" and "pseudo-nans".
3594 Short summary is that the MSB will likely always be
3595 set, and that we don't care about it. */
3596 sig_hi &= 0x7fffffff;
3598 if (sig_hi || sig_lo)
3600 r->cl = rvc_nan;
3601 r->sign = sign;
3602 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3603 if (HOST_BITS_PER_LONG == 32)
3605 r->sig[SIGSZ-1] = sig_hi;
3606 r->sig[SIGSZ-2] = sig_lo;
3608 else
3609 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3611 else
3613 r->cl = rvc_inf;
3614 r->sign = sign;
3617 else
3619 r->cl = rvc_normal;
3620 r->sign = sign;
3621 SET_REAL_EXP (r, exp - 16383 + 1);
3622 if (HOST_BITS_PER_LONG == 32)
3624 r->sig[SIGSZ-1] = sig_hi;
3625 r->sig[SIGSZ-2] = sig_lo;
3627 else
3628 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3632 /* Convert from the internal format to the 12-byte Motorola format
3633 for an IEEE extended real. */
3634 static void
3635 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3636 const long *buf)
3638 long intermed[3];
3640 /* Motorola chips are assumed always to be big-endian. Also, the
3641 padding in a Motorola extended real goes between the exponent and
3642 the mantissa; remove it. */
3643 intermed[0] = buf[2];
3644 intermed[1] = buf[1];
3645 intermed[2] = (unsigned long)buf[0] >> 16;
3647 decode_ieee_extended (fmt, r, intermed);
3650 /* Convert from the internal format to the 12-byte Intel format for
3651 an IEEE extended real. */
3652 static void
3653 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3654 const long *buf)
3656 if (FLOAT_WORDS_BIG_ENDIAN)
3658 /* All the padding in an Intel-format extended real goes at the high
3659 end, which in this case is after the mantissa, not the exponent.
3660 Therefore we must shift everything up 16 bits. */
3661 long intermed[3];
3663 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3664 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3665 intermed[2] = ((unsigned long)buf[0] >> 16);
3667 decode_ieee_extended (fmt, r, intermed);
3669 else
3670 /* decode_ieee_extended produces what we want directly. */
3671 decode_ieee_extended (fmt, r, buf);
3674 /* Convert from the internal format to the 16-byte Intel format for
3675 an IEEE extended real. */
3676 static void
3677 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3678 const long *buf)
3680 /* All the padding in an Intel-format extended real goes at the high end. */
3681 decode_ieee_extended_intel_96 (fmt, r, buf);
3684 const struct real_format ieee_extended_motorola_format =
3686 encode_ieee_extended_motorola,
3687 decode_ieee_extended_motorola,
3691 -16382,
3692 16384,
3695 false,
3696 true,
3697 true,
3698 true,
3699 true,
3700 true,
3701 true,
3702 true,
3703 "ieee_extended_motorola"
3706 const struct real_format ieee_extended_intel_96_format =
3708 encode_ieee_extended_intel_96,
3709 decode_ieee_extended_intel_96,
3713 -16381,
3714 16384,
3717 false,
3718 true,
3719 true,
3720 true,
3721 true,
3722 true,
3723 true,
3724 false,
3725 "ieee_extended_intel_96"
3728 const struct real_format ieee_extended_intel_128_format =
3730 encode_ieee_extended_intel_128,
3731 decode_ieee_extended_intel_128,
3735 -16381,
3736 16384,
3739 false,
3740 true,
3741 true,
3742 true,
3743 true,
3744 true,
3745 true,
3746 false,
3747 "ieee_extended_intel_128"
3750 /* The following caters to i386 systems that set the rounding precision
3751 to 53 bits instead of 64, e.g. FreeBSD. */
3752 const struct real_format ieee_extended_intel_96_round_53_format =
3754 encode_ieee_extended_intel_96,
3755 decode_ieee_extended_intel_96,
3759 -16381,
3760 16384,
3763 false,
3764 true,
3765 true,
3766 true,
3767 true,
3768 true,
3769 true,
3770 false,
3771 "ieee_extended_intel_96_round_53"
3774 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3775 numbers whose sum is equal to the extended precision value. The number
3776 with greater magnitude is first. This format has the same magnitude
3777 range as an IEEE double precision value, but effectively 106 bits of
3778 significand precision. Infinity and NaN are represented by their IEEE
3779 double precision value stored in the first number, the second number is
3780 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3782 static void encode_ibm_extended (const struct real_format *fmt,
3783 long *, const REAL_VALUE_TYPE *);
3784 static void decode_ibm_extended (const struct real_format *,
3785 REAL_VALUE_TYPE *, const long *);
3787 static void
3788 encode_ibm_extended (const struct real_format *fmt, long *buf,
3789 const REAL_VALUE_TYPE *r)
3791 REAL_VALUE_TYPE u, normr, v;
3792 const struct real_format *base_fmt;
3794 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3796 /* Renormalize R before doing any arithmetic on it. */
3797 normr = *r;
3798 if (normr.cl == rvc_normal)
3799 normalize (&normr);
3801 /* u = IEEE double precision portion of significand. */
3802 u = normr;
3803 round_for_format (base_fmt, &u);
3804 encode_ieee_double (base_fmt, &buf[0], &u);
3806 if (u.cl == rvc_normal)
3808 do_add (&v, &normr, &u, 1);
3809 /* Call round_for_format since we might need to denormalize. */
3810 round_for_format (base_fmt, &v);
3811 encode_ieee_double (base_fmt, &buf[2], &v);
3813 else
3815 /* Inf, NaN, 0 are all representable as doubles, so the
3816 least-significant part can be 0.0. */
3817 buf[2] = 0;
3818 buf[3] = 0;
3822 static void
3823 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3824 const long *buf)
3826 REAL_VALUE_TYPE u, v;
3827 const struct real_format *base_fmt;
3829 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3830 decode_ieee_double (base_fmt, &u, &buf[0]);
3832 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3834 decode_ieee_double (base_fmt, &v, &buf[2]);
3835 do_add (r, &u, &v, 0);
3837 else
3838 *r = u;
3841 const struct real_format ibm_extended_format =
3843 encode_ibm_extended,
3844 decode_ibm_extended,
3846 53 + 53,
3848 -1021 + 53,
3849 1024,
3850 127,
3852 false,
3853 true,
3854 true,
3855 true,
3856 true,
3857 true,
3858 true,
3859 false,
3860 "ibm_extended"
3863 const struct real_format mips_extended_format =
3865 encode_ibm_extended,
3866 decode_ibm_extended,
3868 53 + 53,
3870 -1021 + 53,
3871 1024,
3872 127,
3874 false,
3875 true,
3876 true,
3877 true,
3878 true,
3879 true,
3880 false,
3881 true,
3882 "mips_extended"
3886 /* IEEE quad precision format. */
3888 static void encode_ieee_quad (const struct real_format *fmt,
3889 long *, const REAL_VALUE_TYPE *);
3890 static void decode_ieee_quad (const struct real_format *,
3891 REAL_VALUE_TYPE *, const long *);
3893 static void
3894 encode_ieee_quad (const struct real_format *fmt, long *buf,
3895 const REAL_VALUE_TYPE *r)
3897 unsigned long image3, image2, image1, image0, exp;
3898 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3899 REAL_VALUE_TYPE u;
3901 image3 = r->sign << 31;
3902 image2 = 0;
3903 image1 = 0;
3904 image0 = 0;
3906 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3908 switch (r->cl)
3910 case rvc_zero:
3911 break;
3913 case rvc_inf:
3914 if (fmt->has_inf)
3915 image3 |= 32767 << 16;
3916 else
3918 image3 |= 0x7fffffff;
3919 image2 = 0xffffffff;
3920 image1 = 0xffffffff;
3921 image0 = 0xffffffff;
3923 break;
3925 case rvc_nan:
3926 if (fmt->has_nans)
3928 image3 |= 32767 << 16;
3930 if (r->canonical)
3932 if (fmt->canonical_nan_lsbs_set)
3934 image3 |= 0x7fff;
3935 image2 = image1 = image0 = 0xffffffff;
3938 else if (HOST_BITS_PER_LONG == 32)
3940 image0 = u.sig[0];
3941 image1 = u.sig[1];
3942 image2 = u.sig[2];
3943 image3 |= u.sig[3] & 0xffff;
3945 else
3947 image0 = u.sig[0];
3948 image1 = image0 >> 31 >> 1;
3949 image2 = u.sig[1];
3950 image3 |= (image2 >> 31 >> 1) & 0xffff;
3951 image0 &= 0xffffffff;
3952 image2 &= 0xffffffff;
3954 if (r->signalling == fmt->qnan_msb_set)
3955 image3 &= ~0x8000;
3956 else
3957 image3 |= 0x8000;
3958 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3959 image3 |= 0x4000;
3961 else
3963 image3 |= 0x7fffffff;
3964 image2 = 0xffffffff;
3965 image1 = 0xffffffff;
3966 image0 = 0xffffffff;
3968 break;
3970 case rvc_normal:
3971 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3972 whereas the intermediate representation is 0.F x 2**exp.
3973 Which means we're off by one. */
3974 if (denormal)
3975 exp = 0;
3976 else
3977 exp = REAL_EXP (r) + 16383 - 1;
3978 image3 |= exp << 16;
3980 if (HOST_BITS_PER_LONG == 32)
3982 image0 = u.sig[0];
3983 image1 = u.sig[1];
3984 image2 = u.sig[2];
3985 image3 |= u.sig[3] & 0xffff;
3987 else
3989 image0 = u.sig[0];
3990 image1 = image0 >> 31 >> 1;
3991 image2 = u.sig[1];
3992 image3 |= (image2 >> 31 >> 1) & 0xffff;
3993 image0 &= 0xffffffff;
3994 image2 &= 0xffffffff;
3996 break;
3998 default:
3999 gcc_unreachable ();
4002 if (FLOAT_WORDS_BIG_ENDIAN)
4004 buf[0] = image3;
4005 buf[1] = image2;
4006 buf[2] = image1;
4007 buf[3] = image0;
4009 else
4011 buf[0] = image0;
4012 buf[1] = image1;
4013 buf[2] = image2;
4014 buf[3] = image3;
4018 static void
4019 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4020 const long *buf)
4022 unsigned long image3, image2, image1, image0;
4023 bool sign;
4024 int exp;
4026 if (FLOAT_WORDS_BIG_ENDIAN)
4028 image3 = buf[0];
4029 image2 = buf[1];
4030 image1 = buf[2];
4031 image0 = buf[3];
4033 else
4035 image0 = buf[0];
4036 image1 = buf[1];
4037 image2 = buf[2];
4038 image3 = buf[3];
4040 image0 &= 0xffffffff;
4041 image1 &= 0xffffffff;
4042 image2 &= 0xffffffff;
4044 sign = (image3 >> 31) & 1;
4045 exp = (image3 >> 16) & 0x7fff;
4046 image3 &= 0xffff;
4048 memset (r, 0, sizeof (*r));
4050 if (exp == 0)
4052 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4054 r->cl = rvc_normal;
4055 r->sign = sign;
4057 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4058 if (HOST_BITS_PER_LONG == 32)
4060 r->sig[0] = image0;
4061 r->sig[1] = image1;
4062 r->sig[2] = image2;
4063 r->sig[3] = image3;
4065 else
4067 r->sig[0] = (image1 << 31 << 1) | image0;
4068 r->sig[1] = (image3 << 31 << 1) | image2;
4071 normalize (r);
4073 else if (fmt->has_signed_zero)
4074 r->sign = sign;
4076 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4078 if (image3 | image2 | image1 | image0)
4080 r->cl = rvc_nan;
4081 r->sign = sign;
4082 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4084 if (HOST_BITS_PER_LONG == 32)
4086 r->sig[0] = image0;
4087 r->sig[1] = image1;
4088 r->sig[2] = image2;
4089 r->sig[3] = image3;
4091 else
4093 r->sig[0] = (image1 << 31 << 1) | image0;
4094 r->sig[1] = (image3 << 31 << 1) | image2;
4096 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4098 else
4100 r->cl = rvc_inf;
4101 r->sign = sign;
4104 else
4106 r->cl = rvc_normal;
4107 r->sign = sign;
4108 SET_REAL_EXP (r, exp - 16383 + 1);
4110 if (HOST_BITS_PER_LONG == 32)
4112 r->sig[0] = image0;
4113 r->sig[1] = image1;
4114 r->sig[2] = image2;
4115 r->sig[3] = image3;
4117 else
4119 r->sig[0] = (image1 << 31 << 1) | image0;
4120 r->sig[1] = (image3 << 31 << 1) | image2;
4122 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4123 r->sig[SIGSZ-1] |= SIG_MSB;
4127 const struct real_format ieee_quad_format =
4129 encode_ieee_quad,
4130 decode_ieee_quad,
4132 113,
4133 113,
4134 -16381,
4135 16384,
4136 127,
4137 127,
4138 false,
4139 true,
4140 true,
4141 true,
4142 true,
4143 true,
4144 true,
4145 false,
4146 "ieee_quad"
4149 const struct real_format mips_quad_format =
4151 encode_ieee_quad,
4152 decode_ieee_quad,
4154 113,
4155 113,
4156 -16381,
4157 16384,
4158 127,
4159 127,
4160 false,
4161 true,
4162 true,
4163 true,
4164 true,
4165 true,
4166 false,
4167 true,
4168 "mips_quad"
4171 /* Descriptions of VAX floating point formats can be found beginning at
4173 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4175 The thing to remember is that they're almost IEEE, except for word
4176 order, exponent bias, and the lack of infinities, nans, and denormals.
4178 We don't implement the H_floating format here, simply because neither
4179 the VAX or Alpha ports use it. */
4181 static void encode_vax_f (const struct real_format *fmt,
4182 long *, const REAL_VALUE_TYPE *);
4183 static void decode_vax_f (const struct real_format *,
4184 REAL_VALUE_TYPE *, const long *);
4185 static void encode_vax_d (const struct real_format *fmt,
4186 long *, const REAL_VALUE_TYPE *);
4187 static void decode_vax_d (const struct real_format *,
4188 REAL_VALUE_TYPE *, const long *);
4189 static void encode_vax_g (const struct real_format *fmt,
4190 long *, const REAL_VALUE_TYPE *);
4191 static void decode_vax_g (const struct real_format *,
4192 REAL_VALUE_TYPE *, const long *);
4194 static void
4195 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4196 const REAL_VALUE_TYPE *r)
4198 unsigned long sign, exp, sig, image;
4200 sign = r->sign << 15;
4202 switch (r->cl)
4204 case rvc_zero:
4205 image = 0;
4206 break;
4208 case rvc_inf:
4209 case rvc_nan:
4210 image = 0xffff7fff | sign;
4211 break;
4213 case rvc_normal:
4214 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4215 exp = REAL_EXP (r) + 128;
4217 image = (sig << 16) & 0xffff0000;
4218 image |= sign;
4219 image |= exp << 7;
4220 image |= sig >> 16;
4221 break;
4223 default:
4224 gcc_unreachable ();
4227 buf[0] = image;
4230 static void
4231 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4232 REAL_VALUE_TYPE *r, const long *buf)
4234 unsigned long image = buf[0] & 0xffffffff;
4235 int exp = (image >> 7) & 0xff;
4237 memset (r, 0, sizeof (*r));
4239 if (exp != 0)
4241 r->cl = rvc_normal;
4242 r->sign = (image >> 15) & 1;
4243 SET_REAL_EXP (r, exp - 128);
4245 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4246 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4250 static void
4251 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4252 const REAL_VALUE_TYPE *r)
4254 unsigned long image0, image1, sign = r->sign << 15;
4256 switch (r->cl)
4258 case rvc_zero:
4259 image0 = image1 = 0;
4260 break;
4262 case rvc_inf:
4263 case rvc_nan:
4264 image0 = 0xffff7fff | sign;
4265 image1 = 0xffffffff;
4266 break;
4268 case rvc_normal:
4269 /* Extract the significand into straight hi:lo. */
4270 if (HOST_BITS_PER_LONG == 64)
4272 image0 = r->sig[SIGSZ-1];
4273 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4274 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4276 else
4278 image0 = r->sig[SIGSZ-1];
4279 image1 = r->sig[SIGSZ-2];
4280 image1 = (image0 << 24) | (image1 >> 8);
4281 image0 = (image0 >> 8) & 0xffffff;
4284 /* Rearrange the half-words of the significand to match the
4285 external format. */
4286 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4287 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4289 /* Add the sign and exponent. */
4290 image0 |= sign;
4291 image0 |= (REAL_EXP (r) + 128) << 7;
4292 break;
4294 default:
4295 gcc_unreachable ();
4298 if (FLOAT_WORDS_BIG_ENDIAN)
4299 buf[0] = image1, buf[1] = image0;
4300 else
4301 buf[0] = image0, buf[1] = image1;
4304 static void
4305 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4306 REAL_VALUE_TYPE *r, const long *buf)
4308 unsigned long image0, image1;
4309 int exp;
4311 if (FLOAT_WORDS_BIG_ENDIAN)
4312 image1 = buf[0], image0 = buf[1];
4313 else
4314 image0 = buf[0], image1 = buf[1];
4315 image0 &= 0xffffffff;
4316 image1 &= 0xffffffff;
4318 exp = (image0 >> 7) & 0xff;
4320 memset (r, 0, sizeof (*r));
4322 if (exp != 0)
4324 r->cl = rvc_normal;
4325 r->sign = (image0 >> 15) & 1;
4326 SET_REAL_EXP (r, exp - 128);
4328 /* Rearrange the half-words of the external format into
4329 proper ascending order. */
4330 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4331 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4333 if (HOST_BITS_PER_LONG == 64)
4335 image0 = (image0 << 31 << 1) | image1;
4336 image0 <<= 64 - 56;
4337 image0 |= SIG_MSB;
4338 r->sig[SIGSZ-1] = image0;
4340 else
4342 r->sig[SIGSZ-1] = image0;
4343 r->sig[SIGSZ-2] = image1;
4344 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4345 r->sig[SIGSZ-1] |= SIG_MSB;
4350 static void
4351 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4352 const REAL_VALUE_TYPE *r)
4354 unsigned long image0, image1, sign = r->sign << 15;
4356 switch (r->cl)
4358 case rvc_zero:
4359 image0 = image1 = 0;
4360 break;
4362 case rvc_inf:
4363 case rvc_nan:
4364 image0 = 0xffff7fff | sign;
4365 image1 = 0xffffffff;
4366 break;
4368 case rvc_normal:
4369 /* Extract the significand into straight hi:lo. */
4370 if (HOST_BITS_PER_LONG == 64)
4372 image0 = r->sig[SIGSZ-1];
4373 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4374 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4376 else
4378 image0 = r->sig[SIGSZ-1];
4379 image1 = r->sig[SIGSZ-2];
4380 image1 = (image0 << 21) | (image1 >> 11);
4381 image0 = (image0 >> 11) & 0xfffff;
4384 /* Rearrange the half-words of the significand to match the
4385 external format. */
4386 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4387 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4389 /* Add the sign and exponent. */
4390 image0 |= sign;
4391 image0 |= (REAL_EXP (r) + 1024) << 4;
4392 break;
4394 default:
4395 gcc_unreachable ();
4398 if (FLOAT_WORDS_BIG_ENDIAN)
4399 buf[0] = image1, buf[1] = image0;
4400 else
4401 buf[0] = image0, buf[1] = image1;
4404 static void
4405 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4406 REAL_VALUE_TYPE *r, const long *buf)
4408 unsigned long image0, image1;
4409 int exp;
4411 if (FLOAT_WORDS_BIG_ENDIAN)
4412 image1 = buf[0], image0 = buf[1];
4413 else
4414 image0 = buf[0], image1 = buf[1];
4415 image0 &= 0xffffffff;
4416 image1 &= 0xffffffff;
4418 exp = (image0 >> 4) & 0x7ff;
4420 memset (r, 0, sizeof (*r));
4422 if (exp != 0)
4424 r->cl = rvc_normal;
4425 r->sign = (image0 >> 15) & 1;
4426 SET_REAL_EXP (r, exp - 1024);
4428 /* Rearrange the half-words of the external format into
4429 proper ascending order. */
4430 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4431 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4433 if (HOST_BITS_PER_LONG == 64)
4435 image0 = (image0 << 31 << 1) | image1;
4436 image0 <<= 64 - 53;
4437 image0 |= SIG_MSB;
4438 r->sig[SIGSZ-1] = image0;
4440 else
4442 r->sig[SIGSZ-1] = image0;
4443 r->sig[SIGSZ-2] = image1;
4444 lshift_significand (r, r, 64 - 53);
4445 r->sig[SIGSZ-1] |= SIG_MSB;
4450 const struct real_format vax_f_format =
4452 encode_vax_f,
4453 decode_vax_f,
4457 -127,
4458 127,
4461 false,
4462 false,
4463 false,
4464 false,
4465 false,
4466 false,
4467 false,
4468 false,
4469 "vax_f"
4472 const struct real_format vax_d_format =
4474 encode_vax_d,
4475 decode_vax_d,
4479 -127,
4480 127,
4483 false,
4484 false,
4485 false,
4486 false,
4487 false,
4488 false,
4489 false,
4490 false,
4491 "vax_d"
4494 const struct real_format vax_g_format =
4496 encode_vax_g,
4497 decode_vax_g,
4501 -1023,
4502 1023,
4505 false,
4506 false,
4507 false,
4508 false,
4509 false,
4510 false,
4511 false,
4512 false,
4513 "vax_g"
4516 /* Encode real R into a single precision DFP value in BUF. */
4517 static void
4518 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4519 long *buf ATTRIBUTE_UNUSED,
4520 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4522 encode_decimal32 (fmt, buf, r);
4525 /* Decode a single precision DFP value in BUF into a real R. */
4526 static void
4527 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4528 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4529 const long *buf ATTRIBUTE_UNUSED)
4531 decode_decimal32 (fmt, r, buf);
4534 /* Encode real R into a double precision DFP value in BUF. */
4535 static void
4536 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4537 long *buf ATTRIBUTE_UNUSED,
4538 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4540 encode_decimal64 (fmt, buf, r);
4543 /* Decode a double precision DFP value in BUF into a real R. */
4544 static void
4545 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4546 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4547 const long *buf ATTRIBUTE_UNUSED)
4549 decode_decimal64 (fmt, r, buf);
4552 /* Encode real R into a quad precision DFP value in BUF. */
4553 static void
4554 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4555 long *buf ATTRIBUTE_UNUSED,
4556 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4558 encode_decimal128 (fmt, buf, r);
4561 /* Decode a quad precision DFP value in BUF into a real R. */
4562 static void
4563 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4564 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4565 const long *buf ATTRIBUTE_UNUSED)
4567 decode_decimal128 (fmt, r, buf);
4570 /* Single precision decimal floating point (IEEE 754). */
4571 const struct real_format decimal_single_format =
4573 encode_decimal_single,
4574 decode_decimal_single,
4578 -94,
4582 false,
4583 true,
4584 true,
4585 true,
4586 true,
4587 true,
4588 true,
4589 false,
4590 "decimal_single"
4593 /* Double precision decimal floating point (IEEE 754). */
4594 const struct real_format decimal_double_format =
4596 encode_decimal_double,
4597 decode_decimal_double,
4601 -382,
4602 385,
4605 false,
4606 true,
4607 true,
4608 true,
4609 true,
4610 true,
4611 true,
4612 false,
4613 "decimal_double"
4616 /* Quad precision decimal floating point (IEEE 754). */
4617 const struct real_format decimal_quad_format =
4619 encode_decimal_quad,
4620 decode_decimal_quad,
4624 -6142,
4625 6145,
4626 127,
4627 127,
4628 false,
4629 true,
4630 true,
4631 true,
4632 true,
4633 true,
4634 true,
4635 false,
4636 "decimal_quad"
4639 /* Encode half-precision floats. This routine is used both for the IEEE
4640 ARM alternative encodings. */
4641 static void
4642 encode_ieee_half (const struct real_format *fmt, long *buf,
4643 const REAL_VALUE_TYPE *r)
4645 unsigned long image, sig, exp;
4646 unsigned long sign = r->sign;
4647 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
4649 image = sign << 15;
4650 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4652 switch (r->cl)
4654 case rvc_zero:
4655 break;
4657 case rvc_inf:
4658 if (fmt->has_inf)
4659 image |= 31 << 10;
4660 else
4661 image |= 0x7fff;
4662 break;
4664 case rvc_nan:
4665 if (fmt->has_nans)
4667 if (r->canonical)
4668 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4669 if (r->signalling == fmt->qnan_msb_set)
4670 sig &= ~(1 << 9);
4671 else
4672 sig |= 1 << 9;
4673 if (sig == 0)
4674 sig = 1 << 8;
4676 image |= 31 << 10;
4677 image |= sig;
4679 else
4680 image |= 0x3ff;
4681 break;
4683 case rvc_normal:
4684 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4685 whereas the intermediate representation is 0.F x 2**exp.
4686 Which means we're off by one. */
4687 if (denormal)
4688 exp = 0;
4689 else
4690 exp = REAL_EXP (r) + 15 - 1;
4691 image |= exp << 10;
4692 image |= sig;
4693 break;
4695 default:
4696 gcc_unreachable ();
4699 buf[0] = image;
4702 /* Decode half-precision floats. This routine is used both for the IEEE
4703 ARM alternative encodings. */
4704 static void
4705 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4706 const long *buf)
4708 unsigned long image = buf[0] & 0xffff;
4709 bool sign = (image >> 15) & 1;
4710 int exp = (image >> 10) & 0x1f;
4712 memset (r, 0, sizeof (*r));
4713 image <<= HOST_BITS_PER_LONG - 11;
4714 image &= ~SIG_MSB;
4716 if (exp == 0)
4718 if (image && fmt->has_denorm)
4720 r->cl = rvc_normal;
4721 r->sign = sign;
4722 SET_REAL_EXP (r, -14);
4723 r->sig[SIGSZ-1] = image << 1;
4724 normalize (r);
4726 else if (fmt->has_signed_zero)
4727 r->sign = sign;
4729 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4731 if (image)
4733 r->cl = rvc_nan;
4734 r->sign = sign;
4735 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4736 ^ fmt->qnan_msb_set);
4737 r->sig[SIGSZ-1] = image;
4739 else
4741 r->cl = rvc_inf;
4742 r->sign = sign;
4745 else
4747 r->cl = rvc_normal;
4748 r->sign = sign;
4749 SET_REAL_EXP (r, exp - 15 + 1);
4750 r->sig[SIGSZ-1] = image | SIG_MSB;
4754 /* Half-precision format, as specified in IEEE 754R. */
4755 const struct real_format ieee_half_format =
4757 encode_ieee_half,
4758 decode_ieee_half,
4762 -13,
4766 false,
4767 true,
4768 true,
4769 true,
4770 true,
4771 true,
4772 true,
4773 false,
4774 "ieee_half"
4777 /* ARM's alternative half-precision format, similar to IEEE but with
4778 no reserved exponent value for NaNs and infinities; rather, it just
4779 extends the range of exponents by one. */
4780 const struct real_format arm_half_format =
4782 encode_ieee_half,
4783 decode_ieee_half,
4787 -13,
4791 false,
4792 true,
4793 false,
4794 false,
4795 true,
4796 true,
4797 false,
4798 false,
4799 "arm_half"
4802 /* A synthetic "format" for internal arithmetic. It's the size of the
4803 internal significand minus the two bits needed for proper rounding.
4804 The encode and decode routines exist only to satisfy our paranoia
4805 harness. */
4807 static void encode_internal (const struct real_format *fmt,
4808 long *, const REAL_VALUE_TYPE *);
4809 static void decode_internal (const struct real_format *,
4810 REAL_VALUE_TYPE *, const long *);
4812 static void
4813 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4814 const REAL_VALUE_TYPE *r)
4816 memcpy (buf, r, sizeof (*r));
4819 static void
4820 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4821 REAL_VALUE_TYPE *r, const long *buf)
4823 memcpy (r, buf, sizeof (*r));
4826 const struct real_format real_internal_format =
4828 encode_internal,
4829 decode_internal,
4831 SIGNIFICAND_BITS - 2,
4832 SIGNIFICAND_BITS - 2,
4833 -MAX_EXP,
4834 MAX_EXP,
4837 false,
4838 false,
4839 true,
4840 true,
4841 false,
4842 true,
4843 true,
4844 false,
4845 "real_internal"
4848 /* Calculate X raised to the integer exponent N in mode MODE and store
4849 the result in R. Return true if the result may be inexact due to
4850 loss of precision. The algorithm is the classic "left-to-right binary
4851 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4852 Algorithms", "The Art of Computer Programming", Volume 2. */
4854 bool
4855 real_powi (REAL_VALUE_TYPE *r, machine_mode mode,
4856 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4858 unsigned HOST_WIDE_INT bit;
4859 REAL_VALUE_TYPE t;
4860 bool inexact = false;
4861 bool init = false;
4862 bool neg;
4863 int i;
4865 if (n == 0)
4867 *r = dconst1;
4868 return false;
4870 else if (n < 0)
4872 /* Don't worry about overflow, from now on n is unsigned. */
4873 neg = true;
4874 n = -n;
4876 else
4877 neg = false;
4879 t = *x;
4880 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4881 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4883 if (init)
4885 inexact |= do_multiply (&t, &t, &t);
4886 if (n & bit)
4887 inexact |= do_multiply (&t, &t, x);
4889 else if (n & bit)
4890 init = true;
4891 bit >>= 1;
4894 if (neg)
4895 inexact |= do_divide (&t, &dconst1, &t);
4897 real_convert (r, mode, &t);
4898 return inexact;
4901 /* Round X to the nearest integer not larger in absolute value, i.e.
4902 towards zero, placing the result in R in mode MODE. */
4904 void
4905 real_trunc (REAL_VALUE_TYPE *r, machine_mode mode,
4906 const REAL_VALUE_TYPE *x)
4908 do_fix_trunc (r, x);
4909 if (mode != VOIDmode)
4910 real_convert (r, mode, r);
4913 /* Round X to the largest integer not greater in value, i.e. round
4914 down, placing the result in R in mode MODE. */
4916 void
4917 real_floor (REAL_VALUE_TYPE *r, machine_mode mode,
4918 const REAL_VALUE_TYPE *x)
4920 REAL_VALUE_TYPE t;
4922 do_fix_trunc (&t, x);
4923 if (! real_identical (&t, x) && x->sign)
4924 do_add (&t, &t, &dconstm1, 0);
4925 if (mode != VOIDmode)
4926 real_convert (r, mode, &t);
4927 else
4928 *r = t;
4931 /* Round X to the smallest integer not less then argument, i.e. round
4932 up, placing the result in R in mode MODE. */
4934 void
4935 real_ceil (REAL_VALUE_TYPE *r, machine_mode mode,
4936 const REAL_VALUE_TYPE *x)
4938 REAL_VALUE_TYPE t;
4940 do_fix_trunc (&t, x);
4941 if (! real_identical (&t, x) && ! x->sign)
4942 do_add (&t, &t, &dconst1, 0);
4943 if (mode != VOIDmode)
4944 real_convert (r, mode, &t);
4945 else
4946 *r = t;
4949 /* Round X to the nearest integer, but round halfway cases away from
4950 zero. */
4952 void
4953 real_round (REAL_VALUE_TYPE *r, machine_mode mode,
4954 const REAL_VALUE_TYPE *x)
4956 do_add (r, x, &dconsthalf, x->sign);
4957 do_fix_trunc (r, r);
4958 if (mode != VOIDmode)
4959 real_convert (r, mode, r);
4962 /* Set the sign of R to the sign of X. */
4964 void
4965 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4967 r->sign = x->sign;
4970 /* Check whether the real constant value given is an integer. */
4972 bool
4973 real_isinteger (const REAL_VALUE_TYPE *c, machine_mode mode)
4975 REAL_VALUE_TYPE cint;
4977 real_trunc (&cint, mode, c);
4978 return real_identical (c, &cint);
4981 /* Write into BUF the maximum representable finite floating-point
4982 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4983 float string. LEN is the size of BUF, and the buffer must be large
4984 enough to contain the resulting string. */
4986 void
4987 get_max_float (const struct real_format *fmt, char *buf, size_t len)
4989 int i, n;
4990 char *p;
4992 strcpy (buf, "0x0.");
4993 n = fmt->p;
4994 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
4995 *p++ = 'f';
4996 if (i < n)
4997 *p++ = "08ce"[n - i];
4998 sprintf (p, "p%d", fmt->emax);
4999 if (fmt->pnan < fmt->p)
5001 /* This is an IBM extended double format made up of two IEEE
5002 doubles. The value of the long double is the sum of the
5003 values of the two parts. The most significant part is
5004 required to be the value of the long double rounded to the
5005 nearest double. Rounding means we need a slightly smaller
5006 value for LDBL_MAX. */
5007 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5010 gcc_assert (strlen (buf) < len);
5013 /* True if mode M has a NaN representation and
5014 the treatment of NaN operands is important. */
5016 bool
5017 HONOR_NANS (machine_mode m)
5019 return MODE_HAS_NANS (m) && !flag_finite_math_only;
5022 bool
5023 HONOR_NANS (const_tree t)
5025 return HONOR_NANS (element_mode (t));
5028 bool
5029 HONOR_NANS (const_rtx x)
5031 return HONOR_NANS (GET_MODE (x));
5034 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5036 bool
5037 HONOR_SNANS (machine_mode m)
5039 return flag_signaling_nans && HONOR_NANS (m);
5042 bool
5043 HONOR_SNANS (const_tree t)
5045 return HONOR_SNANS (element_mode (t));
5048 bool
5049 HONOR_SNANS (const_rtx x)
5051 return HONOR_SNANS (GET_MODE (x));
5054 /* As for HONOR_NANS, but true if the mode can represent infinity and
5055 the treatment of infinite values is important. */
5057 bool
5058 HONOR_INFINITIES (machine_mode m)
5060 return MODE_HAS_INFINITIES (m) && !flag_finite_math_only;
5063 bool
5064 HONOR_INFINITIES (const_tree t)
5066 return HONOR_INFINITIES (element_mode (t));
5069 bool
5070 HONOR_INFINITIES (const_rtx x)
5072 return HONOR_INFINITIES (GET_MODE (x));
5075 /* Like HONOR_NANS, but true if the given mode distinguishes between
5076 positive and negative zero, and the sign of zero is important. */
5078 bool
5079 HONOR_SIGNED_ZEROS (machine_mode m)
5081 return MODE_HAS_SIGNED_ZEROS (m) && flag_signed_zeros;
5084 bool
5085 HONOR_SIGNED_ZEROS (const_tree t)
5087 return HONOR_SIGNED_ZEROS (element_mode (t));
5090 bool
5091 HONOR_SIGNED_ZEROS (const_rtx x)
5093 return HONOR_SIGNED_ZEROS (GET_MODE (x));
5096 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5097 and the rounding mode is important. */
5099 bool
5100 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m)
5102 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m) && flag_rounding_math;
5105 bool
5106 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t)
5108 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t));
5111 bool
5112 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x)
5114 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x));